csr_test.go 17 KB

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