csr_test.go 20 KB

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