stepcas_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 == "/renew":
  138. if r.Header.Get("Authorization") == "Bearer fail" {
  139. w.WriteHeader(http.StatusBadRequest)
  140. fmt.Fprintf(w, `{"error":"fail","message":"fail"}`)
  141. return
  142. }
  143. w.WriteHeader(http.StatusOK)
  144. writeJSON(w, api.SignResponse{
  145. CertChainPEM: []api.Certificate{api.NewCertificate(testCrt), api.NewCertificate(testIssCrt)},
  146. })
  147. case r.RequestURI == "/revoke":
  148. var msg api.RevokeRequest
  149. parseJSON(r, &msg)
  150. if msg.Serial == "fail" {
  151. w.WriteHeader(http.StatusBadRequest)
  152. fmt.Fprintf(w, `{"error":"fail","message":"fail"}`)
  153. return
  154. }
  155. w.WriteHeader(http.StatusOK)
  156. writeJSON(w, api.RevokeResponse{
  157. Status: "ok",
  158. })
  159. case r.RequestURI == "/provisioners":
  160. w.WriteHeader(http.StatusOK)
  161. writeJSON(w, api.ProvisionersResponse{
  162. NextCursor: "cursor",
  163. Provisioners: []provisioner.Interface{
  164. &provisioner.JWK{
  165. Type: "JWK",
  166. Name: "ra@doe.org",
  167. Key: &jose.JSONWebKey{KeyID: testKeyID, Key: testX5CKey.Public()},
  168. EncryptedKey: testEncryptedJWKKey,
  169. },
  170. &provisioner.JWK{
  171. Type: "JWK",
  172. Name: "empty@doe.org",
  173. Key: &jose.JSONWebKey{KeyID: testKeyID, Key: testX5CKey.Public()},
  174. },
  175. },
  176. })
  177. case r.RequestURI == "/provisioners?cursor=cursor":
  178. w.WriteHeader(http.StatusOK)
  179. writeJSON(w, api.ProvisionersResponse{})
  180. default:
  181. w.WriteHeader(http.StatusNotFound)
  182. fmt.Fprintf(w, `{"error":"not found"}`)
  183. }
  184. }))
  185. t.Cleanup(func() {
  186. srv.Close()
  187. })
  188. u, err := url.Parse(srv.URL)
  189. if err != nil {
  190. srv.Close()
  191. t.Fatal(err)
  192. }
  193. client, err := ca.NewClient(srv.URL, ca.WithTransport(http.DefaultTransport))
  194. if err != nil {
  195. srv.Close()
  196. t.Fatal(err)
  197. }
  198. return u, client
  199. }
  200. func testX5CIssuer(t *testing.T, caURL *url.URL, password string) *x5cIssuer {
  201. t.Helper()
  202. key, givenPassword := testX5CKeyPath, password
  203. if password != "" {
  204. key = testEncryptedKeyPath
  205. password = testPassword
  206. }
  207. x5c, err := newX5CIssuer(caURL, &apiv1.CertificateIssuer{
  208. Type: "x5c",
  209. Provisioner: "X5C",
  210. Certificate: testX5CPath,
  211. Key: key,
  212. Password: password,
  213. })
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. x5c.password = givenPassword
  218. return x5c
  219. }
  220. func testJWKIssuer(t *testing.T, caURL *url.URL, password string) *jwkIssuer {
  221. t.Helper()
  222. client, err := ca.NewClient(caURL.String(), ca.WithTransport(http.DefaultTransport))
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. key := testX5CKeyPath
  227. if password != "" {
  228. key = testEncryptedKeyPath
  229. password = testPassword
  230. }
  231. jwk, err := newJWKIssuer(context.TODO(), caURL, client, &apiv1.CertificateIssuer{
  232. Type: "jwk",
  233. Provisioner: "ra@doe.org",
  234. Key: key,
  235. Password: password,
  236. })
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. return jwk
  241. }
  242. func TestMain(m *testing.M) {
  243. testRootCrt, testRootKey = mustSignCertificate("Test Root Certificate", nil, x509util.DefaultRootTemplate, nil, nil)
  244. testIssCrt, testIssKey = mustSignCertificate("Test Intermediate Certificate", nil, x509util.DefaultIntermediateTemplate, testRootCrt, testRootKey)
  245. testX5CCrt, testX5CKey = mustSignCertificate("Test X5C Certificate", nil, x509util.DefaultLeafTemplate, testIssCrt, testIssKey)
  246. testRootFingerprint = x509util.Fingerprint(testRootCrt)
  247. // Final certificate.
  248. var err error
  249. sans := []string{"doe.org", "jane@doe.org", "127.0.0.1", "::1", "localhost", "uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6;name=value"}
  250. testCrt, testKey = mustSignCertificate("Test Certificate", sans, x509util.DefaultLeafTemplate, testIssCrt, testIssKey)
  251. testCR, err = x509util.CreateCertificateRequest("Test Certificate", sans, testKey)
  252. if err != nil {
  253. panic(err)
  254. }
  255. // CR used in errors.
  256. testFailCR, err = x509util.CreateCertificateRequest("", []string{"fail.doe.org"}, testKey)
  257. if err != nil {
  258. panic(err)
  259. }
  260. // Password used to encrypt the key.
  261. testPassword, err = randutil.Hex(32)
  262. if err != nil {
  263. panic(err)
  264. }
  265. // Encrypted JWK key used when the key is downloaded from the CA.
  266. jwe, err := jose.EncryptJWK(&jose.JSONWebKey{Key: testX5CKey}, []byte(testPassword))
  267. if err != nil {
  268. panic(err)
  269. }
  270. testEncryptedJWKKey, err = jwe.CompactSerialize()
  271. if err != nil {
  272. panic(err)
  273. }
  274. testKeyID, err = jose.Thumbprint(&jose.JSONWebKey{Key: testX5CKey})
  275. if err != nil {
  276. panic(err)
  277. }
  278. // Create test files.
  279. path, err := os.MkdirTemp(os.TempDir(), "stepcas")
  280. if err != nil {
  281. panic(err)
  282. }
  283. testRootPath = filepath.Join(path, "root_ca.crt")
  284. testRootKeyPath = filepath.Join(path, "root_ca.key")
  285. mustSerializeCrt(testRootPath, testRootCrt)
  286. mustSerializeKey(testRootKeyPath, testRootKey)
  287. testIssPath = filepath.Join(path, "intermediate_ca.crt")
  288. testIssKeyPath = filepath.Join(path, "intermediate_ca.key")
  289. mustSerializeCrt(testIssPath, testIssCrt)
  290. mustSerializeKey(testIssKeyPath, testIssKey)
  291. testX5CPath = filepath.Join(path, "x5c.crt")
  292. testX5CKeyPath = filepath.Join(path, "x5c.key")
  293. mustSerializeCrt(testX5CPath, testX5CCrt, testIssCrt)
  294. mustSerializeKey(testX5CKeyPath, testX5CKey)
  295. testEncryptedKeyPath = filepath.Join(path, "x5c.enc.key")
  296. mustEncryptKey(testEncryptedKeyPath, testX5CKey)
  297. code := m.Run()
  298. if err := os.RemoveAll(path); err != nil {
  299. panic(err)
  300. }
  301. os.Exit(code)
  302. }
  303. func Test_init(t *testing.T) {
  304. caURL, _ := testCAHelper(t)
  305. fn, ok := apiv1.LoadCertificateAuthorityServiceNewFunc(apiv1.StepCAS)
  306. if !ok {
  307. t.Errorf("apiv1.Register() ok = %v, want true", ok)
  308. return
  309. }
  310. fn(context.Background(), apiv1.Options{
  311. CertificateAuthority: caURL.String(),
  312. CertificateAuthorityFingerprint: testRootFingerprint,
  313. CertificateIssuer: &apiv1.CertificateIssuer{
  314. Type: "x5c",
  315. Provisioner: "X5C",
  316. Certificate: testX5CPath,
  317. Key: testX5CKeyPath,
  318. },
  319. })
  320. }
  321. func TestNew(t *testing.T) {
  322. caURL, client := testCAHelper(t)
  323. signer, err := newJWKSignerFromEncryptedKey(testKeyID, testEncryptedJWKKey, testPassword)
  324. if err != nil {
  325. t.Fatal(err)
  326. }
  327. type args struct {
  328. ctx context.Context
  329. opts apiv1.Options
  330. }
  331. tests := []struct {
  332. name string
  333. args args
  334. want *StepCAS
  335. wantErr bool
  336. }{
  337. {"ok", args{context.TODO(), apiv1.Options{
  338. CertificateAuthority: caURL.String(),
  339. CertificateAuthorityFingerprint: testRootFingerprint,
  340. CertificateIssuer: &apiv1.CertificateIssuer{
  341. Type: "x5c",
  342. Provisioner: "X5C",
  343. Certificate: testX5CPath,
  344. Key: testX5CKeyPath,
  345. },
  346. }}, &StepCAS{
  347. iss: &x5cIssuer{
  348. caURL: caURL,
  349. certFile: testX5CPath,
  350. keyFile: testX5CKeyPath,
  351. issuer: "X5C",
  352. },
  353. client: client,
  354. fingerprint: testRootFingerprint,
  355. }, false},
  356. {"ok jwk", args{context.TODO(), apiv1.Options{
  357. CertificateAuthority: caURL.String(),
  358. CertificateAuthorityFingerprint: testRootFingerprint,
  359. CertificateIssuer: &apiv1.CertificateIssuer{
  360. Type: "jwk",
  361. Provisioner: "ra@doe.org",
  362. Key: testX5CKeyPath,
  363. },
  364. }}, &StepCAS{
  365. iss: &jwkIssuer{
  366. caURL: caURL,
  367. issuer: "ra@doe.org",
  368. signer: signer,
  369. },
  370. client: client,
  371. fingerprint: testRootFingerprint,
  372. }, false},
  373. {"ok jwk provisioners", args{context.TODO(), apiv1.Options{
  374. CertificateAuthority: caURL.String(),
  375. CertificateAuthorityFingerprint: testRootFingerprint,
  376. CertificateIssuer: &apiv1.CertificateIssuer{
  377. Type: "jwk",
  378. Provisioner: "ra@doe.org",
  379. Password: testPassword,
  380. },
  381. }}, &StepCAS{
  382. iss: &jwkIssuer{
  383. caURL: caURL,
  384. issuer: "ra@doe.org",
  385. signer: signer,
  386. },
  387. client: client,
  388. fingerprint: testRootFingerprint,
  389. }, false},
  390. {"ok ca getter", args{context.TODO(), apiv1.Options{
  391. IsCAGetter: true,
  392. CertificateAuthority: caURL.String(),
  393. CertificateAuthorityFingerprint: testRootFingerprint,
  394. CertificateIssuer: &apiv1.CertificateIssuer{
  395. Type: "jwk",
  396. Provisioner: "ra@doe.org",
  397. },
  398. }}, &StepCAS{
  399. iss: nil,
  400. client: client,
  401. fingerprint: testRootFingerprint,
  402. }, false},
  403. {"fail authority", args{context.TODO(), apiv1.Options{
  404. CertificateAuthority: "",
  405. CertificateAuthorityFingerprint: testRootFingerprint,
  406. CertificateIssuer: &apiv1.CertificateIssuer{
  407. Type: "x5c",
  408. Provisioner: "X5C",
  409. Certificate: testX5CPath,
  410. Key: testX5CKeyPath,
  411. },
  412. }}, nil, true},
  413. {"fail fingerprint", args{context.TODO(), apiv1.Options{
  414. CertificateAuthority: caURL.String(),
  415. CertificateAuthorityFingerprint: "",
  416. CertificateIssuer: &apiv1.CertificateIssuer{
  417. Type: "x5c",
  418. Provisioner: "X5C",
  419. Certificate: testX5CPath,
  420. Key: testX5CKeyPath,
  421. },
  422. }}, nil, true},
  423. {"fail type", args{context.TODO(), apiv1.Options{
  424. CertificateAuthority: caURL.String(),
  425. CertificateAuthorityFingerprint: testRootFingerprint,
  426. CertificateIssuer: &apiv1.CertificateIssuer{
  427. Type: "",
  428. Provisioner: "X5C",
  429. Certificate: testX5CPath,
  430. Key: testX5CKeyPath,
  431. },
  432. }}, nil, true},
  433. {"fail provisioner", args{context.TODO(), apiv1.Options{
  434. CertificateAuthority: caURL.String(),
  435. CertificateAuthorityFingerprint: testRootFingerprint,
  436. CertificateIssuer: &apiv1.CertificateIssuer{
  437. Type: "x5c",
  438. Provisioner: "",
  439. Certificate: testX5CPath,
  440. Key: testX5CKeyPath,
  441. },
  442. }}, nil, true},
  443. {"fail provisioner jwk", args{context.TODO(), apiv1.Options{
  444. CertificateAuthority: caURL.String(),
  445. CertificateAuthorityFingerprint: testRootFingerprint,
  446. CertificateIssuer: &apiv1.CertificateIssuer{
  447. Type: "jwk",
  448. Provisioner: "",
  449. Key: testX5CKeyPath,
  450. },
  451. }}, nil, true},
  452. {"fail provisioner not found", args{context.TODO(), apiv1.Options{
  453. CertificateAuthority: caURL.String(),
  454. CertificateAuthorityFingerprint: testRootFingerprint,
  455. CertificateIssuer: &apiv1.CertificateIssuer{
  456. Type: "jwk",
  457. Provisioner: "notfound@doe.org",
  458. Password: testPassword,
  459. },
  460. }}, nil, true},
  461. {"fail invalid password", args{context.TODO(), apiv1.Options{
  462. CertificateAuthority: caURL.String(),
  463. CertificateAuthorityFingerprint: testRootFingerprint,
  464. CertificateIssuer: &apiv1.CertificateIssuer{
  465. Type: "jwk",
  466. Provisioner: "ra@doe.org",
  467. Password: "bad-password",
  468. },
  469. }}, nil, true},
  470. {"fail no key", args{context.TODO(), apiv1.Options{
  471. CertificateAuthority: caURL.String(),
  472. CertificateAuthorityFingerprint: testRootFingerprint,
  473. CertificateIssuer: &apiv1.CertificateIssuer{
  474. Type: "jwk",
  475. Provisioner: "empty@doe.org",
  476. Password: testPassword,
  477. },
  478. }}, nil, true},
  479. {"fail certificate", args{context.TODO(), apiv1.Options{
  480. CertificateAuthority: caURL.String(),
  481. CertificateAuthorityFingerprint: testRootFingerprint,
  482. CertificateIssuer: &apiv1.CertificateIssuer{
  483. Type: "x5c",
  484. Provisioner: "X5C",
  485. Certificate: "",
  486. Key: testX5CKeyPath,
  487. },
  488. }}, nil, true},
  489. {"fail key", args{context.TODO(), apiv1.Options{
  490. CertificateAuthority: caURL.String(),
  491. CertificateAuthorityFingerprint: testRootFingerprint,
  492. CertificateIssuer: &apiv1.CertificateIssuer{
  493. Type: "x5c",
  494. Provisioner: "X5C",
  495. Certificate: testX5CPath,
  496. Key: "",
  497. },
  498. }}, nil, true},
  499. {"fail key jwk", args{context.TODO(), apiv1.Options{
  500. CertificateAuthority: caURL.String(),
  501. CertificateAuthorityFingerprint: testRootFingerprint,
  502. CertificateIssuer: &apiv1.CertificateIssuer{
  503. Type: "jwk",
  504. Provisioner: "ra@smallstep.com",
  505. Key: "",
  506. },
  507. }}, nil, true},
  508. {"bad authority", args{context.TODO(), apiv1.Options{
  509. CertificateAuthority: "https://foobar",
  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 parse url", args{context.TODO(), apiv1.Options{
  519. CertificateAuthority: "::failparse",
  520. CertificateAuthorityFingerprint: testRootFingerprint,
  521. CertificateIssuer: &apiv1.CertificateIssuer{
  522. Type: "x5c",
  523. Provisioner: "X5C",
  524. Certificate: testX5CPath,
  525. Key: testX5CKeyPath,
  526. },
  527. }}, nil, true},
  528. {"fail new client", args{context.TODO(), apiv1.Options{
  529. CertificateAuthority: caURL.String(),
  530. CertificateAuthorityFingerprint: "foobar",
  531. CertificateIssuer: &apiv1.CertificateIssuer{
  532. Type: "x5c",
  533. Provisioner: "X5C",
  534. Certificate: testX5CPath,
  535. Key: testX5CKeyPath,
  536. },
  537. }}, nil, true},
  538. {"fail new x5c issuer", args{context.TODO(), apiv1.Options{
  539. CertificateAuthority: caURL.String(),
  540. CertificateAuthorityFingerprint: testRootFingerprint,
  541. CertificateIssuer: &apiv1.CertificateIssuer{
  542. Type: "x5c",
  543. Provisioner: "X5C",
  544. Certificate: testX5CPath + ".missing",
  545. Key: testX5CKeyPath,
  546. },
  547. }}, nil, true},
  548. {"fail new jwk issuer", args{context.TODO(), apiv1.Options{
  549. CertificateAuthority: caURL.String(),
  550. CertificateAuthorityFingerprint: testRootFingerprint,
  551. CertificateIssuer: &apiv1.CertificateIssuer{
  552. Type: "jwk",
  553. Provisioner: "ra@doe.org",
  554. Key: testX5CKeyPath + ".missing",
  555. },
  556. }}, nil, true},
  557. {"bad issuer", args{context.TODO(), apiv1.Options{
  558. CertificateAuthority: caURL.String(),
  559. CertificateAuthorityFingerprint: testRootFingerprint,
  560. CertificateIssuer: nil}}, nil, true},
  561. {"bad issuer type", args{context.TODO(), apiv1.Options{
  562. CertificateAuthority: caURL.String(),
  563. CertificateAuthorityFingerprint: testRootFingerprint,
  564. CertificateIssuer: &apiv1.CertificateIssuer{
  565. Type: "fail",
  566. Provisioner: "X5C",
  567. Certificate: testX5CPath,
  568. Key: testX5CKeyPath,
  569. },
  570. }}, nil, true},
  571. }
  572. for _, tt := range tests {
  573. t.Run(tt.name, func(t *testing.T) {
  574. got, err := New(tt.args.ctx, tt.args.opts)
  575. if (err != nil) != tt.wantErr {
  576. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  577. return
  578. }
  579. // We cannot compare neither the client nor the signer.
  580. if got != nil && tt.want != nil {
  581. got.client = tt.want.client
  582. if jwk, ok := got.iss.(*jwkIssuer); ok {
  583. jwk.signer = signer
  584. }
  585. }
  586. if !reflect.DeepEqual(got, tt.want) {
  587. t.Errorf("New() = %v, want %v", got, tt.want)
  588. }
  589. })
  590. }
  591. }
  592. func TestStepCAS_CreateCertificate(t *testing.T) {
  593. caURL, client := testCAHelper(t)
  594. x5c := testX5CIssuer(t, caURL, "")
  595. jwk := testJWKIssuer(t, caURL, "")
  596. x5cEnc := testX5CIssuer(t, caURL, testPassword)
  597. jwkEnc := testJWKIssuer(t, caURL, testPassword)
  598. x5cBad := testX5CIssuer(t, caURL, "bad-password")
  599. type fields struct {
  600. iss stepIssuer
  601. client *ca.Client
  602. fingerprint string
  603. }
  604. type args struct {
  605. req *apiv1.CreateCertificateRequest
  606. }
  607. tests := []struct {
  608. name string
  609. fields fields
  610. args args
  611. want *apiv1.CreateCertificateResponse
  612. wantErr bool
  613. }{
  614. {"ok", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  615. CSR: testCR,
  616. Lifetime: time.Hour,
  617. }}, &apiv1.CreateCertificateResponse{
  618. Certificate: testCrt,
  619. CertificateChain: []*x509.Certificate{testIssCrt},
  620. }, false},
  621. {"ok with password", fields{x5cEnc, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  622. CSR: testCR,
  623. Lifetime: time.Hour,
  624. }}, &apiv1.CreateCertificateResponse{
  625. Certificate: testCrt,
  626. CertificateChain: []*x509.Certificate{testIssCrt},
  627. }, false},
  628. {"ok jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  629. CSR: testCR,
  630. Lifetime: time.Hour,
  631. }}, &apiv1.CreateCertificateResponse{
  632. Certificate: testCrt,
  633. CertificateChain: []*x509.Certificate{testIssCrt},
  634. }, false},
  635. {"ok jwk with password", fields{jwkEnc, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  636. CSR: testCR,
  637. Lifetime: time.Hour,
  638. }}, &apiv1.CreateCertificateResponse{
  639. Certificate: testCrt,
  640. CertificateChain: []*x509.Certificate{testIssCrt},
  641. }, false},
  642. {"ok with provisioner", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  643. CSR: testCR,
  644. Lifetime: time.Hour,
  645. Provisioner: &apiv1.ProvisionerInfo{ID: "provisioner-id", Type: "ACME"},
  646. }}, &apiv1.CreateCertificateResponse{
  647. Certificate: testCrt,
  648. CertificateChain: []*x509.Certificate{testIssCrt},
  649. }, false},
  650. {"ok with server cert", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  651. CSR: testCR,
  652. Lifetime: time.Hour,
  653. IsCAServerCert: true,
  654. }}, &apiv1.CreateCertificateResponse{
  655. Certificate: testCrt,
  656. CertificateChain: []*x509.Certificate{testIssCrt},
  657. }, false},
  658. {"fail CSR", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  659. CSR: nil,
  660. Lifetime: time.Hour,
  661. }}, nil, true},
  662. {"fail lifetime", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  663. CSR: testCR,
  664. Lifetime: 0,
  665. }}, nil, true},
  666. {"fail sign token", fields{mockErrIssuer{}, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  667. CSR: testCR,
  668. Lifetime: time.Hour,
  669. }}, nil, true},
  670. {"fail client sign", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  671. CSR: testFailCR,
  672. Lifetime: time.Hour,
  673. }}, nil, true},
  674. {"fail password", fields{x5cBad, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
  675. CSR: testCR,
  676. Lifetime: time.Hour,
  677. }}, nil, true},
  678. }
  679. for _, tt := range tests {
  680. t.Run(tt.name, func(t *testing.T) {
  681. s := &StepCAS{
  682. iss: tt.fields.iss,
  683. client: tt.fields.client,
  684. authorityID: "authority-id",
  685. fingerprint: tt.fields.fingerprint,
  686. }
  687. got, err := s.CreateCertificate(tt.args.req)
  688. if (err != nil) != tt.wantErr {
  689. t.Errorf("StepCAS.CreateCertificate() error = %v, wantErr %v", err, tt.wantErr)
  690. return
  691. }
  692. if !reflect.DeepEqual(got, tt.want) {
  693. t.Errorf("StepCAS.CreateCertificate() = %v, want %v", got, tt.want)
  694. }
  695. })
  696. }
  697. }
  698. func TestStepCAS_RenewCertificate(t *testing.T) {
  699. caURL, client := testCAHelper(t)
  700. jwk := testJWKIssuer(t, caURL, "")
  701. tokenIssuer := testX5CIssuer(t, caURL, "")
  702. token, err := tokenIssuer.SignToken("test", []string{"test.example.com"}, nil)
  703. if err != nil {
  704. t.Fatal(err)
  705. }
  706. type fields struct {
  707. iss stepIssuer
  708. client *ca.Client
  709. fingerprint string
  710. }
  711. type args struct {
  712. req *apiv1.RenewCertificateRequest
  713. }
  714. tests := []struct {
  715. name string
  716. fields fields
  717. args args
  718. want *apiv1.RenewCertificateResponse
  719. wantErr bool
  720. }{
  721. {"ok", fields{jwk, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
  722. Template: &x509.Certificate{},
  723. Backdate: time.Minute,
  724. Lifetime: time.Hour,
  725. Token: token,
  726. }}, &apiv1.RenewCertificateResponse{
  727. Certificate: testCrt,
  728. CertificateChain: []*x509.Certificate{testIssCrt},
  729. }, false},
  730. {"fail no token", fields{jwk, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
  731. Template: &x509.Certificate{},
  732. Backdate: time.Minute,
  733. Lifetime: time.Hour,
  734. }}, nil, true},
  735. {"fail bad token", fields{jwk, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
  736. Template: &x509.Certificate{},
  737. Backdate: time.Minute,
  738. Lifetime: time.Hour,
  739. Token: "fail",
  740. }}, nil, true},
  741. }
  742. for _, tt := range tests {
  743. t.Run(tt.name, func(t *testing.T) {
  744. s := &StepCAS{
  745. iss: tt.fields.iss,
  746. client: tt.fields.client,
  747. fingerprint: tt.fields.fingerprint,
  748. }
  749. got, err := s.RenewCertificate(tt.args.req)
  750. if (err != nil) != tt.wantErr {
  751. t.Errorf("StepCAS.RenewCertificate() error = %v, wantErr %v", err, tt.wantErr)
  752. return
  753. }
  754. if !reflect.DeepEqual(got, tt.want) {
  755. t.Error(reflect.DeepEqual(got.Certificate, tt.want.Certificate))
  756. t.Error(reflect.DeepEqual(got.CertificateChain, tt.want.CertificateChain))
  757. t.Errorf("StepCAS.RenewCertificate() = %v, want %v", got.Certificate.Subject, tt.want.Certificate.Subject)
  758. }
  759. })
  760. }
  761. }
  762. func TestStepCAS_RevokeCertificate(t *testing.T) {
  763. caURL, client := testCAHelper(t)
  764. x5c := testX5CIssuer(t, caURL, "")
  765. jwk := testJWKIssuer(t, caURL, "")
  766. x5cEnc := testX5CIssuer(t, caURL, testPassword)
  767. jwkEnc := testJWKIssuer(t, caURL, testPassword)
  768. x5cBad := testX5CIssuer(t, caURL, "bad-password")
  769. type fields struct {
  770. iss stepIssuer
  771. client *ca.Client
  772. fingerprint string
  773. }
  774. type args struct {
  775. req *apiv1.RevokeCertificateRequest
  776. }
  777. tests := []struct {
  778. name string
  779. fields fields
  780. args args
  781. want *apiv1.RevokeCertificateResponse
  782. wantErr bool
  783. }{
  784. {"ok serial number", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  785. SerialNumber: "ok",
  786. Certificate: nil,
  787. }}, &apiv1.RevokeCertificateResponse{}, false},
  788. {"ok certificate", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  789. SerialNumber: "",
  790. Certificate: testCrt,
  791. }}, &apiv1.RevokeCertificateResponse{
  792. Certificate: testCrt,
  793. }, false},
  794. {"ok both", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  795. SerialNumber: "ok",
  796. Certificate: testCrt,
  797. }}, &apiv1.RevokeCertificateResponse{
  798. Certificate: testCrt,
  799. }, false},
  800. {"ok with password", fields{x5cEnc, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  801. SerialNumber: "ok",
  802. Certificate: nil,
  803. }}, &apiv1.RevokeCertificateResponse{}, false},
  804. {"ok serial number jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  805. SerialNumber: "ok",
  806. Certificate: nil,
  807. }}, &apiv1.RevokeCertificateResponse{}, false},
  808. {"ok certificate jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  809. SerialNumber: "",
  810. Certificate: testCrt,
  811. }}, &apiv1.RevokeCertificateResponse{
  812. Certificate: testCrt,
  813. }, false},
  814. {"ok both jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  815. SerialNumber: "ok",
  816. Certificate: testCrt,
  817. }}, &apiv1.RevokeCertificateResponse{
  818. Certificate: testCrt,
  819. }, false},
  820. {"ok jwk with password", fields{jwkEnc, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  821. SerialNumber: "ok",
  822. Certificate: nil,
  823. }}, &apiv1.RevokeCertificateResponse{}, false},
  824. {"fail request", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  825. SerialNumber: "",
  826. Certificate: nil,
  827. }}, nil, true},
  828. {"fail revoke token", fields{mockErrIssuer{}, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  829. SerialNumber: "ok",
  830. }}, nil, true},
  831. {"fail client revoke", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  832. SerialNumber: "fail",
  833. }}, nil, true},
  834. {"fail password", fields{x5cBad, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
  835. SerialNumber: "ok",
  836. Certificate: nil,
  837. }}, nil, true},
  838. }
  839. for _, tt := range tests {
  840. t.Run(tt.name, func(t *testing.T) {
  841. s := &StepCAS{
  842. iss: tt.fields.iss,
  843. client: tt.fields.client,
  844. fingerprint: tt.fields.fingerprint,
  845. }
  846. got, err := s.RevokeCertificate(tt.args.req)
  847. if (err != nil) != tt.wantErr {
  848. t.Errorf("StepCAS.RevokeCertificate() error = %v, wantErr %v", err, tt.wantErr)
  849. return
  850. }
  851. if !reflect.DeepEqual(got, tt.want) {
  852. t.Errorf("StepCAS.RevokeCertificate() = %v, want %v", got, tt.want)
  853. }
  854. })
  855. }
  856. }
  857. func TestStepCAS_GetCertificateAuthority(t *testing.T) {
  858. caURL, client := testCAHelper(t)
  859. x5c := testX5CIssuer(t, caURL, "")
  860. jwk := testJWKIssuer(t, caURL, "")
  861. type fields struct {
  862. iss stepIssuer
  863. client *ca.Client
  864. fingerprint string
  865. }
  866. type args struct {
  867. req *apiv1.GetCertificateAuthorityRequest
  868. }
  869. tests := []struct {
  870. name string
  871. fields fields
  872. args args
  873. want *apiv1.GetCertificateAuthorityResponse
  874. wantErr bool
  875. }{
  876. {"ok", fields{x5c, client, testRootFingerprint}, args{&apiv1.GetCertificateAuthorityRequest{
  877. Name: caURL.String(),
  878. }}, &apiv1.GetCertificateAuthorityResponse{
  879. RootCertificate: testRootCrt,
  880. }, false},
  881. {"ok jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.GetCertificateAuthorityRequest{
  882. Name: caURL.String(),
  883. }}, &apiv1.GetCertificateAuthorityResponse{
  884. RootCertificate: testRootCrt,
  885. }, false},
  886. {"fail fingerprint", fields{x5c, client, "fail"}, args{&apiv1.GetCertificateAuthorityRequest{
  887. Name: caURL.String(),
  888. }}, nil, true},
  889. }
  890. for _, tt := range tests {
  891. t.Run(tt.name, func(t *testing.T) {
  892. s := &StepCAS{
  893. iss: tt.fields.iss,
  894. client: tt.fields.client,
  895. fingerprint: tt.fields.fingerprint,
  896. }
  897. got, err := s.GetCertificateAuthority(tt.args.req)
  898. if (err != nil) != tt.wantErr {
  899. t.Errorf("StepCAS.GetCertificateAuthority() error = %v, wantErr %v", err, tt.wantErr)
  900. return
  901. }
  902. if !reflect.DeepEqual(got, tt.want) {
  903. t.Errorf("StepCAS.GetCertificateAuthority() = %v, want %v", got, tt.want)
  904. }
  905. })
  906. }
  907. }