29#if defined(ENABLE_PKCS11)
31#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
51__mygettimeofday(
struct timeval *tv)
53 return gettimeofday(tv, NULL);
57#if defined(__GNUC__) || defined(__clang__)
58#pragma GCC diagnostic push
59#pragma GCC diagnostic ignored "-Wsign-conversion"
63__mysleep(
unsigned long usec)
72 usleep((useconds_t)usec);
77static pkcs11h_engine_system_t s_pkcs11h_sys_engine = { malloc, free, __mytime, __mysleep,
86_pkcs11_msg_pkcs112openvpn(
const unsigned flags)
88#ifdef ENABLE_PKCS11_FORCE_DEBUG
95 case PKCS11H_LOG_DEBUG2:
99 case PKCS11H_LOG_DEBUG1:
103 case PKCS11H_LOG_INFO:
107 case PKCS11H_LOG_WARN:
111 case PKCS11H_LOG_ERROR:
120 return openvpn_flags;
125_pkcs11_msg_openvpn2pkcs11(
const msglvl_t flags)
127#ifdef ENABLE_PKCS11_FORCE_DEBUG
128 return PKCS11H_LOG_DEBUG2;
130 unsigned pkcs11_flags;
134 pkcs11_flags = PKCS11H_LOG_DEBUG2;
138 pkcs11_flags = PKCS11H_LOG_DEBUG1;
140 else if ((flags &
M_INFO) != 0)
142 pkcs11_flags = PKCS11H_LOG_INFO;
144 else if ((flags &
M_WARN) != 0)
146 pkcs11_flags = PKCS11H_LOG_WARN;
148 else if ((flags &
M_FATAL) != 0)
150 pkcs11_flags = PKCS11H_LOG_ERROR;
154 pkcs11_flags = PKCS11H_LOG_ERROR;
162_pkcs11_openvpn_log(
void *
const global_data,
unsigned flags,
const char *
const szFormat,
165 char Buffer[10 * 1024];
169 vsnprintf(Buffer,
sizeof(Buffer), szFormat, args);
170 Buffer[
sizeof(Buffer) - 1] = 0;
172 msg(_pkcs11_msg_pkcs112openvpn(flags),
"%s", Buffer);
176_pkcs11_openvpn_token_prompt(
void *
const global_data,
void *
const user_data,
177 const pkcs11h_token_id_t token,
const unsigned retry)
188 token_resp.defined =
false;
189 token_resp.nocache =
true;
190 snprintf(token_resp.username,
sizeof(token_resp.username),
"Please insert %s token",
193 if (!
get_user_pass(&token_resp, NULL,
"token-insertion-request",
200 return strcmp(token_resp.password,
"ok") == 0;
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)
219 snprintf(prompt,
sizeof(prompt),
"%s token", token->label);
221 token_pass.defined =
false;
222 token_pass.nocache =
true;
232 strlcpy(pin, token_pass.password, pin_max);
235 if (strlen(pin) == 0)
247pkcs11_initialize(
const bool protected_auth,
const int nPINCachePeriod)
249 CK_RV rv = CKR_FUNCTION_FAILED;
253 if ((rv = pkcs11h_engine_setSystem(&s_pkcs11h_sys_engine)) != CKR_OK)
255 msg(
M_FATAL,
"PKCS#11: Cannot initialize system engine %ld-'%s'", rv,
256 pkcs11h_getMessage(rv));
260 if ((rv = pkcs11h_initialize()) != CKR_OK)
262 msg(
M_FATAL,
"PKCS#11: Cannot initialize %ld-'%s'", rv, pkcs11h_getMessage(rv));
266 if ((rv = pkcs11h_setLogHook(_pkcs11_openvpn_log, NULL)) != CKR_OK)
268 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
274 if ((rv = pkcs11h_setForkMode(FALSE)) != CKR_OK)
276 msg(
M_FATAL,
"PKCS#11: Cannot set fork mode %ld-'%s'", rv, pkcs11h_getMessage(rv));
280 if ((rv = pkcs11h_setTokenPromptHook(_pkcs11_openvpn_token_prompt, NULL)) != CKR_OK)
282 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
286 if ((rv = pkcs11h_setPINPromptHook(_pkcs11_openvpn_pin_prompt, NULL)) != CKR_OK)
288 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
292 if ((rv = pkcs11h_setProtectedAuthentication(protected_auth)) != CKR_OK)
294 msg(
M_FATAL,
"PKCS#11: Cannot set protected authentication mode %ld-'%s'", rv,
295 pkcs11h_getMessage(rv));
299 if ((rv = pkcs11h_setPINCachePeriod(nPINCachePeriod)) != CKR_OK)
301 msg(
M_FATAL,
"PKCS#11: Cannot set Pcache period %ld-'%s'", rv, pkcs11h_getMessage(rv));
309 pkcs11h_getMessage(rv));
315pkcs11_terminate(
void)
325pkcs11_addProvider(
const char *
const provider,
const bool protected_auth,
326 const unsigned private_mode,
const bool cert_private)
332 dmsg(
D_PKCS11_DEBUG,
"PKCS#11: pkcs11_addProvider - entered - provider='%s', private_mode=%08x",
333 provider, private_mode);
335 msg(
M_INFO,
"PKCS#11: Adding PKCS#11 provider '%s'", provider);
337 if ((rv = pkcs11h_registerProvider(provider)) != CKR_OK)
339 msg(
M_WARN,
"PKCS#11: Cannot register provider '%s' %ld-'%s'", provider, rv,
340 pkcs11h_getMessage(rv));
344 PKCS11H_BOOL allow_protected_auth = protected_auth;
345 PKCS11H_BOOL cert_is_private = cert_private;
347 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOCATION, provider,
348 strlen(provider) + 1);
352 rv = pkcs11h_setProviderProperty(provider,
353 PKCS11H_PROVIDER_PROPERTY_ALLOW_PROTECTED_AUTH,
354 &allow_protected_auth,
sizeof(allow_protected_auth));
358 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE,
359 &private_mode,
sizeof(private_mode));
363 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE,
364 &cert_is_private,
sizeof(cert_is_private));
366#if defined(WIN32) && defined(PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS)
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));
376 if (rv != CKR_OK || (rv = pkcs11h_initializeProvider(provider)) != CKR_OK)
378 msg(
M_WARN,
"PKCS#11: Cannot initialize provider '%s' %ld-'%s'", provider, rv,
379 pkcs11h_getMessage(rv));
380 pkcs11h_removeProvider(provider);
385 pkcs11h_getMessage(rv));
393 return pkcs11h_logout() == CKR_OK;
397pkcs11_management_id_count(
void)
399 pkcs11h_certificate_id_list_t id_list = NULL;
400 pkcs11h_certificate_id_list_t t = NULL;
406 if ((rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE_EXIST, NULL,
407 PKCS11H_PROMPT_MASK_ALLOW_ALL, NULL, &id_list))
410 msg(
M_WARN,
"PKCS#11: Cannot get certificate list %ld-'%s'", rv, pkcs11h_getMessage(rv));
414 for (count = 0, t = id_list; t != NULL; t = t->next)
421 pkcs11h_certificate_freeCertificateIdList(id_list);
430pkcs11_management_id_get(
const int index,
char **
id,
char **base64)
432 pkcs11h_certificate_id_list_t id_list = NULL;
433 pkcs11h_certificate_id_list_t entry = NULL;
434 pkcs11h_certificate_t certificate = NULL;
436 unsigned char *certificate_blob = NULL;
437 size_t certificate_blob_size = 0;
439 char *internal_id = NULL;
440 char *internal_base64 = NULL;
442 bool success =
false;
452 if ((rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE_EXIST, NULL,
453 PKCS11H_PROMPT_MASK_ALLOW_ALL, NULL, &id_list))
456 msg(
M_WARN,
"PKCS#11: Cannot get certificate list %ld-'%s'", rv, pkcs11h_getMessage(rv));
462 while (entry != NULL && count != index)
470 dmsg(
D_PKCS11_DEBUG,
"PKCS#11: pkcs11_management_id_get - no certificate at index=%d",
475 if ((rv = pkcs11h_certificate_serializeCertificateId(NULL, &max, entry->certificate_id))
478 msg(
M_WARN,
"PKCS#11: Cannot serialize certificate id %ld-'%s'", rv,
479 pkcs11h_getMessage(rv));
483 if ((internal_id = (
char *)malloc(max)) == NULL)
485 msg(
M_FATAL,
"PKCS#11: Cannot allocate memory");
489 if ((rv = pkcs11h_certificate_serializeCertificateId(internal_id, &max, entry->certificate_id))
492 msg(
M_WARN,
"PKCS#11: Cannot serialize certificate id %ld-'%s'", rv,
493 pkcs11h_getMessage(rv));
497 if ((rv = pkcs11h_certificate_create(entry->certificate_id, NULL, PKCS11H_PROMPT_MASK_ALLOW_ALL,
498 PKCS11H_PIN_CACHE_INFINITE, &certificate))
501 msg(
M_WARN,
"PKCS#11: Cannot get certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
505 if ((rv = pkcs11h_certificate_getCertificateBlob(certificate, NULL, &certificate_blob_size))
508 msg(
M_WARN,
"PKCS#11: Cannot get certificate blob %ld-'%s'", rv, pkcs11h_getMessage(rv));
512 if ((certificate_blob = (
unsigned char *)malloc(certificate_blob_size)) == NULL)
514 msg(
M_FATAL,
"PKCS#11: Cannot allocate memory");
518 if ((rv = pkcs11h_certificate_getCertificateBlob(certificate, certificate_blob,
519 &certificate_blob_size))
522 msg(
M_WARN,
"PKCS#11: Cannot get certificate blob %ld-'%s'", rv, pkcs11h_getMessage(rv));
526 if (certificate_blob_size > INT_MAX)
528 msg(
M_WARN,
"PKCS#11: Invalid certificate size %zu", certificate_blob_size);
534 msg(
M_WARN,
"PKCS#11: Cannot encode certificate");
540 *base64 = internal_base64;
541 internal_base64 = NULL;
546 pkcs11h_certificate_freeCertificateIdList(id_list);
549 pkcs11h_certificate_freeCertificate(certificate);
555 free(internal_base64);
556 internal_base64 = NULL;
558 free(certificate_blob);
559 certificate_blob = NULL;
561 dmsg(
D_PKCS11_DEBUG,
"PKCS#11: pkcs11_management_id_get - return success=%d, id='%s'",
562 success ? 1 : 0, *id);
569 const char *
const pkcs11_id)
571 pkcs11h_certificate_id_t certificate_id = NULL;
572 pkcs11h_certificate_t certificate = NULL;
582 "PKCS#11: tls_ctx_use_pkcs11 - entered - ssl_ctx=%p, pkcs11_id_management=%d, pkcs11_id='%s'",
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");
602 if ((rv = pkcs11h_certificate_deserializeCertificateId(&certificate_id, id_resp.password))
605 msg(
M_WARN,
"PKCS#11: Cannot deserialize id %ld-'%s'", rv, pkcs11h_getMessage(rv));
611 if ((rv = pkcs11h_certificate_deserializeCertificateId(&certificate_id, pkcs11_id))
614 msg(
M_WARN,
"PKCS#11: Cannot deserialize id %ld-'%s'", rv, pkcs11h_getMessage(rv));
619 if ((rv = pkcs11h_certificate_create(certificate_id, NULL, PKCS11H_PROMPT_MASK_ALLOW_ALL,
620 PKCS11H_PIN_CACHE_INFINITE, &certificate))
623 msg(
M_WARN,
"PKCS#11: Cannot get certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
627 if ((pkcs11_init_tls_session(certificate, ssl_ctx)))
639 if (certificate != NULL)
641 pkcs11h_certificate_freeCertificate(certificate);
645 if (certificate_id != NULL)
647 pkcs11h_certificate_freeCertificateId(certificate_id);
648 certificate_id = NULL;
651 dmsg(
D_PKCS11_DEBUG,
"PKCS#11: tls_ctx_use_pkcs11 - return ok=%d, rv=%ld", ok ? 1 : 0, rv);
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)
692 pkcs11h_certificate_id_list_t user_certificates = NULL;
693 pkcs11h_certificate_id_list_t current = NULL;
694 CK_RV rv = CKR_FUNCTION_FAILED;
696 if ((rv = pkcs11h_initialize()) != CKR_OK)
698 msg(
M_FATAL,
"PKCS#11: Cannot initialize %ld-'%s'", rv, pkcs11h_getMessage(rv));
702 if ((rv = pkcs11h_setLogHook(_pkcs11_openvpn_log, NULL)) != CKR_OK)
704 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
710 if ((rv = pkcs11h_setProtectedAuthentication(TRUE)) != CKR_OK)
712 msg(
M_FATAL,
"PKCS#11: Cannot set protected authentication %ld-'%s'", rv,
713 pkcs11h_getMessage(rv));
717 if ((rv = pkcs11h_setPINPromptHook(_pkcs11_openvpn_show_pkcs11_ids_pin_prompt, NULL)) != CKR_OK)
719 msg(
M_FATAL,
"PKCS#11: Cannot set PIN hook %ld-'%s'", rv, pkcs11h_getMessage(rv));
723 if (!pkcs11_addProvider(provider, TRUE, 0, cert_private ? TRUE : FALSE))
725 msg(
M_FATAL,
"Failed to add PKCS#11 provider '%s", provider);
729 if ((rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE_EXIST, NULL,
730 PKCS11H_PROMPT_MASK_ALLOW_ALL, NULL,
734 msg(
M_FATAL,
"PKCS#11: Cannot enumerate certificates %ld-'%s'", rv, pkcs11h_getMessage(rv));
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)
745 pkcs11h_certificate_t certificate = NULL;
747 char serial[1024] = { 0 };
751 if ((rv = pkcs11h_certificate_serializeCertificateId(NULL, &ser_len,
752 current->certificate_id))
755 msg(
M_FATAL,
"PKCS#11: Cannot serialize certificate %ld-'%s'", rv,
756 pkcs11h_getMessage(rv));
760 if (rv == CKR_OK && (ser = (
char *)malloc(ser_len)) == NULL)
762 msg(
M_FATAL,
"PKCS#11: Cannot allocate memory");
767 pkcs11h_certificate_serializeCertificateId(ser, &ser_len, current->certificate_id))
770 msg(
M_FATAL,
"PKCS#11: Cannot serialize certificate %ld-'%s'", rv,
771 pkcs11h_getMessage(rv));
775 if ((rv = pkcs11h_certificate_create(current->certificate_id, NULL,
776 PKCS11H_PROMPT_MASK_ALLOW_ALL,
777 PKCS11H_PIN_CACHE_INFINITE, &certificate)))
779 msg(
M_FATAL,
"PKCS#11: Cannot create certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
783 if ((dn = pkcs11_certificate_dn(certificate, &
gc)) == NULL)
788 if ((pkcs11_certificate_serial(certificate, serial,
sizeof(serial))))
798 " Serialized id: %s\n"),
803 if (certificate != NULL)
805 pkcs11h_certificate_freeCertificate(certificate);
814 pkcs11h_certificate_freeCertificateIdList(user_certificates);
815 user_certificates = NULL;
821#if defined(__GNUC__) || defined(__clang__)
822#pragma GCC diagnostic pop
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
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.
void purge_user_pass(struct user_pass *up, const bool force)
#define GET_USER_PASS_MANAGEMENT
#define GET_USER_PASS_PASSWORD_ONLY
#define GET_USER_PASS_NEED_OK
#define GET_USER_PASS_NOFATAL
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.
#define GET_USER_PASS_NEED_STR
msglvl_t get_debug_level(void)
static void update_time(void)
PKCS #11 SSL library-specific backend.
int openvpn_base64_encode(const void *data, int size, char **str)
Wrapper structure for dynamically allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
Garbage collection arena used to keep track of dynamically allocated memory.
Structure that wraps the TLS context.
static int cleanup(void **state)
static bool pkcs11_id_management