cert_v2.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. package cert
  2. import (
  3. "bytes"
  4. "crypto/ecdh"
  5. "crypto/ecdsa"
  6. "crypto/ed25519"
  7. "crypto/elliptic"
  8. "crypto/sha256"
  9. "encoding/hex"
  10. "encoding/json"
  11. "encoding/pem"
  12. "fmt"
  13. "net/netip"
  14. "slices"
  15. "time"
  16. "golang.org/x/crypto/cryptobyte"
  17. "golang.org/x/crypto/cryptobyte/asn1"
  18. "golang.org/x/crypto/curve25519"
  19. )
  20. const (
  21. classConstructed = 0x20
  22. classContextSpecific = 0x80
  23. TagCertDetails = 0 | classConstructed | classContextSpecific
  24. TagCertCurve = 1 | classContextSpecific
  25. TagCertPublicKey = 2 | classContextSpecific
  26. TagCertSignature = 3 | classContextSpecific
  27. TagDetailsName = 0 | classContextSpecific
  28. TagDetailsNetworks = 1 | classConstructed | classContextSpecific
  29. TagDetailsUnsafeNetworks = 2 | classConstructed | classContextSpecific
  30. TagDetailsGroups = 3 | classConstructed | classContextSpecific
  31. TagDetailsIsCA = 4 | classContextSpecific
  32. TagDetailsNotBefore = 5 | classContextSpecific
  33. TagDetailsNotAfter = 6 | classContextSpecific
  34. TagDetailsIssuer = 7 | classContextSpecific
  35. )
  36. const (
  37. // MaxCertificateSize is the maximum length a valid certificate can be
  38. MaxCertificateSize = 65536
  39. // MaxNameLength is limited to a maximum realistic DNS domain name to help facilitate DNS systems
  40. MaxNameLength = 253
  41. // MaxNetworkLength is the maximum length a network value can be.
  42. // 16 bytes for an ipv6 address + 1 byte for the prefix length
  43. MaxNetworkLength = 17
  44. )
  45. type certificateV2 struct {
  46. details detailsV2
  47. // RawDetails contains the entire asn.1 DER encoded Details struct
  48. // This is to benefit forwards compatibility in signature checking.
  49. // signature(RawDetails + Curve + PublicKey) == Signature
  50. rawDetails []byte
  51. curve Curve
  52. publicKey []byte
  53. signature []byte
  54. }
  55. type detailsV2 struct {
  56. name string
  57. networks []netip.Prefix // MUST BE SORTED
  58. unsafeNetworks []netip.Prefix // MUST BE SORTED
  59. groups []string
  60. isCA bool
  61. notBefore time.Time
  62. notAfter time.Time
  63. issuer string
  64. }
  65. func (c *certificateV2) Version() Version {
  66. return Version2
  67. }
  68. func (c *certificateV2) Curve() Curve {
  69. return c.curve
  70. }
  71. func (c *certificateV2) Groups() []string {
  72. return c.details.groups
  73. }
  74. func (c *certificateV2) IsCA() bool {
  75. return c.details.isCA
  76. }
  77. func (c *certificateV2) Issuer() string {
  78. return c.details.issuer
  79. }
  80. func (c *certificateV2) Name() string {
  81. return c.details.name
  82. }
  83. func (c *certificateV2) Networks() []netip.Prefix {
  84. return c.details.networks
  85. }
  86. func (c *certificateV2) NotAfter() time.Time {
  87. return c.details.notAfter
  88. }
  89. func (c *certificateV2) NotBefore() time.Time {
  90. return c.details.notBefore
  91. }
  92. func (c *certificateV2) PublicKey() []byte {
  93. return c.publicKey
  94. }
  95. func (c *certificateV2) Signature() []byte {
  96. return c.signature
  97. }
  98. func (c *certificateV2) UnsafeNetworks() []netip.Prefix {
  99. return c.details.unsafeNetworks
  100. }
  101. func (c *certificateV2) Fingerprint() (string, error) {
  102. if len(c.rawDetails) == 0 {
  103. return "", ErrMissingDetails
  104. }
  105. b := make([]byte, len(c.rawDetails)+1+len(c.publicKey)+len(c.signature))
  106. copy(b, c.rawDetails)
  107. b[len(c.rawDetails)] = byte(c.curve)
  108. copy(b[len(c.rawDetails)+1:], c.publicKey)
  109. copy(b[len(c.rawDetails)+1+len(c.publicKey):], c.signature)
  110. sum := sha256.Sum256(b)
  111. return hex.EncodeToString(sum[:]), nil
  112. }
  113. func (c *certificateV2) CheckSignature(key []byte) bool {
  114. if len(c.rawDetails) == 0 {
  115. return false
  116. }
  117. b := make([]byte, len(c.rawDetails)+1+len(c.publicKey))
  118. copy(b, c.rawDetails)
  119. b[len(c.rawDetails)] = byte(c.curve)
  120. copy(b[len(c.rawDetails)+1:], c.publicKey)
  121. switch c.curve {
  122. case Curve_CURVE25519:
  123. return ed25519.Verify(key, b, c.signature)
  124. case Curve_P256:
  125. x, y := elliptic.Unmarshal(elliptic.P256(), key)
  126. pubKey := &ecdsa.PublicKey{Curve: elliptic.P256(), X: x, Y: y}
  127. hashed := sha256.Sum256(b)
  128. return ecdsa.VerifyASN1(pubKey, hashed[:], c.signature)
  129. default:
  130. return false
  131. }
  132. }
  133. func (c *certificateV2) Expired(t time.Time) bool {
  134. return c.details.notBefore.After(t) || c.details.notAfter.Before(t)
  135. }
  136. func (c *certificateV2) VerifyPrivateKey(curve Curve, key []byte) error {
  137. if curve != c.curve {
  138. return ErrPublicPrivateCurveMismatch
  139. }
  140. if c.details.isCA {
  141. switch curve {
  142. case Curve_CURVE25519:
  143. // the call to PublicKey below will panic slice bounds out of range otherwise
  144. if len(key) != ed25519.PrivateKeySize {
  145. return ErrInvalidPrivateKey
  146. }
  147. if !ed25519.PublicKey(c.publicKey).Equal(ed25519.PrivateKey(key).Public()) {
  148. return ErrPublicPrivateKeyMismatch
  149. }
  150. case Curve_P256:
  151. privkey, err := ecdh.P256().NewPrivateKey(key)
  152. if err != nil {
  153. return ErrInvalidPrivateKey
  154. }
  155. pub := privkey.PublicKey().Bytes()
  156. if !bytes.Equal(pub, c.publicKey) {
  157. return ErrPublicPrivateKeyMismatch
  158. }
  159. default:
  160. return fmt.Errorf("invalid curve: %s", curve)
  161. }
  162. return nil
  163. }
  164. var pub []byte
  165. switch curve {
  166. case Curve_CURVE25519:
  167. var err error
  168. pub, err = curve25519.X25519(key, curve25519.Basepoint)
  169. if err != nil {
  170. return ErrInvalidPrivateKey
  171. }
  172. case Curve_P256:
  173. privkey, err := ecdh.P256().NewPrivateKey(key)
  174. if err != nil {
  175. return ErrInvalidPrivateKey
  176. }
  177. pub = privkey.PublicKey().Bytes()
  178. default:
  179. return fmt.Errorf("invalid curve: %s", curve)
  180. }
  181. if !bytes.Equal(pub, c.publicKey) {
  182. return ErrPublicPrivateKeyMismatch
  183. }
  184. return nil
  185. }
  186. func (c *certificateV2) String() string {
  187. mb, err := c.marshalJSON()
  188. if err != nil {
  189. return fmt.Sprintf("<error marshalling certificate: %v>", err)
  190. }
  191. b, err := json.MarshalIndent(mb, "", "\t")
  192. if err != nil {
  193. return fmt.Sprintf("<error marshalling certificate: %v>", err)
  194. }
  195. return string(b)
  196. }
  197. func (c *certificateV2) MarshalForHandshakes() ([]byte, error) {
  198. if c.rawDetails == nil {
  199. return nil, ErrEmptyRawDetails
  200. }
  201. var b cryptobyte.Builder
  202. // Outermost certificate
  203. b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
  204. // Add the cert details which is already marshalled
  205. b.AddBytes(c.rawDetails)
  206. // Skipping the curve and public key since those come across in a different part of the handshake
  207. // Add the signature
  208. b.AddASN1(TagCertSignature, func(b *cryptobyte.Builder) {
  209. b.AddBytes(c.signature)
  210. })
  211. })
  212. return b.Bytes()
  213. }
  214. func (c *certificateV2) Marshal() ([]byte, error) {
  215. if c.rawDetails == nil {
  216. return nil, ErrEmptyRawDetails
  217. }
  218. var b cryptobyte.Builder
  219. // Outermost certificate
  220. b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
  221. // Add the cert details which is already marshalled
  222. b.AddBytes(c.rawDetails)
  223. // Add the curve only if its not the default value
  224. if c.curve != Curve_CURVE25519 {
  225. b.AddASN1(TagCertCurve, func(b *cryptobyte.Builder) {
  226. b.AddBytes([]byte{byte(c.curve)})
  227. })
  228. }
  229. // Add the public key if it is not empty
  230. if c.publicKey != nil {
  231. b.AddASN1(TagCertPublicKey, func(b *cryptobyte.Builder) {
  232. b.AddBytes(c.publicKey)
  233. })
  234. }
  235. // Add the signature
  236. b.AddASN1(TagCertSignature, func(b *cryptobyte.Builder) {
  237. b.AddBytes(c.signature)
  238. })
  239. })
  240. return b.Bytes()
  241. }
  242. func (c *certificateV2) MarshalPEM() ([]byte, error) {
  243. b, err := c.Marshal()
  244. if err != nil {
  245. return nil, err
  246. }
  247. return pem.EncodeToMemory(&pem.Block{Type: CertificateV2Banner, Bytes: b}), nil
  248. }
  249. func (c *certificateV2) MarshalJSON() ([]byte, error) {
  250. b, err := c.marshalJSON()
  251. if err != nil {
  252. return nil, err
  253. }
  254. return json.Marshal(b)
  255. }
  256. func (c *certificateV2) marshalJSON() (m, error) {
  257. fp, err := c.Fingerprint()
  258. if err != nil {
  259. return nil, err
  260. }
  261. return m{
  262. "details": m{
  263. "name": c.details.name,
  264. "networks": c.details.networks,
  265. "unsafeNetworks": c.details.unsafeNetworks,
  266. "groups": c.details.groups,
  267. "notBefore": c.details.notBefore,
  268. "notAfter": c.details.notAfter,
  269. "isCa": c.details.isCA,
  270. "issuer": c.details.issuer,
  271. },
  272. "version": Version2,
  273. "publicKey": fmt.Sprintf("%x", c.publicKey),
  274. "curve": c.curve.String(),
  275. "fingerprint": fp,
  276. "signature": fmt.Sprintf("%x", c.Signature()),
  277. }, nil
  278. }
  279. func (c *certificateV2) Copy() Certificate {
  280. nc := &certificateV2{
  281. details: detailsV2{
  282. name: c.details.name,
  283. notBefore: c.details.notBefore,
  284. notAfter: c.details.notAfter,
  285. isCA: c.details.isCA,
  286. issuer: c.details.issuer,
  287. },
  288. curve: c.curve,
  289. publicKey: make([]byte, len(c.publicKey)),
  290. signature: make([]byte, len(c.signature)),
  291. rawDetails: make([]byte, len(c.rawDetails)),
  292. }
  293. if c.details.groups != nil {
  294. nc.details.groups = make([]string, len(c.details.groups))
  295. copy(nc.details.groups, c.details.groups)
  296. }
  297. if c.details.networks != nil {
  298. nc.details.networks = make([]netip.Prefix, len(c.details.networks))
  299. copy(nc.details.networks, c.details.networks)
  300. }
  301. if c.details.unsafeNetworks != nil {
  302. nc.details.unsafeNetworks = make([]netip.Prefix, len(c.details.unsafeNetworks))
  303. copy(nc.details.unsafeNetworks, c.details.unsafeNetworks)
  304. }
  305. copy(nc.rawDetails, c.rawDetails)
  306. copy(nc.signature, c.signature)
  307. copy(nc.publicKey, c.publicKey)
  308. return nc
  309. }
  310. func (c *certificateV2) fromTBSCertificate(t *TBSCertificate) error {
  311. c.details = detailsV2{
  312. name: t.Name,
  313. networks: t.Networks,
  314. unsafeNetworks: t.UnsafeNetworks,
  315. groups: t.Groups,
  316. isCA: t.IsCA,
  317. notBefore: t.NotBefore,
  318. notAfter: t.NotAfter,
  319. issuer: t.issuer,
  320. }
  321. c.curve = t.Curve
  322. c.publicKey = t.PublicKey
  323. return c.validate()
  324. }
  325. func (c *certificateV2) validate() error {
  326. // Empty names are allowed
  327. if len(c.publicKey) == 0 {
  328. return ErrInvalidPublicKey
  329. }
  330. if !c.details.isCA && len(c.details.networks) == 0 {
  331. return NewErrInvalidCertificateProperties("non-CA certificate must contain at least 1 network")
  332. }
  333. hasV4Networks := false
  334. hasV6Networks := false
  335. for _, network := range c.details.networks {
  336. if !network.IsValid() || !network.Addr().IsValid() {
  337. return NewErrInvalidCertificateProperties("invalid network: %s", network)
  338. }
  339. if network.Addr().IsUnspecified() {
  340. return NewErrInvalidCertificateProperties("non-CA certificates must not use the zero address as a network: %s", network)
  341. }
  342. if network.Addr().Zone() != "" {
  343. return NewErrInvalidCertificateProperties("networks may not contain zones: %s", network)
  344. }
  345. if network.Addr().Is4In6() {
  346. return NewErrInvalidCertificateProperties("4in6 networks are not allowed: %s", network)
  347. }
  348. hasV4Networks = hasV4Networks || network.Addr().Is4()
  349. hasV6Networks = hasV6Networks || network.Addr().Is6()
  350. }
  351. slices.SortFunc(c.details.networks, comparePrefix)
  352. err := findDuplicatePrefix(c.details.networks)
  353. if err != nil {
  354. return err
  355. }
  356. for _, network := range c.details.unsafeNetworks {
  357. if !network.IsValid() || !network.Addr().IsValid() {
  358. return NewErrInvalidCertificateProperties("invalid unsafe network: %s", network)
  359. }
  360. if network.Addr().Zone() != "" {
  361. return NewErrInvalidCertificateProperties("unsafe networks may not contain zones: %s", network)
  362. }
  363. if !c.details.isCA {
  364. if network.Addr().Is6() {
  365. if !hasV6Networks {
  366. return NewErrInvalidCertificateProperties("IPv6 unsafe networks require an IPv6 address assignment: %s", network)
  367. }
  368. } else if network.Addr().Is4() {
  369. if !hasV4Networks {
  370. return NewErrInvalidCertificateProperties("IPv4 unsafe networks require an IPv4 address assignment: %s", network)
  371. }
  372. }
  373. }
  374. }
  375. slices.SortFunc(c.details.unsafeNetworks, comparePrefix)
  376. err = findDuplicatePrefix(c.details.unsafeNetworks)
  377. if err != nil {
  378. return err
  379. }
  380. return nil
  381. }
  382. func (c *certificateV2) marshalForSigning() ([]byte, error) {
  383. d, err := c.details.Marshal()
  384. if err != nil {
  385. return nil, fmt.Errorf("marshalling certificate details failed: %w", err)
  386. }
  387. c.rawDetails = d
  388. b := make([]byte, len(c.rawDetails)+1+len(c.publicKey))
  389. copy(b, c.rawDetails)
  390. b[len(c.rawDetails)] = byte(c.curve)
  391. copy(b[len(c.rawDetails)+1:], c.publicKey)
  392. return b, nil
  393. }
  394. func (c *certificateV2) setSignature(b []byte) error {
  395. if len(b) == 0 {
  396. return ErrEmptySignature
  397. }
  398. c.signature = b
  399. return nil
  400. }
  401. func (d *detailsV2) Marshal() ([]byte, error) {
  402. var b cryptobyte.Builder
  403. var err error
  404. // Details are a structure
  405. b.AddASN1(TagCertDetails, func(b *cryptobyte.Builder) {
  406. // Add the name
  407. b.AddASN1(TagDetailsName, func(b *cryptobyte.Builder) {
  408. b.AddBytes([]byte(d.name))
  409. })
  410. // Add the networks if any exist
  411. if len(d.networks) > 0 {
  412. b.AddASN1(TagDetailsNetworks, func(b *cryptobyte.Builder) {
  413. for _, n := range d.networks {
  414. sb, innerErr := n.MarshalBinary()
  415. if innerErr != nil {
  416. // MarshalBinary never returns an error
  417. err = fmt.Errorf("unable to marshal network: %w", innerErr)
  418. return
  419. }
  420. b.AddASN1OctetString(sb)
  421. }
  422. })
  423. }
  424. // Add the unsafe networks if any exist
  425. if len(d.unsafeNetworks) > 0 {
  426. b.AddASN1(TagDetailsUnsafeNetworks, func(b *cryptobyte.Builder) {
  427. for _, n := range d.unsafeNetworks {
  428. sb, innerErr := n.MarshalBinary()
  429. if innerErr != nil {
  430. // MarshalBinary never returns an error
  431. err = fmt.Errorf("unable to marshal unsafe network: %w", innerErr)
  432. return
  433. }
  434. b.AddASN1OctetString(sb)
  435. }
  436. })
  437. }
  438. // Add groups if any exist
  439. if len(d.groups) > 0 {
  440. b.AddASN1(TagDetailsGroups, func(b *cryptobyte.Builder) {
  441. for _, group := range d.groups {
  442. b.AddASN1(asn1.UTF8String, func(b *cryptobyte.Builder) {
  443. b.AddBytes([]byte(group))
  444. })
  445. }
  446. })
  447. }
  448. // Add IsCA only if true
  449. if d.isCA {
  450. b.AddASN1(TagDetailsIsCA, func(b *cryptobyte.Builder) {
  451. b.AddUint8(0xff)
  452. })
  453. }
  454. // Add not before
  455. b.AddASN1Int64WithTag(d.notBefore.Unix(), TagDetailsNotBefore)
  456. // Add not after
  457. b.AddASN1Int64WithTag(d.notAfter.Unix(), TagDetailsNotAfter)
  458. // Add the issuer if present
  459. if d.issuer != "" {
  460. issuerBytes, innerErr := hex.DecodeString(d.issuer)
  461. if innerErr != nil {
  462. err = fmt.Errorf("failed to decode issuer: %w", innerErr)
  463. return
  464. }
  465. b.AddASN1(TagDetailsIssuer, func(b *cryptobyte.Builder) {
  466. b.AddBytes(issuerBytes)
  467. })
  468. }
  469. })
  470. if err != nil {
  471. return nil, err
  472. }
  473. return b.Bytes()
  474. }
  475. func unmarshalCertificateV2(b []byte, publicKey []byte, curve Curve) (*certificateV2, error) {
  476. l := len(b)
  477. if l == 0 || l > MaxCertificateSize {
  478. return nil, ErrBadFormat
  479. }
  480. input := cryptobyte.String(b)
  481. // Open the envelope
  482. if !input.ReadASN1(&input, asn1.SEQUENCE) || input.Empty() {
  483. return nil, ErrBadFormat
  484. }
  485. // Grab the cert details, we need to preserve the tag and length
  486. var rawDetails cryptobyte.String
  487. if !input.ReadASN1Element(&rawDetails, TagCertDetails) || rawDetails.Empty() {
  488. return nil, ErrBadFormat
  489. }
  490. //Maybe grab the curve
  491. var rawCurve byte
  492. if !readOptionalASN1Byte(&input, &rawCurve, TagCertCurve, byte(curve)) {
  493. return nil, ErrBadFormat
  494. }
  495. curve = Curve(rawCurve)
  496. // Maybe grab the public key
  497. var rawPublicKey cryptobyte.String
  498. if len(publicKey) > 0 {
  499. rawPublicKey = publicKey
  500. } else if !input.ReadOptionalASN1(&rawPublicKey, nil, TagCertPublicKey) {
  501. return nil, ErrBadFormat
  502. }
  503. if len(rawPublicKey) == 0 {
  504. return nil, ErrBadFormat
  505. }
  506. // Grab the signature
  507. var rawSignature cryptobyte.String
  508. if !input.ReadASN1(&rawSignature, TagCertSignature) || rawSignature.Empty() {
  509. return nil, ErrBadFormat
  510. }
  511. // Finally unmarshal the details
  512. details, err := unmarshalDetails(rawDetails)
  513. if err != nil {
  514. return nil, err
  515. }
  516. c := &certificateV2{
  517. details: details,
  518. rawDetails: rawDetails,
  519. curve: curve,
  520. publicKey: rawPublicKey,
  521. signature: rawSignature,
  522. }
  523. err = c.validate()
  524. if err != nil {
  525. return nil, err
  526. }
  527. return c, nil
  528. }
  529. func unmarshalDetails(b cryptobyte.String) (detailsV2, error) {
  530. // Open the envelope
  531. if !b.ReadASN1(&b, TagCertDetails) || b.Empty() {
  532. return detailsV2{}, ErrBadFormat
  533. }
  534. // Read the name
  535. var name cryptobyte.String
  536. if !b.ReadASN1(&name, TagDetailsName) || name.Empty() || len(name) > MaxNameLength {
  537. return detailsV2{}, ErrBadFormat
  538. }
  539. // Read the network addresses
  540. var subString cryptobyte.String
  541. var found bool
  542. if !b.ReadOptionalASN1(&subString, &found, TagDetailsNetworks) {
  543. return detailsV2{}, ErrBadFormat
  544. }
  545. var networks []netip.Prefix
  546. var val cryptobyte.String
  547. if found {
  548. for !subString.Empty() {
  549. if !subString.ReadASN1(&val, asn1.OCTET_STRING) || val.Empty() || len(val) > MaxNetworkLength {
  550. return detailsV2{}, ErrBadFormat
  551. }
  552. var n netip.Prefix
  553. if err := n.UnmarshalBinary(val); err != nil {
  554. return detailsV2{}, ErrBadFormat
  555. }
  556. networks = append(networks, n)
  557. }
  558. }
  559. // Read out any unsafe networks
  560. if !b.ReadOptionalASN1(&subString, &found, TagDetailsUnsafeNetworks) {
  561. return detailsV2{}, ErrBadFormat
  562. }
  563. var unsafeNetworks []netip.Prefix
  564. if found {
  565. for !subString.Empty() {
  566. if !subString.ReadASN1(&val, asn1.OCTET_STRING) || val.Empty() || len(val) > MaxNetworkLength {
  567. return detailsV2{}, ErrBadFormat
  568. }
  569. var n netip.Prefix
  570. if err := n.UnmarshalBinary(val); err != nil {
  571. return detailsV2{}, ErrBadFormat
  572. }
  573. unsafeNetworks = append(unsafeNetworks, n)
  574. }
  575. }
  576. // Read out any groups
  577. if !b.ReadOptionalASN1(&subString, &found, TagDetailsGroups) {
  578. return detailsV2{}, ErrBadFormat
  579. }
  580. var groups []string
  581. if found {
  582. for !subString.Empty() {
  583. if !subString.ReadASN1(&val, asn1.UTF8String) || val.Empty() {
  584. return detailsV2{}, ErrBadFormat
  585. }
  586. groups = append(groups, string(val))
  587. }
  588. }
  589. // Read out IsCA
  590. var isCa bool
  591. if !readOptionalASN1Boolean(&b, &isCa, TagDetailsIsCA, false) {
  592. return detailsV2{}, ErrBadFormat
  593. }
  594. // Read not before and not after
  595. var notBefore int64
  596. if !b.ReadASN1Int64WithTag(&notBefore, TagDetailsNotBefore) {
  597. return detailsV2{}, ErrBadFormat
  598. }
  599. var notAfter int64
  600. if !b.ReadASN1Int64WithTag(&notAfter, TagDetailsNotAfter) {
  601. return detailsV2{}, ErrBadFormat
  602. }
  603. // Read issuer
  604. var issuer cryptobyte.String
  605. if !b.ReadOptionalASN1(&issuer, nil, TagDetailsIssuer) {
  606. return detailsV2{}, ErrBadFormat
  607. }
  608. return detailsV2{
  609. name: string(name),
  610. networks: networks,
  611. unsafeNetworks: unsafeNetworks,
  612. groups: groups,
  613. isCA: isCa,
  614. notBefore: time.Unix(notBefore, 0),
  615. notAfter: time.Unix(notAfter, 0),
  616. issuer: hex.EncodeToString(issuer),
  617. }, nil
  618. }