stepcas_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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 := ioutil.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 := ioutil.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. {"fail authority", args{context.TODO(), apiv1.Options{
  382. CertificateAuthority: "",
  383. CertificateAuthorityFingerprint: testRootFingerprint,
  384. CertificateIssuer: &apiv1.CertificateIssuer{
  385. Type: "x5c",
  386. Provisioner: "X5C",
  387. Certificate: testX5CPath,
  388. Key: testX5CKeyPath,
  389. },
  390. }}, nil, true},
  391. {"fail fingerprint", args{context.TODO(), apiv1.Options{
  392. CertificateAuthority: caURL.String(),
  393. CertificateAuthorityFingerprint: "",
  394. CertificateIssuer: &apiv1.CertificateIssuer{
  395. Type: "x5c",
  396. Provisioner: "X5C",
  397. Certificate: testX5CPath,
  398. Key: testX5CKeyPath,
  399. },
  400. }}, nil, true},
  401. {"fail type", args{context.TODO(), apiv1.Options{
  402. CertificateAuthority: caURL.String(),
  403. CertificateAuthorityFingerprint: testRootFingerprint,
  404. CertificateIssuer: &apiv1.CertificateIssuer{
  405. Type: "",
  406. Provisioner: "X5C",
  407. Certificate: testX5CPath,
  408. Key: testX5CKeyPath,
  409. },
  410. }}, nil, true},
  411. {"fail provisioner", args{context.TODO(), apiv1.Options{
  412. CertificateAuthority: caURL.String(),
  413. CertificateAuthorityFingerprint: testRootFingerprint,
  414. CertificateIssuer: &apiv1.CertificateIssuer{
  415. Type: "x5c",
  416. Provisioner: "",
  417. Certificate: testX5CPath,
  418. Key: testX5CKeyPath,
  419. },
  420. }}, nil, true},
  421. {"fail provisioner jwk", args{context.TODO(), apiv1.Options{
  422. CertificateAuthority: caURL.String(),
  423. CertificateAuthorityFingerprint: testRootFingerprint,
  424. CertificateIssuer: &apiv1.CertificateIssuer{
  425. Type: "jwk",
  426. Provisioner: "",
  427. Key: testX5CKeyPath,
  428. },
  429. }}, nil, true},
  430. {"fail provisioner not found", args{context.TODO(), apiv1.Options{
  431. CertificateAuthority: caURL.String(),
  432. CertificateAuthorityFingerprint: testRootFingerprint,
  433. CertificateIssuer: &apiv1.CertificateIssuer{
  434. Type: "jwk",
  435. Provisioner: "notfound@doe.org",
  436. Password: testPassword,
  437. },
  438. }}, nil, true},
  439. {"fail invalid password", args{context.TODO(), apiv1.Options{
  440. CertificateAuthority: caURL.String(),
  441. CertificateAuthorityFingerprint: testRootFingerprint,
  442. CertificateIssuer: &apiv1.CertificateIssuer{
  443. Type: "jwk",
  444. Provisioner: "ra@doe.org",
  445. Password: "bad-password",
  446. },
  447. }}, nil, true},
  448. {"fail no key", args{context.TODO(), apiv1.Options{
  449. CertificateAuthority: caURL.String(),
  450. CertificateAuthorityFingerprint: testRootFingerprint,
  451. CertificateIssuer: &apiv1.CertificateIssuer{
  452. Type: "jwk",
  453. Provisioner: "empty@doe.org",
  454. Password: testPassword,
  455. },
  456. }}, nil, true},
  457. {"fail certificate", args{context.TODO(), apiv1.Options{
  458. CertificateAuthority: caURL.String(),
  459. CertificateAuthorityFingerprint: testRootFingerprint,
  460. CertificateIssuer: &apiv1.CertificateIssuer{
  461. Type: "x5c",
  462. Provisioner: "X5C",
  463. Certificate: "",
  464. Key: testX5CKeyPath,
  465. },
  466. }}, nil, true},
  467. {"fail key", args{context.TODO(), apiv1.Options{
  468. CertificateAuthority: caURL.String(),
  469. CertificateAuthorityFingerprint: testRootFingerprint,
  470. CertificateIssuer: &apiv1.CertificateIssuer{
  471. Type: "x5c",
  472. Provisioner: "X5C",
  473. Certificate: testX5CPath,
  474. Key: "",
  475. },
  476. }}, nil, true},
  477. {"fail key jwk", args{context.TODO(), apiv1.Options{
  478. CertificateAuthority: caURL.String(),
  479. CertificateAuthorityFingerprint: testRootFingerprint,
  480. CertificateIssuer: &apiv1.CertificateIssuer{
  481. Type: "jwk",
  482. Provisioner: "ra@smallstep.com",
  483. Key: "",
  484. },
  485. }}, nil, true},
  486. {"bad authority", args{context.TODO(), apiv1.Options{
  487. CertificateAuthority: "https://foobar",
  488. CertificateAuthorityFingerprint: testRootFingerprint,
  489. CertificateIssuer: &apiv1.CertificateIssuer{
  490. Type: "x5c",
  491. Provisioner: "X5C",
  492. Certificate: testX5CPath,
  493. Key: testX5CKeyPath,
  494. },
  495. }}, nil, true},
  496. {"fail parse url", args{context.TODO(), apiv1.Options{
  497. CertificateAuthority: "::failparse",
  498. CertificateAuthorityFingerprint: testRootFingerprint,
  499. CertificateIssuer: &apiv1.CertificateIssuer{
  500. Type: "x5c",
  501. Provisioner: "X5C",
  502. Certificate: testX5CPath,
  503. Key: testX5CKeyPath,
  504. },
  505. }}, nil, true},
  506. {"fail new client", args{context.TODO(), apiv1.Options{
  507. CertificateAuthority: caURL.String(),
  508. CertificateAuthorityFingerprint: "foobar",
  509. CertificateIssuer: &apiv1.CertificateIssuer{
  510. Type: "x5c",
  511. Provisioner: "X5C",
  512. Certificate: testX5CPath,
  513. Key: testX5CKeyPath,
  514. },
  515. }}, nil, true},
  516. {"fail new x5c issuer", args{context.TODO(), apiv1.Options{
  517. CertificateAuthority: caURL.String(),
  518. CertificateAuthorityFingerprint: testRootFingerprint,
  519. CertificateIssuer: &apiv1.CertificateIssuer{
  520. Type: "x5c",
  521. Provisioner: "X5C",
  522. Certificate: testX5CPath + ".missing",
  523. Key: testX5CKeyPath,
  524. },
  525. }}, nil, true},
  526. {"fail new jwk issuer", args{context.TODO(), apiv1.Options{
  527. CertificateAuthority: caURL.String(),
  528. CertificateAuthorityFingerprint: testRootFingerprint,
  529. CertificateIssuer: &apiv1.CertificateIssuer{
  530. Type: "jwk",
  531. Provisioner: "ra@doe.org",
  532. Key: testX5CKeyPath + ".missing",
  533. },
  534. }}, nil, true},
  535. {"bad issuer", args{context.TODO(), apiv1.Options{
  536. CertificateAuthority: caURL.String(),
  537. CertificateAuthorityFingerprint: testRootFingerprint,
  538. CertificateIssuer: nil}}, nil, true},
  539. {"bad issuer type", args{context.TODO(), apiv1.Options{
  540. CertificateAuthority: caURL.String(),
  541. CertificateAuthorityFingerprint: testRootFingerprint,
  542. CertificateIssuer: &apiv1.CertificateIssuer{
  543. Type: "fail",
  544. Provisioner: "X5C",
  545. Certificate: testX5CPath,
  546. Key: testX5CKeyPath,
  547. },
  548. }}, nil, true},
  549. }
  550. for _, tt := range tests {
  551. t.Run(tt.name, func(t *testing.T) {
  552. got, err := New(tt.args.ctx, tt.args.opts)
  553. if (err != nil) != tt.wantErr {
  554. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  555. return
  556. }
  557. // We cannot compare neither the client nor the signer.
  558. if got != nil && tt.want != nil {
  559. got.client = tt.want.client
  560. if jwk, ok := got.iss.(*jwkIssuer); ok {
  561. jwk.signer = signer
  562. }
  563. }
  564. if !reflect.DeepEqual(got, tt.want) {
  565. t.Errorf("New() = %v, want %v", got, tt.want)
  566. }
  567. })
  568. }
  569. }
  570. func TestStepCAS_CreateCertificate(t *testing.T) {
  571. caURL, client := testCAHelper(t)
  572. x5c := testX5CIssuer(t, caURL, "")
  573. jwk := testJWKIssuer(t, caURL, "")
  574. x5cEnc := testX5CIssuer(t, caURL, testPassword)
  575. jwkEnc := testJWKIssuer(t, caURL, testPassword)
  576. x5cBad := testX5CIssuer(t, caURL, "bad-password")
  577. type fields struct {
  578. iss stepIssuer
  579. client *ca.Client
  580. fingerprint string
  581. }
  582. type args struct {
  583. req *apiv1.CreateCertificateRequest
  584. }
  585. tests := []struct {
  586. name string
  587. fields fields
  588. args args
  589. want *apiv1.CreateCertificateResponse
  590. wantErr bool
  591. }{
  592. {"ok", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  593. CSR: testCR,
  594. Lifetime: time.Hour,
  595. }}, &apiv1.CreateCertificateResponse{
  596. Certificate: testCrt,
  597. CertificateChain: []*x509.Certificate{testIssCrt},
  598. }, false},
  599. {"ok with password", fields{x5cEnc, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  600. CSR: testCR,
  601. Lifetime: time.Hour,
  602. }}, &apiv1.CreateCertificateResponse{
  603. Certificate: testCrt,
  604. CertificateChain: []*x509.Certificate{testIssCrt},
  605. }, false},
  606. {"ok jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  607. CSR: testCR,
  608. Lifetime: time.Hour,
  609. }}, &apiv1.CreateCertificateResponse{
  610. Certificate: testCrt,
  611. CertificateChain: []*x509.Certificate{testIssCrt},
  612. }, false},
  613. {"ok jwk with password", fields{jwkEnc, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  614. CSR: testCR,
  615. Lifetime: time.Hour,
  616. }}, &apiv1.CreateCertificateResponse{
  617. Certificate: testCrt,
  618. CertificateChain: []*x509.Certificate{testIssCrt},
  619. }, false},
  620. {"fail CSR", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  621. CSR: nil,
  622. Lifetime: time.Hour,
  623. }}, nil, true},
  624. {"fail lifetime", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  625. CSR: testCR,
  626. Lifetime: 0,
  627. }}, nil, true},
  628. {"fail sign token", fields{mockErrIssuer{}, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  629. CSR: testCR,
  630. Lifetime: time.Hour,
  631. }}, nil, true},
  632. {"fail client sign", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  633. CSR: testFailCR,
  634. Lifetime: time.Hour,
  635. }}, nil, true},
  636. {"fail password", fields{x5cBad, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  637. CSR: testCR,
  638. Lifetime: time.Hour,
  639. }}, nil, true},
  640. }
  641. for _, tt := range tests {
  642. t.Run(tt.name, func(t *testing.T) {
  643. s := &StepCAS{
  644. iss: tt.fields.iss,
  645. client: tt.fields.client,
  646. fingerprint: tt.fields.fingerprint,
  647. }
  648. got, err := s.CreateCertificate(tt.args.req)
  649. if (err != nil) != tt.wantErr {
  650. t.Errorf("StepCAS.CreateCertificate() error = %v, wantErr %v", err, tt.wantErr)
  651. return
  652. }
  653. if !reflect.DeepEqual(got, tt.want) {
  654. t.Errorf("StepCAS.CreateCertificate() = %v, want %v", got, tt.want)
  655. }
  656. })
  657. }
  658. }
  659. func TestStepCAS_RenewCertificate(t *testing.T) {
  660. caURL, client := testCAHelper(t)
  661. x5c := testX5CIssuer(t, caURL, "")
  662. jwk := testJWKIssuer(t, caURL, "")
  663. type fields struct {
  664. iss stepIssuer
  665. client *ca.Client
  666. fingerprint string
  667. }
  668. type args struct {
  669. req *apiv1.RenewCertificateRequest
  670. }
  671. tests := []struct {
  672. name string
  673. fields fields
  674. args args
  675. want *apiv1.RenewCertificateResponse
  676. wantErr bool
  677. }{
  678. {"not implemented", fields{x5c, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
  679. CSR: testCR,
  680. Lifetime: time.Hour,
  681. }}, nil, true},
  682. {"not implemented jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
  683. CSR: testCR,
  684. Lifetime: time.Hour,
  685. }}, nil, true},
  686. }
  687. for _, tt := range tests {
  688. t.Run(tt.name, func(t *testing.T) {
  689. s := &StepCAS{
  690. iss: tt.fields.iss,
  691. client: tt.fields.client,
  692. fingerprint: tt.fields.fingerprint,
  693. }
  694. got, err := s.RenewCertificate(tt.args.req)
  695. if (err != nil) != tt.wantErr {
  696. t.Errorf("StepCAS.RenewCertificate() error = %v, wantErr %v", err, tt.wantErr)
  697. return
  698. }
  699. if !reflect.DeepEqual(got, tt.want) {
  700. t.Errorf("StepCAS.RenewCertificate() = %v, want %v", got, tt.want)
  701. }
  702. })
  703. }
  704. }
  705. func TestStepCAS_RevokeCertificate(t *testing.T) {
  706. caURL, client := testCAHelper(t)
  707. x5c := testX5CIssuer(t, caURL, "")
  708. jwk := testJWKIssuer(t, caURL, "")
  709. x5cEnc := testX5CIssuer(t, caURL, testPassword)
  710. jwkEnc := testJWKIssuer(t, caURL, testPassword)
  711. x5cBad := testX5CIssuer(t, caURL, "bad-password")
  712. type fields struct {
  713. iss stepIssuer
  714. client *ca.Client
  715. fingerprint string
  716. }
  717. type args struct {
  718. req *apiv1.RevokeCertificateRequest
  719. }
  720. tests := []struct {
  721. name string
  722. fields fields
  723. args args
  724. want *apiv1.RevokeCertificateResponse
  725. wantErr bool
  726. }{
  727. {"ok serial number", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  728. SerialNumber: "ok",
  729. Certificate: nil,
  730. }}, &apiv1.RevokeCertificateResponse{}, false},
  731. {"ok certificate", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  732. SerialNumber: "",
  733. Certificate: testCrt,
  734. }}, &apiv1.RevokeCertificateResponse{
  735. Certificate: testCrt,
  736. }, false},
  737. {"ok both", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  738. SerialNumber: "ok",
  739. Certificate: testCrt,
  740. }}, &apiv1.RevokeCertificateResponse{
  741. Certificate: testCrt,
  742. }, false},
  743. {"ok with password", fields{x5cEnc, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  744. SerialNumber: "ok",
  745. Certificate: nil,
  746. }}, &apiv1.RevokeCertificateResponse{}, false},
  747. {"ok serial number jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  748. SerialNumber: "ok",
  749. Certificate: nil,
  750. }}, &apiv1.RevokeCertificateResponse{}, false},
  751. {"ok certificate jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  752. SerialNumber: "",
  753. Certificate: testCrt,
  754. }}, &apiv1.RevokeCertificateResponse{
  755. Certificate: testCrt,
  756. }, false},
  757. {"ok both jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  758. SerialNumber: "ok",
  759. Certificate: testCrt,
  760. }}, &apiv1.RevokeCertificateResponse{
  761. Certificate: testCrt,
  762. }, false},
  763. {"ok jwk with password", fields{jwkEnc, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  764. SerialNumber: "ok",
  765. Certificate: nil,
  766. }}, &apiv1.RevokeCertificateResponse{}, false},
  767. {"fail request", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  768. SerialNumber: "",
  769. Certificate: nil,
  770. }}, nil, true},
  771. {"fail revoke token", fields{mockErrIssuer{}, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  772. SerialNumber: "ok",
  773. }}, nil, true},
  774. {"fail client revoke", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  775. SerialNumber: "fail",
  776. }}, nil, true},
  777. {"fail password", fields{x5cBad, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  778. SerialNumber: "ok",
  779. Certificate: nil,
  780. }}, nil, true},
  781. }
  782. for _, tt := range tests {
  783. t.Run(tt.name, func(t *testing.T) {
  784. s := &StepCAS{
  785. iss: tt.fields.iss,
  786. client: tt.fields.client,
  787. fingerprint: tt.fields.fingerprint,
  788. }
  789. got, err := s.RevokeCertificate(tt.args.req)
  790. if (err != nil) != tt.wantErr {
  791. t.Errorf("StepCAS.RevokeCertificate() error = %v, wantErr %v", err, tt.wantErr)
  792. return
  793. }
  794. if !reflect.DeepEqual(got, tt.want) {
  795. t.Errorf("StepCAS.RevokeCertificate() = %v, want %v", got, tt.want)
  796. }
  797. })
  798. }
  799. }
  800. func TestStepCAS_GetCertificateAuthority(t *testing.T) {
  801. caURL, client := testCAHelper(t)
  802. x5c := testX5CIssuer(t, caURL, "")
  803. jwk := testJWKIssuer(t, caURL, "")
  804. type fields struct {
  805. iss stepIssuer
  806. client *ca.Client
  807. fingerprint string
  808. }
  809. type args struct {
  810. req *apiv1.GetCertificateAuthorityRequest
  811. }
  812. tests := []struct {
  813. name string
  814. fields fields
  815. args args
  816. want *apiv1.GetCertificateAuthorityResponse
  817. wantErr bool
  818. }{
  819. {"ok", fields{x5c, client, testRootFingerprint}, args{&apiv1.GetCertificateAuthorityRequest{
  820. Name: caURL.String(),
  821. }}, &apiv1.GetCertificateAuthorityResponse{
  822. RootCertificate: testRootCrt,
  823. }, false},
  824. {"ok jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.GetCertificateAuthorityRequest{
  825. Name: caURL.String(),
  826. }}, &apiv1.GetCertificateAuthorityResponse{
  827. RootCertificate: testRootCrt,
  828. }, false},
  829. {"fail fingerprint", fields{x5c, client, "fail"}, args{&apiv1.GetCertificateAuthorityRequest{
  830. Name: caURL.String(),
  831. }}, nil, true},
  832. }
  833. for _, tt := range tests {
  834. t.Run(tt.name, func(t *testing.T) {
  835. s := &StepCAS{
  836. iss: tt.fields.iss,
  837. client: tt.fields.client,
  838. fingerprint: tt.fields.fingerprint,
  839. }
  840. got, err := s.GetCertificateAuthority(tt.args.req)
  841. if (err != nil) != tt.wantErr {
  842. t.Errorf("StepCAS.GetCertificateAuthority() error = %v, wantErr %v", err, tt.wantErr)
  843. return
  844. }
  845. if !reflect.DeepEqual(got, tt.want) {
  846. t.Errorf("StepCAS.GetCertificateAuthority() = %v, want %v", got, tt.want)
  847. }
  848. })
  849. }
  850. }