stepcas_test.go 27 KB

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