/src/FreeRDP/libfreerdp/utils/smartcard_call.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Smartcard Device Service Virtual Channel |
4 | | * |
5 | | * Copyright (C) Alexi Volkov <alexi@myrealbox.com> 2006 |
6 | | * Copyright 2011 O.S. Systems Software Ltda. |
7 | | * Copyright 2011 Anthony Tong <atong@trustedcs.com> |
8 | | * Copyright 2015 Thincast Technologies GmbH |
9 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
10 | | * Copyright 2017 Armin Novak <armin.novak@thincast.com> |
11 | | * Copyright 2017 Thincast Technologies GmbH |
12 | | * |
13 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
14 | | * you may not use this file except in compliance with the License. |
15 | | * You may obtain a copy of the License at |
16 | | * |
17 | | * http://www.apache.org/licenses/LICENSE-2.0 |
18 | | * |
19 | | * Unless required by applicable law or agreed to in writing, software |
20 | | * distributed under the License is distributed on an "AS IS" BASIS, |
21 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
22 | | * See the License for the specific language governing permissions and |
23 | | * limitations under the License. |
24 | | */ |
25 | | |
26 | | #include <freerdp/config.h> |
27 | | |
28 | | #include <winpr/assert.h> |
29 | | |
30 | | #include <winpr/crt.h> |
31 | | #include <winpr/print.h> |
32 | | #include <winpr/stream.h> |
33 | | #include <winpr/library.h> |
34 | | #include <winpr/smartcard.h> |
35 | | |
36 | | #include <freerdp/freerdp.h> |
37 | | #include <freerdp/channels/rdpdr.h> |
38 | | #include <freerdp/channels/scard.h> |
39 | | |
40 | | #include <freerdp/utils/rdpdr_utils.h> |
41 | | #include <freerdp/utils/smartcard_pack.h> |
42 | | #include <freerdp/utils/smartcard_call.h> |
43 | | |
44 | | #include <freerdp/log.h> |
45 | 0 | #define TAG FREERDP_TAG("utils.smartcard.call") |
46 | | |
47 | | #if defined(WITH_SMARTCARD_EMULATE) |
48 | | #include <freerdp/emulate/scard/smartcard_emulate.h> |
49 | | |
50 | | #define str(x) #x |
51 | | #define wrap(ctx, fkt, ...) \ |
52 | 0 | ctx->useEmulatedCard ? Emulate_##fkt(ctx->emulation, ##__VA_ARGS__) \ |
53 | 0 | : ctx->pWinSCardApi->pfn##fkt(__VA_ARGS__) |
54 | 0 | #define wrap_ptr(ctx, fkt, ...) wrap(ctx, fkt, ##__VA_ARGS__) |
55 | | #else |
56 | | #define wrap(ctx, fkt, ...) \ |
57 | | ctx->useEmulatedCard ? SCARD_F_INTERNAL_ERROR : ctx->pWinSCardApi->pfn##fkt(__VA_ARGS__) |
58 | | #define wrap_ptr(ctx, fkt, ...) \ |
59 | | ctx->useEmulatedCard ? NULL : ctx->pWinSCardApi->pfn##fkt(__VA_ARGS__) |
60 | | #endif |
61 | | |
62 | | struct s_scard_call_context |
63 | | { |
64 | | BOOL useEmulatedCard; |
65 | | HANDLE StartedEvent; |
66 | | wLinkedList* names; |
67 | | wHashTable* rgSCardContextList; |
68 | | #if defined(WITH_SMARTCARD_EMULATE) |
69 | | SmartcardEmulationContext* emulation; |
70 | | #endif |
71 | | HANDLE hWinSCardLibrary; |
72 | | SCardApiFunctionTable WinSCardApi; |
73 | | const SCardApiFunctionTable* pWinSCardApi; |
74 | | HANDLE stopEvent; |
75 | | void* userdata; |
76 | | |
77 | | void* (*fn_new)(void*, SCARDCONTEXT); |
78 | | void (*fn_free)(void*); |
79 | | }; |
80 | | |
81 | | struct s_scard_context_element |
82 | | { |
83 | | void* context; |
84 | | void (*fn_free)(void*); |
85 | | }; |
86 | | |
87 | | static void context_free(void* arg); |
88 | | |
89 | | static LONG smartcard_EstablishContext_Call(scard_call_context* smartcard, wStream* out, |
90 | | SMARTCARD_OPERATION* operation) |
91 | 0 | { |
92 | 0 | LONG status = 0; |
93 | 0 | SCARDCONTEXT hContext = { 0 }; |
94 | 0 | EstablishContext_Return ret = { 0 }; |
95 | 0 | EstablishContext_Call* call = &operation->call.establishContext; |
96 | 0 | status = ret.ReturnCode = |
97 | 0 | wrap(smartcard, SCardEstablishContext, call->dwScope, NULL, NULL, &hContext); |
98 | |
|
99 | 0 | if (ret.ReturnCode == SCARD_S_SUCCESS) |
100 | 0 | { |
101 | 0 | const void* key = (void*)(size_t)hContext; |
102 | 0 | struct s_scard_context_element* pContext = |
103 | 0 | calloc(1, sizeof(struct s_scard_context_element)); |
104 | 0 | if (!pContext) |
105 | 0 | return STATUS_NO_MEMORY; |
106 | | |
107 | 0 | pContext->fn_free = smartcard->fn_free; |
108 | |
|
109 | 0 | if (smartcard->fn_new) |
110 | 0 | { |
111 | 0 | pContext->context = smartcard->fn_new(smartcard->userdata, hContext); |
112 | 0 | if (!pContext->context) |
113 | 0 | { |
114 | 0 | free(pContext); |
115 | 0 | return STATUS_NO_MEMORY; |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | 0 | if (!HashTable_Insert(smartcard->rgSCardContextList, key, (void*)pContext)) |
120 | 0 | { |
121 | 0 | WLog_ERR(TAG, "ListDictionary_Add failed!"); |
122 | 0 | context_free(pContext); |
123 | 0 | return STATUS_INTERNAL_ERROR; |
124 | 0 | } |
125 | 0 | } |
126 | 0 | else |
127 | 0 | { |
128 | 0 | return scard_log_status_error(TAG, "SCardEstablishContext", status); |
129 | 0 | } |
130 | | |
131 | | // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert takes ownership of pContext |
132 | 0 | smartcard_scard_context_native_to_redir(&(ret.hContext), hContext); |
133 | |
|
134 | 0 | status = smartcard_pack_establish_context_return(out, &ret); |
135 | 0 | if (status != SCARD_S_SUCCESS) |
136 | 0 | { |
137 | 0 | return scard_log_status_error(TAG, "smartcard_pack_establish_context_return", status); |
138 | 0 | } |
139 | | |
140 | 0 | return ret.ReturnCode; |
141 | 0 | } |
142 | | |
143 | | static LONG smartcard_ReleaseContext_Call(scard_call_context* smartcard, wStream* out, |
144 | | SMARTCARD_OPERATION* operation) |
145 | 0 | { |
146 | 0 | Long_Return ret = { 0 }; |
147 | |
|
148 | 0 | WINPR_ASSERT(smartcard); |
149 | 0 | WINPR_ASSERT(out); |
150 | 0 | WINPR_ASSERT(operation); |
151 | | |
152 | 0 | ret.ReturnCode = wrap(smartcard, SCardReleaseContext, operation->hContext); |
153 | |
|
154 | 0 | if (ret.ReturnCode == SCARD_S_SUCCESS) |
155 | 0 | HashTable_Remove(smartcard->rgSCardContextList, (void*)operation->hContext); |
156 | 0 | else |
157 | 0 | { |
158 | 0 | return scard_log_status_error(TAG, "SCardReleaseContext", ret.ReturnCode); |
159 | 0 | } |
160 | | |
161 | 0 | smartcard_trace_long_return(&ret, "ReleaseContext"); |
162 | 0 | return ret.ReturnCode; |
163 | 0 | } |
164 | | |
165 | | static LONG smartcard_IsValidContext_Call(scard_call_context* smartcard, wStream* out, |
166 | | SMARTCARD_OPERATION* operation) |
167 | 0 | { |
168 | 0 | Long_Return ret = { 0 }; |
169 | |
|
170 | 0 | WINPR_ASSERT(smartcard); |
171 | 0 | WINPR_ASSERT(out); |
172 | 0 | WINPR_ASSERT(operation); |
173 | | |
174 | 0 | ret.ReturnCode = wrap(smartcard, SCardIsValidContext, operation->hContext); |
175 | 0 | smartcard_trace_long_return(&ret, "IsValidContext"); |
176 | 0 | return ret.ReturnCode; |
177 | 0 | } |
178 | | |
179 | | static LONG smartcard_ListReaderGroupsA_Call(scard_call_context* smartcard, wStream* out, |
180 | | SMARTCARD_OPERATION* operation) |
181 | 0 | { |
182 | 0 | LONG status = 0; |
183 | 0 | ListReaderGroups_Return ret = { 0 }; |
184 | 0 | LPSTR mszGroups = NULL; |
185 | 0 | DWORD cchGroups = 0; |
186 | |
|
187 | 0 | WINPR_ASSERT(smartcard); |
188 | 0 | WINPR_ASSERT(out); |
189 | 0 | WINPR_ASSERT(operation); |
190 | | |
191 | 0 | cchGroups = SCARD_AUTOALLOCATE; |
192 | 0 | ret.ReturnCode = |
193 | 0 | wrap(smartcard, SCardListReaderGroupsA, operation->hContext, (LPSTR)&mszGroups, &cchGroups); |
194 | 0 | ret.msz = (BYTE*)mszGroups; |
195 | 0 | ret.cBytes = cchGroups; |
196 | |
|
197 | 0 | status = smartcard_pack_list_reader_groups_return(out, &ret, FALSE); |
198 | |
|
199 | 0 | if (status != SCARD_S_SUCCESS) |
200 | 0 | return status; |
201 | | |
202 | 0 | if (mszGroups) |
203 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, mszGroups); |
204 | |
|
205 | 0 | return ret.ReturnCode; |
206 | 0 | } |
207 | | |
208 | | static LONG smartcard_ListReaderGroupsW_Call(scard_call_context* smartcard, wStream* out, |
209 | | SMARTCARD_OPERATION* operation) |
210 | 0 | { |
211 | 0 | LONG status = 0; |
212 | 0 | ListReaderGroups_Return ret = { 0 }; |
213 | 0 | LPWSTR mszGroups = NULL; |
214 | 0 | DWORD cchGroups = 0; |
215 | |
|
216 | 0 | WINPR_ASSERT(smartcard); |
217 | 0 | WINPR_ASSERT(out); |
218 | 0 | WINPR_ASSERT(operation); |
219 | | |
220 | 0 | cchGroups = SCARD_AUTOALLOCATE; |
221 | 0 | status = ret.ReturnCode = wrap(smartcard, SCardListReaderGroupsW, operation->hContext, |
222 | 0 | (LPWSTR)&mszGroups, &cchGroups); |
223 | 0 | ret.msz = (BYTE*)mszGroups; |
224 | 0 | ret.cBytes = cchGroups * sizeof(WCHAR); |
225 | |
|
226 | 0 | if (status != SCARD_S_SUCCESS) |
227 | 0 | return status; |
228 | | |
229 | 0 | status = smartcard_pack_list_reader_groups_return(out, &ret, TRUE); |
230 | |
|
231 | 0 | if (status != SCARD_S_SUCCESS) |
232 | 0 | return status; |
233 | | |
234 | 0 | if (mszGroups) |
235 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, mszGroups); |
236 | |
|
237 | 0 | return ret.ReturnCode; |
238 | 0 | } |
239 | | |
240 | | static BOOL filter_match(wLinkedList* list, LPCSTR reader, size_t readerLen) |
241 | 0 | { |
242 | 0 | if (readerLen < 1) |
243 | 0 | return FALSE; |
244 | | |
245 | 0 | LinkedList_Enumerator_Reset(list); |
246 | |
|
247 | 0 | while (LinkedList_Enumerator_MoveNext(list)) |
248 | 0 | { |
249 | 0 | const char* filter = LinkedList_Enumerator_Current(list); |
250 | |
|
251 | 0 | if (filter) |
252 | 0 | { |
253 | 0 | if (strstr(reader, filter) != NULL) |
254 | 0 | return TRUE; |
255 | 0 | } |
256 | 0 | } |
257 | | |
258 | 0 | return FALSE; |
259 | 0 | } |
260 | | |
261 | | static DWORD filter_device_by_name_a(wLinkedList* list, LPSTR* mszReaders, DWORD cchReaders) |
262 | 0 | { |
263 | 0 | size_t rpos = 0; |
264 | 0 | size_t wpos = 0; |
265 | |
|
266 | 0 | if (*mszReaders == NULL || LinkedList_Count(list) < 1) |
267 | 0 | return cchReaders; |
268 | | |
269 | 0 | do |
270 | 0 | { |
271 | 0 | LPCSTR rreader = &(*mszReaders)[rpos]; |
272 | 0 | LPSTR wreader = &(*mszReaders)[wpos]; |
273 | 0 | size_t readerLen = strnlen(rreader, cchReaders - rpos); |
274 | |
|
275 | 0 | rpos += readerLen + 1; |
276 | |
|
277 | 0 | if (filter_match(list, rreader, readerLen)) |
278 | 0 | { |
279 | 0 | if (rreader != wreader) |
280 | 0 | memmove(wreader, rreader, readerLen + 1); |
281 | |
|
282 | 0 | wpos += readerLen + 1; |
283 | 0 | } |
284 | 0 | } while (rpos < cchReaders); |
285 | | |
286 | | /* this string must be double 0 terminated */ |
287 | 0 | if (rpos != wpos) |
288 | 0 | { |
289 | 0 | if (wpos >= cchReaders) |
290 | 0 | return 0; |
291 | | |
292 | 0 | (*mszReaders)[wpos++] = '\0'; |
293 | 0 | } |
294 | | |
295 | 0 | return (DWORD)wpos; |
296 | 0 | } |
297 | | |
298 | | static DWORD filter_device_by_name_w(wLinkedList* list, LPWSTR* mszReaders, DWORD cchReaders) |
299 | 0 | { |
300 | 0 | DWORD rc = 0; |
301 | 0 | LPSTR readers = NULL; |
302 | |
|
303 | 0 | if (LinkedList_Count(list) < 1) |
304 | 0 | return cchReaders; |
305 | | |
306 | 0 | readers = ConvertMszWCharNToUtf8Alloc(*mszReaders, cchReaders, NULL); |
307 | |
|
308 | 0 | if (!readers) |
309 | 0 | { |
310 | 0 | free(readers); |
311 | 0 | return 0; |
312 | 0 | } |
313 | | |
314 | 0 | free(*mszReaders); |
315 | 0 | *mszReaders = NULL; |
316 | 0 | rc = filter_device_by_name_a(list, &readers, cchReaders); |
317 | |
|
318 | 0 | *mszReaders = ConvertMszUtf8NToWCharAlloc(readers, rc, NULL); |
319 | 0 | if (!*mszReaders) |
320 | 0 | rc = 0; |
321 | |
|
322 | 0 | free(readers); |
323 | 0 | return rc; |
324 | 0 | } |
325 | | |
326 | | static LONG smartcard_ListReadersA_Call(scard_call_context* smartcard, wStream* out, |
327 | | SMARTCARD_OPERATION* operation) |
328 | 0 | { |
329 | 0 | LONG status = 0; |
330 | 0 | ListReaders_Return ret = { 0 }; |
331 | 0 | LPSTR mszReaders = NULL; |
332 | 0 | DWORD cchReaders = 0; |
333 | 0 | ListReaders_Call* call = NULL; |
334 | |
|
335 | 0 | WINPR_ASSERT(smartcard); |
336 | 0 | WINPR_ASSERT(out); |
337 | 0 | WINPR_ASSERT(operation); |
338 | | |
339 | 0 | call = &operation->call.listReaders; |
340 | 0 | cchReaders = SCARD_AUTOALLOCATE; |
341 | 0 | status = ret.ReturnCode = wrap(smartcard, SCardListReadersA, operation->hContext, |
342 | 0 | (LPCSTR)call->mszGroups, (LPSTR)&mszReaders, &cchReaders); |
343 | |
|
344 | 0 | if (status != SCARD_S_SUCCESS) |
345 | 0 | { |
346 | 0 | return scard_log_status_error(TAG, "SCardListReadersA", status); |
347 | 0 | } |
348 | | |
349 | 0 | cchReaders = filter_device_by_name_a(smartcard->names, &mszReaders, cchReaders); |
350 | 0 | ret.msz = (BYTE*)mszReaders; |
351 | 0 | ret.cBytes = cchReaders; |
352 | |
|
353 | 0 | status = smartcard_pack_list_readers_return(out, &ret, FALSE); |
354 | 0 | if (status != SCARD_S_SUCCESS) |
355 | 0 | { |
356 | 0 | return scard_log_status_error(TAG, "smartcard_pack_list_readers_return", status); |
357 | 0 | } |
358 | | |
359 | 0 | if (mszReaders) |
360 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaders); |
361 | |
|
362 | 0 | if (status != SCARD_S_SUCCESS) |
363 | 0 | return status; |
364 | | |
365 | 0 | return ret.ReturnCode; |
366 | 0 | } |
367 | | |
368 | | static LONG smartcard_ListReadersW_Call(scard_call_context* smartcard, wStream* out, |
369 | | SMARTCARD_OPERATION* operation) |
370 | 0 | { |
371 | 0 | LONG status = 0; |
372 | 0 | ListReaders_Return ret = { 0 }; |
373 | 0 | DWORD cchReaders = 0; |
374 | 0 | ListReaders_Call* call = NULL; |
375 | 0 | union |
376 | 0 | { |
377 | 0 | const BYTE* bp; |
378 | 0 | const char* sz; |
379 | 0 | const WCHAR* wz; |
380 | 0 | } string; |
381 | 0 | union |
382 | 0 | { |
383 | 0 | WCHAR** ppw; |
384 | 0 | WCHAR* pw; |
385 | 0 | CHAR* pc; |
386 | 0 | BYTE* pb; |
387 | 0 | } mszReaders; |
388 | |
|
389 | 0 | WINPR_ASSERT(smartcard); |
390 | 0 | WINPR_ASSERT(operation); |
391 | | |
392 | 0 | call = &operation->call.listReaders; |
393 | |
|
394 | 0 | string.bp = call->mszGroups; |
395 | 0 | cchReaders = SCARD_AUTOALLOCATE; |
396 | 0 | status = ret.ReturnCode = wrap(smartcard, SCardListReadersW, operation->hContext, string.wz, |
397 | 0 | (LPWSTR)&mszReaders.pw, &cchReaders); |
398 | |
|
399 | 0 | if (status != SCARD_S_SUCCESS) |
400 | 0 | return scard_log_status_error(TAG, "SCardListReadersW", status); |
401 | | |
402 | 0 | cchReaders = filter_device_by_name_w(smartcard->names, &mszReaders.pw, cchReaders); |
403 | 0 | ret.msz = mszReaders.pb; |
404 | 0 | ret.cBytes = cchReaders * sizeof(WCHAR); |
405 | 0 | status = smartcard_pack_list_readers_return(out, &ret, TRUE); |
406 | |
|
407 | 0 | if (mszReaders.pb) |
408 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaders.pb); |
409 | |
|
410 | 0 | if (status != SCARD_S_SUCCESS) |
411 | 0 | return status; |
412 | | |
413 | 0 | return ret.ReturnCode; |
414 | 0 | } |
415 | | |
416 | | static LONG smartcard_IntroduceReaderGroupA_Call(scard_call_context* smartcard, wStream* out, |
417 | | SMARTCARD_OPERATION* operation) |
418 | 0 | { |
419 | 0 | Long_Return ret = { 0 }; |
420 | 0 | ContextAndStringA_Call* call = NULL; |
421 | |
|
422 | 0 | WINPR_ASSERT(smartcard); |
423 | 0 | WINPR_ASSERT(out); |
424 | 0 | WINPR_ASSERT(operation); |
425 | | |
426 | 0 | call = &operation->call.contextAndStringA; |
427 | 0 | ret.ReturnCode = wrap(smartcard, SCardIntroduceReaderGroupA, operation->hContext, call->sz); |
428 | 0 | scard_log_status_error(TAG, "SCardIntroduceReaderGroupA", ret.ReturnCode); |
429 | 0 | smartcard_trace_long_return(&ret, "IntroduceReaderGroupA"); |
430 | 0 | return ret.ReturnCode; |
431 | 0 | } |
432 | | |
433 | | static LONG smartcard_IntroduceReaderGroupW_Call(scard_call_context* smartcard, wStream* out, |
434 | | SMARTCARD_OPERATION* operation) |
435 | 0 | { |
436 | 0 | Long_Return ret = { 0 }; |
437 | 0 | ContextAndStringW_Call* call = NULL; |
438 | |
|
439 | 0 | WINPR_ASSERT(smartcard); |
440 | 0 | WINPR_ASSERT(out); |
441 | 0 | WINPR_ASSERT(operation); |
442 | | |
443 | 0 | call = &operation->call.contextAndStringW; |
444 | 0 | ret.ReturnCode = wrap(smartcard, SCardIntroduceReaderGroupW, operation->hContext, call->sz); |
445 | 0 | scard_log_status_error(TAG, "SCardIntroduceReaderGroupW", ret.ReturnCode); |
446 | 0 | smartcard_trace_long_return(&ret, "IntroduceReaderGroupW"); |
447 | 0 | return ret.ReturnCode; |
448 | 0 | } |
449 | | |
450 | | static LONG smartcard_IntroduceReaderA_Call(scard_call_context* smartcard, wStream* out, |
451 | | SMARTCARD_OPERATION* operation) |
452 | 0 | { |
453 | 0 | Long_Return ret = { 0 }; |
454 | 0 | ContextAndTwoStringA_Call* call = NULL; |
455 | |
|
456 | 0 | WINPR_ASSERT(smartcard); |
457 | 0 | WINPR_ASSERT(out); |
458 | 0 | WINPR_ASSERT(operation); |
459 | | |
460 | 0 | call = &operation->call.contextAndTwoStringA; |
461 | 0 | ret.ReturnCode = |
462 | 0 | wrap(smartcard, SCardIntroduceReaderA, operation->hContext, call->sz1, call->sz2); |
463 | 0 | scard_log_status_error(TAG, "SCardIntroduceReaderA", ret.ReturnCode); |
464 | 0 | smartcard_trace_long_return(&ret, "IntroduceReaderA"); |
465 | 0 | return ret.ReturnCode; |
466 | 0 | } |
467 | | |
468 | | static LONG smartcard_IntroduceReaderW_Call(scard_call_context* smartcard, wStream* out, |
469 | | SMARTCARD_OPERATION* operation) |
470 | 0 | { |
471 | 0 | Long_Return ret = { 0 }; |
472 | 0 | ContextAndTwoStringW_Call* call = NULL; |
473 | |
|
474 | 0 | WINPR_ASSERT(smartcard); |
475 | 0 | WINPR_ASSERT(out); |
476 | 0 | WINPR_ASSERT(operation); |
477 | | |
478 | 0 | call = &operation->call.contextAndTwoStringW; |
479 | 0 | ret.ReturnCode = |
480 | 0 | wrap(smartcard, SCardIntroduceReaderW, operation->hContext, call->sz1, call->sz2); |
481 | 0 | scard_log_status_error(TAG, "SCardIntroduceReaderW", ret.ReturnCode); |
482 | 0 | smartcard_trace_long_return(&ret, "IntroduceReaderW"); |
483 | 0 | return ret.ReturnCode; |
484 | 0 | } |
485 | | |
486 | | static LONG smartcard_ForgetReaderA_Call(scard_call_context* smartcard, wStream* out, |
487 | | SMARTCARD_OPERATION* operation) |
488 | 0 | { |
489 | 0 | Long_Return ret = { 0 }; |
490 | 0 | ContextAndStringA_Call* call = NULL; |
491 | |
|
492 | 0 | WINPR_ASSERT(smartcard); |
493 | 0 | WINPR_ASSERT(out); |
494 | 0 | WINPR_ASSERT(operation); |
495 | | |
496 | 0 | call = &operation->call.contextAndStringA; |
497 | 0 | ret.ReturnCode = wrap(smartcard, SCardForgetReaderA, operation->hContext, call->sz); |
498 | 0 | scard_log_status_error(TAG, "SCardForgetReaderA", ret.ReturnCode); |
499 | 0 | smartcard_trace_long_return(&ret, "SCardForgetReaderA"); |
500 | 0 | return ret.ReturnCode; |
501 | 0 | } |
502 | | |
503 | | static LONG smartcard_ForgetReaderW_Call(scard_call_context* smartcard, wStream* out, |
504 | | SMARTCARD_OPERATION* operation) |
505 | 0 | { |
506 | 0 | Long_Return ret = { 0 }; |
507 | 0 | ContextAndStringW_Call* call = NULL; |
508 | |
|
509 | 0 | WINPR_ASSERT(smartcard); |
510 | 0 | WINPR_ASSERT(out); |
511 | 0 | WINPR_ASSERT(operation); |
512 | | |
513 | 0 | call = &operation->call.contextAndStringW; |
514 | 0 | ret.ReturnCode = wrap(smartcard, SCardForgetReaderW, operation->hContext, call->sz); |
515 | 0 | scard_log_status_error(TAG, "SCardForgetReaderW", ret.ReturnCode); |
516 | 0 | smartcard_trace_long_return(&ret, "SCardForgetReaderW"); |
517 | 0 | return ret.ReturnCode; |
518 | 0 | } |
519 | | |
520 | | static LONG smartcard_AddReaderToGroupA_Call(scard_call_context* smartcard, wStream* out, |
521 | | SMARTCARD_OPERATION* operation) |
522 | 0 | { |
523 | 0 | Long_Return ret = { 0 }; |
524 | 0 | ContextAndTwoStringA_Call* call = NULL; |
525 | |
|
526 | 0 | WINPR_ASSERT(smartcard); |
527 | 0 | WINPR_ASSERT(out); |
528 | 0 | WINPR_ASSERT(operation); |
529 | | |
530 | 0 | call = &operation->call.contextAndTwoStringA; |
531 | 0 | ret.ReturnCode = |
532 | 0 | wrap(smartcard, SCardAddReaderToGroupA, operation->hContext, call->sz1, call->sz2); |
533 | 0 | scard_log_status_error(TAG, "SCardAddReaderToGroupA", ret.ReturnCode); |
534 | 0 | smartcard_trace_long_return(&ret, "SCardAddReaderToGroupA"); |
535 | 0 | return ret.ReturnCode; |
536 | 0 | } |
537 | | |
538 | | static LONG smartcard_AddReaderToGroupW_Call(scard_call_context* smartcard, wStream* out, |
539 | | SMARTCARD_OPERATION* operation) |
540 | 0 | { |
541 | 0 | Long_Return ret = { 0 }; |
542 | 0 | ContextAndTwoStringW_Call* call = NULL; |
543 | |
|
544 | 0 | WINPR_ASSERT(smartcard); |
545 | 0 | WINPR_ASSERT(out); |
546 | 0 | WINPR_ASSERT(operation); |
547 | | |
548 | 0 | call = &operation->call.contextAndTwoStringW; |
549 | 0 | ret.ReturnCode = |
550 | 0 | wrap(smartcard, SCardAddReaderToGroupW, operation->hContext, call->sz1, call->sz2); |
551 | 0 | scard_log_status_error(TAG, "SCardAddReaderToGroupW", ret.ReturnCode); |
552 | 0 | smartcard_trace_long_return(&ret, "SCardAddReaderToGroupA"); |
553 | 0 | return ret.ReturnCode; |
554 | 0 | } |
555 | | |
556 | | static LONG smartcard_RemoveReaderFromGroupA_Call(scard_call_context* smartcard, wStream* out, |
557 | | SMARTCARD_OPERATION* operation) |
558 | 0 | { |
559 | 0 | Long_Return ret = { 0 }; |
560 | 0 | ContextAndTwoStringA_Call* call = NULL; |
561 | |
|
562 | 0 | WINPR_ASSERT(smartcard); |
563 | 0 | WINPR_ASSERT(out); |
564 | 0 | WINPR_ASSERT(operation); |
565 | | |
566 | 0 | call = &operation->call.contextAndTwoStringA; |
567 | 0 | ret.ReturnCode = |
568 | 0 | wrap(smartcard, SCardRemoveReaderFromGroupA, operation->hContext, call->sz1, call->sz2); |
569 | 0 | scard_log_status_error(TAG, "SCardRemoveReaderFromGroupA", ret.ReturnCode); |
570 | 0 | smartcard_trace_long_return(&ret, "SCardRemoveReaderFromGroupA"); |
571 | 0 | return ret.ReturnCode; |
572 | 0 | } |
573 | | |
574 | | static LONG smartcard_RemoveReaderFromGroupW_Call(scard_call_context* smartcard, wStream* out, |
575 | | SMARTCARD_OPERATION* operation) |
576 | 0 | { |
577 | 0 | Long_Return ret = { 0 }; |
578 | 0 | ContextAndTwoStringW_Call* call = NULL; |
579 | |
|
580 | 0 | WINPR_ASSERT(smartcard); |
581 | 0 | WINPR_ASSERT(out); |
582 | 0 | WINPR_ASSERT(operation); |
583 | | |
584 | 0 | call = &operation->call.contextAndTwoStringW; |
585 | 0 | ret.ReturnCode = |
586 | 0 | wrap(smartcard, SCardRemoveReaderFromGroupW, operation->hContext, call->sz1, call->sz2); |
587 | 0 | scard_log_status_error(TAG, "SCardRemoveReaderFromGroupW", ret.ReturnCode); |
588 | 0 | smartcard_trace_long_return(&ret, "SCardRemoveReaderFromGroupW"); |
589 | 0 | return ret.ReturnCode; |
590 | 0 | } |
591 | | |
592 | | static LONG smartcard_LocateCardsA_Call(scard_call_context* smartcard, wStream* out, |
593 | | SMARTCARD_OPERATION* operation) |
594 | 0 | { |
595 | 0 | LONG status = 0; |
596 | 0 | LocateCards_Return ret = { 0 }; |
597 | 0 | LocateCardsA_Call* call = NULL; |
598 | |
|
599 | 0 | WINPR_ASSERT(smartcard); |
600 | 0 | WINPR_ASSERT(operation); |
601 | | |
602 | 0 | call = &operation->call.locateCardsA; |
603 | |
|
604 | 0 | ret.ReturnCode = wrap(smartcard, SCardLocateCardsA, operation->hContext, call->mszCards, |
605 | 0 | call->rgReaderStates, call->cReaders); |
606 | 0 | scard_log_status_error(TAG, "SCardLocateCardsA", ret.ReturnCode); |
607 | 0 | ret.cReaders = call->cReaders; |
608 | 0 | ret.rgReaderStates = NULL; |
609 | |
|
610 | 0 | if (ret.cReaders > 0) |
611 | 0 | { |
612 | 0 | ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return)); |
613 | |
|
614 | 0 | if (!ret.rgReaderStates) |
615 | 0 | return STATUS_NO_MEMORY; |
616 | 0 | } |
617 | | |
618 | 0 | for (UINT32 x = 0; x < ret.cReaders; x++) |
619 | 0 | { |
620 | 0 | ret.rgReaderStates[x].dwCurrentState = call->rgReaderStates[x].dwCurrentState; |
621 | 0 | ret.rgReaderStates[x].dwEventState = call->rgReaderStates[x].dwEventState; |
622 | 0 | ret.rgReaderStates[x].cbAtr = call->rgReaderStates[x].cbAtr; |
623 | 0 | CopyMemory(&(ret.rgReaderStates[x].rgbAtr), &(call->rgReaderStates[x].rgbAtr), |
624 | 0 | sizeof(ret.rgReaderStates[x].rgbAtr)); |
625 | 0 | } |
626 | |
|
627 | 0 | status = smartcard_pack_locate_cards_return(out, &ret); |
628 | |
|
629 | 0 | if (status != SCARD_S_SUCCESS) |
630 | 0 | return status; |
631 | | |
632 | 0 | return ret.ReturnCode; |
633 | 0 | } |
634 | | |
635 | | static LONG smartcard_LocateCardsW_Call(scard_call_context* smartcard, wStream* out, |
636 | | SMARTCARD_OPERATION* operation) |
637 | 0 | { |
638 | 0 | LONG status = 0; |
639 | 0 | LocateCards_Return ret = { 0 }; |
640 | 0 | LocateCardsW_Call* call = NULL; |
641 | |
|
642 | 0 | WINPR_ASSERT(smartcard); |
643 | 0 | WINPR_ASSERT(operation); |
644 | | |
645 | 0 | call = &operation->call.locateCardsW; |
646 | |
|
647 | 0 | ret.ReturnCode = wrap(smartcard, SCardLocateCardsW, operation->hContext, call->mszCards, |
648 | 0 | call->rgReaderStates, call->cReaders); |
649 | 0 | scard_log_status_error(TAG, "SCardLocateCardsW", ret.ReturnCode); |
650 | 0 | ret.cReaders = call->cReaders; |
651 | 0 | ret.rgReaderStates = NULL; |
652 | |
|
653 | 0 | if (ret.cReaders > 0) |
654 | 0 | { |
655 | 0 | ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return)); |
656 | |
|
657 | 0 | if (!ret.rgReaderStates) |
658 | 0 | return STATUS_NO_MEMORY; |
659 | 0 | } |
660 | | |
661 | 0 | for (UINT32 x = 0; x < ret.cReaders; x++) |
662 | 0 | { |
663 | 0 | ret.rgReaderStates[x].dwCurrentState = call->rgReaderStates[x].dwCurrentState; |
664 | 0 | ret.rgReaderStates[x].dwEventState = call->rgReaderStates[x].dwEventState; |
665 | 0 | ret.rgReaderStates[x].cbAtr = call->rgReaderStates[x].cbAtr; |
666 | 0 | CopyMemory(&(ret.rgReaderStates[x].rgbAtr), &(call->rgReaderStates[x].rgbAtr), |
667 | 0 | sizeof(ret.rgReaderStates[x].rgbAtr)); |
668 | 0 | } |
669 | |
|
670 | 0 | status = smartcard_pack_locate_cards_return(out, &ret); |
671 | |
|
672 | 0 | if (status != SCARD_S_SUCCESS) |
673 | 0 | return status; |
674 | | |
675 | 0 | return ret.ReturnCode; |
676 | 0 | } |
677 | | |
678 | | static LONG smartcard_ReadCacheA_Call(scard_call_context* smartcard, wStream* out, |
679 | | SMARTCARD_OPERATION* operation) |
680 | 0 | { |
681 | 0 | LONG status = 0; |
682 | 0 | BOOL autoalloc = 0; |
683 | 0 | ReadCache_Return ret = { 0 }; |
684 | 0 | ReadCacheA_Call* call = NULL; |
685 | |
|
686 | 0 | WINPR_ASSERT(smartcard); |
687 | 0 | WINPR_ASSERT(out); |
688 | 0 | WINPR_ASSERT(operation); |
689 | | |
690 | 0 | call = &operation->call.readCacheA; |
691 | 0 | autoalloc = (call->Common.cbDataLen == SCARD_AUTOALLOCATE); |
692 | |
|
693 | 0 | if (!call->Common.fPbDataIsNULL) |
694 | 0 | { |
695 | 0 | ret.cbDataLen = call->Common.cbDataLen; |
696 | 0 | if (!autoalloc) |
697 | 0 | { |
698 | 0 | ret.pbData = malloc(ret.cbDataLen); |
699 | 0 | if (!ret.pbData) |
700 | 0 | return SCARD_F_INTERNAL_ERROR; |
701 | 0 | } |
702 | 0 | } |
703 | | |
704 | 0 | if (autoalloc) |
705 | 0 | ret.ReturnCode = wrap(smartcard, SCardReadCacheA, operation->hContext, |
706 | 0 | call->Common.CardIdentifier, call->Common.FreshnessCounter, |
707 | 0 | call->szLookupName, (BYTE*)&ret.pbData, &ret.cbDataLen); |
708 | 0 | else |
709 | 0 | ret.ReturnCode = |
710 | 0 | wrap(smartcard, SCardReadCacheA, operation->hContext, call->Common.CardIdentifier, |
711 | 0 | call->Common.FreshnessCounter, call->szLookupName, ret.pbData, &ret.cbDataLen); |
712 | 0 | if ((ret.ReturnCode != SCARD_W_CACHE_ITEM_NOT_FOUND) && |
713 | 0 | (ret.ReturnCode != SCARD_W_CACHE_ITEM_STALE)) |
714 | 0 | { |
715 | 0 | scard_log_status_error(TAG, "SCardReadCacheA", ret.ReturnCode); |
716 | 0 | } |
717 | |
|
718 | 0 | status = smartcard_pack_read_cache_return(out, &ret); |
719 | 0 | if (autoalloc) |
720 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbData); |
721 | 0 | else |
722 | 0 | free(ret.pbData); |
723 | 0 | if (status != SCARD_S_SUCCESS) |
724 | 0 | return status; |
725 | | |
726 | 0 | return ret.ReturnCode; |
727 | 0 | } |
728 | | |
729 | | static LONG smartcard_ReadCacheW_Call(scard_call_context* smartcard, wStream* out, |
730 | | SMARTCARD_OPERATION* operation) |
731 | 0 | { |
732 | 0 | LONG status = 0; |
733 | 0 | ReadCache_Return ret = { 0 }; |
734 | 0 | ReadCacheW_Call* call = NULL; |
735 | |
|
736 | 0 | WINPR_ASSERT(smartcard); |
737 | 0 | WINPR_ASSERT(out); |
738 | 0 | WINPR_ASSERT(operation); |
739 | | |
740 | 0 | call = &operation->call.readCacheW; |
741 | |
|
742 | 0 | if (!call->Common.fPbDataIsNULL) |
743 | 0 | ret.cbDataLen = SCARD_AUTOALLOCATE; |
744 | |
|
745 | 0 | ret.ReturnCode = |
746 | 0 | wrap(smartcard, SCardReadCacheW, operation->hContext, call->Common.CardIdentifier, |
747 | 0 | call->Common.FreshnessCounter, call->szLookupName, (BYTE*)&ret.pbData, &ret.cbDataLen); |
748 | |
|
749 | 0 | if ((ret.ReturnCode != SCARD_W_CACHE_ITEM_NOT_FOUND) && |
750 | 0 | (ret.ReturnCode != SCARD_W_CACHE_ITEM_STALE)) |
751 | 0 | { |
752 | 0 | scard_log_status_error(TAG, "SCardReadCacheA", ret.ReturnCode); |
753 | 0 | } |
754 | |
|
755 | 0 | status = smartcard_pack_read_cache_return(out, &ret); |
756 | |
|
757 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbData); |
758 | |
|
759 | 0 | if (status != SCARD_S_SUCCESS) |
760 | 0 | return status; |
761 | | |
762 | 0 | return ret.ReturnCode; |
763 | 0 | } |
764 | | |
765 | | static LONG smartcard_WriteCacheA_Call(scard_call_context* smartcard, wStream* out, |
766 | | SMARTCARD_OPERATION* operation) |
767 | 0 | { |
768 | 0 | Long_Return ret = { 0 }; |
769 | 0 | WriteCacheA_Call* call = NULL; |
770 | |
|
771 | 0 | WINPR_ASSERT(smartcard); |
772 | 0 | WINPR_ASSERT(out); |
773 | 0 | WINPR_ASSERT(operation); |
774 | | |
775 | 0 | call = &operation->call.writeCacheA; |
776 | |
|
777 | 0 | ret.ReturnCode = wrap(smartcard, SCardWriteCacheA, operation->hContext, |
778 | 0 | call->Common.CardIdentifier, call->Common.FreshnessCounter, |
779 | 0 | call->szLookupName, call->Common.pbData, call->Common.cbDataLen); |
780 | 0 | scard_log_status_error(TAG, "SCardWriteCacheA", ret.ReturnCode); |
781 | 0 | smartcard_trace_long_return(&ret, "SCardWriteCacheA"); |
782 | 0 | return ret.ReturnCode; |
783 | 0 | } |
784 | | |
785 | | static LONG smartcard_WriteCacheW_Call(scard_call_context* smartcard, wStream* out, |
786 | | SMARTCARD_OPERATION* operation) |
787 | 0 | { |
788 | 0 | Long_Return ret = { 0 }; |
789 | 0 | WriteCacheW_Call* call = NULL; |
790 | |
|
791 | 0 | WINPR_ASSERT(smartcard); |
792 | 0 | WINPR_ASSERT(out); |
793 | 0 | WINPR_ASSERT(operation); |
794 | | |
795 | 0 | call = &operation->call.writeCacheW; |
796 | |
|
797 | 0 | ret.ReturnCode = wrap(smartcard, SCardWriteCacheW, operation->hContext, |
798 | 0 | call->Common.CardIdentifier, call->Common.FreshnessCounter, |
799 | 0 | call->szLookupName, call->Common.pbData, call->Common.cbDataLen); |
800 | 0 | scard_log_status_error(TAG, "SCardWriteCacheW", ret.ReturnCode); |
801 | 0 | smartcard_trace_long_return(&ret, "SCardWriteCacheW"); |
802 | 0 | return ret.ReturnCode; |
803 | 0 | } |
804 | | |
805 | | static LONG smartcard_GetTransmitCount_Call(scard_call_context* smartcard, wStream* out, |
806 | | SMARTCARD_OPERATION* operation) |
807 | 0 | { |
808 | 0 | LONG status = 0; |
809 | 0 | GetTransmitCount_Return ret = { 0 }; |
810 | |
|
811 | 0 | WINPR_ASSERT(smartcard); |
812 | 0 | WINPR_ASSERT(out); |
813 | 0 | WINPR_ASSERT(operation); |
814 | | |
815 | 0 | ret.ReturnCode = wrap(smartcard, SCardGetTransmitCount, operation->hCard, &ret.cTransmitCount); |
816 | 0 | scard_log_status_error(TAG, "SCardGetTransmitCount", ret.ReturnCode); |
817 | 0 | status = smartcard_pack_get_transmit_count_return(out, &ret); |
818 | 0 | if (status != SCARD_S_SUCCESS) |
819 | 0 | return status; |
820 | | |
821 | 0 | return ret.ReturnCode; |
822 | 0 | } |
823 | | |
824 | | static LONG smartcard_ReleaseStartedEvent_Call(scard_call_context* smartcard, wStream* out, |
825 | | SMARTCARD_OPERATION* operation) |
826 | 0 | { |
827 | 0 | WINPR_UNUSED(smartcard); |
828 | 0 | WINPR_UNUSED(out); |
829 | 0 | WINPR_UNUSED(operation); |
830 | |
|
831 | 0 | WLog_WARN(TAG, "According to [MS-RDPESC] 3.1.4 Message Processing Events and Sequencing Rules " |
832 | 0 | "this is not supported?!?"); |
833 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
834 | 0 | } |
835 | | |
836 | | static LONG smartcard_GetReaderIcon_Call(scard_call_context* smartcard, wStream* out, |
837 | | SMARTCARD_OPERATION* operation) |
838 | 0 | { |
839 | 0 | LONG status = 0; |
840 | 0 | GetReaderIcon_Return ret = { 0 }; |
841 | 0 | GetReaderIcon_Call* call = NULL; |
842 | |
|
843 | 0 | WINPR_ASSERT(smartcard); |
844 | 0 | WINPR_ASSERT(out); |
845 | 0 | WINPR_ASSERT(operation); |
846 | | |
847 | 0 | call = &operation->call.getReaderIcon; |
848 | |
|
849 | 0 | ret.cbDataLen = SCARD_AUTOALLOCATE; |
850 | 0 | ret.ReturnCode = wrap(smartcard, SCardGetReaderIconW, operation->hContext, call->szReaderName, |
851 | 0 | (LPBYTE)&ret.pbData, &ret.cbDataLen); |
852 | 0 | scard_log_status_error(TAG, "SCardGetReaderIconW", ret.ReturnCode); |
853 | |
|
854 | 0 | status = smartcard_pack_get_reader_icon_return(out, &ret); |
855 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbData); |
856 | 0 | if (status != SCARD_S_SUCCESS) |
857 | 0 | return status; |
858 | | |
859 | 0 | return ret.ReturnCode; |
860 | 0 | } |
861 | | |
862 | | static LONG smartcard_GetDeviceTypeId_Call(scard_call_context* smartcard, wStream* out, |
863 | | SMARTCARD_OPERATION* operation) |
864 | 0 | { |
865 | 0 | LONG status = 0; |
866 | 0 | GetDeviceTypeId_Return ret = { 0 }; |
867 | 0 | GetDeviceTypeId_Call* call = NULL; |
868 | |
|
869 | 0 | WINPR_ASSERT(smartcard); |
870 | 0 | WINPR_ASSERT(out); |
871 | 0 | WINPR_ASSERT(operation); |
872 | | |
873 | 0 | call = &operation->call.getDeviceTypeId; |
874 | |
|
875 | 0 | ret.ReturnCode = wrap(smartcard, SCardGetDeviceTypeIdW, operation->hContext, call->szReaderName, |
876 | 0 | &ret.dwDeviceId); |
877 | 0 | scard_log_status_error(TAG, "SCardGetDeviceTypeIdW", ret.ReturnCode); |
878 | |
|
879 | 0 | status = smartcard_pack_device_type_id_return(out, &ret); |
880 | 0 | if (status != SCARD_S_SUCCESS) |
881 | 0 | return status; |
882 | | |
883 | 0 | return ret.ReturnCode; |
884 | 0 | } |
885 | | |
886 | | static LONG smartcard_GetStatusChangeA_Call(scard_call_context* smartcard, wStream* out, |
887 | | SMARTCARD_OPERATION* operation) |
888 | 0 | { |
889 | 0 | LONG status = STATUS_NO_MEMORY; |
890 | 0 | DWORD dwTimeOut = 0; |
891 | 0 | const DWORD dwTimeStep = 100; |
892 | 0 | GetStatusChange_Return ret = { 0 }; |
893 | 0 | GetStatusChangeA_Call* call = NULL; |
894 | 0 | LPSCARD_READERSTATEA rgReaderStates = NULL; |
895 | |
|
896 | 0 | WINPR_ASSERT(smartcard); |
897 | 0 | WINPR_ASSERT(out); |
898 | 0 | WINPR_ASSERT(operation); |
899 | | |
900 | 0 | call = &operation->call.getStatusChangeA; |
901 | 0 | dwTimeOut = call->dwTimeOut; |
902 | |
|
903 | 0 | if (call->cReaders > 0) |
904 | 0 | { |
905 | 0 | ret.cReaders = call->cReaders; |
906 | 0 | rgReaderStates = calloc(ret.cReaders, sizeof(SCARD_READERSTATEA)); |
907 | 0 | ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return)); |
908 | 0 | if (!rgReaderStates || !ret.rgReaderStates) |
909 | 0 | goto fail; |
910 | 0 | } |
911 | | |
912 | 0 | for (UINT32 x = 0; x < MAX(1, dwTimeOut);) |
913 | 0 | { |
914 | 0 | if (call->cReaders > 0) |
915 | 0 | memcpy(rgReaderStates, call->rgReaderStates, |
916 | 0 | call->cReaders * sizeof(SCARD_READERSTATEA)); |
917 | 0 | ret.ReturnCode = wrap(smartcard, SCardGetStatusChangeA, operation->hContext, |
918 | 0 | MIN(dwTimeOut, dwTimeStep), rgReaderStates, call->cReaders); |
919 | 0 | if (ret.ReturnCode != SCARD_E_TIMEOUT) |
920 | 0 | break; |
921 | 0 | if (WaitForSingleObject(smartcard->stopEvent, 0) == WAIT_OBJECT_0) |
922 | 0 | break; |
923 | 0 | if (dwTimeOut != INFINITE) |
924 | 0 | x += dwTimeStep; |
925 | 0 | } |
926 | 0 | scard_log_status_error(TAG, "SCardGetStatusChangeA", ret.ReturnCode); |
927 | |
|
928 | 0 | for (UINT32 index = 0; index < ret.cReaders; index++) |
929 | 0 | { |
930 | 0 | const SCARD_READERSTATEA* cur = &rgReaderStates[index]; |
931 | 0 | ReaderState_Return* rout = &ret.rgReaderStates[index]; |
932 | |
|
933 | 0 | rout->dwCurrentState = cur->dwCurrentState; |
934 | 0 | rout->dwEventState = cur->dwEventState; |
935 | 0 | rout->cbAtr = cur->cbAtr; |
936 | 0 | CopyMemory(&(rout->rgbAtr), cur->rgbAtr, sizeof(rout->rgbAtr)); |
937 | 0 | } |
938 | |
|
939 | 0 | status = smartcard_pack_get_status_change_return(out, &ret, TRUE); |
940 | 0 | fail: |
941 | 0 | free(ret.rgReaderStates); |
942 | 0 | free(rgReaderStates); |
943 | 0 | if (status != SCARD_S_SUCCESS) |
944 | 0 | return status; |
945 | 0 | return ret.ReturnCode; |
946 | 0 | } |
947 | | |
948 | | static LONG smartcard_GetStatusChangeW_Call(scard_call_context* smartcard, wStream* out, |
949 | | SMARTCARD_OPERATION* operation) |
950 | 0 | { |
951 | 0 | LONG status = STATUS_NO_MEMORY; |
952 | 0 | DWORD dwTimeOut = 0; |
953 | 0 | const DWORD dwTimeStep = 100; |
954 | 0 | GetStatusChange_Return ret = { 0 }; |
955 | 0 | GetStatusChangeW_Call* call = NULL; |
956 | 0 | LPSCARD_READERSTATEW rgReaderStates = NULL; |
957 | |
|
958 | 0 | WINPR_ASSERT(smartcard); |
959 | 0 | WINPR_ASSERT(out); |
960 | 0 | WINPR_ASSERT(operation); |
961 | | |
962 | 0 | call = &operation->call.getStatusChangeW; |
963 | 0 | dwTimeOut = call->dwTimeOut; |
964 | |
|
965 | 0 | if (call->cReaders > 0) |
966 | 0 | { |
967 | 0 | ret.cReaders = call->cReaders; |
968 | 0 | rgReaderStates = calloc(ret.cReaders, sizeof(SCARD_READERSTATEW)); |
969 | 0 | ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return)); |
970 | 0 | if (!rgReaderStates || !ret.rgReaderStates) |
971 | 0 | goto fail; |
972 | 0 | } |
973 | | |
974 | 0 | for (UINT32 x = 0; x < MAX(1, dwTimeOut);) |
975 | 0 | { |
976 | 0 | if (call->cReaders > 0) |
977 | 0 | memcpy(rgReaderStates, call->rgReaderStates, |
978 | 0 | call->cReaders * sizeof(SCARD_READERSTATEW)); |
979 | 0 | { |
980 | 0 | ret.ReturnCode = wrap(smartcard, SCardGetStatusChangeW, operation->hContext, |
981 | 0 | MIN(dwTimeOut, dwTimeStep), rgReaderStates, call->cReaders); |
982 | 0 | } |
983 | 0 | if (ret.ReturnCode != SCARD_E_TIMEOUT) |
984 | 0 | break; |
985 | 0 | if (WaitForSingleObject(smartcard->stopEvent, 0) == WAIT_OBJECT_0) |
986 | 0 | break; |
987 | 0 | if (dwTimeOut != INFINITE) |
988 | 0 | x += dwTimeStep; |
989 | 0 | } |
990 | 0 | scard_log_status_error(TAG, "SCardGetStatusChangeW", ret.ReturnCode); |
991 | |
|
992 | 0 | for (UINT32 index = 0; index < ret.cReaders; index++) |
993 | 0 | { |
994 | 0 | const SCARD_READERSTATEW* cur = &rgReaderStates[index]; |
995 | 0 | ReaderState_Return* rout = &ret.rgReaderStates[index]; |
996 | |
|
997 | 0 | rout->dwCurrentState = cur->dwCurrentState; |
998 | 0 | rout->dwEventState = cur->dwEventState; |
999 | 0 | rout->cbAtr = cur->cbAtr; |
1000 | 0 | CopyMemory(&(rout->rgbAtr), cur->rgbAtr, sizeof(rout->rgbAtr)); |
1001 | 0 | } |
1002 | |
|
1003 | 0 | status = smartcard_pack_get_status_change_return(out, &ret, TRUE); |
1004 | 0 | fail: |
1005 | 0 | free(ret.rgReaderStates); |
1006 | 0 | free(rgReaderStates); |
1007 | 0 | if (status != SCARD_S_SUCCESS) |
1008 | 0 | return status; |
1009 | 0 | return ret.ReturnCode; |
1010 | 0 | } |
1011 | | |
1012 | | static LONG smartcard_Cancel_Call(scard_call_context* smartcard, wStream* out, |
1013 | | SMARTCARD_OPERATION* operation) |
1014 | 0 | { |
1015 | 0 | Long_Return ret = { 0 }; |
1016 | |
|
1017 | 0 | WINPR_ASSERT(smartcard); |
1018 | 0 | WINPR_ASSERT(out); |
1019 | 0 | WINPR_ASSERT(operation); |
1020 | | |
1021 | 0 | ret.ReturnCode = wrap(smartcard, SCardCancel, operation->hContext); |
1022 | 0 | scard_log_status_error(TAG, "SCardCancel", ret.ReturnCode); |
1023 | 0 | smartcard_trace_long_return(&ret, "Cancel"); |
1024 | 0 | return ret.ReturnCode; |
1025 | 0 | } |
1026 | | |
1027 | | static LONG smartcard_ConnectA_Call(scard_call_context* smartcard, wStream* out, |
1028 | | SMARTCARD_OPERATION* operation) |
1029 | 0 | { |
1030 | 0 | LONG status = 0; |
1031 | 0 | SCARDHANDLE hCard = 0; |
1032 | 0 | Connect_Return ret = { 0 }; |
1033 | 0 | ConnectA_Call* call = NULL; |
1034 | |
|
1035 | 0 | WINPR_ASSERT(smartcard); |
1036 | 0 | WINPR_ASSERT(out); |
1037 | 0 | WINPR_ASSERT(operation); |
1038 | | |
1039 | 0 | call = &operation->call.connectA; |
1040 | |
|
1041 | 0 | if ((call->Common.dwPreferredProtocols == SCARD_PROTOCOL_UNDEFINED) && |
1042 | 0 | (call->Common.dwShareMode != SCARD_SHARE_DIRECT)) |
1043 | 0 | { |
1044 | 0 | call->Common.dwPreferredProtocols = SCARD_PROTOCOL_Tx; |
1045 | 0 | } |
1046 | |
|
1047 | 0 | ret.ReturnCode = wrap(smartcard, SCardConnectA, operation->hContext, (char*)call->szReader, |
1048 | 0 | call->Common.dwShareMode, call->Common.dwPreferredProtocols, &hCard, |
1049 | 0 | &ret.dwActiveProtocol); |
1050 | 0 | smartcard_scard_context_native_to_redir(&(ret.hContext), operation->hContext); |
1051 | 0 | smartcard_scard_handle_native_to_redir(&(ret.hCard), hCard); |
1052 | |
|
1053 | 0 | status = smartcard_pack_connect_return(out, &ret); |
1054 | 0 | if (status != SCARD_S_SUCCESS) |
1055 | 0 | goto out_fail; |
1056 | | |
1057 | 0 | status = ret.ReturnCode; |
1058 | 0 | out_fail: |
1059 | |
|
1060 | 0 | return status; |
1061 | 0 | } |
1062 | | |
1063 | | static LONG smartcard_ConnectW_Call(scard_call_context* smartcard, wStream* out, |
1064 | | SMARTCARD_OPERATION* operation) |
1065 | 0 | { |
1066 | 0 | LONG status = 0; |
1067 | 0 | SCARDHANDLE hCard = 0; |
1068 | 0 | Connect_Return ret = { 0 }; |
1069 | 0 | ConnectW_Call* call = NULL; |
1070 | |
|
1071 | 0 | WINPR_ASSERT(smartcard); |
1072 | 0 | WINPR_ASSERT(out); |
1073 | 0 | WINPR_ASSERT(operation); |
1074 | | |
1075 | 0 | call = &operation->call.connectW; |
1076 | |
|
1077 | 0 | if ((call->Common.dwPreferredProtocols == SCARD_PROTOCOL_UNDEFINED) && |
1078 | 0 | (call->Common.dwShareMode != SCARD_SHARE_DIRECT)) |
1079 | 0 | { |
1080 | 0 | call->Common.dwPreferredProtocols = SCARD_PROTOCOL_Tx; |
1081 | 0 | } |
1082 | |
|
1083 | 0 | ret.ReturnCode = wrap(smartcard, SCardConnectW, operation->hContext, (WCHAR*)call->szReader, |
1084 | 0 | call->Common.dwShareMode, call->Common.dwPreferredProtocols, &hCard, |
1085 | 0 | &ret.dwActiveProtocol); |
1086 | 0 | smartcard_scard_context_native_to_redir(&(ret.hContext), operation->hContext); |
1087 | 0 | smartcard_scard_handle_native_to_redir(&(ret.hCard), hCard); |
1088 | |
|
1089 | 0 | status = smartcard_pack_connect_return(out, &ret); |
1090 | 0 | if (status != SCARD_S_SUCCESS) |
1091 | 0 | goto out_fail; |
1092 | | |
1093 | 0 | status = ret.ReturnCode; |
1094 | 0 | out_fail: |
1095 | |
|
1096 | 0 | return status; |
1097 | 0 | } |
1098 | | |
1099 | | static LONG smartcard_Reconnect_Call(scard_call_context* smartcard, wStream* out, |
1100 | | SMARTCARD_OPERATION* operation) |
1101 | 0 | { |
1102 | 0 | LONG status = 0; |
1103 | 0 | Reconnect_Return ret = { 0 }; |
1104 | 0 | Reconnect_Call* call = NULL; |
1105 | |
|
1106 | 0 | WINPR_ASSERT(smartcard); |
1107 | 0 | WINPR_ASSERT(out); |
1108 | 0 | WINPR_ASSERT(operation); |
1109 | | |
1110 | 0 | call = &operation->call.reconnect; |
1111 | 0 | ret.ReturnCode = |
1112 | 0 | wrap(smartcard, SCardReconnect, operation->hCard, call->dwShareMode, |
1113 | 0 | call->dwPreferredProtocols, call->dwInitialization, &ret.dwActiveProtocol); |
1114 | 0 | scard_log_status_error(TAG, "SCardReconnect", ret.ReturnCode); |
1115 | 0 | status = smartcard_pack_reconnect_return(out, &ret); |
1116 | 0 | if (status != SCARD_S_SUCCESS) |
1117 | 0 | return status; |
1118 | | |
1119 | 0 | return ret.ReturnCode; |
1120 | 0 | } |
1121 | | |
1122 | | static LONG smartcard_Disconnect_Call(scard_call_context* smartcard, wStream* out, |
1123 | | SMARTCARD_OPERATION* operation) |
1124 | 0 | { |
1125 | 0 | Long_Return ret = { 0 }; |
1126 | 0 | HCardAndDisposition_Call* call = NULL; |
1127 | |
|
1128 | 0 | WINPR_ASSERT(smartcard); |
1129 | 0 | WINPR_ASSERT(out); |
1130 | 0 | WINPR_ASSERT(operation); |
1131 | | |
1132 | 0 | call = &operation->call.hCardAndDisposition; |
1133 | |
|
1134 | 0 | ret.ReturnCode = wrap(smartcard, SCardDisconnect, operation->hCard, call->dwDisposition); |
1135 | 0 | scard_log_status_error(TAG, "SCardDisconnect", ret.ReturnCode); |
1136 | 0 | smartcard_trace_long_return(&ret, "Disconnect"); |
1137 | |
|
1138 | 0 | return ret.ReturnCode; |
1139 | 0 | } |
1140 | | |
1141 | | static LONG smartcard_BeginTransaction_Call(scard_call_context* smartcard, wStream* out, |
1142 | | SMARTCARD_OPERATION* operation) |
1143 | 0 | { |
1144 | 0 | Long_Return ret = { 0 }; |
1145 | |
|
1146 | 0 | WINPR_ASSERT(smartcard); |
1147 | 0 | WINPR_ASSERT(out); |
1148 | 0 | WINPR_ASSERT(operation); |
1149 | | |
1150 | 0 | ret.ReturnCode = wrap(smartcard, SCardBeginTransaction, operation->hCard); |
1151 | 0 | scard_log_status_error(TAG, "SCardBeginTransaction", ret.ReturnCode); |
1152 | 0 | smartcard_trace_long_return(&ret, "BeginTransaction"); |
1153 | 0 | return ret.ReturnCode; |
1154 | 0 | } |
1155 | | |
1156 | | static LONG smartcard_EndTransaction_Call(scard_call_context* smartcard, wStream* out, |
1157 | | SMARTCARD_OPERATION* operation) |
1158 | 0 | { |
1159 | 0 | Long_Return ret = { 0 }; |
1160 | 0 | HCardAndDisposition_Call* call = NULL; |
1161 | |
|
1162 | 0 | WINPR_ASSERT(smartcard); |
1163 | 0 | WINPR_ASSERT(out); |
1164 | 0 | WINPR_ASSERT(operation); |
1165 | | |
1166 | 0 | call = &operation->call.hCardAndDisposition; |
1167 | |
|
1168 | 0 | ret.ReturnCode = wrap(smartcard, SCardEndTransaction, operation->hCard, call->dwDisposition); |
1169 | 0 | scard_log_status_error(TAG, "SCardEndTransaction", ret.ReturnCode); |
1170 | 0 | smartcard_trace_long_return(&ret, "EndTransaction"); |
1171 | 0 | return ret.ReturnCode; |
1172 | 0 | } |
1173 | | |
1174 | | static LONG smartcard_State_Call(scard_call_context* smartcard, wStream* out, |
1175 | | SMARTCARD_OPERATION* operation) |
1176 | 0 | { |
1177 | 0 | LONG status = 0; |
1178 | 0 | State_Return ret = { 0 }; |
1179 | |
|
1180 | 0 | WINPR_ASSERT(smartcard); |
1181 | 0 | WINPR_ASSERT(out); |
1182 | 0 | WINPR_ASSERT(operation); |
1183 | | |
1184 | 0 | ret.cbAtrLen = SCARD_ATR_LENGTH; |
1185 | 0 | ret.ReturnCode = wrap(smartcard, SCardState, operation->hCard, &ret.dwState, &ret.dwProtocol, |
1186 | 0 | (BYTE*)&ret.rgAtr, &ret.cbAtrLen); |
1187 | |
|
1188 | 0 | scard_log_status_error(TAG, "SCardState", ret.ReturnCode); |
1189 | 0 | status = smartcard_pack_state_return(out, &ret); |
1190 | 0 | if (status != SCARD_S_SUCCESS) |
1191 | 0 | return status; |
1192 | | |
1193 | 0 | return ret.ReturnCode; |
1194 | 0 | } |
1195 | | |
1196 | | static LONG smartcard_StatusA_Call(scard_call_context* smartcard, wStream* out, |
1197 | | SMARTCARD_OPERATION* operation) |
1198 | 0 | { |
1199 | 0 | LONG status = 0; |
1200 | 0 | Status_Return ret = { 0 }; |
1201 | 0 | DWORD cchReaderLen = 0; |
1202 | 0 | DWORD cbAtrLen = 0; |
1203 | 0 | LPSTR mszReaderNames = NULL; |
1204 | 0 | Status_Call* call = NULL; |
1205 | |
|
1206 | 0 | WINPR_ASSERT(smartcard); |
1207 | 0 | WINPR_ASSERT(out); |
1208 | 0 | WINPR_ASSERT(operation); |
1209 | | |
1210 | 0 | call = &operation->call.status; |
1211 | |
|
1212 | 0 | call->cbAtrLen = 32; |
1213 | 0 | cbAtrLen = call->cbAtrLen; |
1214 | |
|
1215 | 0 | if (call->fmszReaderNamesIsNULL) |
1216 | 0 | cchReaderLen = 0; |
1217 | 0 | else |
1218 | 0 | cchReaderLen = SCARD_AUTOALLOCATE; |
1219 | |
|
1220 | 0 | status = ret.ReturnCode = |
1221 | 0 | wrap(smartcard, SCardStatusA, operation->hCard, |
1222 | 0 | call->fmszReaderNamesIsNULL ? NULL : (LPSTR)&mszReaderNames, &cchReaderLen, |
1223 | 0 | &ret.dwState, &ret.dwProtocol, cbAtrLen ? (BYTE*)&ret.pbAtr : NULL, &cbAtrLen); |
1224 | |
|
1225 | 0 | scard_log_status_error(TAG, "SCardStatusA", status); |
1226 | 0 | if (status == SCARD_S_SUCCESS) |
1227 | 0 | { |
1228 | 0 | if (!call->fmszReaderNamesIsNULL) |
1229 | 0 | ret.mszReaderNames = (BYTE*)mszReaderNames; |
1230 | |
|
1231 | 0 | ret.cBytes = cchReaderLen; |
1232 | |
|
1233 | 0 | if (call->cbAtrLen) |
1234 | 0 | ret.cbAtrLen = cbAtrLen; |
1235 | 0 | } |
1236 | |
|
1237 | 0 | status = smartcard_pack_status_return(out, &ret, FALSE); |
1238 | |
|
1239 | 0 | if (mszReaderNames) |
1240 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaderNames); |
1241 | |
|
1242 | 0 | if (status != SCARD_S_SUCCESS) |
1243 | 0 | return status; |
1244 | 0 | return ret.ReturnCode; |
1245 | 0 | } |
1246 | | |
1247 | | static LONG smartcard_StatusW_Call(scard_call_context* smartcard, wStream* out, |
1248 | | SMARTCARD_OPERATION* operation) |
1249 | 0 | { |
1250 | 0 | LONG status = 0; |
1251 | 0 | Status_Return ret = { 0 }; |
1252 | 0 | LPWSTR mszReaderNames = NULL; |
1253 | 0 | Status_Call* call = NULL; |
1254 | 0 | DWORD cbAtrLen = 0; |
1255 | |
|
1256 | 0 | WINPR_ASSERT(smartcard); |
1257 | 0 | WINPR_ASSERT(out); |
1258 | 0 | WINPR_ASSERT(operation); |
1259 | | |
1260 | 0 | call = &operation->call.status; |
1261 | | |
1262 | | /** |
1263 | | * [MS-RDPESC] |
1264 | | * According to 2.2.2.18 Status_Call cbAtrLen is unused an must be ignored upon receipt. |
1265 | | */ |
1266 | 0 | cbAtrLen = call->cbAtrLen = 32; |
1267 | |
|
1268 | 0 | if (call->fmszReaderNamesIsNULL) |
1269 | 0 | ret.cBytes = 0; |
1270 | 0 | else |
1271 | 0 | ret.cBytes = SCARD_AUTOALLOCATE; |
1272 | |
|
1273 | 0 | status = ret.ReturnCode = |
1274 | 0 | wrap(smartcard, SCardStatusW, operation->hCard, |
1275 | 0 | call->fmszReaderNamesIsNULL ? NULL : (LPWSTR)&mszReaderNames, &ret.cBytes, |
1276 | 0 | &ret.dwState, &ret.dwProtocol, (BYTE*)&ret.pbAtr, &cbAtrLen); |
1277 | 0 | scard_log_status_error(TAG, "SCardStatusW", status); |
1278 | 0 | if (status == SCARD_S_SUCCESS) |
1279 | 0 | { |
1280 | 0 | if (!call->fmszReaderNamesIsNULL) |
1281 | 0 | ret.mszReaderNames = (BYTE*)mszReaderNames; |
1282 | |
|
1283 | 0 | ret.cbAtrLen = cbAtrLen; |
1284 | 0 | } |
1285 | | |
1286 | | /* SCardStatusW returns number of characters, we need number of bytes */ |
1287 | 0 | ret.cBytes *= sizeof(WCHAR); |
1288 | |
|
1289 | 0 | status = smartcard_pack_status_return(out, &ret, TRUE); |
1290 | 0 | if (status != SCARD_S_SUCCESS) |
1291 | 0 | return status; |
1292 | | |
1293 | 0 | if (mszReaderNames) |
1294 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaderNames); |
1295 | |
|
1296 | 0 | return ret.ReturnCode; |
1297 | 0 | } |
1298 | | |
1299 | | static LONG smartcard_Transmit_Call(scard_call_context* smartcard, wStream* out, |
1300 | | SMARTCARD_OPERATION* operation) |
1301 | 0 | { |
1302 | 0 | LONG status = 0; |
1303 | 0 | Transmit_Return ret = { 0 }; |
1304 | 0 | Transmit_Call* call = NULL; |
1305 | |
|
1306 | 0 | WINPR_ASSERT(smartcard); |
1307 | 0 | WINPR_ASSERT(out); |
1308 | 0 | WINPR_ASSERT(operation); |
1309 | | |
1310 | 0 | call = &operation->call.transmit; |
1311 | 0 | ret.cbRecvLength = 0; |
1312 | 0 | ret.pbRecvBuffer = NULL; |
1313 | |
|
1314 | 0 | if (call->cbRecvLength && !call->fpbRecvBufferIsNULL) |
1315 | 0 | { |
1316 | 0 | if (call->cbRecvLength >= 66560) |
1317 | 0 | call->cbRecvLength = 66560; |
1318 | |
|
1319 | 0 | ret.cbRecvLength = call->cbRecvLength; |
1320 | 0 | ret.pbRecvBuffer = (BYTE*)malloc(ret.cbRecvLength); |
1321 | |
|
1322 | 0 | if (!ret.pbRecvBuffer) |
1323 | 0 | return STATUS_NO_MEMORY; |
1324 | 0 | } |
1325 | | |
1326 | 0 | ret.pioRecvPci = call->pioRecvPci; |
1327 | 0 | ret.ReturnCode = |
1328 | 0 | wrap(smartcard, SCardTransmit, operation->hCard, call->pioSendPci, call->pbSendBuffer, |
1329 | 0 | call->cbSendLength, ret.pioRecvPci, ret.pbRecvBuffer, &(ret.cbRecvLength)); |
1330 | |
|
1331 | 0 | scard_log_status_error(TAG, "SCardTransmit", ret.ReturnCode); |
1332 | |
|
1333 | 0 | status = smartcard_pack_transmit_return(out, &ret); |
1334 | 0 | free(ret.pbRecvBuffer); |
1335 | |
|
1336 | 0 | if (status != SCARD_S_SUCCESS) |
1337 | 0 | return status; |
1338 | 0 | return ret.ReturnCode; |
1339 | 0 | } |
1340 | | |
1341 | | static LONG smartcard_Control_Call(scard_call_context* smartcard, wStream* out, |
1342 | | SMARTCARD_OPERATION* operation) |
1343 | 0 | { |
1344 | 0 | LONG status = 0; |
1345 | 0 | Control_Return ret = { 0 }; |
1346 | 0 | Control_Call* call = NULL; |
1347 | |
|
1348 | 0 | WINPR_ASSERT(smartcard); |
1349 | 0 | WINPR_ASSERT(out); |
1350 | 0 | WINPR_ASSERT(operation); |
1351 | | |
1352 | 0 | call = &operation->call.control; |
1353 | 0 | ret.cbOutBufferSize = call->cbOutBufferSize; |
1354 | 0 | ret.pvOutBuffer = (BYTE*)malloc(call->cbOutBufferSize); |
1355 | |
|
1356 | 0 | if (!ret.pvOutBuffer) |
1357 | 0 | return SCARD_E_NO_MEMORY; |
1358 | | |
1359 | 0 | ret.ReturnCode = |
1360 | 0 | wrap(smartcard, SCardControl, operation->hCard, call->dwControlCode, call->pvInBuffer, |
1361 | 0 | call->cbInBufferSize, ret.pvOutBuffer, call->cbOutBufferSize, &ret.cbOutBufferSize); |
1362 | 0 | scard_log_status_error(TAG, "SCardControl", ret.ReturnCode); |
1363 | 0 | status = smartcard_pack_control_return(out, &ret); |
1364 | |
|
1365 | 0 | free(ret.pvOutBuffer); |
1366 | 0 | if (status != SCARD_S_SUCCESS) |
1367 | 0 | return status; |
1368 | 0 | return ret.ReturnCode; |
1369 | 0 | } |
1370 | | |
1371 | | static LONG smartcard_GetAttrib_Call(scard_call_context* smartcard, wStream* out, |
1372 | | SMARTCARD_OPERATION* operation) |
1373 | 0 | { |
1374 | 0 | BOOL autoAllocate = FALSE; |
1375 | 0 | LONG status = 0; |
1376 | 0 | DWORD cbAttrLen = 0; |
1377 | 0 | LPBYTE pbAttr = NULL; |
1378 | 0 | GetAttrib_Return ret = { 0 }; |
1379 | 0 | const GetAttrib_Call* call = NULL; |
1380 | |
|
1381 | 0 | WINPR_ASSERT(smartcard); |
1382 | 0 | WINPR_ASSERT(operation); |
1383 | | |
1384 | 0 | call = &operation->call.getAttrib; |
1385 | |
|
1386 | 0 | if (!call->fpbAttrIsNULL) |
1387 | 0 | { |
1388 | 0 | autoAllocate = (call->cbAttrLen == SCARD_AUTOALLOCATE) ? TRUE : FALSE; |
1389 | 0 | cbAttrLen = call->cbAttrLen; |
1390 | 0 | if (cbAttrLen && !autoAllocate) |
1391 | 0 | { |
1392 | 0 | ret.pbAttr = (BYTE*)malloc(cbAttrLen); |
1393 | |
|
1394 | 0 | if (!ret.pbAttr) |
1395 | 0 | return SCARD_E_NO_MEMORY; |
1396 | 0 | } |
1397 | | |
1398 | 0 | pbAttr = autoAllocate ? (LPBYTE) & (ret.pbAttr) : ret.pbAttr; |
1399 | 0 | } |
1400 | | |
1401 | 0 | ret.ReturnCode = |
1402 | 0 | wrap(smartcard, SCardGetAttrib, operation->hCard, call->dwAttrId, pbAttr, &cbAttrLen); |
1403 | 0 | scard_log_status_error(TAG, "SCardGetAttrib", ret.ReturnCode); |
1404 | 0 | ret.cbAttrLen = cbAttrLen; |
1405 | |
|
1406 | 0 | status = smartcard_pack_get_attrib_return(out, &ret, call->dwAttrId, call->cbAttrLen); |
1407 | |
|
1408 | 0 | if (autoAllocate) |
1409 | 0 | wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbAttr); |
1410 | 0 | else |
1411 | 0 | free(ret.pbAttr); |
1412 | 0 | return status; |
1413 | 0 | } |
1414 | | |
1415 | | static LONG smartcard_SetAttrib_Call(scard_call_context* smartcard, wStream* out, |
1416 | | SMARTCARD_OPERATION* operation) |
1417 | 0 | { |
1418 | 0 | Long_Return ret = { 0 }; |
1419 | 0 | SetAttrib_Call* call = NULL; |
1420 | |
|
1421 | 0 | WINPR_ASSERT(smartcard); |
1422 | 0 | WINPR_ASSERT(out); |
1423 | 0 | WINPR_ASSERT(operation); |
1424 | | |
1425 | 0 | call = &operation->call.setAttrib; |
1426 | |
|
1427 | 0 | ret.ReturnCode = wrap(smartcard, SCardSetAttrib, operation->hCard, call->dwAttrId, call->pbAttr, |
1428 | 0 | call->cbAttrLen); |
1429 | 0 | scard_log_status_error(TAG, "SCardSetAttrib", ret.ReturnCode); |
1430 | 0 | smartcard_trace_long_return(&ret, "SetAttrib"); |
1431 | |
|
1432 | 0 | return ret.ReturnCode; |
1433 | 0 | } |
1434 | | |
1435 | | static LONG smartcard_AccessStartedEvent_Call(scard_call_context* smartcard, wStream* out, |
1436 | | SMARTCARD_OPERATION* operation) |
1437 | 0 | { |
1438 | 0 | LONG status = SCARD_S_SUCCESS; |
1439 | |
|
1440 | 0 | WINPR_ASSERT(smartcard); |
1441 | 0 | WINPR_ASSERT(out); |
1442 | 0 | WINPR_UNUSED(operation); |
1443 | |
|
1444 | 0 | if (!smartcard->StartedEvent) |
1445 | 0 | smartcard->StartedEvent = wrap_ptr(smartcard, SCardAccessStartedEvent); |
1446 | |
|
1447 | 0 | if (!smartcard->StartedEvent) |
1448 | 0 | status = SCARD_E_NO_SERVICE; |
1449 | |
|
1450 | 0 | return status; |
1451 | 0 | } |
1452 | | |
1453 | | static LONG smartcard_LocateCardsByATRA_Call(scard_call_context* smartcard, wStream* out, |
1454 | | SMARTCARD_OPERATION* operation) |
1455 | 0 | { |
1456 | 0 | LONG status = 0; |
1457 | 0 | GetStatusChange_Return ret = { 0 }; |
1458 | 0 | LPSCARD_READERSTATEA state = NULL; |
1459 | 0 | LPSCARD_READERSTATEA states = NULL; |
1460 | 0 | LocateCardsByATRA_Call* call = NULL; |
1461 | |
|
1462 | 0 | WINPR_ASSERT(smartcard); |
1463 | 0 | WINPR_ASSERT(operation); |
1464 | | |
1465 | 0 | call = &operation->call.locateCardsByATRA; |
1466 | 0 | states = (LPSCARD_READERSTATEA)calloc(call->cReaders, sizeof(SCARD_READERSTATEA)); |
1467 | |
|
1468 | 0 | if (!states) |
1469 | 0 | return STATUS_NO_MEMORY; |
1470 | | |
1471 | 0 | for (UINT32 i = 0; i < call->cReaders; i++) |
1472 | 0 | { |
1473 | 0 | states[i].szReader = (LPSTR)call->rgReaderStates[i].szReader; |
1474 | 0 | states[i].dwCurrentState = call->rgReaderStates[i].dwCurrentState; |
1475 | 0 | states[i].dwEventState = call->rgReaderStates[i].dwEventState; |
1476 | 0 | states[i].cbAtr = call->rgReaderStates[i].cbAtr; |
1477 | 0 | CopyMemory(&(states[i].rgbAtr), &(call->rgReaderStates[i].rgbAtr), 36); |
1478 | 0 | } |
1479 | |
|
1480 | 0 | status = ret.ReturnCode = wrap(smartcard, SCardGetStatusChangeA, operation->hContext, |
1481 | 0 | 0x000001F4, states, call->cReaders); |
1482 | |
|
1483 | 0 | scard_log_status_error(TAG, "SCardGetStatusChangeA", status); |
1484 | 0 | for (UINT32 i = 0; i < call->cAtrs; i++) |
1485 | 0 | { |
1486 | 0 | for (UINT32 j = 0; j < call->cReaders; j++) |
1487 | 0 | { |
1488 | 0 | for (UINT32 k = 0; k < call->rgAtrMasks[i].cbAtr; k++) |
1489 | 0 | { |
1490 | 0 | if ((call->rgAtrMasks[i].rgbAtr[k] & call->rgAtrMasks[i].rgbMask[k]) != |
1491 | 0 | (states[j].rgbAtr[k] & call->rgAtrMasks[i].rgbMask[k])) |
1492 | 0 | { |
1493 | 0 | break; |
1494 | 0 | } |
1495 | | |
1496 | 0 | states[j].dwEventState |= SCARD_STATE_ATRMATCH; |
1497 | 0 | } |
1498 | 0 | } |
1499 | 0 | } |
1500 | |
|
1501 | 0 | ret.cReaders = call->cReaders; |
1502 | 0 | ret.rgReaderStates = NULL; |
1503 | |
|
1504 | 0 | if (ret.cReaders > 0) |
1505 | 0 | ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return)); |
1506 | |
|
1507 | 0 | if (!ret.rgReaderStates) |
1508 | 0 | { |
1509 | 0 | free(states); |
1510 | 0 | return STATUS_NO_MEMORY; |
1511 | 0 | } |
1512 | | |
1513 | 0 | for (UINT32 i = 0; i < ret.cReaders; i++) |
1514 | 0 | { |
1515 | 0 | state = &states[i]; |
1516 | 0 | ret.rgReaderStates[i].dwCurrentState = state->dwCurrentState; |
1517 | 0 | ret.rgReaderStates[i].dwEventState = state->dwEventState; |
1518 | 0 | ret.rgReaderStates[i].cbAtr = state->cbAtr; |
1519 | 0 | CopyMemory(&(ret.rgReaderStates[i].rgbAtr), &(state->rgbAtr), |
1520 | 0 | sizeof(ret.rgReaderStates[i].rgbAtr)); |
1521 | 0 | } |
1522 | |
|
1523 | 0 | free(states); |
1524 | |
|
1525 | 0 | status = smartcard_pack_get_status_change_return(out, &ret, FALSE); |
1526 | |
|
1527 | 0 | free(ret.rgReaderStates); |
1528 | 0 | if (status != SCARD_S_SUCCESS) |
1529 | 0 | return status; |
1530 | 0 | return ret.ReturnCode; |
1531 | 0 | } |
1532 | | |
1533 | | LONG smartcard_irp_device_control_call(scard_call_context* smartcard, wStream* out, |
1534 | | UINT32* pIoStatus, SMARTCARD_OPERATION* operation) |
1535 | 0 | { |
1536 | 0 | LONG result = 0; |
1537 | 0 | UINT32 offset = 0; |
1538 | 0 | UINT32 ioControlCode = 0; |
1539 | 0 | size_t outputBufferLength = 0; |
1540 | 0 | size_t objectBufferLength = 0; |
1541 | |
|
1542 | 0 | WINPR_ASSERT(smartcard); |
1543 | 0 | WINPR_ASSERT(out); |
1544 | 0 | WINPR_ASSERT(pIoStatus); |
1545 | 0 | WINPR_ASSERT(operation); |
1546 | | |
1547 | 0 | ioControlCode = operation->ioControlCode; |
1548 | | /** |
1549 | | * [MS-RDPESC] 3.2.5.1: Sending Outgoing Messages: |
1550 | | * the output buffer length SHOULD be set to 2048 |
1551 | | * |
1552 | | * Since it's a SHOULD and not a MUST, we don't care |
1553 | | * about it, but we still reserve at least 2048 bytes. |
1554 | | */ |
1555 | 0 | if (!Stream_EnsureRemainingCapacity(out, 2048)) |
1556 | 0 | return SCARD_E_NO_MEMORY; |
1557 | | |
1558 | | /* Device Control Response */ |
1559 | 0 | Stream_Write_UINT32(out, 0); /* OutputBufferLength (4 bytes) */ |
1560 | 0 | Stream_Zero(out, SMARTCARD_COMMON_TYPE_HEADER_LENGTH); /* CommonTypeHeader (8 bytes) */ |
1561 | 0 | Stream_Zero(out, SMARTCARD_PRIVATE_TYPE_HEADER_LENGTH); /* PrivateTypeHeader (8 bytes) */ |
1562 | 0 | Stream_Write_UINT32(out, 0); /* Result (4 bytes) */ |
1563 | | |
1564 | | /* Call */ |
1565 | 0 | switch (ioControlCode) |
1566 | 0 | { |
1567 | 0 | case SCARD_IOCTL_ESTABLISHCONTEXT: |
1568 | 0 | result = smartcard_EstablishContext_Call(smartcard, out, operation); |
1569 | 0 | break; |
1570 | | |
1571 | 0 | case SCARD_IOCTL_RELEASECONTEXT: |
1572 | 0 | result = smartcard_ReleaseContext_Call(smartcard, out, operation); |
1573 | 0 | break; |
1574 | | |
1575 | 0 | case SCARD_IOCTL_ISVALIDCONTEXT: |
1576 | 0 | result = smartcard_IsValidContext_Call(smartcard, out, operation); |
1577 | 0 | break; |
1578 | | |
1579 | 0 | case SCARD_IOCTL_LISTREADERGROUPSA: |
1580 | 0 | result = smartcard_ListReaderGroupsA_Call(smartcard, out, operation); |
1581 | 0 | break; |
1582 | | |
1583 | 0 | case SCARD_IOCTL_LISTREADERGROUPSW: |
1584 | 0 | result = smartcard_ListReaderGroupsW_Call(smartcard, out, operation); |
1585 | 0 | break; |
1586 | | |
1587 | 0 | case SCARD_IOCTL_LISTREADERSA: |
1588 | 0 | result = smartcard_ListReadersA_Call(smartcard, out, operation); |
1589 | 0 | break; |
1590 | | |
1591 | 0 | case SCARD_IOCTL_LISTREADERSW: |
1592 | 0 | result = smartcard_ListReadersW_Call(smartcard, out, operation); |
1593 | 0 | break; |
1594 | | |
1595 | 0 | case SCARD_IOCTL_INTRODUCEREADERGROUPA: |
1596 | 0 | result = smartcard_IntroduceReaderGroupA_Call(smartcard, out, operation); |
1597 | 0 | break; |
1598 | | |
1599 | 0 | case SCARD_IOCTL_INTRODUCEREADERGROUPW: |
1600 | 0 | result = smartcard_IntroduceReaderGroupW_Call(smartcard, out, operation); |
1601 | 0 | break; |
1602 | | |
1603 | 0 | case SCARD_IOCTL_FORGETREADERGROUPA: |
1604 | 0 | result = smartcard_ForgetReaderA_Call(smartcard, out, operation); |
1605 | 0 | break; |
1606 | | |
1607 | 0 | case SCARD_IOCTL_FORGETREADERGROUPW: |
1608 | 0 | result = smartcard_ForgetReaderW_Call(smartcard, out, operation); |
1609 | 0 | break; |
1610 | | |
1611 | 0 | case SCARD_IOCTL_INTRODUCEREADERA: |
1612 | 0 | result = smartcard_IntroduceReaderA_Call(smartcard, out, operation); |
1613 | 0 | break; |
1614 | | |
1615 | 0 | case SCARD_IOCTL_INTRODUCEREADERW: |
1616 | 0 | result = smartcard_IntroduceReaderW_Call(smartcard, out, operation); |
1617 | 0 | break; |
1618 | | |
1619 | 0 | case SCARD_IOCTL_FORGETREADERA: |
1620 | 0 | result = smartcard_ForgetReaderA_Call(smartcard, out, operation); |
1621 | 0 | break; |
1622 | | |
1623 | 0 | case SCARD_IOCTL_FORGETREADERW: |
1624 | 0 | result = smartcard_ForgetReaderW_Call(smartcard, out, operation); |
1625 | 0 | break; |
1626 | | |
1627 | 0 | case SCARD_IOCTL_ADDREADERTOGROUPA: |
1628 | 0 | result = smartcard_AddReaderToGroupA_Call(smartcard, out, operation); |
1629 | 0 | break; |
1630 | | |
1631 | 0 | case SCARD_IOCTL_ADDREADERTOGROUPW: |
1632 | 0 | result = smartcard_AddReaderToGroupW_Call(smartcard, out, operation); |
1633 | 0 | break; |
1634 | | |
1635 | 0 | case SCARD_IOCTL_REMOVEREADERFROMGROUPA: |
1636 | 0 | result = smartcard_RemoveReaderFromGroupA_Call(smartcard, out, operation); |
1637 | 0 | break; |
1638 | | |
1639 | 0 | case SCARD_IOCTL_REMOVEREADERFROMGROUPW: |
1640 | 0 | result = smartcard_RemoveReaderFromGroupW_Call(smartcard, out, operation); |
1641 | 0 | break; |
1642 | | |
1643 | 0 | case SCARD_IOCTL_LOCATECARDSA: |
1644 | 0 | result = smartcard_LocateCardsA_Call(smartcard, out, operation); |
1645 | 0 | break; |
1646 | | |
1647 | 0 | case SCARD_IOCTL_LOCATECARDSW: |
1648 | 0 | result = smartcard_LocateCardsW_Call(smartcard, out, operation); |
1649 | 0 | break; |
1650 | | |
1651 | 0 | case SCARD_IOCTL_GETSTATUSCHANGEA: |
1652 | 0 | result = smartcard_GetStatusChangeA_Call(smartcard, out, operation); |
1653 | 0 | break; |
1654 | | |
1655 | 0 | case SCARD_IOCTL_GETSTATUSCHANGEW: |
1656 | 0 | result = smartcard_GetStatusChangeW_Call(smartcard, out, operation); |
1657 | 0 | break; |
1658 | | |
1659 | 0 | case SCARD_IOCTL_CANCEL: |
1660 | 0 | result = smartcard_Cancel_Call(smartcard, out, operation); |
1661 | 0 | break; |
1662 | | |
1663 | 0 | case SCARD_IOCTL_CONNECTA: |
1664 | 0 | result = smartcard_ConnectA_Call(smartcard, out, operation); |
1665 | 0 | break; |
1666 | | |
1667 | 0 | case SCARD_IOCTL_CONNECTW: |
1668 | 0 | result = smartcard_ConnectW_Call(smartcard, out, operation); |
1669 | 0 | break; |
1670 | | |
1671 | 0 | case SCARD_IOCTL_RECONNECT: |
1672 | 0 | result = smartcard_Reconnect_Call(smartcard, out, operation); |
1673 | 0 | break; |
1674 | | |
1675 | 0 | case SCARD_IOCTL_DISCONNECT: |
1676 | 0 | result = smartcard_Disconnect_Call(smartcard, out, operation); |
1677 | 0 | break; |
1678 | | |
1679 | 0 | case SCARD_IOCTL_BEGINTRANSACTION: |
1680 | 0 | result = smartcard_BeginTransaction_Call(smartcard, out, operation); |
1681 | 0 | break; |
1682 | | |
1683 | 0 | case SCARD_IOCTL_ENDTRANSACTION: |
1684 | 0 | result = smartcard_EndTransaction_Call(smartcard, out, operation); |
1685 | 0 | break; |
1686 | | |
1687 | 0 | case SCARD_IOCTL_STATE: |
1688 | 0 | result = smartcard_State_Call(smartcard, out, operation); |
1689 | 0 | break; |
1690 | | |
1691 | 0 | case SCARD_IOCTL_STATUSA: |
1692 | 0 | result = smartcard_StatusA_Call(smartcard, out, operation); |
1693 | 0 | break; |
1694 | | |
1695 | 0 | case SCARD_IOCTL_STATUSW: |
1696 | 0 | result = smartcard_StatusW_Call(smartcard, out, operation); |
1697 | 0 | break; |
1698 | | |
1699 | 0 | case SCARD_IOCTL_TRANSMIT: |
1700 | 0 | result = smartcard_Transmit_Call(smartcard, out, operation); |
1701 | 0 | break; |
1702 | | |
1703 | 0 | case SCARD_IOCTL_CONTROL: |
1704 | 0 | result = smartcard_Control_Call(smartcard, out, operation); |
1705 | 0 | break; |
1706 | | |
1707 | 0 | case SCARD_IOCTL_GETATTRIB: |
1708 | 0 | result = smartcard_GetAttrib_Call(smartcard, out, operation); |
1709 | 0 | break; |
1710 | | |
1711 | 0 | case SCARD_IOCTL_SETATTRIB: |
1712 | 0 | result = smartcard_SetAttrib_Call(smartcard, out, operation); |
1713 | 0 | break; |
1714 | | |
1715 | 0 | case SCARD_IOCTL_ACCESSSTARTEDEVENT: |
1716 | 0 | result = smartcard_AccessStartedEvent_Call(smartcard, out, operation); |
1717 | 0 | break; |
1718 | | |
1719 | 0 | case SCARD_IOCTL_LOCATECARDSBYATRA: |
1720 | 0 | result = smartcard_LocateCardsByATRA_Call(smartcard, out, operation); |
1721 | 0 | break; |
1722 | | |
1723 | 0 | case SCARD_IOCTL_LOCATECARDSBYATRW: |
1724 | 0 | result = smartcard_LocateCardsW_Call(smartcard, out, operation); |
1725 | 0 | break; |
1726 | | |
1727 | 0 | case SCARD_IOCTL_READCACHEA: |
1728 | 0 | result = smartcard_ReadCacheA_Call(smartcard, out, operation); |
1729 | 0 | break; |
1730 | | |
1731 | 0 | case SCARD_IOCTL_READCACHEW: |
1732 | 0 | result = smartcard_ReadCacheW_Call(smartcard, out, operation); |
1733 | 0 | break; |
1734 | | |
1735 | 0 | case SCARD_IOCTL_WRITECACHEA: |
1736 | 0 | result = smartcard_WriteCacheA_Call(smartcard, out, operation); |
1737 | 0 | break; |
1738 | | |
1739 | 0 | case SCARD_IOCTL_WRITECACHEW: |
1740 | 0 | result = smartcard_WriteCacheW_Call(smartcard, out, operation); |
1741 | 0 | break; |
1742 | | |
1743 | 0 | case SCARD_IOCTL_GETTRANSMITCOUNT: |
1744 | 0 | result = smartcard_GetTransmitCount_Call(smartcard, out, operation); |
1745 | 0 | break; |
1746 | | |
1747 | 0 | case SCARD_IOCTL_RELEASETARTEDEVENT: |
1748 | 0 | result = smartcard_ReleaseStartedEvent_Call(smartcard, out, operation); |
1749 | 0 | break; |
1750 | | |
1751 | 0 | case SCARD_IOCTL_GETREADERICON: |
1752 | 0 | result = smartcard_GetReaderIcon_Call(smartcard, out, operation); |
1753 | 0 | break; |
1754 | | |
1755 | 0 | case SCARD_IOCTL_GETDEVICETYPEID: |
1756 | 0 | result = smartcard_GetDeviceTypeId_Call(smartcard, out, operation); |
1757 | 0 | break; |
1758 | | |
1759 | 0 | default: |
1760 | 0 | result = STATUS_UNSUCCESSFUL; |
1761 | 0 | break; |
1762 | 0 | } |
1763 | | |
1764 | | /** |
1765 | | * [MS-RPCE] 2.2.6.3 Primitive Type Serialization |
1766 | | * The type MUST be aligned on an 8-byte boundary. If the size of the |
1767 | | * primitive type is not a multiple of 8 bytes, the data MUST be padded. |
1768 | | */ |
1769 | | |
1770 | 0 | if ((ioControlCode != SCARD_IOCTL_ACCESSSTARTEDEVENT) && |
1771 | 0 | (ioControlCode != SCARD_IOCTL_RELEASETARTEDEVENT)) |
1772 | 0 | { |
1773 | 0 | offset = (RDPDR_DEVICE_IO_RESPONSE_LENGTH + RDPDR_DEVICE_IO_CONTROL_RSP_HDR_LENGTH); |
1774 | 0 | smartcard_pack_write_size_align(out, Stream_GetPosition(out) - offset, 8); |
1775 | 0 | } |
1776 | |
|
1777 | 0 | if ((result != SCARD_S_SUCCESS) && (result != SCARD_E_TIMEOUT) && |
1778 | 0 | (result != SCARD_E_NO_READERS_AVAILABLE) && (result != SCARD_E_NO_SERVICE) && |
1779 | 0 | (result != SCARD_W_CACHE_ITEM_NOT_FOUND) && (result != SCARD_W_CACHE_ITEM_STALE)) |
1780 | 0 | { |
1781 | 0 | WLog_WARN(TAG, "IRP failure: %s (0x%08" PRIX32 "), status: %s (0x%08" PRIX32 ")", |
1782 | 0 | scard_get_ioctl_string(ioControlCode, TRUE), ioControlCode, |
1783 | 0 | SCardGetErrorString(result), result); |
1784 | 0 | } |
1785 | |
|
1786 | 0 | *pIoStatus = STATUS_SUCCESS; |
1787 | |
|
1788 | 0 | if ((result & 0xC0000000L) == 0xC0000000L) |
1789 | 0 | { |
1790 | | /* NTSTATUS error */ |
1791 | 0 | *pIoStatus = (UINT32)result; |
1792 | 0 | WLog_WARN(TAG, "IRP failure: %s (0x%08" PRIX32 "), ntstatus: 0x%08" PRIX32 "", |
1793 | 0 | scard_get_ioctl_string(ioControlCode, TRUE), ioControlCode, result); |
1794 | 0 | } |
1795 | |
|
1796 | 0 | Stream_SealLength(out); |
1797 | 0 | outputBufferLength = Stream_Length(out); |
1798 | 0 | WINPR_ASSERT(outputBufferLength >= RDPDR_DEVICE_IO_RESPONSE_LENGTH - 4U); |
1799 | 0 | outputBufferLength -= (RDPDR_DEVICE_IO_RESPONSE_LENGTH + 4U); |
1800 | 0 | WINPR_ASSERT(outputBufferLength >= RDPDR_DEVICE_IO_RESPONSE_LENGTH); |
1801 | 0 | objectBufferLength = outputBufferLength - RDPDR_DEVICE_IO_RESPONSE_LENGTH; |
1802 | 0 | WINPR_ASSERT(outputBufferLength <= UINT32_MAX); |
1803 | 0 | WINPR_ASSERT(objectBufferLength <= UINT32_MAX); |
1804 | 0 | Stream_SetPosition(out, RDPDR_DEVICE_IO_RESPONSE_LENGTH); |
1805 | | /* Device Control Response */ |
1806 | 0 | Stream_Write_UINT32(out, (UINT32)outputBufferLength); /* OutputBufferLength (4 bytes) */ |
1807 | 0 | smartcard_pack_common_type_header(out); /* CommonTypeHeader (8 bytes) */ |
1808 | 0 | smartcard_pack_private_type_header( |
1809 | 0 | out, (UINT32)objectBufferLength); /* PrivateTypeHeader (8 bytes) */ |
1810 | 0 | Stream_Write_INT32(out, result); /* Result (4 bytes) */ |
1811 | 0 | Stream_SetPosition(out, Stream_Length(out)); |
1812 | 0 | return SCARD_S_SUCCESS; |
1813 | 0 | } |
1814 | | |
1815 | | void context_free(void* arg) |
1816 | 0 | { |
1817 | 0 | struct s_scard_context_element* element = arg; |
1818 | 0 | if (!arg) |
1819 | 0 | return; |
1820 | | |
1821 | 0 | if (element->fn_free) |
1822 | 0 | element->fn_free(element->context); |
1823 | 0 | free(element); |
1824 | 0 | } |
1825 | | |
1826 | | scard_call_context* smartcard_call_context_new(const rdpSettings* settings) |
1827 | 0 | { |
1828 | 0 | wObject* obj = NULL; |
1829 | 0 | scard_call_context* ctx = NULL; |
1830 | |
|
1831 | 0 | WINPR_ASSERT(settings); |
1832 | 0 | ctx = calloc(1, sizeof(scard_call_context)); |
1833 | 0 | if (!ctx) |
1834 | 0 | goto fail; |
1835 | | |
1836 | 0 | ctx->stopEvent = CreateEventA(NULL, TRUE, FALSE, NULL); |
1837 | 0 | if (!ctx->stopEvent) |
1838 | 0 | goto fail; |
1839 | | |
1840 | 0 | ctx->names = LinkedList_New(); |
1841 | 0 | if (!ctx->names) |
1842 | 0 | goto fail; |
1843 | | |
1844 | 0 | #if defined(WITH_SMARTCARD_EMULATE) |
1845 | 0 | ctx->useEmulatedCard = freerdp_settings_get_bool(settings, FreeRDP_SmartcardEmulation); |
1846 | 0 | #endif |
1847 | |
|
1848 | 0 | if (ctx->useEmulatedCard) |
1849 | 0 | { |
1850 | 0 | #if defined(WITH_SMARTCARD_EMULATE) |
1851 | 0 | ctx->emulation = Emulate_New(settings); |
1852 | 0 | if (!ctx->emulation) |
1853 | 0 | goto fail; |
1854 | | #else |
1855 | | WLog_ERR(TAG, "Smartcard emulation requested, but not supported!"); |
1856 | | goto fail; |
1857 | | #endif |
1858 | 0 | } |
1859 | 0 | else |
1860 | 0 | { |
1861 | 0 | const char* WinSCardModule = freerdp_settings_get_string(settings, FreeRDP_WinSCardModule); |
1862 | 0 | if (WinSCardModule) |
1863 | 0 | { |
1864 | 0 | ctx->hWinSCardLibrary = LoadLibraryX(WinSCardModule); |
1865 | |
|
1866 | 0 | if (!ctx->hWinSCardLibrary) |
1867 | 0 | { |
1868 | 0 | WLog_ERR(TAG, "Failed to load WinSCard library: '%s'", WinSCardModule); |
1869 | 0 | goto fail; |
1870 | 0 | } |
1871 | | |
1872 | 0 | if (!WinSCard_LoadApiTableFunctions(&ctx->WinSCardApi, ctx->hWinSCardLibrary)) |
1873 | 0 | goto fail; |
1874 | 0 | ctx->pWinSCardApi = &ctx->WinSCardApi; |
1875 | 0 | } |
1876 | 0 | else |
1877 | 0 | { |
1878 | 0 | ctx->pWinSCardApi = WinPR_GetSCardApiFunctionTable(); |
1879 | 0 | } |
1880 | | |
1881 | 0 | if (!ctx->pWinSCardApi) |
1882 | 0 | { |
1883 | 0 | WLog_ERR(TAG, "Failed to load WinSCard API!"); |
1884 | 0 | goto fail; |
1885 | 0 | } |
1886 | 0 | } |
1887 | | |
1888 | 0 | ctx->rgSCardContextList = HashTable_New(FALSE); |
1889 | 0 | if (!ctx->rgSCardContextList) |
1890 | 0 | goto fail; |
1891 | | |
1892 | 0 | obj = HashTable_ValueObject(ctx->rgSCardContextList); |
1893 | 0 | WINPR_ASSERT(obj); |
1894 | 0 | obj->fnObjectFree = context_free; |
1895 | |
|
1896 | 0 | return ctx; |
1897 | 0 | fail: |
1898 | 0 | WINPR_PRAGMA_DIAG_PUSH |
1899 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
1900 | 0 | smartcard_call_context_free(ctx); |
1901 | 0 | WINPR_PRAGMA_DIAG_POP |
1902 | 0 | return NULL; |
1903 | 0 | } |
1904 | | |
1905 | | void smartcard_call_context_free(scard_call_context* ctx) |
1906 | 0 | { |
1907 | 0 | if (!ctx) |
1908 | 0 | return; |
1909 | | |
1910 | 0 | smartcard_call_context_signal_stop(ctx, FALSE); |
1911 | |
|
1912 | 0 | LinkedList_Free(ctx->names); |
1913 | 0 | if (ctx->StartedEvent) |
1914 | 0 | { |
1915 | 0 | WINPR_ASSERT(ctx->useEmulatedCard || ctx->pWinSCardApi); |
1916 | 0 | wrap(ctx, SCardReleaseStartedEvent); |
1917 | 0 | } |
1918 | | |
1919 | 0 | if (ctx->useEmulatedCard) |
1920 | 0 | { |
1921 | 0 | #ifdef WITH_SMARTCARD_EMULATE |
1922 | 0 | if (ctx->emulation) |
1923 | 0 | { |
1924 | 0 | Emulate_Free(ctx->emulation); |
1925 | 0 | ctx->emulation = NULL; |
1926 | 0 | } |
1927 | 0 | #endif |
1928 | 0 | } |
1929 | |
|
1930 | 0 | if (ctx->hWinSCardLibrary) |
1931 | 0 | { |
1932 | 0 | ZeroMemory(&ctx->WinSCardApi, sizeof(SCardApiFunctionTable)); |
1933 | 0 | FreeLibrary(ctx->hWinSCardLibrary); |
1934 | 0 | ctx->hWinSCardLibrary = NULL; |
1935 | 0 | } |
1936 | |
|
1937 | 0 | ctx->pWinSCardApi = NULL; |
1938 | |
|
1939 | 0 | HashTable_Free(ctx->rgSCardContextList); |
1940 | 0 | CloseHandle(ctx->stopEvent); |
1941 | 0 | free(ctx); |
1942 | 0 | } |
1943 | | |
1944 | | BOOL smartcard_call_context_add(scard_call_context* ctx, const char* name) |
1945 | 0 | { |
1946 | 0 | WINPR_ASSERT(ctx); |
1947 | 0 | WINPR_ASSERT(name); |
1948 | 0 | return LinkedList_AddLast(ctx->names, name); |
1949 | 0 | } |
1950 | | |
1951 | | BOOL smartcard_call_cancel_context(scard_call_context* ctx, SCARDCONTEXT hContext) |
1952 | 0 | { |
1953 | 0 | WINPR_ASSERT(ctx); |
1954 | 0 | if (wrap(ctx, SCardIsValidContext, hContext) == SCARD_S_SUCCESS) |
1955 | 0 | { |
1956 | 0 | wrap(ctx, SCardCancel, hContext); |
1957 | 0 | } |
1958 | 0 | return TRUE; |
1959 | 0 | } |
1960 | | |
1961 | | BOOL smartcard_call_release_context(scard_call_context* ctx, SCARDCONTEXT hContext) |
1962 | 0 | { |
1963 | 0 | WINPR_ASSERT(ctx); |
1964 | 0 | wrap(ctx, SCardReleaseContext, hContext); |
1965 | 0 | return TRUE; |
1966 | 0 | } |
1967 | | |
1968 | | BOOL smartcard_call_cancel_all_context(scard_call_context* ctx) |
1969 | 0 | { |
1970 | 0 | WINPR_ASSERT(ctx); |
1971 | | |
1972 | 0 | HashTable_Clear(ctx->rgSCardContextList); |
1973 | 0 | return TRUE; |
1974 | 0 | } |
1975 | | |
1976 | | BOOL smarcard_call_set_callbacks(scard_call_context* ctx, void* userdata, |
1977 | | void* (*fn_new)(void*, SCARDCONTEXT), void (*fn_free)(void*)) |
1978 | 0 | { |
1979 | 0 | WINPR_ASSERT(ctx); |
1980 | 0 | ctx->userdata = userdata; |
1981 | 0 | ctx->fn_new = fn_new; |
1982 | 0 | ctx->fn_free = fn_free; |
1983 | 0 | return TRUE; |
1984 | 0 | } |
1985 | | |
1986 | | void* smartcard_call_get_context(scard_call_context* ctx, SCARDCONTEXT hContext) |
1987 | 0 | { |
1988 | 0 | struct s_scard_context_element* element = NULL; |
1989 | |
|
1990 | 0 | WINPR_ASSERT(ctx); |
1991 | 0 | element = HashTable_GetItemValue(ctx->rgSCardContextList, (void*)hContext); |
1992 | 0 | if (!element) |
1993 | 0 | return NULL; |
1994 | 0 | return element->context; |
1995 | 0 | } |
1996 | | |
1997 | | BOOL smartcard_call_is_configured(scard_call_context* ctx) |
1998 | 0 | { |
1999 | 0 | WINPR_ASSERT(ctx); |
2000 | | |
2001 | 0 | #if defined(WITH_SMARTCARD_EMULATE) |
2002 | 0 | if (ctx->useEmulatedCard) |
2003 | 0 | return Emulate_IsConfigured(ctx->emulation); |
2004 | 0 | #endif |
2005 | | |
2006 | 0 | return FALSE; |
2007 | 0 | } |
2008 | | |
2009 | | BOOL smartcard_call_context_signal_stop(scard_call_context* ctx, BOOL reset) |
2010 | 0 | { |
2011 | 0 | WINPR_ASSERT(ctx); |
2012 | 0 | if (!ctx->stopEvent) |
2013 | 0 | return TRUE; |
2014 | | |
2015 | 0 | if (reset) |
2016 | 0 | return ResetEvent(ctx->stopEvent); |
2017 | 0 | else |
2018 | 0 | return SetEvent(ctx->stopEvent); |
2019 | 0 | } |