pkcs11_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. // +build cgo
  2. package pkcs11
  3. import (
  4. "context"
  5. "crypto"
  6. "crypto/ecdsa"
  7. "crypto/ed25519"
  8. "crypto/rand"
  9. "crypto/rsa"
  10. "crypto/x509"
  11. "math/big"
  12. "reflect"
  13. "strings"
  14. "testing"
  15. "github.com/ThalesIgnite/crypto11"
  16. "github.com/pkg/errors"
  17. "github.com/smallstep/certificates/kms/apiv1"
  18. "golang.org/x/crypto/cryptobyte"
  19. "golang.org/x/crypto/cryptobyte/asn1"
  20. )
  21. func TestNew(t *testing.T) {
  22. tmp := p11Configure
  23. t.Cleanup(func() {
  24. p11Configure = tmp
  25. })
  26. k := mustPKCS11(t)
  27. t.Cleanup(func() {
  28. k.Close()
  29. })
  30. p11Configure = func(config *crypto11.Config) (P11, error) {
  31. if strings.Contains(config.Path, "fail") {
  32. return nil, errors.New("an error")
  33. }
  34. return k.p11, nil
  35. }
  36. type args struct {
  37. ctx context.Context
  38. opts apiv1.Options
  39. }
  40. tests := []struct {
  41. name string
  42. args args
  43. want *PKCS11
  44. wantErr bool
  45. }{
  46. {"ok", args{context.Background(), apiv1.Options{
  47. Type: "pkcs11",
  48. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test?pin-value=password",
  49. }}, k, false},
  50. {"ok with serial", args{context.Background(), apiv1.Options{
  51. Type: "pkcs11",
  52. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;serial=0123456789?pin-value=password",
  53. }}, k, false},
  54. {"ok with slot-id", args{context.Background(), apiv1.Options{
  55. Type: "pkcs11",
  56. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;slot-id=0?pin-value=password",
  57. }}, k, false},
  58. {"ok with pin", args{context.Background(), apiv1.Options{
  59. Type: "pkcs11",
  60. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test",
  61. Pin: "passowrd",
  62. }}, k, false},
  63. {"fail missing module", args{context.Background(), apiv1.Options{
  64. Type: "pkcs11",
  65. URI: "pkcs11:token=pkcs11-test",
  66. Pin: "passowrd",
  67. }}, nil, true},
  68. {"fail missing pin", args{context.Background(), apiv1.Options{
  69. Type: "pkcs11",
  70. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test",
  71. }}, nil, true},
  72. {"fail missing token/serial/slot-id", args{context.Background(), apiv1.Options{
  73. Type: "pkcs11",
  74. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so",
  75. Pin: "passowrd",
  76. }}, nil, true},
  77. {"fail token+serial+slot-id", args{context.Background(), apiv1.Options{
  78. Type: "pkcs11",
  79. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test;serial=0123456789;slot-id=0",
  80. Pin: "passowrd",
  81. }}, nil, true},
  82. {"fail token+serial", args{context.Background(), apiv1.Options{
  83. Type: "pkcs11",
  84. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test;serial=0123456789",
  85. Pin: "passowrd",
  86. }}, nil, true},
  87. {"fail token+slot-id", args{context.Background(), apiv1.Options{
  88. Type: "pkcs11",
  89. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test;slot-id=0",
  90. Pin: "passowrd",
  91. }}, nil, true},
  92. {"fail serial+slot-id", args{context.Background(), apiv1.Options{
  93. Type: "pkcs11",
  94. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;serial=0123456789;slot-id=0",
  95. Pin: "passowrd",
  96. }}, nil, true},
  97. {"fail slot-id", args{context.Background(), apiv1.Options{
  98. Type: "pkcs11",
  99. URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;slot-id=x?pin-value=password",
  100. }}, nil, true},
  101. {"fail scheme", args{context.Background(), apiv1.Options{
  102. Type: "pkcs11",
  103. URI: "foo:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test?pin-value=password",
  104. }}, nil, true},
  105. {"fail configure", args{context.Background(), apiv1.Options{
  106. Type: "pkcs11",
  107. URI: "pkcs11:module-path=/usr/local/lib/fail.so;token=pkcs11-test?pin-value=password",
  108. }}, nil, true},
  109. }
  110. for _, tt := range tests {
  111. t.Run(tt.name, func(t *testing.T) {
  112. got, err := New(tt.args.ctx, tt.args.opts)
  113. if (err != nil) != tt.wantErr {
  114. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  115. return
  116. }
  117. if !reflect.DeepEqual(got, tt.want) {
  118. t.Errorf("New() = %v, want %v", got, tt.want)
  119. }
  120. })
  121. }
  122. }
  123. func TestPKCS11_GetPublicKey(t *testing.T) {
  124. k := setupPKCS11(t)
  125. type args struct {
  126. req *apiv1.GetPublicKeyRequest
  127. }
  128. tests := []struct {
  129. name string
  130. args args
  131. want crypto.PublicKey
  132. wantErr bool
  133. }{
  134. {"RSA", args{&apiv1.GetPublicKeyRequest{
  135. Name: "pkcs11:id=7371;object=rsa-key",
  136. }}, &rsa.PublicKey{}, false},
  137. {"RSA by id", args{&apiv1.GetPublicKeyRequest{
  138. Name: "pkcs11:id=7371",
  139. }}, &rsa.PublicKey{}, false},
  140. {"RSA by label", args{&apiv1.GetPublicKeyRequest{
  141. Name: "pkcs11:object=rsa-key",
  142. }}, &rsa.PublicKey{}, false},
  143. {"ECDSA", args{&apiv1.GetPublicKeyRequest{
  144. Name: "pkcs11:id=7373;object=ecdsa-p256-key",
  145. }}, &ecdsa.PublicKey{}, false},
  146. {"ECDSA by id", args{&apiv1.GetPublicKeyRequest{
  147. Name: "pkcs11:id=7373",
  148. }}, &ecdsa.PublicKey{}, false},
  149. {"ECDSA by label", args{&apiv1.GetPublicKeyRequest{
  150. Name: "pkcs11:object=ecdsa-p256-key",
  151. }}, &ecdsa.PublicKey{}, false},
  152. {"fail name", args{&apiv1.GetPublicKeyRequest{
  153. Name: "",
  154. }}, nil, true},
  155. {"fail uri", args{&apiv1.GetPublicKeyRequest{
  156. Name: "https:id=9999;object=https",
  157. }}, nil, true},
  158. {"fail missing", args{&apiv1.GetPublicKeyRequest{
  159. Name: "pkcs11:id=9999;object=rsa-key",
  160. }}, nil, true},
  161. {"fail FindKeyPair", args{&apiv1.GetPublicKeyRequest{
  162. Name: "pkcs11:foo=bar",
  163. }}, nil, true},
  164. }
  165. for _, tt := range tests {
  166. t.Run(tt.name, func(t *testing.T) {
  167. got, err := k.GetPublicKey(tt.args.req)
  168. if (err != nil) != tt.wantErr {
  169. t.Errorf("PKCS11.GetPublicKey() error = %v, wantErr %v", err, tt.wantErr)
  170. return
  171. }
  172. if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
  173. t.Errorf("PKCS11.GetPublicKey() = %T, want %T", got, tt.want)
  174. }
  175. })
  176. }
  177. }
  178. func TestPKCS11_CreateKey(t *testing.T) {
  179. k := setupPKCS11(t)
  180. // Make sure to delete the created key
  181. k.DeleteKey(testObject)
  182. type args struct {
  183. req *apiv1.CreateKeyRequest
  184. }
  185. tests := []struct {
  186. name string
  187. args args
  188. want *apiv1.CreateKeyResponse
  189. wantErr bool
  190. }{
  191. {"default", args{&apiv1.CreateKeyRequest{
  192. Name: testObject,
  193. }}, &apiv1.CreateKeyResponse{
  194. Name: testObject,
  195. PublicKey: &ecdsa.PublicKey{},
  196. CreateSignerRequest: apiv1.CreateSignerRequest{
  197. SigningKey: testObject,
  198. },
  199. }, false},
  200. {"RSA SHA256WithRSA", args{&apiv1.CreateKeyRequest{
  201. Name: testObject,
  202. SignatureAlgorithm: apiv1.SHA256WithRSA,
  203. }}, &apiv1.CreateKeyResponse{
  204. Name: testObject,
  205. PublicKey: &rsa.PublicKey{},
  206. CreateSignerRequest: apiv1.CreateSignerRequest{
  207. SigningKey: testObject,
  208. },
  209. }, false},
  210. {"RSA SHA384WithRSA", args{&apiv1.CreateKeyRequest{
  211. Name: testObject,
  212. SignatureAlgorithm: apiv1.SHA384WithRSA,
  213. }}, &apiv1.CreateKeyResponse{
  214. Name: testObject,
  215. PublicKey: &rsa.PublicKey{},
  216. CreateSignerRequest: apiv1.CreateSignerRequest{
  217. SigningKey: testObject,
  218. },
  219. }, false},
  220. {"RSA SHA512WithRSA", args{&apiv1.CreateKeyRequest{
  221. Name: testObject,
  222. SignatureAlgorithm: apiv1.SHA512WithRSA,
  223. }}, &apiv1.CreateKeyResponse{
  224. Name: testObject,
  225. PublicKey: &rsa.PublicKey{},
  226. CreateSignerRequest: apiv1.CreateSignerRequest{
  227. SigningKey: testObject,
  228. },
  229. }, false},
  230. {"RSA SHA256WithRSAPSS", args{&apiv1.CreateKeyRequest{
  231. Name: testObject,
  232. SignatureAlgorithm: apiv1.SHA256WithRSAPSS,
  233. }}, &apiv1.CreateKeyResponse{
  234. Name: testObject,
  235. PublicKey: &rsa.PublicKey{},
  236. CreateSignerRequest: apiv1.CreateSignerRequest{
  237. SigningKey: testObject,
  238. },
  239. }, false},
  240. {"RSA SHA384WithRSAPSS", args{&apiv1.CreateKeyRequest{
  241. Name: testObject,
  242. SignatureAlgorithm: apiv1.SHA384WithRSAPSS,
  243. }}, &apiv1.CreateKeyResponse{
  244. Name: testObject,
  245. PublicKey: &rsa.PublicKey{},
  246. CreateSignerRequest: apiv1.CreateSignerRequest{
  247. SigningKey: testObject,
  248. },
  249. }, false},
  250. {"RSA SHA512WithRSAPSS", args{&apiv1.CreateKeyRequest{
  251. Name: testObject,
  252. SignatureAlgorithm: apiv1.SHA512WithRSAPSS,
  253. }}, &apiv1.CreateKeyResponse{
  254. Name: testObject,
  255. PublicKey: &rsa.PublicKey{},
  256. CreateSignerRequest: apiv1.CreateSignerRequest{
  257. SigningKey: testObject,
  258. },
  259. }, false},
  260. {"RSA 2048", args{&apiv1.CreateKeyRequest{
  261. Name: testObject,
  262. SignatureAlgorithm: apiv1.SHA256WithRSA,
  263. Bits: 2048,
  264. }}, &apiv1.CreateKeyResponse{
  265. Name: testObject,
  266. PublicKey: &rsa.PublicKey{},
  267. CreateSignerRequest: apiv1.CreateSignerRequest{
  268. SigningKey: testObject,
  269. },
  270. }, false},
  271. {"RSA 4096", args{&apiv1.CreateKeyRequest{
  272. Name: testObject,
  273. SignatureAlgorithm: apiv1.SHA256WithRSA,
  274. Bits: 4096,
  275. }}, &apiv1.CreateKeyResponse{
  276. Name: testObject,
  277. PublicKey: &rsa.PublicKey{},
  278. CreateSignerRequest: apiv1.CreateSignerRequest{
  279. SigningKey: testObject,
  280. },
  281. }, false},
  282. {"ECDSA P256", args{&apiv1.CreateKeyRequest{
  283. Name: testObject,
  284. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  285. }}, &apiv1.CreateKeyResponse{
  286. Name: testObject,
  287. PublicKey: &ecdsa.PublicKey{},
  288. CreateSignerRequest: apiv1.CreateSignerRequest{
  289. SigningKey: testObject,
  290. },
  291. }, false},
  292. {"ECDSA P384", args{&apiv1.CreateKeyRequest{
  293. Name: testObject,
  294. SignatureAlgorithm: apiv1.ECDSAWithSHA384,
  295. }}, &apiv1.CreateKeyResponse{
  296. Name: testObject,
  297. PublicKey: &ecdsa.PublicKey{},
  298. CreateSignerRequest: apiv1.CreateSignerRequest{
  299. SigningKey: testObject,
  300. },
  301. }, false},
  302. {"ECDSA P521", args{&apiv1.CreateKeyRequest{
  303. Name: testObject,
  304. SignatureAlgorithm: apiv1.ECDSAWithSHA512,
  305. }}, &apiv1.CreateKeyResponse{
  306. Name: testObject,
  307. PublicKey: &ecdsa.PublicKey{},
  308. CreateSignerRequest: apiv1.CreateSignerRequest{
  309. SigningKey: testObject,
  310. },
  311. }, false},
  312. {"fail name", args{&apiv1.CreateKeyRequest{
  313. Name: "",
  314. }}, nil, true},
  315. {"fail no id", args{&apiv1.CreateKeyRequest{
  316. Name: "pkcs11:object=create-key",
  317. }}, nil, true},
  318. {"fail no object", args{&apiv1.CreateKeyRequest{
  319. Name: "pkcs11:id=9999",
  320. }}, nil, true},
  321. {"fail schema", args{&apiv1.CreateKeyRequest{
  322. Name: "pkcs12:id=9999;object=create-key",
  323. }}, nil, true},
  324. {"fail bits", args{&apiv1.CreateKeyRequest{
  325. Name: "pkcs11:id=9999;object=create-key",
  326. Bits: -1,
  327. SignatureAlgorithm: apiv1.SHA256WithRSAPSS,
  328. }}, nil, true},
  329. {"fail ed25519", args{&apiv1.CreateKeyRequest{
  330. Name: "pkcs11:id=9999;object=create-key",
  331. SignatureAlgorithm: apiv1.PureEd25519,
  332. }}, nil, true},
  333. {"fail unknown", args{&apiv1.CreateKeyRequest{
  334. Name: "pkcs11:id=9999;object=create-key",
  335. SignatureAlgorithm: apiv1.SignatureAlgorithm(100),
  336. }}, nil, true},
  337. {"fail FindKeyPair", args{&apiv1.CreateKeyRequest{
  338. Name: "pkcs11:foo=bar",
  339. SignatureAlgorithm: apiv1.SHA256WithRSAPSS,
  340. }}, nil, true},
  341. {"fail already exists", args{&apiv1.CreateKeyRequest{
  342. Name: "pkcs11:id=7373;object=ecdsa-p256-key",
  343. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  344. }}, nil, true},
  345. }
  346. for _, tt := range tests {
  347. t.Run(tt.name, func(t *testing.T) {
  348. got, err := k.CreateKey(tt.args.req)
  349. if (err != nil) != tt.wantErr {
  350. t.Errorf("PKCS11.CreateKey() error = %v, wantErr %v", err, tt.wantErr)
  351. return
  352. }
  353. if got != nil {
  354. got.PublicKey = tt.want.PublicKey
  355. }
  356. if !reflect.DeepEqual(got, tt.want) {
  357. t.Errorf("PKCS11.CreateKey() = %v, want %v", got, tt.want)
  358. }
  359. if got != nil {
  360. if err := k.DeleteKey(got.Name); err != nil {
  361. t.Errorf("PKCS11.DeleteKey() error = %v", err)
  362. }
  363. }
  364. })
  365. }
  366. }
  367. func TestPKCS11_CreateSigner(t *testing.T) {
  368. k := setupPKCS11(t)
  369. data := []byte("buggy-coheir-RUBRIC-rabbet-liberal-eaglet-khartoum-stagger")
  370. // VerifyASN1 verifies the ASN.1 encoded signature, sig, of hash using the
  371. // public key, pub. Its return value records whether the signature is valid.
  372. verifyASN1 := func(pub *ecdsa.PublicKey, hash, sig []byte) bool {
  373. var (
  374. r, s = &big.Int{}, &big.Int{}
  375. inner cryptobyte.String
  376. )
  377. input := cryptobyte.String(sig)
  378. if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
  379. !input.Empty() ||
  380. !inner.ReadASN1Integer(r) ||
  381. !inner.ReadASN1Integer(s) ||
  382. !inner.Empty() {
  383. return false
  384. }
  385. return ecdsa.Verify(pub, hash, r, s)
  386. }
  387. type args struct {
  388. req *apiv1.CreateSignerRequest
  389. }
  390. tests := []struct {
  391. name string
  392. args args
  393. algorithm apiv1.SignatureAlgorithm
  394. signerOpts crypto.SignerOpts
  395. wantErr bool
  396. }{
  397. // SoftHSM2
  398. {"RSA", args{&apiv1.CreateSignerRequest{
  399. SigningKey: "pkcs11:id=7371;object=rsa-key",
  400. }}, apiv1.SHA256WithRSA, crypto.SHA256, false},
  401. {"RSA PSS", args{&apiv1.CreateSignerRequest{
  402. SigningKey: "pkcs11:id=7372;object=rsa-pss-key",
  403. }}, apiv1.SHA256WithRSAPSS, &rsa.PSSOptions{
  404. SaltLength: rsa.PSSSaltLengthEqualsHash,
  405. Hash: crypto.SHA256,
  406. }, false},
  407. {"ECDSA P256", args{&apiv1.CreateSignerRequest{
  408. SigningKey: "pkcs11:id=7373;object=ecdsa-p256-key",
  409. }}, apiv1.ECDSAWithSHA256, crypto.SHA256, false},
  410. {"ECDSA P384", args{&apiv1.CreateSignerRequest{
  411. SigningKey: "pkcs11:id=7374;object=ecdsa-p384-key",
  412. }}, apiv1.ECDSAWithSHA384, crypto.SHA384, false},
  413. {"ECDSA P521", args{&apiv1.CreateSignerRequest{
  414. SigningKey: "pkcs11:id=7375;object=ecdsa-p521-key",
  415. }}, apiv1.ECDSAWithSHA512, crypto.SHA512, false},
  416. {"fail SigningKey", args{&apiv1.CreateSignerRequest{
  417. SigningKey: "",
  418. }}, 0, nil, true},
  419. {"fail uri", args{&apiv1.CreateSignerRequest{
  420. SigningKey: "https:id=7375;object=ecdsa-p521-key",
  421. }}, 0, nil, true},
  422. {"fail FindKeyPair", args{&apiv1.CreateSignerRequest{
  423. SigningKey: "pkcs11:foo=bar",
  424. }}, 0, nil, true},
  425. }
  426. for _, tt := range tests {
  427. t.Run(tt.name, func(t *testing.T) {
  428. got, err := k.CreateSigner(tt.args.req)
  429. if (err != nil) != tt.wantErr {
  430. t.Errorf("PKCS11.CreateSigner() error = %v, wantErr %v", err, tt.wantErr)
  431. return
  432. }
  433. if got != nil {
  434. hash := tt.signerOpts.HashFunc()
  435. h := hash.New()
  436. h.Write(data)
  437. digest := h.Sum(nil)
  438. sig, err := got.Sign(rand.Reader, digest, tt.signerOpts)
  439. if err != nil {
  440. t.Errorf("cyrpto.Signer.Sign() error = %v", err)
  441. }
  442. switch tt.algorithm {
  443. case apiv1.SHA256WithRSA, apiv1.SHA384WithRSA, apiv1.SHA512WithRSA:
  444. pub := got.Public().(*rsa.PublicKey)
  445. if err := rsa.VerifyPKCS1v15(pub, hash, digest, sig); err != nil {
  446. t.Errorf("rsa.VerifyPKCS1v15() error = %v", err)
  447. }
  448. case apiv1.UnspecifiedSignAlgorithm, apiv1.SHA256WithRSAPSS, apiv1.SHA384WithRSAPSS, apiv1.SHA512WithRSAPSS:
  449. pub := got.Public().(*rsa.PublicKey)
  450. if err := rsa.VerifyPSS(pub, hash, digest, sig, tt.signerOpts.(*rsa.PSSOptions)); err != nil {
  451. t.Errorf("rsa.VerifyPSS() error = %v", err)
  452. }
  453. case apiv1.ECDSAWithSHA256, apiv1.ECDSAWithSHA384, apiv1.ECDSAWithSHA512:
  454. pub := got.Public().(*ecdsa.PublicKey)
  455. if !verifyASN1(pub, digest, sig) {
  456. t.Error("ecdsa.VerifyASN1() failed")
  457. }
  458. default:
  459. t.Errorf("signature algorithm %s is not supported", tt.algorithm)
  460. }
  461. }
  462. })
  463. }
  464. }
  465. func TestPKCS11_LoadCertificate(t *testing.T) {
  466. k := setupPKCS11(t)
  467. getCertFn := func(i, j int) func() *x509.Certificate {
  468. return func() *x509.Certificate {
  469. return testCerts[i].Certificates[j]
  470. }
  471. }
  472. type args struct {
  473. req *apiv1.LoadCertificateRequest
  474. }
  475. tests := []struct {
  476. name string
  477. args args
  478. wantFn func() *x509.Certificate
  479. wantErr bool
  480. }{
  481. {"load", args{&apiv1.LoadCertificateRequest{
  482. Name: "pkcs11:id=7376;object=test-root",
  483. }}, getCertFn(0, 0), false},
  484. {"load by id", args{&apiv1.LoadCertificateRequest{
  485. Name: "pkcs11:id=7376",
  486. }}, getCertFn(0, 0), false},
  487. {"load by label", args{&apiv1.LoadCertificateRequest{
  488. Name: "pkcs11:object=test-root",
  489. }}, getCertFn(0, 0), false},
  490. {"load by serial", args{&apiv1.LoadCertificateRequest{
  491. Name: "pkcs11:serial=64",
  492. }}, getCertFn(0, 0), false},
  493. {"fail missing", args{&apiv1.LoadCertificateRequest{
  494. Name: "pkcs11:id=9999;object=test-root",
  495. }}, nil, true},
  496. {"fail name", args{&apiv1.LoadCertificateRequest{
  497. Name: "",
  498. }}, nil, true},
  499. {"fail scheme", args{&apiv1.LoadCertificateRequest{
  500. Name: "foo:id=7376;object=test-root",
  501. }}, nil, true},
  502. {"fail serial", args{&apiv1.LoadCertificateRequest{
  503. Name: "pkcs11:serial=foo",
  504. }}, nil, true},
  505. {"fail FindCertificate", args{&apiv1.LoadCertificateRequest{
  506. Name: "pkcs11:foo=bar",
  507. }}, nil, true},
  508. }
  509. for _, tt := range tests {
  510. t.Run(tt.name, func(t *testing.T) {
  511. got, err := k.LoadCertificate(tt.args.req)
  512. if (err != nil) != tt.wantErr {
  513. t.Errorf("PKCS11.LoadCertificate() error = %v, wantErr %v", err, tt.wantErr)
  514. return
  515. }
  516. var want *x509.Certificate
  517. if tt.wantFn != nil {
  518. want = tt.wantFn()
  519. got.Raw, got.RawIssuer, got.RawSubject, got.RawTBSCertificate, got.RawSubjectPublicKeyInfo = nil, nil, nil, nil, nil
  520. want.Raw, want.RawIssuer, want.RawSubject, want.RawTBSCertificate, want.RawSubjectPublicKeyInfo = nil, nil, nil, nil, nil
  521. }
  522. if !reflect.DeepEqual(got, want) {
  523. t.Errorf("PKCS11.LoadCertificate() = %v, want %v", got, want)
  524. }
  525. })
  526. }
  527. }
  528. func TestPKCS11_StoreCertificate(t *testing.T) {
  529. k := setupPKCS11(t)
  530. pub, priv, err := ed25519.GenerateKey(rand.Reader)
  531. if err != nil {
  532. t.Fatalf("ed25519.GenerateKey() error = %v", err)
  533. }
  534. cert, err := generateCertificate(pub, priv)
  535. if err != nil {
  536. t.Fatalf("x509.CreateCertificate() error = %v", err)
  537. }
  538. // Make sure to delete the created certificate
  539. t.Cleanup(func() {
  540. k.DeleteCertificate(testObject)
  541. })
  542. type args struct {
  543. req *apiv1.StoreCertificateRequest
  544. }
  545. tests := []struct {
  546. name string
  547. args args
  548. wantErr bool
  549. }{
  550. {"ok", args{&apiv1.StoreCertificateRequest{
  551. Name: testObject,
  552. Certificate: cert,
  553. }}, false},
  554. {"fail already exists", args{&apiv1.StoreCertificateRequest{
  555. Name: testObject,
  556. Certificate: cert,
  557. }}, true},
  558. {"fail name", args{&apiv1.StoreCertificateRequest{
  559. Name: "",
  560. Certificate: cert,
  561. }}, true},
  562. {"fail certificate", args{&apiv1.StoreCertificateRequest{
  563. Name: testObject,
  564. Certificate: nil,
  565. }}, true},
  566. {"fail uri", args{&apiv1.StoreCertificateRequest{
  567. Name: "http:id=7770;object=create-cert",
  568. Certificate: cert,
  569. }}, true},
  570. {"fail ImportCertificateWithLabel", args{&apiv1.StoreCertificateRequest{
  571. Name: "pkcs11:foo=bar",
  572. Certificate: cert,
  573. }}, true},
  574. }
  575. for _, tt := range tests {
  576. t.Run(tt.name, func(t *testing.T) {
  577. if err := k.StoreCertificate(tt.args.req); (err != nil) != tt.wantErr {
  578. t.Errorf("PKCS11.StoreCertificate() error = %v, wantErr %v", err, tt.wantErr)
  579. }
  580. if !tt.wantErr {
  581. got, err := k.LoadCertificate(&apiv1.LoadCertificateRequest{
  582. Name: tt.args.req.Name,
  583. })
  584. if err != nil {
  585. t.Errorf("PKCS11.LoadCertificate() error = %v", err)
  586. }
  587. if !reflect.DeepEqual(got, cert) {
  588. t.Errorf("PKCS11.LoadCertificate() = %v, want %v", got, cert)
  589. }
  590. }
  591. })
  592. }
  593. }
  594. func TestPKCS11_DeleteKey(t *testing.T) {
  595. k := setupPKCS11(t)
  596. type args struct {
  597. uri string
  598. }
  599. tests := []struct {
  600. name string
  601. args args
  602. wantErr bool
  603. }{
  604. {"delete", args{testObject}, false},
  605. {"delete by id", args{testObjectByID}, false},
  606. {"delete by label", args{testObjectByLabel}, false},
  607. {"delete missing", args{"pkcs11:id=9999;object=missing-key"}, false},
  608. {"fail name", args{""}, true},
  609. {"fail FindKeyPair", args{"pkcs11:foo=bar"}, true},
  610. }
  611. for _, tt := range tests {
  612. t.Run(tt.name, func(t *testing.T) {
  613. if _, err := k.CreateKey(&apiv1.CreateKeyRequest{
  614. Name: testObject,
  615. }); err != nil {
  616. t.Fatalf("PKCS1.CreateKey() error = %v", err)
  617. }
  618. if err := k.DeleteKey(tt.args.uri); (err != nil) != tt.wantErr {
  619. t.Errorf("PKCS11.DeleteKey() error = %v, wantErr %v", err, tt.wantErr)
  620. }
  621. if _, err := k.GetPublicKey(&apiv1.GetPublicKeyRequest{
  622. Name: tt.args.uri,
  623. }); err == nil {
  624. t.Error("PKCS11.GetPublicKey() public key found and not expected")
  625. }
  626. // Make sure to delete the created one.
  627. if err := k.DeleteKey(testObject); err != nil {
  628. t.Errorf("PKCS11.DeleteKey() error = %v", err)
  629. }
  630. })
  631. }
  632. }
  633. func TestPKCS11_DeleteCertificate(t *testing.T) {
  634. k := setupPKCS11(t)
  635. pub, priv, err := ed25519.GenerateKey(rand.Reader)
  636. if err != nil {
  637. t.Fatalf("ed25519.GenerateKey() error = %v", err)
  638. }
  639. cert, err := generateCertificate(pub, priv)
  640. if err != nil {
  641. t.Fatalf("x509.CreateCertificate() error = %v", err)
  642. }
  643. type args struct {
  644. uri string
  645. }
  646. tests := []struct {
  647. name string
  648. args args
  649. wantErr bool
  650. }{
  651. {"delete", args{testObject}, false},
  652. {"delete by id", args{testObjectByID}, false},
  653. {"delete by label", args{testObjectByLabel}, false},
  654. {"delete missing", args{"pkcs11:id=9999;object=missing-key"}, false},
  655. {"fail name", args{""}, true},
  656. {"fail DeleteCertificate", args{"pkcs11:foo=bar"}, true},
  657. }
  658. for _, tt := range tests {
  659. t.Run(tt.name, func(t *testing.T) {
  660. if err := k.StoreCertificate(&apiv1.StoreCertificateRequest{
  661. Name: testObject,
  662. Certificate: cert,
  663. }); err != nil {
  664. t.Fatalf("PKCS11.StoreCertificate() error = %v", err)
  665. }
  666. if err := k.DeleteCertificate(tt.args.uri); (err != nil) != tt.wantErr {
  667. t.Errorf("PKCS11.DeleteCertificate() error = %v, wantErr %v", err, tt.wantErr)
  668. }
  669. if _, err := k.LoadCertificate(&apiv1.LoadCertificateRequest{
  670. Name: tt.args.uri,
  671. }); err == nil {
  672. t.Error("PKCS11.LoadCertificate() certificate found and not expected")
  673. }
  674. // Make sure to delete the created one.
  675. if err := k.DeleteCertificate(testObject); err != nil {
  676. t.Errorf("PKCS11.DeleteCertificate() error = %v", err)
  677. }
  678. })
  679. }
  680. }
  681. func TestPKCS11_Close(t *testing.T) {
  682. k := mustPKCS11(t)
  683. tests := []struct {
  684. name string
  685. wantErr bool
  686. }{
  687. {"ok", false},
  688. {"second", false},
  689. }
  690. for _, tt := range tests {
  691. t.Run(tt.name, func(t *testing.T) {
  692. if err := k.Close(); (err != nil) != tt.wantErr {
  693. t.Errorf("PKCS11.Close() error = %v, wantErr %v", err, tt.wantErr)
  694. }
  695. })
  696. }
  697. }