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