stepcas_test.go 30 KB

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