scep_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/rand"
  6. "crypto/rsa"
  7. "crypto/x509"
  8. "crypto/x509/pkix"
  9. "encoding/json"
  10. "encoding/pem"
  11. "errors"
  12. "net/http"
  13. "net/http/httptest"
  14. "os"
  15. "path/filepath"
  16. "testing"
  17. "github.com/smallstep/certificates/webhook"
  18. "github.com/smallstep/linkedca"
  19. "github.com/stretchr/testify/assert"
  20. "github.com/stretchr/testify/require"
  21. "go.step.sm/crypto/kms/softkms"
  22. "go.step.sm/crypto/minica"
  23. "go.step.sm/crypto/pemutil"
  24. "go.step.sm/crypto/x509util"
  25. )
  26. func Test_challengeValidationController_Validate(t *testing.T) {
  27. dummyCSR := &x509.CertificateRequest{
  28. Raw: []byte{1},
  29. }
  30. type request struct {
  31. ProvisionerName string `json:"provisionerName,omitempty"`
  32. Request *webhook.X509CertificateRequest `json:"x509CertificateRequest,omitempty"`
  33. Challenge string `json:"scepChallenge"`
  34. TransactionID string `json:"scepTransactionID"`
  35. }
  36. type response struct {
  37. Allow bool `json:"allow"`
  38. Data any `json:"data"`
  39. }
  40. nokServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  41. req := &request{}
  42. err := json.NewDecoder(r.Body).Decode(req)
  43. require.NoError(t, err)
  44. assert.Equal(t, "my-scep-provisioner", req.ProvisionerName)
  45. assert.Equal(t, "not-allowed", req.Challenge)
  46. assert.Equal(t, "transaction-1", req.TransactionID)
  47. b, err := json.Marshal(response{Allow: false})
  48. require.NoError(t, err)
  49. w.WriteHeader(200)
  50. w.Write(b)
  51. }))
  52. okServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  53. req := &request{}
  54. err := json.NewDecoder(r.Body).Decode(req)
  55. require.NoError(t, err)
  56. assert.Equal(t, "my-scep-provisioner", req.ProvisionerName)
  57. assert.Equal(t, "challenge", req.Challenge)
  58. assert.Equal(t, "transaction-1", req.TransactionID)
  59. if assert.NotNil(t, req.Request) {
  60. assert.Equal(t, []byte{1}, req.Request.Raw)
  61. }
  62. resp := response{Allow: true}
  63. if r.Header.Get("X-Smallstep-Webhook-Id") == "webhook-id-2" {
  64. resp.Data = map[string]any{
  65. "ID": "2adcbfec-5e4a-4b93-8913-640e24faf101",
  66. "Email": "admin@example.com",
  67. }
  68. }
  69. b, err := json.Marshal(resp)
  70. require.NoError(t, err)
  71. w.WriteHeader(200)
  72. w.Write(b)
  73. }))
  74. t.Cleanup(func() {
  75. nokServer.Close()
  76. okServer.Close()
  77. })
  78. type fields struct {
  79. client *http.Client
  80. webhooks []*Webhook
  81. }
  82. type args struct {
  83. provisionerName string
  84. challenge string
  85. transactionID string
  86. }
  87. tests := []struct {
  88. name string
  89. fields fields
  90. args args
  91. want x509util.TemplateData
  92. expErr error
  93. }{
  94. {
  95. name: "fail/no-webhook",
  96. fields: fields{http.DefaultClient, nil},
  97. args: args{"my-scep-provisioner", "no-webhook", "transaction-1"},
  98. expErr: errors.New("webhook server did not allow request"),
  99. },
  100. {
  101. name: "fail/wrong-cert-type",
  102. fields: fields{http.DefaultClient, []*Webhook{
  103. {
  104. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  105. CertType: linkedca.Webhook_SSH.String(),
  106. },
  107. }},
  108. args: args{"my-scep-provisioner", "wrong-cert-type", "transaction-1"},
  109. expErr: errors.New("webhook server did not allow request"),
  110. },
  111. {
  112. name: "fail/wrong-secret-value",
  113. fields: fields{http.DefaultClient, []*Webhook{
  114. {
  115. ID: "webhook-id-1",
  116. Name: "webhook-name-1",
  117. Secret: "{{}}",
  118. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  119. CertType: linkedca.Webhook_X509.String(),
  120. URL: okServer.URL,
  121. },
  122. }},
  123. args: args{
  124. provisionerName: "my-scep-provisioner",
  125. challenge: "wrong-secret-value",
  126. transactionID: "transaction-1",
  127. },
  128. expErr: errors.New("failed executing webhook request: illegal base64 data at input byte 0"),
  129. },
  130. {
  131. name: "fail/not-allowed",
  132. fields: fields{http.DefaultClient, []*Webhook{
  133. {
  134. ID: "webhook-id-1",
  135. Name: "webhook-name-1",
  136. Secret: "MTIzNAo=",
  137. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  138. CertType: linkedca.Webhook_X509.String(),
  139. URL: nokServer.URL,
  140. },
  141. }},
  142. args: args{
  143. provisionerName: "my-scep-provisioner",
  144. challenge: "not-allowed",
  145. transactionID: "transaction-1",
  146. },
  147. expErr: errors.New("webhook server did not allow request"),
  148. },
  149. {
  150. name: "ok",
  151. fields: fields{http.DefaultClient, []*Webhook{
  152. {
  153. ID: "webhook-id-1",
  154. Name: "webhook-name-1",
  155. Secret: "MTIzNAo=",
  156. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  157. CertType: linkedca.Webhook_X509.String(),
  158. URL: okServer.URL,
  159. },
  160. }},
  161. args: args{
  162. provisionerName: "my-scep-provisioner",
  163. challenge: "challenge",
  164. transactionID: "transaction-1",
  165. },
  166. want: x509util.TemplateData{
  167. x509util.WebhooksKey: map[string]any{
  168. "webhook-name-1": nil,
  169. },
  170. },
  171. },
  172. {
  173. name: "ok with data",
  174. fields: fields{http.DefaultClient, []*Webhook{
  175. {
  176. ID: "webhook-id-2",
  177. Name: "webhook-name-2",
  178. Secret: "MTIzNAo=",
  179. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  180. CertType: linkedca.Webhook_X509.String(),
  181. URL: okServer.URL,
  182. },
  183. }},
  184. args: args{
  185. provisionerName: "my-scep-provisioner",
  186. challenge: "challenge",
  187. transactionID: "transaction-1",
  188. },
  189. want: x509util.TemplateData{
  190. x509util.WebhooksKey: map[string]any{
  191. "webhook-name-2": map[string]any{
  192. "ID": "2adcbfec-5e4a-4b93-8913-640e24faf101",
  193. "Email": "admin@example.com",
  194. },
  195. },
  196. },
  197. },
  198. }
  199. for _, tt := range tests {
  200. t.Run(tt.name, func(t *testing.T) {
  201. c := newChallengeValidationController(tt.fields.client, nil, tt.fields.webhooks)
  202. ctx := context.Background()
  203. got, err := c.Validate(ctx, dummyCSR, tt.args.provisionerName, tt.args.challenge, tt.args.transactionID)
  204. if tt.expErr != nil {
  205. assert.EqualError(t, err, tt.expErr.Error())
  206. return
  207. }
  208. assert.NoError(t, err)
  209. data := x509util.TemplateData{}
  210. for _, o := range got {
  211. if m, ok := o.(TemplateDataModifier); ok {
  212. m.Modify(data)
  213. } else {
  214. t.Errorf("Validate() got = %T, want TemplateDataModifier", o)
  215. }
  216. }
  217. assert.Equal(t, tt.want, data)
  218. })
  219. }
  220. }
  221. func TestController_isCertTypeOK(t *testing.T) {
  222. assert.True(t, isCertTypeOK(&Webhook{CertType: linkedca.Webhook_X509.String()}))
  223. assert.True(t, isCertTypeOK(&Webhook{CertType: linkedca.Webhook_ALL.String()}))
  224. assert.True(t, isCertTypeOK(&Webhook{CertType: ""}))
  225. assert.False(t, isCertTypeOK(&Webhook{CertType: linkedca.Webhook_SSH.String()}))
  226. }
  227. func Test_selectValidationMethod(t *testing.T) {
  228. tests := []struct {
  229. name string
  230. p *SCEP
  231. want validationMethod
  232. }{
  233. {"webhooks", &SCEP{
  234. Name: "SCEP",
  235. Type: "SCEP",
  236. Options: &Options{
  237. Webhooks: []*Webhook{
  238. {
  239. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  240. },
  241. },
  242. },
  243. }, "webhook"},
  244. {"challenge", &SCEP{
  245. Name: "SCEP",
  246. Type: "SCEP",
  247. ChallengePassword: "pass",
  248. }, "static"},
  249. {"challenge-with-different-webhook", &SCEP{
  250. Name: "SCEP",
  251. Type: "SCEP",
  252. Options: &Options{
  253. Webhooks: []*Webhook{
  254. {
  255. Kind: linkedca.Webhook_AUTHORIZING.String(),
  256. },
  257. },
  258. },
  259. ChallengePassword: "pass",
  260. }, "static"},
  261. {"none", &SCEP{
  262. Name: "SCEP",
  263. Type: "SCEP",
  264. }, "none"},
  265. {"none-with-different-webhook", &SCEP{
  266. Name: "SCEP",
  267. Type: "SCEP",
  268. Options: &Options{
  269. Webhooks: []*Webhook{
  270. {
  271. Kind: linkedca.Webhook_AUTHORIZING.String(),
  272. },
  273. },
  274. },
  275. }, "none"},
  276. }
  277. for _, tt := range tests {
  278. t.Run(tt.name, func(t *testing.T) {
  279. err := tt.p.Init(Config{Claims: globalProvisionerClaims})
  280. require.NoError(t, err)
  281. got := tt.p.selectValidationMethod()
  282. assert.Equal(t, tt.want, got)
  283. })
  284. }
  285. }
  286. func TestSCEP_ValidateChallenge(t *testing.T) {
  287. dummyCSR := &x509.CertificateRequest{
  288. Raw: []byte{1},
  289. }
  290. type request struct {
  291. ProvisionerName string `json:"provisionerName,omitempty"`
  292. Request *webhook.X509CertificateRequest `json:"x509CertificateRequest,omitempty"`
  293. Challenge string `json:"scepChallenge"`
  294. TransactionID string `json:"scepTransactionID"`
  295. }
  296. type response struct {
  297. Allow bool `json:"allow"`
  298. Data any `json:"data"`
  299. }
  300. okServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  301. req := &request{}
  302. err := json.NewDecoder(r.Body).Decode(req)
  303. require.NoError(t, err)
  304. assert.Equal(t, "SCEP", req.ProvisionerName)
  305. assert.Equal(t, "webhook-challenge", req.Challenge)
  306. assert.Equal(t, "webhook-transaction-1", req.TransactionID)
  307. if assert.NotNil(t, req.Request) {
  308. assert.Equal(t, []byte{1}, req.Request.Raw)
  309. }
  310. resp := response{Allow: true}
  311. if r.Header.Get("X-Smallstep-Webhook-Id") == "webhook-id-2" {
  312. resp.Data = map[string]any{
  313. "ID": "2adcbfec-5e4a-4b93-8913-640e24faf101",
  314. "Email": "admin@example.com",
  315. }
  316. }
  317. b, err := json.Marshal(resp)
  318. require.NoError(t, err)
  319. w.WriteHeader(200)
  320. w.Write(b)
  321. }))
  322. t.Cleanup(okServer.Close)
  323. type args struct {
  324. challenge string
  325. transactionID string
  326. }
  327. tests := []struct {
  328. name string
  329. p *SCEP
  330. server *httptest.Server
  331. args args
  332. want x509util.TemplateData
  333. expErr error
  334. }{
  335. {"ok/webhooks", &SCEP{
  336. Name: "SCEP",
  337. Type: "SCEP",
  338. Options: &Options{
  339. Webhooks: []*Webhook{
  340. {
  341. ID: "webhook-id-1",
  342. Name: "webhook-name-1",
  343. Secret: "MTIzNAo=",
  344. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  345. CertType: linkedca.Webhook_X509.String(),
  346. URL: okServer.URL,
  347. },
  348. },
  349. },
  350. }, okServer, args{"webhook-challenge", "webhook-transaction-1"}, x509util.TemplateData{
  351. x509util.WebhooksKey: map[string]any{
  352. "webhook-name-1": nil,
  353. },
  354. }, nil},
  355. {"ok/with-data", &SCEP{
  356. Name: "SCEP",
  357. Type: "SCEP",
  358. Options: &Options{
  359. Webhooks: []*Webhook{
  360. {
  361. ID: "webhook-id-1",
  362. Name: "webhook-name-1",
  363. Secret: "MTIzNAo=",
  364. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  365. CertType: linkedca.Webhook_X509.String(),
  366. URL: okServer.URL,
  367. },
  368. {
  369. ID: "webhook-id-2",
  370. Name: "webhook-name-2",
  371. Secret: "MTIzNAo=",
  372. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  373. CertType: linkedca.Webhook_X509.String(),
  374. URL: okServer.URL,
  375. },
  376. },
  377. },
  378. }, okServer, args{"webhook-challenge", "webhook-transaction-1"}, x509util.TemplateData{
  379. x509util.WebhooksKey: map[string]any{
  380. "webhook-name-1": nil,
  381. "webhook-name-2": map[string]any{
  382. "ID": "2adcbfec-5e4a-4b93-8913-640e24faf101",
  383. "Email": "admin@example.com",
  384. },
  385. },
  386. }, nil},
  387. {"fail/webhooks-secret-configuration", &SCEP{
  388. Name: "SCEP",
  389. Type: "SCEP",
  390. Options: &Options{
  391. Webhooks: []*Webhook{
  392. {
  393. ID: "webhook-id-1",
  394. Name: "webhook-name-1",
  395. Secret: "{{}}",
  396. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  397. CertType: linkedca.Webhook_X509.String(),
  398. URL: okServer.URL,
  399. },
  400. },
  401. },
  402. }, nil, args{"webhook-challenge", "webhook-transaction-1"}, nil, errors.New("failed executing webhook request: illegal base64 data at input byte 0")},
  403. {"ok/static-challenge", &SCEP{
  404. Name: "SCEP",
  405. Type: "SCEP",
  406. Options: &Options{},
  407. ChallengePassword: "secret-static-challenge",
  408. }, nil, args{"secret-static-challenge", "static-transaction-1"}, x509util.TemplateData{}, nil},
  409. {"fail/wrong-static-challenge", &SCEP{
  410. Name: "SCEP",
  411. Type: "SCEP",
  412. Options: &Options{},
  413. ChallengePassword: "secret-static-challenge",
  414. }, nil, args{"the-wrong-challenge-secret", "static-transaction-1"}, nil, errors.New("invalid challenge password provided")},
  415. {"ok/no-challenge", &SCEP{
  416. Name: "SCEP",
  417. Type: "SCEP",
  418. Options: &Options{},
  419. ChallengePassword: "",
  420. }, nil, args{"", "static-transaction-1"}, x509util.TemplateData{}, nil},
  421. {"fail/no-challenge-but-provided", &SCEP{
  422. Name: "SCEP",
  423. Type: "SCEP",
  424. Options: &Options{},
  425. ChallengePassword: "",
  426. }, nil, args{"a-challenge-value", "static-transaction-1"}, nil, errors.New("invalid challenge password provided")},
  427. }
  428. for _, tt := range tests {
  429. t.Run(tt.name, func(t *testing.T) {
  430. err := tt.p.Init(Config{Claims: globalProvisionerClaims, WebhookClient: http.DefaultClient})
  431. require.NoError(t, err)
  432. ctx := context.Background()
  433. got, err := tt.p.ValidateChallenge(ctx, dummyCSR, tt.args.challenge, tt.args.transactionID)
  434. if tt.expErr != nil {
  435. assert.EqualError(t, err, tt.expErr.Error())
  436. return
  437. }
  438. assert.NoError(t, err)
  439. data := x509util.TemplateData{}
  440. for _, o := range got {
  441. if m, ok := o.(TemplateDataModifier); ok {
  442. m.Modify(data)
  443. } else {
  444. t.Errorf("Validate() got = %T, want TemplateDataModifier", o)
  445. }
  446. }
  447. assert.Equal(t, tt.want, data)
  448. })
  449. }
  450. }
  451. func TestSCEP_Init(t *testing.T) {
  452. serialize := func(key crypto.PrivateKey, password string) []byte {
  453. var opts []pemutil.Options
  454. if password == "" {
  455. opts = append(opts, pemutil.WithPasswordPrompt("no password", func(s string) ([]byte, error) {
  456. return nil, nil
  457. }))
  458. } else {
  459. opts = append(opts, pemutil.WithPassword([]byte("password")))
  460. }
  461. block, err := pemutil.Serialize(key, opts...)
  462. require.NoError(t, err)
  463. return pem.EncodeToMemory(block)
  464. }
  465. ca, err := minica.New()
  466. require.NoError(t, err)
  467. key, err := rsa.GenerateKey(rand.Reader, 2048)
  468. require.NoError(t, err)
  469. badKey, err := rsa.GenerateKey(rand.Reader, 2048)
  470. require.NoError(t, err)
  471. cert, err := ca.Sign(&x509.Certificate{
  472. Subject: pkix.Name{CommonName: "SCEP decryptor"},
  473. PublicKey: key.Public(),
  474. })
  475. require.NoError(t, err)
  476. certPEM := pem.EncodeToMemory(&pem.Block{
  477. Type: "CERTIFICATE", Bytes: cert.Raw,
  478. })
  479. certPEMWithIntermediate := append(pem.EncodeToMemory(&pem.Block{
  480. Type: "CERTIFICATE", Bytes: cert.Raw,
  481. }), pem.EncodeToMemory(&pem.Block{
  482. Type: "CERTIFICATE", Bytes: ca.Intermediate.Raw,
  483. })...)
  484. keyPEM := serialize(key, "password")
  485. keyPEMNoPassword := serialize(key, "")
  486. badKeyPEM := serialize(badKey, "password")
  487. tmp := t.TempDir()
  488. path := filepath.Join(tmp, "rsa.priv")
  489. pathNoPassword := filepath.Join(tmp, "rsa.key")
  490. require.NoError(t, os.WriteFile(path, keyPEM, 0600))
  491. require.NoError(t, os.WriteFile(pathNoPassword, keyPEMNoPassword, 0600))
  492. type args struct {
  493. config Config
  494. }
  495. tests := []struct {
  496. name string
  497. s *SCEP
  498. args args
  499. wantErr bool
  500. }{
  501. {"ok", &SCEP{
  502. Type: "SCEP",
  503. Name: "scep",
  504. ChallengePassword: "password123",
  505. MinimumPublicKeyLength: 0,
  506. DecrypterCertificate: certPEM,
  507. DecrypterKeyPEM: keyPEM,
  508. DecrypterKeyPassword: "password",
  509. EncryptionAlgorithmIdentifier: 0,
  510. }, args{Config{Claims: globalProvisionerClaims}}, false},
  511. {"ok no password", &SCEP{
  512. Type: "SCEP",
  513. Name: "scep",
  514. ChallengePassword: "password123",
  515. MinimumPublicKeyLength: 0,
  516. DecrypterCertificate: certPEM,
  517. DecrypterKeyPEM: keyPEMNoPassword,
  518. DecrypterKeyPassword: "",
  519. EncryptionAlgorithmIdentifier: 1,
  520. }, args{Config{Claims: globalProvisionerClaims}}, false},
  521. {"ok with uri", &SCEP{
  522. Type: "SCEP",
  523. Name: "scep",
  524. ChallengePassword: "password123",
  525. MinimumPublicKeyLength: 1024,
  526. DecrypterCertificate: certPEM,
  527. DecrypterKeyURI: "softkms:path=" + path,
  528. DecrypterKeyPassword: "password",
  529. EncryptionAlgorithmIdentifier: 2,
  530. }, args{Config{Claims: globalProvisionerClaims}}, false},
  531. {"ok with uri no password", &SCEP{
  532. Type: "SCEP",
  533. Name: "scep",
  534. ChallengePassword: "password123",
  535. MinimumPublicKeyLength: 2048,
  536. DecrypterCertificate: certPEM,
  537. DecrypterKeyURI: "softkms:path=" + pathNoPassword,
  538. DecrypterKeyPassword: "",
  539. EncryptionAlgorithmIdentifier: 3,
  540. }, args{Config{Claims: globalProvisionerClaims}}, false},
  541. {"ok with SCEPKeyManager", &SCEP{
  542. Type: "SCEP",
  543. Name: "scep",
  544. ChallengePassword: "password123",
  545. MinimumPublicKeyLength: 2048,
  546. DecrypterCertificate: certPEM,
  547. DecrypterKeyURI: "softkms:path=" + pathNoPassword,
  548. DecrypterKeyPassword: "",
  549. EncryptionAlgorithmIdentifier: 4,
  550. }, args{Config{Claims: globalProvisionerClaims, SCEPKeyManager: &softkms.SoftKMS{}}}, false},
  551. {"ok intermediate", &SCEP{
  552. Type: "SCEP",
  553. Name: "scep",
  554. ChallengePassword: "password123",
  555. MinimumPublicKeyLength: 0,
  556. DecrypterCertificate: nil,
  557. DecrypterKeyPEM: nil,
  558. DecrypterKeyPassword: "",
  559. EncryptionAlgorithmIdentifier: 0,
  560. }, args{Config{Claims: globalProvisionerClaims}}, false},
  561. {"fail type", &SCEP{
  562. Type: "",
  563. Name: "scep",
  564. ChallengePassword: "password123",
  565. MinimumPublicKeyLength: 0,
  566. DecrypterCertificate: certPEM,
  567. DecrypterKeyPEM: keyPEM,
  568. DecrypterKeyPassword: "password",
  569. EncryptionAlgorithmIdentifier: 0,
  570. }, args{Config{Claims: globalProvisionerClaims}}, true},
  571. {"fail name", &SCEP{
  572. Type: "SCEP",
  573. Name: "",
  574. ChallengePassword: "password123",
  575. MinimumPublicKeyLength: 0,
  576. DecrypterCertificate: certPEM,
  577. DecrypterKeyPEM: keyPEM,
  578. DecrypterKeyPassword: "password",
  579. EncryptionAlgorithmIdentifier: 0,
  580. }, args{Config{Claims: globalProvisionerClaims}}, true},
  581. {"fail minimumPublicKeyLength", &SCEP{
  582. Type: "SCEP",
  583. Name: "scep",
  584. ChallengePassword: "password123",
  585. MinimumPublicKeyLength: 2001,
  586. DecrypterCertificate: certPEM,
  587. DecrypterKeyPEM: keyPEM,
  588. DecrypterKeyPassword: "password",
  589. EncryptionAlgorithmIdentifier: 0,
  590. }, args{Config{Claims: globalProvisionerClaims}}, true},
  591. {"fail encryptionAlgorithmIdentifier", &SCEP{
  592. Type: "SCEP",
  593. Name: "scep",
  594. ChallengePassword: "password123",
  595. MinimumPublicKeyLength: 0,
  596. DecrypterCertificate: certPEM,
  597. DecrypterKeyPEM: keyPEM,
  598. DecrypterKeyPassword: "password",
  599. EncryptionAlgorithmIdentifier: 5,
  600. }, args{Config{Claims: globalProvisionerClaims}}, true},
  601. {"fail negative encryptionAlgorithmIdentifier", &SCEP{
  602. Type: "SCEP",
  603. Name: "scep",
  604. ChallengePassword: "password123",
  605. MinimumPublicKeyLength: 0,
  606. DecrypterCertificate: certPEM,
  607. DecrypterKeyPEM: keyPEM,
  608. DecrypterKeyPassword: "password",
  609. EncryptionAlgorithmIdentifier: -1,
  610. }, args{Config{Claims: globalProvisionerClaims}}, true},
  611. {"fail key decode", &SCEP{
  612. Type: "SCEP",
  613. Name: "scep",
  614. ChallengePassword: "password123",
  615. MinimumPublicKeyLength: 0,
  616. DecrypterCertificate: certPEM,
  617. DecrypterKeyPEM: []byte("not a pem"),
  618. DecrypterKeyPassword: "password",
  619. EncryptionAlgorithmIdentifier: 0,
  620. }, args{Config{Claims: globalProvisionerClaims}}, true},
  621. {"fail certificate decode", &SCEP{
  622. Type: "SCEP",
  623. Name: "scep",
  624. ChallengePassword: "password123",
  625. MinimumPublicKeyLength: 0,
  626. DecrypterCertificate: []byte("not a pem"),
  627. DecrypterKeyPEM: keyPEM,
  628. DecrypterKeyPassword: "password",
  629. EncryptionAlgorithmIdentifier: 0,
  630. }, args{Config{Claims: globalProvisionerClaims}}, true},
  631. {"fail certificate with intermediate", &SCEP{
  632. Type: "SCEP",
  633. Name: "scep",
  634. ChallengePassword: "password123",
  635. MinimumPublicKeyLength: 0,
  636. DecrypterCertificate: certPEMWithIntermediate,
  637. DecrypterKeyPEM: keyPEM,
  638. DecrypterKeyPassword: "password",
  639. }, args{Config{Claims: globalProvisionerClaims}}, true},
  640. {"fail decrypter password", &SCEP{
  641. Type: "SCEP",
  642. Name: "scep",
  643. ChallengePassword: "password123",
  644. MinimumPublicKeyLength: 0,
  645. DecrypterCertificate: certPEM,
  646. DecrypterKeyPEM: keyPEM,
  647. DecrypterKeyPassword: "badpassword",
  648. EncryptionAlgorithmIdentifier: 0,
  649. }, args{Config{Claims: globalProvisionerClaims}}, true},
  650. {"fail uri", &SCEP{
  651. Type: "SCEP",
  652. Name: "scep",
  653. ChallengePassword: "password123",
  654. MinimumPublicKeyLength: 0,
  655. DecrypterCertificate: certPEM,
  656. DecrypterKeyURI: "softkms:path=missing.key",
  657. DecrypterKeyPassword: "password",
  658. EncryptionAlgorithmIdentifier: 0,
  659. }, args{Config{Claims: globalProvisionerClaims}}, true},
  660. {"fail uri password", &SCEP{
  661. Type: "SCEP",
  662. Name: "scep",
  663. ChallengePassword: "password123",
  664. MinimumPublicKeyLength: 0,
  665. DecrypterCertificate: certPEM,
  666. DecrypterKeyURI: "softkms:path=" + path,
  667. DecrypterKeyPassword: "badpassword",
  668. EncryptionAlgorithmIdentifier: 0,
  669. }, args{Config{Claims: globalProvisionerClaims}}, true},
  670. {"fail uri type", &SCEP{
  671. Type: "SCEP",
  672. Name: "scep",
  673. ChallengePassword: "password123",
  674. MinimumPublicKeyLength: 0,
  675. DecrypterCertificate: certPEM,
  676. DecrypterKeyURI: "foo:path=" + path,
  677. DecrypterKeyPassword: "password",
  678. EncryptionAlgorithmIdentifier: 0,
  679. }, args{Config{Claims: globalProvisionerClaims}}, true},
  680. {"fail missing certificate", &SCEP{
  681. Type: "SCEP",
  682. Name: "scep",
  683. ChallengePassword: "password123",
  684. MinimumPublicKeyLength: 0,
  685. DecrypterCertificate: nil,
  686. DecrypterKeyPEM: keyPEM,
  687. DecrypterKeyPassword: "password",
  688. EncryptionAlgorithmIdentifier: 0,
  689. }, args{Config{Claims: globalProvisionerClaims}}, true},
  690. {"fail key match", &SCEP{
  691. Type: "SCEP",
  692. Name: "scep",
  693. ChallengePassword: "password123",
  694. MinimumPublicKeyLength: 0,
  695. DecrypterCertificate: certPEM,
  696. DecrypterKeyPEM: badKeyPEM,
  697. DecrypterKeyPassword: "password",
  698. EncryptionAlgorithmIdentifier: 0,
  699. }, args{Config{Claims: globalProvisionerClaims}}, true},
  700. }
  701. for _, tt := range tests {
  702. t.Run(tt.name, func(t *testing.T) {
  703. if err := tt.s.Init(tt.args.config); (err != nil) != tt.wantErr {
  704. t.Errorf("SCEP.Init() error = %v, wantErr %v", err, tt.wantErr)
  705. }
  706. })
  707. }
  708. }