22#include <winpr/library.h>
23#include <winpr/assert.h>
24#include <winpr/spec.h>
25#include <winpr/smartcard.h>
26#include <winpr/asn1.h>
32#include "pkcs11-headers/pkcs11.h"
34#define TAG WINPR_TAG("ncryptp11")
38#define MAX_KEYS_PER_SLOT 64
46 CK_FUNCTION_LIST_PTR p11;
48} NCryptP11ProviderHandle;
54 NCryptP11ProviderHandle* provider;
56 CK_BYTE keyCertId[64];
57 CK_ULONG keyCertIdLen;
65 CK_CHAR keyLabel[256];
73 CK_SLOT_ID slots[MAX_SLOTS];
75 NCryptKeyEnum keys[MAX_KEYS];
84static const piv_cert_tags_t piv_cert_tags[] = {
85 {
"X.509 Certificate for PIV Authentication", { 0x5F, 0xC1, 0x05 } },
86 {
"X.509 Certificate for Digital Signature", { 0x5F, 0xC1, 0x0A } },
87 {
"X.509 Certificate for Key Management", { 0x5F, 0xC1, 0x0B } },
88 {
"X.509 Certificate for Card Authentication", { 0x5F, 0xC1, 0x01 } },
90 {
"Certificate for PIV Authentication", { 0x5F, 0xC1, 0x05 } },
91 {
"Certificate for Digital Signature", { 0x5F, 0xC1, 0x0A } },
92 {
"Certificate for Key Management", { 0x5F, 0xC1, 0x0B } },
93 {
"Certificate for Card Authentication", { 0x5F, 0xC1, 0x01 } },
95 {
"Retired Certificate for Key Management 1", { 0x5F, 0xC1, 0x0D } },
96 {
"Retired Certificate for Key Management 2", { 0x5F, 0xC1, 0x0E } },
97 {
"Retired Certificate for Key Management 3", { 0x5F, 0xC1, 0x0F } },
98 {
"Retired Certificate for Key Management 4", { 0x5F, 0xC1, 0x10 } },
99 {
"Retired Certificate for Key Management 5", { 0x5F, 0xC1, 0x11 } },
100 {
"Retired Certificate for Key Management 6", { 0x5F, 0xC1, 0x12 } },
101 {
"Retired Certificate for Key Management 7", { 0x5F, 0xC1, 0x13 } },
102 {
"Retired Certificate for Key Management 8", { 0x5F, 0xC1, 0x14 } },
103 {
"Retired Certificate for Key Management 9", { 0x5F, 0xC1, 0x15 } },
104 {
"Retired Certificate for Key Management 10", { 0x5F, 0xC1, 0x16 } },
105 {
"Retired Certificate for Key Management 11", { 0x5F, 0xC1, 0x17 } },
106 {
"Retired Certificate for Key Management 12", { 0x5F, 0xC1, 0x18 } },
107 {
"Retired Certificate for Key Management 13", { 0x5F, 0xC1, 0x19 } },
108 {
"Retired Certificate for Key Management 14", { 0x5F, 0xC1, 0x1A } },
109 {
"Retired Certificate for Key Management 15", { 0x5F, 0xC1, 0x1B } },
110 {
"Retired Certificate for Key Management 16", { 0x5F, 0xC1, 0x1C } },
111 {
"Retired Certificate for Key Management 17", { 0x5F, 0xC1, 0x1D } },
112 {
"Retired Certificate for Key Management 18", { 0x5F, 0xC1, 0x1E } },
113 {
"Retired Certificate for Key Management 19", { 0x5F, 0xC1, 0x1F } },
114 {
"Retired Certificate for Key Management 20", { 0x5F, 0xC1, 0x20 } },
117static const BYTE APDU_PIV_SELECT_AID[] = { 0x00, 0xA4, 0x04, 0x00, 0x09, 0xA0, 0x00, 0x00,
118 0x03, 0x08, 0x00, 0x00, 0x10, 0x00, 0x00 };
119static const BYTE APDU_PIV_GET_CHUID[] = { 0x00, 0xCB, 0x3F, 0xFF, 0x05, 0x5C,
120 0x03, 0x5F, 0xC1, 0x02, 0x00 };
121static const BYTE APDU_PIV_GET_MSCMAP[] = { 0x00, 0xCB, 0x3F, 0xFF, 0x05, 0x5C,
122 0x03, 0x5F, 0xFF, 0x10, 0x00 };
123static const BYTE APDU_GET_RESPONSE[] = { 0x00, 0xC0, 0x00, 0x00, 0x00 };
125#define PIV_CONTAINER_NAME_LEN 36
126#define MAX_CONTAINER_NAME_LEN 39
127#define MSCMAP_RECORD_SIZE 107
128#define MSCMAP_SLOT_OFFSET 80
137static const piv_tag_to_slot_t piv_tag_to_slot[] = {
138 { { 0x5F, 0xC1, 0x05 }, 0x9A },
139 { { 0x5F, 0xC1, 0x0A }, 0x9C },
140 { { 0x5F, 0xC1, 0x0B }, 0x9D },
141 { { 0x5F, 0xC1, 0x01 }, 0x9E },
142 { { 0x5F, 0xC1, 0x0D }, 0x82 },
143 { { 0x5F, 0xC1, 0x0E }, 0x83 },
144 { { 0x5F, 0xC1, 0x0F }, 0x84 },
145 { { 0x5F, 0xC1, 0x10 }, 0x85 },
146 { { 0x5F, 0xC1, 0x11 }, 0x86 },
147 { { 0x5F, 0xC1, 0x12 }, 0x87 },
148 { { 0x5F, 0xC1, 0x13 }, 0x88 },
149 { { 0x5F, 0xC1, 0x14 }, 0x89 },
150 { { 0x5F, 0xC1, 0x15 }, 0x8A },
151 { { 0x5F, 0xC1, 0x16 }, 0x8B },
152 { { 0x5F, 0xC1, 0x17 }, 0x8C },
153 { { 0x5F, 0xC1, 0x18 }, 0x8D },
154 { { 0x5F, 0xC1, 0x19 }, 0x8E },
155 { { 0x5F, 0xC1, 0x1A }, 0x8F },
156 { { 0x5F, 0xC1, 0x1B }, 0x90 },
157 { { 0x5F, 0xC1, 0x1C }, 0x91 },
158 { { 0x5F, 0xC1, 0x1D }, 0x92 },
159 { { 0x5F, 0xC1, 0x1E }, 0x93 },
160 { { 0x5F, 0xC1, 0x1F }, 0x94 },
161 { { 0x5F, 0xC1, 0x20 }, 0x95 },
164static CK_OBJECT_CLASS object_class_public_key = CKO_PUBLIC_KEY;
165static CK_BBOOL object_verify = CK_TRUE;
167static CK_ATTRIBUTE public_key_filter[] = { { CKA_CLASS, &object_class_public_key,
168 sizeof(object_class_public_key) },
169 { CKA_VERIFY, &object_verify,
sizeof(object_verify) } };
171static const char* CK_RV_error_string(CK_RV rv);
173static SECURITY_STATUS NCryptP11StorageProvider_dtor(NCRYPT_HANDLE handle)
175 NCryptP11ProviderHandle* provider = (NCryptP11ProviderHandle*)handle;
180 if (provider->p11 && provider->p11->C_Finalize)
181 rv = provider->p11->C_Finalize(
nullptr);
183 WLog_WARN(TAG,
"C_Finalize failed with %s [0x%08lx]", CK_RV_error_string(rv), rv);
185 free(provider->modulePath);
187 if (provider->library)
188 FreeLibrary(provider->library);
191 return winpr_NCryptDefault_dtor(handle);
194static void fix_padded_string(
char* str,
size_t maxlen)
200 char* ptr = &str[maxlen - 1];
202 while ((ptr > str) && (*ptr ==
' '))
209static BOOL attributes_have_unallocated_buffers(CK_ATTRIBUTE_PTR attributes, CK_ULONG count)
211 for (CK_ULONG i = 0; i < count; i++)
213 if (!attributes[i].pValue && (attributes[i].ulValueLen != CK_UNAVAILABLE_INFORMATION))
220static BOOL attribute_allocate_attribute_array(CK_ATTRIBUTE_PTR attribute)
222 WINPR_ASSERT(attribute);
223 attribute->pValue = calloc(attribute->ulValueLen,
sizeof(
void*));
224 return !!attribute->pValue;
227static BOOL attribute_allocate_ulong_array(CK_ATTRIBUTE_PTR attribute)
229 attribute->pValue = calloc(attribute->ulValueLen,
sizeof(CK_ULONG));
230 return !!attribute->pValue;
233static BOOL attribute_allocate_buffer(CK_ATTRIBUTE_PTR attribute)
235 attribute->pValue = calloc(attribute->ulValueLen, 1);
236 return !!attribute->pValue;
239static BOOL attributes_allocate_buffers(CK_ATTRIBUTE_PTR attributes, CK_ULONG count)
243 for (CK_ULONG i = 0; i < count; i++)
245 if (attributes[i].pValue || (attributes[i].ulValueLen == CK_UNAVAILABLE_INFORMATION))
248 switch (attributes[i].type)
250 case CKA_WRAP_TEMPLATE:
251 case CKA_UNWRAP_TEMPLATE:
252 ret &= attribute_allocate_attribute_array(&attributes[i]);
255 case CKA_ALLOWED_MECHANISMS:
256 ret &= attribute_allocate_ulong_array(&attributes[i]);
260 ret &= attribute_allocate_buffer(&attributes[i]);
268static CK_RV object_load_attributes(NCryptP11ProviderHandle* provider, CK_SESSION_HANDLE session,
269 CK_OBJECT_HANDLE
object, CK_ATTRIBUTE_PTR attributes,
272 WINPR_ASSERT(provider);
273 WINPR_ASSERT(provider->p11);
274 WINPR_ASSERT(provider->p11->C_GetAttributeValue);
276 CK_RV rv = provider->p11->C_GetAttributeValue(session,
object, attributes, count);
281 if (!attributes_have_unallocated_buffers(attributes, count))
285 case CKR_ATTRIBUTE_SENSITIVE:
286 case CKR_ATTRIBUTE_TYPE_INVALID:
287 case CKR_BUFFER_TOO_SMALL:
289 if (!attributes_allocate_buffers(attributes, count))
290 return CKR_HOST_MEMORY;
292 rv = provider->p11->C_GetAttributeValue(session,
object, attributes, count);
294 WLog_WARN(TAG,
"C_GetAttributeValue failed with %s [0x%08lx]",
295 CK_RV_error_string(rv), rv);
298 WLog_WARN(TAG,
"C_GetAttributeValue failed with %s [0x%08lx]", CK_RV_error_string(rv),
305 case CKR_ATTRIBUTE_SENSITIVE:
306 case CKR_ATTRIBUTE_TYPE_INVALID:
307 case CKR_BUFFER_TOO_SMALL:
309 "C_GetAttributeValue failed with %s [0x%08lx] even after buffer allocation",
310 CK_RV_error_string(rv), rv);
318static const char* CK_RV_error_string(CK_RV rv)
320 static char generic_buffer[200];
321#define ERR_ENTRY(X) \
328 ERR_ENTRY(CKR_CANCEL);
329 ERR_ENTRY(CKR_HOST_MEMORY);
330 ERR_ENTRY(CKR_SLOT_ID_INVALID);
331 ERR_ENTRY(CKR_GENERAL_ERROR);
332 ERR_ENTRY(CKR_FUNCTION_FAILED);
333 ERR_ENTRY(CKR_ARGUMENTS_BAD);
334 ERR_ENTRY(CKR_NO_EVENT);
335 ERR_ENTRY(CKR_NEED_TO_CREATE_THREADS);
336 ERR_ENTRY(CKR_CANT_LOCK);
337 ERR_ENTRY(CKR_ATTRIBUTE_READ_ONLY);
338 ERR_ENTRY(CKR_ATTRIBUTE_SENSITIVE);
339 ERR_ENTRY(CKR_ATTRIBUTE_TYPE_INVALID);
340 ERR_ENTRY(CKR_ATTRIBUTE_VALUE_INVALID);
341 ERR_ENTRY(CKR_DATA_INVALID);
342 ERR_ENTRY(CKR_DATA_LEN_RANGE);
343 ERR_ENTRY(CKR_DEVICE_ERROR);
344 ERR_ENTRY(CKR_DEVICE_MEMORY);
345 ERR_ENTRY(CKR_DEVICE_REMOVED);
346 ERR_ENTRY(CKR_ENCRYPTED_DATA_INVALID);
347 ERR_ENTRY(CKR_ENCRYPTED_DATA_LEN_RANGE);
348 ERR_ENTRY(CKR_FUNCTION_CANCELED);
349 ERR_ENTRY(CKR_FUNCTION_NOT_PARALLEL);
350 ERR_ENTRY(CKR_FUNCTION_NOT_SUPPORTED);
351 ERR_ENTRY(CKR_KEY_HANDLE_INVALID);
352 ERR_ENTRY(CKR_KEY_SIZE_RANGE);
353 ERR_ENTRY(CKR_KEY_TYPE_INCONSISTENT);
354 ERR_ENTRY(CKR_KEY_NOT_NEEDED);
355 ERR_ENTRY(CKR_KEY_CHANGED);
356 ERR_ENTRY(CKR_KEY_NEEDED);
357 ERR_ENTRY(CKR_KEY_INDIGESTIBLE);
358 ERR_ENTRY(CKR_KEY_FUNCTION_NOT_PERMITTED);
359 ERR_ENTRY(CKR_KEY_NOT_WRAPPABLE);
360 ERR_ENTRY(CKR_KEY_UNEXTRACTABLE);
361 ERR_ENTRY(CKR_MECHANISM_INVALID);
362 ERR_ENTRY(CKR_MECHANISM_PARAM_INVALID);
363 ERR_ENTRY(CKR_OBJECT_HANDLE_INVALID);
364 ERR_ENTRY(CKR_OPERATION_ACTIVE);
365 ERR_ENTRY(CKR_OPERATION_NOT_INITIALIZED);
366 ERR_ENTRY(CKR_PIN_INCORRECT);
367 ERR_ENTRY(CKR_PIN_INVALID);
368 ERR_ENTRY(CKR_PIN_LEN_RANGE);
369 ERR_ENTRY(CKR_PIN_EXPIRED);
370 ERR_ENTRY(CKR_PIN_LOCKED);
371 ERR_ENTRY(CKR_SESSION_CLOSED);
372 ERR_ENTRY(CKR_SESSION_COUNT);
373 ERR_ENTRY(CKR_SESSION_HANDLE_INVALID);
374 ERR_ENTRY(CKR_SESSION_PARALLEL_NOT_SUPPORTED);
375 ERR_ENTRY(CKR_SESSION_READ_ONLY);
376 ERR_ENTRY(CKR_SESSION_EXISTS);
377 ERR_ENTRY(CKR_SESSION_READ_ONLY_EXISTS);
378 ERR_ENTRY(CKR_SESSION_READ_WRITE_SO_EXISTS);
379 ERR_ENTRY(CKR_SIGNATURE_INVALID);
380 ERR_ENTRY(CKR_SIGNATURE_LEN_RANGE);
381 ERR_ENTRY(CKR_TEMPLATE_INCOMPLETE);
382 ERR_ENTRY(CKR_TEMPLATE_INCONSISTENT);
383 ERR_ENTRY(CKR_TOKEN_NOT_PRESENT);
384 ERR_ENTRY(CKR_TOKEN_NOT_RECOGNIZED);
385 ERR_ENTRY(CKR_TOKEN_WRITE_PROTECTED);
386 ERR_ENTRY(CKR_UNWRAPPING_KEY_HANDLE_INVALID);
387 ERR_ENTRY(CKR_UNWRAPPING_KEY_SIZE_RANGE);
388 ERR_ENTRY(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT);
389 ERR_ENTRY(CKR_USER_ALREADY_LOGGED_IN);
390 ERR_ENTRY(CKR_USER_NOT_LOGGED_IN);
391 ERR_ENTRY(CKR_USER_PIN_NOT_INITIALIZED);
392 ERR_ENTRY(CKR_USER_TYPE_INVALID);
393 ERR_ENTRY(CKR_USER_ANOTHER_ALREADY_LOGGED_IN);
394 ERR_ENTRY(CKR_USER_TOO_MANY_TYPES);
395 ERR_ENTRY(CKR_WRAPPED_KEY_INVALID);
396 ERR_ENTRY(CKR_WRAPPED_KEY_LEN_RANGE);
397 ERR_ENTRY(CKR_WRAPPING_KEY_HANDLE_INVALID);
398 ERR_ENTRY(CKR_WRAPPING_KEY_SIZE_RANGE);
399 ERR_ENTRY(CKR_WRAPPING_KEY_TYPE_INCONSISTENT);
400 ERR_ENTRY(CKR_RANDOM_SEED_NOT_SUPPORTED);
401 ERR_ENTRY(CKR_RANDOM_NO_RNG);
402 ERR_ENTRY(CKR_DOMAIN_PARAMS_INVALID);
403 ERR_ENTRY(CKR_BUFFER_TOO_SMALL);
404 ERR_ENTRY(CKR_SAVED_STATE_INVALID);
405 ERR_ENTRY(CKR_INFORMATION_SENSITIVE);
406 ERR_ENTRY(CKR_STATE_UNSAVEABLE);
407 ERR_ENTRY(CKR_CRYPTOKI_NOT_INITIALIZED);
408 ERR_ENTRY(CKR_CRYPTOKI_ALREADY_INITIALIZED);
409 ERR_ENTRY(CKR_MUTEX_BAD);
410 ERR_ENTRY(CKR_MUTEX_NOT_LOCKED);
411 ERR_ENTRY(CKR_FUNCTION_REJECTED);
413 (void)snprintf(generic_buffer,
sizeof(generic_buffer),
"unknown 0x%lx", rv);
414 return generic_buffer;
419#define loge(tag, msg, rv, index, slot) \
420 log_((tag), (msg), (rv), (index), (slot), __FILE__, __func__, __LINE__)
421static void log_(
const char* tag,
const char* msg, CK_RV rv, CK_ULONG index, CK_SLOT_ID slot,
422 const char* file,
const char* fkt,
size_t line)
424 const DWORD log_level = WLOG_ERROR;
425 static wLog* log_cached_ptr =
nullptr;
427 log_cached_ptr = WLog_Get(tag);
428 if (!WLog_IsLevelActive(log_cached_ptr, log_level))
431 WLog_PrintTextMessage(log_cached_ptr, log_level, line, file, fkt,
432 "%s for slot #%lu(%lu), rv=%s", msg, index, slot, CK_RV_error_string(rv));
435static SECURITY_STATUS collect_keys(NCryptP11ProviderHandle* provider, P11EnumKeysState* state)
437 CK_OBJECT_HANDLE slotObjects[MAX_KEYS_PER_SLOT] = WINPR_C_ARRAY_INIT;
439 WINPR_ASSERT(provider);
441 CK_FUNCTION_LIST_PTR p11 = provider->p11;
444 WLog_DBG(TAG,
"checking %lx slots for valid keys...", state->nslots);
446 for (CK_ULONG i = 0; i < state->nslots; i++)
448 CK_SESSION_HANDLE session = 0;
452 WINPR_ASSERT(p11->C_GetSlotInfo);
453 CK_RV rv = p11->C_GetSlotInfo(state->slots[i], &slotInfo);
456 loge(TAG,
"unable to retrieve information", rv, i, state->slots[i]);
460 fix_padded_string((
char*)slotInfo.slotDescription,
sizeof(slotInfo.slotDescription));
461 WLog_DBG(TAG,
"collecting keys for slot #%lx(%lu) descr='%s' flags=0x%lx", i,
462 state->slots[i], slotInfo.slotDescription, slotInfo.flags);
466 if (!(slotInfo.flags & CKF_TOKEN_PRESENT))
468 WLog_INFO(TAG,
"token not present for slot #%lu(%lu)", i, state->slots[i]);
472 WINPR_ASSERT(p11->C_GetTokenInfo);
473 rv = p11->C_GetTokenInfo(state->slots[i], &tokenInfo);
475 loge(TAG,
"unable to retrieve token info", rv, i, state->slots[i]);
478 fix_padded_string((
char*)tokenInfo.label,
sizeof(tokenInfo.label));
479 WLog_DBG(TAG,
"token, label='%s' flags=0x%lx", tokenInfo.label, tokenInfo.flags);
482 WINPR_ASSERT(p11->C_OpenSession);
483 rv = p11->C_OpenSession(state->slots[i], CKF_SERIAL_SESSION,
nullptr,
nullptr, &session);
486 WLog_ERR(TAG,
"unable to openSession for slot #%lu(%lu), session=%p rv=%s", i,
487 state->slots[i], WINPR_CXX_COMPAT_CAST(
const void*, session),
488 CK_RV_error_string(rv));
492 WINPR_ASSERT(p11->C_FindObjectsInit);
493 rv = p11->C_FindObjectsInit(session, public_key_filter, ARRAYSIZE(public_key_filter));
497 loge(TAG,
"unable to initiate search", rv, i, state->slots[i]);
498 goto cleanup_FindObjectsInit;
502 CK_ULONG nslotObjects = 0;
503 WINPR_ASSERT(p11->C_FindObjects);
505 p11->C_FindObjects(session, &slotObjects[0], ARRAYSIZE(slotObjects), &nslotObjects);
508 loge(TAG,
"unable to findObjects", rv, i, state->slots[i]);
509 goto cleanup_FindObjects;
512 WLog_DBG(TAG,
"slot has %lu objects", nslotObjects);
513 for (CK_ULONG j = 0; j < nslotObjects; j++)
515 NCryptKeyEnum* key = &state->keys[state->nKeys];
516 CK_OBJECT_CLASS dataClass = CKO_PUBLIC_KEY;
518 { CKA_ID, &key->id,
sizeof(key->id) },
519 { CKA_CLASS, &dataClass,
sizeof(dataClass) },
520 { CKA_LABEL, &key->keyLabel,
sizeof(key->keyLabel) },
521 { CKA_KEY_TYPE, &key->keyType,
sizeof(key->keyType) }
524 rv = object_load_attributes(provider, session, slotObjects[j], key_or_certAttrs,
525 ARRAYSIZE(key_or_certAttrs));
528 WLog_ERR(TAG,
"error getting attributes, rv=%s", CK_RV_error_string(rv));
532 key->idLen = key_or_certAttrs[0].ulValueLen;
533 key->slotId = state->slots[i];
534 key->slotInfo = slotInfo;
540 WINPR_ASSERT(p11->C_FindObjectsFinal);
541 rv = p11->C_FindObjectsFinal(session);
543 loge(TAG,
"error during C_FindObjectsFinal", rv, i, state->slots[i]);
544 cleanup_FindObjectsInit:
545 WINPR_ASSERT(p11->C_CloseSession);
546 rv = p11->C_CloseSession(session);
548 loge(TAG,
"error closing session", rv, i, state->slots[i]);
551 return ERROR_SUCCESS;
554static BOOL convertKeyType(CK_KEY_TYPE k, LPWSTR dest, DWORD len, DWORD* outlen)
556 const WCHAR* r =
nullptr;
559#define ALGO_CASE(V, S) \
562 retLen = _wcsnlen((S), ARRAYSIZE((S))); \
566 ALGO_CASE(CKK_RSA, BCRYPT_RSA_ALGORITHM);
567 ALGO_CASE(CKK_DSA, BCRYPT_DSA_ALGORITHM);
568 ALGO_CASE(CKK_DH, BCRYPT_DH_ALGORITHM);
569 ALGO_CASE(CKK_EC, BCRYPT_ECDSA_ALGORITHM);
570 ALGO_CASE(CKK_RC2, BCRYPT_RC2_ALGORITHM);
571 ALGO_CASE(CKK_RC4, BCRYPT_RC4_ALGORITHM);
572 ALGO_CASE(CKK_DES, BCRYPT_DES_ALGORITHM);
573 ALGO_CASE(CKK_DES3, BCRYPT_3DES_ALGORITHM);
577 case CKK_GENERIC_SECRET:
595 if (retLen > UINT32_MAX)
599 *outlen = (UINT32)retLen;
610 if (retLen + 1 > len)
612 WLog_ERR(TAG,
"target buffer is too small for algo name");
616 memcpy(dest, r,
sizeof(WCHAR) * retLen);
623static void wprintKeyName(LPWSTR str, CK_SLOT_ID slotId, CK_BYTE*
id, CK_ULONG idLen)
625 char asciiName[128] = WINPR_C_ARRAY_INIT;
626 char* ptr = asciiName;
627 const CK_BYTE* bytePtr =
nullptr;
632 bytePtr = ((CK_BYTE*)&slotId);
633 for (CK_ULONG i = 0; i <
sizeof(slotId); i++, bytePtr++, ptr += 2)
634 (
void)snprintf(ptr, 3,
"%.2x", *bytePtr);
639 for (CK_ULONG i = 0; i < idLen; i++,
id++, ptr += 2)
640 (
void)snprintf(ptr, 3,
"%.2x", *
id);
642 (void)ConvertUtf8NToWChar(asciiName, ARRAYSIZE(asciiName), str,
643 strnlen(asciiName, ARRAYSIZE(asciiName)) + 1);
646static size_t parseHex(
const char* str,
const char* end, CK_BYTE* target)
650 for (; str != end && *str; str++, ret++, target++)
653 if (*str <=
'9' && *str >=
'0')
657 else if (*str <=
'f' && *str >=
'a')
659 v = (10 + *str -
'a');
661 else if (*str <=
'F' && *str >=
'A')
663 v |= (10 + *str -
'A');
672 if (!*str || str == end)
675 if (*str <=
'9' && *str >=
'0')
679 else if (*str <=
'f' && *str >=
'a')
681 v |= (10 + *str -
'a');
683 else if (*str <=
'F' && *str >=
'A')
685 v |= (10 + *str -
'A');
697static SECURITY_STATUS parseKeyName(LPCWSTR pszKeyName, CK_SLOT_ID* slotId, CK_BYTE*
id,
700 char asciiKeyName[128] = WINPR_C_ARRAY_INIT;
703 if (ConvertWCharToUtf8(pszKeyName, asciiKeyName, ARRAYSIZE(asciiKeyName)) < 0)
706 if (*asciiKeyName !=
'\\')
709 pos = strchr(&asciiKeyName[1],
'\\');
713 if ((
size_t)(pos - &asciiKeyName[1]) >
sizeof(CK_SLOT_ID) * 2ull)
716 *slotId = (CK_SLOT_ID)0;
717 if (parseHex(&asciiKeyName[1], pos, (CK_BYTE*)slotId) !=
sizeof(CK_SLOT_ID))
720 *idLen = parseHex(pos + 1,
nullptr,
id);
724 return ERROR_SUCCESS;
727static SECURITY_STATUS NCryptP11EnumKeys(NCRYPT_PROV_HANDLE hProvider, LPCWSTR pszScope,
729 WINPR_ATTR_UNUSED DWORD dwFlags)
731 NCryptP11ProviderHandle* provider = (NCryptP11ProviderHandle*)hProvider;
732 P11EnumKeysState* state = (P11EnumKeysState*)*ppEnumState;
733 CK_RV rv = WINPR_C_ARRAY_INIT;
734 CK_SLOT_ID currentSlot = WINPR_C_ARRAY_INIT;
735 CK_SESSION_HANDLE currentSession = 0;
736 char slotFilterBuffer[65] = WINPR_C_ARRAY_INIT;
737 char* slotFilter =
nullptr;
738 size_t slotFilterLen = 0;
740 SECURITY_STATUS ret = checkNCryptHandle((NCRYPT_HANDLE)hProvider, WINPR_NCRYPT_PROVIDER);
741 if (ret != ERROR_SUCCESS)
750 char asciiScope[128 + 6 + 1] = WINPR_C_ARRAY_INIT;
751 size_t asciiScopeLen = 0;
753 if (ConvertWCharToUtf8(pszScope, asciiScope, ARRAYSIZE(asciiScope) - 1) < 0)
755 WLog_WARN(TAG,
"Invalid scope");
756 return NTE_INVALID_PARAMETER;
759 if (strstr(asciiScope,
"\\\\.\\") != asciiScope)
761 WLog_WARN(TAG,
"Invalid scope '%s'", asciiScope);
762 return NTE_INVALID_PARAMETER;
765 asciiScopeLen = strnlen(asciiScope, ARRAYSIZE(asciiScope));
766 if ((asciiScopeLen < 1) || (asciiScope[asciiScopeLen - 1] !=
'\\'))
768 WLog_WARN(TAG,
"Invalid scope '%s'", asciiScope);
769 return NTE_INVALID_PARAMETER;
772 asciiScope[asciiScopeLen - 1] = 0;
774 strncpy(slotFilterBuffer, &asciiScope[4],
sizeof(slotFilterBuffer));
775 slotFilter = slotFilterBuffer;
776 slotFilterLen = asciiScopeLen - 5;
781 state = (P11EnumKeysState*)calloc(1,
sizeof(*state));
783 return NTE_NO_MEMORY;
785 WINPR_ASSERT(provider->p11->C_GetSlotList);
786 rv = provider->p11->C_GetSlotList(CK_TRUE,
nullptr, &state->nslots);
791 WLog_WARN(TAG,
"C_GetSlotList failed with %s [0x%08lx]", CK_RV_error_string(rv), rv);
795 if (state->nslots > MAX_SLOTS)
796 state->nslots = MAX_SLOTS;
798 rv = provider->p11->C_GetSlotList(CK_TRUE, state->slots, &state->nslots);
803 WLog_WARN(TAG,
"C_GetSlotList failed with %s [0x%08lx]", CK_RV_error_string(rv), rv);
807 ret = collect_keys(provider, state);
808 if (ret != ERROR_SUCCESS)
814 *ppEnumState = state;
817 for (; state->keyIndex < state->nKeys; state->keyIndex++)
820 NCryptKeyEnum* key = &state->keys[state->keyIndex];
821 CK_OBJECT_CLASS oclass = CKO_CERTIFICATE;
822 CK_CERTIFICATE_TYPE ctype = CKC_X_509;
823 CK_ATTRIBUTE certificateFilter[] = { { CKA_CLASS, &oclass,
sizeof(oclass) },
824 { CKA_CERTIFICATE_TYPE, &ctype,
sizeof(ctype) },
825 { CKA_ID, key->id, key->idLen } };
826 CK_ULONG ncertObjects = 0;
827 CK_OBJECT_HANDLE certObject = 0;
830 if (slotFilter && memcmp(key->slotInfo.slotDescription, slotFilter, slotFilterLen) != 0)
833 if (!currentSession || (currentSlot != key->slotId))
839 WINPR_ASSERT(provider->p11->C_CloseSession);
840 rv = provider->p11->C_CloseSession(currentSession);
842 WLog_WARN(TAG,
"C_CloseSession failed with %s [0x%08lx]",
843 CK_RV_error_string(rv), rv);
847 WINPR_ASSERT(provider->p11->C_OpenSession);
848 rv = provider->p11->C_OpenSession(key->slotId, CKF_SERIAL_SESSION,
nullptr,
nullptr,
852 WLog_ERR(TAG,
"C_OpenSession failed with %s [0x%08lx] for slot %lu",
853 CK_RV_error_string(rv), rv, key->slotId);
856 currentSlot = key->slotId;
860 WINPR_ASSERT(provider->p11->C_FindObjectsInit);
861 rv = provider->p11->C_FindObjectsInit(currentSession, certificateFilter,
862 ARRAYSIZE(certificateFilter));
865 WLog_ERR(TAG,
"C_FindObjectsInit failed with %s [0x%08lx] for slot %lu",
866 CK_RV_error_string(rv), rv, key->slotId);
870 WINPR_ASSERT(provider->p11->C_FindObjects);
871 rv = provider->p11->C_FindObjects(currentSession, &certObject, 1, &ncertObjects);
874 WLog_ERR(TAG,
"C_FindObjects failed with %s [0x%08lx] for slot %lu",
875 CK_RV_error_string(rv), rv, currentSlot);
876 goto cleanup_FindObjects;
883 size_t KEYNAME_SZ = (1ull + (
sizeof(key->slotId) * 2ull) + 1ull +
884 (key->idLen * 2ull) + 1ull) *
887 convertKeyType(key->keyType,
nullptr, 0, &algoSz);
888 KEYNAME_SZ += (1ULL + algoSz) *
sizeof(WCHAR);
890 keyName = calloc(1,
sizeof(*keyName) + KEYNAME_SZ);
893 WLog_ERR(TAG,
"unable to allocate keyName");
894 goto cleanup_FindObjects;
896 keyName->dwLegacyKeySpec = AT_KEYEXCHANGE | AT_SIGNATURE;
897 keyName->dwFlags = NCRYPT_MACHINE_KEY_FLAG;
898 keyName->pszName = (LPWSTR)(keyName + 1);
899 wprintKeyName(keyName->pszName, key->slotId, key->id, key->idLen);
901 keyName->pszAlgid = keyName->pszName + _wcslen(keyName->pszName) + 1;
902 convertKeyType(key->keyType, keyName->pszAlgid, algoSz + 1,
nullptr);
906 WINPR_ASSERT(provider->p11->C_FindObjectsFinal);
907 rv = provider->p11->C_FindObjectsFinal(currentSession);
909 WLog_ERR(TAG,
"C_FindObjectsFinal failed with %s [0x%08lx]", CK_RV_error_string(rv),
914 *ppKeyName = keyName;
916 return ERROR_SUCCESS;
920 return NTE_NO_MORE_ITEMS;
923static BOOL piv_check_sw(DWORD buf_len,
const BYTE* buf,
size_t bufsize, BYTE expected_sw1)
925 return (buf_len >= 2) && (buf_len <= bufsize) && (buf[buf_len - 2] == expected_sw1);
928static BOOL piv_check_sw_success(DWORD buf_len,
const BYTE* buf,
size_t bufsize)
930 return (buf_len >= 2) && (buf_len <= bufsize) && (buf[buf_len - 2] == 0x90) &&
931 (buf[buf_len - 1] == 0x00);
934static SECURITY_STATUS get_piv_container_name_from_mscmap(SCARDHANDLE card,
936 const BYTE* piv_tag, BYTE* output,
939 BYTE buf[258] = WINPR_C_ARRAY_INIT;
940 BYTE mscmap_buf[2148] = WINPR_C_ARRAY_INIT;
941 DWORD buf_len =
sizeof(buf);
942 DWORD mscmap_total = 0;
944 if (SCardTransmit(card, pci, APDU_PIV_GET_MSCMAP,
sizeof(APDU_PIV_GET_MSCMAP),
nullptr, buf,
945 &buf_len) != SCARD_S_SUCCESS)
946 return NTE_NOT_FOUND;
948 if (piv_check_sw_success(buf_len, buf,
sizeof(buf)))
950 mscmap_total = buf_len - 2;
951 if (mscmap_total >
sizeof(mscmap_buf))
952 return NTE_NOT_FOUND;
953 memcpy(mscmap_buf, buf, mscmap_total);
955 else if (piv_check_sw(buf_len, buf,
sizeof(buf), 0x61))
957 mscmap_total = buf_len - 2;
958 if (mscmap_total <=
sizeof(mscmap_buf))
959 memcpy(mscmap_buf, buf, mscmap_total);
961 while (piv_check_sw(buf_len, buf,
sizeof(buf), 0x61) && mscmap_total <
sizeof(mscmap_buf))
963 BYTE get_resp[5] = { 0x00, 0xC0, 0x00, 0x00, buf[buf_len - 1] };
964 buf_len =
sizeof(buf);
966 const SECURITY_STATUS status =
967 SCardTransmit(card, pci, get_resp,
sizeof(get_resp),
nullptr, buf, &buf_len);
968 if (status != SCARD_S_SUCCESS)
969 return NTE_NOT_FOUND;
972 if (piv_check_sw_success(buf_len, buf,
sizeof(buf)) ||
973 piv_check_sw(buf_len, buf,
sizeof(buf), 0x61))
975 if (chunk == 0 || mscmap_total + chunk >
sizeof(mscmap_buf))
977 memcpy(mscmap_buf + mscmap_total, buf, chunk);
978 mscmap_total += chunk;
980 if (!piv_check_sw_success(buf_len, buf,
sizeof(buf)))
981 return NTE_NOT_FOUND;
984 return NTE_NOT_FOUND;
987 const BYTE* mscmap_data = mscmap_buf;
988 DWORD mscmap_data_len = mscmap_total;
990 for (
int tlv_pass = 0; tlv_pass < 2; tlv_pass++)
992 if (mscmap_data_len < 2)
994 BYTE tlv_tag = mscmap_data[0];
995 if (tlv_tag != 0x53 && tlv_tag != 0x81)
998 if (mscmap_data[1] == 0x82 && mscmap_data_len > 4)
1000 else if (mscmap_data[1] == 0x81 && mscmap_data_len > 3)
1003 mscmap_data_len -= (DWORD)hdr;
1007 BYTE target_slot = 0;
1008 for (
size_t i = 0; i < ARRAYSIZE(piv_tag_to_slot); i++)
1010 if (memcmp(piv_tag, piv_tag_to_slot[i].tag, 3) == 0)
1012 target_slot = piv_tag_to_slot[i].slot;
1016 if (target_slot == 0)
1017 return NTE_NOT_FOUND;
1020 size_t num_records = mscmap_data_len / MSCMAP_RECORD_SIZE;
1021 for (
size_t i = 0; i < num_records; i++)
1023 const BYTE* record = mscmap_data + (i * MSCMAP_RECORD_SIZE);
1024 if (record[MSCMAP_SLOT_OFFSET] == target_slot)
1026 size_t copy_len = (MAX_CONTAINER_NAME_LEN + 1) *
sizeof(WCHAR);
1027 if (copy_len > output_len)
1028 copy_len = output_len;
1029 memcpy(output, record, copy_len);
1030 return ERROR_SUCCESS;
1033 return NTE_NOT_FOUND;
1036static SECURITY_STATUS get_piv_container_name_from_chuid(SCARDHANDLE card,
1038 const BYTE* piv_tag, BYTE* output,
1041 BYTE buf[258] = WINPR_C_ARRAY_INIT;
1042 DWORD buf_len =
sizeof(buf);
1043 char container_name[PIV_CONTAINER_NAME_LEN + 1] = WINPR_C_ARRAY_INIT;
1045 if (SCardTransmit(card, pci, APDU_PIV_GET_CHUID,
sizeof(APDU_PIV_GET_CHUID),
nullptr, buf,
1046 &buf_len) != SCARD_S_SUCCESS)
1048 if (!piv_check_sw_success(buf_len, buf,
sizeof(buf)) &&
1049 !piv_check_sw(buf_len, buf,
sizeof(buf), 0x61))
1057 WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_BER, buf, buf_len);
1058 if (!WinPrAsn1DecReadTagAndLen(&dec, &tag, &len) || tag != 0x53)
1060 while (WinPrAsn1DecReadTagLenValue(&dec, &tag, &len, &dec2) && tag != 0x34)
1062 if (tag != 0x34 || len != 16)
1065 wStream s = WinPrAsn1DecGetStream(&dec2);
1066 BYTE* p = Stream_Buffer(&s);
1068 (void)snprintf(container_name, PIV_CONTAINER_NAME_LEN + 1,
1069 "%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x", p[3],
1070 p[2], p[1], p[0], p[5], p[4], p[7], p[6], p[8], p[9], p[10], p[11], p[12],
1071 piv_tag[0], piv_tag[1], piv_tag[2]);
1079 if (ConvertUtf8NToWChar(container_name, ARRAYSIZE(container_name), cnv.wc,
1080 output_len /
sizeof(WCHAR)) > 0)
1081 return ERROR_SUCCESS;
1085static SECURITY_STATUS get_piv_container_name(NCryptP11KeyHandle* key,
const BYTE* piv_tag,
1086 BYTE* output,
size_t output_len)
1089 CK_FUNCTION_LIST_PTR p11 =
nullptr;
1090 WCHAR* reader =
nullptr;
1091 SCARDCONTEXT context = 0;
1092 SCARDHANDLE card = 0;
1095 BYTE buf[258] = WINPR_C_ARRAY_INIT;
1097 SECURITY_STATUS ret = NTE_BAD_KEY;
1100 WINPR_ASSERT(piv_tag);
1102 WINPR_ASSERT(key->provider);
1103 p11 = key->provider->p11;
1106 WINPR_ASSERT(p11->C_GetSlotInfo);
1107 if (p11->C_GetSlotInfo(key->slotId, &slot_info) != CKR_OK)
1110 fix_padded_string((
char*)slot_info.slotDescription,
sizeof(slot_info.slotDescription));
1111 reader = ConvertUtf8NToWCharAlloc((
char*)slot_info.slotDescription,
1112 ARRAYSIZE(slot_info.slotDescription),
nullptr);
1113 ret = NTE_NO_MEMORY;
1118 if (SCardEstablishContext(SCARD_SCOPE_USER,
nullptr,
nullptr, &context) != SCARD_S_SUCCESS)
1121 if (SCardConnectW(context, reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_Tx, &card, &proto) !=
1124 pci = (proto == SCARD_PROTOCOL_T0) ? SCARD_PCI_T0 : SCARD_PCI_T1;
1126 buf_len =
sizeof(buf);
1127 if (SCardTransmit(card, pci, APDU_PIV_SELECT_AID,
sizeof(APDU_PIV_SELECT_AID),
nullptr, buf,
1128 &buf_len) != SCARD_S_SUCCESS)
1130 if (!piv_check_sw_success(buf_len, buf,
sizeof(buf)) &&
1131 !piv_check_sw(buf_len, buf,
sizeof(buf), 0x61))
1135 ret = get_piv_container_name_from_mscmap(card, pci, piv_tag, output, output_len);
1136 if (ret != ERROR_SUCCESS)
1137 ret = get_piv_container_name_from_chuid(card, pci, piv_tag, output, output_len);
1142 SCardDisconnect(card, SCARD_LEAVE_CARD);
1144 SCardReleaseContext(context);
1148static SECURITY_STATUS check_for_piv_container_name(NCryptP11KeyHandle* key, BYTE* pbOutput,
1149 DWORD cbOutput, DWORD* pcbResult,
char* label,
1152 for (
size_t i = 0; i < ARRAYSIZE(piv_cert_tags); i++)
1154 const piv_cert_tags_t* cur = &piv_cert_tags[i];
1155 if (strncmp(label, cur->label, label_len) == 0)
1157 *pcbResult = (MAX_CONTAINER_NAME_LEN + 1) *
sizeof(WCHAR);
1159 return ERROR_SUCCESS;
1160 else if (cbOutput < (MAX_CONTAINER_NAME_LEN + 1) *
sizeof(WCHAR))
1161 return NTE_NO_MEMORY;
1163 return get_piv_container_name(key, cur->tag, pbOutput, cbOutput);
1166 return NTE_NOT_FOUND;
1169static SECURITY_STATUS NCryptP11KeyGetProperties(NCryptP11KeyHandle* keyHandle,
1170 NCryptKeyGetPropertyEnum property, PBYTE pbOutput,
1171 DWORD cbOutput, DWORD* pcbResult,
1172 WINPR_ATTR_UNUSED DWORD dwFlags)
1174 SECURITY_STATUS ret = NTE_FAIL;
1176 CK_SESSION_HANDLE session = 0;
1177 CK_OBJECT_HANDLE objectHandle = 0;
1178 CK_ULONG objectCount = 0;
1179 NCryptP11ProviderHandle* provider =
nullptr;
1180 CK_OBJECT_CLASS oclass = CKO_CERTIFICATE;
1181 CK_CERTIFICATE_TYPE ctype = CKC_X_509;
1182 CK_ATTRIBUTE certificateFilter[] = { { CKA_CLASS, &oclass,
sizeof(oclass) },
1183 { CKA_CERTIFICATE_TYPE, &ctype,
sizeof(ctype) },
1184 { CKA_ID, keyHandle->keyCertId,
1185 keyHandle->keyCertIdLen } };
1187 CK_ULONG objectFilterLen = ARRAYSIZE(certificateFilter);
1189 WINPR_ASSERT(keyHandle);
1190 provider = keyHandle->provider;
1191 WINPR_ASSERT(provider);
1196 case NCRYPT_PROPERTY_CERTIFICATE:
1197 case NCRYPT_PROPERTY_NAME:
1199 case NCRYPT_PROPERTY_READER:
1203 WINPR_ASSERT(provider->p11->C_GetSlotInfo);
1204 rv = provider->p11->C_GetSlotInfo(keyHandle->slotId, &slotInfo);
1208#define SLOT_DESC_SZ sizeof(slotInfo.slotDescription)
1209 fix_padded_string((
char*)slotInfo.slotDescription, SLOT_DESC_SZ);
1210 const size_t len = 2ULL * (strnlen((
char*)slotInfo.slotDescription, SLOT_DESC_SZ) + 1);
1211 if (len > UINT32_MAX)
1212 return NTE_BAD_DATA;
1213 *pcbResult = (UINT32)len;
1222 if (cbOutput < *pcbResult)
1223 return NTE_NO_MEMORY;
1225 if (ConvertUtf8NToWChar((
char*)slotInfo.slotDescription, SLOT_DESC_SZ, cnv.wc,
1226 cbOutput /
sizeof(WCHAR)) < 0)
1227 return NTE_NO_MEMORY;
1229 return ERROR_SUCCESS;
1231 case NCRYPT_PROPERTY_SLOTID:
1236 UINT32* ptr = (UINT32*)pbOutput;
1239 return NTE_NO_MEMORY;
1240 if (keyHandle->slotId > UINT32_MAX)
1245 *ptr = (UINT32)keyHandle->slotId;
1247 return ERROR_SUCCESS;
1249 case NCRYPT_PROPERTY_UNKNOWN:
1251 return NTE_NOT_SUPPORTED;
1254 WINPR_ASSERT(provider->p11->C_OpenSession);
1255 rv = provider->p11->C_OpenSession(keyHandle->slotId, CKF_SERIAL_SESSION,
nullptr,
nullptr,
1259 WLog_ERR(TAG,
"error opening session on slot %lu", keyHandle->slotId);
1263 WINPR_ASSERT(provider->p11->C_FindObjectsInit);
1264 rv = provider->p11->C_FindObjectsInit(session, objectFilter, objectFilterLen);
1267 WLog_ERR(TAG,
"unable to initiate search for slot %lu", keyHandle->slotId);
1271 WINPR_ASSERT(provider->p11->C_FindObjects);
1272 rv = provider->p11->C_FindObjects(session, &objectHandle, 1, &objectCount);
1275 WLog_ERR(TAG,
"unable to findObjects for slot %lu", keyHandle->slotId);
1280 ret = NTE_NOT_FOUND;
1286 case NCRYPT_PROPERTY_CERTIFICATE:
1288 CK_ATTRIBUTE certValue = { CKA_VALUE, pbOutput, cbOutput };
1290 WINPR_ASSERT(provider->p11->C_GetAttributeValue);
1291 rv = provider->p11->C_GetAttributeValue(session, objectHandle, &certValue, 1);
1297 if (certValue.ulValueLen > UINT32_MAX)
1302 *pcbResult = (UINT32)certValue.ulValueLen;
1303 ret = ERROR_SUCCESS;
1306 case NCRYPT_PROPERTY_NAME:
1309 char* label =
nullptr;
1311 WINPR_ASSERT(provider->p11->C_GetAttributeValue);
1312 rv = provider->p11->C_GetAttributeValue(session, objectHandle, &attr, 1);
1315 label = calloc(1, attr.ulValueLen);
1318 ret = NTE_NO_MEMORY;
1322 attr.pValue = label;
1323 rv = provider->p11->C_GetAttributeValue(session, objectHandle, &attr, 1);
1329 ret = check_for_piv_container_name(keyHandle, pbOutput, cbOutput, pcbResult, label,
1333 if (ret == NTE_NOT_FOUND)
1340 const size_t olen = pbOutput ? cbOutput /
sizeof(WCHAR) : 0;
1342 SSIZE_T size = ConvertUtf8NToWChar(label, attr.ulValueLen, cnv.wc, olen);
1344 ret = ERROR_CONVERT_TO_LARGE;
1347 *pcbResult = (UINT32)size *
sizeof(WCHAR);
1348 ret = ERROR_SUCCESS;
1357 ret = NTE_NOT_SUPPORTED;
1362 WINPR_ASSERT(provider->p11->C_FindObjectsFinal);
1363 rv = provider->p11->C_FindObjectsFinal(session);
1366 WLog_ERR(TAG,
"error in C_FindObjectsFinal() for slot %lu", keyHandle->slotId);
1369 WINPR_ASSERT(provider->p11->C_CloseSession);
1370 rv = provider->p11->C_CloseSession(session);
1373 WLog_ERR(TAG,
"error in C_CloseSession() for slot %lu", keyHandle->slotId);
1378static SECURITY_STATUS NCryptP11GetProperty(NCRYPT_HANDLE hObject, NCryptKeyGetPropertyEnum prop,
1379 PBYTE pbOutput, DWORD cbOutput, DWORD* pcbResult,
1387 case WINPR_NCRYPT_PROVIDER:
1388 return ERROR_CALL_NOT_IMPLEMENTED;
1389 case WINPR_NCRYPT_KEY:
1390 return NCryptP11KeyGetProperties((NCryptP11KeyHandle*)hObject, prop, pbOutput, cbOutput,
1391 pcbResult, dwFlags);
1393 return ERROR_INVALID_HANDLE;
1395 return ERROR_SUCCESS;
1398static SECURITY_STATUS NCryptP11OpenKey(NCRYPT_PROV_HANDLE hProvider, NCRYPT_KEY_HANDLE* phKey,
1399 LPCWSTR pszKeyName, WINPR_ATTR_UNUSED DWORD dwLegacyKeySpec,
1400 WINPR_ATTR_UNUSED DWORD dwFlags)
1402 SECURITY_STATUS ret = 0;
1403 CK_SLOT_ID slotId = 0;
1404 CK_BYTE keyCertId[64] = WINPR_C_ARRAY_INIT;
1405 CK_ULONG keyCertIdLen = 0;
1406 NCryptP11KeyHandle* keyHandle =
nullptr;
1408 ret = parseKeyName(pszKeyName, &slotId, keyCertId, &keyCertIdLen);
1409 if (ret != ERROR_SUCCESS)
1412 keyHandle = (NCryptP11KeyHandle*)ncrypt_new_handle(
1413 WINPR_NCRYPT_KEY,
sizeof(*keyHandle), NCryptP11GetProperty, winpr_NCryptDefault_dtor);
1415 return NTE_NO_MEMORY;
1417 keyHandle->provider = (NCryptP11ProviderHandle*)hProvider;
1418 keyHandle->slotId = slotId;
1419 memcpy(keyHandle->keyCertId, keyCertId,
sizeof(keyCertId));
1420 keyHandle->keyCertIdLen = keyCertIdLen;
1421 *phKey = (NCRYPT_KEY_HANDLE)keyHandle;
1422 return ERROR_SUCCESS;
1425static SECURITY_STATUS initialize_pkcs11(HANDLE handle,
1426 CK_RV (*c_get_function_list)(CK_FUNCTION_LIST_PTR_PTR),
1427 NCRYPT_PROV_HANDLE* phProvider)
1429 SECURITY_STATUS status = ERROR_SUCCESS;
1430 NCryptP11ProviderHandle* ret =
nullptr;
1433 WINPR_ASSERT(c_get_function_list);
1434 WINPR_ASSERT(phProvider);
1436 ret = (NCryptP11ProviderHandle*)ncrypt_new_handle(
1437 WINPR_NCRYPT_PROVIDER,
sizeof(*ret), NCryptP11GetProperty, NCryptP11StorageProvider_dtor);
1439 return NTE_NO_MEMORY;
1441 ret->library = handle;
1442 ret->baseProvider.enumKeysFn = NCryptP11EnumKeys;
1443 ret->baseProvider.openKeyFn = NCryptP11OpenKey;
1445 rv = c_get_function_list(&ret->p11);
1448 status = NTE_PROVIDER_DLL_FAIL;
1452 WINPR_ASSERT(ret->p11);
1453 WINPR_ASSERT(ret->p11->C_Initialize);
1454 rv = ret->p11->C_Initialize(
nullptr);
1457 status = NTE_PROVIDER_DLL_FAIL;
1461 *phProvider = (NCRYPT_PROV_HANDLE)ret;
1464 if (status != ERROR_SUCCESS)
1465 ret->baseProvider.baseHandle.releaseFn((NCRYPT_HANDLE)ret);
1469SECURITY_STATUS NCryptOpenP11StorageProviderEx(NCRYPT_PROV_HANDLE* phProvider,
1470 WINPR_ATTR_UNUSED LPCWSTR pszProviderName,
1471 WINPR_ATTR_UNUSED DWORD dwFlags, LPCSTR* modulePaths)
1473 SECURITY_STATUS status = ERROR_INVALID_PARAMETER;
1474 LPCSTR defaultPaths[] = {
"p11-kit-proxy.so",
"opensc-pkcs11.so",
nullptr };
1477 return ERROR_INVALID_PARAMETER;
1480 modulePaths = defaultPaths;
1482 while (*modulePaths)
1484 const char* modulePath = *modulePaths++;
1485 HANDLE library = LoadLibrary(modulePath);
1486 typedef CK_RV (*c_get_function_list_t)(CK_FUNCTION_LIST_PTR_PTR);
1487 NCryptP11ProviderHandle* provider =
nullptr;
1489 WLog_DBG(TAG,
"Trying pkcs11 module '%s'", modulePath);
1492 status = NTE_PROV_DLL_NOT_FOUND;
1493 goto out_load_library;
1497 c_get_function_list_t c_get_function_list =
1498 GetProcAddressAs(library,
"C_GetFunctionList", c_get_function_list_t);
1500 if (!c_get_function_list)
1502 status = NTE_PROV_TYPE_ENTRY_BAD;
1503 goto out_load_library;
1506 status = initialize_pkcs11(library, c_get_function_list, phProvider);
1508 if (status != ERROR_SUCCESS)
1510 status = NTE_PROVIDER_DLL_FAIL;
1511 goto out_load_library;
1514 provider = (NCryptP11ProviderHandle*)*phProvider;
1515 provider->modulePath = _strdup(modulePath);
1516 if (!provider->modulePath)
1518 status = NTE_NO_MEMORY;
1519 goto out_load_library;
1522 WLog_DBG(TAG,
"module '%s' loaded", modulePath);
1523 return ERROR_SUCCESS;
1527 FreeLibrary(library);
1533const char* NCryptGetModulePath(NCRYPT_PROV_HANDLE phProvider)
1535 NCryptP11ProviderHandle* provider = (NCryptP11ProviderHandle*)phProvider;
1537 WINPR_ASSERT(provider);
1539 return provider->modulePath;
common ncrypt handle items
common ncrypt provider items