cert_v2.go 19 KB

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