stepcas_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. package stepcas
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto"
  6. "crypto/ed25519"
  7. "crypto/rand"
  8. "crypto/x509"
  9. "encoding/json"
  10. "encoding/pem"
  11. "fmt"
  12. "net/http"
  13. "net/http/httptest"
  14. "net/url"
  15. "os"
  16. "path/filepath"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/smallstep/certificates/api"
  21. "github.com/smallstep/certificates/authority/provisioner"
  22. "github.com/smallstep/certificates/ca"
  23. "github.com/smallstep/certificates/cas/apiv1"
  24. "go.step.sm/crypto/jose"
  25. "go.step.sm/crypto/pemutil"
  26. "go.step.sm/crypto/randutil"
  27. "go.step.sm/crypto/x509util"
  28. )
  29. var (
  30. testRootCrt *x509.Certificate
  31. testRootKey crypto.Signer
  32. testRootPath, testRootKeyPath string
  33. testRootFingerprint string
  34. testIssCrt *x509.Certificate
  35. testIssKey crypto.Signer
  36. testIssPath, testIssKeyPath string
  37. testX5CCrt *x509.Certificate
  38. testX5CKey crypto.Signer
  39. testX5CPath, testX5CKeyPath string
  40. testPassword, testEncryptedKeyPath string
  41. testKeyID, testEncryptedJWKKey string
  42. testCR *x509.CertificateRequest
  43. testCrt *x509.Certificate
  44. testKey crypto.Signer
  45. testFailCR *x509.CertificateRequest
  46. )
  47. func mustSignCertificate(subject string, sans []string, template string, parent *x509.Certificate, signer crypto.Signer) (*x509.Certificate, crypto.Signer) {
  48. pub, priv, err := ed25519.GenerateKey(rand.Reader)
  49. if err != nil {
  50. panic(err)
  51. }
  52. cr, err := x509util.CreateCertificateRequest(subject, sans, priv)
  53. if err != nil {
  54. panic(err)
  55. }
  56. cert, err := x509util.NewCertificate(cr, x509util.WithTemplate(template, x509util.CreateTemplateData(subject, sans)))
  57. if err != nil {
  58. panic(err)
  59. }
  60. crt := cert.GetCertificate()
  61. crt.NotBefore = time.Now()
  62. crt.NotAfter = crt.NotBefore.Add(time.Hour)
  63. if parent == nil {
  64. parent = crt
  65. }
  66. if signer == nil {
  67. signer = priv
  68. }
  69. if crt, err = x509util.CreateCertificate(crt, parent, pub, signer); err != nil {
  70. panic(err)
  71. }
  72. return crt, priv
  73. }
  74. func mustSerializeCrt(filename string, certs ...*x509.Certificate) {
  75. buf := new(bytes.Buffer)
  76. for _, c := range certs {
  77. if err := pem.Encode(buf, &pem.Block{
  78. Type: "CERTIFICATE",
  79. Bytes: c.Raw,
  80. }); err != nil {
  81. panic(err)
  82. }
  83. }
  84. if err := os.WriteFile(filename, buf.Bytes(), 0600); err != nil {
  85. panic(err)
  86. }
  87. }
  88. func mustSerializeKey(filename string, key crypto.Signer) {
  89. b, err := x509.MarshalPKCS8PrivateKey(key)
  90. if err != nil {
  91. panic(err)
  92. }
  93. b = pem.EncodeToMemory(&pem.Block{
  94. Type: "PRIVATE KEY",
  95. Bytes: b,
  96. })
  97. if err := os.WriteFile(filename, b, 0600); err != nil {
  98. panic(err)
  99. }
  100. }
  101. func mustEncryptKey(filename string, key crypto.Signer) {
  102. _, err := pemutil.Serialize(key,
  103. pemutil.ToFile(filename, 0600),
  104. pemutil.WithPKCS8(true),
  105. pemutil.WithPassword([]byte(testPassword)))
  106. if err != nil {
  107. panic(err)
  108. }
  109. }
  110. func testCAHelper(t *testing.T) (*url.URL, *ca.Client) {
  111. t.Helper()
  112. writeJSON := func(w http.ResponseWriter, v interface{}) {
  113. _ = json.NewEncoder(w).Encode(v)
  114. }
  115. parseJSON := func(r *http.Request, v interface{}) {
  116. _ = json.NewDecoder(r.Body).Decode(v)
  117. }
  118. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  119. switch {
  120. case r.RequestURI == "/root/"+testRootFingerprint:
  121. w.WriteHeader(http.StatusOK)
  122. writeJSON(w, api.RootResponse{
  123. RootPEM: api.NewCertificate(testRootCrt),
  124. })
  125. case r.RequestURI == "/sign":
  126. var msg api.SignRequest
  127. parseJSON(r, &msg)
  128. if msg.CsrPEM.DNSNames[0] == "fail.doe.org" {
  129. w.WriteHeader(http.StatusBadRequest)
  130. fmt.Fprintf(w, `{"error":"fail","message":"fail"}`)
  131. return
  132. }
  133. w.WriteHeader(http.StatusOK)
  134. writeJSON(w, api.SignResponse{
  135. CertChainPEM: []api.Certificate{api.NewCertificate(testCrt), api.NewCertificate(testIssCrt)},
  136. })
  137. case r.RequestURI == "/revoke":
  138. var msg api.RevokeRequest
  139. parseJSON(r, &msg)
  140. if msg.Serial == "fail" {
  141. w.WriteHeader(http.StatusBadRequest)
  142. fmt.Fprintf(w, `{"error":"fail","message":"fail"}`)
  143. return
  144. }
  145. w.WriteHeader(http.StatusOK)
  146. writeJSON(w, api.RevokeResponse{
  147. Status: "ok",
  148. })
  149. case r.RequestURI == "/provisioners":
  150. w.WriteHeader(http.StatusOK)
  151. writeJSON(w, api.ProvisionersResponse{
  152. NextCursor: "cursor",
  153. Provisioners: []provisioner.Interface{
  154. &provisioner.JWK{
  155. Type: "JWK",
  156. Name: "ra@doe.org",
  157. Key: &jose.JSONWebKey{KeyID: testKeyID, Key: testX5CKey.Public()},
  158. EncryptedKey: testEncryptedJWKKey,
  159. },
  160. &provisioner.JWK{
  161. Type: "JWK",
  162. Name: "empty@doe.org",
  163. Key: &jose.JSONWebKey{KeyID: testKeyID, Key: testX5CKey.Public()},
  164. },
  165. },
  166. })
  167. case r.RequestURI == "/provisioners?cursor=cursor":
  168. w.WriteHeader(http.StatusOK)
  169. writeJSON(w, api.ProvisionersResponse{})
  170. default:
  171. w.WriteHeader(http.StatusNotFound)
  172. fmt.Fprintf(w, `{"error":"not found"}`)
  173. }
  174. }))
  175. t.Cleanup(func() {
  176. srv.Close()
  177. })
  178. u, err := url.Parse(srv.URL)
  179. if err != nil {
  180. srv.Close()
  181. t.Fatal(err)
  182. }
  183. client, err := ca.NewClient(srv.URL, ca.WithTransport(http.DefaultTransport))
  184. if err != nil {
  185. srv.Close()
  186. t.Fatal(err)
  187. }
  188. return u, client
  189. }
  190. func testX5CIssuer(t *testing.T, caURL *url.URL, password string) *x5cIssuer {
  191. t.Helper()
  192. key, givenPassword := testX5CKeyPath, password
  193. if password != "" {
  194. key = testEncryptedKeyPath
  195. password = testPassword
  196. }
  197. x5c, err := newX5CIssuer(caURL, &apiv1.CertificateIssuer{
  198. Type: "x5c",
  199. Provisioner: "X5C",
  200. Certificate: testX5CPath,
  201. Key: key,
  202. Password: password,
  203. })
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. x5c.password = givenPassword
  208. return x5c
  209. }
  210. func testJWKIssuer(t *testing.T, caURL *url.URL, password string) *jwkIssuer {
  211. t.Helper()
  212. client, err := ca.NewClient(caURL.String(), ca.WithTransport(http.DefaultTransport))
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. key := testX5CKeyPath
  217. if password != "" {
  218. key = testEncryptedKeyPath
  219. password = testPassword
  220. }
  221. jwk, err := newJWKIssuer(caURL, client, &apiv1.CertificateIssuer{
  222. Type: "jwk",
  223. Provisioner: "ra@doe.org",
  224. Key: key,
  225. Password: password,
  226. })
  227. if err != nil {
  228. t.Fatal(err)
  229. }
  230. return jwk
  231. }
  232. func TestMain(m *testing.M) {
  233. testRootCrt, testRootKey = mustSignCertificate("Test Root Certificate", nil, x509util.DefaultRootTemplate, nil, nil)
  234. testIssCrt, testIssKey = mustSignCertificate("Test Intermediate Certificate", nil, x509util.DefaultIntermediateTemplate, testRootCrt, testRootKey)
  235. testX5CCrt, testX5CKey = mustSignCertificate("Test X5C Certificate", nil, x509util.DefaultLeafTemplate, testIssCrt, testIssKey)
  236. testRootFingerprint = x509util.Fingerprint(testRootCrt)
  237. // Final certificate.
  238. var err error
  239. sans := []string{"doe.org", "jane@doe.org", "127.0.0.1", "::1", "localhost", "uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6;name=value"}
  240. testCrt, testKey = mustSignCertificate("Test Certificate", sans, x509util.DefaultLeafTemplate, testIssCrt, testIssKey)
  241. testCR, err = x509util.CreateCertificateRequest("Test Certificate", sans, testKey)
  242. if err != nil {
  243. panic(err)
  244. }
  245. // CR used in errors.
  246. testFailCR, err = x509util.CreateCertificateRequest("", []string{"fail.doe.org"}, testKey)
  247. if err != nil {
  248. panic(err)
  249. }
  250. // Password used to encrypt the key.
  251. testPassword, err = randutil.Hex(32)
  252. if err != nil {
  253. panic(err)
  254. }
  255. // Encrypted JWK key used when the key is downloaded from the CA.
  256. jwe, err := jose.EncryptJWK(&jose.JSONWebKey{Key: testX5CKey}, []byte(testPassword))
  257. if err != nil {
  258. panic(err)
  259. }
  260. testEncryptedJWKKey, err = jwe.CompactSerialize()
  261. if err != nil {
  262. panic(err)
  263. }
  264. testKeyID, err = jose.Thumbprint(&jose.JSONWebKey{Key: testX5CKey})
  265. if err != nil {
  266. panic(err)
  267. }
  268. // Create test files.
  269. path, err := os.MkdirTemp(os.TempDir(), "stepcas")
  270. if err != nil {
  271. panic(err)
  272. }
  273. testRootPath = filepath.Join(path, "root_ca.crt")
  274. testRootKeyPath = filepath.Join(path, "root_ca.key")
  275. mustSerializeCrt(testRootPath, testRootCrt)
  276. mustSerializeKey(testRootKeyPath, testRootKey)
  277. testIssPath = filepath.Join(path, "intermediate_ca.crt")
  278. testIssKeyPath = filepath.Join(path, "intermediate_ca.key")
  279. mustSerializeCrt(testIssPath, testIssCrt)
  280. mustSerializeKey(testIssKeyPath, testIssKey)
  281. testX5CPath = filepath.Join(path, "x5c.crt")
  282. testX5CKeyPath = filepath.Join(path, "x5c.key")
  283. mustSerializeCrt(testX5CPath, testX5CCrt, testIssCrt)
  284. mustSerializeKey(testX5CKeyPath, testX5CKey)
  285. testEncryptedKeyPath = filepath.Join(path, "x5c.enc.key")
  286. mustEncryptKey(testEncryptedKeyPath, testX5CKey)
  287. code := m.Run()
  288. if err := os.RemoveAll(path); err != nil {
  289. panic(err)
  290. }
  291. os.Exit(code)
  292. }
  293. func Test_init(t *testing.T) {
  294. caURL, _ := testCAHelper(t)
  295. fn, ok := apiv1.LoadCertificateAuthorityServiceNewFunc(apiv1.StepCAS)
  296. if !ok {
  297. t.Errorf("apiv1.Register() ok = %v, want true", ok)
  298. return
  299. }
  300. fn(context.Background(), apiv1.Options{
  301. CertificateAuthority: caURL.String(),
  302. CertificateAuthorityFingerprint: testRootFingerprint,
  303. CertificateIssuer: &apiv1.CertificateIssuer{
  304. Type: "x5c",
  305. Provisioner: "X5C",
  306. Certificate: testX5CPath,
  307. Key: testX5CKeyPath,
  308. },
  309. })
  310. }
  311. func TestNew(t *testing.T) {
  312. caURL, client := testCAHelper(t)
  313. signer, err := newJWKSignerFromEncryptedKey(testKeyID, testEncryptedJWKKey, testPassword)
  314. if err != nil {
  315. t.Fatal(err)
  316. }
  317. type args struct {
  318. ctx context.Context
  319. opts apiv1.Options
  320. }
  321. tests := []struct {
  322. name string
  323. args args
  324. want *StepCAS
  325. wantErr bool
  326. }{
  327. {"ok", args{context.TODO(), apiv1.Options{
  328. CertificateAuthority: caURL.String(),
  329. CertificateAuthorityFingerprint: testRootFingerprint,
  330. CertificateIssuer: &apiv1.CertificateIssuer{
  331. Type: "x5c",
  332. Provisioner: "X5C",
  333. Certificate: testX5CPath,
  334. Key: testX5CKeyPath,
  335. },
  336. }}, &StepCAS{
  337. iss: &x5cIssuer{
  338. caURL: caURL,
  339. certFile: testX5CPath,
  340. keyFile: testX5CKeyPath,
  341. issuer: "X5C",
  342. },
  343. client: client,
  344. fingerprint: testRootFingerprint,
  345. }, false},
  346. {"ok jwk", args{context.TODO(), apiv1.Options{
  347. CertificateAuthority: caURL.String(),
  348. CertificateAuthorityFingerprint: testRootFingerprint,
  349. CertificateIssuer: &apiv1.CertificateIssuer{
  350. Type: "jwk",
  351. Provisioner: "ra@doe.org",
  352. Key: testX5CKeyPath,
  353. },
  354. }}, &StepCAS{
  355. iss: &jwkIssuer{
  356. caURL: caURL,
  357. issuer: "ra@doe.org",
  358. signer: signer,
  359. },
  360. client: client,
  361. fingerprint: testRootFingerprint,
  362. }, false},
  363. {"ok jwk provisioners", args{context.TODO(), apiv1.Options{
  364. CertificateAuthority: caURL.String(),
  365. CertificateAuthorityFingerprint: testRootFingerprint,
  366. CertificateIssuer: &apiv1.CertificateIssuer{
  367. Type: "jwk",
  368. Provisioner: "ra@doe.org",
  369. Password: testPassword,
  370. },
  371. }}, &StepCAS{
  372. iss: &jwkIssuer{
  373. caURL: caURL,
  374. issuer: "ra@doe.org",
  375. signer: signer,
  376. },
  377. client: client,
  378. fingerprint: testRootFingerprint,
  379. }, false},
  380. {"ok ca getter", args{context.TODO(), apiv1.Options{
  381. IsCAGetter: true,
  382. CertificateAuthority: caURL.String(),
  383. CertificateAuthorityFingerprint: testRootFingerprint,
  384. CertificateIssuer: &apiv1.CertificateIssuer{
  385. Type: "jwk",
  386. Provisioner: "ra@doe.org",
  387. },
  388. }}, &StepCAS{
  389. iss: nil,
  390. client: client,
  391. fingerprint: testRootFingerprint,
  392. }, false},
  393. {"fail authority", args{context.TODO(), apiv1.Options{
  394. CertificateAuthority: "",
  395. CertificateAuthorityFingerprint: testRootFingerprint,
  396. CertificateIssuer: &apiv1.CertificateIssuer{
  397. Type: "x5c",
  398. Provisioner: "X5C",
  399. Certificate: testX5CPath,
  400. Key: testX5CKeyPath,
  401. },
  402. }}, nil, true},
  403. {"fail fingerprint", args{context.TODO(), apiv1.Options{
  404. CertificateAuthority: caURL.String(),
  405. CertificateAuthorityFingerprint: "",
  406. CertificateIssuer: &apiv1.CertificateIssuer{
  407. Type: "x5c",
  408. Provisioner: "X5C",
  409. Certificate: testX5CPath,
  410. Key: testX5CKeyPath,
  411. },
  412. }}, nil, true},
  413. {"fail type", args{context.TODO(), apiv1.Options{
  414. CertificateAuthority: caURL.String(),
  415. CertificateAuthorityFingerprint: testRootFingerprint,
  416. CertificateIssuer: &apiv1.CertificateIssuer{
  417. Type: "",
  418. Provisioner: "X5C",
  419. Certificate: testX5CPath,
  420. Key: testX5CKeyPath,
  421. },
  422. }}, nil, true},
  423. {"fail provisioner", args{context.TODO(), apiv1.Options{
  424. CertificateAuthority: caURL.String(),
  425. CertificateAuthorityFingerprint: testRootFingerprint,
  426. CertificateIssuer: &apiv1.CertificateIssuer{
  427. Type: "x5c",
  428. Provisioner: "",
  429. Certificate: testX5CPath,
  430. Key: testX5CKeyPath,
  431. },
  432. }}, nil, true},
  433. {"fail provisioner jwk", args{context.TODO(), apiv1.Options{
  434. CertificateAuthority: caURL.String(),
  435. CertificateAuthorityFingerprint: testRootFingerprint,
  436. CertificateIssuer: &apiv1.CertificateIssuer{
  437. Type: "jwk",
  438. Provisioner: "",
  439. Key: testX5CKeyPath,
  440. },
  441. }}, nil, true},
  442. {"fail provisioner not found", args{context.TODO(), apiv1.Options{
  443. CertificateAuthority: caURL.String(),
  444. CertificateAuthorityFingerprint: testRootFingerprint,
  445. CertificateIssuer: &apiv1.CertificateIssuer{
  446. Type: "jwk",
  447. Provisioner: "notfound@doe.org",
  448. Password: testPassword,
  449. },
  450. }}, nil, true},
  451. {"fail invalid password", args{context.TODO(), apiv1.Options{
  452. CertificateAuthority: caURL.String(),
  453. CertificateAuthorityFingerprint: testRootFingerprint,
  454. CertificateIssuer: &apiv1.CertificateIssuer{
  455. Type: "jwk",
  456. Provisioner: "ra@doe.org",
  457. Password: "bad-password",
  458. },
  459. }}, nil, true},
  460. {"fail no key", args{context.TODO(), apiv1.Options{
  461. CertificateAuthority: caURL.String(),
  462. CertificateAuthorityFingerprint: testRootFingerprint,
  463. CertificateIssuer: &apiv1.CertificateIssuer{
  464. Type: "jwk",
  465. Provisioner: "empty@doe.org",
  466. Password: testPassword,
  467. },
  468. }}, nil, true},
  469. {"fail certificate", args{context.TODO(), apiv1.Options{
  470. CertificateAuthority: caURL.String(),
  471. CertificateAuthorityFingerprint: testRootFingerprint,
  472. CertificateIssuer: &apiv1.CertificateIssuer{
  473. Type: "x5c",
  474. Provisioner: "X5C",
  475. Certificate: "",
  476. Key: testX5CKeyPath,
  477. },
  478. }}, nil, true},
  479. {"fail key", args{context.TODO(), apiv1.Options{
  480. CertificateAuthority: caURL.String(),
  481. CertificateAuthorityFingerprint: testRootFingerprint,
  482. CertificateIssuer: &apiv1.CertificateIssuer{
  483. Type: "x5c",
  484. Provisioner: "X5C",
  485. Certificate: testX5CPath,
  486. Key: "",
  487. },
  488. }}, nil, true},
  489. {"fail key jwk", args{context.TODO(), apiv1.Options{
  490. CertificateAuthority: caURL.String(),
  491. CertificateAuthorityFingerprint: testRootFingerprint,
  492. CertificateIssuer: &apiv1.CertificateIssuer{
  493. Type: "jwk",
  494. Provisioner: "ra@smallstep.com",
  495. Key: "",
  496. },
  497. }}, nil, true},
  498. {"bad authority", args{context.TODO(), apiv1.Options{
  499. CertificateAuthority: "https://foobar",
  500. CertificateAuthorityFingerprint: testRootFingerprint,
  501. CertificateIssuer: &apiv1.CertificateIssuer{
  502. Type: "x5c",
  503. Provisioner: "X5C",
  504. Certificate: testX5CPath,
  505. Key: testX5CKeyPath,
  506. },
  507. }}, nil, true},
  508. {"fail parse url", args{context.TODO(), apiv1.Options{
  509. CertificateAuthority: "::failparse",
  510. CertificateAuthorityFingerprint: testRootFingerprint,
  511. CertificateIssuer: &apiv1.CertificateIssuer{
  512. Type: "x5c",
  513. Provisioner: "X5C",
  514. Certificate: testX5CPath,
  515. Key: testX5CKeyPath,
  516. },
  517. }}, nil, true},
  518. {"fail new client", args{context.TODO(), apiv1.Options{
  519. CertificateAuthority: caURL.String(),
  520. CertificateAuthorityFingerprint: "foobar",
  521. CertificateIssuer: &apiv1.CertificateIssuer{
  522. Type: "x5c",
  523. Provisioner: "X5C",
  524. Certificate: testX5CPath,
  525. Key: testX5CKeyPath,
  526. },
  527. }}, nil, true},
  528. {"fail new x5c issuer", args{context.TODO(), apiv1.Options{
  529. CertificateAuthority: caURL.String(),
  530. CertificateAuthorityFingerprint: testRootFingerprint,
  531. CertificateIssuer: &apiv1.CertificateIssuer{
  532. Type: "x5c",
  533. Provisioner: "X5C",
  534. Certificate: testX5CPath + ".missing",
  535. Key: testX5CKeyPath,
  536. },
  537. }}, nil, true},
  538. {"fail new jwk issuer", args{context.TODO(), apiv1.Options{
  539. CertificateAuthority: caURL.String(),
  540. CertificateAuthorityFingerprint: testRootFingerprint,
  541. CertificateIssuer: &apiv1.CertificateIssuer{
  542. Type: "jwk",
  543. Provisioner: "ra@doe.org",
  544. Key: testX5CKeyPath + ".missing",
  545. },
  546. }}, nil, true},
  547. {"bad issuer", args{context.TODO(), apiv1.Options{
  548. CertificateAuthority: caURL.String(),
  549. CertificateAuthorityFingerprint: testRootFingerprint,
  550. CertificateIssuer: nil}}, nil, true},
  551. {"bad issuer type", args{context.TODO(), apiv1.Options{
  552. CertificateAuthority: caURL.String(),
  553. CertificateAuthorityFingerprint: testRootFingerprint,
  554. CertificateIssuer: &apiv1.CertificateIssuer{
  555. Type: "fail",
  556. Provisioner: "X5C",
  557. Certificate: testX5CPath,
  558. Key: testX5CKeyPath,
  559. },
  560. }}, nil, true},
  561. }
  562. for _, tt := range tests {
  563. t.Run(tt.name, func(t *testing.T) {
  564. got, err := New(tt.args.ctx, tt.args.opts)
  565. if (err != nil) != tt.wantErr {
  566. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  567. return
  568. }
  569. // We cannot compare neither the client nor the signer.
  570. if got != nil && tt.want != nil {
  571. got.client = tt.want.client
  572. if jwk, ok := got.iss.(*jwkIssuer); ok {
  573. jwk.signer = signer
  574. }
  575. }
  576. if !reflect.DeepEqual(got, tt.want) {
  577. t.Errorf("New() = %v, want %v", got, tt.want)
  578. }
  579. })
  580. }
  581. }
  582. func TestStepCAS_CreateCertificate(t *testing.T) {
  583. caURL, client := testCAHelper(t)
  584. x5c := testX5CIssuer(t, caURL, "")
  585. jwk := testJWKIssuer(t, caURL, "")
  586. x5cEnc := testX5CIssuer(t, caURL, testPassword)
  587. jwkEnc := testJWKIssuer(t, caURL, testPassword)
  588. x5cBad := testX5CIssuer(t, caURL, "bad-password")
  589. type fields struct {
  590. iss stepIssuer
  591. client *ca.Client
  592. fingerprint string
  593. }
  594. type args struct {
  595. req *apiv1.CreateCertificateRequest
  596. }
  597. tests := []struct {
  598. name string
  599. fields fields
  600. args args
  601. want *apiv1.CreateCertificateResponse
  602. wantErr bool
  603. }{
  604. {"ok", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  605. CSR: testCR,
  606. Lifetime: time.Hour,
  607. }}, &apiv1.CreateCertificateResponse{
  608. Certificate: testCrt,
  609. CertificateChain: []*x509.Certificate{testIssCrt},
  610. }, false},
  611. {"ok with password", fields{x5cEnc, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  612. CSR: testCR,
  613. Lifetime: time.Hour,
  614. }}, &apiv1.CreateCertificateResponse{
  615. Certificate: testCrt,
  616. CertificateChain: []*x509.Certificate{testIssCrt},
  617. }, false},
  618. {"ok jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  619. CSR: testCR,
  620. Lifetime: time.Hour,
  621. }}, &apiv1.CreateCertificateResponse{
  622. Certificate: testCrt,
  623. CertificateChain: []*x509.Certificate{testIssCrt},
  624. }, false},
  625. {"ok jwk with password", fields{jwkEnc, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  626. CSR: testCR,
  627. Lifetime: time.Hour,
  628. }}, &apiv1.CreateCertificateResponse{
  629. Certificate: testCrt,
  630. CertificateChain: []*x509.Certificate{testIssCrt},
  631. }, false},
  632. {"ok with provisioner", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  633. CSR: testCR,
  634. Lifetime: time.Hour,
  635. Provisioner: &apiv1.ProvisionerInfo{ID: "provisioner-id", Type: "ACME"},
  636. }}, &apiv1.CreateCertificateResponse{
  637. Certificate: testCrt,
  638. CertificateChain: []*x509.Certificate{testIssCrt},
  639. }, false},
  640. {"ok with server cert", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  641. CSR: testCR,
  642. Lifetime: time.Hour,
  643. IsCAServerCert: true,
  644. }}, &apiv1.CreateCertificateResponse{
  645. Certificate: testCrt,
  646. CertificateChain: []*x509.Certificate{testIssCrt},
  647. }, false},
  648. {"fail CSR", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  649. CSR: nil,
  650. Lifetime: time.Hour,
  651. }}, nil, true},
  652. {"fail lifetime", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  653. CSR: testCR,
  654. Lifetime: 0,
  655. }}, nil, true},
  656. {"fail sign token", fields{mockErrIssuer{}, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  657. CSR: testCR,
  658. Lifetime: time.Hour,
  659. }}, nil, true},
  660. {"fail client sign", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  661. CSR: testFailCR,
  662. Lifetime: time.Hour,
  663. }}, nil, true},
  664. {"fail password", fields{x5cBad, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  665. CSR: testCR,
  666. Lifetime: time.Hour,
  667. }}, nil, true},
  668. }
  669. for _, tt := range tests {
  670. t.Run(tt.name, func(t *testing.T) {
  671. s := &StepCAS{
  672. iss: tt.fields.iss,
  673. client: tt.fields.client,
  674. authorityID: "authority-id",
  675. fingerprint: tt.fields.fingerprint,
  676. }
  677. got, err := s.CreateCertificate(tt.args.req)
  678. if (err != nil) != tt.wantErr {
  679. t.Errorf("StepCAS.CreateCertificate() error = %v, wantErr %v", err, tt.wantErr)
  680. return
  681. }
  682. if !reflect.DeepEqual(got, tt.want) {
  683. t.Errorf("StepCAS.CreateCertificate() = %v, want %v", got, tt.want)
  684. }
  685. })
  686. }
  687. }
  688. func TestStepCAS_RenewCertificate(t *testing.T) {
  689. caURL, client := testCAHelper(t)
  690. x5c := testX5CIssuer(t, caURL, "")
  691. jwk := testJWKIssuer(t, caURL, "")
  692. type fields struct {
  693. iss stepIssuer
  694. client *ca.Client
  695. fingerprint string
  696. }
  697. type args struct {
  698. req *apiv1.RenewCertificateRequest
  699. }
  700. tests := []struct {
  701. name string
  702. fields fields
  703. args args
  704. want *apiv1.RenewCertificateResponse
  705. wantErr bool
  706. }{
  707. {"not implemented", fields{x5c, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
  708. CSR: testCR,
  709. Lifetime: time.Hour,
  710. }}, nil, true},
  711. {"not implemented jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
  712. CSR: testCR,
  713. Lifetime: time.Hour,
  714. }}, nil, true},
  715. }
  716. for _, tt := range tests {
  717. t.Run(tt.name, func(t *testing.T) {
  718. s := &StepCAS{
  719. iss: tt.fields.iss,
  720. client: tt.fields.client,
  721. fingerprint: tt.fields.fingerprint,
  722. }
  723. got, err := s.RenewCertificate(tt.args.req)
  724. if (err != nil) != tt.wantErr {
  725. t.Errorf("StepCAS.RenewCertificate() error = %v, wantErr %v", err, tt.wantErr)
  726. return
  727. }
  728. if !reflect.DeepEqual(got, tt.want) {
  729. t.Errorf("StepCAS.RenewCertificate() = %v, want %v", got, tt.want)
  730. }
  731. })
  732. }
  733. }
  734. func TestStepCAS_RevokeCertificate(t *testing.T) {
  735. caURL, client := testCAHelper(t)
  736. x5c := testX5CIssuer(t, caURL, "")
  737. jwk := testJWKIssuer(t, caURL, "")
  738. x5cEnc := testX5CIssuer(t, caURL, testPassword)
  739. jwkEnc := testJWKIssuer(t, caURL, testPassword)
  740. x5cBad := testX5CIssuer(t, caURL, "bad-password")
  741. type fields struct {
  742. iss stepIssuer
  743. client *ca.Client
  744. fingerprint string
  745. }
  746. type args struct {
  747. req *apiv1.RevokeCertificateRequest
  748. }
  749. tests := []struct {
  750. name string
  751. fields fields
  752. args args
  753. want *apiv1.RevokeCertificateResponse
  754. wantErr bool
  755. }{
  756. {"ok serial number", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  757. SerialNumber: "ok",
  758. Certificate: nil,
  759. }}, &apiv1.RevokeCertificateResponse{}, false},
  760. {"ok certificate", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  761. SerialNumber: "",
  762. Certificate: testCrt,
  763. }}, &apiv1.RevokeCertificateResponse{
  764. Certificate: testCrt,
  765. }, false},
  766. {"ok both", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  767. SerialNumber: "ok",
  768. Certificate: testCrt,
  769. }}, &apiv1.RevokeCertificateResponse{
  770. Certificate: testCrt,
  771. }, false},
  772. {"ok with password", fields{x5cEnc, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  773. SerialNumber: "ok",
  774. Certificate: nil,
  775. }}, &apiv1.RevokeCertificateResponse{}, false},
  776. {"ok serial number jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  777. SerialNumber: "ok",
  778. Certificate: nil,
  779. }}, &apiv1.RevokeCertificateResponse{}, false},
  780. {"ok certificate jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  781. SerialNumber: "",
  782. Certificate: testCrt,
  783. }}, &apiv1.RevokeCertificateResponse{
  784. Certificate: testCrt,
  785. }, false},
  786. {"ok both jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  787. SerialNumber: "ok",
  788. Certificate: testCrt,
  789. }}, &apiv1.RevokeCertificateResponse{
  790. Certificate: testCrt,
  791. }, false},
  792. {"ok jwk with password", fields{jwkEnc, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  793. SerialNumber: "ok",
  794. Certificate: nil,
  795. }}, &apiv1.RevokeCertificateResponse{}, false},
  796. {"fail request", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  797. SerialNumber: "",
  798. Certificate: nil,
  799. }}, nil, true},
  800. {"fail revoke token", fields{mockErrIssuer{}, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  801. SerialNumber: "ok",
  802. }}, nil, true},
  803. {"fail client revoke", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  804. SerialNumber: "fail",
  805. }}, nil, true},
  806. {"fail password", fields{x5cBad, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  807. SerialNumber: "ok",
  808. Certificate: nil,
  809. }}, nil, true},
  810. }
  811. for _, tt := range tests {
  812. t.Run(tt.name, func(t *testing.T) {
  813. s := &StepCAS{
  814. iss: tt.fields.iss,
  815. client: tt.fields.client,
  816. fingerprint: tt.fields.fingerprint,
  817. }
  818. got, err := s.RevokeCertificate(tt.args.req)
  819. if (err != nil) != tt.wantErr {
  820. t.Errorf("StepCAS.RevokeCertificate() error = %v, wantErr %v", err, tt.wantErr)
  821. return
  822. }
  823. if !reflect.DeepEqual(got, tt.want) {
  824. t.Errorf("StepCAS.RevokeCertificate() = %v, want %v", got, tt.want)
  825. }
  826. })
  827. }
  828. }
  829. func TestStepCAS_GetCertificateAuthority(t *testing.T) {
  830. caURL, client := testCAHelper(t)
  831. x5c := testX5CIssuer(t, caURL, "")
  832. jwk := testJWKIssuer(t, caURL, "")
  833. type fields struct {
  834. iss stepIssuer
  835. client *ca.Client
  836. fingerprint string
  837. }
  838. type args struct {
  839. req *apiv1.GetCertificateAuthorityRequest
  840. }
  841. tests := []struct {
  842. name string
  843. fields fields
  844. args args
  845. want *apiv1.GetCertificateAuthorityResponse
  846. wantErr bool
  847. }{
  848. {"ok", fields{x5c, client, testRootFingerprint}, args{&apiv1.GetCertificateAuthorityRequest{
  849. Name: caURL.String(),
  850. }}, &apiv1.GetCertificateAuthorityResponse{
  851. RootCertificate: testRootCrt,
  852. }, false},
  853. {"ok jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.GetCertificateAuthorityRequest{
  854. Name: caURL.String(),
  855. }}, &apiv1.GetCertificateAuthorityResponse{
  856. RootCertificate: testRootCrt,
  857. }, false},
  858. {"fail fingerprint", fields{x5c, client, "fail"}, args{&apiv1.GetCertificateAuthorityRequest{
  859. Name: caURL.String(),
  860. }}, nil, true},
  861. }
  862. for _, tt := range tests {
  863. t.Run(tt.name, func(t *testing.T) {
  864. s := &StepCAS{
  865. iss: tt.fields.iss,
  866. client: tt.fields.client,
  867. fingerprint: tt.fields.fingerprint,
  868. }
  869. got, err := s.GetCertificateAuthority(tt.args.req)
  870. if (err != nil) != tt.wantErr {
  871. t.Errorf("StepCAS.GetCertificateAuthority() error = %v, wantErr %v", err, tt.wantErr)
  872. return
  873. }
  874. if !reflect.DeepEqual(got, tt.want) {
  875. t.Errorf("StepCAS.GetCertificateAuthority() = %v, want %v", got, tt.want)
  876. }
  877. })
  878. }
  879. }