4040#include "console.h"
4141#include "pkcs11_backend.h"
4242
43+
44+ struct pkcs11_context {
45+ int nPINCachePeriod ;
46+ struct user_pass token_pass ;
47+ const char * pin_file ;
48+ };
49+
50+ static struct pkcs11_context pkcs11_ctx ; /* GLOBAL */
51+
4352static
4453time_t
4554__mytime (void )
@@ -181,6 +190,43 @@ _pkcs11_openvpn_log(
181190 msg (_pkcs11_msg_pkcs112openvpn (flags ), "%s" , Buffer );
182191}
183192
193+ static
194+ PKCS11H_BOOL
195+ pkcs11_password_setup (
196+ const char * pkcs11_pin_file ,
197+ struct user_pass * token_pass
198+ )
199+ {
200+ if (!token_pass )
201+ {
202+ return false;
203+ }
204+ if (pkcs11_pin_file )
205+ {
206+ msg (M_INFO , "pkcs11_password_setup - pkcs11_pin_file='%s'" , pkcs11_pin_file );
207+ }
208+ else
209+ {
210+ /* If pin file is not provided, clear the token_pass and continue */
211+ CLEAR (token_pass );
212+ return true;
213+ }
214+ token_pass -> defined = false;
215+ token_pass -> nocache = true;
216+
217+ if (!strlen (token_pass -> password ))
218+ {
219+ get_user_pass (
220+ token_pass ,
221+ pkcs11_pin_file ,
222+ UP_TYPE_PRIVATE_KEY ,
223+ GET_USER_PASS_MANAGEMENT |GET_USER_PASS_PASSWORD_ONLY
224+ );
225+ }
226+
227+ return true;
228+ }
229+
184230static
185231PKCS11H_BOOL
186232_pkcs11_openvpn_token_prompt (
@@ -236,24 +282,29 @@ _pkcs11_openvpn_pin_prompt(
236282 const size_t pin_max
237283 )
238284{
239- struct user_pass token_pass ;
240285 char prompt [1024 ];
241- CLEAR (token_pass );
286+ struct pkcs11_context * ctx = NULL ;
287+
288+ if (!global_data )
289+ {
290+ return false;
291+ }
292+ ctx = (struct pkcs11_context * )global_data ;
242293
243- (void )global_data ;
244294 (void )user_data ;
245295 (void )retry ;
246296
247297 ASSERT (token != NULL );
248298
249299 snprintf (prompt , sizeof (prompt ), "%s token" , token -> label );
250300
251- token_pass .defined = false;
252- token_pass .nocache = true;
301+ ctx -> token_pass .defined = false;
302+ ctx -> token_pass .nocache = true;
253303
254304 if (
255- !get_user_pass (
256- & token_pass ,
305+ !strlen (ctx -> token_pass .password )
306+ && !get_user_pass (
307+ & ctx -> token_pass ,
257308 NULL ,
258309 prompt ,
259310 GET_USER_PASS_MANAGEMENT |GET_USER_PASS_PASSWORD_ONLY |GET_USER_PASS_NOFATAL
@@ -264,8 +315,8 @@ _pkcs11_openvpn_pin_prompt(
264315 }
265316 else
266317 {
267- strncpynt (pin , token_pass .password , pin_max );
268- purge_user_pass (& token_pass , true);
318+ strncpynt (pin , ctx -> token_pass .password , pin_max );
319+ purge_user_pass (& ctx -> token_pass , true);
269320
270321 if (strlen (pin ) == 0 )
271322 {
@@ -281,16 +332,24 @@ _pkcs11_openvpn_pin_prompt(
281332bool
282333pkcs11_initialize (
283334 const bool protected_auth ,
284- const int nPINCachePeriod
335+ const int nPINCachePeriod ,
336+ const char * pin_file
285337 )
286338{
287339 CK_RV rv = CKR_FUNCTION_FAILED ;
340+ pkcs11_ctx .nPINCachePeriod = nPINCachePeriod ;
288341
289342 dmsg (
290343 D_PKCS11_DEBUG ,
291344 "PKCS#11: pkcs11_initialize - entered"
292345 );
293346
347+ if (!pkcs11_password_setup (pin_file , & pkcs11_ctx .token_pass ))
348+ {
349+ msg (M_FATAL , "PKCS#11: Cannot initialize pkcs11 password" );
350+ return false;
351+ }
352+
294353 if ((rv = pkcs11h_engine_setSystem (& s_pkcs11h_sys_engine )) != CKR_OK )
295354 {
296355 msg (M_FATAL , "PKCS#11: Cannot initialize system engine %ld-'%s'" , rv , pkcs11h_getMessage (rv ));
@@ -317,13 +376,13 @@ pkcs11_initialize(
317376 goto cleanup ;
318377 }
319378
320- if ((rv = pkcs11h_setTokenPromptHook (_pkcs11_openvpn_token_prompt , NULL )) != CKR_OK )
379+ if ((rv = pkcs11h_setTokenPromptHook (_pkcs11_openvpn_token_prompt , & pkcs11_ctx )) != CKR_OK )
321380 {
322381 msg (M_FATAL , "PKCS#11: Cannot set hooks %ld-'%s'" , rv , pkcs11h_getMessage (rv ));
323382 goto cleanup ;
324383 }
325384
326- if ((rv = pkcs11h_setPINPromptHook (_pkcs11_openvpn_pin_prompt , NULL )) != CKR_OK )
385+ if ((rv = pkcs11h_setPINPromptHook (_pkcs11_openvpn_pin_prompt , & pkcs11_ctx )) != CKR_OK )
327386 {
328387 msg (M_FATAL , "PKCS#11: Cannot set hooks %ld-'%s'" , rv , pkcs11h_getMessage (rv ));
329388 goto cleanup ;
0 commit comments