/src/FreeRDP/winpr/libwinpr/ncrypt/ncrypt_pkcs11.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * NCrypt pkcs11 provider |
4 | | * |
5 | | * Copyright 2021 David Fort <contact@hardening-consulting.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <stdlib.h> |
21 | | |
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> |
27 | | |
28 | | #include "../log.h" |
29 | | #include "ncrypt.h" |
30 | | |
31 | | /* https://github.com/latchset/pkcs11-headers/blob/main/public-domain/3.1/pkcs11.h */ |
32 | | #include "pkcs11-headers/pkcs11.h" |
33 | | |
34 | | #define TAG WINPR_TAG("ncryptp11") |
35 | | |
36 | 0 | #define MAX_SLOTS 64 |
37 | | #define MAX_KEYS 64 |
38 | | #define MAX_KEYS_PER_SLOT 64 |
39 | | |
40 | | /** @brief ncrypt provider handle */ |
41 | | typedef struct |
42 | | { |
43 | | NCryptBaseProvider baseProvider; |
44 | | |
45 | | HANDLE library; |
46 | | CK_FUNCTION_LIST_PTR p11; |
47 | | char* modulePath; |
48 | | } NCryptP11ProviderHandle; |
49 | | |
50 | | /** @brief a handle returned by NCryptOpenKey */ |
51 | | typedef struct |
52 | | { |
53 | | NCryptBaseHandle base; |
54 | | NCryptP11ProviderHandle* provider; |
55 | | CK_SLOT_ID slotId; |
56 | | CK_BYTE keyCertId[64]; |
57 | | CK_ULONG keyCertIdLen; |
58 | | } NCryptP11KeyHandle; |
59 | | |
60 | | typedef struct |
61 | | { |
62 | | CK_SLOT_ID slotId; |
63 | | CK_SLOT_INFO slotInfo; |
64 | | CK_KEY_TYPE keyType; |
65 | | CK_CHAR keyLabel[256]; |
66 | | CK_ULONG idLen; |
67 | | CK_BYTE id[64]; |
68 | | } NCryptKeyEnum; |
69 | | |
70 | | typedef struct |
71 | | { |
72 | | CK_ULONG nslots; |
73 | | CK_SLOT_ID slots[MAX_SLOTS]; |
74 | | CK_ULONG nKeys; |
75 | | NCryptKeyEnum keys[MAX_KEYS]; |
76 | | CK_ULONG keyIndex; |
77 | | } P11EnumKeysState; |
78 | | |
79 | | typedef struct |
80 | | { |
81 | | const char* label; |
82 | | BYTE tag[3]; |
83 | | } piv_cert_tags_t; |
84 | | static const piv_cert_tags_t piv_cert_tags[] = { |
85 | | { "Certificate for PIV Authentication", "\x5F\xC1\x05" }, |
86 | | { "Certificate for Digital Signature", "\x5F\xC1\x0A" }, |
87 | | { "Certificate for Key Management", "\x5F\xC1\x0B" }, |
88 | | { "Certificate for Card Authentication", "\x5F\xC1\x01" }, |
89 | | }; |
90 | | |
91 | | static const BYTE APDU_PIV_SELECT_AID[] = { 0x00, 0xA4, 0x04, 0x00, 0x09, 0xA0, 0x00, 0x00, |
92 | | 0x03, 0x08, 0x00, 0x00, 0x10, 0x00, 0x00 }; |
93 | | static const BYTE APDU_PIV_GET_CHUID[] = { 0x00, 0xCB, 0x3F, 0xFF, 0x05, 0x5C, |
94 | | 0x03, 0x5F, 0xC1, 0x02, 0x00 }; |
95 | 0 | #define PIV_CONTAINER_NAME_LEN 36 |
96 | | |
97 | | static CK_OBJECT_CLASS object_class_public_key = CKO_PUBLIC_KEY; |
98 | | static CK_BBOOL object_verify = CK_TRUE; |
99 | | static CK_KEY_TYPE object_ktype_rsa = CKK_RSA; |
100 | | |
101 | | static CK_ATTRIBUTE public_key_filter[] = { |
102 | | { CKA_CLASS, &object_class_public_key, sizeof(object_class_public_key) }, |
103 | | { CKA_VERIFY, &object_verify, sizeof(object_verify) }, |
104 | | { CKA_KEY_TYPE, &object_ktype_rsa, sizeof(object_ktype_rsa) } |
105 | | }; |
106 | | |
107 | | static const char* CK_RV_error_string(CK_RV rv); |
108 | | |
109 | | static SECURITY_STATUS NCryptP11StorageProvider_dtor(NCRYPT_HANDLE handle) |
110 | 0 | { |
111 | 0 | NCryptP11ProviderHandle* provider = (NCryptP11ProviderHandle*)handle; |
112 | 0 | CK_RV rv = CKR_OK; |
113 | |
|
114 | 0 | if (provider) |
115 | 0 | { |
116 | 0 | if (provider->p11 && provider->p11->C_Finalize) |
117 | 0 | rv = provider->p11->C_Finalize(NULL); |
118 | 0 | if (rv != CKR_OK) |
119 | 0 | WLog_WARN(TAG, "C_Finalize failed with %s [0x%08" PRIx32 "]", CK_RV_error_string(rv), |
120 | 0 | rv); |
121 | |
|
122 | 0 | free(provider->modulePath); |
123 | |
|
124 | 0 | if (provider->library) |
125 | 0 | FreeLibrary(provider->library); |
126 | 0 | } |
127 | |
|
128 | 0 | return winpr_NCryptDefault_dtor(handle); |
129 | 0 | } |
130 | | |
131 | | static void fix_padded_string(char* str, size_t maxlen) |
132 | 0 | { |
133 | 0 | if (maxlen == 0) |
134 | 0 | return; |
135 | | |
136 | 0 | WINPR_ASSERT(str); |
137 | 0 | char* ptr = &str[maxlen - 1]; |
138 | |
|
139 | 0 | while ((ptr > str) && (*ptr == ' ')) |
140 | 0 | { |
141 | 0 | *ptr = '\0'; |
142 | 0 | ptr--; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | static BOOL attributes_have_unallocated_buffers(CK_ATTRIBUTE_PTR attributes, CK_ULONG count) |
147 | 0 | { |
148 | 0 | for (CK_ULONG i = 0; i < count; i++) |
149 | 0 | { |
150 | 0 | if (!attributes[i].pValue && (attributes[i].ulValueLen != CK_UNAVAILABLE_INFORMATION)) |
151 | 0 | return TRUE; |
152 | 0 | } |
153 | | |
154 | 0 | return FALSE; |
155 | 0 | } |
156 | | |
157 | | static BOOL attribute_allocate_attribute_array(CK_ATTRIBUTE_PTR attribute) |
158 | 0 | { |
159 | 0 | WINPR_ASSERT(attribute); |
160 | 0 | attribute->pValue = calloc(attribute->ulValueLen, sizeof(void*)); |
161 | 0 | return !!attribute->pValue; |
162 | 0 | } |
163 | | |
164 | | static BOOL attribute_allocate_ulong_array(CK_ATTRIBUTE_PTR attribute) |
165 | 0 | { |
166 | 0 | attribute->pValue = calloc(attribute->ulValueLen, sizeof(CK_ULONG)); |
167 | 0 | return !!attribute->pValue; |
168 | 0 | } |
169 | | |
170 | | static BOOL attribute_allocate_buffer(CK_ATTRIBUTE_PTR attribute) |
171 | 0 | { |
172 | 0 | attribute->pValue = calloc(attribute->ulValueLen, 1); |
173 | 0 | return !!attribute->pValue; |
174 | 0 | } |
175 | | |
176 | | static BOOL attributes_allocate_buffers(CK_ATTRIBUTE_PTR attributes, CK_ULONG count) |
177 | 0 | { |
178 | 0 | BOOL ret = TRUE; |
179 | |
|
180 | 0 | for (CK_ULONG i = 0; i < count; i++) |
181 | 0 | { |
182 | 0 | if (attributes[i].pValue || (attributes[i].ulValueLen == CK_UNAVAILABLE_INFORMATION)) |
183 | 0 | continue; |
184 | | |
185 | 0 | switch (attributes[i].type) |
186 | 0 | { |
187 | 0 | case CKA_WRAP_TEMPLATE: |
188 | 0 | case CKA_UNWRAP_TEMPLATE: |
189 | 0 | ret &= attribute_allocate_attribute_array(&attributes[i]); |
190 | 0 | break; |
191 | | |
192 | 0 | case CKA_ALLOWED_MECHANISMS: |
193 | 0 | ret &= attribute_allocate_ulong_array(&attributes[i]); |
194 | 0 | break; |
195 | | |
196 | 0 | default: |
197 | 0 | ret &= attribute_allocate_buffer(&attributes[i]); |
198 | 0 | break; |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | 0 | return ret; |
203 | 0 | } |
204 | | |
205 | | static CK_RV object_load_attributes(NCryptP11ProviderHandle* provider, CK_SESSION_HANDLE session, |
206 | | CK_OBJECT_HANDLE object, CK_ATTRIBUTE_PTR attributes, |
207 | | CK_ULONG count) |
208 | 0 | { |
209 | 0 | WINPR_ASSERT(provider); |
210 | 0 | WINPR_ASSERT(provider->p11); |
211 | 0 | WINPR_ASSERT(provider->p11->C_GetAttributeValue); |
212 | | |
213 | 0 | CK_RV rv = provider->p11->C_GetAttributeValue(session, object, attributes, count); |
214 | |
|
215 | 0 | switch (rv) |
216 | 0 | { |
217 | 0 | case CKR_OK: |
218 | 0 | if (!attributes_have_unallocated_buffers(attributes, count)) |
219 | 0 | return rv; |
220 | | /* fallthrough */ |
221 | 0 | WINPR_FALLTHROUGH |
222 | 0 | case CKR_ATTRIBUTE_SENSITIVE: |
223 | 0 | case CKR_ATTRIBUTE_TYPE_INVALID: |
224 | 0 | case CKR_BUFFER_TOO_SMALL: |
225 | | /* attributes need some buffers for the result value */ |
226 | 0 | if (!attributes_allocate_buffers(attributes, count)) |
227 | 0 | return CKR_HOST_MEMORY; |
228 | | |
229 | 0 | rv = provider->p11->C_GetAttributeValue(session, object, attributes, count); |
230 | 0 | if (rv != CKR_OK) |
231 | 0 | WLog_WARN(TAG, "C_GetAttributeValue failed with %s [0x%08" PRIx32 "]", |
232 | 0 | CK_RV_error_string(rv), rv); |
233 | 0 | break; |
234 | 0 | default: |
235 | 0 | WLog_WARN(TAG, "C_GetAttributeValue failed with %s [0x%08" PRIx32 "]", |
236 | 0 | CK_RV_error_string(rv), rv); |
237 | 0 | return rv; |
238 | 0 | } |
239 | | |
240 | 0 | switch (rv) |
241 | 0 | { |
242 | 0 | case CKR_ATTRIBUTE_SENSITIVE: |
243 | 0 | case CKR_ATTRIBUTE_TYPE_INVALID: |
244 | 0 | case CKR_BUFFER_TOO_SMALL: |
245 | 0 | WLog_ERR(TAG, |
246 | 0 | "C_GetAttributeValue failed with %s [0x%08" PRIx32 |
247 | 0 | "] even after buffer allocation", |
248 | 0 | CK_RV_error_string(rv), rv); |
249 | 0 | break; |
250 | 0 | default: |
251 | 0 | break; |
252 | 0 | } |
253 | 0 | return rv; |
254 | 0 | } |
255 | | |
256 | | static const char* CK_RV_error_string(CK_RV rv) |
257 | 0 | { |
258 | 0 | static char generic_buffer[200]; |
259 | 0 | #define ERR_ENTRY(X) \ |
260 | 0 | case X: \ |
261 | 0 | return #X |
262 | |
|
263 | 0 | switch (rv) |
264 | 0 | { |
265 | 0 | ERR_ENTRY(CKR_OK); |
266 | 0 | ERR_ENTRY(CKR_CANCEL); |
267 | 0 | ERR_ENTRY(CKR_HOST_MEMORY); |
268 | 0 | ERR_ENTRY(CKR_SLOT_ID_INVALID); |
269 | 0 | ERR_ENTRY(CKR_GENERAL_ERROR); |
270 | 0 | ERR_ENTRY(CKR_FUNCTION_FAILED); |
271 | 0 | ERR_ENTRY(CKR_ARGUMENTS_BAD); |
272 | 0 | ERR_ENTRY(CKR_NO_EVENT); |
273 | 0 | ERR_ENTRY(CKR_NEED_TO_CREATE_THREADS); |
274 | 0 | ERR_ENTRY(CKR_CANT_LOCK); |
275 | 0 | ERR_ENTRY(CKR_ATTRIBUTE_READ_ONLY); |
276 | 0 | ERR_ENTRY(CKR_ATTRIBUTE_SENSITIVE); |
277 | 0 | ERR_ENTRY(CKR_ATTRIBUTE_TYPE_INVALID); |
278 | 0 | ERR_ENTRY(CKR_ATTRIBUTE_VALUE_INVALID); |
279 | 0 | ERR_ENTRY(CKR_DATA_INVALID); |
280 | 0 | ERR_ENTRY(CKR_DATA_LEN_RANGE); |
281 | 0 | ERR_ENTRY(CKR_DEVICE_ERROR); |
282 | 0 | ERR_ENTRY(CKR_DEVICE_MEMORY); |
283 | 0 | ERR_ENTRY(CKR_DEVICE_REMOVED); |
284 | 0 | ERR_ENTRY(CKR_ENCRYPTED_DATA_INVALID); |
285 | 0 | ERR_ENTRY(CKR_ENCRYPTED_DATA_LEN_RANGE); |
286 | 0 | ERR_ENTRY(CKR_FUNCTION_CANCELED); |
287 | 0 | ERR_ENTRY(CKR_FUNCTION_NOT_PARALLEL); |
288 | 0 | ERR_ENTRY(CKR_FUNCTION_NOT_SUPPORTED); |
289 | 0 | ERR_ENTRY(CKR_KEY_HANDLE_INVALID); |
290 | 0 | ERR_ENTRY(CKR_KEY_SIZE_RANGE); |
291 | 0 | ERR_ENTRY(CKR_KEY_TYPE_INCONSISTENT); |
292 | 0 | ERR_ENTRY(CKR_KEY_NOT_NEEDED); |
293 | 0 | ERR_ENTRY(CKR_KEY_CHANGED); |
294 | 0 | ERR_ENTRY(CKR_KEY_NEEDED); |
295 | 0 | ERR_ENTRY(CKR_KEY_INDIGESTIBLE); |
296 | 0 | ERR_ENTRY(CKR_KEY_FUNCTION_NOT_PERMITTED); |
297 | 0 | ERR_ENTRY(CKR_KEY_NOT_WRAPPABLE); |
298 | 0 | ERR_ENTRY(CKR_KEY_UNEXTRACTABLE); |
299 | 0 | ERR_ENTRY(CKR_MECHANISM_INVALID); |
300 | 0 | ERR_ENTRY(CKR_MECHANISM_PARAM_INVALID); |
301 | 0 | ERR_ENTRY(CKR_OBJECT_HANDLE_INVALID); |
302 | 0 | ERR_ENTRY(CKR_OPERATION_ACTIVE); |
303 | 0 | ERR_ENTRY(CKR_OPERATION_NOT_INITIALIZED); |
304 | 0 | ERR_ENTRY(CKR_PIN_INCORRECT); |
305 | 0 | ERR_ENTRY(CKR_PIN_INVALID); |
306 | 0 | ERR_ENTRY(CKR_PIN_LEN_RANGE); |
307 | 0 | ERR_ENTRY(CKR_PIN_EXPIRED); |
308 | 0 | ERR_ENTRY(CKR_PIN_LOCKED); |
309 | 0 | ERR_ENTRY(CKR_SESSION_CLOSED); |
310 | 0 | ERR_ENTRY(CKR_SESSION_COUNT); |
311 | 0 | ERR_ENTRY(CKR_SESSION_HANDLE_INVALID); |
312 | 0 | ERR_ENTRY(CKR_SESSION_PARALLEL_NOT_SUPPORTED); |
313 | 0 | ERR_ENTRY(CKR_SESSION_READ_ONLY); |
314 | 0 | ERR_ENTRY(CKR_SESSION_EXISTS); |
315 | 0 | ERR_ENTRY(CKR_SESSION_READ_ONLY_EXISTS); |
316 | 0 | ERR_ENTRY(CKR_SESSION_READ_WRITE_SO_EXISTS); |
317 | 0 | ERR_ENTRY(CKR_SIGNATURE_INVALID); |
318 | 0 | ERR_ENTRY(CKR_SIGNATURE_LEN_RANGE); |
319 | 0 | ERR_ENTRY(CKR_TEMPLATE_INCOMPLETE); |
320 | 0 | ERR_ENTRY(CKR_TEMPLATE_INCONSISTENT); |
321 | 0 | ERR_ENTRY(CKR_TOKEN_NOT_PRESENT); |
322 | 0 | ERR_ENTRY(CKR_TOKEN_NOT_RECOGNIZED); |
323 | 0 | ERR_ENTRY(CKR_TOKEN_WRITE_PROTECTED); |
324 | 0 | ERR_ENTRY(CKR_UNWRAPPING_KEY_HANDLE_INVALID); |
325 | 0 | ERR_ENTRY(CKR_UNWRAPPING_KEY_SIZE_RANGE); |
326 | 0 | ERR_ENTRY(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT); |
327 | 0 | ERR_ENTRY(CKR_USER_ALREADY_LOGGED_IN); |
328 | 0 | ERR_ENTRY(CKR_USER_NOT_LOGGED_IN); |
329 | 0 | ERR_ENTRY(CKR_USER_PIN_NOT_INITIALIZED); |
330 | 0 | ERR_ENTRY(CKR_USER_TYPE_INVALID); |
331 | 0 | ERR_ENTRY(CKR_USER_ANOTHER_ALREADY_LOGGED_IN); |
332 | 0 | ERR_ENTRY(CKR_USER_TOO_MANY_TYPES); |
333 | 0 | ERR_ENTRY(CKR_WRAPPED_KEY_INVALID); |
334 | 0 | ERR_ENTRY(CKR_WRAPPED_KEY_LEN_RANGE); |
335 | 0 | ERR_ENTRY(CKR_WRAPPING_KEY_HANDLE_INVALID); |
336 | 0 | ERR_ENTRY(CKR_WRAPPING_KEY_SIZE_RANGE); |
337 | 0 | ERR_ENTRY(CKR_WRAPPING_KEY_TYPE_INCONSISTENT); |
338 | 0 | ERR_ENTRY(CKR_RANDOM_SEED_NOT_SUPPORTED); |
339 | 0 | ERR_ENTRY(CKR_RANDOM_NO_RNG); |
340 | 0 | ERR_ENTRY(CKR_DOMAIN_PARAMS_INVALID); |
341 | 0 | ERR_ENTRY(CKR_BUFFER_TOO_SMALL); |
342 | 0 | ERR_ENTRY(CKR_SAVED_STATE_INVALID); |
343 | 0 | ERR_ENTRY(CKR_INFORMATION_SENSITIVE); |
344 | 0 | ERR_ENTRY(CKR_STATE_UNSAVEABLE); |
345 | 0 | ERR_ENTRY(CKR_CRYPTOKI_NOT_INITIALIZED); |
346 | 0 | ERR_ENTRY(CKR_CRYPTOKI_ALREADY_INITIALIZED); |
347 | 0 | ERR_ENTRY(CKR_MUTEX_BAD); |
348 | 0 | ERR_ENTRY(CKR_MUTEX_NOT_LOCKED); |
349 | 0 | ERR_ENTRY(CKR_FUNCTION_REJECTED); |
350 | 0 | default: |
351 | 0 | (void)snprintf(generic_buffer, sizeof(generic_buffer), "unknown 0x%lx", rv); |
352 | 0 | return generic_buffer; |
353 | 0 | } |
354 | 0 | #undef ERR_ENTRY |
355 | 0 | } |
356 | | |
357 | | #define loge(tag, msg, rv, index, slot) \ |
358 | 0 | log_((tag), (msg), (rv), (index), (slot), __FILE__, __func__, __LINE__) |
359 | | static void log_(const char* tag, const char* msg, CK_RV rv, CK_ULONG index, CK_SLOT_ID slot, |
360 | | const char* file, const char* fkt, size_t line) |
361 | 0 | { |
362 | 0 | const DWORD log_level = WLOG_ERROR; |
363 | 0 | static wLog* log_cached_ptr = NULL; |
364 | 0 | if (!log_cached_ptr) |
365 | 0 | log_cached_ptr = WLog_Get(tag); |
366 | 0 | if (!WLog_IsLevelActive(log_cached_ptr, log_level)) |
367 | 0 | return; |
368 | | |
369 | 0 | WLog_PrintMessage(log_cached_ptr, WLOG_MESSAGE_TEXT, log_level, line, file, fkt, |
370 | 0 | "%s for slot #%" PRIu32 "(%" PRIu32 "), rv=%s", msg, index, slot, |
371 | 0 | CK_RV_error_string(rv)); |
372 | 0 | } |
373 | | |
374 | | static SECURITY_STATUS collect_keys(NCryptP11ProviderHandle* provider, P11EnumKeysState* state) |
375 | 0 | { |
376 | 0 | CK_OBJECT_HANDLE slotObjects[MAX_KEYS_PER_SLOT] = { 0 }; |
377 | |
|
378 | 0 | WINPR_ASSERT(provider); |
379 | | |
380 | 0 | CK_FUNCTION_LIST_PTR p11 = provider->p11; |
381 | 0 | WINPR_ASSERT(p11); |
382 | | |
383 | 0 | WLog_DBG(TAG, "checking %" PRIu32 " slots for valid keys...", state->nslots); |
384 | 0 | state->nKeys = 0; |
385 | 0 | for (CK_ULONG i = 0; i < state->nslots; i++) |
386 | 0 | { |
387 | 0 | CK_SESSION_HANDLE session = (CK_SESSION_HANDLE)NULL; |
388 | 0 | CK_SLOT_INFO slotInfo = { 0 }; |
389 | 0 | CK_TOKEN_INFO tokenInfo = { 0 }; |
390 | |
|
391 | 0 | WINPR_ASSERT(p11->C_GetSlotInfo); |
392 | 0 | CK_RV rv = p11->C_GetSlotInfo(state->slots[i], &slotInfo); |
393 | 0 | if (rv != CKR_OK) |
394 | 0 | { |
395 | 0 | loge(TAG, "unable to retrieve information", rv, i, state->slots[i]); |
396 | 0 | continue; |
397 | 0 | } |
398 | | |
399 | 0 | fix_padded_string((char*)slotInfo.slotDescription, sizeof(slotInfo.slotDescription)); |
400 | 0 | WLog_DBG(TAG, "collecting keys for slot #%" PRIu32 "(%" PRIu32 ") descr='%s' flags=0x%x", i, |
401 | 0 | state->slots[i], slotInfo.slotDescription, slotInfo.flags); |
402 | | |
403 | | /* this is a safety guard as we're supposed to have listed only readers with tokens in them |
404 | | */ |
405 | 0 | if (!(slotInfo.flags & CKF_TOKEN_PRESENT)) |
406 | 0 | { |
407 | 0 | WLog_INFO(TAG, "token not present for slot #%" PRIu32 "(%" PRIu32 ")", i, |
408 | 0 | state->slots[i]); |
409 | 0 | continue; |
410 | 0 | } |
411 | | |
412 | 0 | WINPR_ASSERT(p11->C_GetTokenInfo); |
413 | 0 | rv = p11->C_GetTokenInfo(state->slots[i], &tokenInfo); |
414 | 0 | if (rv != CKR_OK) |
415 | 0 | loge(TAG, "unable to retrieve token info", rv, i, state->slots[i]); |
416 | 0 | else |
417 | 0 | { |
418 | 0 | fix_padded_string((char*)tokenInfo.label, sizeof(tokenInfo.label)); |
419 | 0 | WLog_DBG(TAG, "token, label='%s' flags=0x%x", tokenInfo.label, tokenInfo.flags); |
420 | 0 | } |
421 | |
|
422 | 0 | WINPR_ASSERT(p11->C_OpenSession); |
423 | 0 | rv = p11->C_OpenSession(state->slots[i], CKF_SERIAL_SESSION, NULL, NULL, &session); |
424 | 0 | if (rv != CKR_OK) |
425 | 0 | { |
426 | 0 | WLog_ERR(TAG, |
427 | 0 | "unable to openSession for slot #%" PRIu32 "(%" PRIu32 "), session=%p rv=%s", |
428 | 0 | i, state->slots[i], session, CK_RV_error_string(rv)); |
429 | 0 | continue; |
430 | 0 | } |
431 | | |
432 | 0 | WINPR_ASSERT(p11->C_FindObjectsInit); |
433 | 0 | rv = p11->C_FindObjectsInit(session, public_key_filter, ARRAYSIZE(public_key_filter)); |
434 | 0 | if (rv != CKR_OK) |
435 | 0 | { |
436 | | // TODO: shall it be fatal ? |
437 | 0 | loge(TAG, "unable to initiate search", rv, i, state->slots[i]); |
438 | 0 | goto cleanup_FindObjectsInit; |
439 | 0 | } |
440 | | |
441 | 0 | CK_ULONG nslotObjects = 0; |
442 | 0 | WINPR_ASSERT(p11->C_FindObjects); |
443 | 0 | rv = p11->C_FindObjects(session, &slotObjects[0], ARRAYSIZE(slotObjects), &nslotObjects); |
444 | 0 | if (rv != CKR_OK) |
445 | 0 | { |
446 | 0 | loge(TAG, "unable to findObjects", rv, i, state->slots[i]); |
447 | 0 | goto cleanup_FindObjects; |
448 | 0 | } |
449 | | |
450 | 0 | WLog_DBG(TAG, "slot has %d objects", nslotObjects); |
451 | 0 | for (CK_ULONG j = 0; j < nslotObjects; j++) |
452 | 0 | { |
453 | 0 | NCryptKeyEnum* key = &state->keys[state->nKeys]; |
454 | 0 | CK_OBJECT_CLASS dataClass = CKO_PUBLIC_KEY; |
455 | 0 | CK_ATTRIBUTE key_or_certAttrs[] = { |
456 | 0 | { CKA_ID, &key->id, sizeof(key->id) }, |
457 | 0 | { CKA_CLASS, &dataClass, sizeof(dataClass) }, |
458 | 0 | { CKA_LABEL, &key->keyLabel, sizeof(key->keyLabel) }, |
459 | 0 | { CKA_KEY_TYPE, &key->keyType, sizeof(key->keyType) } |
460 | 0 | }; |
461 | |
|
462 | 0 | rv = object_load_attributes(provider, session, slotObjects[j], key_or_certAttrs, |
463 | 0 | ARRAYSIZE(key_or_certAttrs)); |
464 | 0 | if (rv != CKR_OK) |
465 | 0 | { |
466 | 0 | WLog_ERR(TAG, "error getting attributes, rv=%s", CK_RV_error_string(rv)); |
467 | 0 | continue; |
468 | 0 | } |
469 | | |
470 | 0 | key->idLen = key_or_certAttrs[0].ulValueLen; |
471 | 0 | key->slotId = state->slots[i]; |
472 | 0 | key->slotInfo = slotInfo; |
473 | 0 | state->nKeys++; |
474 | 0 | } |
475 | |
|
476 | 0 | cleanup_FindObjects: |
477 | 0 | WINPR_ASSERT(p11->C_FindObjectsFinal); |
478 | 0 | rv = p11->C_FindObjectsFinal(session); |
479 | 0 | if (rv != CKR_OK) |
480 | 0 | loge(TAG, "error during C_FindObjectsFinal", rv, i, state->slots[i]); |
481 | 0 | cleanup_FindObjectsInit: |
482 | 0 | WINPR_ASSERT(p11->C_CloseSession); |
483 | 0 | rv = p11->C_CloseSession(session); |
484 | 0 | if (rv != CKR_OK) |
485 | 0 | loge(TAG, "error closing session", rv, i, state->slots[i]); |
486 | 0 | } |
487 | | |
488 | 0 | return ERROR_SUCCESS; |
489 | 0 | } |
490 | | |
491 | | static BOOL convertKeyType(CK_KEY_TYPE k, LPWSTR dest, DWORD len, DWORD* outlen) |
492 | 0 | { |
493 | 0 | const WCHAR* r = NULL; |
494 | 0 | size_t retLen = 0; |
495 | |
|
496 | 0 | #define ALGO_CASE(V, S) \ |
497 | 0 | case V: \ |
498 | 0 | r = S; \ |
499 | 0 | retLen = _wcsnlen((S), ARRAYSIZE((S))); \ |
500 | 0 | break |
501 | 0 | switch (k) |
502 | 0 | { |
503 | 0 | ALGO_CASE(CKK_RSA, BCRYPT_RSA_ALGORITHM); |
504 | 0 | ALGO_CASE(CKK_DSA, BCRYPT_DSA_ALGORITHM); |
505 | 0 | ALGO_CASE(CKK_DH, BCRYPT_DH_ALGORITHM); |
506 | 0 | ALGO_CASE(CKK_EC, BCRYPT_ECDSA_ALGORITHM); |
507 | 0 | ALGO_CASE(CKK_RC2, BCRYPT_RC2_ALGORITHM); |
508 | 0 | ALGO_CASE(CKK_RC4, BCRYPT_RC4_ALGORITHM); |
509 | 0 | ALGO_CASE(CKK_DES, BCRYPT_DES_ALGORITHM); |
510 | 0 | ALGO_CASE(CKK_DES3, BCRYPT_3DES_ALGORITHM); |
511 | 0 | case CKK_DES2: |
512 | 0 | case CKK_X9_42_DH: |
513 | 0 | case CKK_KEA: |
514 | 0 | case CKK_GENERIC_SECRET: |
515 | 0 | case CKK_CAST: |
516 | 0 | case CKK_CAST3: |
517 | 0 | case CKK_CAST128: |
518 | 0 | case CKK_RC5: |
519 | 0 | case CKK_IDEA: |
520 | 0 | case CKK_SKIPJACK: |
521 | 0 | case CKK_BATON: |
522 | 0 | case CKK_JUNIPER: |
523 | 0 | case CKK_CDMF: |
524 | 0 | case CKK_AES: |
525 | 0 | case CKK_BLOWFISH: |
526 | 0 | case CKK_TWOFISH: |
527 | 0 | default: |
528 | 0 | break; |
529 | 0 | } |
530 | 0 | #undef ALGO_CASE |
531 | | |
532 | 0 | if (retLen > UINT32_MAX) |
533 | 0 | return FALSE; |
534 | | |
535 | 0 | if (outlen) |
536 | 0 | *outlen = (UINT32)retLen; |
537 | |
|
538 | 0 | if (!r) |
539 | 0 | { |
540 | 0 | if (dest && len > 0) |
541 | 0 | dest[0] = 0; |
542 | 0 | return FALSE; |
543 | 0 | } |
544 | | |
545 | 0 | if (dest) |
546 | 0 | { |
547 | 0 | if (retLen + 1 > len) |
548 | 0 | { |
549 | 0 | WLog_ERR(TAG, "target buffer is too small for algo name"); |
550 | 0 | return FALSE; |
551 | 0 | } |
552 | | |
553 | 0 | memcpy(dest, r, sizeof(WCHAR) * retLen); |
554 | 0 | dest[retLen] = 0; |
555 | 0 | } |
556 | | |
557 | 0 | return TRUE; |
558 | 0 | } |
559 | | |
560 | | static void wprintKeyName(LPWSTR str, CK_SLOT_ID slotId, CK_BYTE* id, CK_ULONG idLen) |
561 | 0 | { |
562 | 0 | char asciiName[128] = { 0 }; |
563 | 0 | char* ptr = asciiName; |
564 | 0 | const CK_BYTE* bytePtr = NULL; |
565 | |
|
566 | 0 | *ptr = '\\'; |
567 | 0 | ptr++; |
568 | |
|
569 | 0 | bytePtr = ((CK_BYTE*)&slotId); |
570 | 0 | for (CK_ULONG i = 0; i < sizeof(slotId); i++, bytePtr++, ptr += 2) |
571 | 0 | (void)snprintf(ptr, 3, "%.2x", *bytePtr); |
572 | |
|
573 | 0 | *ptr = '\\'; |
574 | 0 | ptr++; |
575 | |
|
576 | 0 | for (CK_ULONG i = 0; i < idLen; i++, id++, ptr += 2) |
577 | 0 | (void)snprintf(ptr, 3, "%.2x", *id); |
578 | |
|
579 | 0 | (void)ConvertUtf8NToWChar(asciiName, ARRAYSIZE(asciiName), str, |
580 | 0 | strnlen(asciiName, ARRAYSIZE(asciiName)) + 1); |
581 | 0 | } |
582 | | |
583 | | static size_t parseHex(const char* str, const char* end, CK_BYTE* target) |
584 | 0 | { |
585 | 0 | size_t ret = 0; |
586 | |
|
587 | 0 | for (; str != end && *str; str++, ret++, target++) |
588 | 0 | { |
589 | 0 | int v = 0; |
590 | 0 | if (*str <= '9' && *str >= '0') |
591 | 0 | { |
592 | 0 | v = (*str - '0'); |
593 | 0 | } |
594 | 0 | else if (*str <= 'f' && *str >= 'a') |
595 | 0 | { |
596 | 0 | v = (10 + *str - 'a'); |
597 | 0 | } |
598 | 0 | else if (*str <= 'F' && *str >= 'A') |
599 | 0 | { |
600 | 0 | v |= (10 + *str - 'A'); |
601 | 0 | } |
602 | 0 | else |
603 | 0 | { |
604 | 0 | return 0; |
605 | 0 | } |
606 | 0 | v <<= 4; |
607 | 0 | str++; |
608 | |
|
609 | 0 | if (!*str || str == end) |
610 | 0 | return 0; |
611 | | |
612 | 0 | if (*str <= '9' && *str >= '0') |
613 | 0 | { |
614 | 0 | v |= (*str - '0'); |
615 | 0 | } |
616 | 0 | else if (*str <= 'f' && *str >= 'a') |
617 | 0 | { |
618 | 0 | v |= (10 + *str - 'a'); |
619 | 0 | } |
620 | 0 | else if (*str <= 'F' && *str >= 'A') |
621 | 0 | { |
622 | 0 | v |= (10 + *str - 'A'); |
623 | 0 | } |
624 | 0 | else |
625 | 0 | { |
626 | 0 | return 0; |
627 | 0 | } |
628 | | |
629 | 0 | *target = v & 0xFF; |
630 | 0 | } |
631 | 0 | return ret; |
632 | 0 | } |
633 | | |
634 | | static SECURITY_STATUS parseKeyName(LPCWSTR pszKeyName, CK_SLOT_ID* slotId, CK_BYTE* id, |
635 | | CK_ULONG* idLen) |
636 | 0 | { |
637 | 0 | char asciiKeyName[128] = { 0 }; |
638 | 0 | char* pos = NULL; |
639 | |
|
640 | 0 | if (ConvertWCharToUtf8(pszKeyName, asciiKeyName, ARRAYSIZE(asciiKeyName)) < 0) |
641 | 0 | return NTE_BAD_KEY; |
642 | | |
643 | 0 | if (*asciiKeyName != '\\') |
644 | 0 | return NTE_BAD_KEY; |
645 | | |
646 | 0 | pos = strchr(&asciiKeyName[1], '\\'); |
647 | 0 | if (!pos) |
648 | 0 | return NTE_BAD_KEY; |
649 | | |
650 | 0 | if ((size_t)(pos - &asciiKeyName[1]) > sizeof(CK_SLOT_ID) * 2ull) |
651 | 0 | return NTE_BAD_KEY; |
652 | | |
653 | 0 | *slotId = (CK_SLOT_ID)0; |
654 | 0 | if (parseHex(&asciiKeyName[1], pos, (CK_BYTE*)slotId) != sizeof(CK_SLOT_ID)) |
655 | 0 | return NTE_BAD_KEY; |
656 | | |
657 | 0 | *idLen = parseHex(pos + 1, NULL, id); |
658 | 0 | if (!*idLen) |
659 | 0 | return NTE_BAD_KEY; |
660 | | |
661 | 0 | return ERROR_SUCCESS; |
662 | 0 | } |
663 | | |
664 | | static SECURITY_STATUS NCryptP11EnumKeys(NCRYPT_PROV_HANDLE hProvider, LPCWSTR pszScope, |
665 | | NCryptKeyName** ppKeyName, PVOID* ppEnumState, |
666 | | WINPR_ATTR_UNUSED DWORD dwFlags) |
667 | 0 | { |
668 | 0 | NCryptP11ProviderHandle* provider = (NCryptP11ProviderHandle*)hProvider; |
669 | 0 | P11EnumKeysState* state = (P11EnumKeysState*)*ppEnumState; |
670 | 0 | CK_RV rv = { 0 }; |
671 | 0 | CK_SLOT_ID currentSlot = { 0 }; |
672 | 0 | CK_SESSION_HANDLE currentSession = (CK_SESSION_HANDLE)NULL; |
673 | 0 | char slotFilterBuffer[65] = { 0 }; |
674 | 0 | char* slotFilter = NULL; |
675 | 0 | size_t slotFilterLen = 0; |
676 | |
|
677 | 0 | SECURITY_STATUS ret = checkNCryptHandle((NCRYPT_HANDLE)hProvider, WINPR_NCRYPT_PROVIDER); |
678 | 0 | if (ret != ERROR_SUCCESS) |
679 | 0 | return ret; |
680 | | |
681 | 0 | if (pszScope) |
682 | 0 | { |
683 | | /* |
684 | | * check whether pszScope is of the form \\.\<reader name>\ for filtering by |
685 | | * card reader |
686 | | */ |
687 | 0 | char asciiScope[128 + 6 + 1] = { 0 }; |
688 | 0 | size_t asciiScopeLen = 0; |
689 | |
|
690 | 0 | if (ConvertWCharToUtf8(pszScope, asciiScope, ARRAYSIZE(asciiScope) - 1) < 0) |
691 | 0 | { |
692 | 0 | WLog_WARN(TAG, "Invalid scope"); |
693 | 0 | return NTE_INVALID_PARAMETER; |
694 | 0 | } |
695 | | |
696 | 0 | if (strstr(asciiScope, "\\\\.\\") != asciiScope) |
697 | 0 | { |
698 | 0 | WLog_WARN(TAG, "Invalid scope '%s'", asciiScope); |
699 | 0 | return NTE_INVALID_PARAMETER; |
700 | 0 | } |
701 | | |
702 | 0 | asciiScopeLen = strnlen(asciiScope, ARRAYSIZE(asciiScope)); |
703 | 0 | if ((asciiScopeLen < 1) || (asciiScope[asciiScopeLen - 1] != '\\')) |
704 | 0 | { |
705 | 0 | WLog_WARN(TAG, "Invalid scope '%s'", asciiScope); |
706 | 0 | return NTE_INVALID_PARAMETER; |
707 | 0 | } |
708 | | |
709 | 0 | asciiScope[asciiScopeLen - 1] = 0; |
710 | |
|
711 | 0 | strncpy(slotFilterBuffer, &asciiScope[4], sizeof(slotFilterBuffer)); |
712 | 0 | slotFilter = slotFilterBuffer; |
713 | 0 | slotFilterLen = asciiScopeLen - 5; |
714 | 0 | } |
715 | | |
716 | 0 | if (!state) |
717 | 0 | { |
718 | 0 | state = (P11EnumKeysState*)calloc(1, sizeof(*state)); |
719 | 0 | if (!state) |
720 | 0 | return NTE_NO_MEMORY; |
721 | | |
722 | 0 | WINPR_ASSERT(provider->p11->C_GetSlotList); |
723 | 0 | rv = provider->p11->C_GetSlotList(CK_TRUE, NULL, &state->nslots); |
724 | 0 | if (rv != CKR_OK) |
725 | 0 | { |
726 | 0 | free(state); |
727 | | /* TODO: perhaps convert rv to NTE_*** errors */ |
728 | 0 | WLog_WARN(TAG, "C_GetSlotList failed with %s [0x%08" PRIx32 "]", CK_RV_error_string(rv), |
729 | 0 | rv); |
730 | 0 | return NTE_FAIL; |
731 | 0 | } |
732 | | |
733 | 0 | if (state->nslots > MAX_SLOTS) |
734 | 0 | state->nslots = MAX_SLOTS; |
735 | |
|
736 | 0 | rv = provider->p11->C_GetSlotList(CK_TRUE, state->slots, &state->nslots); |
737 | 0 | if (rv != CKR_OK) |
738 | 0 | { |
739 | 0 | free(state); |
740 | | /* TODO: perhaps convert rv to NTE_*** errors */ |
741 | 0 | WLog_WARN(TAG, "C_GetSlotList failed with %s [0x%08" PRIx32 "]", CK_RV_error_string(rv), |
742 | 0 | rv); |
743 | 0 | return NTE_FAIL; |
744 | 0 | } |
745 | | |
746 | 0 | ret = collect_keys(provider, state); |
747 | 0 | if (ret != ERROR_SUCCESS) |
748 | 0 | { |
749 | 0 | free(state); |
750 | 0 | return ret; |
751 | 0 | } |
752 | | |
753 | 0 | *ppEnumState = state; |
754 | 0 | } |
755 | | |
756 | 0 | for (; state->keyIndex < state->nKeys; state->keyIndex++) |
757 | 0 | { |
758 | 0 | NCryptKeyName* keyName = NULL; |
759 | 0 | NCryptKeyEnum* key = &state->keys[state->keyIndex]; |
760 | 0 | CK_OBJECT_CLASS oclass = CKO_CERTIFICATE; |
761 | 0 | CK_CERTIFICATE_TYPE ctype = CKC_X_509; |
762 | 0 | CK_ATTRIBUTE certificateFilter[] = { { CKA_CLASS, &oclass, sizeof(oclass) }, |
763 | 0 | { CKA_CERTIFICATE_TYPE, &ctype, sizeof(ctype) }, |
764 | 0 | { CKA_ID, key->id, key->idLen } }; |
765 | 0 | CK_ULONG ncertObjects = 0; |
766 | 0 | CK_OBJECT_HANDLE certObject = 0; |
767 | | |
768 | | /* check the reader filter if any */ |
769 | 0 | if (slotFilter && memcmp(key->slotInfo.slotDescription, slotFilter, slotFilterLen) != 0) |
770 | 0 | continue; |
771 | | |
772 | 0 | if (!currentSession || (currentSlot != key->slotId)) |
773 | 0 | { |
774 | | /* if the current session doesn't match the current key's slot, open a new one |
775 | | */ |
776 | 0 | if (currentSession) |
777 | 0 | { |
778 | 0 | WINPR_ASSERT(provider->p11->C_CloseSession); |
779 | 0 | rv = provider->p11->C_CloseSession(currentSession); |
780 | 0 | if (rv != CKR_OK) |
781 | 0 | WLog_WARN(TAG, "C_CloseSession failed with %s [0x%08" PRIx32 "]", |
782 | 0 | CK_RV_error_string(rv), rv); |
783 | 0 | currentSession = (CK_SESSION_HANDLE)NULL; |
784 | 0 | } |
785 | | |
786 | 0 | WINPR_ASSERT(provider->p11->C_OpenSession); |
787 | 0 | rv = provider->p11->C_OpenSession(key->slotId, CKF_SERIAL_SESSION, NULL, NULL, |
788 | 0 | ¤tSession); |
789 | 0 | if (rv != CKR_OK) |
790 | 0 | { |
791 | 0 | WLog_ERR(TAG, "C_OpenSession failed with %s [0x%08" PRIx32 "] for slot %d", |
792 | 0 | CK_RV_error_string(rv), rv, key->slotId); |
793 | 0 | continue; |
794 | 0 | } |
795 | 0 | currentSlot = key->slotId; |
796 | 0 | } |
797 | | |
798 | | /* look if we can find a certificate that matches the key's id */ |
799 | 0 | WINPR_ASSERT(provider->p11->C_FindObjectsInit); |
800 | 0 | rv = provider->p11->C_FindObjectsInit(currentSession, certificateFilter, |
801 | 0 | ARRAYSIZE(certificateFilter)); |
802 | 0 | if (rv != CKR_OK) |
803 | 0 | { |
804 | 0 | WLog_ERR(TAG, "C_FindObjectsInit failed with %s [0x%08" PRIx32 "] for slot %d", |
805 | 0 | CK_RV_error_string(rv), rv, key->slotId); |
806 | 0 | continue; |
807 | 0 | } |
808 | | |
809 | 0 | WINPR_ASSERT(provider->p11->C_FindObjects); |
810 | 0 | rv = provider->p11->C_FindObjects(currentSession, &certObject, 1, &ncertObjects); |
811 | 0 | if (rv != CKR_OK) |
812 | 0 | { |
813 | 0 | WLog_ERR(TAG, "C_FindObjects failed with %s [0x%08" PRIx32 "] for slot %d", |
814 | 0 | CK_RV_error_string(rv), rv, currentSlot); |
815 | 0 | goto cleanup_FindObjects; |
816 | 0 | } |
817 | | |
818 | 0 | if (ncertObjects) |
819 | 0 | { |
820 | | /* sizeof keyName struct + "\<slotId>\<certId>" + keyName->pszAlgid */ |
821 | 0 | DWORD algoSz = 0; |
822 | 0 | size_t KEYNAME_SZ = |
823 | 0 | (1 + (sizeof(key->slotId) * 2) /*slotId*/ + 1 + (key->idLen * 2) + 1) * 2; |
824 | |
|
825 | 0 | convertKeyType(key->keyType, NULL, 0, &algoSz); |
826 | 0 | KEYNAME_SZ += (1ULL + algoSz) * 2ULL; |
827 | |
|
828 | 0 | keyName = calloc(1, sizeof(*keyName) + KEYNAME_SZ); |
829 | 0 | if (!keyName) |
830 | 0 | { |
831 | 0 | WLog_ERR(TAG, "unable to allocate keyName"); |
832 | 0 | goto cleanup_FindObjects; |
833 | 0 | } |
834 | 0 | keyName->dwLegacyKeySpec = AT_KEYEXCHANGE | AT_SIGNATURE; |
835 | 0 | keyName->dwFlags = NCRYPT_MACHINE_KEY_FLAG; |
836 | 0 | keyName->pszName = (LPWSTR)(keyName + 1); |
837 | 0 | wprintKeyName(keyName->pszName, key->slotId, key->id, key->idLen); |
838 | |
|
839 | 0 | keyName->pszAlgid = keyName->pszName + _wcslen(keyName->pszName) + 1; |
840 | 0 | convertKeyType(key->keyType, keyName->pszAlgid, algoSz + 1, NULL); |
841 | 0 | } |
842 | | |
843 | 0 | cleanup_FindObjects: |
844 | 0 | WINPR_ASSERT(provider->p11->C_FindObjectsFinal); |
845 | 0 | rv = provider->p11->C_FindObjectsFinal(currentSession); |
846 | 0 | if (rv != CKR_OK) |
847 | 0 | WLog_ERR(TAG, "C_FindObjectsFinal failed with %s [0x%08" PRIx32 "]", |
848 | 0 | CK_RV_error_string(rv), rv); |
849 | |
|
850 | 0 | if (keyName) |
851 | 0 | { |
852 | 0 | *ppKeyName = keyName; |
853 | 0 | state->keyIndex++; |
854 | 0 | return ERROR_SUCCESS; |
855 | 0 | } |
856 | 0 | } |
857 | | |
858 | 0 | return NTE_NO_MORE_ITEMS; |
859 | 0 | } |
860 | | |
861 | | static SECURITY_STATUS get_piv_container_name(NCryptP11KeyHandle* key, const BYTE* piv_tag, |
862 | | BYTE* output, size_t output_len) |
863 | 0 | { |
864 | 0 | CK_SLOT_INFO slot_info = { 0 }; |
865 | 0 | CK_FUNCTION_LIST_PTR p11 = NULL; |
866 | 0 | WCHAR* reader = NULL; |
867 | 0 | SCARDCONTEXT context = 0; |
868 | 0 | SCARDHANDLE card = 0; |
869 | 0 | DWORD proto = 0; |
870 | 0 | const SCARD_IO_REQUEST* pci = NULL; |
871 | 0 | BYTE buf[258] = { 0 }; |
872 | 0 | char container_name[PIV_CONTAINER_NAME_LEN + 1] = { 0 }; |
873 | 0 | DWORD buf_len = 0; |
874 | 0 | SECURITY_STATUS ret = NTE_BAD_KEY; |
875 | 0 | WinPrAsn1Decoder dec = { 0 }; |
876 | 0 | WinPrAsn1Decoder dec2 = { 0 }; |
877 | 0 | size_t len = 0; |
878 | 0 | BYTE tag = 0; |
879 | 0 | BYTE* p = NULL; |
880 | 0 | wStream s = { 0 }; |
881 | |
|
882 | 0 | WINPR_ASSERT(key); |
883 | 0 | WINPR_ASSERT(piv_tag); |
884 | | |
885 | 0 | WINPR_ASSERT(key->provider); |
886 | 0 | p11 = key->provider->p11; |
887 | 0 | WINPR_ASSERT(p11); |
888 | | |
889 | | /* Get the reader the card is in */ |
890 | 0 | WINPR_ASSERT(p11->C_GetSlotInfo); |
891 | 0 | if (p11->C_GetSlotInfo(key->slotId, &slot_info) != CKR_OK) |
892 | 0 | return NTE_BAD_KEY; |
893 | | |
894 | 0 | fix_padded_string((char*)slot_info.slotDescription, sizeof(slot_info.slotDescription)); |
895 | 0 | reader = ConvertUtf8NToWCharAlloc((char*)slot_info.slotDescription, |
896 | 0 | ARRAYSIZE(slot_info.slotDescription), NULL); |
897 | 0 | ret = NTE_NO_MEMORY; |
898 | 0 | if (!reader) |
899 | 0 | goto out; |
900 | | |
901 | 0 | ret = NTE_BAD_KEY; |
902 | 0 | if (SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &context) != SCARD_S_SUCCESS) |
903 | 0 | goto out; |
904 | | |
905 | 0 | if (SCardConnectW(context, reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_Tx, &card, &proto) != |
906 | 0 | SCARD_S_SUCCESS) |
907 | 0 | goto out; |
908 | 0 | pci = (proto == SCARD_PROTOCOL_T0) ? SCARD_PCI_T0 : SCARD_PCI_T1; |
909 | |
|
910 | 0 | buf_len = sizeof(buf); |
911 | 0 | if (SCardTransmit(card, pci, APDU_PIV_SELECT_AID, sizeof(APDU_PIV_SELECT_AID), NULL, buf, |
912 | 0 | &buf_len) != SCARD_S_SUCCESS) |
913 | 0 | goto out; |
914 | 0 | if ((buf[buf_len - 2] != 0x90 || buf[buf_len - 1] != 0) && buf[buf_len - 2] != 0x61) |
915 | 0 | goto out; |
916 | | |
917 | 0 | buf_len = sizeof(buf); |
918 | 0 | if (SCardTransmit(card, pci, APDU_PIV_GET_CHUID, sizeof(APDU_PIV_GET_CHUID), NULL, buf, |
919 | 0 | &buf_len) != SCARD_S_SUCCESS) |
920 | 0 | goto out; |
921 | 0 | if ((buf[buf_len - 2] != 0x90 || buf[buf_len - 1] != 0) && buf[buf_len - 2] != 0x61) |
922 | 0 | goto out; |
923 | | |
924 | | /* Find the GUID field in the CHUID data object */ |
925 | 0 | WinPrAsn1Decoder_InitMem(&dec, WINPR_ASN1_BER, buf, buf_len); |
926 | 0 | if (!WinPrAsn1DecReadTagAndLen(&dec, &tag, &len) || tag != 0x53) |
927 | 0 | goto out; |
928 | 0 | while (WinPrAsn1DecReadTagLenValue(&dec, &tag, &len, &dec2) && tag != 0x34) |
929 | 0 | ; |
930 | 0 | if (tag != 0x34 || len != 16) |
931 | 0 | goto out; |
932 | | |
933 | 0 | s = WinPrAsn1DecGetStream(&dec2); |
934 | 0 | p = Stream_Buffer(&s); |
935 | | |
936 | | /* Construct the value Windows would use for a PIV key's container name */ |
937 | 0 | (void)snprintf(container_name, PIV_CONTAINER_NAME_LEN + 1, |
938 | 0 | "%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x", p[3], |
939 | 0 | p[2], p[1], p[0], p[5], p[4], p[7], p[6], p[8], p[9], p[10], p[11], p[12], |
940 | 0 | piv_tag[0], piv_tag[1], piv_tag[2]); |
941 | | |
942 | | /* And convert it to UTF-16 */ |
943 | 0 | union |
944 | 0 | { |
945 | 0 | WCHAR* wc; |
946 | 0 | BYTE* b; |
947 | 0 | } cnv; |
948 | 0 | cnv.b = output; |
949 | 0 | if (ConvertUtf8NToWChar(container_name, ARRAYSIZE(container_name), cnv.wc, |
950 | 0 | output_len / sizeof(WCHAR)) > 0) |
951 | 0 | ret = ERROR_SUCCESS; |
952 | |
|
953 | 0 | out: |
954 | 0 | free(reader); |
955 | 0 | if (card) |
956 | 0 | SCardDisconnect(card, SCARD_LEAVE_CARD); |
957 | 0 | if (context) |
958 | 0 | SCardReleaseContext(context); |
959 | 0 | return ret; |
960 | 0 | } |
961 | | |
962 | | static SECURITY_STATUS check_for_piv_container_name(NCryptP11KeyHandle* key, BYTE* pbOutput, |
963 | | DWORD cbOutput, DWORD* pcbResult, char* label, |
964 | | size_t label_len) |
965 | 0 | { |
966 | 0 | for (size_t i = 0; i < ARRAYSIZE(piv_cert_tags); i++) |
967 | 0 | { |
968 | 0 | const piv_cert_tags_t* cur = &piv_cert_tags[i]; |
969 | 0 | if (strncmp(label, cur->label, label_len) == 0) |
970 | 0 | { |
971 | 0 | *pcbResult = (PIV_CONTAINER_NAME_LEN + 1) * sizeof(WCHAR); |
972 | 0 | if (!pbOutput) |
973 | 0 | return ERROR_SUCCESS; |
974 | 0 | else if (cbOutput < (PIV_CONTAINER_NAME_LEN + 1) * sizeof(WCHAR)) |
975 | 0 | return NTE_NO_MEMORY; |
976 | 0 | else |
977 | 0 | return get_piv_container_name(key, cur->tag, pbOutput, cbOutput); |
978 | 0 | } |
979 | 0 | } |
980 | 0 | return NTE_NOT_FOUND; |
981 | 0 | } |
982 | | |
983 | | static SECURITY_STATUS NCryptP11KeyGetProperties(NCryptP11KeyHandle* keyHandle, |
984 | | NCryptKeyGetPropertyEnum property, PBYTE pbOutput, |
985 | | DWORD cbOutput, DWORD* pcbResult, |
986 | | WINPR_ATTR_UNUSED DWORD dwFlags) |
987 | 0 | { |
988 | 0 | SECURITY_STATUS ret = NTE_FAIL; |
989 | 0 | CK_RV rv = 0; |
990 | 0 | CK_SESSION_HANDLE session = 0; |
991 | 0 | CK_OBJECT_HANDLE objectHandle = 0; |
992 | 0 | CK_ULONG objectCount = 0; |
993 | 0 | NCryptP11ProviderHandle* provider = NULL; |
994 | 0 | CK_OBJECT_CLASS oclass = CKO_CERTIFICATE; |
995 | 0 | CK_CERTIFICATE_TYPE ctype = CKC_X_509; |
996 | 0 | CK_ATTRIBUTE certificateFilter[] = { { CKA_CLASS, &oclass, sizeof(oclass) }, |
997 | 0 | { CKA_CERTIFICATE_TYPE, &ctype, sizeof(ctype) }, |
998 | 0 | { CKA_ID, keyHandle->keyCertId, |
999 | 0 | keyHandle->keyCertIdLen } }; |
1000 | 0 | CK_ATTRIBUTE* objectFilter = certificateFilter; |
1001 | 0 | CK_ULONG objectFilterLen = ARRAYSIZE(certificateFilter); |
1002 | |
|
1003 | 0 | WINPR_ASSERT(keyHandle); |
1004 | 0 | provider = keyHandle->provider; |
1005 | 0 | WINPR_ASSERT(provider); |
1006 | | |
1007 | 0 | switch (property) |
1008 | |
|
1009 | 0 | { |
1010 | 0 | case NCRYPT_PROPERTY_CERTIFICATE: |
1011 | 0 | case NCRYPT_PROPERTY_NAME: |
1012 | 0 | break; |
1013 | 0 | case NCRYPT_PROPERTY_READER: |
1014 | 0 | { |
1015 | 0 | CK_SLOT_INFO slotInfo; |
1016 | |
|
1017 | 0 | WINPR_ASSERT(provider->p11->C_GetSlotInfo); |
1018 | 0 | rv = provider->p11->C_GetSlotInfo(keyHandle->slotId, &slotInfo); |
1019 | 0 | if (rv != CKR_OK) |
1020 | 0 | return NTE_BAD_KEY; |
1021 | | |
1022 | 0 | #define SLOT_DESC_SZ sizeof(slotInfo.slotDescription) |
1023 | 0 | fix_padded_string((char*)slotInfo.slotDescription, SLOT_DESC_SZ); |
1024 | 0 | const size_t len = 2ULL * (strnlen((char*)slotInfo.slotDescription, SLOT_DESC_SZ) + 1); |
1025 | 0 | if (len > UINT32_MAX) |
1026 | 0 | return NTE_BAD_DATA; |
1027 | 0 | *pcbResult = (UINT32)len; |
1028 | 0 | if (pbOutput) |
1029 | 0 | { |
1030 | 0 | union |
1031 | 0 | { |
1032 | 0 | WCHAR* wc; |
1033 | 0 | BYTE* b; |
1034 | 0 | } cnv; |
1035 | 0 | cnv.b = pbOutput; |
1036 | 0 | if (cbOutput < *pcbResult) |
1037 | 0 | return NTE_NO_MEMORY; |
1038 | | |
1039 | 0 | if (ConvertUtf8ToWChar((char*)slotInfo.slotDescription, cnv.wc, |
1040 | 0 | cbOutput / sizeof(WCHAR)) < 0) |
1041 | 0 | return NTE_NO_MEMORY; |
1042 | 0 | } |
1043 | 0 | return ERROR_SUCCESS; |
1044 | 0 | } |
1045 | 0 | case NCRYPT_PROPERTY_SLOTID: |
1046 | 0 | { |
1047 | 0 | *pcbResult = 4; |
1048 | 0 | if (pbOutput) |
1049 | 0 | { |
1050 | 0 | UINT32* ptr = (UINT32*)pbOutput; |
1051 | |
|
1052 | 0 | if (cbOutput < 4) |
1053 | 0 | return NTE_NO_MEMORY; |
1054 | 0 | if (keyHandle->slotId > UINT32_MAX) |
1055 | 0 | { |
1056 | 0 | ret = NTE_BAD_DATA; |
1057 | 0 | goto out_final; |
1058 | 0 | } |
1059 | 0 | *ptr = (UINT32)keyHandle->slotId; |
1060 | 0 | } |
1061 | 0 | return ERROR_SUCCESS; |
1062 | 0 | } |
1063 | 0 | case NCRYPT_PROPERTY_UNKNOWN: |
1064 | 0 | default: |
1065 | 0 | return NTE_NOT_SUPPORTED; |
1066 | 0 | } |
1067 | | |
1068 | 0 | WINPR_ASSERT(provider->p11->C_OpenSession); |
1069 | 0 | rv = provider->p11->C_OpenSession(keyHandle->slotId, CKF_SERIAL_SESSION, NULL, NULL, &session); |
1070 | 0 | if (rv != CKR_OK) |
1071 | 0 | { |
1072 | 0 | WLog_ERR(TAG, "error opening session on slot %d", keyHandle->slotId); |
1073 | 0 | return NTE_FAIL; |
1074 | 0 | } |
1075 | | |
1076 | 0 | WINPR_ASSERT(provider->p11->C_FindObjectsInit); |
1077 | 0 | rv = provider->p11->C_FindObjectsInit(session, objectFilter, objectFilterLen); |
1078 | 0 | if (rv != CKR_OK) |
1079 | 0 | { |
1080 | 0 | WLog_ERR(TAG, "unable to initiate search for slot %d", keyHandle->slotId); |
1081 | 0 | goto out; |
1082 | 0 | } |
1083 | | |
1084 | 0 | WINPR_ASSERT(provider->p11->C_FindObjects); |
1085 | 0 | rv = provider->p11->C_FindObjects(session, &objectHandle, 1, &objectCount); |
1086 | 0 | if (rv != CKR_OK) |
1087 | 0 | { |
1088 | 0 | WLog_ERR(TAG, "unable to findObjects for slot %d", keyHandle->slotId); |
1089 | 0 | goto out_final; |
1090 | 0 | } |
1091 | 0 | if (!objectCount) |
1092 | 0 | { |
1093 | 0 | ret = NTE_NOT_FOUND; |
1094 | 0 | goto out_final; |
1095 | 0 | } |
1096 | | |
1097 | 0 | switch (property) |
1098 | 0 | { |
1099 | 0 | case NCRYPT_PROPERTY_CERTIFICATE: |
1100 | 0 | { |
1101 | 0 | CK_ATTRIBUTE certValue = { CKA_VALUE, pbOutput, cbOutput }; |
1102 | |
|
1103 | 0 | WINPR_ASSERT(provider->p11->C_GetAttributeValue); |
1104 | 0 | rv = provider->p11->C_GetAttributeValue(session, objectHandle, &certValue, 1); |
1105 | 0 | if (rv != CKR_OK) |
1106 | 0 | { |
1107 | | // TODO: do a kind of translation from CKR_* to NTE_* |
1108 | 0 | } |
1109 | |
|
1110 | 0 | if (certValue.ulValueLen > UINT32_MAX) |
1111 | 0 | { |
1112 | 0 | ret = NTE_BAD_DATA; |
1113 | 0 | goto out_final; |
1114 | 0 | } |
1115 | 0 | *pcbResult = (UINT32)certValue.ulValueLen; |
1116 | 0 | ret = ERROR_SUCCESS; |
1117 | 0 | break; |
1118 | 0 | } |
1119 | 0 | case NCRYPT_PROPERTY_NAME: |
1120 | 0 | { |
1121 | 0 | CK_ATTRIBUTE attr = { CKA_LABEL, NULL, 0 }; |
1122 | 0 | char* label = NULL; |
1123 | |
|
1124 | 0 | WINPR_ASSERT(provider->p11->C_GetAttributeValue); |
1125 | 0 | rv = provider->p11->C_GetAttributeValue(session, objectHandle, &attr, 1); |
1126 | 0 | if (rv == CKR_OK) |
1127 | 0 | { |
1128 | 0 | label = calloc(1, attr.ulValueLen); |
1129 | 0 | if (!label) |
1130 | 0 | { |
1131 | 0 | ret = NTE_NO_MEMORY; |
1132 | 0 | break; |
1133 | 0 | } |
1134 | | |
1135 | 0 | attr.pValue = label; |
1136 | 0 | rv = provider->p11->C_GetAttributeValue(session, objectHandle, &attr, 1); |
1137 | 0 | } |
1138 | | |
1139 | 0 | if (rv == CKR_OK) |
1140 | 0 | { |
1141 | | /* Check if we have a PIV card */ |
1142 | 0 | ret = check_for_piv_container_name(keyHandle, pbOutput, cbOutput, pcbResult, label, |
1143 | 0 | attr.ulValueLen); |
1144 | | |
1145 | | /* Otherwise, at least for GIDS cards the label will be the correct value */ |
1146 | 0 | if (ret == NTE_NOT_FOUND) |
1147 | 0 | { |
1148 | 0 | union |
1149 | 0 | { |
1150 | 0 | WCHAR* wc; |
1151 | 0 | BYTE* b; |
1152 | 0 | } cnv; |
1153 | 0 | const size_t olen = pbOutput ? cbOutput / sizeof(WCHAR) : 0; |
1154 | 0 | cnv.b = pbOutput; |
1155 | 0 | SSIZE_T size = ConvertUtf8NToWChar(label, attr.ulValueLen, cnv.wc, olen); |
1156 | 0 | if (size < 0) |
1157 | 0 | ret = ERROR_CONVERT_TO_LARGE; |
1158 | 0 | else |
1159 | 0 | ret = ERROR_SUCCESS; |
1160 | 0 | } |
1161 | 0 | } |
1162 | |
|
1163 | 0 | free(label); |
1164 | 0 | break; |
1165 | 0 | } |
1166 | 0 | default: |
1167 | 0 | ret = NTE_NOT_SUPPORTED; |
1168 | 0 | break; |
1169 | 0 | } |
1170 | | |
1171 | 0 | out_final: |
1172 | 0 | WINPR_ASSERT(provider->p11->C_FindObjectsFinal); |
1173 | 0 | rv = provider->p11->C_FindObjectsFinal(session); |
1174 | 0 | if (rv != CKR_OK) |
1175 | 0 | { |
1176 | 0 | WLog_ERR(TAG, "error in C_FindObjectsFinal() for slot %d", keyHandle->slotId); |
1177 | 0 | } |
1178 | 0 | out: |
1179 | 0 | WINPR_ASSERT(provider->p11->C_CloseSession); |
1180 | 0 | rv = provider->p11->C_CloseSession(session); |
1181 | 0 | if (rv != CKR_OK) |
1182 | 0 | { |
1183 | 0 | WLog_ERR(TAG, "error in C_CloseSession() for slot %d", keyHandle->slotId); |
1184 | 0 | } |
1185 | 0 | return ret; |
1186 | 0 | } |
1187 | | |
1188 | | static SECURITY_STATUS NCryptP11GetProperty(NCRYPT_HANDLE hObject, NCryptKeyGetPropertyEnum prop, |
1189 | | PBYTE pbOutput, DWORD cbOutput, DWORD* pcbResult, |
1190 | | DWORD dwFlags) |
1191 | 0 | { |
1192 | 0 | NCryptBaseHandle* base = (NCryptBaseHandle*)hObject; |
1193 | |
|
1194 | 0 | WINPR_ASSERT(base); |
1195 | 0 | switch (base->type) |
1196 | 0 | { |
1197 | 0 | case WINPR_NCRYPT_PROVIDER: |
1198 | 0 | return ERROR_CALL_NOT_IMPLEMENTED; |
1199 | 0 | case WINPR_NCRYPT_KEY: |
1200 | 0 | return NCryptP11KeyGetProperties((NCryptP11KeyHandle*)hObject, prop, pbOutput, cbOutput, |
1201 | 0 | pcbResult, dwFlags); |
1202 | 0 | default: |
1203 | 0 | return ERROR_INVALID_HANDLE; |
1204 | 0 | } |
1205 | 0 | return ERROR_SUCCESS; |
1206 | 0 | } |
1207 | | |
1208 | | static SECURITY_STATUS NCryptP11OpenKey(NCRYPT_PROV_HANDLE hProvider, NCRYPT_KEY_HANDLE* phKey, |
1209 | | LPCWSTR pszKeyName, WINPR_ATTR_UNUSED DWORD dwLegacyKeySpec, |
1210 | | WINPR_ATTR_UNUSED DWORD dwFlags) |
1211 | 0 | { |
1212 | 0 | SECURITY_STATUS ret = 0; |
1213 | 0 | CK_SLOT_ID slotId = 0; |
1214 | 0 | CK_BYTE keyCertId[64] = { 0 }; |
1215 | 0 | CK_ULONG keyCertIdLen = 0; |
1216 | 0 | NCryptP11KeyHandle* keyHandle = NULL; |
1217 | |
|
1218 | 0 | ret = parseKeyName(pszKeyName, &slotId, keyCertId, &keyCertIdLen); |
1219 | 0 | if (ret != ERROR_SUCCESS) |
1220 | 0 | return ret; |
1221 | | |
1222 | 0 | keyHandle = (NCryptP11KeyHandle*)ncrypt_new_handle( |
1223 | 0 | WINPR_NCRYPT_KEY, sizeof(*keyHandle), NCryptP11GetProperty, winpr_NCryptDefault_dtor); |
1224 | 0 | if (!keyHandle) |
1225 | 0 | return NTE_NO_MEMORY; |
1226 | | |
1227 | 0 | keyHandle->provider = (NCryptP11ProviderHandle*)hProvider; |
1228 | 0 | keyHandle->slotId = slotId; |
1229 | 0 | memcpy(keyHandle->keyCertId, keyCertId, sizeof(keyCertId)); |
1230 | 0 | keyHandle->keyCertIdLen = keyCertIdLen; |
1231 | 0 | *phKey = (NCRYPT_KEY_HANDLE)keyHandle; |
1232 | 0 | return ERROR_SUCCESS; |
1233 | 0 | } |
1234 | | |
1235 | | static SECURITY_STATUS initialize_pkcs11(HANDLE handle, |
1236 | | CK_RV (*c_get_function_list)(CK_FUNCTION_LIST_PTR_PTR), |
1237 | | NCRYPT_PROV_HANDLE* phProvider) |
1238 | 0 | { |
1239 | 0 | SECURITY_STATUS status = ERROR_SUCCESS; |
1240 | 0 | NCryptP11ProviderHandle* ret = NULL; |
1241 | 0 | CK_RV rv = 0; |
1242 | |
|
1243 | 0 | WINPR_ASSERT(c_get_function_list); |
1244 | 0 | WINPR_ASSERT(phProvider); |
1245 | | |
1246 | 0 | ret = (NCryptP11ProviderHandle*)ncrypt_new_handle( |
1247 | 0 | WINPR_NCRYPT_PROVIDER, sizeof(*ret), NCryptP11GetProperty, NCryptP11StorageProvider_dtor); |
1248 | 0 | if (!ret) |
1249 | 0 | return NTE_NO_MEMORY; |
1250 | | |
1251 | 0 | ret->library = handle; |
1252 | 0 | ret->baseProvider.enumKeysFn = NCryptP11EnumKeys; |
1253 | 0 | ret->baseProvider.openKeyFn = NCryptP11OpenKey; |
1254 | |
|
1255 | 0 | rv = c_get_function_list(&ret->p11); |
1256 | 0 | if (rv != CKR_OK) |
1257 | 0 | { |
1258 | 0 | status = NTE_PROVIDER_DLL_FAIL; |
1259 | 0 | goto fail; |
1260 | 0 | } |
1261 | | |
1262 | 0 | WINPR_ASSERT(ret->p11); |
1263 | 0 | WINPR_ASSERT(ret->p11->C_Initialize); |
1264 | 0 | rv = ret->p11->C_Initialize(NULL); |
1265 | 0 | if (rv != CKR_OK) |
1266 | 0 | { |
1267 | 0 | status = NTE_PROVIDER_DLL_FAIL; |
1268 | 0 | goto fail; |
1269 | 0 | } |
1270 | | |
1271 | 0 | *phProvider = (NCRYPT_PROV_HANDLE)ret; |
1272 | |
|
1273 | 0 | fail: |
1274 | 0 | if (status != ERROR_SUCCESS) |
1275 | 0 | ret->baseProvider.baseHandle.releaseFn((NCRYPT_HANDLE)ret); |
1276 | 0 | return status; |
1277 | 0 | } |
1278 | | |
1279 | | SECURITY_STATUS NCryptOpenP11StorageProviderEx(NCRYPT_PROV_HANDLE* phProvider, |
1280 | | WINPR_ATTR_UNUSED LPCWSTR pszProviderName, |
1281 | | WINPR_ATTR_UNUSED DWORD dwFlags, LPCSTR* modulePaths) |
1282 | 0 | { |
1283 | 0 | SECURITY_STATUS status = ERROR_INVALID_PARAMETER; |
1284 | 0 | LPCSTR defaultPaths[] = { "p11-kit-proxy.so", "opensc-pkcs11.so", NULL }; |
1285 | |
|
1286 | 0 | if (!phProvider) |
1287 | 0 | return ERROR_INVALID_PARAMETER; |
1288 | | |
1289 | 0 | if (!modulePaths) |
1290 | 0 | modulePaths = defaultPaths; |
1291 | |
|
1292 | 0 | while (*modulePaths) |
1293 | 0 | { |
1294 | 0 | const char* modulePath = *modulePaths++; |
1295 | 0 | HANDLE library = LoadLibrary(modulePath); |
1296 | 0 | typedef CK_RV (*c_get_function_list_t)(CK_FUNCTION_LIST_PTR_PTR); |
1297 | 0 | NCryptP11ProviderHandle* provider = NULL; |
1298 | |
|
1299 | 0 | WLog_DBG(TAG, "Trying pkcs11 module '%s'", modulePath); |
1300 | 0 | if (!library) |
1301 | 0 | { |
1302 | 0 | status = NTE_PROV_DLL_NOT_FOUND; |
1303 | 0 | goto out_load_library; |
1304 | 0 | } |
1305 | | |
1306 | 0 | c_get_function_list_t c_get_function_list = |
1307 | 0 | GetProcAddressAs(library, "C_GetFunctionList", c_get_function_list_t); |
1308 | |
|
1309 | 0 | if (!c_get_function_list) |
1310 | 0 | { |
1311 | 0 | status = NTE_PROV_TYPE_ENTRY_BAD; |
1312 | 0 | goto out_load_library; |
1313 | 0 | } |
1314 | | |
1315 | 0 | status = initialize_pkcs11(library, c_get_function_list, phProvider); |
1316 | 0 | if (status != ERROR_SUCCESS) |
1317 | 0 | { |
1318 | 0 | status = NTE_PROVIDER_DLL_FAIL; |
1319 | 0 | goto out_load_library; |
1320 | 0 | } |
1321 | | |
1322 | 0 | provider = (NCryptP11ProviderHandle*)*phProvider; |
1323 | 0 | provider->modulePath = _strdup(modulePath); |
1324 | 0 | if (!provider->modulePath) |
1325 | 0 | { |
1326 | 0 | status = NTE_NO_MEMORY; |
1327 | 0 | goto out_load_library; |
1328 | 0 | } |
1329 | | |
1330 | 0 | WLog_DBG(TAG, "module '%s' loaded", modulePath); |
1331 | 0 | return ERROR_SUCCESS; |
1332 | | |
1333 | 0 | out_load_library: |
1334 | 0 | if (library) |
1335 | 0 | FreeLibrary(library); |
1336 | 0 | } |
1337 | | |
1338 | 0 | return status; |
1339 | 0 | } |
1340 | | |
1341 | | const char* NCryptGetModulePath(NCRYPT_PROV_HANDLE phProvider) |
1342 | 0 | { |
1343 | 0 | NCryptP11ProviderHandle* provider = (NCryptP11ProviderHandle*)phProvider; |
1344 | |
|
1345 | 0 | WINPR_ASSERT(provider); |
1346 | | |
1347 | 0 | return provider->modulePath; |
1348 | 0 | } |