pkcs11_test.go 22 KB

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