cert_v2.go 18 KB

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