cert_v2.go 18 KB

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