/src/FreeRDP/libfreerdp/core/smartcardlogon.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Logging in with smartcards |
4 | | * |
5 | | * Copyright 2022 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 | | #include <string.h> |
20 | | |
21 | | #include <winpr/error.h> |
22 | | #include <winpr/ncrypt.h> |
23 | | #include <winpr/string.h> |
24 | | #include <winpr/wlog.h> |
25 | | #include <winpr/crypto.h> |
26 | | #include <winpr/path.h> |
27 | | |
28 | | #include <freerdp/log.h> |
29 | | #include <freerdp/freerdp.h> |
30 | | #include <winpr/print.h> |
31 | | |
32 | | #include <freerdp/utils/smartcardlogon.h> |
33 | | #include <freerdp/crypto/crypto.h> |
34 | | |
35 | | #include <openssl/obj_mac.h> |
36 | | |
37 | | #define TAG FREERDP_TAG("smartcardlogon") |
38 | | |
39 | | struct SmartcardKeyInfo_st |
40 | | { |
41 | | char* certPath; |
42 | | char* keyPath; |
43 | | }; |
44 | | |
45 | | static void delete_file(char* path) |
46 | 0 | { |
47 | 0 | if (!path) |
48 | 0 | return; |
49 | | |
50 | | /* Overwrite data in files before deletion */ |
51 | 0 | { |
52 | 0 | FILE* fp = winpr_fopen(path, "r+"); |
53 | 0 | if (fp) |
54 | 0 | { |
55 | 0 | const char buffer[8192] = { 0 }; |
56 | 0 | INT64 size = 0; |
57 | 0 | int rs = _fseeki64(fp, 0, SEEK_END); |
58 | 0 | if (rs == 0) |
59 | 0 | size = _ftelli64(fp); |
60 | 0 | (void)_fseeki64(fp, 0, SEEK_SET); |
61 | |
|
62 | 0 | for (INT64 x = 0; x < size; x += sizeof(buffer)) |
63 | 0 | { |
64 | 0 | const size_t dnmemb = (size_t)(size - x); |
65 | 0 | const size_t nmemb = MIN(sizeof(buffer), dnmemb); |
66 | 0 | const size_t count = fwrite(buffer, nmemb, 1, fp); |
67 | 0 | if (count != 1) |
68 | 0 | break; |
69 | 0 | } |
70 | |
|
71 | 0 | (void)fclose(fp); |
72 | 0 | } |
73 | 0 | } |
74 | |
|
75 | 0 | winpr_DeleteFile(path); |
76 | 0 | free(path); |
77 | 0 | } |
78 | | |
79 | | static void smartcardKeyInfo_Free(SmartcardKeyInfo* key_info) |
80 | 0 | { |
81 | 0 | if (!key_info) |
82 | 0 | return; |
83 | | |
84 | 0 | delete_file(key_info->certPath); |
85 | 0 | delete_file(key_info->keyPath); |
86 | |
|
87 | 0 | free(key_info); |
88 | 0 | } |
89 | | |
90 | | void smartcardCertInfo_Free(SmartcardCertInfo* scCert) |
91 | 0 | { |
92 | 0 | if (!scCert) |
93 | 0 | return; |
94 | | |
95 | 0 | free(scCert->csp); |
96 | 0 | free(scCert->reader); |
97 | 0 | freerdp_certificate_free(scCert->certificate); |
98 | 0 | free(scCert->pkinitArgs); |
99 | 0 | free(scCert->keyName); |
100 | 0 | free(scCert->containerName); |
101 | 0 | free(scCert->upn); |
102 | 0 | free(scCert->userHint); |
103 | 0 | free(scCert->domainHint); |
104 | 0 | free(scCert->subject); |
105 | 0 | free(scCert->issuer); |
106 | 0 | smartcardKeyInfo_Free(scCert->key_info); |
107 | |
|
108 | 0 | free(scCert); |
109 | 0 | } |
110 | | |
111 | | void smartcardCertList_Free(SmartcardCertInfo** cert_list, size_t count) |
112 | 0 | { |
113 | 0 | if (!cert_list) |
114 | 0 | return; |
115 | | |
116 | 0 | for (size_t i = 0; i < count; i++) |
117 | 0 | { |
118 | 0 | SmartcardCertInfo* cert = cert_list[i]; |
119 | 0 | smartcardCertInfo_Free(cert); |
120 | 0 | } |
121 | |
|
122 | 0 | free((void*)cert_list); |
123 | 0 | } |
124 | | |
125 | | static BOOL add_cert_to_list(SmartcardCertInfo*** certInfoList, size_t* count, |
126 | | SmartcardCertInfo* certInfo) |
127 | 0 | { |
128 | 0 | size_t curCount = *count; |
129 | 0 | SmartcardCertInfo** curInfoList = *certInfoList; |
130 | | |
131 | | /* Check if the certificate is already in the list */ |
132 | 0 | for (size_t i = 0; i < curCount; ++i) |
133 | 0 | { |
134 | 0 | if (_wcscmp(curInfoList[i]->containerName, certInfo->containerName) == 0) |
135 | 0 | { |
136 | 0 | smartcardCertInfo_Free(certInfo); |
137 | 0 | return TRUE; |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | 0 | { |
142 | 0 | SmartcardCertInfo** tmpInfoList = (SmartcardCertInfo**)realloc( |
143 | 0 | (void*)curInfoList, sizeof(SmartcardCertInfo*) * (curCount + 1)); |
144 | 0 | if (!tmpInfoList) |
145 | 0 | { |
146 | 0 | WLog_ERR(TAG, "unable to reallocate certs"); |
147 | 0 | return FALSE; |
148 | 0 | } |
149 | 0 | curInfoList = tmpInfoList; |
150 | 0 | } |
151 | | |
152 | 0 | curInfoList[curCount++] = certInfo; |
153 | 0 | *certInfoList = curInfoList; |
154 | 0 | *count = curCount; |
155 | 0 | return TRUE; |
156 | 0 | } |
157 | | |
158 | | static BOOL treat_sc_cert(SmartcardCertInfo* scCert) |
159 | 0 | { |
160 | 0 | WINPR_ASSERT(scCert); |
161 | | |
162 | 0 | scCert->upn = freerdp_certificate_get_upn(scCert->certificate); |
163 | 0 | if (!scCert->upn) |
164 | 0 | { |
165 | 0 | WLog_DBG(TAG, "%s has no UPN, trying emailAddress", scCert->keyName); |
166 | 0 | scCert->upn = freerdp_certificate_get_email(scCert->certificate); |
167 | 0 | } |
168 | |
|
169 | 0 | if (scCert->upn) |
170 | 0 | { |
171 | 0 | size_t userLen = 0; |
172 | 0 | const char* atPos = strchr(scCert->upn, '@'); |
173 | |
|
174 | 0 | if (!atPos) |
175 | 0 | { |
176 | 0 | WLog_ERR(TAG, "invalid UPN, for key %s (no @)", scCert->keyName); |
177 | 0 | return FALSE; |
178 | 0 | } |
179 | | |
180 | 0 | userLen = (size_t)(atPos - scCert->upn); |
181 | 0 | scCert->userHint = malloc(userLen + 1); |
182 | 0 | scCert->domainHint = _strdup(atPos + 1); |
183 | |
|
184 | 0 | if (!scCert->userHint || !scCert->domainHint) |
185 | 0 | { |
186 | 0 | WLog_ERR(TAG, "error allocating userHint or domainHint, for key %s", scCert->keyName); |
187 | 0 | return FALSE; |
188 | 0 | } |
189 | | |
190 | 0 | memcpy(scCert->userHint, scCert->upn, userLen); |
191 | 0 | scCert->userHint[userLen] = 0; |
192 | 0 | } |
193 | | |
194 | 0 | scCert->subject = freerdp_certificate_get_subject(scCert->certificate); |
195 | 0 | scCert->issuer = freerdp_certificate_get_issuer(scCert->certificate); |
196 | 0 | return TRUE; |
197 | 0 | } |
198 | | |
199 | | static BOOL set_info_certificate(SmartcardCertInfo* cert, BYTE* certBytes, DWORD cbCertBytes, |
200 | | const char* userFilter, const char* domainFilter) |
201 | 0 | { |
202 | 0 | if (!winpr_Digest(WINPR_MD_SHA1, certBytes, cbCertBytes, cert->sha1Hash, |
203 | 0 | sizeof(cert->sha1Hash))) |
204 | 0 | { |
205 | 0 | WLog_ERR(TAG, "unable to compute certificate sha1 for key %s", cert->keyName); |
206 | 0 | return FALSE; |
207 | 0 | } |
208 | | |
209 | 0 | cert->certificate = freerdp_certificate_new_from_der(certBytes, cbCertBytes); |
210 | 0 | if (!cert->certificate) |
211 | 0 | { |
212 | 0 | WLog_ERR(TAG, "unable to parse X509 certificate for key %s", cert->keyName); |
213 | 0 | return FALSE; |
214 | 0 | } |
215 | | |
216 | 0 | if (!freerdp_certificate_check_eku(cert->certificate, NID_ms_smartcard_login)) |
217 | 0 | { |
218 | 0 | WLog_DBG(TAG, "discarding certificate without Smartcard Login EKU for key %s", |
219 | 0 | cert->keyName); |
220 | 0 | return FALSE; |
221 | 0 | } |
222 | | |
223 | 0 | if (!treat_sc_cert(cert)) |
224 | 0 | { |
225 | 0 | WLog_DBG(TAG, "error treating cert"); |
226 | 0 | return FALSE; |
227 | 0 | } |
228 | | |
229 | 0 | if (userFilter && (!cert->upn || (strcmp(cert->upn, userFilter) != 0))) |
230 | 0 | { |
231 | 0 | if (cert->userHint && strcmp(cert->userHint, userFilter) != 0) |
232 | 0 | { |
233 | 0 | WLog_DBG(TAG, "discarding non matching cert by user %s@%s", cert->userHint, |
234 | 0 | cert->domainHint); |
235 | 0 | return FALSE; |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | 0 | if (domainFilter && cert->domainHint && strcmp(cert->domainHint, domainFilter) != 0) |
240 | 0 | { |
241 | 0 | WLog_DBG(TAG, "discarding non matching cert by domain(%s) %s@%s", domainFilter, |
242 | 0 | cert->userHint, cert->domainHint); |
243 | 0 | return FALSE; |
244 | 0 | } |
245 | | |
246 | 0 | return TRUE; |
247 | 0 | } |
248 | | |
249 | | #ifndef _WIN32 |
250 | | static BOOL build_pkinit_args(NCRYPT_PROV_HANDLE provider, SmartcardCertInfo* scCert) |
251 | 0 | { |
252 | | /* pkinit args only under windows |
253 | | * PKCS11:module_name=opensc-pkcs11.so |
254 | | */ |
255 | 0 | const char* pkModule = winpr_NCryptGetModulePath(provider); |
256 | 0 | size_t size = 0; |
257 | |
|
258 | 0 | if (winpr_asprintf(&scCert->pkinitArgs, &size, "PKCS11:module_name=%s:slotid=%" PRIu16, |
259 | 0 | pkModule, (UINT16)scCert->slotId) <= 0) |
260 | 0 | return FALSE; |
261 | 0 | return TRUE; |
262 | 0 | } |
263 | | #endif /* _WIN32 */ |
264 | | |
265 | | #ifdef _WIN32 |
266 | | static BOOL list_capi_provider_keys(const rdpSettings* settings, LPCWSTR csp, LPCWSTR scope, |
267 | | const char* userFilter, const char* domainFilter, |
268 | | SmartcardCertInfo*** pcerts, size_t* pcount) |
269 | | { |
270 | | BOOL ret = FALSE; |
271 | | HCRYPTKEY hKey = 0; |
272 | | HCRYPTPROV hProvider = 0; |
273 | | SmartcardCertInfo* cert = NULL; |
274 | | BYTE* certBytes = NULL; |
275 | | CHAR* readerName = NULL; |
276 | | |
277 | | if (!CryptAcquireContextW(&hProvider, scope, csp, PROV_RSA_FULL, CRYPT_SILENT)) |
278 | | { |
279 | | WLog_DBG(TAG, "Unable to acquire context: %d", GetLastError()); |
280 | | goto out; |
281 | | } |
282 | | |
283 | | cert = calloc(1, sizeof(SmartcardCertInfo)); |
284 | | if (!cert) |
285 | | goto out; |
286 | | |
287 | | cert->csp = _wcsdup(csp); |
288 | | if (!cert->csp) |
289 | | goto out; |
290 | | |
291 | | /* ====== retrieve key's reader ====== */ |
292 | | DWORD dwDataLen = 0; |
293 | | if (!CryptGetProvParam(hProvider, PP_SMARTCARD_READER, NULL, &dwDataLen, 0)) |
294 | | { |
295 | | WLog_DBG(TAG, "Unable to get provider param: %d", GetLastError()); |
296 | | goto out; |
297 | | } |
298 | | |
299 | | readerName = malloc(dwDataLen); |
300 | | if (!readerName) |
301 | | goto out; |
302 | | |
303 | | if (!CryptGetProvParam(hProvider, PP_SMARTCARD_READER, readerName, &dwDataLen, 0)) |
304 | | { |
305 | | WLog_DBG(TAG, "Unable to get reader name: %d", GetLastError()); |
306 | | goto out; |
307 | | } |
308 | | |
309 | | cert->reader = ConvertUtf8ToWCharAlloc(readerName, NULL); |
310 | | if (!cert->reader) |
311 | | goto out; |
312 | | |
313 | | /* ====== retrieve key container name ====== */ |
314 | | dwDataLen = 0; |
315 | | if (!CryptGetProvParam(hProvider, PP_CONTAINER, NULL, &dwDataLen, 0)) |
316 | | { |
317 | | WLog_DBG(TAG, "Unable to get provider param: %d", GetLastError()); |
318 | | goto out; |
319 | | } |
320 | | |
321 | | cert->keyName = malloc(dwDataLen); |
322 | | if (!cert->keyName) |
323 | | goto out; |
324 | | |
325 | | if (!CryptGetProvParam(hProvider, PP_CONTAINER, cert->keyName, &dwDataLen, 0)) |
326 | | { |
327 | | WLog_DBG(TAG, "Unable to get container name: %d", GetLastError()); |
328 | | goto out; |
329 | | } |
330 | | |
331 | | cert->containerName = ConvertUtf8ToWCharAlloc(cert->keyName, NULL); |
332 | | if (!cert->containerName) |
333 | | goto out; |
334 | | |
335 | | /* ========= retrieve the certificate ===============*/ |
336 | | if (!CryptGetUserKey(hProvider, AT_KEYEXCHANGE, &hKey)) |
337 | | { |
338 | | WLog_DBG(TAG, "Unable to get user key for %s: %d", cert->keyName, GetLastError()); |
339 | | goto out; |
340 | | } |
341 | | |
342 | | dwDataLen = 0; |
343 | | if (!CryptGetKeyParam(hKey, KP_CERTIFICATE, NULL, &dwDataLen, 0)) |
344 | | { |
345 | | WLog_DBG(TAG, "Unable to get key param for key %s: %d", cert->keyName, GetLastError()); |
346 | | goto out; |
347 | | } |
348 | | |
349 | | certBytes = malloc(dwDataLen); |
350 | | if (!certBytes) |
351 | | { |
352 | | WLog_ERR(TAG, "unable to allocate %" PRIu32 " certBytes for key %s", dwDataLen, |
353 | | cert->keyName); |
354 | | goto out; |
355 | | } |
356 | | |
357 | | if (!CryptGetKeyParam(hKey, KP_CERTIFICATE, certBytes, &dwDataLen, 0)) |
358 | | { |
359 | | WLog_ERR(TAG, "unable to retrieve certificate for key %s", cert->keyName); |
360 | | goto out; |
361 | | } |
362 | | |
363 | | if (!set_info_certificate(cert, certBytes, dwDataLen, userFilter, domainFilter)) |
364 | | goto out; |
365 | | |
366 | | if (!add_cert_to_list(pcerts, pcount, cert)) |
367 | | goto out; |
368 | | |
369 | | ret = TRUE; |
370 | | |
371 | | out: |
372 | | free(readerName); |
373 | | free(certBytes); |
374 | | if (hKey) |
375 | | CryptDestroyKey(hKey); |
376 | | if (hProvider) |
377 | | CryptReleaseContext(hProvider, 0); |
378 | | if (!ret) |
379 | | smartcardCertInfo_Free(cert); |
380 | | return ret; |
381 | | } |
382 | | #endif /* _WIN32 */ |
383 | | |
384 | | static BOOL list_provider_keys(WINPR_ATTR_UNUSED const rdpSettings* settings, |
385 | | NCRYPT_PROV_HANDLE provider, LPCWSTR csp, LPCWSTR scope, |
386 | | const char* userFilter, const char* domainFilter, |
387 | | SmartcardCertInfo*** pcerts, size_t* pcount) |
388 | 0 | { |
389 | 0 | BOOL ret = FALSE; |
390 | 0 | NCryptKeyName* keyName = NULL; |
391 | 0 | PVOID enumState = NULL; |
392 | 0 | SmartcardCertInfo** cert_list = *pcerts; |
393 | 0 | size_t count = *pcount; |
394 | |
|
395 | 0 | while (NCryptEnumKeys(provider, scope, &keyName, &enumState, NCRYPT_SILENT_FLAG) == |
396 | 0 | ERROR_SUCCESS) |
397 | 0 | { |
398 | 0 | NCRYPT_KEY_HANDLE phKey = 0; |
399 | 0 | PBYTE certBytes = NULL; |
400 | 0 | DWORD dwFlags = NCRYPT_SILENT_FLAG; |
401 | 0 | DWORD cbOutput = 0; |
402 | 0 | SmartcardCertInfo* cert = NULL; |
403 | 0 | BOOL haveError = TRUE; |
404 | 0 | SECURITY_STATUS status = 0; |
405 | |
|
406 | 0 | cert = calloc(1, sizeof(SmartcardCertInfo)); |
407 | 0 | if (!cert) |
408 | 0 | goto out; |
409 | | |
410 | 0 | cert->keyName = ConvertWCharToUtf8Alloc(keyName->pszName, NULL); |
411 | 0 | if (!cert->keyName) |
412 | 0 | goto endofloop; |
413 | | |
414 | 0 | WLog_DBG(TAG, "opening key %s", cert->keyName); |
415 | |
|
416 | 0 | status = |
417 | 0 | NCryptOpenKey(provider, &phKey, keyName->pszName, keyName->dwLegacyKeySpec, dwFlags); |
418 | 0 | if (status != ERROR_SUCCESS) |
419 | 0 | { |
420 | 0 | WLog_DBG(TAG, |
421 | 0 | "unable to NCryptOpenKey(dwLegacyKeySpec=0x%" PRIx32 " dwFlags=0x%" PRIx32 |
422 | 0 | "), status=%s, skipping", |
423 | 0 | status, keyName->dwLegacyKeySpec, keyName->dwFlags, |
424 | 0 | winpr_NCryptSecurityStatusError(status)); |
425 | 0 | goto endofloop; |
426 | 0 | } |
427 | | |
428 | 0 | cert->csp = _wcsdup(csp); |
429 | 0 | if (!cert->csp) |
430 | 0 | goto endofloop; |
431 | | |
432 | 0 | #ifndef _WIN32 |
433 | 0 | status = NCryptGetProperty(phKey, NCRYPT_WINPR_SLOTID, (PBYTE)&cert->slotId, 4, &cbOutput, |
434 | 0 | dwFlags); |
435 | 0 | if (status != ERROR_SUCCESS) |
436 | 0 | { |
437 | 0 | WLog_ERR(TAG, "unable to retrieve slotId for key %s, status=%s", cert->keyName, |
438 | 0 | winpr_NCryptSecurityStatusError(status)); |
439 | 0 | goto endofloop; |
440 | 0 | } |
441 | 0 | #endif /* _WIN32 */ |
442 | | |
443 | | /* ====== retrieve key's reader ====== */ |
444 | 0 | cbOutput = 0; |
445 | 0 | status = NCryptGetProperty(phKey, NCRYPT_READER_PROPERTY, NULL, 0, &cbOutput, dwFlags); |
446 | 0 | if (status != ERROR_SUCCESS) |
447 | 0 | { |
448 | 0 | WLog_DBG(TAG, "unable to retrieve reader's name length for key %s", cert->keyName); |
449 | 0 | goto endofloop; |
450 | 0 | } |
451 | | |
452 | 0 | cert->reader = calloc(1, cbOutput + 2); |
453 | 0 | if (!cert->reader) |
454 | 0 | { |
455 | 0 | WLog_ERR(TAG, "unable to allocate reader's name for key %s", cert->keyName); |
456 | 0 | goto endofloop; |
457 | 0 | } |
458 | | |
459 | 0 | status = NCryptGetProperty(phKey, NCRYPT_READER_PROPERTY, (PBYTE)cert->reader, cbOutput + 2, |
460 | 0 | &cbOutput, dwFlags); |
461 | 0 | if (status != ERROR_SUCCESS) |
462 | 0 | { |
463 | 0 | WLog_ERR(TAG, "unable to retrieve reader's name for key %s", cert->keyName); |
464 | 0 | goto endofloop; |
465 | 0 | } |
466 | | |
467 | | /* ====== retrieve key container name ====== */ |
468 | | /* When using PKCS11, this will try to return what Windows would use for the key's name */ |
469 | 0 | cbOutput = 0; |
470 | 0 | status = NCryptGetProperty(phKey, NCRYPT_NAME_PROPERTY, NULL, 0, &cbOutput, dwFlags); |
471 | 0 | if (status == ERROR_SUCCESS) |
472 | 0 | { |
473 | 0 | cert->containerName = calloc(1, cbOutput + sizeof(WCHAR)); |
474 | 0 | if (!cert->containerName) |
475 | 0 | { |
476 | 0 | WLog_ERR(TAG, "unable to allocate key container name for key %s", cert->keyName); |
477 | 0 | goto endofloop; |
478 | 0 | } |
479 | | |
480 | 0 | status = NCryptGetProperty(phKey, NCRYPT_NAME_PROPERTY, (BYTE*)cert->containerName, |
481 | 0 | cbOutput, &cbOutput, dwFlags); |
482 | 0 | } |
483 | | |
484 | 0 | if (status != ERROR_SUCCESS) |
485 | 0 | { |
486 | 0 | WLog_ERR(TAG, "unable to retrieve key container name for key %s", cert->keyName); |
487 | 0 | goto endofloop; |
488 | 0 | } |
489 | | |
490 | | /* ========= retrieve the certificate ===============*/ |
491 | 0 | cbOutput = 0; |
492 | 0 | status = NCryptGetProperty(phKey, NCRYPT_CERTIFICATE_PROPERTY, NULL, 0, &cbOutput, dwFlags); |
493 | 0 | if (status != ERROR_SUCCESS) |
494 | 0 | { |
495 | | /* can happen that key don't have certificates */ |
496 | 0 | WLog_DBG(TAG, "unable to retrieve certificate property len, status=0x%lx, skipping", |
497 | 0 | status); |
498 | 0 | goto endofloop; |
499 | 0 | } |
500 | | |
501 | 0 | certBytes = calloc(1, cbOutput); |
502 | 0 | if (!certBytes) |
503 | 0 | { |
504 | 0 | WLog_ERR(TAG, "unable to allocate %" PRIu32 " certBytes for key %s", cbOutput, |
505 | 0 | cert->keyName); |
506 | 0 | goto endofloop; |
507 | 0 | } |
508 | | |
509 | 0 | status = NCryptGetProperty(phKey, NCRYPT_CERTIFICATE_PROPERTY, certBytes, cbOutput, |
510 | 0 | &cbOutput, dwFlags); |
511 | 0 | if (status != ERROR_SUCCESS) |
512 | 0 | { |
513 | 0 | WLog_ERR(TAG, "unable to retrieve certificate for key %s", cert->keyName); |
514 | 0 | goto endofloop; |
515 | 0 | } |
516 | | |
517 | 0 | if (!set_info_certificate(cert, certBytes, cbOutput, userFilter, domainFilter)) |
518 | 0 | goto endofloop; |
519 | | |
520 | 0 | #ifndef _WIN32 |
521 | 0 | if (!build_pkinit_args(provider, cert)) |
522 | 0 | { |
523 | 0 | WLog_ERR(TAG, "error build pkinit args"); |
524 | 0 | goto endofloop; |
525 | 0 | } |
526 | 0 | #endif |
527 | 0 | haveError = FALSE; |
528 | |
|
529 | 0 | endofloop: |
530 | 0 | free(certBytes); |
531 | 0 | NCryptFreeBuffer(keyName); |
532 | 0 | if (phKey) |
533 | 0 | NCryptFreeObject((NCRYPT_HANDLE)phKey); |
534 | |
|
535 | 0 | if (haveError) |
536 | 0 | smartcardCertInfo_Free(cert); |
537 | 0 | else |
538 | 0 | { |
539 | 0 | if (!add_cert_to_list(&cert_list, &count, cert)) |
540 | 0 | goto out; |
541 | 0 | } |
542 | 0 | } |
543 | | |
544 | 0 | ret = TRUE; |
545 | 0 | out: |
546 | 0 | if (count == 0) |
547 | 0 | { |
548 | 0 | char cspa[128] = { 0 }; |
549 | |
|
550 | 0 | (void)ConvertWCharToUtf8(csp, cspa, sizeof(cspa)); |
551 | 0 | char scopea[128] = { 0 }; |
552 | 0 | (void)ConvertWCharToUtf8(scope, scopea, sizeof(scopea)); |
553 | 0 | WLog_WARN(TAG, "%s [%s] no certificates found", cspa, scopea); |
554 | 0 | } |
555 | 0 | *pcount = count; |
556 | 0 | *pcerts = cert_list; |
557 | 0 | NCryptFreeBuffer(enumState); |
558 | 0 | return ret; |
559 | 0 | } |
560 | | |
561 | | static BOOL smartcard_hw_enumerateCerts(const rdpSettings* settings, LPCWSTR csp, |
562 | | const char* reader, const char* userFilter, |
563 | | const char* domainFilter, SmartcardCertInfo*** scCerts, |
564 | | size_t* retCount) |
565 | 0 | { |
566 | 0 | BOOL ret = FALSE; |
567 | 0 | LPWSTR scope = NULL; |
568 | 0 | NCRYPT_PROV_HANDLE provider = 0; |
569 | 0 | SECURITY_STATUS status = 0; |
570 | 0 | size_t count = 0; |
571 | 0 | SmartcardCertInfo** cert_list = NULL; |
572 | 0 | const char* Pkcs11Module = freerdp_settings_get_string(settings, FreeRDP_Pkcs11Module); |
573 | |
|
574 | 0 | WINPR_ASSERT(scCerts); |
575 | 0 | WINPR_ASSERT(retCount); |
576 | | |
577 | 0 | if (reader) |
578 | 0 | { |
579 | 0 | size_t readerSz = strlen(reader); |
580 | 0 | char* scopeStr = malloc(4 + readerSz + 1 + 1); |
581 | 0 | if (!scopeStr) |
582 | 0 | goto out; |
583 | | |
584 | 0 | (void)_snprintf(scopeStr, readerSz + 5, "\\\\.\\%s\\", reader); |
585 | 0 | scope = ConvertUtf8NToWCharAlloc(scopeStr, readerSz + 5, NULL); |
586 | 0 | free(scopeStr); |
587 | |
|
588 | 0 | if (!scope) |
589 | 0 | goto out; |
590 | 0 | } |
591 | | |
592 | 0 | if (Pkcs11Module) |
593 | 0 | { |
594 | | /* load a unique CSP by pkcs11 module path */ |
595 | 0 | LPCSTR paths[] = { Pkcs11Module, NULL }; |
596 | |
|
597 | 0 | if (!csp) |
598 | 0 | csp = MS_SCARD_PROV; |
599 | |
|
600 | 0 | status = winpr_NCryptOpenStorageProviderEx(&provider, csp, 0, paths); |
601 | 0 | if (status != ERROR_SUCCESS) |
602 | 0 | { |
603 | 0 | WLog_ERR(TAG, "unable to open provider given by pkcs11 module"); |
604 | 0 | goto out; |
605 | 0 | } |
606 | | |
607 | 0 | status = list_provider_keys(settings, provider, csp, scope, userFilter, domainFilter, |
608 | 0 | &cert_list, &count); |
609 | 0 | NCryptFreeObject((NCRYPT_HANDLE)provider); |
610 | 0 | if (!status) |
611 | 0 | { |
612 | 0 | WLog_ERR(TAG, "error listing keys from CSP loaded from %s", Pkcs11Module); |
613 | 0 | goto out; |
614 | 0 | } |
615 | 0 | } |
616 | 0 | else |
617 | 0 | { |
618 | 0 | NCryptProviderName* names = NULL; |
619 | 0 | DWORD nproviders = 0; |
620 | |
|
621 | | #ifdef _WIN32 |
622 | | /* On Windows, mstsc first enumerates the legacy CAPI providers for usable certificates. */ |
623 | | DWORD provType, cbProvName = 0; |
624 | | for (DWORD i = 0; CryptEnumProvidersW(i, NULL, 0, &provType, NULL, &cbProvName); ++i) |
625 | | { |
626 | | char providerNameStr[256] = { 0 }; |
627 | | LPWSTR szProvName = malloc(cbProvName * sizeof(WCHAR)); |
628 | | if (!CryptEnumProvidersW(i, NULL, 0, &provType, szProvName, &cbProvName)) |
629 | | { |
630 | | free(szProvName); |
631 | | break; |
632 | | } |
633 | | |
634 | | if (ConvertWCharToUtf8(szProvName, providerNameStr, ARRAYSIZE(providerNameStr)) < 0) |
635 | | { |
636 | | _snprintf(providerNameStr, sizeof(providerNameStr), "<unknown>"); |
637 | | WLog_ERR(TAG, "unable to convert provider name to char*, will show it as '%s'", |
638 | | providerNameStr); |
639 | | } |
640 | | |
641 | | WLog_DBG(TAG, "exploring CSP '%s'", providerNameStr); |
642 | | if (provType != PROV_RSA_FULL || (csp && _wcscmp(szProvName, csp) != 0)) |
643 | | { |
644 | | WLog_DBG(TAG, "CSP '%s' filtered out", providerNameStr); |
645 | | goto end_of_loop; |
646 | | } |
647 | | |
648 | | if (!list_capi_provider_keys(settings, szProvName, scope, userFilter, domainFilter, |
649 | | &cert_list, &count)) |
650 | | WLog_INFO(TAG, "error when retrieving keys in CSP '%s'", providerNameStr); |
651 | | |
652 | | end_of_loop: |
653 | | free(szProvName); |
654 | | } |
655 | | #endif |
656 | |
|
657 | 0 | status = NCryptEnumStorageProviders(&nproviders, &names, NCRYPT_SILENT_FLAG); |
658 | 0 | if (status != ERROR_SUCCESS) |
659 | 0 | { |
660 | 0 | WLog_ERR(TAG, "error listing providers"); |
661 | 0 | goto out; |
662 | 0 | } |
663 | | |
664 | 0 | for (DWORD i = 0; i < nproviders; i++) |
665 | 0 | { |
666 | 0 | char providerNameStr[256] = { 0 }; |
667 | 0 | const NCryptProviderName* name = &names[i]; |
668 | |
|
669 | 0 | if (ConvertWCharToUtf8(name->pszName, providerNameStr, ARRAYSIZE(providerNameStr)) < 0) |
670 | 0 | { |
671 | 0 | (void)_snprintf(providerNameStr, sizeof(providerNameStr), "<unknown>"); |
672 | 0 | WLog_ERR(TAG, "unable to convert provider name to char*, will show it as '%s'", |
673 | 0 | providerNameStr); |
674 | 0 | } |
675 | |
|
676 | 0 | WLog_DBG(TAG, "exploring CSP '%s'", providerNameStr); |
677 | 0 | if (csp && _wcscmp(name->pszName, csp) != 0) |
678 | 0 | { |
679 | 0 | WLog_DBG(TAG, "CSP '%s' filtered out", providerNameStr); |
680 | 0 | continue; |
681 | 0 | } |
682 | | |
683 | 0 | status = NCryptOpenStorageProvider(&provider, name->pszName, 0); |
684 | 0 | if (status != ERROR_SUCCESS) |
685 | 0 | continue; |
686 | | |
687 | 0 | if (!list_provider_keys(settings, provider, name->pszName, scope, userFilter, |
688 | 0 | domainFilter, &cert_list, &count)) |
689 | 0 | WLog_INFO(TAG, "error when retrieving keys in CSP '%s'", providerNameStr); |
690 | |
|
691 | 0 | NCryptFreeObject((NCRYPT_HANDLE)provider); |
692 | 0 | } |
693 | |
|
694 | 0 | NCryptFreeBuffer(names); |
695 | 0 | } |
696 | | |
697 | 0 | *scCerts = cert_list; |
698 | 0 | *retCount = count; |
699 | 0 | ret = TRUE; |
700 | |
|
701 | 0 | out: |
702 | 0 | if (!ret) |
703 | 0 | smartcardCertList_Free(cert_list, count); |
704 | 0 | free(scope); |
705 | 0 | return ret; |
706 | 0 | } |
707 | | |
708 | | static char* create_temporary_file(void) |
709 | 0 | { |
710 | 0 | BYTE buffer[32]; |
711 | 0 | char* hex = NULL; |
712 | 0 | char* path = NULL; |
713 | |
|
714 | 0 | winpr_RAND(buffer, sizeof(buffer)); |
715 | 0 | hex = winpr_BinToHexString(buffer, sizeof(buffer), FALSE); |
716 | 0 | path = GetKnownSubPath(KNOWN_PATH_TEMP, hex); |
717 | 0 | free(hex); |
718 | 0 | return path; |
719 | 0 | } |
720 | | |
721 | | static SmartcardCertInfo* smartcardCertInfo_New(const char* privKeyPEM, const char* certPEM) |
722 | 0 | { |
723 | 0 | size_t size = 0; |
724 | |
|
725 | 0 | WINPR_ASSERT(privKeyPEM); |
726 | 0 | WINPR_ASSERT(certPEM); |
727 | | |
728 | 0 | SmartcardCertInfo* cert = calloc(1, sizeof(SmartcardCertInfo)); |
729 | 0 | if (!cert) |
730 | 0 | goto fail; |
731 | | |
732 | 0 | SmartcardKeyInfo* info = cert->key_info = calloc(1, sizeof(SmartcardKeyInfo)); |
733 | 0 | if (!info) |
734 | 0 | goto fail; |
735 | | |
736 | 0 | cert->certificate = freerdp_certificate_new_from_pem(certPEM); |
737 | 0 | if (!cert->certificate) |
738 | 0 | { |
739 | 0 | WLog_ERR(TAG, "unable to read smartcard certificate"); |
740 | 0 | goto fail; |
741 | 0 | } |
742 | | |
743 | 0 | if (!treat_sc_cert(cert)) |
744 | 0 | { |
745 | 0 | WLog_ERR(TAG, "unable to treat smartcard certificate"); |
746 | 0 | goto fail; |
747 | 0 | } |
748 | | |
749 | 0 | cert->reader = ConvertUtf8ToWCharAlloc("FreeRDP Emulator", NULL); |
750 | 0 | if (!cert->reader) |
751 | 0 | goto fail; |
752 | | |
753 | 0 | cert->containerName = ConvertUtf8ToWCharAlloc("Private Key 00", NULL); |
754 | 0 | if (!cert->containerName) |
755 | 0 | goto fail; |
756 | | |
757 | | /* compute PKINIT args FILE:<cert file>,<key file> |
758 | | * |
759 | | * We need files for PKINIT to read, so write the certificate to some |
760 | | * temporary location and use that. |
761 | | */ |
762 | 0 | info->keyPath = create_temporary_file(); |
763 | 0 | WLog_DBG(TAG, "writing PKINIT key to %s", info->keyPath); |
764 | 0 | if (!crypto_write_pem(info->keyPath, privKeyPEM, strlen(privKeyPEM))) |
765 | 0 | goto fail; |
766 | | |
767 | 0 | info->certPath = create_temporary_file(); |
768 | 0 | WLog_DBG(TAG, "writing PKINIT cert to %s", info->certPath); |
769 | 0 | if (!crypto_write_pem(info->certPath, certPEM, strlen(certPEM))) |
770 | 0 | goto fail; |
771 | | |
772 | 0 | int res = winpr_asprintf(&cert->pkinitArgs, &size, "FILE:%s,%s", info->certPath, info->keyPath); |
773 | 0 | if (res <= 0) |
774 | 0 | goto fail; |
775 | | |
776 | 0 | return cert; |
777 | 0 | fail: |
778 | 0 | smartcardCertInfo_Free(cert); |
779 | 0 | return NULL; |
780 | 0 | } |
781 | | |
782 | | static BOOL smartcard_sw_enumerateCerts(const rdpSettings* settings, SmartcardCertInfo*** scCerts, |
783 | | size_t* retCount) |
784 | 0 | { |
785 | 0 | BOOL rc = FALSE; |
786 | 0 | SmartcardCertInfo** cert_list = NULL; |
787 | |
|
788 | 0 | WINPR_ASSERT(settings); |
789 | 0 | WINPR_ASSERT(scCerts); |
790 | 0 | WINPR_ASSERT(retCount); |
791 | | |
792 | 0 | const char* privKeyPEM = freerdp_settings_get_string(settings, FreeRDP_SmartcardPrivateKey); |
793 | 0 | const char* certPEM = freerdp_settings_get_string(settings, FreeRDP_SmartcardCertificate); |
794 | 0 | if (!privKeyPEM) |
795 | 0 | { |
796 | 0 | WLog_ERR(TAG, "Invalid smartcard private key PEM, aborting"); |
797 | 0 | goto out_error; |
798 | 0 | } |
799 | 0 | if (!certPEM) |
800 | 0 | { |
801 | 0 | WLog_ERR(TAG, "Invalid smartcard certificate PEM, aborting"); |
802 | 0 | goto out_error; |
803 | 0 | } |
804 | | |
805 | 0 | cert_list = (SmartcardCertInfo**)calloc(1, sizeof(SmartcardCertInfo*)); |
806 | 0 | if (!cert_list) |
807 | 0 | goto out_error; |
808 | | |
809 | 0 | { |
810 | 0 | SmartcardCertInfo* cert = smartcardCertInfo_New(privKeyPEM, certPEM); |
811 | 0 | if (!cert) |
812 | 0 | goto out_error; |
813 | 0 | cert_list[0] = cert; |
814 | 0 | } |
815 | | |
816 | 0 | rc = TRUE; |
817 | 0 | *scCerts = cert_list; |
818 | 0 | *retCount = 1; |
819 | |
|
820 | 0 | out_error: |
821 | 0 | if (!rc) |
822 | 0 | smartcardCertList_Free(cert_list, 1); |
823 | 0 | return rc; |
824 | 0 | } |
825 | | |
826 | | BOOL smartcard_enumerateCerts(const rdpSettings* settings, SmartcardCertInfo*** scCerts, |
827 | | size_t* retCount, BOOL gateway) |
828 | 0 | { |
829 | 0 | BOOL ret = 0; |
830 | 0 | LPWSTR csp = NULL; |
831 | 0 | const char* ReaderName = freerdp_settings_get_string(settings, FreeRDP_ReaderName); |
832 | 0 | const char* CspName = freerdp_settings_get_string(settings, FreeRDP_CspName); |
833 | 0 | const char* Username = NULL; |
834 | 0 | const char* Domain = NULL; |
835 | |
|
836 | 0 | if (gateway) |
837 | 0 | { |
838 | 0 | Username = freerdp_settings_get_string(settings, FreeRDP_GatewayUsername); |
839 | 0 | Domain = freerdp_settings_get_string(settings, FreeRDP_GatewayDomain); |
840 | 0 | } |
841 | 0 | else |
842 | 0 | { |
843 | 0 | Username = freerdp_settings_get_string(settings, FreeRDP_Username); |
844 | 0 | Domain = freerdp_settings_get_string(settings, FreeRDP_Domain); |
845 | 0 | } |
846 | |
|
847 | 0 | WINPR_ASSERT(settings); |
848 | 0 | WINPR_ASSERT(scCerts); |
849 | 0 | WINPR_ASSERT(retCount); |
850 | | |
851 | 0 | if (Domain && !strlen(Domain)) |
852 | 0 | Domain = NULL; |
853 | |
|
854 | 0 | if (freerdp_settings_get_bool(settings, FreeRDP_SmartcardEmulation)) |
855 | 0 | return smartcard_sw_enumerateCerts(settings, scCerts, retCount); |
856 | | |
857 | 0 | if (CspName && (!(csp = ConvertUtf8ToWCharAlloc(CspName, NULL)))) |
858 | 0 | { |
859 | 0 | WLog_ERR(TAG, "error while converting CSP to WCHAR"); |
860 | 0 | return FALSE; |
861 | 0 | } |
862 | | |
863 | 0 | ret = |
864 | 0 | smartcard_hw_enumerateCerts(settings, csp, ReaderName, Username, Domain, scCerts, retCount); |
865 | 0 | free(csp); |
866 | 0 | return ret; |
867 | 0 | } |
868 | | |
869 | | static BOOL set_settings_from_smartcard(rdpSettings* settings, FreeRDP_Settings_Keys_String id, |
870 | | const char* value) |
871 | 0 | { |
872 | 0 | WINPR_ASSERT(settings); |
873 | | |
874 | 0 | if (!freerdp_settings_get_string(settings, id) && value) |
875 | 0 | if (!freerdp_settings_set_string(settings, id, value)) |
876 | 0 | return FALSE; |
877 | | |
878 | 0 | return TRUE; |
879 | 0 | } |
880 | | |
881 | | BOOL smartcard_getCert(const rdpContext* context, SmartcardCertInfo** cert, BOOL gateway) |
882 | 0 | { |
883 | 0 | WINPR_ASSERT(context); |
884 | | |
885 | 0 | const freerdp* instance = context->instance; |
886 | 0 | rdpSettings* settings = context->settings; |
887 | 0 | SmartcardCertInfo** cert_list = NULL; |
888 | 0 | size_t count = 0; |
889 | |
|
890 | 0 | WINPR_ASSERT(instance); |
891 | 0 | WINPR_ASSERT(settings); |
892 | | |
893 | 0 | if (!smartcard_enumerateCerts(settings, &cert_list, &count, gateway)) |
894 | 0 | return FALSE; |
895 | | |
896 | 0 | if (count < 1) |
897 | 0 | { |
898 | 0 | WLog_ERR(TAG, "no suitable smartcard certificates were found"); |
899 | 0 | return FALSE; |
900 | 0 | } |
901 | | |
902 | 0 | if (count > UINT32_MAX) |
903 | 0 | { |
904 | 0 | WLog_ERR(TAG, "smartcard certificate count %" PRIuz " exceeds UINT32_MAX", count); |
905 | 0 | return FALSE; |
906 | 0 | } |
907 | | |
908 | 0 | if (count > 1) |
909 | 0 | { |
910 | 0 | DWORD index = 0; |
911 | |
|
912 | 0 | if (!instance->ChooseSmartcard || |
913 | 0 | !instance->ChooseSmartcard(context->instance, cert_list, (UINT32)count, &index, |
914 | 0 | gateway)) |
915 | 0 | { |
916 | 0 | WLog_ERR(TAG, "more than one suitable smartcard certificate was found"); |
917 | 0 | smartcardCertList_Free(cert_list, count); |
918 | 0 | return FALSE; |
919 | 0 | } |
920 | 0 | *cert = cert_list[index]; |
921 | |
|
922 | 0 | for (DWORD i = 0; i < index; i++) |
923 | 0 | smartcardCertInfo_Free(cert_list[i]); |
924 | 0 | for (DWORD i = index + 1; i < count; i++) |
925 | 0 | smartcardCertInfo_Free(cert_list[i]); |
926 | 0 | } |
927 | 0 | else |
928 | 0 | *cert = cert_list[0]; |
929 | | |
930 | 0 | FreeRDP_Settings_Keys_String username_setting = |
931 | 0 | gateway ? FreeRDP_GatewayUsername : FreeRDP_Username; |
932 | 0 | FreeRDP_Settings_Keys_String domain_setting = gateway ? FreeRDP_GatewayDomain : FreeRDP_Domain; |
933 | |
|
934 | 0 | free((void*)cert_list); |
935 | |
|
936 | 0 | if (!set_settings_from_smartcard(settings, username_setting, (*cert)->userHint) || |
937 | 0 | !set_settings_from_smartcard(settings, domain_setting, (*cert)->domainHint)) |
938 | 0 | { |
939 | 0 | WLog_ERR(TAG, "unable to set settings from smartcard!"); |
940 | 0 | smartcardCertInfo_Free(*cert); |
941 | 0 | return FALSE; |
942 | 0 | } |
943 | | |
944 | 0 | return TRUE; |
945 | 0 | } |