csr_test.go 18 KB

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