stepcas_test.go 30 KB

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