1
0

cert_v2.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. // Empty names are allowed
  335. if len(c.publicKey) == 0 {
  336. return ErrInvalidPublicKey
  337. }
  338. if !c.details.isCA && len(c.details.networks) == 0 {
  339. return NewErrInvalidCertificateProperties("non-CA certificate must contain at least 1 network")
  340. }
  341. hasV4Networks := false
  342. hasV6Networks := false
  343. for _, network := range c.details.networks {
  344. if !network.IsValid() || !network.Addr().IsValid() {
  345. return NewErrInvalidCertificateProperties("invalid network: %s", network)
  346. }
  347. if network.Addr().IsUnspecified() {
  348. return NewErrInvalidCertificateProperties("non-CA certificates must not use the zero address as a network: %s", network)
  349. }
  350. if network.Addr().Zone() != "" {
  351. return NewErrInvalidCertificateProperties("networks may not contain zones: %s", network)
  352. }
  353. if network.Addr().Is4In6() {
  354. return NewErrInvalidCertificateProperties("4in6 networks are not allowed: %s", network)
  355. }
  356. hasV4Networks = hasV4Networks || network.Addr().Is4()
  357. hasV6Networks = hasV6Networks || network.Addr().Is6()
  358. }
  359. slices.SortFunc(c.details.networks, comparePrefix)
  360. err := findDuplicatePrefix(c.details.networks)
  361. if err != nil {
  362. return err
  363. }
  364. for _, network := range c.details.unsafeNetworks {
  365. if !network.IsValid() || !network.Addr().IsValid() {
  366. return NewErrInvalidCertificateProperties("invalid unsafe network: %s", network)
  367. }
  368. if network.Addr().Zone() != "" {
  369. return NewErrInvalidCertificateProperties("unsafe networks may not contain zones: %s", network)
  370. }
  371. if !c.details.isCA {
  372. if network.Addr().Is6() {
  373. if !hasV6Networks {
  374. return NewErrInvalidCertificateProperties("IPv6 unsafe networks require an IPv6 address assignment: %s", network)
  375. }
  376. } else if network.Addr().Is4() {
  377. if !hasV4Networks {
  378. return NewErrInvalidCertificateProperties("IPv4 unsafe networks require an IPv4 address assignment: %s", network)
  379. }
  380. }
  381. }
  382. }
  383. slices.SortFunc(c.details.unsafeNetworks, comparePrefix)
  384. err = findDuplicatePrefix(c.details.unsafeNetworks)
  385. if err != nil {
  386. return err
  387. }
  388. return nil
  389. }
  390. func (c *certificateV2) marshalForSigning() ([]byte, error) {
  391. d, err := c.details.Marshal()
  392. if err != nil {
  393. return nil, fmt.Errorf("marshalling certificate details failed: %w", err)
  394. }
  395. c.rawDetails = d
  396. b := make([]byte, len(c.rawDetails)+1+len(c.publicKey))
  397. copy(b, c.rawDetails)
  398. b[len(c.rawDetails)] = byte(c.curve)
  399. copy(b[len(c.rawDetails)+1:], c.publicKey)
  400. return b, nil
  401. }
  402. func (c *certificateV2) setSignature(b []byte) error {
  403. if len(b) == 0 {
  404. return ErrEmptySignature
  405. }
  406. c.signature = b
  407. return nil
  408. }
  409. func (d *detailsV2) Marshal() ([]byte, error) {
  410. var b cryptobyte.Builder
  411. var err error
  412. // Details are a structure
  413. b.AddASN1(TagCertDetails, func(b *cryptobyte.Builder) {
  414. // Add the name
  415. b.AddASN1(TagDetailsName, func(b *cryptobyte.Builder) {
  416. b.AddBytes([]byte(d.name))
  417. })
  418. // Add the networks if any exist
  419. if len(d.networks) > 0 {
  420. b.AddASN1(TagDetailsNetworks, func(b *cryptobyte.Builder) {
  421. for _, n := range d.networks {
  422. sb, innerErr := n.MarshalBinary()
  423. if innerErr != nil {
  424. // MarshalBinary never returns an error
  425. err = fmt.Errorf("unable to marshal network: %w", innerErr)
  426. return
  427. }
  428. b.AddASN1OctetString(sb)
  429. }
  430. })
  431. }
  432. // Add the unsafe networks if any exist
  433. if len(d.unsafeNetworks) > 0 {
  434. b.AddASN1(TagDetailsUnsafeNetworks, func(b *cryptobyte.Builder) {
  435. for _, n := range d.unsafeNetworks {
  436. sb, innerErr := n.MarshalBinary()
  437. if innerErr != nil {
  438. // MarshalBinary never returns an error
  439. err = fmt.Errorf("unable to marshal unsafe network: %w", innerErr)
  440. return
  441. }
  442. b.AddASN1OctetString(sb)
  443. }
  444. })
  445. }
  446. // Add groups if any exist
  447. if len(d.groups) > 0 {
  448. b.AddASN1(TagDetailsGroups, func(b *cryptobyte.Builder) {
  449. for _, group := range d.groups {
  450. b.AddASN1(asn1.UTF8String, func(b *cryptobyte.Builder) {
  451. b.AddBytes([]byte(group))
  452. })
  453. }
  454. })
  455. }
  456. // Add IsCA only if true
  457. if d.isCA {
  458. b.AddASN1(TagDetailsIsCA, func(b *cryptobyte.Builder) {
  459. b.AddUint8(0xff)
  460. })
  461. }
  462. // Add not before
  463. b.AddASN1Int64WithTag(d.notBefore.Unix(), TagDetailsNotBefore)
  464. // Add not after
  465. b.AddASN1Int64WithTag(d.notAfter.Unix(), TagDetailsNotAfter)
  466. // Add the issuer if present
  467. if d.issuer != "" {
  468. issuerBytes, innerErr := hex.DecodeString(d.issuer)
  469. if innerErr != nil {
  470. err = fmt.Errorf("failed to decode issuer: %w", innerErr)
  471. return
  472. }
  473. b.AddASN1(TagDetailsIssuer, func(b *cryptobyte.Builder) {
  474. b.AddBytes(issuerBytes)
  475. })
  476. }
  477. })
  478. if err != nil {
  479. return nil, err
  480. }
  481. return b.Bytes()
  482. }
  483. func unmarshalCertificateV2(b []byte, publicKey []byte, curve Curve) (*certificateV2, error) {
  484. l := len(b)
  485. if l == 0 || l > MaxCertificateSize {
  486. return nil, ErrBadFormat
  487. }
  488. input := cryptobyte.String(b)
  489. // Open the envelope
  490. if !input.ReadASN1(&input, asn1.SEQUENCE) || input.Empty() {
  491. return nil, ErrBadFormat
  492. }
  493. // Grab the cert details, we need to preserve the tag and length
  494. var rawDetails cryptobyte.String
  495. if !input.ReadASN1Element(&rawDetails, TagCertDetails) || rawDetails.Empty() {
  496. return nil, ErrBadFormat
  497. }
  498. //Maybe grab the curve
  499. var rawCurve byte
  500. if !readOptionalASN1Byte(&input, &rawCurve, TagCertCurve, byte(curve)) {
  501. return nil, ErrBadFormat
  502. }
  503. curve = Curve(rawCurve)
  504. // Maybe grab the public key
  505. var rawPublicKey cryptobyte.String
  506. if len(publicKey) > 0 {
  507. // If a public key is passed in, then the handshake certificate must
  508. // not have a public key present
  509. if input.PeekASN1Tag(TagCertPublicKey) {
  510. return nil, ErrCertPubkeyPresent
  511. }
  512. rawPublicKey = make(cryptobyte.String, len(publicKey))
  513. copy(rawPublicKey, publicKey)
  514. } else if !input.ReadOptionalASN1(&rawPublicKey, nil, TagCertPublicKey) {
  515. return nil, ErrBadFormat
  516. }
  517. if len(rawPublicKey) == 0 {
  518. return nil, ErrBadFormat
  519. }
  520. // Grab the signature
  521. var rawSignature cryptobyte.String
  522. if !input.ReadASN1(&rawSignature, TagCertSignature) || rawSignature.Empty() {
  523. return nil, ErrBadFormat
  524. }
  525. // Finally unmarshal the details
  526. details, err := unmarshalDetails(rawDetails)
  527. if err != nil {
  528. return nil, err
  529. }
  530. c := &certificateV2{
  531. details: details,
  532. rawDetails: rawDetails,
  533. curve: curve,
  534. publicKey: rawPublicKey,
  535. signature: rawSignature,
  536. }
  537. err = c.validate()
  538. if err != nil {
  539. return nil, err
  540. }
  541. return c, nil
  542. }
  543. func unmarshalDetails(b cryptobyte.String) (detailsV2, error) {
  544. // Open the envelope
  545. if !b.ReadASN1(&b, TagCertDetails) || b.Empty() {
  546. return detailsV2{}, ErrBadFormat
  547. }
  548. // Read the name
  549. var name cryptobyte.String
  550. if !b.ReadASN1(&name, TagDetailsName) || name.Empty() || len(name) > MaxNameLength {
  551. return detailsV2{}, ErrBadFormat
  552. }
  553. // Read the network addresses
  554. var subString cryptobyte.String
  555. var found bool
  556. if !b.ReadOptionalASN1(&subString, &found, TagDetailsNetworks) {
  557. return detailsV2{}, ErrBadFormat
  558. }
  559. var networks []netip.Prefix
  560. var val cryptobyte.String
  561. if found {
  562. for !subString.Empty() {
  563. if !subString.ReadASN1(&val, asn1.OCTET_STRING) || val.Empty() || len(val) > MaxNetworkLength {
  564. return detailsV2{}, ErrBadFormat
  565. }
  566. var n netip.Prefix
  567. if err := n.UnmarshalBinary(val); err != nil {
  568. return detailsV2{}, ErrBadFormat
  569. }
  570. networks = append(networks, n)
  571. }
  572. }
  573. // Read out any unsafe networks
  574. if !b.ReadOptionalASN1(&subString, &found, TagDetailsUnsafeNetworks) {
  575. return detailsV2{}, ErrBadFormat
  576. }
  577. var unsafeNetworks []netip.Prefix
  578. if found {
  579. for !subString.Empty() {
  580. if !subString.ReadASN1(&val, asn1.OCTET_STRING) || val.Empty() || len(val) > MaxNetworkLength {
  581. return detailsV2{}, ErrBadFormat
  582. }
  583. var n netip.Prefix
  584. if err := n.UnmarshalBinary(val); err != nil {
  585. return detailsV2{}, ErrBadFormat
  586. }
  587. unsafeNetworks = append(unsafeNetworks, n)
  588. }
  589. }
  590. // Read out any groups
  591. if !b.ReadOptionalASN1(&subString, &found, TagDetailsGroups) {
  592. return detailsV2{}, ErrBadFormat
  593. }
  594. var groups []string
  595. if found {
  596. for !subString.Empty() {
  597. if !subString.ReadASN1(&val, asn1.UTF8String) || val.Empty() {
  598. return detailsV2{}, ErrBadFormat
  599. }
  600. groups = append(groups, string(val))
  601. }
  602. }
  603. // Read out IsCA
  604. var isCa bool
  605. if !readOptionalASN1Boolean(&b, &isCa, TagDetailsIsCA, false) {
  606. return detailsV2{}, ErrBadFormat
  607. }
  608. // Read not before and not after
  609. var notBefore int64
  610. if !b.ReadASN1Int64WithTag(&notBefore, TagDetailsNotBefore) {
  611. return detailsV2{}, ErrBadFormat
  612. }
  613. var notAfter int64
  614. if !b.ReadASN1Int64WithTag(&notAfter, TagDetailsNotAfter) {
  615. return detailsV2{}, ErrBadFormat
  616. }
  617. // Read issuer
  618. var issuer cryptobyte.String
  619. if !b.ReadOptionalASN1(&issuer, nil, TagDetailsIssuer) {
  620. return detailsV2{}, ErrBadFormat
  621. }
  622. return detailsV2{
  623. name: string(name),
  624. networks: networks,
  625. unsafeNetworks: unsafeNetworks,
  626. groups: groups,
  627. isCA: isCa,
  628. notBefore: time.Unix(notBefore, 0),
  629. notAfter: time.Unix(notAfter, 0),
  630. issuer: hex.EncodeToString(issuer),
  631. }, nil
  632. }