29#if defined(ENABLE_PKCS11)
31#include <pkcs11-helper-1.0/pkcs11h-certificate.h>
50__mygettimeofday(
struct timeval *tv)
52 return gettimeofday(tv, NULL);
57__mysleep(
unsigned long usec)
66 usleep((useconds_t)usec);
71static pkcs11h_engine_system_t s_pkcs11h_sys_engine = { malloc, free, __mytime, __mysleep,
80_pkcs11_msg_pkcs112openvpn(
const unsigned flags)
86 case PKCS11H_LOG_DEBUG2:
90 case PKCS11H_LOG_DEBUG1:
94 case PKCS11H_LOG_INFO:
98 case PKCS11H_LOG_WARN:
102 case PKCS11H_LOG_ERROR:
111#if defined(ENABLE_PKCS11_FORCE_DEBUG)
115 return openvpn_flags;
119_pkcs11_msg_openvpn2pkcs11(
const msglvl_t flags)
121 unsigned pkcs11_flags;
125 pkcs11_flags = PKCS11H_LOG_DEBUG2;
129 pkcs11_flags = PKCS11H_LOG_DEBUG1;
131 else if ((flags &
M_INFO) != 0)
133 pkcs11_flags = PKCS11H_LOG_INFO;
135 else if ((flags &
M_WARN) != 0)
137 pkcs11_flags = PKCS11H_LOG_WARN;
139 else if ((flags &
M_FATAL) != 0)
141 pkcs11_flags = PKCS11H_LOG_ERROR;
145 pkcs11_flags = PKCS11H_LOG_ERROR;
148#if defined(ENABLE_PKCS11_FORCE_DEBUG)
149 pkcs11_flags = PKCS11H_LOG_DEBUG2;
156_pkcs11_openvpn_log(
void *
const global_data,
unsigned flags,
const char *
const szFormat,
159 char Buffer[10 * 1024];
163 vsnprintf(Buffer,
sizeof(Buffer), szFormat, args);
164 Buffer[
sizeof(Buffer) - 1] = 0;
166 msg(_pkcs11_msg_pkcs112openvpn(flags),
"%s", Buffer);
170_pkcs11_openvpn_token_prompt(
void *
const global_data,
void *
const user_data,
171 const pkcs11h_token_id_t token,
const unsigned retry)
182 token_resp.defined =
false;
183 token_resp.nocache =
true;
184 snprintf(token_resp.username,
sizeof(token_resp.username),
"Please insert %s token",
187 if (!
get_user_pass(&token_resp, NULL,
"token-insertion-request",
194 return strcmp(token_resp.password,
"ok") == 0;
199_pkcs11_openvpn_pin_prompt(
void *
const global_data,
void *
const user_data,
200 const pkcs11h_token_id_t token,
const unsigned retry,
char *
const pin,
201 const size_t pin_max)
213 snprintf(prompt,
sizeof(prompt),
"%s token", token->label);
215 token_pass.defined =
false;
216 token_pass.nocache =
true;
226 strncpynt(pin, token_pass.password, pin_max);
229 if (strlen(pin) == 0)
241pkcs11_initialize(
const bool protected_auth,
const int nPINCachePeriod)
243 CK_RV rv = CKR_FUNCTION_FAILED;
247 if ((rv = pkcs11h_engine_setSystem(&s_pkcs11h_sys_engine)) != CKR_OK)
249 msg(
M_FATAL,
"PKCS#11: Cannot initialize system engine %ld-'%s'", rv,
250 pkcs11h_getMessage(rv));
254 if ((rv = pkcs11h_initialize()) != CKR_OK)
256 msg(
M_FATAL,
"PKCS#11: Cannot initialize %ld-'%s'", rv, pkcs11h_getMessage(rv));
260 if ((rv = pkcs11h_setLogHook(_pkcs11_openvpn_log, NULL)) != CKR_OK)
262 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
268 if ((rv = pkcs11h_setForkMode(FALSE)) != CKR_OK)
270 msg(
M_FATAL,
"PKCS#11: Cannot set fork mode %ld-'%s'", rv, pkcs11h_getMessage(rv));
274 if ((rv = pkcs11h_setTokenPromptHook(_pkcs11_openvpn_token_prompt, NULL)) != CKR_OK)
276 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
280 if ((rv = pkcs11h_setPINPromptHook(_pkcs11_openvpn_pin_prompt, NULL)) != CKR_OK)
282 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
286 if ((rv = pkcs11h_setProtectedAuthentication(protected_auth)) != CKR_OK)
288 msg(
M_FATAL,
"PKCS#11: Cannot set protected authentication mode %ld-'%s'", rv,
289 pkcs11h_getMessage(rv));
293 if ((rv = pkcs11h_setPINCachePeriod(nPINCachePeriod)) != CKR_OK)
295 msg(
M_FATAL,
"PKCS#11: Cannot set Pcache period %ld-'%s'", rv, pkcs11h_getMessage(rv));
303 pkcs11h_getMessage(rv));
309pkcs11_terminate(
void)
319pkcs11_addProvider(
const char *
const provider,
const bool protected_auth,
320 const unsigned private_mode,
const bool cert_private)
326 dmsg(
D_PKCS11_DEBUG,
"PKCS#11: pkcs11_addProvider - entered - provider='%s', private_mode=%08x",
327 provider, private_mode);
329 msg(
M_INFO,
"PKCS#11: Adding PKCS#11 provider '%s'", provider);
331#if PKCS11H_VERSION >= ((1 << 16) | (28 << 8) | (0 << 0))
332 if ((rv = pkcs11h_registerProvider(provider)) != CKR_OK)
334 msg(
M_WARN,
"PKCS#11: Cannot register provider '%s' %ld-'%s'", provider, rv,
335 pkcs11h_getMessage(rv));
339 PKCS11H_BOOL allow_protected_auth = protected_auth;
340 PKCS11H_BOOL cert_is_private = cert_private;
342 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOCATION, provider,
343 strlen(provider) + 1);
347 rv = pkcs11h_setProviderProperty(provider,
348 PKCS11H_PROVIDER_PROPERTY_ALLOW_PROTECTED_AUTH,
349 &allow_protected_auth,
sizeof(allow_protected_auth));
353 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE,
354 &private_mode,
sizeof(private_mode));
358 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE,
359 &cert_is_private,
sizeof(cert_is_private));
361#if defined(WIN32) && defined(PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS)
364 unsigned loader_flags =
365 LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
366 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS,
367 &loader_flags,
sizeof(loader_flags));
371 if (rv != CKR_OK || (rv = pkcs11h_initializeProvider(provider)) != CKR_OK)
373 msg(
M_WARN,
"PKCS#11: Cannot initialize provider '%s' %ld-'%s'", provider, rv,
374 pkcs11h_getMessage(rv));
375 pkcs11h_removeProvider(provider);
379 if ((rv = pkcs11h_addProvider(provider, provider, protected_auth, private_mode,
380 PKCS11H_SLOTEVENT_METHOD_AUTO, 0, cert_private))
383 msg(
M_WARN,
"PKCS#11: Cannot initialize provider '%s' %ld-'%s'", provider, rv,
384 pkcs11h_getMessage(rv));
389 pkcs11h_getMessage(rv));
397 return pkcs11h_logout() == CKR_OK;
401pkcs11_management_id_count(
void)
403 pkcs11h_certificate_id_list_t id_list = NULL;
404 pkcs11h_certificate_id_list_t t = NULL;
410 if ((rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE_EXIST, NULL,
411 PKCS11H_PROMPT_MASK_ALLOW_ALL, NULL, &id_list))
414 msg(
M_WARN,
"PKCS#11: Cannot get certificate list %ld-'%s'", rv, pkcs11h_getMessage(rv));
418 for (count = 0, t = id_list; t != NULL; t = t->next)
425 pkcs11h_certificate_freeCertificateIdList(id_list);
434pkcs11_management_id_get(
const int index,
char **
id,
char **base64)
436 pkcs11h_certificate_id_list_t id_list = NULL;
437 pkcs11h_certificate_id_list_t entry = NULL;
438 pkcs11h_certificate_t certificate = NULL;
440 unsigned char *certificate_blob = NULL;
441 size_t certificate_blob_size = 0;
443 char *internal_id = NULL;
444 char *internal_base64 = NULL;
446 bool success =
false;
456 if ((rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE_EXIST, NULL,
457 PKCS11H_PROMPT_MASK_ALLOW_ALL, NULL, &id_list))
460 msg(
M_WARN,
"PKCS#11: Cannot get certificate list %ld-'%s'", rv, pkcs11h_getMessage(rv));
466 while (entry != NULL && count != index)
474 dmsg(
D_PKCS11_DEBUG,
"PKCS#11: pkcs11_management_id_get - no certificate at index=%d",
479 if ((rv = pkcs11h_certificate_serializeCertificateId(NULL, &max, entry->certificate_id))
482 msg(
M_WARN,
"PKCS#11: Cannot serialize certificate id %ld-'%s'", rv,
483 pkcs11h_getMessage(rv));
487 if ((internal_id = (
char *)malloc(max)) == NULL)
489 msg(
M_FATAL,
"PKCS#11: Cannot allocate memory");
493 if ((rv = pkcs11h_certificate_serializeCertificateId(internal_id, &max, entry->certificate_id))
496 msg(
M_WARN,
"PKCS#11: Cannot serialize certificate id %ld-'%s'", rv,
497 pkcs11h_getMessage(rv));
501 if ((rv = pkcs11h_certificate_create(entry->certificate_id, NULL, PKCS11H_PROMPT_MASK_ALLOW_ALL,
502 PKCS11H_PIN_CACHE_INFINITE, &certificate))
505 msg(
M_WARN,
"PKCS#11: Cannot get certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
509 if ((rv = pkcs11h_certificate_getCertificateBlob(certificate, NULL, &certificate_blob_size))
512 msg(
M_WARN,
"PKCS#11: Cannot get certificate blob %ld-'%s'", rv, pkcs11h_getMessage(rv));
516 if ((certificate_blob = (
unsigned char *)malloc(certificate_blob_size)) == NULL)
518 msg(
M_FATAL,
"PKCS#11: Cannot allocate memory");
522 if ((rv = pkcs11h_certificate_getCertificateBlob(certificate, certificate_blob,
523 &certificate_blob_size))
526 msg(
M_WARN,
"PKCS#11: Cannot get certificate blob %ld-'%s'", rv, pkcs11h_getMessage(rv));
530 if (certificate_blob_size > INT_MAX)
532 msg(
M_WARN,
"PKCS#11: Invalid certificate size %zu", certificate_blob_size);
538 msg(
M_WARN,
"PKCS#11: Cannot encode certificate");
544 *base64 = internal_base64;
545 internal_base64 = NULL;
550 pkcs11h_certificate_freeCertificateIdList(id_list);
553 pkcs11h_certificate_freeCertificate(certificate);
559 free(internal_base64);
560 internal_base64 = NULL;
562 free(certificate_blob);
563 certificate_blob = NULL;
565 dmsg(
D_PKCS11_DEBUG,
"PKCS#11: pkcs11_management_id_get - return success=%d, id='%s'",
566 success ? 1 : 0, *id);
573 const char *
const pkcs11_id)
575 pkcs11h_certificate_id_t certificate_id = NULL;
576 pkcs11h_certificate_t certificate = NULL;
586 "PKCS#11: tls_ctx_use_pkcs11 - entered - ssl_ctx=%p, pkcs11_id_management=%d, pkcs11_id='%s'",
595 id_resp.defined =
false;
596 id_resp.nocache =
true;
597 snprintf(id_resp.username,
sizeof(id_resp.username),
"Please specify PKCS#11 id to use");
606 if ((rv = pkcs11h_certificate_deserializeCertificateId(&certificate_id, id_resp.password))
609 msg(
M_WARN,
"PKCS#11: Cannot deserialize id %ld-'%s'", rv, pkcs11h_getMessage(rv));
615 if ((rv = pkcs11h_certificate_deserializeCertificateId(&certificate_id, pkcs11_id))
618 msg(
M_WARN,
"PKCS#11: Cannot deserialize id %ld-'%s'", rv, pkcs11h_getMessage(rv));
623 if ((rv = pkcs11h_certificate_create(certificate_id, NULL, PKCS11H_PROMPT_MASK_ALLOW_ALL,
624 PKCS11H_PIN_CACHE_INFINITE, &certificate))
627 msg(
M_WARN,
"PKCS#11: Cannot get certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
631 if ((pkcs11_init_tls_session(certificate, ssl_ctx)))
643 if (certificate != NULL)
645 pkcs11h_certificate_freeCertificate(certificate);
649 if (certificate_id != NULL)
651 pkcs11h_certificate_freeCertificateId(certificate_id);
652 certificate_id = NULL;
655 dmsg(
D_PKCS11_DEBUG,
"PKCS#11: tls_ctx_use_pkcs11 - return ok=%d, rv=%ld", ok ? 1 : 0, rv);
661_pkcs11_openvpn_show_pkcs11_ids_pin_prompt(
void *
const global_data,
void *
const user_data,
662 const pkcs11h_token_id_t token,
const unsigned retry,
663 char *
const pin,
const size_t pin_max)
696 pkcs11h_certificate_id_list_t user_certificates = NULL;
697 pkcs11h_certificate_id_list_t current = NULL;
698 CK_RV rv = CKR_FUNCTION_FAILED;
700 if ((rv = pkcs11h_initialize()) != CKR_OK)
702 msg(
M_FATAL,
"PKCS#11: Cannot initialize %ld-'%s'", rv, pkcs11h_getMessage(rv));
706 if ((rv = pkcs11h_setLogHook(_pkcs11_openvpn_log, NULL)) != CKR_OK)
708 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
714 if ((rv = pkcs11h_setProtectedAuthentication(TRUE)) != CKR_OK)
716 msg(
M_FATAL,
"PKCS#11: Cannot set protected authentication %ld-'%s'", rv,
717 pkcs11h_getMessage(rv));
721 if ((rv = pkcs11h_setPINPromptHook(_pkcs11_openvpn_show_pkcs11_ids_pin_prompt, NULL)) != CKR_OK)
723 msg(
M_FATAL,
"PKCS#11: Cannot set PIN hook %ld-'%s'", rv, pkcs11h_getMessage(rv));
727 if (!pkcs11_addProvider(provider, TRUE, 0, cert_private ? TRUE : FALSE))
729 msg(
M_FATAL,
"Failed to add PKCS#11 provider '%s", provider);
733 if ((rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE_EXIST, NULL,
734 PKCS11H_PROMPT_MASK_ALLOW_ALL, NULL,
738 msg(
M_FATAL,
"PKCS#11: Cannot enumerate certificates %ld-'%s'", rv, pkcs11h_getMessage(rv));
744 "The following objects are available for use.\n"
745 "Each object shown below may be used as parameter to\n"
746 "--pkcs11-id option please remember to use single quote mark.\n"));
747 for (current = user_certificates; current != NULL; current = current->next)
749 pkcs11h_certificate_t certificate = NULL;
751 char serial[1024] = { 0 };
755 if ((rv = pkcs11h_certificate_serializeCertificateId(NULL, &ser_len,
756 current->certificate_id))
759 msg(
M_FATAL,
"PKCS#11: Cannot serialize certificate %ld-'%s'", rv,
760 pkcs11h_getMessage(rv));
764 if (rv == CKR_OK && (ser = (
char *)malloc(ser_len)) == NULL)
766 msg(
M_FATAL,
"PKCS#11: Cannot allocate memory");
771 pkcs11h_certificate_serializeCertificateId(ser, &ser_len, current->certificate_id))
774 msg(
M_FATAL,
"PKCS#11: Cannot serialize certificate %ld-'%s'", rv,
775 pkcs11h_getMessage(rv));
779 if ((rv = pkcs11h_certificate_create(current->certificate_id, NULL,
780 PKCS11H_PROMPT_MASK_ALLOW_ALL,
781 PKCS11H_PIN_CACHE_INFINITE, &certificate)))
783 msg(
M_FATAL,
"PKCS#11: Cannot create certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
787 if ((dn = pkcs11_certificate_dn(certificate, &
gc)) == NULL)
792 if ((pkcs11_certificate_serial(certificate, serial,
sizeof(serial))))
802 " Serialized id: %s\n"),
807 if (certificate != NULL)
809 pkcs11h_certificate_freeCertificate(certificate);
818 pkcs11h_certificate_freeCertificateIdList(user_certificates);
819 user_certificates = NULL;
bool buf_printf(struct buffer *buf, const char *format,...)
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
static void strncpynt(char *dest, const char *src, size_t maxlen)
static void gc_free(struct gc_arena *a)
static struct gc_arena gc_new(void)
static bool query_user_SINGLE(char *prompt, char *resp, int 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 time_t openvpn_time(time_t *t)
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