@@ -13,12 +13,16 @@ import (
13
13
"crypto/rsa"
14
14
"crypto/x509"
15
15
"math/big"
16
+ "os"
17
+ "path/filepath"
16
18
"reflect"
17
19
"strings"
18
20
"testing"
19
21
20
22
"github.com/ThalesIgnite/crypto11"
21
23
"github.com/pkg/errors"
24
+ "github.com/stretchr/testify/assert"
25
+ "github.com/stretchr/testify/require"
22
26
"go.step.sm/crypto/kms/apiv1"
23
27
"golang.org/x/crypto/cryptobyte"
24
28
"golang.org/x/crypto/cryptobyte/asn1"
@@ -77,6 +81,10 @@ func TestNew(t *testing.T) {
77
81
Type : "pkcs11" ,
78
82
URI : "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test" ,
79
83
}}, k , false },
84
+ {"ok empty pin" , args {context .Background (), apiv1.Options {
85
+ Type : "pkcs11" ,
86
+ URI : "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test;pin-value=" ,
87
+ }}, k , false },
80
88
{"ok with missing module" , args {context .Background (), apiv1.Options {
81
89
Type : "pkcs11" ,
82
90
URI : "pkcs11:token=pkcs11-test" ,
@@ -141,6 +149,71 @@ func TestNew(t *testing.T) {
141
149
}
142
150
}
143
151
152
+ func TestNew_config (t * testing.T ) {
153
+ tmp0 := p11Configure
154
+ t .Cleanup (func () {
155
+ p11Configure = tmp0
156
+ })
157
+
158
+ k := mustPKCS11 (t )
159
+ t .Cleanup (func () {
160
+ k .Close ()
161
+ })
162
+
163
+ path := filepath .Join (t .TempDir (), "pin.txt" )
164
+ require .NoError (t , os .WriteFile (path , []byte ("123456\n " ), 0o0600 ))
165
+
166
+ var zero int
167
+
168
+ ctx := context .Background ()
169
+ type args struct {
170
+ ctx context.Context
171
+ opts apiv1.Options
172
+ }
173
+ tests := []struct {
174
+ name string
175
+ args args
176
+ wantConfig * crypto11.Config
177
+ }{
178
+ {"ok" , args {ctx , apiv1.Options {URI : "pkcs11:module-path=module.so;token=token?pin-value=password" }}, & crypto11.Config {
179
+ Path : "module.so" , TokenLabel : "token" , Pin : "password" ,
180
+ }},
181
+ {"ok default module" , args {ctx , apiv1.Options {URI : "pkcs11:token=token?pin-value=password" }}, & crypto11.Config {
182
+ Path : defaultModule , TokenLabel : "token" , Pin : "password" ,
183
+ }},
184
+ {"ok serial" , args {ctx , apiv1.Options {URI : "pkcs11:module-path=module.so;serial=1234567890?pin-value=password" }}, & crypto11.Config {
185
+ Path : "module.so" , TokenSerial : "1234567890" , Pin : "password" ,
186
+ }},
187
+ {"ok slot-id" , args {ctx , apiv1.Options {URI : "pkcs11:module-path=module.so;slot-id=0?pin-value=password" }}, & crypto11.Config {
188
+ Path : "module.so" , SlotNumber : & zero , Pin : "password" ,
189
+ }},
190
+ {"ok max-sessions" , args {ctx , apiv1.Options {URI : "pkcs11:module-path=module.so;slot-id=0;max-sessions=100?pin-value=password" }}, & crypto11.Config {
191
+ Path : "module.so" , SlotNumber : & zero , Pin : "password" , MaxSessions : 100 ,
192
+ }},
193
+ {"ok pin-source" , args {ctx , apiv1.Options {URI : "pkcs11:module-path=module.so;token=token?pin-source=" + path }}, & crypto11.Config {
194
+ Path : "module.so" , TokenLabel : "token" , Pin : "123456" ,
195
+ }},
196
+ {"ok login not supported" , args {ctx , apiv1.Options {URI : "pkcs11:module-path=module.so;token=token" }}, & crypto11.Config {
197
+ Path : "module.so" , TokenLabel : "token" , LoginNotSupported : true ,
198
+ }},
199
+ {"ok empty pin" , args {ctx , apiv1.Options {URI : "pkcs11:module-path=module.so;token=token?pin-value=" }}, & crypto11.Config {
200
+ Path : "module.so" , TokenLabel : "token" , Pin : "" ,
201
+ }},
202
+ {"ok pin option" , args {ctx , apiv1.Options {URI : "pkcs11:module-path=module.so;token=token?pin-value=" , Pin : "password" }}, & crypto11.Config {
203
+ Path : "module.so" , TokenLabel : "token" , Pin : "password" ,
204
+ }},
205
+ }
206
+ for _ , tt := range tests {
207
+ t .Run (tt .name , func (t * testing.T ) {
208
+ p11Configure = func (config * crypto11.Config ) (P11 , error ) {
209
+ assert .Equal (t , tt .wantConfig , config )
210
+ return k .p11 , nil
211
+ }
212
+ _ , err := New (tt .args .ctx , tt .args .opts )
213
+ assert .NoError (t , err )
214
+ })
215
+ }
216
+ }
144
217
func TestPKCS11_GetPublicKey (t * testing.T ) {
145
218
k := setupPKCS11 (t )
146
219
type args struct {
0 commit comments