30 #if defined(ENABLE_PKCS11)
32 #include <pkcs11-helper-1.0/pkcs11h-certificate.h>
53 __mygettimeofday(
struct timeval *tv)
55 return gettimeofday(tv, NULL);
61 __mysleep(
const unsigned long usec)
71 static pkcs11h_engine_system_t s_pkcs11h_sys_engine = {
85 _pkcs11_msg_pkcs112openvpn(
89 unsigned openvpn_flags;
93 case PKCS11H_LOG_DEBUG2:
97 case PKCS11H_LOG_DEBUG1:
101 case PKCS11H_LOG_INFO:
105 case PKCS11H_LOG_WARN:
109 case PKCS11H_LOG_ERROR:
118 #if defined(ENABLE_PKCS11_FORCE_DEBUG)
122 return openvpn_flags;
127 _pkcs11_msg_openvpn2pkcs11(
131 unsigned pkcs11_flags;
135 pkcs11_flags = PKCS11H_LOG_DEBUG2;
139 pkcs11_flags = PKCS11H_LOG_DEBUG1;
141 else if ((flags &
M_INFO) != 0)
143 pkcs11_flags = PKCS11H_LOG_INFO;
145 else if ((flags &
M_WARN) != 0)
147 pkcs11_flags = PKCS11H_LOG_WARN;
149 else if ((flags &
M_FATAL) != 0)
151 pkcs11_flags = PKCS11H_LOG_ERROR;
155 pkcs11_flags = PKCS11H_LOG_ERROR;
158 #if defined(ENABLE_PKCS11_FORCE_DEBUG)
159 pkcs11_flags = PKCS11H_LOG_DEBUG2;
168 void *
const global_data,
170 const char *
const szFormat,
174 char Buffer[10*1024];
178 vsnprintf(Buffer,
sizeof(Buffer), szFormat, args);
179 Buffer[
sizeof(Buffer)-1] = 0;
181 msg(_pkcs11_msg_pkcs112openvpn(flags),
"%s", Buffer);
186 _pkcs11_openvpn_token_prompt(
187 void *
const global_data,
188 void *
const user_data,
189 const pkcs11h_token_id_t token,
202 token_resp.defined =
false;
203 token_resp.nocache =
true;
206 sizeof(token_resp.username),
207 "Please insert %s token",
215 "token-insertion-request",
224 return strcmp(token_resp.password,
"ok") == 0;
230 _pkcs11_openvpn_pin_prompt(
231 void *
const global_data,
232 void *
const user_data,
233 const pkcs11h_token_id_t token,
234 const unsigned retry,
248 snprintf(prompt,
sizeof(prompt),
"%s token", token->label);
250 token_pass.defined =
false;
251 token_pass.nocache =
true;
266 strncpynt(pin, token_pass.password, pin_max);
269 if (strlen(pin) == 0)
282 const bool protected_auth,
283 const int nPINCachePeriod
286 CK_RV rv = CKR_FUNCTION_FAILED;
290 "PKCS#11: pkcs11_initialize - entered"
293 if ((rv = pkcs11h_engine_setSystem(&s_pkcs11h_sys_engine)) != CKR_OK)
295 msg(
M_FATAL,
"PKCS#11: Cannot initialize system engine %ld-'%s'", rv, pkcs11h_getMessage(rv));
299 if ((rv = pkcs11h_initialize()) != CKR_OK)
301 msg(
M_FATAL,
"PKCS#11: Cannot initialize %ld-'%s'", rv, pkcs11h_getMessage(rv));
305 if ((rv = pkcs11h_setLogHook(_pkcs11_openvpn_log, NULL)) != CKR_OK)
307 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
313 if ((rv = pkcs11h_setForkMode(FALSE)) != CKR_OK)
315 msg(
M_FATAL,
"PKCS#11: Cannot set fork mode %ld-'%s'", rv, pkcs11h_getMessage(rv));
319 if ((rv = pkcs11h_setTokenPromptHook(_pkcs11_openvpn_token_prompt, NULL)) != CKR_OK)
321 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
325 if ((rv = pkcs11h_setPINPromptHook(_pkcs11_openvpn_pin_prompt, NULL)) != CKR_OK)
327 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
331 if ((rv = pkcs11h_setProtectedAuthentication(protected_auth)) != CKR_OK)
333 msg(
M_FATAL,
"PKCS#11: Cannot set protected authentication mode %ld-'%s'", rv, pkcs11h_getMessage(rv));
337 if ((rv = pkcs11h_setPINCachePeriod(nPINCachePeriod)) != CKR_OK)
339 msg(
M_FATAL,
"PKCS#11: Cannot set Pcache period %ld-'%s'", rv, pkcs11h_getMessage(rv));
348 "PKCS#11: pkcs11_initialize - return %ld-'%s'",
350 pkcs11h_getMessage(rv)
357 pkcs11_terminate(
void)
361 "PKCS#11: pkcs11_terminate - entered"
368 "PKCS#11: pkcs11_terminate - return"
374 const char *
const provider,
375 const bool protected_auth,
376 const unsigned private_mode,
377 const bool cert_private
386 "PKCS#11: pkcs11_addProvider - entered - provider='%s', private_mode=%08x",
393 "PKCS#11: Adding PKCS#11 provider '%s'",
397 #if PKCS11H_VERSION >= ((1<<16) | (28<<8) | (0<<0))
398 if ((rv = pkcs11h_registerProvider(provider)) != CKR_OK)
400 msg(
M_WARN,
"PKCS#11: Cannot register provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
404 PKCS11H_BOOL allow_protected_auth = protected_auth;
405 PKCS11H_BOOL cert_is_private = cert_private;
407 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOCATION, provider, strlen(provider) + 1);
411 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_ALLOW_PROTECTED_AUTH, &allow_protected_auth,
sizeof(allow_protected_auth));
415 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_MASK_PRIVATE_MODE, &private_mode,
sizeof(private_mode));
419 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE, &cert_is_private,
sizeof(cert_is_private));
421 #if defined(WIN32) && defined(PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS)
424 unsigned loader_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
425 rv = pkcs11h_setProviderProperty(provider, PKCS11H_PROVIDER_PROPERTY_LOADER_FLAGS, &loader_flags,
sizeof(loader_flags));
429 if (rv != CKR_OK || (rv = pkcs11h_initializeProvider(provider)) != CKR_OK)
431 msg(
M_WARN,
"PKCS#11: Cannot initialize provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
432 pkcs11h_removeProvider(provider);
437 (rv = pkcs11h_addProvider(
442 PKCS11H_SLOTEVENT_METHOD_AUTO,
448 msg(
M_WARN,
"PKCS#11: Cannot initialize provider '%s' %ld-'%s'", provider, rv, pkcs11h_getMessage(rv));
454 "PKCS#11: pkcs11_addProvider - return rv=%ld-'%s'",
456 pkcs11h_getMessage(rv)
465 return pkcs11h_logout() == CKR_OK;
469 pkcs11_management_id_count(
void)
471 pkcs11h_certificate_id_list_t id_list = NULL;
472 pkcs11h_certificate_id_list_t t = NULL;
478 "PKCS#11: pkcs11_management_id_count - entered"
482 (rv = pkcs11h_certificate_enumCertificateIds(
483 PKCS11H_ENUM_METHOD_CACHE_EXIST,
485 PKCS11H_PROMPT_MASK_ALLOW_ALL,
491 msg(
M_WARN,
"PKCS#11: Cannot get certificate list %ld-'%s'", rv, pkcs11h_getMessage(rv));
495 for (count = 0, t = id_list; t != NULL; t = t->next)
502 pkcs11h_certificate_freeCertificateIdList(id_list);
507 "PKCS#11: pkcs11_management_id_count - return count=%d",
515 pkcs11_management_id_get(
521 pkcs11h_certificate_id_list_t id_list = NULL;
522 pkcs11h_certificate_id_list_t entry = NULL;
524 pkcs11h_certificate_id_t certificate_id = NULL;
526 pkcs11h_certificate_t certificate = NULL;
528 unsigned char *certificate_blob = NULL;
529 size_t certificate_blob_size = 0;
531 char *internal_id = NULL;
532 char *internal_base64 = NULL;
534 bool success =
false;
541 "PKCS#11: pkcs11_management_id_get - entered index=%d",
549 (rv = pkcs11h_certificate_enumCertificateIds(
550 PKCS11H_ENUM_METHOD_CACHE_EXIST,
552 PKCS11H_PROMPT_MASK_ALLOW_ALL,
558 msg(
M_WARN,
"PKCS#11: Cannot get certificate list %ld-'%s'", rv, pkcs11h_getMessage(rv));
564 while (entry != NULL && count != index)
574 "PKCS#11: pkcs11_management_id_get - no certificate at index=%d",
581 (rv = pkcs11h_certificate_serializeCertificateId(
584 entry->certificate_id
588 msg(
M_WARN,
"PKCS#11: Cannot serialize certificate id %ld-'%s'", rv, pkcs11h_getMessage(rv));
592 if ((internal_id = (
char *)malloc(max)) == NULL)
594 msg(
M_FATAL,
"PKCS#11: Cannot allocate memory");
599 (rv = pkcs11h_certificate_serializeCertificateId(
602 entry->certificate_id
606 msg(
M_WARN,
"PKCS#11: Cannot serialize certificate id %ld-'%s'", rv, pkcs11h_getMessage(rv));
611 (rv = pkcs11h_certificate_create(
612 entry->certificate_id,
614 PKCS11H_PROMPT_MASK_ALLOW_ALL,
615 PKCS11H_PIN_CACHE_INFINITE,
620 msg(
M_WARN,
"PKCS#11: Cannot get certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
625 (rv = pkcs11h_certificate_getCertificateBlob(
628 &certificate_blob_size
632 msg(
M_WARN,
"PKCS#11: Cannot get certificate blob %ld-'%s'", rv, pkcs11h_getMessage(rv));
636 if ((certificate_blob = (
unsigned char *)malloc(certificate_blob_size)) == NULL)
638 msg(
M_FATAL,
"PKCS#11: Cannot allocate memory");
643 (rv = pkcs11h_certificate_getCertificateBlob(
646 &certificate_blob_size
650 msg(
M_WARN,
"PKCS#11: Cannot get certificate blob %ld-'%s'", rv, pkcs11h_getMessage(rv));
656 msg(
M_WARN,
"PKCS#11: Cannot encode certificate");
662 *base64 = internal_base64;
663 internal_base64 = NULL;
668 pkcs11h_certificate_freeCertificateIdList(id_list);
674 free(internal_base64);
675 internal_base64 = NULL;
677 free(certificate_blob);
678 certificate_blob = NULL;
682 "PKCS#11: pkcs11_management_id_get - return success=%d, id='%s'",
694 const char *
const pkcs11_id
697 pkcs11h_certificate_id_t certificate_id = NULL;
698 pkcs11h_certificate_t certificate = NULL;
708 "PKCS#11: tls_ctx_use_pkcs11 - entered - ssl_ctx=%p, pkcs11_id_management=%d, pkcs11_id='%s'",
720 id_resp.defined =
false;
721 id_resp.nocache =
true;
724 sizeof(id_resp.username),
725 "Please specify PKCS#11 id to use"
741 (rv = pkcs11h_certificate_deserializeCertificateId(
747 msg(
M_WARN,
"PKCS#11: Cannot deserialize id %ld-'%s'", rv, pkcs11h_getMessage(rv));
754 (rv = pkcs11h_certificate_deserializeCertificateId(
760 msg(
M_WARN,
"PKCS#11: Cannot deserialize id %ld-'%s'", rv, pkcs11h_getMessage(rv));
766 (rv = pkcs11h_certificate_create(
769 PKCS11H_PROMPT_MASK_ALLOW_ALL,
770 PKCS11H_PIN_CACHE_INFINITE,
775 msg(
M_WARN,
"PKCS#11: Cannot get certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
780 (pkcs11_init_tls_session(
796 if (certificate != NULL)
798 pkcs11h_certificate_freeCertificate(certificate);
802 if (certificate_id != NULL)
804 pkcs11h_certificate_freeCertificateId(certificate_id);
805 certificate_id = NULL;
810 "PKCS#11: tls_ctx_use_pkcs11 - return ok=%d, rv=%ld",
820 _pkcs11_openvpn_show_pkcs11_ids_pin_prompt(
821 void *
const global_data,
822 void *
const user_data,
823 const pkcs11h_token_id_t token,
824 const unsigned retry,
838 buf_printf(&pass_prompt,
"Please enter '%s' token PIN or 'cancel': ", token->display);
840 pin, pin_max,
false))
847 if (!strcmp(pin,
"cancel"))
859 const char *
const provider,
864 pkcs11h_certificate_id_list_t user_certificates = NULL;
865 pkcs11h_certificate_id_list_t current = NULL;
866 CK_RV rv = CKR_FUNCTION_FAILED;
868 if ((rv = pkcs11h_initialize()) != CKR_OK)
870 msg(
M_FATAL,
"PKCS#11: Cannot initialize %ld-'%s'", rv, pkcs11h_getMessage(rv));
874 if ((rv = pkcs11h_setLogHook(_pkcs11_openvpn_log, NULL)) != CKR_OK)
876 msg(
M_FATAL,
"PKCS#11: Cannot set hooks %ld-'%s'", rv, pkcs11h_getMessage(rv));
882 if ((rv = pkcs11h_setProtectedAuthentication(TRUE)) != CKR_OK)
884 msg(
M_FATAL,
"PKCS#11: Cannot set protected authentication %ld-'%s'", rv, pkcs11h_getMessage(rv));
888 if ((rv = pkcs11h_setPINPromptHook(_pkcs11_openvpn_show_pkcs11_ids_pin_prompt, NULL)) != CKR_OK)
890 msg(
M_FATAL,
"PKCS#11: Cannot set PIN hook %ld-'%s'", rv, pkcs11h_getMessage(rv));
894 if (!pkcs11_addProvider(provider, TRUE, 0, cert_private ? TRUE : FALSE))
896 msg(
M_FATAL,
"Failed to add PKCS#11 provider '%s", provider);
901 (rv = pkcs11h_certificate_enumCertificateIds(
902 PKCS11H_ENUM_METHOD_CACHE_EXIST,
904 PKCS11H_PROMPT_MASK_ALLOW_ALL,
910 msg(
M_FATAL,
"PKCS#11: Cannot enumerate certificates %ld-'%s'", rv, pkcs11h_getMessage(rv));
918 "The following objects are available for use.\n"
919 "Each object shown below may be used as parameter to\n"
920 "--pkcs11-id option please remember to use single quote mark.\n"
923 for (current = user_certificates; current != NULL; current = current->next)
925 pkcs11h_certificate_t certificate = NULL;
927 char serial[1024] = {0};
932 (rv = pkcs11h_certificate_serializeCertificateId(
935 current->certificate_id
939 msg(
M_FATAL,
"PKCS#11: Cannot serialize certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
945 && (ser = (
char *)malloc(ser_len)) == NULL
948 msg(
M_FATAL,
"PKCS#11: Cannot allocate memory");
953 (rv = pkcs11h_certificate_serializeCertificateId(
956 current->certificate_id
960 msg(
M_FATAL,
"PKCS#11: Cannot serialize certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
965 (rv = pkcs11h_certificate_create(
966 current->certificate_id,
968 PKCS11H_PROMPT_MASK_ALLOW_ALL,
969 PKCS11H_PIN_CACHE_INFINITE,
974 msg(
M_FATAL,
"PKCS#11: Cannot create certificate %ld-'%s'", rv, pkcs11h_getMessage(rv));
979 (dn = pkcs11_certificate_dn(
989 (pkcs11_certificate_serial(
1006 " Serialized id: %s\n"
1015 if (certificate != NULL)
1017 pkcs11h_certificate_freeCertificate(certificate);
1026 pkcs11h_certificate_freeCertificateIdList(user_certificates);
1027 user_certificates = NULL;
1029 pkcs11h_terminate();