OpenVPN
pkcs11.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#if defined(ENABLE_PKCS11)
30
31#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
32#include "basic.h"
33#include "error.h"
34#include "manage.h"
35#include "base64.h"
36#include "pkcs11.h"
37#include "misc.h"
38#include "otime.h"
39#include "console.h"
40#include "pkcs11_backend.h"
41
42static time_t
43__mytime(void)
44{
46 return now;
47}
48
49#if !defined(_WIN32)
50static int
51__mygettimeofday(struct timeval *tv)
52{
53 return gettimeofday(tv, NULL);
54}
55#endif
56
57#if defined(__GNUC__) || defined(__clang__)
58#pragma GCC diagnostic push
59#pragma GCC diagnostic ignored "-Wsign-conversion"
60#endif
61
62static void
63__mysleep(unsigned long usec)
64{
65#if defined(_WIN32)
66 Sleep(usec / 1000);
67#else
68 if (usec > UINT_MAX)
69 {
70 usec = UINT_MAX;
71 }
72 usleep((useconds_t)usec);
73#endif
74}
75
76
77static pkcs11h_engine_system_t s_pkcs11h_sys_engine = { malloc, free, __mytime, __mysleep,
78#if defined(_WIN32)
79 NULL
80#else
81 __mygettimeofday
82#endif
83};
84
85static msglvl_t
86_pkcs11_msg_pkcs112openvpn(const unsigned flags)
87{
88#ifdef ENABLE_PKCS11_FORCE_DEBUG
89 return M_INFO;
90#else
91 msglvl_t openvpn_flags;
92
93 switch (flags)
94 {
95 case PKCS11H_LOG_DEBUG2:
96 openvpn_flags = D_PKCS11_DEBUG;
97 break;
98
99 case PKCS11H_LOG_DEBUG1:
100 openvpn_flags = D_SHOW_PKCS11;
101 break;
102
103 case PKCS11H_LOG_INFO:
104 openvpn_flags = M_INFO;
105 break;
106
107 case PKCS11H_LOG_WARN:
108 openvpn_flags = M_WARN;
109 break;
110
111 case PKCS11H_LOG_ERROR:
112 openvpn_flags = M_FATAL;
113 break;
114
115 default:
116 openvpn_flags = M_FATAL;
117 break;
118 }
119
120 return openvpn_flags;
121#endif
122}
123
124static unsigned
125_pkcs11_msg_openvpn2pkcs11(const msglvl_t flags)
126{
127#ifdef ENABLE_PKCS11_FORCE_DEBUG
128 return PKCS11H_LOG_DEBUG2;
129#else
130 unsigned pkcs11_flags;
131
132 if ((flags & D_PKCS11_DEBUG) != 0)
133 {
134 pkcs11_flags = PKCS11H_LOG_DEBUG2;
135 }
136 else if ((flags & D_SHOW_PKCS11) != 0)
137 {
138 pkcs11_flags = PKCS11H_LOG_DEBUG1;
139 }
140 else if ((flags & M_INFO) != 0)
141 {
142 pkcs11_flags = PKCS11H_LOG_INFO;
143 }
144 else if ((flags & M_WARN) != 0)
145 {
146 pkcs11_flags = PKCS11H_LOG_WARN;
147 }
148 else if ((flags & M_FATAL) != 0)
149 {
150 pkcs11_flags = PKCS11H_LOG_ERROR;
151 }
152 else
153 {
154 pkcs11_flags = PKCS11H_LOG_ERROR;
155 }
156
157 return pkcs11_flags;
158#endif
159}
160
161static void
162_pkcs11_openvpn_log(void *const global_data, unsigned flags, const char *const szFormat,
163 va_list args)
164{
165 char Buffer[10 * 1024];
166
167 (void)global_data;
168
169 vsnprintf(Buffer, sizeof(Buffer), szFormat, args);
170 Buffer[sizeof(Buffer) - 1] = 0;
171
172 msg(_pkcs11_msg_pkcs112openvpn(flags), "%s", Buffer);
173}
174
175static PKCS11H_BOOL
176_pkcs11_openvpn_token_prompt(void *const global_data, void *const user_data,
177 const pkcs11h_token_id_t token, const unsigned retry)
178{
179 struct user_pass token_resp;
180
181 (void)global_data;
182 (void)user_data;
183 (void)retry;
184
185 ASSERT(token != NULL);
186
187 CLEAR(token_resp);
188 token_resp.defined = false;
189 token_resp.nocache = true;
190 snprintf(token_resp.username, sizeof(token_resp.username), "Please insert %s token",
191 token->label);
192
193 if (!get_user_pass(&token_resp, NULL, "token-insertion-request",
195 {
196 return false;
197 }
198 else
199 {
200 return strcmp(token_resp.password, "ok") == 0;
201 }
202}
203
204static PKCS11H_BOOL
205_pkcs11_openvpn_pin_prompt(void *const global_data, void *const user_data,
206 const pkcs11h_token_id_t token, const unsigned retry, char *const pin,
207 const size_t pin_max)
208{
209 struct user_pass token_pass;
210 char prompt[1024];
211 CLEAR(token_pass);
212
213 (void)global_data;
214 (void)user_data;
215 (void)retry;
216
217 ASSERT(token != NULL);
218
219 snprintf(prompt, sizeof(prompt), "%s token", token->label);
220
221 token_pass.defined = false;
222 token_pass.nocache = true;
223
224 if (!get_user_pass(&token_pass, NULL, prompt,
227 {
228 return false;
229 }
230 else
231 {
232 strlcpy(pin, token_pass.password, pin_max);
233 purge_user_pass(&token_pass, true);
234
235 if (strlen(pin) == 0)
236 {
237 return false;
238 }
239 else
240 {
241 return true;
242 }
243 }
244}
245
246bool
247pkcs11_initialize(const bool protected_auth, const int nPINCachePeriod)
248{
249 CK_RV rv = CKR_FUNCTION_FAILED;
250
251 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_initialize - entered");
252
253 if ((rv = pkcs11h_engine_setSystem(&s_pkcs11h_sys_engine)) != CKR_OK)
254 {
255 msg(M_FATAL, "PKCS#11: Cannot initialize system engine %ld-'%s'", rv,
256 pkcs11h_getMessage(rv));
257 goto cleanup;
258 }
259
260 if ((rv = pkcs11h_initialize()) != CKR_OK)
261 {
262 msg(M_FATAL, "PKCS#11: Cannot initialize %ld-'%s'", rv, pkcs11h_getMessage(rv));
263 goto cleanup;
264 }
265
266 if ((rv = pkcs11h_setLogHook(_pkcs11_openvpn_log, NULL)) != CKR_OK)
267 {
268 msg(M_FATAL, "PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
269 goto cleanup;
270 }
271
272 pkcs11h_setLogLevel(_pkcs11_msg_openvpn2pkcs11(get_debug_level()));
273
274 if ((rv = pkcs11h_setForkMode(FALSE)) != CKR_OK)
275 {
276 msg(M_FATAL, "PKCS#11: Cannot set fork mode %ld-'%s'", rv, pkcs11h_getMessage(rv));
277 goto cleanup;
278 }
279
280 if ((rv = pkcs11h_setTokenPromptHook(_pkcs11_openvpn_token_prompt, NULL)) != CKR_OK)
281 {
282 msg(M_FATAL, "PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
283 goto cleanup;
284 }
285
286 if ((rv = pkcs11h_setPINPromptHook(_pkcs11_openvpn_pin_prompt, NULL)) != CKR_OK)
287 {
288 msg(M_FATAL, "PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
289 goto cleanup;
290 }
291
292 if ((rv = pkcs11h_setProtectedAuthentication(protected_auth)) != CKR_OK)
293 {
294 msg(M_FATAL, "PKCS#11: Cannot set protected authentication mode %ld-'%s'", rv,
295 pkcs11h_getMessage(rv));
296 goto cleanup;
297 }
298
299 if ((rv = pkcs11h_setPINCachePeriod(nPINCachePeriod)) != CKR_OK)
300 {
301 msg(M_FATAL, "PKCS#11: Cannot set Pcache period %ld-'%s'", rv, pkcs11h_getMessage(rv));
302 goto cleanup;
303 }
304
305 rv = CKR_OK;
306
307cleanup:
308 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_initialize - return %ld-'%s'", rv,
309 pkcs11h_getMessage(rv));
310
311 return rv == CKR_OK;
312}
313
314void
315pkcs11_terminate(void)
316{
317 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_terminate - entered");
318
319 pkcs11h_terminate();
320
321 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_terminate - return");
322}
323
324bool
325pkcs11_addProvider(const char *const provider, const bool protected_auth,
326 const unsigned private_mode, const bool cert_private)
327{
328 CK_RV rv = CKR_OK;
329
330 ASSERT(provider != NULL);
331
332 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_addProvider - entered - provider='%s', private_mode=%08x",
333 provider, private_mode);
334
335 msg(M_INFO, "PKCS#11: Adding PKCS#11 provider '%s'", provider);
336
337 if ((rv = pkcs11h_registerProvider(provider)) != CKR_OK)
338 {
339 msg(M_WARN, "PKCS#11: Cannot register provider '%s' %ld-'%s'", provider, rv,
340 pkcs11h_getMessage(rv));
341 }
342 else
343 {
344 PKCS11H_BOOL allow_protected_auth = protected_auth;
345 PKCS11H_BOOL cert_is_private = cert_private;
346
347 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOCATION, provider,
348 strlen(provider) + 1);
349
350 if (rv == CKR_OK)
351 {
352 rv = pkcs11h_setProviderProperty(provider,
353 PKCS11H_PROVIDER_PROPERTY_ALLOW_PROTECTED_AUTH,
354 &allow_protected_auth, sizeof(allow_protected_auth));
355 }
356 if (rv == CKR_OK)
357 {
358 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE,
359 &private_mode, sizeof(private_mode));
360 }
361 if (rv == CKR_OK)
362 {
363 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE,
364 &cert_is_private, sizeof(cert_is_private));
365 }
366#if defined(WIN32) && defined(PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS)
367 if (rv == CKR_OK && platform_absolute_pathname(provider))
368 {
369 unsigned loader_flags =
370 LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
371 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS,
372 &loader_flags, sizeof(loader_flags));
373 }
374#endif
375
376 if (rv != CKR_OK || (rv = pkcs11h_initializeProvider(provider)) != CKR_OK)
377 {
378 msg(M_WARN, "PKCS#11: Cannot initialize provider '%s' %ld-'%s'", provider, rv,
379 pkcs11h_getMessage(rv));
380 pkcs11h_removeProvider(provider);
381 }
382 }
383
384 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_addProvider - return rv=%ld-'%s'", rv,
385 pkcs11h_getMessage(rv));
386
387 return rv == CKR_OK;
388}
389
390int
391pkcs11_logout(void)
392{
393 return pkcs11h_logout() == CKR_OK;
394}
395
396int
397pkcs11_management_id_count(void)
398{
399 pkcs11h_certificate_id_list_t id_list = NULL;
400 pkcs11h_certificate_id_list_t t = NULL;
401 CK_RV rv = CKR_OK;
402 int count = 0;
403
404 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_management_id_count - entered");
405
406 if ((rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE_EXIST, NULL,
407 PKCS11H_PROMPT_MASK_ALLOW_ALL, NULL, &id_list))
408 != CKR_OK)
409 {
410 msg(M_WARN, "PKCS#11: Cannot get certificate list %ld-'%s'", rv, pkcs11h_getMessage(rv));
411 goto cleanup;
412 }
413
414 for (count = 0, t = id_list; t != NULL; t = t->next)
415 {
416 count++;
417 }
418
419cleanup:
420
421 pkcs11h_certificate_freeCertificateIdList(id_list);
422 id_list = NULL;
423
424 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_management_id_count - return count=%d", count);
425
426 return count;
427}
428
429bool
430pkcs11_management_id_get(const int index, char **id, char **base64)
431{
432 pkcs11h_certificate_id_list_t id_list = NULL;
433 pkcs11h_certificate_id_list_t entry = NULL;
434 pkcs11h_certificate_t certificate = NULL;
435 CK_RV rv = CKR_OK;
436 unsigned char *certificate_blob = NULL;
437 size_t certificate_blob_size = 0;
438 size_t max;
439 char *internal_id = NULL;
440 char *internal_base64 = NULL;
441 int count = 0;
442 bool success = false;
443
444 ASSERT(id != NULL);
445 ASSERT(base64 != NULL);
446
447 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_management_id_get - entered index=%d", index);
448
449 *id = NULL;
450 *base64 = NULL;
451
452 if ((rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE_EXIST, NULL,
453 PKCS11H_PROMPT_MASK_ALLOW_ALL, NULL, &id_list))
454 != CKR_OK)
455 {
456 msg(M_WARN, "PKCS#11: Cannot get certificate list %ld-'%s'", rv, pkcs11h_getMessage(rv));
457 goto cleanup;
458 }
459
460 entry = id_list;
461 count = 0;
462 while (entry != NULL && count != index)
463 {
464 count++;
465 entry = entry->next;
466 }
467
468 if (entry == NULL)
469 {
470 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_management_id_get - no certificate at index=%d",
471 index);
472 goto cleanup;
473 }
474
475 if ((rv = pkcs11h_certificate_serializeCertificateId(NULL, &max, entry->certificate_id))
476 != CKR_OK)
477 {
478 msg(M_WARN, "PKCS#11: Cannot serialize certificate id %ld-'%s'", rv,
479 pkcs11h_getMessage(rv));
480 goto cleanup;
481 }
482
483 if ((internal_id = (char *)malloc(max)) == NULL)
484 {
485 msg(M_FATAL, "PKCS#11: Cannot allocate memory");
486 goto cleanup;
487 }
488
489 if ((rv = pkcs11h_certificate_serializeCertificateId(internal_id, &max, entry->certificate_id))
490 != CKR_OK)
491 {
492 msg(M_WARN, "PKCS#11: Cannot serialize certificate id %ld-'%s'", rv,
493 pkcs11h_getMessage(rv));
494 goto cleanup;
495 }
496
497 if ((rv = pkcs11h_certificate_create(entry->certificate_id, NULL, PKCS11H_PROMPT_MASK_ALLOW_ALL,
498 PKCS11H_PIN_CACHE_INFINITE, &certificate))
499 != CKR_OK)
500 {
501 msg(M_WARN, "PKCS#11: Cannot get certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
502 goto cleanup;
503 }
504
505 if ((rv = pkcs11h_certificate_getCertificateBlob(certificate, NULL, &certificate_blob_size))
506 != CKR_OK)
507 {
508 msg(M_WARN, "PKCS#11: Cannot get certificate blob %ld-'%s'", rv, pkcs11h_getMessage(rv));
509 goto cleanup;
510 }
511
512 if ((certificate_blob = (unsigned char *)malloc(certificate_blob_size)) == NULL)
513 {
514 msg(M_FATAL, "PKCS#11: Cannot allocate memory");
515 goto cleanup;
516 }
517
518 if ((rv = pkcs11h_certificate_getCertificateBlob(certificate, certificate_blob,
519 &certificate_blob_size))
520 != CKR_OK)
521 {
522 msg(M_WARN, "PKCS#11: Cannot get certificate blob %ld-'%s'", rv, pkcs11h_getMessage(rv));
523 goto cleanup;
524 }
525
526 if (certificate_blob_size > INT_MAX)
527 {
528 msg(M_WARN, "PKCS#11: Invalid certificate size %zu", certificate_blob_size);
529 goto cleanup;
530 }
531
532 if (openvpn_base64_encode(certificate_blob, (int)certificate_blob_size, &internal_base64) == -1)
533 {
534 msg(M_WARN, "PKCS#11: Cannot encode certificate");
535 goto cleanup;
536 }
537
538 *id = internal_id;
539 internal_id = NULL;
540 *base64 = internal_base64;
541 internal_base64 = NULL;
542 success = true;
543
544cleanup:
545
546 pkcs11h_certificate_freeCertificateIdList(id_list);
547 id_list = NULL;
548
549 pkcs11h_certificate_freeCertificate(certificate);
550 certificate = NULL;
551
552 free(internal_id);
553 internal_id = NULL;
554
555 free(internal_base64);
556 internal_base64 = NULL;
557
558 free(certificate_blob);
559 certificate_blob = NULL;
560
561 dmsg(D_PKCS11_DEBUG, "PKCS#11: pkcs11_management_id_get - return success=%d, id='%s'",
562 success ? 1 : 0, *id);
563
564 return success;
565}
566
567int
568tls_ctx_use_pkcs11(struct tls_root_ctx *const ssl_ctx, bool pkcs11_id_management,
569 const char *const pkcs11_id)
570{
571 pkcs11h_certificate_id_t certificate_id = NULL;
572 pkcs11h_certificate_t certificate = NULL;
573 CK_RV rv = CKR_OK;
574
575 bool ok = false;
576
577 ASSERT(ssl_ctx != NULL);
578 ASSERT(pkcs11_id_management || pkcs11_id != NULL);
579
580 dmsg(
582 "PKCS#11: tls_ctx_use_pkcs11 - entered - ssl_ctx=%p, pkcs11_id_management=%d, pkcs11_id='%s'",
583 (void *)ssl_ctx, pkcs11_id_management ? 1 : 0, pkcs11_id);
584
586 {
587 struct user_pass id_resp;
588
589 CLEAR(id_resp);
590
591 id_resp.defined = false;
592 id_resp.nocache = true;
593 snprintf(id_resp.username, sizeof(id_resp.username), "Please specify PKCS#11 id to use");
594
595 if (!get_user_pass(&id_resp, NULL, "pkcs11-id-request",
598 {
599 goto cleanup;
600 }
601
602 if ((rv = pkcs11h_certificate_deserializeCertificateId(&certificate_id, id_resp.password))
603 != CKR_OK)
604 {
605 msg(M_WARN, "PKCS#11: Cannot deserialize id %ld-'%s'", rv, pkcs11h_getMessage(rv));
606 goto cleanup;
607 }
608 }
609 else
610 {
611 if ((rv = pkcs11h_certificate_deserializeCertificateId(&certificate_id, pkcs11_id))
612 != CKR_OK)
613 {
614 msg(M_WARN, "PKCS#11: Cannot deserialize id %ld-'%s'", rv, pkcs11h_getMessage(rv));
615 goto cleanup;
616 }
617 }
618
619 if ((rv = pkcs11h_certificate_create(certificate_id, NULL, PKCS11H_PROMPT_MASK_ALLOW_ALL,
620 PKCS11H_PIN_CACHE_INFINITE, &certificate))
621 != CKR_OK)
622 {
623 msg(M_WARN, "PKCS#11: Cannot get certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
624 goto cleanup;
625 }
626
627 if ((pkcs11_init_tls_session(certificate, ssl_ctx)))
628 {
629 /* Handled by SSL context free */
630 certificate = NULL;
631 goto cleanup;
632 }
633
634 /* Handled by SSL context free */
635 certificate = NULL;
636 ok = true;
637
638cleanup:
639 if (certificate != NULL)
640 {
641 pkcs11h_certificate_freeCertificate(certificate);
642 certificate = NULL;
643 }
644
645 if (certificate_id != NULL)
646 {
647 pkcs11h_certificate_freeCertificateId(certificate_id);
648 certificate_id = NULL;
649 }
650
651 dmsg(D_PKCS11_DEBUG, "PKCS#11: tls_ctx_use_pkcs11 - return ok=%d, rv=%ld", ok ? 1 : 0, rv);
652
653 return ok ? 1 : 0;
654}
655
656static PKCS11H_BOOL
657_pkcs11_openvpn_show_pkcs11_ids_pin_prompt(void *const global_data, void *const user_data,
658 const pkcs11h_token_id_t token, const unsigned retry,
659 char *const pin, const size_t pin_max)
660{
661 struct gc_arena gc = gc_new();
662 struct buffer pass_prompt = alloc_buf_gc(128, &gc);
663
666 (void)retry;
667
668 ASSERT(token != NULL);
669
670 buf_printf(&pass_prompt, "Please enter '%s' token PIN or 'cancel': ", token->display);
671 if (!query_user_SINGLE(BSTR(&pass_prompt), pin, (int)pin_max, false))
672 {
673 msg(M_FATAL, "Could not retrieve the PIN");
674 }
675
676 gc_free(&gc);
677
678 if (!strcmp(pin, "cancel"))
679 {
680 return FALSE;
681 }
682 else
683 {
684 return TRUE;
685 }
686}
687
688void
689show_pkcs11_ids(const char *const provider, bool cert_private)
690{
691 struct gc_arena gc = gc_new();
692 pkcs11h_certificate_id_list_t user_certificates = NULL;
693 pkcs11h_certificate_id_list_t current = NULL;
694 CK_RV rv = CKR_FUNCTION_FAILED;
695
696 if ((rv = pkcs11h_initialize()) != CKR_OK)
697 {
698 msg(M_FATAL, "PKCS#11: Cannot initialize %ld-'%s'", rv, pkcs11h_getMessage(rv));
699 goto cleanup;
700 }
701
702 if ((rv = pkcs11h_setLogHook(_pkcs11_openvpn_log, NULL)) != CKR_OK)
703 {
704 msg(M_FATAL, "PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
705 goto cleanup;
706 }
707
708 pkcs11h_setLogLevel(_pkcs11_msg_openvpn2pkcs11(get_debug_level()));
709
710 if ((rv = pkcs11h_setProtectedAuthentication(TRUE)) != CKR_OK)
711 {
712 msg(M_FATAL, "PKCS#11: Cannot set protected authentication %ld-'%s'", rv,
713 pkcs11h_getMessage(rv));
714 goto cleanup;
715 }
716
717 if ((rv = pkcs11h_setPINPromptHook(_pkcs11_openvpn_show_pkcs11_ids_pin_prompt, NULL)) != CKR_OK)
718 {
719 msg(M_FATAL, "PKCS#11: Cannot set PIN hook %ld-'%s'", rv, pkcs11h_getMessage(rv));
720 goto cleanup;
721 }
722
723 if (!pkcs11_addProvider(provider, TRUE, 0, cert_private ? TRUE : FALSE))
724 {
725 msg(M_FATAL, "Failed to add PKCS#11 provider '%s", provider);
726 goto cleanup;
727 }
728
729 if ((rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE_EXIST, NULL,
730 PKCS11H_PROMPT_MASK_ALLOW_ALL, NULL,
731 &user_certificates))
732 != CKR_OK)
733 {
734 msg(M_FATAL, "PKCS#11: Cannot enumerate certificates %ld-'%s'", rv, pkcs11h_getMessage(rv));
735 goto cleanup;
736 }
737
739 ("\n"
740 "The following objects are available for use.\n"
741 "Each object shown below may be used as parameter to\n"
742 "--pkcs11-id option please remember to use single quote mark.\n"));
743 for (current = user_certificates; current != NULL; current = current->next)
744 {
745 pkcs11h_certificate_t certificate = NULL;
746 char *dn = NULL;
747 char serial[1024] = { 0 };
748 char *ser = NULL;
749 size_t ser_len = 0;
750
751 if ((rv = pkcs11h_certificate_serializeCertificateId(NULL, &ser_len,
752 current->certificate_id))
753 != CKR_OK)
754 {
755 msg(M_FATAL, "PKCS#11: Cannot serialize certificate %ld-'%s'", rv,
756 pkcs11h_getMessage(rv));
757 goto cleanup1;
758 }
759
760 if (rv == CKR_OK && (ser = (char *)malloc(ser_len)) == NULL)
761 {
762 msg(M_FATAL, "PKCS#11: Cannot allocate memory");
763 goto cleanup1;
764 }
765
766 if ((rv =
767 pkcs11h_certificate_serializeCertificateId(ser, &ser_len, current->certificate_id))
768 != CKR_OK)
769 {
770 msg(M_FATAL, "PKCS#11: Cannot serialize certificate %ld-'%s'", rv,
771 pkcs11h_getMessage(rv));
772 goto cleanup1;
773 }
774
775 if ((rv = pkcs11h_certificate_create(current->certificate_id, NULL,
776 PKCS11H_PROMPT_MASK_ALLOW_ALL,
777 PKCS11H_PIN_CACHE_INFINITE, &certificate)))
778 {
779 msg(M_FATAL, "PKCS#11: Cannot create certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
780 goto cleanup1;
781 }
782
783 if ((dn = pkcs11_certificate_dn(certificate, &gc)) == NULL)
784 {
785 goto cleanup1;
786 }
787
788 if ((pkcs11_certificate_serial(certificate, serial, sizeof(serial))))
789 {
790 goto cleanup1;
791 }
792
794 ("\n"
795 "Certificate\n"
796 " DN: %s\n"
797 " Serial: %s\n"
798 " Serialized id: %s\n"),
799 dn, serial, ser);
800
801cleanup1:
802
803 if (certificate != NULL)
804 {
805 pkcs11h_certificate_freeCertificate(certificate);
806 certificate = NULL;
807 }
808
809 free(ser);
810 ser = NULL;
811 }
812
813cleanup:
814 pkcs11h_certificate_freeCertificateIdList(user_certificates);
815 user_certificates = NULL;
816
817 pkcs11h_terminate();
818 gc_free(&gc);
819}
820
821#if defined(__GNUC__) || defined(__clang__)
822#pragma GCC diagnostic pop
823#endif
824
825#endif /* ENABLE_PKCS11 */
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
size_t strlcpy(char *dest, const char *src, size_t maxlen)
static bool query_user_SINGLE(char *prompt, char *resp, size_t resp_len, bool echo)
A plain "make Gert happy" wrapper.
Definition console.h:118
#define D_PKCS11_DEBUG
Definition errlevel.h:169
#define D_SHOW_PKCS11
Definition errlevel.h:138
#define M_INFO
Definition errlevel.h:54
void purge_user_pass(struct user_pass *up, const bool force)
Definition misc.c:485
#define GET_USER_PASS_MANAGEMENT
Definition misc.h:113
#define GET_USER_PASS_PASSWORD_ONLY
Definition misc.h:115
#define GET_USER_PASS_NEED_OK
Definition misc.h:116
#define GET_USER_PASS_NOFATAL
Definition misc.h:117
static bool get_user_pass(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags)
Retrieves the user credentials from various sources depending on the flags.
Definition misc.h:155
#define GET_USER_PASS_NEED_STR
Definition misc.h:118
#define CLEAR(x)
Definition basic.h:32
msglvl_t get_debug_level(void)
Definition error.c:133
#define M_NOPREFIX
Definition error.h:98
#define M_FATAL
Definition error.h:90
#define M_NOLF
Definition error.h:102
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
time_t now
Definition otime.c:33
static void update_time(void)
Definition otime.h:84
PKCS #11 SSL library-specific backend.
bool platform_absolute_pathname(const char *pathname)
Return true if pathname is absolute.
Definition platform.c:671
int openvpn_base64_encode(const void *data, int size, char **str)
Definition base64.c:51
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Structure that wraps the TLS context.
Definition ssl_mbedtls.h:86
static int cleanup(void **state)
static bool pkcs11_id_management
struct gc_arena gc
Definition test_ssl.c:138