cert_v2.go 18 KB

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