Skip to content

Commit a366ed2

Browse files
committed
added pkcs11-pin feature
1 parent 45a4f4e commit a366ed2

7 files changed

Lines changed: 109 additions & 15 deletions

File tree

doc/man-sections/pkcs11-options.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ PKCS#11 / SmartCard options
8383

8484
``--verb`` option can be used BEFORE this option to produce debugging
8585
information.
86+
87+
--pkcs11-pin pin
88+
Specify the file with the PIN for the PKCS#11 token.
89+
90+
Valid syntax:
91+
::
92+
93+
pkcs11-pin file.txt
94+
95+
If the PIN is specified from file, the file should contain only the PIN,
96+
without any additional characters.
97+
When ``--pkcs11-pin`` is not specified, the user will be prompted to enter the PIN.

src/openvpn/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ context_init_1(struct context *c)
760760
if (c->first_time)
761761
{
762762
int i;
763-
pkcs11_initialize(true, c->options.pkcs11_pin_cache_period);
763+
pkcs11_initialize(true, c->options.pkcs11_pin_cache_period, c->options.pkcs11_pin_file);
764764
for (i = 0; i<MAX_PARMS && c->options.pkcs11_providers[i] != NULL; i++)
765765
{
766766
pkcs11_addProvider(c->options.pkcs11_providers[i], c->options.pkcs11_protected_authentication[i],

src/openvpn/options.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ static const char usage_message[] =
693693
" cache until token is removed.\n"
694694
"--pkcs11-id-management : Acquire identity from management interface.\n"
695695
"--pkcs11-id serialized-id 'id' : Identity to use, get using standalone --show-pkcs11-ids\n"
696+
"--pkcs11-pin file : File containing the PIN for the PKCS#11 token.\n"
696697
#endif /* ENABLE_PKCS11 */
697698
"\n"
698699
"SSL Library information:\n"
@@ -4153,6 +4154,14 @@ options_postprocess_filechecks(struct options *options)
41534154
errs |= check_file_access_chroot(options->chroot_dir, CHKACC_FILE, options->tmp_dir,
41544155
R_OK|W_OK|X_OK, "Temporary directory (--tmp-dir)");
41554156

4157+
#ifdef ENABLE_PKCS11
4158+
if (options->pkcs11_pin_file)
4159+
{
4160+
errs |= check_file_access(CHKACC_FILE|CHKACC_ACPTSTDIN|CHKACC_PRIVATE,
4161+
options->pkcs11_pin_file, R_OK, "--pkcs11-pin");
4162+
}
4163+
#endif
4164+
41564165
if (errs)
41574166
{
41584167
msg(M_USAGE, "Please correct these errors.");
@@ -9444,6 +9453,18 @@ add_option(struct options *options,
94449453
VERIFY_PERMISSION(OPT_P_GENERAL);
94459454
options->pkcs11_id_management = true;
94469455
}
9456+
else if (streq(p[0], "pkcs11-pin") && p[1] && !p[2])
9457+
{
9458+
VERIFY_PERMISSION(OPT_P_GENERAL);
9459+
if (p[1])
9460+
{
9461+
options->pkcs11_pin_file = p[1];
9462+
}
9463+
else
9464+
{
9465+
options->pkcs11_pin_file = NULL;
9466+
}
9467+
}
94479468
#endif /* ifdef ENABLE_PKCS11 */
94489469
else if (streq(p[0], "rmtun") && !p[1])
94499470
{

src/openvpn/options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ struct options
633633
int pkcs11_pin_cache_period;
634634
const char *pkcs11_id;
635635
bool pkcs11_id_management;
636+
const char *pkcs11_pin_file;
636637
#endif
637638

638639
#ifdef ENABLE_CRYPTOAPI

src/openvpn/pkcs11.c

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
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+
4352
static
4453
time_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+
184230
static
185231
PKCS11H_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(
281332
bool
282333
pkcs11_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;

src/openvpn/pkcs11.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
bool
3232
pkcs11_initialize(
3333
const bool fProtectedAuthentication,
34-
const int nPINCachePeriod
34+
const int nPINCachePeriod,
35+
const char *pin_file
3536
);
3637

3738
void

tests/unit_tests/openvpn/test_pkcs11.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ setup_pkcs11(void **state)
321321
/* set default propq as we do in ssl_openssl.c */
322322
EVP_set_default_properties(tls_libctx, "?provider!=ovpn.xkey");
323323
#endif
324-
pkcs11_initialize(true, 0); /* protected auth enabled, pin-cache = 0 */
324+
pkcs11_initialize(true, 0, NULL); /* protected auth enabled, pin-cache = 0, pin_file = NULL */
325325
pkcs11_addProvider(SOFTHSM2_MODULE_PATH, false, 0, false);
326326
return 0;
327327
}

0 commit comments

Comments
 (0)