csr_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. package csr
  2. import (
  3. "crypto"
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rsa"
  7. "crypto/x509"
  8. "encoding/asn1"
  9. "encoding/pem"
  10. "io/ioutil"
  11. "testing"
  12. "github.com/cloudflare/cfssl/errors"
  13. "github.com/cloudflare/cfssl/helpers"
  14. )
  15. //TestNew validate the CertificateRequest created to return with a BasicKeyRequest
  16. //in KeyRequest field
  17. func TestNew(t *testing.T) {
  18. if cr := New(); cr.KeyRequest == nil {
  19. t.Fatalf("Should create a new, empty certificate request with BasicKeyRequest")
  20. }
  21. }
  22. // TestBasicKeyRequest ensures that key generation returns the same type of
  23. // key specified in the BasicKeyRequest.
  24. func TestBasicKeyRequest(t *testing.T) {
  25. kr := NewBasicKeyRequest()
  26. priv, err := kr.Generate()
  27. if err != nil {
  28. t.Fatalf("%v", err)
  29. }
  30. switch priv.(type) {
  31. case *rsa.PrivateKey:
  32. if kr.Algo() != "rsa" {
  33. t.Fatal("RSA key generated, but expected", kr.Algo())
  34. }
  35. case *ecdsa.PrivateKey:
  36. if kr.Algo() != "ecdsa" {
  37. t.Fatal("ECDSA key generated, but expected", kr.Algo())
  38. }
  39. }
  40. }
  41. // TestPKIXName validates building a pkix.Name structure from a
  42. // CertificateRequest.
  43. func TestPKIXName(t *testing.T) {
  44. var cr = &CertificateRequest{
  45. CN: "Test Common Name",
  46. Names: []Name{
  47. {
  48. C: "US",
  49. ST: "California",
  50. L: "San Francisco",
  51. O: "CloudFlare, Inc.",
  52. OU: "Systems Engineering",
  53. },
  54. {
  55. C: "GB",
  56. ST: "London",
  57. L: "London",
  58. O: "CloudFlare, Inc",
  59. OU: "Systems Engineering",
  60. },
  61. },
  62. Hosts: []string{"cloudflare.com", "www.cloudflare.com"},
  63. KeyRequest: NewBasicKeyRequest(),
  64. }
  65. name := cr.Name()
  66. if len(name.Country) != 2 {
  67. t.Fatal("Expected two countries in SubjInfo.")
  68. } else if len(name.Province) != 2 {
  69. t.Fatal("Expected two states in SubjInfo.")
  70. } else if len(name.Locality) != 2 {
  71. t.Fatal("Expected two localities in SubjInfo.")
  72. } else if len(name.Country) != 2 {
  73. t.Fatal("Expected two countries in SubjInfo.")
  74. } else if len(name.Organization) != 2 {
  75. t.Fatal("Expected two organization in SubjInfo.")
  76. } else if len(name.OrganizationalUnit) != 2 {
  77. t.Fatal("Expected two organizational units in SubjInfo.")
  78. }
  79. }
  80. // TestParseRequest ensures that a valid certificate request does not
  81. // error.
  82. func TestParseRequest(t *testing.T) {
  83. var cr = &CertificateRequest{
  84. CN: "Test Common Name",
  85. Names: []Name{
  86. {
  87. C: "US",
  88. ST: "California",
  89. L: "San Francisco",
  90. O: "CloudFlare, Inc.",
  91. OU: "Systems Engineering",
  92. },
  93. {
  94. C: "GB",
  95. ST: "London",
  96. L: "London",
  97. O: "CloudFlare, Inc",
  98. OU: "Systems Engineering",
  99. },
  100. },
  101. Hosts: []string{"cloudflare.com", "www.cloudflare.com", "192.168.0.1", "jdoe@example.com"},
  102. KeyRequest: NewBasicKeyRequest(),
  103. }
  104. _, _, err := ParseRequest(cr)
  105. if err != nil {
  106. t.Fatalf("%v", err)
  107. }
  108. }
  109. // TestParseRequestCA ensures that a valid CA certificate request does not
  110. // error and the resulting CSR includes the BasicConstraint extension
  111. func TestParseRequestCA(t *testing.T) {
  112. var cr = &CertificateRequest{
  113. CN: "Test Common Name",
  114. Names: []Name{
  115. {
  116. C: "US",
  117. ST: "California",
  118. L: "San Francisco",
  119. O: "CloudFlare, Inc.",
  120. OU: "Systems Engineering",
  121. },
  122. {
  123. C: "GB",
  124. ST: "London",
  125. L: "London",
  126. O: "CloudFlare, Inc",
  127. OU: "Systems Engineering",
  128. },
  129. },
  130. CA: &CAConfig{
  131. PathLength: 0,
  132. PathLenZero: true,
  133. },
  134. KeyRequest: NewBasicKeyRequest(),
  135. }
  136. csrBytes, _, err := ParseRequest(cr)
  137. if err != nil {
  138. t.Fatalf("%v", err)
  139. }
  140. block, _ := pem.Decode(csrBytes)
  141. if block == nil {
  142. t.Fatalf("%v", err)
  143. }
  144. if block.Type != "CERTIFICATE REQUEST" {
  145. t.Fatalf("Incorrect block type: %s", block.Type)
  146. }
  147. csr, err := x509.ParseCertificateRequest(block.Bytes)
  148. if err != nil {
  149. t.Fatalf("%v", err)
  150. }
  151. found := false
  152. for _, ext := range csr.Extensions {
  153. if ext.Id.Equal(asn1.ObjectIdentifier{2, 5, 29, 19}) {
  154. found = true
  155. break
  156. }
  157. }
  158. if !found {
  159. t.Fatalf("CSR did not include BasicConstraint Extension")
  160. }
  161. }
  162. // TestParseRequestCANoPathlen ensures that a valid CA certificate request
  163. // with an unspecified pathlen does not error and the resulting CSR includes
  164. // the BasicConstraint extension
  165. func TestParseRequestCANoPathlen(t *testing.T) {
  166. var cr = &CertificateRequest{
  167. CN: "Test Common Name",
  168. Names: []Name{
  169. {
  170. C: "US",
  171. ST: "California",
  172. L: "San Francisco",
  173. O: "CloudFlare, Inc.",
  174. OU: "Systems Engineering",
  175. },
  176. {
  177. C: "GB",
  178. ST: "London",
  179. L: "London",
  180. O: "CloudFlare, Inc",
  181. OU: "Systems Engineering",
  182. },
  183. },
  184. CA: &CAConfig{
  185. PathLength: 0,
  186. PathLenZero: false,
  187. },
  188. KeyRequest: NewBasicKeyRequest(),
  189. }
  190. csrBytes, _, err := ParseRequest(cr)
  191. if err != nil {
  192. t.Fatalf("%v", err)
  193. }
  194. block, _ := pem.Decode(csrBytes)
  195. if block == nil {
  196. t.Fatalf("%v", err)
  197. }
  198. if block.Type != "CERTIFICATE REQUEST" {
  199. t.Fatalf("Incorrect block type: %s", block.Type)
  200. }
  201. csr, err := x509.ParseCertificateRequest(block.Bytes)
  202. if err != nil {
  203. t.Fatalf("%v", err)
  204. }
  205. found := false
  206. for _, ext := range csr.Extensions {
  207. if ext.Id.Equal(asn1.ObjectIdentifier{2, 5, 29, 19}) {
  208. bc := &BasicConstraints{}
  209. asn1.Unmarshal(ext.Value, bc)
  210. if bc.IsCA == true && bc.MaxPathLen == -1 {
  211. found = true
  212. break
  213. }
  214. }
  215. }
  216. if !found {
  217. t.Fatalf("CSR did not include BasicConstraint Extension")
  218. }
  219. }
  220. func whichCurve(sz int) elliptic.Curve {
  221. switch sz {
  222. case 256:
  223. return elliptic.P256()
  224. case 384:
  225. return elliptic.P384()
  226. case 521:
  227. return elliptic.P521()
  228. }
  229. return nil
  230. }
  231. // TestECGeneration ensures that the proper curve is used depending on
  232. // the bit size specified in a key request and that an appropriate
  233. // signature algorithm is returned.
  234. func TestECGeneration(t *testing.T) {
  235. var eckey *ecdsa.PrivateKey
  236. for _, sz := range []int{256, 384, 521} {
  237. kr := &BasicKeyRequest{"ecdsa", sz}
  238. priv, err := kr.Generate()
  239. if err != nil {
  240. t.Fatalf("%v", err)
  241. }
  242. eckey = priv.(*ecdsa.PrivateKey)
  243. if eckey.Curve != whichCurve(sz) {
  244. t.Fatal("Generated key has wrong curve.")
  245. }
  246. if sa := kr.SigAlgo(); sa == x509.UnknownSignatureAlgorithm {
  247. t.Fatal("Invalid signature algorithm!")
  248. }
  249. }
  250. }
  251. func TestRSAKeyGeneration(t *testing.T) {
  252. var rsakey *rsa.PrivateKey
  253. for _, sz := range []int{2048, 3072, 4096} {
  254. kr := &BasicKeyRequest{"rsa", sz}
  255. priv, err := kr.Generate()
  256. if err != nil {
  257. t.Fatalf("%v", err)
  258. }
  259. rsakey = priv.(*rsa.PrivateKey)
  260. if rsakey.PublicKey.N.BitLen() != kr.Size() {
  261. t.Fatal("Generated key has wrong size.")
  262. }
  263. if sa := kr.SigAlgo(); sa == x509.UnknownSignatureAlgorithm {
  264. t.Fatal("Invalid signature algorithm!")
  265. }
  266. }
  267. }
  268. // TestBadBasicKeyRequest ensures that generating a key from a BasicKeyRequest
  269. // fails with an invalid algorithm, or an invalid RSA or ECDSA key
  270. // size. An invalid ECDSA key size is any size other than 256, 384, or
  271. // 521; an invalid RSA key size is any size less than 2048 bits.
  272. func TestBadBasicKeyRequest(t *testing.T) {
  273. kr := &BasicKeyRequest{"yolocrypto", 1024}
  274. if _, err := kr.Generate(); err == nil {
  275. t.Fatal("Key generation should fail with invalid algorithm")
  276. } else if sa := kr.SigAlgo(); sa != x509.UnknownSignatureAlgorithm {
  277. t.Fatal("The wrong signature algorithm was returned from SigAlgo!")
  278. }
  279. kr.A = "ecdsa"
  280. if _, err := kr.Generate(); err == nil {
  281. t.Fatal("Key generation should fail with invalid key size")
  282. } else if sa := kr.SigAlgo(); sa != x509.ECDSAWithSHA1 {
  283. t.Fatal("The wrong signature algorithm was returned from SigAlgo!")
  284. }
  285. kr.A = "rsa"
  286. if _, err := kr.Generate(); err == nil {
  287. t.Fatal("Key generation should fail with invalid key size")
  288. } else if sa := kr.SigAlgo(); sa != x509.SHA1WithRSA {
  289. t.Fatal("The wrong signature algorithm was returned from SigAlgo!")
  290. }
  291. kr = &BasicKeyRequest{"tobig", 9216}
  292. kr.A = "rsa"
  293. if _, err := kr.Generate(); err == nil {
  294. t.Fatal("Key generation should fail with invalid key size")
  295. } else if sa := kr.SigAlgo(); sa != x509.SHA512WithRSA {
  296. t.Fatal("The wrong signature algorithm was returned from SigAlgo!")
  297. }
  298. }
  299. // TestDefaultBasicKeyRequest makes sure that certificate requests without
  300. // explicit key requests fall back to the default key request.
  301. func TestDefaultBasicKeyRequest(t *testing.T) {
  302. var req = &CertificateRequest{
  303. Names: []Name{
  304. {
  305. C: "US",
  306. ST: "California",
  307. L: "San Francisco",
  308. O: "CloudFlare",
  309. OU: "Systems Engineering",
  310. },
  311. },
  312. CN: "cloudflare.com",
  313. Hosts: []string{"cloudflare.com", "www.cloudflare.com", "jdoe@example.com"},
  314. }
  315. _, priv, err := ParseRequest(req)
  316. if err != nil {
  317. t.Fatalf("%v", err)
  318. }
  319. // If the default key type changes, this will need to be changed.
  320. block, _ := pem.Decode(priv)
  321. if block == nil {
  322. t.Fatal("Bad private key was generated!")
  323. }
  324. DefaultKeyRequest := NewBasicKeyRequest()
  325. switch block.Type {
  326. case "RSA PRIVATE KEY":
  327. if DefaultKeyRequest.Algo() != "rsa" {
  328. t.Fatal("Invalid default key request.")
  329. }
  330. case "EC PRIVATE KEY":
  331. if DefaultKeyRequest.Algo() != "ecdsa" {
  332. t.Fatal("Invalid default key request.")
  333. }
  334. }
  335. }
  336. // TestRSACertRequest validates parsing a certificate request with an
  337. // RSA key.
  338. func TestRSACertRequest(t *testing.T) {
  339. var req = &CertificateRequest{
  340. Names: []Name{
  341. {
  342. C: "US",
  343. ST: "California",
  344. L: "San Francisco",
  345. O: "CloudFlare",
  346. OU: "Systems Engineering",
  347. },
  348. },
  349. CN: "cloudflare.com",
  350. Hosts: []string{"cloudflare.com", "www.cloudflare.com", "jdoe@example.com"},
  351. KeyRequest: &BasicKeyRequest{"rsa", 2048},
  352. }
  353. _, _, err := ParseRequest(req)
  354. if err != nil {
  355. t.Fatalf("%v", err)
  356. }
  357. }
  358. // TestBadCertRequest checks for failure conditions of ParseRequest.
  359. func TestBadCertRequest(t *testing.T) {
  360. var req = &CertificateRequest{
  361. Names: []Name{
  362. {
  363. C: "US",
  364. ST: "California",
  365. L: "San Francisco",
  366. O: "CloudFlare",
  367. OU: "Systems Engineering",
  368. },
  369. },
  370. CN: "cloudflare.com",
  371. Hosts: []string{"cloudflare.com", "www.cloudflare.com"},
  372. KeyRequest: &BasicKeyRequest{"yolo-crypto", 2048},
  373. }
  374. _, _, err := ParseRequest(req)
  375. if err == nil {
  376. t.Fatal("ParseRequest should fail with a bad key algorithm.")
  377. }
  378. }
  379. // testValidator is a stripped-down validator that checks to make sure
  380. // the request has a common name. It should mimic some of the
  381. // functionality expected in an actual validator.
  382. func testValidator(req *CertificateRequest) error {
  383. if req.CN == "" {
  384. return errors.NewBadRequestMissingParameter("CN")
  385. }
  386. return nil
  387. }
  388. // TestGenerator ensures that a valid request is processed properly
  389. // and returns a certificate request and key.
  390. func TestGenerator(t *testing.T) {
  391. g := &Generator{testValidator}
  392. var req = &CertificateRequest{
  393. Names: []Name{
  394. {
  395. C: "US",
  396. ST: "California",
  397. L: "San Francisco",
  398. O: "CloudFlare",
  399. OU: "Systems Engineering",
  400. },
  401. },
  402. CN: "cloudflare.com",
  403. Hosts: []string{"cloudflare.com", "www.cloudflare.com", "192.168.0.1", "jdoe@example.com"},
  404. KeyRequest: &BasicKeyRequest{"rsa", 2048},
  405. }
  406. csrBytes, _, err := g.ProcessRequest(req)
  407. if err != nil {
  408. t.Fatal(err)
  409. }
  410. block, _ := pem.Decode([]byte(csrBytes))
  411. if block == nil {
  412. t.Fatalf("bad CSR in PEM")
  413. }
  414. if block.Type != "CERTIFICATE REQUEST" {
  415. t.Fatalf("bad CSR in PEM")
  416. }
  417. csr, err := x509.ParseCertificateRequest(block.Bytes)
  418. if err != nil {
  419. t.Fatal(err)
  420. }
  421. if len(csr.DNSNames) != 2 {
  422. t.Fatal("SAN parsing error")
  423. }
  424. if len(csr.IPAddresses) != 1 {
  425. t.Fatal("SAN parsing error")
  426. }
  427. if len(csr.EmailAddresses) != 1 {
  428. t.Fatal("SAN parsing error")
  429. }
  430. }
  431. // TestBadGenerator ensures that a request that fails the validator is
  432. // not processed.
  433. func TestBadGenerator(t *testing.T) {
  434. g := &Generator{testValidator}
  435. missingCN := &CertificateRequest{
  436. Names: []Name{
  437. {
  438. C: "US",
  439. ST: "California",
  440. L: "San Francisco",
  441. O: "CloudFlare",
  442. OU: "Systems Engineering",
  443. },
  444. },
  445. // Missing CN
  446. Hosts: []string{"cloudflare.com", "www.cloudflare.com"},
  447. KeyRequest: &BasicKeyRequest{"rsa", 2048},
  448. }
  449. _, _, err := g.ProcessRequest(missingCN)
  450. if err == nil {
  451. t.Fatalf("Request should have failed.")
  452. }
  453. }
  454. func TestWeakCSR(t *testing.T) {
  455. weakKey := &CertificateRequest{
  456. Names: []Name{
  457. {
  458. C: "US",
  459. ST: "California",
  460. L: "San Francisco",
  461. O: "CloudFlare",
  462. OU: "Systems Engineering",
  463. },
  464. },
  465. CN: "cloudflare.com",
  466. Hosts: []string{"cloudflare.com", "www.cloudflare.com", "jdoe@example.com"},
  467. KeyRequest: &BasicKeyRequest{"rsa", 1024},
  468. }
  469. g := &Generator{testValidator}
  470. _, _, err := g.ProcessRequest(weakKey)
  471. if err == nil {
  472. t.Fatalf("Request should have failed.")
  473. }
  474. }
  475. var testEmpty = []struct {
  476. name Name
  477. ok bool
  478. }{
  479. {
  480. Name{},
  481. true,
  482. },
  483. {
  484. Name{C: "OK"},
  485. false,
  486. },
  487. {
  488. Name{ST: "OK"},
  489. false,
  490. },
  491. {
  492. Name{L: "OK"},
  493. false,
  494. },
  495. {
  496. Name{O: "OK"},
  497. false,
  498. },
  499. {
  500. Name{OU: "OK"},
  501. false,
  502. },
  503. }
  504. func TestIsNameEmpty(t *testing.T) {
  505. for i, c := range testEmpty {
  506. if IsNameEmpty(c.name) != c.ok {
  507. t.Fatalf("%d: expected IsNameEmpty to return %v, but have %v", i, c.ok, !c.ok)
  508. }
  509. }
  510. }
  511. func TestGenerate(t *testing.T) {
  512. var req = &CertificateRequest{
  513. Names: []Name{
  514. {
  515. C: "US",
  516. ST: "California",
  517. L: "San Francisco",
  518. O: "CloudFlare",
  519. OU: "Systems Engineering",
  520. },
  521. },
  522. CN: "cloudflare.com",
  523. Hosts: []string{"cloudflare.com", "www.cloudflare.com", "192.168.0.1", "jdoe@example.com"},
  524. KeyRequest: &BasicKeyRequest{"ecdsa", 256},
  525. }
  526. key, err := req.KeyRequest.Generate()
  527. if err != nil {
  528. t.Fatalf("%v", err)
  529. }
  530. priv, ok := key.(crypto.Signer)
  531. if !ok {
  532. t.Fatal("Private key is not a signer.")
  533. }
  534. csrPEM, err := Generate(priv, req)
  535. if err != nil {
  536. t.Fatalf("%v", err)
  537. }
  538. csr, _, err := helpers.ParseCSR(csrPEM)
  539. if err != nil {
  540. t.Fatalf("%v", err)
  541. }
  542. if len(csr.DNSNames) != 2 {
  543. t.Fatal("SAN parsing error")
  544. }
  545. if len(csr.IPAddresses) != 1 {
  546. t.Fatal("SAN parsing error")
  547. }
  548. if len(csr.EmailAddresses) != 1 {
  549. t.Fatal("SAN parsing error")
  550. }
  551. }
  552. // TestReGenerate ensures Regenerate() is abel to use the provided CSR as a template for signing a new
  553. // CSR using priv.
  554. func TestReGenerate(t *testing.T) {
  555. var req = &CertificateRequest{
  556. Names: []Name{
  557. {
  558. C: "US",
  559. ST: "California",
  560. L: "San Francisco",
  561. O: "CloudFlare",
  562. OU: "Systems Engineering",
  563. },
  564. },
  565. CN: "cloudflare.com",
  566. Hosts: []string{"cloudflare.com", "www.cloudflare.com", "192.168.0.1"},
  567. KeyRequest: &BasicKeyRequest{"ecdsa", 256},
  568. }
  569. _, key, err := ParseRequest(req)
  570. if err != nil {
  571. t.Fatalf("%v", err)
  572. }
  573. priv, err := helpers.ParsePrivateKeyPEM(key)
  574. if err != nil {
  575. t.Fatalf("%v", err)
  576. }
  577. csr, err := Generate(priv, req)
  578. if err != nil {
  579. t.Fatalf("%v", err)
  580. }
  581. if _, _, err = helpers.ParseCSR(csr); err != nil {
  582. t.Fatalf("%v", err)
  583. }
  584. _, err = Regenerate(priv, csr)
  585. if err != nil {
  586. t.Fatalf("%v", err)
  587. }
  588. }
  589. // TestBadReGenerator ensures that a request that fails the ParseCSR is
  590. // not processed.
  591. func TestBadReGenerate(t *testing.T) {
  592. var req = &CertificateRequest{
  593. Names: []Name{
  594. {
  595. C: "US",
  596. ST: "California",
  597. L: "San Francisco",
  598. O: "CloudFlare",
  599. OU: "Systems Engineering",
  600. },
  601. },
  602. CN: "cloudflare.com",
  603. Hosts: []string{"cloudflare.com", "www.cloudflare.com", "192.168.0.1"},
  604. KeyRequest: &BasicKeyRequest{"ecdsa", 256},
  605. }
  606. _, key, err := ParseRequest(req)
  607. if err != nil {
  608. t.Fatalf("%v", err)
  609. }
  610. priv, err := helpers.ParsePrivateKeyPEM(key)
  611. if err != nil {
  612. t.Fatalf("%v", err)
  613. }
  614. csr, err := Generate(priv, req)
  615. if err != nil {
  616. t.Fatalf("%v", err)
  617. }
  618. block := pem.Block{
  619. Type: "CERTIFICATE REQUEST",
  620. Headers: map[string]string{
  621. "Location": "UCSD",
  622. },
  623. Bytes: csr,
  624. }
  625. csr = pem.EncodeToMemory(&block)
  626. _, err = Regenerate(priv, csr)
  627. if err == nil {
  628. t.Fatalf("%v", err)
  629. }
  630. }
  631. var testECDSACertificateFile = "testdata/test-ecdsa-ca.pem"
  632. func TestExtractCertificateRequest(t *testing.T) {
  633. certPEM, err := ioutil.ReadFile(testECDSACertificateFile)
  634. if err != nil {
  635. t.Fatal(err)
  636. }
  637. // must parse ok
  638. cert, err := helpers.ParseCertificatePEM(certPEM)
  639. if err != nil {
  640. t.Fatal(err)
  641. }
  642. req := ExtractCertificateRequest(cert)
  643. if req.CN != "" {
  644. t.Fatal("Bad Certificate Request!")
  645. }
  646. if len(req.Names) != 1 {
  647. t.Fatal("Bad Certificate Request!")
  648. }
  649. name := req.Names[0]
  650. if name.C != "US" || name.ST != "California" || name.O != "CloudFlare, Inc." ||
  651. name.OU != "Test Certificate Authority" || name.L != "San Francisco" {
  652. t.Fatal("Bad Certificate Request!")
  653. }
  654. if req.CA == nil || req.CA.PathLength != 2 {
  655. t.Fatal("Bad Certificate Request!")
  656. }
  657. }