Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7a8158c

Browse files
committedSep 30, 2021
Add ability to specify initialize flags for pkcs11 provider
New pkcs11-helper interface allows to setup pkcs11 provider via properties: alonbl/pkcs11-helper@b78d21c Also pkcs11-helper added ability to setup init args for pkcs11 provider: alonbl/pkcs11-helper@133f893
1 parent 7205cdd commit 7a8158c

File tree

5 files changed

+90
-22
lines changed

5 files changed

+90
-22
lines changed
 

‎src/openvpn/init.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,8 @@ context_init_1(struct context *c)
682682
for (i = 0; i<MAX_PARMS && c->options.pkcs11_providers[i] != NULL; i++)
683683
{
684684
pkcs11_addProvider(c->options.pkcs11_providers[i], c->options.pkcs11_protected_authentication[i],
685-
c->options.pkcs11_private_mode[i], c->options.pkcs11_cert_private[i]);
685+
c->options.pkcs11_private_mode[i], c->options.pkcs11_cert_private[i],
686+
c->options.pkcs11_init_flags[i]);
686687
}
687688
}
688689
#endif

‎src/openvpn/options.c

+23
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,11 @@ static const char usage_message[] =
664664
" 8 : Use Unwrap.\n"
665665
"--pkcs11-cert-private [0|1] ... : Set if login should be performed before\n"
666666
" certificate can be accessed. Set for each provider.\n"
667+
"--pkcs11-init-flags hex ... : PKCS#11 init flags.\n"
668+
" It's bitwise OR of some PKCS#11 initialize flags.\n"
669+
" Most popular of them is:\n"
670+
" 1 : CKF_LIBRARY_CANT_CREATE_OS_THREADS\n"
671+
" 2 : CKF_OS_LOCKING_OK\n"
667672
"--pkcs11-pin-cache seconds : Number of seconds to cache PIN. The default is -1\n"
668673
" cache until token is removed.\n"
669674
"--pkcs11-id-management : Acquire identity from management interface.\n"
@@ -1838,6 +1843,13 @@ show_settings(const struct options *o)
18381843
SHOW_PARM(pkcs11_cert_private, o->pkcs11_cert_private[i] ? "ENABLED" : "DISABLED", "%s");
18391844
}
18401845
}
1846+
{
1847+
int i;
1848+
for (i = 0; i<MAX_PARMS; i++)
1849+
{
1850+
SHOW_PARM(pkcs11_init_flags, o->pkcs11_init_flags[i], "%08x");
1851+
}
1852+
}
18411853
SHOW_INT(pkcs11_pin_cache_period);
18421854
SHOW_STR(pkcs11_id);
18431855
SHOW_BOOL(pkcs11_id_management);
@@ -8778,6 +8790,17 @@ add_option(struct options *options,
87788790
options->pkcs11_cert_private[j-1] = atoi(p[j]) != 0 ? 1 : 0;
87798791
}
87808792
}
8793+
else if (streq(p[0], "pkcs11-init-flags"))
8794+
{
8795+
int j;
8796+
8797+
VERIFY_PERMISSION(OPT_P_GENERAL);
8798+
8799+
for (j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
8800+
{
8801+
sscanf(p[j], "%x", &(options->pkcs11_init_flags[j-1]));
8802+
}
8803+
}
87818804
else if (streq(p[0], "pkcs11-pin-cache") && p[1] && !p[2])
87828805
{
87838806
VERIFY_PERMISSION(OPT_P_GENERAL);

‎src/openvpn/options.h

+1
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ struct options
573573
unsigned pkcs11_private_mode[MAX_PARMS];
574574
bool pkcs11_protected_authentication[MAX_PARMS];
575575
bool pkcs11_cert_private[MAX_PARMS];
576+
unsigned pkcs11_init_flags[MAX_PARMS];
576577
int pkcs11_pin_cache_period;
577578
const char *pkcs11_id;
578579
bool pkcs11_id_management;

‎src/openvpn/pkcs11.c

+62-20
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,17 @@ pkcs11_terminate(void)
374374
bool
375375
pkcs11_addProvider(
376376
const char *const provider,
377-
const bool protected_auth,
377+
const bool _protected_auth,
378378
const unsigned private_mode,
379-
const bool cert_private
379+
const bool _cert_private,
380+
const unsigned init_flags
380381
)
381382
{
382383
CK_RV rv = CKR_OK;
384+
int success = true;
385+
PKCS11H_BOOL protected_auth = _protected_auth;
386+
PKCS11H_BOOL cert_private = _cert_private;
387+
CK_C_INITIALIZE_ARGS_PTR p_init_args;
383388

384389
ASSERT(provider!=NULL);
385390

@@ -396,29 +401,66 @@ pkcs11_addProvider(
396401
provider
397402
);
398403

399-
if (
400-
(rv = pkcs11h_addProvider(
401-
provider,
402-
provider,
403-
protected_auth,
404-
private_mode,
405-
PKCS11H_SLOTEVENT_METHOD_AUTO,
406-
0,
407-
cert_private
408-
)) != CKR_OK
409-
)
410-
{
411-
msg(M_WARN, "PKCS#11: Cannot initialize provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
404+
if ((rv = pkcs11h_registerProvider(provider)) != CKR_OK) {
405+
msg(M_WARN, "PKCS#11: Cannot register provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
406+
success = false;
407+
goto exit;
408+
}
409+
if ((rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOCATION, provider, strlen(provider) + 1)) != CKR_OK) {
410+
msg(M_WARN, "PKCS#11: Cannot setup provider '%s' location '%s' %ld-'%s'", provider, provider, rv, pkcs11h_getMessage(rv));
411+
success = false;
412+
goto cleanup;
413+
}
414+
if ((rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_ALLOW_PROTECTED_AUTH, &protected_auth, sizeof(protected_auth))) != CKR_OK) {
415+
msg(M_WARN, "PKCS#11: Cannot setup provider '%s' ptorected auth mode '%s' %ld-'%s'", provider, protected_auth ? "true" : "false", rv, pkcs11h_getMessage(rv));
416+
success = false;
417+
goto cleanup;
418+
}
419+
if ((rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE, &private_mode, sizeof(private_mode))) != CKR_OK) {
420+
msg(M_WARN, "PKCS#11: Cannot setup provider '%s' private mask mode '%08x' %ld-'%s'", provider, private_mode, rv, pkcs11h_getMessage(rv));
421+
success = false;
422+
goto cleanup;
423+
}
424+
if ((rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE, &cert_private, sizeof(cert_private))) != CKR_OK) {
425+
msg(M_WARN, "PKCS#11: Cannot setup provider '%s' private cert mode '%s' %ld-'%s'", provider, cert_private ? "true" : "false", rv, pkcs11h_getMessage(rv));
426+
success = false;
427+
goto cleanup;
412428
}
413429

430+
// pkcs11-helper take ownership over this pointer
431+
if ((p_init_args = malloc(sizeof(*p_init_args))) == NULL) {
432+
msg(M_FATAL, "PKCS#11: Cannot allocate memory");
433+
success = false;
434+
goto cleanup;
435+
}
436+
437+
memset(p_init_args, 0, sizeof(*p_init_args));
438+
p_init_args->flags = init_flags;
439+
440+
if ((rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_INIT_ARGS, &p_init_args, sizeof(p_init_args))) != CKR_OK) {
441+
msg(M_WARN, "PKCS#11: Cannot setup provider '%s' init flags '%08x' %ld-'%s'", provider, init_flags, rv, pkcs11h_getMessage(rv));
442+
free(p_init_args);
443+
success = false;
444+
goto cleanup;
445+
}
446+
if ((rv = pkcs11h_initializeProvider(provider)) != CKR_OK) {
447+
success = false;
448+
goto cleanup;
449+
}
450+
451+
cleanup:
452+
if (!success) {
453+
pkcs11h_removeProvider(provider);
454+
}
455+
456+
exit:
414457
dmsg(
415458
D_PKCS11_DEBUG,
416-
"PKCS#11: pkcs11_addProvider - return rv=%ld-'%s'",
417-
rv,
418-
pkcs11h_getMessage(rv)
419-
);
459+
"PKCS#11: pkcs11 registration is %s",
460+
success ? "success" : "failed"
461+
);
420462

421-
return rv == CKR_OK;
463+
return success;
422464
}
423465

424466
int

‎src/openvpn/pkcs11.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ pkcs11_addProvider(
4242
const char *const provider,
4343
const bool fProtectedAuthentication,
4444
const unsigned private_mode,
45-
const bool fCertIsPrivate
45+
const bool fCertIsPrivate,
46+
const unsigned init_flags
4647
);
4748

4849
int

0 commit comments

Comments
 (0)
Please sign in to comment.