/src/FreeRDP/winpr/libwinpr/smartcard/smartcard_pcsc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Smart Card API |
4 | | * |
5 | | * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2020 Armin Novak <armin.novak@thincast.com> |
7 | | * Copyright 2020 Thincast Technologies GmbH |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <winpr/config.h> |
23 | | |
24 | | #ifndef _WIN32 |
25 | | |
26 | | #ifdef __APPLE__ |
27 | | #include <sys/types.h> |
28 | | #include <sys/param.h> |
29 | | #include <sys/sysctl.h> |
30 | | #include <string.h> |
31 | | #include <ctype.h> |
32 | | #include <errno.h> |
33 | | #endif |
34 | | |
35 | | #include <stdio.h> |
36 | | #include <stdlib.h> |
37 | | |
38 | | #include <winpr/crt.h> |
39 | | #include <winpr/assert.h> |
40 | | #include <winpr/synch.h> |
41 | | #include <winpr/library.h> |
42 | | #include <winpr/smartcard.h> |
43 | | #include <winpr/collections.h> |
44 | | #include <winpr/environment.h> |
45 | | |
46 | | #include "smartcard_pcsc.h" |
47 | | |
48 | | #include "../log.h" |
49 | | #define TAG WINPR_TAG("smartcard") |
50 | | |
51 | | #define WINSCARD_LOAD_PROC_EX(module, pcsc, _fname, _name, ...) \ |
52 | 0 | do \ |
53 | 0 | { \ |
54 | 0 | WINPR_PRAGMA_DIAG_PUSH \ |
55 | 0 | WINPR_PRAGMA_DIAG_IGNORED_PEDANTIC \ |
56 | 0 | pcsc.pfn##_fname = (fnPCSC##_fname)GetProcAddress(module, #_name); \ |
57 | 0 | WINPR_PRAGMA_DIAG_POP \ |
58 | 0 | } while (0) |
59 | | |
60 | | #define WINSCARD_LOAD_PROC(module, pcsc, _name, ...) \ |
61 | 0 | WINSCARD_LOAD_PROC_EX(module, pcsc, _name, _name, ##__VA_ARGS__) |
62 | | |
63 | | /** |
64 | | * PC/SC transactions: |
65 | | * http://developersblog.wwpass.com/?p=180 |
66 | | */ |
67 | | |
68 | | /** |
69 | | * Smart Card Logon on Windows Vista: |
70 | | * http://blogs.msdn.com/b/shivaram/archive/2007/02/26/smart-card-logon-on-windows-vista.aspx |
71 | | */ |
72 | | |
73 | | /** |
74 | | * The Smart Card Cryptographic Service Provider Cookbook: |
75 | | * http://msdn.microsoft.com/en-us/library/ms953432.aspx |
76 | | * |
77 | | * SCARDCONTEXT |
78 | | * |
79 | | * The context is a communication channel with the smart card resource manager and |
80 | | * all calls to the resource manager must go through this link. |
81 | | * |
82 | | * All functions that take a context as a parameter or a card handle as parameter, |
83 | | * which is indirectly associated with a particular context, may be blocking calls. |
84 | | * Examples of these are SCardGetStatusChange and SCardBeginTransaction, which takes |
85 | | * a card handle as a parameter. If such a function blocks then all operations wanting |
86 | | * to use the context are blocked as well. So, it is recommended that a CSP using |
87 | | * monitoring establishes at least two contexts with the resource manager; one for |
88 | | * monitoring (with SCardGetStatusChange) and one for other operations. |
89 | | * |
90 | | * If multiple cards are present, it is recommended that a separate context or pair |
91 | | * of contexts be established for each card to prevent operations on one card from |
92 | | * blocking operations on another. |
93 | | * |
94 | | * Example one |
95 | | * |
96 | | * The example below shows what can happen if a CSP using SCardGetStatusChange for |
97 | | * monitoring does not establish two contexts with the resource manager. |
98 | | * The context becomes unusable until SCardGetStatusChange unblocks. |
99 | | * |
100 | | * In this example, there is one process running called P1. |
101 | | * P1 calls SCardEstablishContext, which returns the context hCtx. |
102 | | * P1 calls SCardConnect (with the hCtx context) which returns a handle to the card, hCard. |
103 | | * P1 calls SCardGetStatusChange (with the hCtx context) which blocks because |
104 | | * there are no status changes to report. |
105 | | * Until the thread running SCardGetStatusChange unblocks, another thread in P1 trying to |
106 | | * perform an operation using the context hCtx (or the card hCard) will also be blocked. |
107 | | * |
108 | | * Example two |
109 | | * |
110 | | * The example below shows how transaction control ensures that operations meant to be |
111 | | * performed without interruption can do so safely within a transaction. |
112 | | * |
113 | | * In this example, there are two different processes running; P1 and P2. |
114 | | * P1 calls SCardEstablishContext, which returns the context hCtx1. |
115 | | * P2 calls SCardEstablishContext, which returns the context hCtx2. |
116 | | * P1 calls SCardConnect (with the hCtx1 context) which returns a handle to the card, hCard1. |
117 | | * P2 calls SCardConnect (with the hCtx2 context) which returns a handle to the same card, hCard2. |
118 | | * P1 calls SCardBeginTransaction (with the hCard 1 context). |
119 | | * Until P1 calls SCardEndTransaction (with the hCard1 context), |
120 | | * any operation using hCard2 will be blocked. |
121 | | * Once an operation using hCard2 is blocked and until it's returning, |
122 | | * any operation using hCtx2 (and hCard2) will also be blocked. |
123 | | */ |
124 | | |
125 | | //#define DISABLE_PCSC_SCARD_AUTOALLOCATE |
126 | | #include "smartcard_pcsc.h" |
127 | | |
128 | 0 | #define PCSC_SCARD_PCI_T0 (&g_PCSC_rgSCardT0Pci) |
129 | 0 | #define PCSC_SCARD_PCI_T1 (&g_PCSC_rgSCardT1Pci) |
130 | 0 | #define PCSC_SCARD_PCI_RAW (&g_PCSC_rgSCardRawPci) |
131 | | |
132 | | typedef PCSC_LONG (*fnPCSCSCardEstablishContext)(PCSC_DWORD dwScope, LPCVOID pvReserved1, |
133 | | LPCVOID pvReserved2, LPSCARDCONTEXT phContext); |
134 | | typedef PCSC_LONG (*fnPCSCSCardReleaseContext)(SCARDCONTEXT hContext); |
135 | | typedef PCSC_LONG (*fnPCSCSCardIsValidContext)(SCARDCONTEXT hContext); |
136 | | typedef PCSC_LONG (*fnPCSCSCardConnect)(SCARDCONTEXT hContext, LPCSTR szReader, |
137 | | PCSC_DWORD dwShareMode, PCSC_DWORD dwPreferredProtocols, |
138 | | LPSCARDHANDLE phCard, PCSC_LPDWORD pdwActiveProtocol); |
139 | | typedef PCSC_LONG (*fnPCSCSCardReconnect)(SCARDHANDLE hCard, PCSC_DWORD dwShareMode, |
140 | | PCSC_DWORD dwPreferredProtocols, |
141 | | PCSC_DWORD dwInitialization, |
142 | | PCSC_LPDWORD pdwActiveProtocol); |
143 | | typedef PCSC_LONG (*fnPCSCSCardDisconnect)(SCARDHANDLE hCard, PCSC_DWORD dwDisposition); |
144 | | typedef PCSC_LONG (*fnPCSCSCardBeginTransaction)(SCARDHANDLE hCard); |
145 | | typedef PCSC_LONG (*fnPCSCSCardEndTransaction)(SCARDHANDLE hCard, PCSC_DWORD dwDisposition); |
146 | | typedef PCSC_LONG (*fnPCSCSCardStatus)(SCARDHANDLE hCard, LPSTR mszReaderName, |
147 | | PCSC_LPDWORD pcchReaderLen, PCSC_LPDWORD pdwState, |
148 | | PCSC_LPDWORD pdwProtocol, LPBYTE pbAtr, |
149 | | PCSC_LPDWORD pcbAtrLen); |
150 | | typedef PCSC_LONG (*fnPCSCSCardGetStatusChange)(SCARDCONTEXT hContext, PCSC_DWORD dwTimeout, |
151 | | PCSC_SCARD_READERSTATE* rgReaderStates, |
152 | | PCSC_DWORD cReaders); |
153 | | typedef PCSC_LONG (*fnPCSCSCardControl)(SCARDHANDLE hCard, PCSC_DWORD dwControlCode, |
154 | | LPCVOID pbSendBuffer, PCSC_DWORD cbSendLength, |
155 | | LPVOID pbRecvBuffer, PCSC_DWORD cbRecvLength, |
156 | | PCSC_LPDWORD lpBytesReturned); |
157 | | typedef PCSC_LONG (*fnPCSCSCardTransmit)(SCARDHANDLE hCard, const PCSC_SCARD_IO_REQUEST* pioSendPci, |
158 | | LPCBYTE pbSendBuffer, PCSC_DWORD cbSendLength, |
159 | | PCSC_SCARD_IO_REQUEST* pioRecvPci, LPBYTE pbRecvBuffer, |
160 | | PCSC_LPDWORD pcbRecvLength); |
161 | | typedef PCSC_LONG (*fnPCSCSCardListReaderGroups)(SCARDCONTEXT hContext, LPSTR mszGroups, |
162 | | PCSC_LPDWORD pcchGroups); |
163 | | typedef PCSC_LONG (*fnPCSCSCardListReaders)(SCARDCONTEXT hContext, LPCSTR mszGroups, |
164 | | LPSTR mszReaders, PCSC_LPDWORD pcchReaders); |
165 | | typedef PCSC_LONG (*fnPCSCSCardFreeMemory)(SCARDCONTEXT hContext, LPCVOID pvMem); |
166 | | typedef PCSC_LONG (*fnPCSCSCardCancel)(SCARDCONTEXT hContext); |
167 | | typedef PCSC_LONG (*fnPCSCSCardGetAttrib)(SCARDHANDLE hCard, PCSC_DWORD dwAttrId, LPBYTE pbAttr, |
168 | | PCSC_LPDWORD pcbAttrLen); |
169 | | typedef PCSC_LONG (*fnPCSCSCardSetAttrib)(SCARDHANDLE hCard, PCSC_DWORD dwAttrId, LPCBYTE pbAttr, |
170 | | PCSC_DWORD cbAttrLen); |
171 | | |
172 | | typedef struct |
173 | | { |
174 | | fnPCSCSCardEstablishContext pfnSCardEstablishContext; |
175 | | fnPCSCSCardReleaseContext pfnSCardReleaseContext; |
176 | | fnPCSCSCardIsValidContext pfnSCardIsValidContext; |
177 | | fnPCSCSCardConnect pfnSCardConnect; |
178 | | fnPCSCSCardReconnect pfnSCardReconnect; |
179 | | fnPCSCSCardDisconnect pfnSCardDisconnect; |
180 | | fnPCSCSCardBeginTransaction pfnSCardBeginTransaction; |
181 | | fnPCSCSCardEndTransaction pfnSCardEndTransaction; |
182 | | fnPCSCSCardStatus pfnSCardStatus; |
183 | | fnPCSCSCardGetStatusChange pfnSCardGetStatusChange; |
184 | | fnPCSCSCardControl pfnSCardControl; |
185 | | fnPCSCSCardTransmit pfnSCardTransmit; |
186 | | fnPCSCSCardListReaderGroups pfnSCardListReaderGroups; |
187 | | fnPCSCSCardListReaders pfnSCardListReaders; |
188 | | fnPCSCSCardFreeMemory pfnSCardFreeMemory; |
189 | | fnPCSCSCardCancel pfnSCardCancel; |
190 | | fnPCSCSCardGetAttrib pfnSCardGetAttrib; |
191 | | fnPCSCSCardSetAttrib pfnSCardSetAttrib; |
192 | | } PCSCFunctionTable; |
193 | | |
194 | | typedef struct |
195 | | { |
196 | | DWORD len; |
197 | | DWORD freshness; |
198 | | BYTE* data; |
199 | | } PCSC_CACHE_ITEM; |
200 | | |
201 | | typedef struct |
202 | | { |
203 | | SCARDHANDLE owner; |
204 | | CRITICAL_SECTION lock; |
205 | | SCARDCONTEXT hContext; |
206 | | DWORD dwCardHandleCount; |
207 | | BOOL isTransactionLocked; |
208 | | wHashTable* cache; |
209 | | } PCSC_SCARDCONTEXT; |
210 | | |
211 | | typedef struct |
212 | | { |
213 | | BOOL shared; |
214 | | SCARDCONTEXT hSharedContext; |
215 | | } PCSC_SCARDHANDLE; |
216 | | |
217 | | static HMODULE g_PCSCModule = NULL; |
218 | | static PCSCFunctionTable g_PCSC = { 0 }; |
219 | | |
220 | | static HANDLE g_StartedEvent = NULL; |
221 | | static int g_StartedEventRefCount = 0; |
222 | | |
223 | | static BOOL g_SCardAutoAllocate = FALSE; |
224 | | static BOOL g_PnP_Notification = TRUE; |
225 | | |
226 | | #ifdef __MACOSX__ |
227 | | static unsigned int OSXVersion = 0; |
228 | | #endif |
229 | | |
230 | | static wListDictionary* g_CardHandles = NULL; |
231 | | static wListDictionary* g_CardContexts = NULL; |
232 | | static wListDictionary* g_MemoryBlocks = NULL; |
233 | | |
234 | | static const char SMARTCARD_PNP_NOTIFICATION_A[] = "\\\\?PnP?\\Notification"; |
235 | | |
236 | | static const PCSC_SCARD_IO_REQUEST g_PCSC_rgSCardT0Pci = { SCARD_PROTOCOL_T0, |
237 | | sizeof(PCSC_SCARD_IO_REQUEST) }; |
238 | | static const PCSC_SCARD_IO_REQUEST g_PCSC_rgSCardT1Pci = { SCARD_PROTOCOL_T1, |
239 | | sizeof(PCSC_SCARD_IO_REQUEST) }; |
240 | | static const PCSC_SCARD_IO_REQUEST g_PCSC_rgSCardRawPci = { PCSC_SCARD_PROTOCOL_RAW, |
241 | | sizeof(PCSC_SCARD_IO_REQUEST) }; |
242 | | |
243 | | static LONG WINAPI PCSC_SCardFreeMemory_Internal(SCARDCONTEXT hContext, LPVOID pvMem); |
244 | | static LONG WINAPI PCSC_SCardEstablishContext_Internal(DWORD dwScope, LPCVOID pvReserved1, |
245 | | LPCVOID pvReserved2, |
246 | | LPSCARDCONTEXT phContext); |
247 | | static LONG WINAPI PCSC_SCardReleaseContext_Internal(SCARDCONTEXT hContext); |
248 | | |
249 | | static LONG PCSC_SCard_LogError(const char* what) |
250 | 0 | { |
251 | 0 | WLog_WARN(TAG, "Missing function pointer %s=NULL", what); |
252 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
253 | 0 | } |
254 | | |
255 | | static LONG PCSC_MapErrorCodeToWinSCard(PCSC_LONG errorCode) |
256 | 0 | { |
257 | | /** |
258 | | * pcsc-lite returns SCARD_E_UNEXPECTED when it |
259 | | * should return SCARD_E_UNSUPPORTED_FEATURE. |
260 | | * |
261 | | * Additionally, the pcsc-lite headers incorrectly |
262 | | * define SCARD_E_UNSUPPORTED_FEATURE to 0x8010001F, |
263 | | * when the real value should be 0x80100022. |
264 | | */ |
265 | 0 | if (errorCode != SCARD_S_SUCCESS) |
266 | 0 | { |
267 | 0 | if (errorCode == SCARD_E_UNEXPECTED) |
268 | 0 | errorCode = SCARD_E_UNSUPPORTED_FEATURE; |
269 | 0 | } |
270 | |
|
271 | 0 | return (LONG)errorCode; |
272 | 0 | } |
273 | | |
274 | | static DWORD PCSC_ConvertCardStateToWinSCard(DWORD dwCardState, PCSC_LONG status) |
275 | 0 | { |
276 | | /** |
277 | | * pcsc-lite's SCardStatus returns a bit-field, not an enumerated value. |
278 | | * |
279 | | * State WinSCard pcsc-lite |
280 | | * |
281 | | * SCARD_UNKNOWN 0 0x0001 |
282 | | * SCARD_ABSENT 1 0x0002 |
283 | | * SCARD_PRESENT 2 0x0004 |
284 | | * SCARD_SWALLOWED 3 0x0008 |
285 | | * SCARD_POWERED 4 0x0010 |
286 | | * SCARD_NEGOTIABLE 5 0x0020 |
287 | | * SCARD_SPECIFIC 6 0x0040 |
288 | | * |
289 | | * pcsc-lite also never sets SCARD_SPECIFIC, |
290 | | * which is expected by some windows applications. |
291 | | */ |
292 | 0 | if (status == SCARD_S_SUCCESS) |
293 | 0 | { |
294 | 0 | if ((dwCardState & PCSC_SCARD_NEGOTIABLE) || (dwCardState & PCSC_SCARD_SPECIFIC)) |
295 | 0 | return SCARD_SPECIFIC; |
296 | 0 | } |
297 | | |
298 | 0 | if (dwCardState & PCSC_SCARD_POWERED) |
299 | 0 | return SCARD_POWERED; |
300 | | |
301 | 0 | if (dwCardState & PCSC_SCARD_NEGOTIABLE) |
302 | 0 | return SCARD_NEGOTIABLE; |
303 | | |
304 | 0 | if (dwCardState & PCSC_SCARD_SPECIFIC) |
305 | 0 | return SCARD_SPECIFIC; |
306 | | |
307 | 0 | if (dwCardState & PCSC_SCARD_ABSENT) |
308 | 0 | return SCARD_ABSENT; |
309 | | |
310 | 0 | if (dwCardState & PCSC_SCARD_PRESENT) |
311 | 0 | return SCARD_PRESENT; |
312 | | |
313 | 0 | if (dwCardState & PCSC_SCARD_SWALLOWED) |
314 | 0 | return SCARD_SWALLOWED; |
315 | | |
316 | 0 | if (dwCardState & PCSC_SCARD_UNKNOWN) |
317 | 0 | return SCARD_UNKNOWN; |
318 | | |
319 | 0 | return SCARD_UNKNOWN; |
320 | 0 | } |
321 | | |
322 | | static DWORD PCSC_ConvertProtocolsToWinSCard(PCSC_DWORD dwProtocols) |
323 | 0 | { |
324 | | /** |
325 | | * pcsc-lite uses a different value for SCARD_PROTOCOL_RAW, |
326 | | * and also has SCARD_PROTOCOL_T15 which is not in WinSCard. |
327 | | */ |
328 | 0 | if (dwProtocols & PCSC_SCARD_PROTOCOL_RAW) |
329 | 0 | { |
330 | 0 | dwProtocols &= ~PCSC_SCARD_PROTOCOL_RAW; |
331 | 0 | dwProtocols |= SCARD_PROTOCOL_RAW; |
332 | 0 | } |
333 | |
|
334 | 0 | if (dwProtocols & PCSC_SCARD_PROTOCOL_T15) |
335 | 0 | { |
336 | 0 | dwProtocols &= ~PCSC_SCARD_PROTOCOL_T15; |
337 | 0 | } |
338 | |
|
339 | 0 | return (DWORD)dwProtocols; |
340 | 0 | } |
341 | | |
342 | | static DWORD PCSC_ConvertProtocolsFromWinSCard(DWORD dwProtocols) |
343 | 0 | { |
344 | | /** |
345 | | * pcsc-lite uses a different value for SCARD_PROTOCOL_RAW, |
346 | | * and it does not define WinSCard's SCARD_PROTOCOL_DEFAULT. |
347 | | */ |
348 | 0 | if (dwProtocols & SCARD_PROTOCOL_RAW) |
349 | 0 | { |
350 | 0 | dwProtocols &= ~SCARD_PROTOCOL_RAW; |
351 | 0 | dwProtocols |= PCSC_SCARD_PROTOCOL_RAW; |
352 | 0 | } |
353 | |
|
354 | 0 | if (dwProtocols & SCARD_PROTOCOL_DEFAULT) |
355 | 0 | { |
356 | 0 | dwProtocols &= ~SCARD_PROTOCOL_DEFAULT; |
357 | 0 | } |
358 | |
|
359 | 0 | if (dwProtocols == SCARD_PROTOCOL_UNDEFINED) |
360 | 0 | { |
361 | 0 | dwProtocols = SCARD_PROTOCOL_Tx; |
362 | 0 | } |
363 | |
|
364 | 0 | return dwProtocols; |
365 | 0 | } |
366 | | |
367 | | static PCSC_SCARDCONTEXT* PCSC_GetCardContextData(SCARDCONTEXT hContext) |
368 | 0 | { |
369 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
370 | |
|
371 | 0 | if (!g_CardContexts) |
372 | 0 | return NULL; |
373 | | |
374 | 0 | pContext = (PCSC_SCARDCONTEXT*)ListDictionary_GetItemValue(g_CardContexts, (void*)hContext); |
375 | |
|
376 | 0 | if (!pContext) |
377 | 0 | return NULL; |
378 | | |
379 | 0 | return pContext; |
380 | 0 | } |
381 | | |
382 | | static void pcsc_cache_item_free(void* ptr) |
383 | 0 | { |
384 | 0 | PCSC_CACHE_ITEM* data = ptr; |
385 | 0 | if (data) |
386 | 0 | free(data->data); |
387 | 0 | free(data); |
388 | 0 | } |
389 | | |
390 | | static PCSC_SCARDCONTEXT* PCSC_EstablishCardContext(SCARDCONTEXT hContext) |
391 | 0 | { |
392 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
393 | 0 | pContext = (PCSC_SCARDCONTEXT*)calloc(1, sizeof(PCSC_SCARDCONTEXT)); |
394 | |
|
395 | 0 | if (!pContext) |
396 | 0 | return NULL; |
397 | | |
398 | 0 | pContext->hContext = hContext; |
399 | |
|
400 | 0 | if (!InitializeCriticalSectionAndSpinCount(&(pContext->lock), 4000)) |
401 | 0 | goto error_spinlock; |
402 | | |
403 | 0 | pContext->cache = HashTable_New(FALSE); |
404 | 0 | if (!pContext->cache) |
405 | 0 | goto errors; |
406 | 0 | if (!HashTable_SetupForStringData(pContext->cache, FALSE)) |
407 | 0 | goto errors; |
408 | 0 | { |
409 | 0 | wObject* obj = HashTable_ValueObject(pContext->cache); |
410 | 0 | obj->fnObjectFree = pcsc_cache_item_free; |
411 | 0 | } |
412 | |
|
413 | 0 | if (!g_CardContexts) |
414 | 0 | { |
415 | 0 | g_CardContexts = ListDictionary_New(TRUE); |
416 | |
|
417 | 0 | if (!g_CardContexts) |
418 | 0 | goto errors; |
419 | 0 | } |
420 | | |
421 | 0 | if (!ListDictionary_Add(g_CardContexts, (void*)hContext, (void*)pContext)) |
422 | 0 | goto errors; |
423 | | |
424 | 0 | return pContext; |
425 | 0 | errors: |
426 | 0 | HashTable_Free(pContext->cache); |
427 | 0 | DeleteCriticalSection(&(pContext->lock)); |
428 | 0 | error_spinlock: |
429 | 0 | free(pContext); |
430 | 0 | return NULL; |
431 | 0 | } |
432 | | |
433 | | static void PCSC_ReleaseCardContext(SCARDCONTEXT hContext) |
434 | 0 | { |
435 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
436 | 0 | pContext = PCSC_GetCardContextData(hContext); |
437 | |
|
438 | 0 | if (!pContext) |
439 | 0 | { |
440 | 0 | WLog_ERR(TAG, "PCSC_ReleaseCardContext: null pContext!"); |
441 | 0 | return; |
442 | 0 | } |
443 | | |
444 | 0 | DeleteCriticalSection(&(pContext->lock)); |
445 | 0 | HashTable_Free(pContext->cache); |
446 | 0 | free(pContext); |
447 | |
|
448 | 0 | if (!g_CardContexts) |
449 | 0 | return; |
450 | | |
451 | 0 | ListDictionary_Remove(g_CardContexts, (void*)hContext); |
452 | 0 | } |
453 | | |
454 | | static BOOL PCSC_LockCardContext(SCARDCONTEXT hContext) |
455 | 0 | { |
456 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
457 | 0 | pContext = PCSC_GetCardContextData(hContext); |
458 | |
|
459 | 0 | if (!pContext) |
460 | 0 | { |
461 | 0 | WLog_ERR(TAG, "PCSC_LockCardContext: invalid context (%p)", (void*)hContext); |
462 | 0 | return FALSE; |
463 | 0 | } |
464 | | |
465 | 0 | EnterCriticalSection(&(pContext->lock)); |
466 | 0 | return TRUE; |
467 | 0 | } |
468 | | |
469 | | static BOOL PCSC_UnlockCardContext(SCARDCONTEXT hContext) |
470 | 0 | { |
471 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
472 | 0 | pContext = PCSC_GetCardContextData(hContext); |
473 | |
|
474 | 0 | if (!pContext) |
475 | 0 | { |
476 | 0 | WLog_ERR(TAG, "PCSC_UnlockCardContext: invalid context (%p)", (void*)hContext); |
477 | 0 | return FALSE; |
478 | 0 | } |
479 | | |
480 | 0 | LeaveCriticalSection(&(pContext->lock)); |
481 | 0 | return TRUE; |
482 | 0 | } |
483 | | |
484 | | static PCSC_SCARDHANDLE* PCSC_GetCardHandleData(SCARDHANDLE hCard) |
485 | 0 | { |
486 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
487 | |
|
488 | 0 | if (!g_CardHandles) |
489 | 0 | return NULL; |
490 | | |
491 | 0 | pCard = (PCSC_SCARDHANDLE*)ListDictionary_GetItemValue(g_CardHandles, (void*)hCard); |
492 | |
|
493 | 0 | if (!pCard) |
494 | 0 | return NULL; |
495 | | |
496 | 0 | return pCard; |
497 | 0 | } |
498 | | |
499 | | static SCARDCONTEXT PCSC_GetCardContextFromHandle(SCARDHANDLE hCard) |
500 | 0 | { |
501 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
502 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
503 | |
|
504 | 0 | if (!pCard) |
505 | 0 | return 0; |
506 | | |
507 | 0 | return pCard->hSharedContext; |
508 | 0 | } |
509 | | |
510 | | static BOOL PCSC_WaitForCardAccess(SCARDCONTEXT hContext, SCARDHANDLE hCard, BOOL shared) |
511 | 0 | { |
512 | 0 | BOOL status = TRUE; |
513 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
514 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
515 | |
|
516 | 0 | if (!hCard) |
517 | 0 | { |
518 | | /* SCardConnect */ |
519 | 0 | pContext = PCSC_GetCardContextData(hContext); |
520 | |
|
521 | 0 | if (!pContext) |
522 | 0 | return FALSE; |
523 | | |
524 | 0 | if (!pContext->owner) |
525 | 0 | return TRUE; |
526 | | |
527 | | /* wait for card ownership */ |
528 | 0 | return TRUE; |
529 | 0 | } |
530 | | |
531 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
532 | |
|
533 | 0 | if (!pCard) |
534 | 0 | return FALSE; |
535 | | |
536 | 0 | shared = pCard->shared; |
537 | 0 | hContext = pCard->hSharedContext; |
538 | 0 | pContext = PCSC_GetCardContextData(hContext); |
539 | |
|
540 | 0 | if (!pContext) |
541 | 0 | return FALSE; |
542 | | |
543 | 0 | if (!pContext->owner) |
544 | 0 | { |
545 | | /* card is not owned */ |
546 | 0 | if (!shared) |
547 | 0 | pContext->owner = hCard; |
548 | |
|
549 | 0 | return TRUE; |
550 | 0 | } |
551 | | |
552 | 0 | if (pContext->owner == hCard) |
553 | 0 | { |
554 | | /* already card owner */ |
555 | 0 | } |
556 | 0 | else |
557 | 0 | { |
558 | | /* wait for card ownership */ |
559 | 0 | } |
560 | |
|
561 | 0 | return status; |
562 | 0 | } |
563 | | |
564 | | static BOOL PCSC_ReleaseCardAccess(SCARDCONTEXT hContext, SCARDHANDLE hCard) |
565 | 0 | { |
566 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
567 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
568 | |
|
569 | 0 | if (!hCard) |
570 | 0 | { |
571 | | /* release current owner */ |
572 | 0 | pContext = PCSC_GetCardContextData(hContext); |
573 | |
|
574 | 0 | if (!pContext) |
575 | 0 | return FALSE; |
576 | | |
577 | 0 | hCard = pContext->owner; |
578 | |
|
579 | 0 | if (!hCard) |
580 | 0 | return TRUE; |
581 | | |
582 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
583 | |
|
584 | 0 | if (!pCard) |
585 | 0 | return FALSE; |
586 | | |
587 | | /* release card ownership */ |
588 | 0 | pContext->owner = 0; |
589 | 0 | return TRUE; |
590 | 0 | } |
591 | | |
592 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
593 | |
|
594 | 0 | if (!pCard) |
595 | 0 | return FALSE; |
596 | | |
597 | 0 | hContext = pCard->hSharedContext; |
598 | 0 | pContext = PCSC_GetCardContextData(hContext); |
599 | |
|
600 | 0 | if (!pContext) |
601 | 0 | return FALSE; |
602 | | |
603 | 0 | if (pContext->owner == hCard) |
604 | 0 | { |
605 | | /* release card ownership */ |
606 | 0 | pContext->owner = 0; |
607 | 0 | } |
608 | |
|
609 | 0 | return TRUE; |
610 | 0 | } |
611 | | |
612 | | static PCSC_SCARDHANDLE* PCSC_ConnectCardHandle(SCARDCONTEXT hSharedContext, SCARDHANDLE hCard) |
613 | 0 | { |
614 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
615 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
616 | 0 | pContext = PCSC_GetCardContextData(hSharedContext); |
617 | |
|
618 | 0 | if (!pContext) |
619 | 0 | { |
620 | 0 | WLog_ERR(TAG, "PCSC_ConnectCardHandle: null pContext!"); |
621 | 0 | return NULL; |
622 | 0 | } |
623 | | |
624 | 0 | pCard = (PCSC_SCARDHANDLE*)calloc(1, sizeof(PCSC_SCARDHANDLE)); |
625 | |
|
626 | 0 | if (!pCard) |
627 | 0 | return NULL; |
628 | | |
629 | 0 | pCard->hSharedContext = hSharedContext; |
630 | |
|
631 | 0 | if (!g_CardHandles) |
632 | 0 | { |
633 | 0 | g_CardHandles = ListDictionary_New(TRUE); |
634 | |
|
635 | 0 | if (!g_CardHandles) |
636 | 0 | goto error; |
637 | 0 | } |
638 | | |
639 | 0 | if (!ListDictionary_Add(g_CardHandles, (void*)hCard, (void*)pCard)) |
640 | 0 | goto error; |
641 | | |
642 | 0 | pContext->dwCardHandleCount++; |
643 | 0 | return pCard; |
644 | 0 | error: |
645 | 0 | free(pCard); |
646 | 0 | return NULL; |
647 | 0 | } |
648 | | |
649 | | static void PCSC_DisconnectCardHandle(SCARDHANDLE hCard) |
650 | 0 | { |
651 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
652 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
653 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
654 | |
|
655 | 0 | if (!pCard) |
656 | 0 | return; |
657 | | |
658 | 0 | pContext = PCSC_GetCardContextData(pCard->hSharedContext); |
659 | 0 | free(pCard); |
660 | |
|
661 | 0 | if (!g_CardHandles) |
662 | 0 | return; |
663 | | |
664 | 0 | ListDictionary_Remove(g_CardHandles, (void*)hCard); |
665 | |
|
666 | 0 | if (!pContext) |
667 | 0 | { |
668 | 0 | WLog_ERR(TAG, "PCSC_DisconnectCardHandle: null pContext!"); |
669 | 0 | return; |
670 | 0 | } |
671 | | |
672 | 0 | pContext->dwCardHandleCount--; |
673 | 0 | } |
674 | | |
675 | | static BOOL PCSC_AddMemoryBlock(SCARDCONTEXT hContext, void* pvMem) |
676 | 0 | { |
677 | 0 | if (!g_MemoryBlocks) |
678 | 0 | { |
679 | 0 | g_MemoryBlocks = ListDictionary_New(TRUE); |
680 | |
|
681 | 0 | if (!g_MemoryBlocks) |
682 | 0 | return FALSE; |
683 | 0 | } |
684 | | |
685 | 0 | return ListDictionary_Add(g_MemoryBlocks, pvMem, (void*)hContext); |
686 | 0 | } |
687 | | |
688 | | static void* PCSC_RemoveMemoryBlock(SCARDCONTEXT hContext, void* pvMem) |
689 | 0 | { |
690 | 0 | WINPR_UNUSED(hContext); |
691 | |
|
692 | 0 | if (!g_MemoryBlocks) |
693 | 0 | return NULL; |
694 | | |
695 | 0 | return ListDictionary_Take(g_MemoryBlocks, pvMem); |
696 | 0 | } |
697 | | |
698 | | /** |
699 | | * Standard Windows Smart Card API (PCSC) |
700 | | */ |
701 | | |
702 | | static LONG WINAPI PCSC_SCardEstablishContext_Internal(DWORD dwScope, LPCVOID pvReserved1, |
703 | | LPCVOID pvReserved2, |
704 | | LPSCARDCONTEXT phContext) |
705 | 0 | { |
706 | 0 | WINPR_UNUSED(dwScope); /* SCARD_SCOPE_SYSTEM is the only scope supported by pcsc-lite */ |
707 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
708 | |
|
709 | 0 | if (!g_PCSC.pfnSCardEstablishContext) |
710 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardEstablishContext"); |
711 | | |
712 | 0 | status = |
713 | 0 | g_PCSC.pfnSCardEstablishContext(SCARD_SCOPE_SYSTEM, pvReserved1, pvReserved2, phContext); |
714 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
715 | 0 | } |
716 | | |
717 | | static LONG WINAPI PCSC_SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, |
718 | | LPCVOID pvReserved2, LPSCARDCONTEXT phContext) |
719 | 0 | { |
720 | 0 | LONG status = 0; |
721 | |
|
722 | 0 | status = PCSC_SCardEstablishContext_Internal(dwScope, pvReserved1, pvReserved2, phContext); |
723 | |
|
724 | 0 | if (status == SCARD_S_SUCCESS) |
725 | 0 | PCSC_EstablishCardContext(*phContext); |
726 | |
|
727 | 0 | return status; |
728 | 0 | } |
729 | | |
730 | | static LONG WINAPI PCSC_SCardReleaseContext_Internal(SCARDCONTEXT hContext) |
731 | 0 | { |
732 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
733 | |
|
734 | 0 | if (!g_PCSC.pfnSCardReleaseContext) |
735 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardReleaseContext"); |
736 | | |
737 | 0 | if (!hContext) |
738 | 0 | { |
739 | 0 | WLog_ERR(TAG, "SCardReleaseContext: null hContext"); |
740 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
741 | 0 | } |
742 | | |
743 | 0 | status = g_PCSC.pfnSCardReleaseContext(hContext); |
744 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
745 | 0 | } |
746 | | |
747 | | static LONG WINAPI PCSC_SCardReleaseContext(SCARDCONTEXT hContext) |
748 | 0 | { |
749 | 0 | LONG status = SCARD_S_SUCCESS; |
750 | |
|
751 | 0 | status = PCSC_SCardReleaseContext_Internal(hContext); |
752 | |
|
753 | 0 | if (status != SCARD_S_SUCCESS) |
754 | 0 | PCSC_ReleaseCardContext(hContext); |
755 | |
|
756 | 0 | return status; |
757 | 0 | } |
758 | | |
759 | | static LONG WINAPI PCSC_SCardIsValidContext(SCARDCONTEXT hContext) |
760 | 0 | { |
761 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
762 | |
|
763 | 0 | if (!g_PCSC.pfnSCardIsValidContext) |
764 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardIsValidContext"); |
765 | | |
766 | 0 | status = g_PCSC.pfnSCardIsValidContext(hContext); |
767 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
768 | 0 | } |
769 | | |
770 | | static LONG WINAPI PCSC_SCardListReaderGroups_Internal(SCARDCONTEXT hContext, LPSTR mszGroups, |
771 | | LPDWORD pcchGroups) |
772 | 0 | { |
773 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
774 | 0 | BOOL pcchGroupsAlloc = FALSE; |
775 | 0 | PCSC_DWORD pcsc_cchGroups = 0; |
776 | |
|
777 | 0 | if (!pcchGroups) |
778 | 0 | return SCARD_E_INVALID_PARAMETER; |
779 | | |
780 | 0 | if (!g_PCSC.pfnSCardListReaderGroups) |
781 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaderGroups"); |
782 | | |
783 | 0 | if (*pcchGroups == SCARD_AUTOALLOCATE) |
784 | 0 | pcchGroupsAlloc = TRUE; |
785 | |
|
786 | 0 | pcsc_cchGroups = pcchGroupsAlloc ? PCSC_SCARD_AUTOALLOCATE : (PCSC_DWORD)*pcchGroups; |
787 | |
|
788 | 0 | if (pcchGroupsAlloc && !g_SCardAutoAllocate) |
789 | 0 | { |
790 | 0 | pcsc_cchGroups = 0; |
791 | 0 | status = g_PCSC.pfnSCardListReaderGroups(hContext, NULL, &pcsc_cchGroups); |
792 | |
|
793 | 0 | if (status == SCARD_S_SUCCESS) |
794 | 0 | { |
795 | 0 | LPSTR tmp = calloc(1, pcsc_cchGroups); |
796 | |
|
797 | 0 | if (!tmp) |
798 | 0 | return SCARD_E_NO_MEMORY; |
799 | | |
800 | 0 | status = g_PCSC.pfnSCardListReaderGroups(hContext, tmp, &pcsc_cchGroups); |
801 | |
|
802 | 0 | if (status != SCARD_S_SUCCESS) |
803 | 0 | { |
804 | 0 | free(tmp); |
805 | 0 | tmp = NULL; |
806 | 0 | } |
807 | 0 | else |
808 | 0 | PCSC_AddMemoryBlock(hContext, tmp); |
809 | |
|
810 | 0 | *(LPSTR*)mszGroups = tmp; |
811 | 0 | } |
812 | 0 | } |
813 | 0 | else |
814 | 0 | { |
815 | 0 | status = g_PCSC.pfnSCardListReaderGroups(hContext, mszGroups, &pcsc_cchGroups); |
816 | 0 | } |
817 | | |
818 | 0 | *pcchGroups = (DWORD)pcsc_cchGroups; |
819 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
820 | 0 | } |
821 | | |
822 | | static LONG WINAPI PCSC_SCardListReaderGroupsA(SCARDCONTEXT hContext, LPSTR mszGroups, |
823 | | LPDWORD pcchGroups) |
824 | 0 | { |
825 | 0 | LONG status = SCARD_S_SUCCESS; |
826 | |
|
827 | 0 | if (!g_PCSC.pfnSCardListReaderGroups) |
828 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaderGroups"); |
829 | | |
830 | 0 | if (!PCSC_LockCardContext(hContext)) |
831 | 0 | return SCARD_E_INVALID_HANDLE; |
832 | | |
833 | 0 | status = PCSC_SCardListReaderGroups_Internal(hContext, mszGroups, pcchGroups); |
834 | |
|
835 | 0 | if (!PCSC_UnlockCardContext(hContext)) |
836 | 0 | return SCARD_E_INVALID_HANDLE; |
837 | | |
838 | 0 | return status; |
839 | 0 | } |
840 | | |
841 | | static LONG WINAPI PCSC_SCardListReaderGroupsW(SCARDCONTEXT hContext, LPWSTR mszGroups, |
842 | | LPDWORD pcchGroups) |
843 | 0 | { |
844 | 0 | LPSTR mszGroupsA = NULL; |
845 | 0 | LPSTR* pMszGroupsA = &mszGroupsA; |
846 | 0 | LONG status = SCARD_S_SUCCESS; |
847 | |
|
848 | 0 | if (!g_PCSC.pfnSCardListReaderGroups) |
849 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaderGroups"); |
850 | | |
851 | 0 | if (!PCSC_LockCardContext(hContext)) |
852 | 0 | return SCARD_E_INVALID_HANDLE; |
853 | | |
854 | 0 | status = PCSC_SCardListReaderGroups_Internal(hContext, (LPSTR)&mszGroupsA, pcchGroups); |
855 | |
|
856 | 0 | if (status == SCARD_S_SUCCESS) |
857 | 0 | { |
858 | 0 | size_t size = 0; |
859 | 0 | WCHAR* str = ConvertMszUtf8NToWCharAlloc(*pMszGroupsA, *pcchGroups, &size); |
860 | 0 | if (!str) |
861 | 0 | return SCARD_E_NO_MEMORY; |
862 | 0 | *(WCHAR**)mszGroups = str; |
863 | 0 | *pcchGroups = (DWORD)size; |
864 | 0 | PCSC_AddMemoryBlock(hContext, str); |
865 | 0 | PCSC_SCardFreeMemory_Internal(hContext, *pMszGroupsA); |
866 | 0 | } |
867 | | |
868 | 0 | if (!PCSC_UnlockCardContext(hContext)) |
869 | 0 | return SCARD_E_INVALID_HANDLE; |
870 | | |
871 | 0 | return status; |
872 | 0 | } |
873 | | |
874 | | static LONG WINAPI PCSC_SCardListReaders_Internal(SCARDCONTEXT hContext, LPCSTR mszGroups, |
875 | | LPSTR mszReaders, LPDWORD pcchReaders) |
876 | 0 | { |
877 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
878 | 0 | BOOL pcchReadersAlloc = FALSE; |
879 | 0 | PCSC_DWORD pcsc_cchReaders = 0; |
880 | 0 | if (!pcchReaders) |
881 | 0 | return SCARD_E_INVALID_PARAMETER; |
882 | | |
883 | 0 | if (!g_PCSC.pfnSCardListReaders) |
884 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaders"); |
885 | | |
886 | 0 | mszGroups = NULL; /* mszGroups is not supported by pcsc-lite */ |
887 | |
|
888 | 0 | if (*pcchReaders == SCARD_AUTOALLOCATE) |
889 | 0 | pcchReadersAlloc = TRUE; |
890 | |
|
891 | 0 | pcsc_cchReaders = pcchReadersAlloc ? PCSC_SCARD_AUTOALLOCATE : (PCSC_DWORD)*pcchReaders; |
892 | |
|
893 | 0 | if (pcchReadersAlloc && !g_SCardAutoAllocate) |
894 | 0 | { |
895 | 0 | pcsc_cchReaders = 0; |
896 | 0 | status = g_PCSC.pfnSCardListReaders(hContext, mszGroups, NULL, &pcsc_cchReaders); |
897 | |
|
898 | 0 | if (status == SCARD_S_SUCCESS) |
899 | 0 | { |
900 | 0 | char* tmp = calloc(1, pcsc_cchReaders); |
901 | |
|
902 | 0 | if (!tmp) |
903 | 0 | return SCARD_E_NO_MEMORY; |
904 | | |
905 | 0 | status = g_PCSC.pfnSCardListReaders(hContext, mszGroups, tmp, &pcsc_cchReaders); |
906 | |
|
907 | 0 | if (status != SCARD_S_SUCCESS) |
908 | 0 | { |
909 | 0 | free(tmp); |
910 | 0 | tmp = NULL; |
911 | 0 | } |
912 | 0 | else |
913 | 0 | PCSC_AddMemoryBlock(hContext, tmp); |
914 | |
|
915 | 0 | *(char**)mszReaders = tmp; |
916 | 0 | } |
917 | 0 | } |
918 | 0 | else |
919 | 0 | { |
920 | 0 | status = g_PCSC.pfnSCardListReaders(hContext, mszGroups, mszReaders, &pcsc_cchReaders); |
921 | 0 | } |
922 | | |
923 | 0 | *pcchReaders = (DWORD)pcsc_cchReaders; |
924 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
925 | 0 | } |
926 | | |
927 | | static LONG WINAPI PCSC_SCardListReadersA(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, |
928 | | LPDWORD pcchReaders) |
929 | 0 | { |
930 | 0 | BOOL nullCardContext = FALSE; |
931 | 0 | LONG status = SCARD_S_SUCCESS; |
932 | |
|
933 | 0 | if (!g_PCSC.pfnSCardListReaders) |
934 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaders"); |
935 | | |
936 | 0 | if (!hContext) |
937 | 0 | { |
938 | 0 | status = PCSC_SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); |
939 | |
|
940 | 0 | if (status != SCARD_S_SUCCESS) |
941 | 0 | return status; |
942 | | |
943 | 0 | nullCardContext = TRUE; |
944 | 0 | } |
945 | | |
946 | 0 | if (!PCSC_LockCardContext(hContext)) |
947 | 0 | return SCARD_E_INVALID_HANDLE; |
948 | | |
949 | 0 | status = PCSC_SCardListReaders_Internal(hContext, mszGroups, mszReaders, pcchReaders); |
950 | |
|
951 | 0 | if (!PCSC_UnlockCardContext(hContext)) |
952 | 0 | return SCARD_E_INVALID_HANDLE; |
953 | | |
954 | 0 | if (nullCardContext) |
955 | 0 | { |
956 | 0 | status = PCSC_SCardReleaseContext(hContext); |
957 | 0 | } |
958 | |
|
959 | 0 | return status; |
960 | 0 | } |
961 | | |
962 | | static LONG WINAPI PCSC_SCardListReadersW(SCARDCONTEXT hContext, LPCWSTR mszGroups, |
963 | | LPWSTR mszReaders, LPDWORD pcchReaders) |
964 | 0 | { |
965 | 0 | LPSTR mszGroupsA = NULL; |
966 | 0 | LPSTR mszReadersA = NULL; |
967 | 0 | LONG status = SCARD_S_SUCCESS; |
968 | 0 | BOOL nullCardContext = FALSE; |
969 | |
|
970 | 0 | if (!g_PCSC.pfnSCardListReaders) |
971 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaders"); |
972 | | |
973 | 0 | if (!hContext) |
974 | 0 | { |
975 | 0 | status = PCSC_SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); |
976 | |
|
977 | 0 | if (status != SCARD_S_SUCCESS) |
978 | 0 | return status; |
979 | | |
980 | 0 | nullCardContext = TRUE; |
981 | 0 | } |
982 | | |
983 | 0 | if (!PCSC_LockCardContext(hContext)) |
984 | 0 | return SCARD_E_INVALID_HANDLE; |
985 | | |
986 | 0 | mszGroups = NULL; /* mszGroups is not supported by pcsc-lite */ |
987 | |
|
988 | 0 | if (mszGroups) |
989 | 0 | { |
990 | 0 | mszGroupsA = ConvertWCharToUtf8Alloc(mszGroups, NULL); |
991 | 0 | if (!mszGroups) |
992 | 0 | return SCARD_E_NO_MEMORY; |
993 | 0 | } |
994 | | |
995 | 0 | union |
996 | 0 | { |
997 | 0 | LPSTR* ppc; |
998 | 0 | LPSTR pc; |
999 | 0 | } cnv; |
1000 | 0 | cnv.ppc = &mszReadersA; |
1001 | |
|
1002 | 0 | status = PCSC_SCardListReaders_Internal(hContext, mszGroupsA, cnv.pc, pcchReaders); |
1003 | 0 | if (status == SCARD_S_SUCCESS) |
1004 | 0 | { |
1005 | 0 | size_t size = 0; |
1006 | 0 | WCHAR* str = ConvertMszUtf8NToWCharAlloc(mszReadersA, *pcchReaders, &size); |
1007 | 0 | PCSC_SCardFreeMemory_Internal(hContext, mszReadersA); |
1008 | 0 | if (!str || (size > UINT32_MAX)) |
1009 | 0 | { |
1010 | 0 | free(mszGroupsA); |
1011 | 0 | return SCARD_E_NO_MEMORY; |
1012 | 0 | } |
1013 | 0 | *(LPWSTR*)mszReaders = str; |
1014 | 0 | *pcchReaders = (DWORD)size; |
1015 | 0 | PCSC_AddMemoryBlock(hContext, str); |
1016 | 0 | } |
1017 | | |
1018 | 0 | free(mszGroupsA); |
1019 | |
|
1020 | 0 | if (!PCSC_UnlockCardContext(hContext)) |
1021 | 0 | return SCARD_E_INVALID_HANDLE; |
1022 | | |
1023 | 0 | if (nullCardContext) |
1024 | 0 | { |
1025 | 0 | status = PCSC_SCardReleaseContext(hContext); |
1026 | 0 | } |
1027 | |
|
1028 | 0 | return status; |
1029 | 0 | } |
1030 | | |
1031 | | typedef struct |
1032 | | { |
1033 | | BYTE atr[64]; |
1034 | | size_t atrLen; |
1035 | | const char* cardName; |
1036 | | } PcscKnownAtr; |
1037 | | |
1038 | | static PcscKnownAtr knownAtrs[] = { |
1039 | | /* Yubico YubiKey 5 NFC (PKI) */ |
1040 | | { { 0x3B, 0xFD, 0x13, 0x00, 0x00, 0x81, 0x31, 0xFE, 0x15, 0x80, 0x73, 0xC0, |
1041 | | 0x21, 0xC0, 0x57, 0x59, 0x75, 0x62, 0x69, 0x4B, 0x65, 0x79, 0x40 }, |
1042 | | 23, |
1043 | | "NIST SP 800-73 [PIV]" }, |
1044 | | /* PIVKey C910 PKI Smart Card (eID) */ |
1045 | | { { 0x3B, 0xFC, 0x18, 0x00, 0x00, 0x81, 0x31, 0x80, 0x45, 0x90, 0x67, |
1046 | | 0x46, 0x4A, 0x00, 0x64, 0x16, 0x06, 0xF2, 0x72, 0x7E, 0x00, 0xE0 }, |
1047 | | 22, |
1048 | | "PIVKey Feitian (E0)" } |
1049 | | }; |
1050 | | |
1051 | | #ifndef ARRAY_LENGTH |
1052 | 0 | #define ARRAY_LENGTH(a) (sizeof(a) / sizeof(a)[0]) |
1053 | | #endif |
1054 | | |
1055 | | static const char* findCardByAtr(LPCBYTE pbAtr) |
1056 | 0 | { |
1057 | 0 | for (size_t i = 0; i < ARRAY_LENGTH(knownAtrs); i++) |
1058 | 0 | { |
1059 | 0 | if (memcmp(knownAtrs[i].atr, pbAtr, knownAtrs[i].atrLen) == 0) |
1060 | 0 | return knownAtrs[i].cardName; |
1061 | 0 | } |
1062 | | |
1063 | 0 | return NULL; |
1064 | 0 | } |
1065 | | |
1066 | | static LONG WINAPI PCSC_SCardListCardsA(SCARDCONTEXT hContext, LPCBYTE pbAtr, |
1067 | | LPCGUID rgquidInterfaces, DWORD cguidInterfaceCount, |
1068 | | CHAR* mszCards, LPDWORD pcchCards) |
1069 | 0 | { |
1070 | 0 | const char* cardName = NULL; |
1071 | 0 | DWORD outputLen = 1; |
1072 | 0 | CHAR* output = NULL; |
1073 | 0 | BOOL autoAllocate = 0; |
1074 | |
|
1075 | 0 | if (!pbAtr || rgquidInterfaces || cguidInterfaceCount) |
1076 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1077 | | |
1078 | 0 | if (!pcchCards) |
1079 | 0 | return SCARD_E_INVALID_PARAMETER; |
1080 | | |
1081 | 0 | autoAllocate = (*pcchCards == SCARD_AUTOALLOCATE); |
1082 | |
|
1083 | 0 | cardName = findCardByAtr(pbAtr); |
1084 | 0 | if (cardName) |
1085 | 0 | outputLen += strlen(cardName) + 1; |
1086 | |
|
1087 | 0 | *pcchCards = outputLen; |
1088 | 0 | if (autoAllocate) |
1089 | 0 | { |
1090 | 0 | output = malloc(outputLen); |
1091 | 0 | if (!output) |
1092 | 0 | return SCARD_E_NO_MEMORY; |
1093 | | |
1094 | 0 | *((LPSTR*)mszCards) = output; |
1095 | 0 | } |
1096 | 0 | else |
1097 | 0 | { |
1098 | 0 | if (!mszCards) |
1099 | 0 | return SCARD_S_SUCCESS; |
1100 | | |
1101 | 0 | if (*pcchCards < outputLen) |
1102 | 0 | return SCARD_E_INSUFFICIENT_BUFFER; |
1103 | | |
1104 | 0 | output = mszCards; |
1105 | 0 | } |
1106 | | |
1107 | 0 | if (cardName) |
1108 | 0 | { |
1109 | 0 | size_t toCopy = strlen(cardName) + 1; |
1110 | 0 | memcpy(output, cardName, toCopy); |
1111 | 0 | output += toCopy; |
1112 | 0 | } |
1113 | |
|
1114 | 0 | *output = '\0'; |
1115 | |
|
1116 | 0 | return SCARD_S_SUCCESS; |
1117 | 0 | } |
1118 | | |
1119 | | static LONG WINAPI PCSC_SCardListCardsW(SCARDCONTEXT hContext, LPCBYTE pbAtr, |
1120 | | LPCGUID rgquidInterfaces, DWORD cguidInterfaceCount, |
1121 | | WCHAR* mszCards, LPDWORD pcchCards) |
1122 | 0 | { |
1123 | 0 | const char* cardName = NULL; |
1124 | 0 | DWORD outputLen = 1; |
1125 | 0 | WCHAR* output = NULL; |
1126 | 0 | BOOL autoAllocate = 0; |
1127 | |
|
1128 | 0 | if (!pbAtr || rgquidInterfaces || cguidInterfaceCount) |
1129 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1130 | | |
1131 | 0 | if (!pcchCards) |
1132 | 0 | return SCARD_E_INVALID_PARAMETER; |
1133 | | |
1134 | 0 | autoAllocate = (*pcchCards == SCARD_AUTOALLOCATE); |
1135 | |
|
1136 | 0 | cardName = findCardByAtr(pbAtr); |
1137 | 0 | if (cardName) |
1138 | 0 | outputLen += strlen(cardName) + 1; |
1139 | |
|
1140 | 0 | *pcchCards = outputLen; |
1141 | 0 | if (autoAllocate) |
1142 | 0 | { |
1143 | 0 | output = malloc(outputLen * 2); |
1144 | 0 | if (!output) |
1145 | 0 | return SCARD_E_NO_MEMORY; |
1146 | | |
1147 | 0 | *((LPWSTR*)mszCards) = output; |
1148 | 0 | } |
1149 | 0 | else |
1150 | 0 | { |
1151 | 0 | if (!mszCards) |
1152 | 0 | return SCARD_S_SUCCESS; |
1153 | | |
1154 | 0 | if (*pcchCards < outputLen) |
1155 | 0 | return SCARD_E_INSUFFICIENT_BUFFER; |
1156 | | |
1157 | 0 | output = mszCards; |
1158 | 0 | } |
1159 | | |
1160 | 0 | if (cardName) |
1161 | 0 | { |
1162 | 0 | size_t toCopy = strlen(cardName) + 1; |
1163 | 0 | if (ConvertUtf8ToWChar(cardName, output, toCopy) < 0) |
1164 | 0 | return SCARD_F_INTERNAL_ERROR; |
1165 | 0 | output += toCopy; |
1166 | 0 | } |
1167 | | |
1168 | 0 | *output = 0; |
1169 | |
|
1170 | 0 | return SCARD_S_SUCCESS; |
1171 | 0 | } |
1172 | | |
1173 | | static LONG WINAPI PCSC_SCardListInterfacesA(SCARDCONTEXT hContext, LPCSTR szCard, |
1174 | | LPGUID pguidInterfaces, LPDWORD pcguidInterfaces) |
1175 | 0 | { |
1176 | 0 | WINPR_UNUSED(hContext); |
1177 | 0 | WINPR_UNUSED(szCard); |
1178 | 0 | WINPR_UNUSED(pguidInterfaces); |
1179 | 0 | WINPR_UNUSED(pcguidInterfaces); |
1180 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1181 | 0 | } |
1182 | | |
1183 | | static LONG WINAPI PCSC_SCardListInterfacesW(SCARDCONTEXT hContext, LPCWSTR szCard, |
1184 | | LPGUID pguidInterfaces, LPDWORD pcguidInterfaces) |
1185 | 0 | { |
1186 | 0 | WINPR_UNUSED(hContext); |
1187 | 0 | WINPR_UNUSED(szCard); |
1188 | 0 | WINPR_UNUSED(pguidInterfaces); |
1189 | 0 | WINPR_UNUSED(pcguidInterfaces); |
1190 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1191 | 0 | } |
1192 | | |
1193 | | static LONG WINAPI PCSC_SCardGetProviderIdA(SCARDCONTEXT hContext, LPCSTR szCard, |
1194 | | LPGUID pguidProviderId) |
1195 | 0 | { |
1196 | 0 | WINPR_UNUSED(hContext); |
1197 | 0 | WINPR_UNUSED(szCard); |
1198 | 0 | WINPR_UNUSED(pguidProviderId); |
1199 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1200 | 0 | } |
1201 | | |
1202 | | static LONG WINAPI PCSC_SCardGetProviderIdW(SCARDCONTEXT hContext, LPCWSTR szCard, |
1203 | | LPGUID pguidProviderId) |
1204 | 0 | { |
1205 | 0 | WINPR_UNUSED(hContext); |
1206 | 0 | WINPR_UNUSED(szCard); |
1207 | 0 | WINPR_UNUSED(pguidProviderId); |
1208 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1209 | 0 | } |
1210 | | |
1211 | | static LONG WINAPI PCSC_SCardGetCardTypeProviderNameA(SCARDCONTEXT hContext, LPCSTR szCardName, |
1212 | | DWORD dwProviderId, CHAR* szProvider, |
1213 | | LPDWORD pcchProvider) |
1214 | 0 | { |
1215 | 0 | WINPR_UNUSED(hContext); |
1216 | 0 | WINPR_UNUSED(szCardName); |
1217 | 0 | WINPR_UNUSED(dwProviderId); |
1218 | 0 | WINPR_UNUSED(szProvider); |
1219 | 0 | WINPR_UNUSED(pcchProvider); |
1220 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1221 | 0 | } |
1222 | | |
1223 | | static LONG WINAPI PCSC_SCardGetCardTypeProviderNameW(SCARDCONTEXT hContext, LPCWSTR szCardName, |
1224 | | DWORD dwProviderId, WCHAR* szProvider, |
1225 | | LPDWORD pcchProvider) |
1226 | 0 | { |
1227 | 0 | WINPR_UNUSED(hContext); |
1228 | 0 | WINPR_UNUSED(szCardName); |
1229 | 0 | WINPR_UNUSED(dwProviderId); |
1230 | 0 | WINPR_UNUSED(szProvider); |
1231 | 0 | WINPR_UNUSED(pcchProvider); |
1232 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1233 | 0 | } |
1234 | | |
1235 | | static LONG WINAPI PCSC_SCardIntroduceReaderGroupA(SCARDCONTEXT hContext, LPCSTR szGroupName) |
1236 | 0 | { |
1237 | 0 | WINPR_UNUSED(hContext); |
1238 | 0 | WINPR_UNUSED(szGroupName); |
1239 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1240 | 0 | } |
1241 | | |
1242 | | static LONG WINAPI PCSC_SCardIntroduceReaderGroupW(SCARDCONTEXT hContext, LPCWSTR szGroupName) |
1243 | 0 | { |
1244 | 0 | WINPR_UNUSED(hContext); |
1245 | 0 | WINPR_UNUSED(szGroupName); |
1246 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1247 | 0 | } |
1248 | | |
1249 | | static LONG WINAPI PCSC_SCardForgetReaderGroupA(SCARDCONTEXT hContext, LPCSTR szGroupName) |
1250 | 0 | { |
1251 | 0 | WINPR_UNUSED(hContext); |
1252 | 0 | WINPR_UNUSED(szGroupName); |
1253 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1254 | 0 | } |
1255 | | |
1256 | | static LONG WINAPI PCSC_SCardForgetReaderGroupW(SCARDCONTEXT hContext, LPCWSTR szGroupName) |
1257 | 0 | { |
1258 | 0 | WINPR_UNUSED(hContext); |
1259 | 0 | WINPR_UNUSED(szGroupName); |
1260 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1261 | 0 | } |
1262 | | |
1263 | | static LONG WINAPI PCSC_SCardIntroduceReaderA(SCARDCONTEXT hContext, LPCSTR szReaderName, |
1264 | | LPCSTR szDeviceName) |
1265 | 0 | { |
1266 | 0 | WINPR_UNUSED(hContext); |
1267 | 0 | WINPR_UNUSED(szReaderName); |
1268 | 0 | WINPR_UNUSED(szDeviceName); |
1269 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1270 | 0 | } |
1271 | | |
1272 | | static LONG WINAPI PCSC_SCardIntroduceReaderW(SCARDCONTEXT hContext, LPCWSTR szReaderName, |
1273 | | LPCWSTR szDeviceName) |
1274 | 0 | { |
1275 | 0 | WINPR_UNUSED(hContext); |
1276 | 0 | WINPR_UNUSED(szReaderName); |
1277 | 0 | WINPR_UNUSED(szDeviceName); |
1278 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1279 | 0 | } |
1280 | | |
1281 | | static LONG WINAPI PCSC_SCardForgetReaderA(SCARDCONTEXT hContext, LPCSTR szReaderName) |
1282 | 0 | { |
1283 | 0 | WINPR_UNUSED(hContext); |
1284 | 0 | WINPR_UNUSED(szReaderName); |
1285 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1286 | 0 | } |
1287 | | |
1288 | | static LONG WINAPI PCSC_SCardForgetReaderW(SCARDCONTEXT hContext, LPCWSTR szReaderName) |
1289 | 0 | { |
1290 | 0 | WINPR_UNUSED(hContext); |
1291 | 0 | WINPR_UNUSED(szReaderName); |
1292 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1293 | 0 | } |
1294 | | |
1295 | | static LONG WINAPI PCSC_SCardAddReaderToGroupA(SCARDCONTEXT hContext, LPCSTR szReaderName, |
1296 | | LPCSTR szGroupName) |
1297 | 0 | { |
1298 | 0 | WINPR_UNUSED(hContext); |
1299 | 0 | WINPR_UNUSED(szReaderName); |
1300 | 0 | WINPR_UNUSED(szGroupName); |
1301 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1302 | 0 | } |
1303 | | |
1304 | | static LONG WINAPI PCSC_SCardAddReaderToGroupW(SCARDCONTEXT hContext, LPCWSTR szReaderName, |
1305 | | LPCWSTR szGroupName) |
1306 | 0 | { |
1307 | 0 | WINPR_UNUSED(hContext); |
1308 | 0 | WINPR_UNUSED(szReaderName); |
1309 | 0 | WINPR_UNUSED(szGroupName); |
1310 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1311 | 0 | } |
1312 | | |
1313 | | static LONG WINAPI PCSC_SCardRemoveReaderFromGroupA(SCARDCONTEXT hContext, LPCSTR szReaderName, |
1314 | | LPCSTR szGroupName) |
1315 | 0 | { |
1316 | 0 | WINPR_UNUSED(hContext); |
1317 | 0 | WINPR_UNUSED(szReaderName); |
1318 | 0 | WINPR_UNUSED(szGroupName); |
1319 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1320 | 0 | } |
1321 | | |
1322 | | static LONG WINAPI PCSC_SCardRemoveReaderFromGroupW(SCARDCONTEXT hContext, LPCWSTR szReaderName, |
1323 | | LPCWSTR szGroupName) |
1324 | 0 | { |
1325 | 0 | WINPR_UNUSED(hContext); |
1326 | 0 | WINPR_UNUSED(szReaderName); |
1327 | 0 | WINPR_UNUSED(szGroupName); |
1328 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1329 | 0 | } |
1330 | | |
1331 | | static LONG WINAPI PCSC_SCardIntroduceCardTypeA(SCARDCONTEXT hContext, LPCSTR szCardName, |
1332 | | LPCGUID pguidPrimaryProvider, |
1333 | | LPCGUID rgguidInterfaces, DWORD dwInterfaceCount, |
1334 | | LPCBYTE pbAtr, LPCBYTE pbAtrMask, DWORD cbAtrLen) |
1335 | 0 | { |
1336 | 0 | WINPR_UNUSED(hContext); |
1337 | 0 | WINPR_UNUSED(szCardName); |
1338 | 0 | WINPR_UNUSED(pguidPrimaryProvider); |
1339 | 0 | WINPR_UNUSED(rgguidInterfaces); |
1340 | 0 | WINPR_UNUSED(dwInterfaceCount); |
1341 | 0 | WINPR_UNUSED(pbAtr); |
1342 | 0 | WINPR_UNUSED(pbAtrMask); |
1343 | 0 | WINPR_UNUSED(cbAtrLen); |
1344 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1345 | 0 | } |
1346 | | |
1347 | | static LONG WINAPI PCSC_SCardIntroduceCardTypeW(SCARDCONTEXT hContext, LPCWSTR szCardName, |
1348 | | LPCGUID pguidPrimaryProvider, |
1349 | | LPCGUID rgguidInterfaces, DWORD dwInterfaceCount, |
1350 | | LPCBYTE pbAtr, LPCBYTE pbAtrMask, DWORD cbAtrLen) |
1351 | 0 | { |
1352 | 0 | WINPR_UNUSED(hContext); |
1353 | 0 | WINPR_UNUSED(szCardName); |
1354 | 0 | WINPR_UNUSED(pguidPrimaryProvider); |
1355 | 0 | WINPR_UNUSED(rgguidInterfaces); |
1356 | 0 | WINPR_UNUSED(dwInterfaceCount); |
1357 | 0 | WINPR_UNUSED(pbAtr); |
1358 | 0 | WINPR_UNUSED(pbAtrMask); |
1359 | 0 | WINPR_UNUSED(cbAtrLen); |
1360 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1361 | 0 | } |
1362 | | |
1363 | | static LONG WINAPI PCSC_SCardSetCardTypeProviderNameA(SCARDCONTEXT hContext, LPCSTR szCardName, |
1364 | | DWORD dwProviderId, LPCSTR szProvider) |
1365 | 0 | { |
1366 | 0 | WINPR_UNUSED(hContext); |
1367 | 0 | WINPR_UNUSED(szCardName); |
1368 | 0 | WINPR_UNUSED(dwProviderId); |
1369 | 0 | WINPR_UNUSED(szProvider); |
1370 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1371 | 0 | } |
1372 | | |
1373 | | static LONG WINAPI PCSC_SCardSetCardTypeProviderNameW(SCARDCONTEXT hContext, LPCWSTR szCardName, |
1374 | | DWORD dwProviderId, LPCWSTR szProvider) |
1375 | 0 | { |
1376 | 0 | WINPR_UNUSED(hContext); |
1377 | 0 | WINPR_UNUSED(szCardName); |
1378 | 0 | WINPR_UNUSED(dwProviderId); |
1379 | 0 | WINPR_UNUSED(szProvider); |
1380 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1381 | 0 | } |
1382 | | |
1383 | | static LONG WINAPI PCSC_SCardForgetCardTypeA(SCARDCONTEXT hContext, LPCSTR szCardName) |
1384 | 0 | { |
1385 | 0 | WINPR_UNUSED(hContext); |
1386 | 0 | WINPR_UNUSED(szCardName); |
1387 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1388 | 0 | } |
1389 | | |
1390 | | static LONG WINAPI PCSC_SCardForgetCardTypeW(SCARDCONTEXT hContext, LPCWSTR szCardName) |
1391 | 0 | { |
1392 | 0 | WINPR_UNUSED(hContext); |
1393 | 0 | WINPR_UNUSED(szCardName); |
1394 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1395 | 0 | } |
1396 | | |
1397 | | static LONG WINAPI PCSC_SCardFreeMemory_Internal(SCARDCONTEXT hContext, LPVOID pvMem) |
1398 | 0 | { |
1399 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
1400 | |
|
1401 | 0 | if (PCSC_RemoveMemoryBlock(hContext, pvMem)) |
1402 | 0 | { |
1403 | 0 | free((void*)pvMem); |
1404 | 0 | status = SCARD_S_SUCCESS; |
1405 | 0 | } |
1406 | 0 | else |
1407 | 0 | { |
1408 | 0 | if (g_PCSC.pfnSCardFreeMemory) |
1409 | 0 | { |
1410 | 0 | status = g_PCSC.pfnSCardFreeMemory(hContext, pvMem); |
1411 | 0 | } |
1412 | 0 | } |
1413 | |
|
1414 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
1415 | 0 | } |
1416 | | |
1417 | | static LONG WINAPI PCSC_SCardFreeMemory(SCARDCONTEXT hContext, LPVOID pvMem) |
1418 | 0 | { |
1419 | 0 | LONG status = SCARD_S_SUCCESS; |
1420 | |
|
1421 | 0 | if (hContext) |
1422 | 0 | { |
1423 | 0 | if (!PCSC_LockCardContext(hContext)) |
1424 | 0 | return SCARD_E_INVALID_HANDLE; |
1425 | 0 | } |
1426 | | |
1427 | 0 | status = PCSC_SCardFreeMemory_Internal(hContext, pvMem); |
1428 | |
|
1429 | 0 | if (hContext) |
1430 | 0 | { |
1431 | 0 | if (!PCSC_UnlockCardContext(hContext)) |
1432 | 0 | return SCARD_E_INVALID_HANDLE; |
1433 | 0 | } |
1434 | | |
1435 | 0 | return status; |
1436 | 0 | } |
1437 | | |
1438 | | static HANDLE WINAPI PCSC_SCardAccessStartedEvent(void) |
1439 | 0 | { |
1440 | 0 | LONG status = 0; |
1441 | 0 | SCARDCONTEXT hContext = 0; |
1442 | |
|
1443 | 0 | status = PCSC_SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); |
1444 | |
|
1445 | 0 | if (status != SCARD_S_SUCCESS) |
1446 | 0 | return NULL; |
1447 | | |
1448 | 0 | status = PCSC_SCardReleaseContext(hContext); |
1449 | |
|
1450 | 0 | if (status != SCARD_S_SUCCESS) |
1451 | 0 | return NULL; |
1452 | | |
1453 | 0 | if (!g_StartedEvent) |
1454 | 0 | { |
1455 | 0 | if (!(g_StartedEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) |
1456 | 0 | return NULL; |
1457 | | |
1458 | 0 | if (!SetEvent(g_StartedEvent)) |
1459 | 0 | { |
1460 | 0 | CloseHandle(g_StartedEvent); |
1461 | 0 | return NULL; |
1462 | 0 | } |
1463 | 0 | } |
1464 | | |
1465 | 0 | g_StartedEventRefCount++; |
1466 | 0 | return g_StartedEvent; |
1467 | 0 | } |
1468 | | |
1469 | | static void WINAPI PCSC_SCardReleaseStartedEvent(void) |
1470 | 0 | { |
1471 | 0 | g_StartedEventRefCount--; |
1472 | |
|
1473 | 0 | if (g_StartedEventRefCount == 0) |
1474 | 0 | { |
1475 | 0 | if (g_StartedEvent) |
1476 | 0 | { |
1477 | 0 | CloseHandle(g_StartedEvent); |
1478 | 0 | g_StartedEvent = NULL; |
1479 | 0 | } |
1480 | 0 | } |
1481 | 0 | } |
1482 | | |
1483 | | static LONG WINAPI PCSC_SCardLocateCardsA(SCARDCONTEXT hContext, LPCSTR mszCards, |
1484 | | LPSCARD_READERSTATEA rgReaderStates, DWORD cReaders) |
1485 | 0 | { |
1486 | 0 | WINPR_UNUSED(hContext); |
1487 | 0 | WINPR_UNUSED(mszCards); |
1488 | 0 | WINPR_UNUSED(rgReaderStates); |
1489 | 0 | WINPR_UNUSED(cReaders); |
1490 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1491 | 0 | } |
1492 | | |
1493 | | static LONG WINAPI PCSC_SCardLocateCardsW(SCARDCONTEXT hContext, LPCWSTR mszCards, |
1494 | | LPSCARD_READERSTATEW rgReaderStates, DWORD cReaders) |
1495 | 0 | { |
1496 | 0 | WINPR_UNUSED(hContext); |
1497 | 0 | WINPR_UNUSED(mszCards); |
1498 | 0 | WINPR_UNUSED(rgReaderStates); |
1499 | 0 | WINPR_UNUSED(cReaders); |
1500 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1501 | 0 | } |
1502 | | |
1503 | | static LONG WINAPI PCSC_SCardLocateCardsByATRA(SCARDCONTEXT hContext, LPSCARD_ATRMASK rgAtrMasks, |
1504 | | DWORD cAtrs, LPSCARD_READERSTATEA rgReaderStates, |
1505 | | DWORD cReaders) |
1506 | 0 | { |
1507 | 0 | WINPR_UNUSED(hContext); |
1508 | 0 | WINPR_UNUSED(rgAtrMasks); |
1509 | 0 | WINPR_UNUSED(cAtrs); |
1510 | 0 | WINPR_UNUSED(rgReaderStates); |
1511 | 0 | WINPR_UNUSED(cReaders); |
1512 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1513 | 0 | } |
1514 | | |
1515 | | static LONG WINAPI PCSC_SCardLocateCardsByATRW(SCARDCONTEXT hContext, LPSCARD_ATRMASK rgAtrMasks, |
1516 | | DWORD cAtrs, LPSCARD_READERSTATEW rgReaderStates, |
1517 | | DWORD cReaders) |
1518 | 0 | { |
1519 | 0 | WINPR_UNUSED(hContext); |
1520 | 0 | WINPR_UNUSED(rgAtrMasks); |
1521 | 0 | WINPR_UNUSED(cAtrs); |
1522 | 0 | WINPR_UNUSED(rgReaderStates); |
1523 | 0 | WINPR_UNUSED(cReaders); |
1524 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
1525 | 0 | } |
1526 | | |
1527 | | static LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext, DWORD dwTimeout, |
1528 | | LPSCARD_READERSTATEA rgReaderStates, |
1529 | | DWORD cReaders) |
1530 | 0 | { |
1531 | 0 | INT64* map = NULL; |
1532 | 0 | PCSC_DWORD cMappedReaders = 0; |
1533 | 0 | PCSC_SCARD_READERSTATE* states = NULL; |
1534 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
1535 | 0 | PCSC_DWORD pcsc_dwTimeout = (PCSC_DWORD)dwTimeout; |
1536 | 0 | PCSC_DWORD pcsc_cReaders = (PCSC_DWORD)cReaders; |
1537 | |
|
1538 | 0 | if (!g_PCSC.pfnSCardGetStatusChange) |
1539 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardGetStatusChange"); |
1540 | | |
1541 | 0 | if (!cReaders) |
1542 | 0 | return SCARD_S_SUCCESS; |
1543 | | |
1544 | | /* pcsc-lite interprets value 0 as INFINITE, work around the problem by using value 1 */ |
1545 | 0 | pcsc_dwTimeout = pcsc_dwTimeout ? pcsc_dwTimeout : 1; |
1546 | | /** |
1547 | | * Apple's SmartCard Services (not vanilla pcsc-lite) appears to have trouble with the |
1548 | | * "\\\\?PnP?\\Notification" reader name. I am always getting EXC_BAD_ACCESS with it. |
1549 | | * |
1550 | | * The SmartCard Services tarballs can be found here: |
1551 | | * http://opensource.apple.com/tarballs/SmartCardServices/ |
1552 | | * |
1553 | | * The "\\\\?PnP?\\Notification" string cannot be found anywhere in the sources, |
1554 | | * while this string is present in the vanilla pcsc-lite sources. |
1555 | | * |
1556 | | * To work around this apparent lack of "\\\\?PnP?\\Notification" support, |
1557 | | * we have to filter rgReaderStates to exclude the special PnP reader name. |
1558 | | */ |
1559 | 0 | map = (INT64*)calloc(pcsc_cReaders, sizeof(INT64)); |
1560 | |
|
1561 | 0 | if (!map) |
1562 | 0 | return SCARD_E_NO_MEMORY; |
1563 | | |
1564 | 0 | states = (PCSC_SCARD_READERSTATE*)calloc(pcsc_cReaders, sizeof(PCSC_SCARD_READERSTATE)); |
1565 | |
|
1566 | 0 | if (!states) |
1567 | 0 | { |
1568 | 0 | free(map); |
1569 | 0 | return SCARD_E_NO_MEMORY; |
1570 | 0 | } |
1571 | | |
1572 | 0 | PCSC_DWORD j = 0; |
1573 | 0 | for (PCSC_DWORD i = 0; i < pcsc_cReaders; i++) |
1574 | 0 | { |
1575 | 0 | if (!g_PnP_Notification) |
1576 | 0 | { |
1577 | 0 | LPSCARD_READERSTATEA reader = &rgReaderStates[i]; |
1578 | 0 | if (!reader->szReader) |
1579 | 0 | continue; |
1580 | 0 | if (0 == _stricmp(reader->szReader, SMARTCARD_PNP_NOTIFICATION_A)) |
1581 | 0 | { |
1582 | 0 | map[i] = -1; /* unmapped */ |
1583 | 0 | continue; |
1584 | 0 | } |
1585 | 0 | } |
1586 | | |
1587 | 0 | map[i] = (INT64)j; |
1588 | 0 | states[j].szReader = rgReaderStates[i].szReader; |
1589 | 0 | states[j].dwCurrentState = rgReaderStates[i].dwCurrentState; |
1590 | 0 | states[j].pvUserData = rgReaderStates[i].pvUserData; |
1591 | 0 | states[j].dwEventState = rgReaderStates[i].dwEventState; |
1592 | 0 | states[j].cbAtr = rgReaderStates[i].cbAtr; |
1593 | 0 | CopyMemory(&(states[j].rgbAtr), &(rgReaderStates[i].rgbAtr), PCSC_MAX_ATR_SIZE); |
1594 | 0 | j++; |
1595 | 0 | } |
1596 | |
|
1597 | 0 | cMappedReaders = j; |
1598 | |
|
1599 | 0 | if (cMappedReaders > 0) |
1600 | 0 | { |
1601 | 0 | status = g_PCSC.pfnSCardGetStatusChange(hContext, pcsc_dwTimeout, states, cMappedReaders); |
1602 | 0 | } |
1603 | 0 | else |
1604 | 0 | { |
1605 | 0 | status = SCARD_S_SUCCESS; |
1606 | 0 | } |
1607 | |
|
1608 | 0 | for (PCSC_DWORD i = 0; i < pcsc_cReaders; i++) |
1609 | 0 | { |
1610 | 0 | if (map[i] < 0) |
1611 | 0 | continue; /* unmapped */ |
1612 | | |
1613 | 0 | PCSC_DWORD k = (PCSC_DWORD)map[i]; |
1614 | 0 | rgReaderStates[i].dwCurrentState = (DWORD)states[k].dwCurrentState; |
1615 | 0 | rgReaderStates[i].cbAtr = (DWORD)states[k].cbAtr; |
1616 | 0 | CopyMemory(&(rgReaderStates[i].rgbAtr), &(states[k].rgbAtr), PCSC_MAX_ATR_SIZE); |
1617 | 0 | rgReaderStates[i].dwEventState = (DWORD)states[k].dwEventState; |
1618 | 0 | } |
1619 | |
|
1620 | 0 | free(map); |
1621 | 0 | free(states); |
1622 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
1623 | 0 | } |
1624 | | |
1625 | | static LONG WINAPI PCSC_SCardGetStatusChangeA(SCARDCONTEXT hContext, DWORD dwTimeout, |
1626 | | LPSCARD_READERSTATEA rgReaderStates, DWORD cReaders) |
1627 | 0 | { |
1628 | 0 | LONG status = SCARD_S_SUCCESS; |
1629 | |
|
1630 | 0 | if (!PCSC_LockCardContext(hContext)) |
1631 | 0 | return SCARD_E_INVALID_HANDLE; |
1632 | | |
1633 | 0 | status = PCSC_SCardGetStatusChange_Internal(hContext, dwTimeout, rgReaderStates, cReaders); |
1634 | |
|
1635 | 0 | if (!PCSC_UnlockCardContext(hContext)) |
1636 | 0 | return SCARD_E_INVALID_HANDLE; |
1637 | | |
1638 | 0 | return status; |
1639 | 0 | } |
1640 | | |
1641 | | static LONG WINAPI PCSC_SCardGetStatusChangeW(SCARDCONTEXT hContext, DWORD dwTimeout, |
1642 | | LPSCARD_READERSTATEW rgReaderStates, DWORD cReaders) |
1643 | 0 | { |
1644 | 0 | LPSCARD_READERSTATEA states = NULL; |
1645 | 0 | LONG status = SCARD_S_SUCCESS; |
1646 | |
|
1647 | 0 | if (!g_PCSC.pfnSCardGetStatusChange) |
1648 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardGetStatusChange"); |
1649 | | |
1650 | 0 | if (!PCSC_LockCardContext(hContext)) |
1651 | 0 | return SCARD_E_INVALID_HANDLE; |
1652 | | |
1653 | 0 | states = (LPSCARD_READERSTATEA)calloc(cReaders, sizeof(SCARD_READERSTATEA)); |
1654 | |
|
1655 | 0 | if (!states) |
1656 | 0 | { |
1657 | 0 | PCSC_UnlockCardContext(hContext); |
1658 | 0 | return SCARD_E_NO_MEMORY; |
1659 | 0 | } |
1660 | | |
1661 | 0 | for (DWORD index = 0; index < cReaders; index++) |
1662 | 0 | { |
1663 | 0 | const LPSCARD_READERSTATEW curReader = &rgReaderStates[index]; |
1664 | 0 | LPSCARD_READERSTATEA cur = &states[index]; |
1665 | |
|
1666 | 0 | cur->szReader = ConvertWCharToUtf8Alloc(curReader->szReader, NULL); |
1667 | 0 | cur->pvUserData = curReader->pvUserData; |
1668 | 0 | cur->dwCurrentState = curReader->dwCurrentState; |
1669 | 0 | cur->dwEventState = curReader->dwEventState; |
1670 | 0 | cur->cbAtr = curReader->cbAtr; |
1671 | 0 | CopyMemory(&(cur->rgbAtr), &(curReader->rgbAtr), ARRAYSIZE(cur->rgbAtr)); |
1672 | 0 | } |
1673 | |
|
1674 | 0 | status = PCSC_SCardGetStatusChange_Internal(hContext, dwTimeout, states, cReaders); |
1675 | |
|
1676 | 0 | for (DWORD index = 0; index < cReaders; index++) |
1677 | 0 | { |
1678 | 0 | free((void*)states[index].szReader); |
1679 | 0 | rgReaderStates[index].pvUserData = states[index].pvUserData; |
1680 | 0 | rgReaderStates[index].dwCurrentState = states[index].dwCurrentState; |
1681 | 0 | rgReaderStates[index].dwEventState = states[index].dwEventState; |
1682 | 0 | rgReaderStates[index].cbAtr = states[index].cbAtr; |
1683 | 0 | CopyMemory(&(rgReaderStates[index].rgbAtr), &(states[index].rgbAtr), 36); |
1684 | 0 | } |
1685 | |
|
1686 | 0 | free(states); |
1687 | |
|
1688 | 0 | if (!PCSC_UnlockCardContext(hContext)) |
1689 | 0 | return SCARD_E_INVALID_HANDLE; |
1690 | | |
1691 | 0 | return status; |
1692 | 0 | } |
1693 | | |
1694 | | static LONG WINAPI PCSC_SCardCancel(SCARDCONTEXT hContext) |
1695 | 0 | { |
1696 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
1697 | |
|
1698 | 0 | if (!g_PCSC.pfnSCardCancel) |
1699 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardCancel"); |
1700 | | |
1701 | 0 | status = g_PCSC.pfnSCardCancel(hContext); |
1702 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
1703 | 0 | } |
1704 | | |
1705 | | static LONG WINAPI PCSC_SCardConnect_Internal(SCARDCONTEXT hContext, LPCSTR szReader, |
1706 | | DWORD dwShareMode, DWORD dwPreferredProtocols, |
1707 | | LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol) |
1708 | 0 | { |
1709 | 0 | BOOL shared = 0; |
1710 | 0 | const char* szReaderPCSC = NULL; |
1711 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
1712 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
1713 | 0 | PCSC_DWORD pcsc_dwShareMode = (PCSC_DWORD)dwShareMode; |
1714 | 0 | PCSC_DWORD pcsc_dwPreferredProtocols = 0; |
1715 | 0 | PCSC_DWORD pcsc_dwActiveProtocol = 0; |
1716 | |
|
1717 | 0 | if (!g_PCSC.pfnSCardConnect) |
1718 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardConnect"); |
1719 | | |
1720 | 0 | shared = (dwShareMode == SCARD_SHARE_DIRECT) ? TRUE : FALSE; |
1721 | 0 | PCSC_WaitForCardAccess(hContext, 0, shared); |
1722 | 0 | szReaderPCSC = szReader; |
1723 | | |
1724 | | /** |
1725 | | * As stated here : |
1726 | | * https://pcsclite.alioth.debian.org/api/group__API.html#ga4e515829752e0a8dbc4d630696a8d6a5 |
1727 | | * SCARD_PROTOCOL_UNDEFINED is valid for dwPreferredProtocols (only) if dwShareMode == |
1728 | | * SCARD_SHARE_DIRECT and allows to send control commands to the reader (with SCardControl()) |
1729 | | * even if a card is not present in the reader |
1730 | | */ |
1731 | 0 | if (pcsc_dwShareMode == SCARD_SHARE_DIRECT && dwPreferredProtocols == SCARD_PROTOCOL_UNDEFINED) |
1732 | 0 | pcsc_dwPreferredProtocols = SCARD_PROTOCOL_UNDEFINED; |
1733 | 0 | else |
1734 | 0 | pcsc_dwPreferredProtocols = |
1735 | 0 | (PCSC_DWORD)PCSC_ConvertProtocolsFromWinSCard(dwPreferredProtocols); |
1736 | |
|
1737 | 0 | status = g_PCSC.pfnSCardConnect(hContext, szReaderPCSC, pcsc_dwShareMode, |
1738 | 0 | pcsc_dwPreferredProtocols, phCard, &pcsc_dwActiveProtocol); |
1739 | |
|
1740 | 0 | if (status == SCARD_S_SUCCESS) |
1741 | 0 | { |
1742 | 0 | pCard = PCSC_ConnectCardHandle(hContext, *phCard); |
1743 | 0 | *pdwActiveProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwActiveProtocol); |
1744 | 0 | pCard->shared = shared; |
1745 | | |
1746 | | // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): ListDictionary_Add takes ownership of pCard |
1747 | 0 | PCSC_WaitForCardAccess(hContext, pCard->hSharedContext, shared); |
1748 | 0 | } |
1749 | |
|
1750 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
1751 | 0 | } |
1752 | | |
1753 | | static LONG WINAPI PCSC_SCardConnectA(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, |
1754 | | DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, |
1755 | | LPDWORD pdwActiveProtocol) |
1756 | 0 | { |
1757 | 0 | LONG status = SCARD_S_SUCCESS; |
1758 | |
|
1759 | 0 | if (!PCSC_LockCardContext(hContext)) |
1760 | 0 | return SCARD_E_INVALID_HANDLE; |
1761 | | |
1762 | 0 | status = PCSC_SCardConnect_Internal(hContext, szReader, dwShareMode, dwPreferredProtocols, |
1763 | 0 | phCard, pdwActiveProtocol); |
1764 | |
|
1765 | 0 | if (!PCSC_UnlockCardContext(hContext)) |
1766 | 0 | return SCARD_E_INVALID_HANDLE; |
1767 | | |
1768 | 0 | return status; |
1769 | 0 | } |
1770 | | |
1771 | | static LONG WINAPI PCSC_SCardConnectW(SCARDCONTEXT hContext, LPCWSTR szReader, DWORD dwShareMode, |
1772 | | DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, |
1773 | | LPDWORD pdwActiveProtocol) |
1774 | 0 | { |
1775 | 0 | LPSTR szReaderA = NULL; |
1776 | 0 | LONG status = SCARD_S_SUCCESS; |
1777 | |
|
1778 | 0 | if (!PCSC_LockCardContext(hContext)) |
1779 | 0 | return SCARD_E_INVALID_HANDLE; |
1780 | | |
1781 | 0 | if (szReader) |
1782 | 0 | { |
1783 | 0 | szReaderA = ConvertWCharToUtf8Alloc(szReader, NULL); |
1784 | 0 | if (!szReaderA) |
1785 | 0 | return SCARD_E_INSUFFICIENT_BUFFER; |
1786 | 0 | } |
1787 | | |
1788 | 0 | status = PCSC_SCardConnect_Internal(hContext, szReaderA, dwShareMode, dwPreferredProtocols, |
1789 | 0 | phCard, pdwActiveProtocol); |
1790 | 0 | free(szReaderA); |
1791 | |
|
1792 | 0 | if (!PCSC_UnlockCardContext(hContext)) |
1793 | 0 | return SCARD_E_INVALID_HANDLE; |
1794 | | |
1795 | 0 | return status; |
1796 | 0 | } |
1797 | | |
1798 | | static LONG WINAPI PCSC_SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, |
1799 | | DWORD dwPreferredProtocols, DWORD dwInitialization, |
1800 | | LPDWORD pdwActiveProtocol) |
1801 | 0 | { |
1802 | 0 | BOOL shared = 0; |
1803 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
1804 | 0 | PCSC_DWORD pcsc_dwShareMode = (PCSC_DWORD)dwShareMode; |
1805 | 0 | PCSC_DWORD pcsc_dwPreferredProtocols = 0; |
1806 | 0 | PCSC_DWORD pcsc_dwInitialization = (PCSC_DWORD)dwInitialization; |
1807 | 0 | PCSC_DWORD pcsc_dwActiveProtocol = 0; |
1808 | |
|
1809 | 0 | if (!g_PCSC.pfnSCardReconnect) |
1810 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardReconnect"); |
1811 | | |
1812 | 0 | shared = (dwShareMode == SCARD_SHARE_DIRECT) ? TRUE : FALSE; |
1813 | 0 | PCSC_WaitForCardAccess(0, hCard, shared); |
1814 | 0 | pcsc_dwPreferredProtocols = (PCSC_DWORD)PCSC_ConvertProtocolsFromWinSCard(dwPreferredProtocols); |
1815 | 0 | status = g_PCSC.pfnSCardReconnect(hCard, pcsc_dwShareMode, pcsc_dwPreferredProtocols, |
1816 | 0 | pcsc_dwInitialization, &pcsc_dwActiveProtocol); |
1817 | |
|
1818 | 0 | *pdwActiveProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwActiveProtocol); |
1819 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
1820 | 0 | } |
1821 | | |
1822 | | static LONG WINAPI PCSC_SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition) |
1823 | 0 | { |
1824 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
1825 | 0 | PCSC_DWORD pcsc_dwDisposition = (PCSC_DWORD)dwDisposition; |
1826 | |
|
1827 | 0 | if (!g_PCSC.pfnSCardDisconnect) |
1828 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardDisconnect"); |
1829 | | |
1830 | 0 | status = g_PCSC.pfnSCardDisconnect(hCard, pcsc_dwDisposition); |
1831 | |
|
1832 | 0 | if (status == SCARD_S_SUCCESS) |
1833 | 0 | { |
1834 | 0 | PCSC_DisconnectCardHandle(hCard); |
1835 | 0 | } |
1836 | |
|
1837 | 0 | PCSC_ReleaseCardAccess(0, hCard); |
1838 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
1839 | 0 | } |
1840 | | |
1841 | | static LONG WINAPI PCSC_SCardBeginTransaction(SCARDHANDLE hCard) |
1842 | 0 | { |
1843 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
1844 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
1845 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
1846 | |
|
1847 | 0 | if (!g_PCSC.pfnSCardBeginTransaction) |
1848 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardBeginTransaction"); |
1849 | | |
1850 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
1851 | |
|
1852 | 0 | if (!pCard) |
1853 | 0 | return SCARD_E_INVALID_HANDLE; |
1854 | | |
1855 | 0 | pContext = PCSC_GetCardContextData(pCard->hSharedContext); |
1856 | |
|
1857 | 0 | if (!pContext) |
1858 | 0 | return SCARD_E_INVALID_HANDLE; |
1859 | | |
1860 | 0 | if (pContext->isTransactionLocked) |
1861 | 0 | return SCARD_S_SUCCESS; /* disable nested transactions */ |
1862 | | |
1863 | 0 | status = g_PCSC.pfnSCardBeginTransaction(hCard); |
1864 | |
|
1865 | 0 | pContext->isTransactionLocked = TRUE; |
1866 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
1867 | 0 | } |
1868 | | |
1869 | | static LONG WINAPI PCSC_SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition) |
1870 | 0 | { |
1871 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
1872 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
1873 | 0 | PCSC_SCARDCONTEXT* pContext = NULL; |
1874 | 0 | PCSC_DWORD pcsc_dwDisposition = (PCSC_DWORD)dwDisposition; |
1875 | |
|
1876 | 0 | if (!g_PCSC.pfnSCardEndTransaction) |
1877 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardEndTransaction"); |
1878 | | |
1879 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
1880 | |
|
1881 | 0 | if (!pCard) |
1882 | 0 | return SCARD_E_INVALID_HANDLE; |
1883 | | |
1884 | 0 | pContext = PCSC_GetCardContextData(pCard->hSharedContext); |
1885 | |
|
1886 | 0 | if (!pContext) |
1887 | 0 | return SCARD_E_INVALID_HANDLE; |
1888 | | |
1889 | 0 | PCSC_ReleaseCardAccess(0, hCard); |
1890 | |
|
1891 | 0 | if (!pContext->isTransactionLocked) |
1892 | 0 | return SCARD_S_SUCCESS; /* disable nested transactions */ |
1893 | | |
1894 | 0 | status = g_PCSC.pfnSCardEndTransaction(hCard, pcsc_dwDisposition); |
1895 | |
|
1896 | 0 | pContext->isTransactionLocked = FALSE; |
1897 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
1898 | 0 | } |
1899 | | |
1900 | | static LONG WINAPI PCSC_SCardCancelTransaction(SCARDHANDLE hCard) |
1901 | 0 | { |
1902 | 0 | WINPR_UNUSED(hCard); |
1903 | 0 | return SCARD_S_SUCCESS; |
1904 | 0 | } |
1905 | | |
1906 | | /* |
1907 | | * PCSC returns a string but Windows SCardStatus requires the return to be a multi string. |
1908 | | * Therefore extra length checks and additional buffer allocation is required |
1909 | | */ |
1910 | | static LONG WINAPI PCSC_SCardStatus_Internal(SCARDHANDLE hCard, LPSTR mszReaderNames, |
1911 | | LPDWORD pcchReaderLen, LPDWORD pdwState, |
1912 | | LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen, |
1913 | | BOOL unicode) |
1914 | 0 | { |
1915 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
1916 | 0 | SCARDCONTEXT hContext = 0; |
1917 | 0 | PCSC_LONG status = 0; |
1918 | 0 | PCSC_DWORD pcsc_cchReaderLen = 0; |
1919 | 0 | PCSC_DWORD pcsc_cbAtrLen = 0; |
1920 | 0 | PCSC_DWORD pcsc_dwState = 0; |
1921 | 0 | PCSC_DWORD pcsc_dwProtocol = 0; |
1922 | 0 | BOOL allocateReader = FALSE; |
1923 | 0 | BOOL allocateAtr = FALSE; |
1924 | 0 | LPSTR readerNames = mszReaderNames; |
1925 | 0 | LPBYTE atr = pbAtr; |
1926 | 0 | LPSTR tReader = NULL; |
1927 | 0 | LPBYTE tATR = NULL; |
1928 | |
|
1929 | 0 | if (!g_PCSC.pfnSCardStatus) |
1930 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardStatus"); |
1931 | | |
1932 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
1933 | |
|
1934 | 0 | if (!pCard) |
1935 | 0 | return SCARD_E_INVALID_VALUE; |
1936 | | |
1937 | 0 | PCSC_WaitForCardAccess(0, hCard, pCard->shared); |
1938 | 0 | hContext = PCSC_GetCardContextFromHandle(hCard); |
1939 | |
|
1940 | 0 | if (!hContext) |
1941 | 0 | return SCARD_E_INVALID_VALUE; |
1942 | | |
1943 | 0 | status = |
1944 | 0 | g_PCSC.pfnSCardStatus(hCard, NULL, &pcsc_cchReaderLen, NULL, NULL, NULL, &pcsc_cbAtrLen); |
1945 | |
|
1946 | 0 | if (status != STATUS_SUCCESS) |
1947 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
1948 | | |
1949 | 0 | pcsc_cchReaderLen++; |
1950 | |
|
1951 | 0 | if (unicode) |
1952 | 0 | pcsc_cchReaderLen *= 2; |
1953 | |
|
1954 | 0 | if (pcchReaderLen) |
1955 | 0 | { |
1956 | 0 | if (*pcchReaderLen == SCARD_AUTOALLOCATE) |
1957 | 0 | allocateReader = TRUE; |
1958 | 0 | else if (mszReaderNames && (*pcchReaderLen < pcsc_cchReaderLen)) |
1959 | 0 | return SCARD_E_INSUFFICIENT_BUFFER; |
1960 | 0 | else |
1961 | 0 | pcsc_cchReaderLen = *pcchReaderLen; |
1962 | 0 | } |
1963 | | |
1964 | 0 | if (pcbAtrLen) |
1965 | 0 | { |
1966 | 0 | if (*pcbAtrLen == SCARD_AUTOALLOCATE) |
1967 | 0 | allocateAtr = TRUE; |
1968 | 0 | else if (pbAtr && (*pcbAtrLen < pcsc_cbAtrLen)) |
1969 | 0 | return SCARD_E_INSUFFICIENT_BUFFER; |
1970 | 0 | else |
1971 | 0 | pcsc_cbAtrLen = *pcbAtrLen; |
1972 | 0 | } |
1973 | | |
1974 | 0 | if (allocateReader && pcsc_cchReaderLen > 0 && mszReaderNames) |
1975 | 0 | { |
1976 | | #ifdef __MACOSX__ |
1977 | | |
1978 | | /** |
1979 | | * Workaround for SCardStatus Bug in MAC OS X Yosemite |
1980 | | */ |
1981 | | if (OSXVersion == 0x10100000) |
1982 | | pcsc_cchReaderLen++; |
1983 | | |
1984 | | #endif |
1985 | 0 | tReader = calloc(sizeof(CHAR), pcsc_cchReaderLen + 1); |
1986 | |
|
1987 | 0 | if (!tReader) |
1988 | 0 | { |
1989 | 0 | status = ERROR_NOT_ENOUGH_MEMORY; |
1990 | 0 | goto out_fail; |
1991 | 0 | } |
1992 | | |
1993 | 0 | readerNames = tReader; |
1994 | 0 | } |
1995 | | |
1996 | 0 | if (allocateAtr && pcsc_cbAtrLen > 0 && pbAtr) |
1997 | 0 | { |
1998 | 0 | tATR = calloc(1, pcsc_cbAtrLen); |
1999 | |
|
2000 | 0 | if (!tATR) |
2001 | 0 | { |
2002 | 0 | status = ERROR_NOT_ENOUGH_MEMORY; |
2003 | 0 | goto out_fail; |
2004 | 0 | } |
2005 | | |
2006 | 0 | atr = tATR; |
2007 | 0 | } |
2008 | | |
2009 | 0 | status = g_PCSC.pfnSCardStatus(hCard, readerNames, &pcsc_cchReaderLen, &pcsc_dwState, |
2010 | 0 | &pcsc_dwProtocol, atr, &pcsc_cbAtrLen); |
2011 | |
|
2012 | 0 | if (status != STATUS_SUCCESS) |
2013 | 0 | goto out_fail; |
2014 | | |
2015 | 0 | if (tATR) |
2016 | 0 | { |
2017 | 0 | PCSC_AddMemoryBlock(hContext, tATR); |
2018 | 0 | *(BYTE**)pbAtr = tATR; |
2019 | 0 | } |
2020 | |
|
2021 | 0 | if (tReader) |
2022 | 0 | { |
2023 | 0 | if (unicode) |
2024 | 0 | { |
2025 | 0 | size_t size = 0; |
2026 | 0 | WCHAR* tmp = ConvertMszUtf8NToWCharAlloc(tReader, pcsc_cchReaderLen + 1, &size); |
2027 | |
|
2028 | 0 | if (tmp == NULL) |
2029 | 0 | { |
2030 | 0 | status = ERROR_NOT_ENOUGH_MEMORY; |
2031 | 0 | goto out_fail; |
2032 | 0 | } |
2033 | | |
2034 | 0 | free(tReader); |
2035 | |
|
2036 | 0 | PCSC_AddMemoryBlock(hContext, tmp); |
2037 | 0 | *(WCHAR**)mszReaderNames = tmp; |
2038 | 0 | } |
2039 | 0 | else |
2040 | 0 | { |
2041 | 0 | tReader[pcsc_cchReaderLen - 1] = '\0'; |
2042 | 0 | PCSC_AddMemoryBlock(hContext, tReader); |
2043 | 0 | *(char**)mszReaderNames = tReader; |
2044 | 0 | } |
2045 | 0 | } |
2046 | | |
2047 | 0 | pcsc_dwState &= 0xFFFF; |
2048 | |
|
2049 | 0 | if (pdwState) |
2050 | 0 | *pdwState = PCSC_ConvertCardStateToWinSCard((DWORD)pcsc_dwState, status); |
2051 | |
|
2052 | 0 | if (pdwProtocol) |
2053 | 0 | *pdwProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwProtocol); |
2054 | |
|
2055 | 0 | if (pcbAtrLen) |
2056 | 0 | *pcbAtrLen = (DWORD)pcsc_cbAtrLen; |
2057 | |
|
2058 | 0 | if (pcchReaderLen) |
2059 | 0 | { |
2060 | 0 | WINPR_ASSERT(pcsc_cchReaderLen < UINT32_MAX); |
2061 | 0 | *pcchReaderLen = (DWORD)pcsc_cchReaderLen + 1u; |
2062 | 0 | } |
2063 | | |
2064 | 0 | return (LONG)status; |
2065 | 0 | out_fail: |
2066 | 0 | free(tReader); |
2067 | 0 | free(tATR); |
2068 | 0 | return (LONG)status; |
2069 | 0 | } |
2070 | | |
2071 | | static LONG WINAPI PCSC_SCardState(SCARDHANDLE hCard, LPDWORD pdwState, LPDWORD pdwProtocol, |
2072 | | LPBYTE pbAtr, LPDWORD pcbAtrLen) |
2073 | 0 | { |
2074 | 0 | DWORD cchReaderLen = 0; |
2075 | 0 | SCARDCONTEXT hContext = 0; |
2076 | 0 | LPSTR mszReaderNames = NULL; |
2077 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
2078 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
2079 | 0 | DWORD pcsc_dwState = 0; |
2080 | 0 | DWORD pcsc_dwProtocol = 0; |
2081 | 0 | DWORD pcsc_cbAtrLen = 0; |
2082 | |
|
2083 | 0 | if (pcbAtrLen) |
2084 | 0 | pcsc_cbAtrLen = (DWORD)*pcbAtrLen; |
2085 | |
|
2086 | 0 | if (!g_PCSC.pfnSCardStatus) |
2087 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardStatus"); |
2088 | | |
2089 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
2090 | |
|
2091 | 0 | if (!pCard) |
2092 | 0 | return SCARD_E_INVALID_VALUE; |
2093 | | |
2094 | 0 | PCSC_WaitForCardAccess(0, hCard, pCard->shared); |
2095 | 0 | hContext = PCSC_GetCardContextFromHandle(hCard); |
2096 | |
|
2097 | 0 | if (!hContext) |
2098 | 0 | return SCARD_E_INVALID_VALUE; |
2099 | | |
2100 | 0 | cchReaderLen = SCARD_AUTOALLOCATE; |
2101 | 0 | status = PCSC_SCardStatus_Internal(hCard, (LPSTR)&mszReaderNames, &cchReaderLen, &pcsc_dwState, |
2102 | 0 | &pcsc_dwProtocol, pbAtr, &pcsc_cbAtrLen, FALSE); |
2103 | |
|
2104 | 0 | if (mszReaderNames) |
2105 | 0 | PCSC_SCardFreeMemory_Internal(hContext, mszReaderNames); |
2106 | |
|
2107 | 0 | *pdwState = (DWORD)pcsc_dwState; |
2108 | 0 | *pdwProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwProtocol); |
2109 | 0 | if (pcbAtrLen) |
2110 | 0 | *pcbAtrLen = (DWORD)pcsc_cbAtrLen; |
2111 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
2112 | 0 | } |
2113 | | |
2114 | | static LONG WINAPI PCSC_SCardStatusA(SCARDHANDLE hCard, LPSTR mszReaderNames, LPDWORD pcchReaderLen, |
2115 | | LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, |
2116 | | LPDWORD pcbAtrLen) |
2117 | 0 | { |
2118 | |
|
2119 | 0 | return PCSC_SCardStatus_Internal(hCard, mszReaderNames, pcchReaderLen, pdwState, pdwProtocol, |
2120 | 0 | pbAtr, pcbAtrLen, FALSE); |
2121 | 0 | } |
2122 | | |
2123 | | static LONG WINAPI PCSC_SCardStatusW(SCARDHANDLE hCard, LPWSTR mszReaderNames, |
2124 | | LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, |
2125 | | LPBYTE pbAtr, LPDWORD pcbAtrLen) |
2126 | 0 | { |
2127 | |
|
2128 | 0 | return PCSC_SCardStatus_Internal(hCard, (LPSTR)mszReaderNames, pcchReaderLen, pdwState, |
2129 | 0 | pdwProtocol, pbAtr, pcbAtrLen, TRUE); |
2130 | 0 | } |
2131 | | |
2132 | | static LONG WINAPI PCSC_SCardTransmit(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pioSendPci, |
2133 | | LPCBYTE pbSendBuffer, DWORD cbSendLength, |
2134 | | LPSCARD_IO_REQUEST pioRecvPci, LPBYTE pbRecvBuffer, |
2135 | | LPDWORD pcbRecvLength) |
2136 | 0 | { |
2137 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
2138 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
2139 | 0 | PCSC_DWORD cbExtraBytes = 0; |
2140 | 0 | BYTE* pbExtraBytes = NULL; |
2141 | 0 | BYTE* pcsc_pbExtraBytes = NULL; |
2142 | 0 | PCSC_DWORD pcsc_cbSendLength = (PCSC_DWORD)cbSendLength; |
2143 | 0 | PCSC_DWORD pcsc_cbRecvLength = 0; |
2144 | 0 | union |
2145 | 0 | { |
2146 | 0 | const PCSC_SCARD_IO_REQUEST* pcs; |
2147 | 0 | PCSC_SCARD_IO_REQUEST* ps; |
2148 | 0 | LPSCARD_IO_REQUEST lps; |
2149 | 0 | LPCSCARD_IO_REQUEST lpcs; |
2150 | 0 | BYTE* pb; |
2151 | 0 | } sendPci, recvPci, inRecvPci, inSendPci; |
2152 | |
|
2153 | 0 | sendPci.ps = NULL; |
2154 | 0 | recvPci.ps = NULL; |
2155 | 0 | inRecvPci.lps = pioRecvPci; |
2156 | 0 | inSendPci.lpcs = pioSendPci; |
2157 | |
|
2158 | 0 | if (!g_PCSC.pfnSCardTransmit) |
2159 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardTransmit"); |
2160 | | |
2161 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
2162 | |
|
2163 | 0 | if (!pCard) |
2164 | 0 | return SCARD_E_INVALID_VALUE; |
2165 | | |
2166 | 0 | PCSC_WaitForCardAccess(0, hCard, pCard->shared); |
2167 | |
|
2168 | 0 | if (!pcbRecvLength) |
2169 | 0 | return SCARD_E_INVALID_PARAMETER; |
2170 | | |
2171 | 0 | if (*pcbRecvLength == SCARD_AUTOALLOCATE) |
2172 | 0 | return SCARD_E_INVALID_PARAMETER; |
2173 | | |
2174 | 0 | pcsc_cbRecvLength = (PCSC_DWORD)*pcbRecvLength; |
2175 | |
|
2176 | 0 | if (!inSendPci.lpcs) |
2177 | 0 | { |
2178 | 0 | PCSC_DWORD dwState = 0; |
2179 | 0 | PCSC_DWORD cbAtrLen = 0; |
2180 | 0 | PCSC_DWORD dwProtocol = 0; |
2181 | 0 | PCSC_DWORD cchReaderLen = 0; |
2182 | | /** |
2183 | | * pcsc-lite cannot have a null pioSendPci parameter, unlike WinSCard. |
2184 | | * Query the current protocol and use default SCARD_IO_REQUEST for it. |
2185 | | */ |
2186 | 0 | status = g_PCSC.pfnSCardStatus(hCard, NULL, &cchReaderLen, &dwState, &dwProtocol, NULL, |
2187 | 0 | &cbAtrLen); |
2188 | |
|
2189 | 0 | if (status == SCARD_S_SUCCESS) |
2190 | 0 | { |
2191 | 0 | if (dwProtocol == SCARD_PROTOCOL_T0) |
2192 | 0 | sendPci.pcs = PCSC_SCARD_PCI_T0; |
2193 | 0 | else if (dwProtocol == SCARD_PROTOCOL_T1) |
2194 | 0 | sendPci.pcs = PCSC_SCARD_PCI_T1; |
2195 | 0 | else if (dwProtocol == PCSC_SCARD_PROTOCOL_RAW) |
2196 | 0 | sendPci.pcs = PCSC_SCARD_PCI_RAW; |
2197 | 0 | } |
2198 | 0 | } |
2199 | 0 | else |
2200 | 0 | { |
2201 | 0 | cbExtraBytes = inSendPci.lpcs->cbPciLength - sizeof(SCARD_IO_REQUEST); |
2202 | 0 | sendPci.ps = (PCSC_SCARD_IO_REQUEST*)malloc(sizeof(PCSC_SCARD_IO_REQUEST) + cbExtraBytes); |
2203 | |
|
2204 | 0 | if (!sendPci.ps) |
2205 | 0 | return SCARD_E_NO_MEMORY; |
2206 | | |
2207 | 0 | sendPci.ps->dwProtocol = (PCSC_DWORD)inSendPci.lpcs->dwProtocol; |
2208 | 0 | sendPci.ps->cbPciLength = sizeof(PCSC_SCARD_IO_REQUEST) + cbExtraBytes; |
2209 | 0 | pbExtraBytes = &(inSendPci.pb)[sizeof(SCARD_IO_REQUEST)]; |
2210 | 0 | pcsc_pbExtraBytes = &(sendPci.pb)[sizeof(PCSC_SCARD_IO_REQUEST)]; |
2211 | 0 | CopyMemory(pcsc_pbExtraBytes, pbExtraBytes, cbExtraBytes); |
2212 | 0 | } |
2213 | | |
2214 | 0 | if (inRecvPci.lps) |
2215 | 0 | { |
2216 | 0 | cbExtraBytes = inRecvPci.lps->cbPciLength - sizeof(SCARD_IO_REQUEST); |
2217 | 0 | recvPci.ps = (PCSC_SCARD_IO_REQUEST*)malloc(sizeof(PCSC_SCARD_IO_REQUEST) + cbExtraBytes); |
2218 | |
|
2219 | 0 | if (!recvPci.ps) |
2220 | 0 | { |
2221 | 0 | if (inSendPci.lpcs) |
2222 | 0 | free(sendPci.ps); |
2223 | |
|
2224 | 0 | return SCARD_E_NO_MEMORY; |
2225 | 0 | } |
2226 | | |
2227 | 0 | recvPci.ps->dwProtocol = (PCSC_DWORD)inRecvPci.lps->dwProtocol; |
2228 | 0 | recvPci.ps->cbPciLength = sizeof(PCSC_SCARD_IO_REQUEST) + cbExtraBytes; |
2229 | 0 | pbExtraBytes = &(inRecvPci.pb)[sizeof(SCARD_IO_REQUEST)]; |
2230 | 0 | pcsc_pbExtraBytes = &(recvPci.pb)[sizeof(PCSC_SCARD_IO_REQUEST)]; |
2231 | 0 | CopyMemory(pcsc_pbExtraBytes, pbExtraBytes, cbExtraBytes); |
2232 | 0 | } |
2233 | | |
2234 | 0 | status = g_PCSC.pfnSCardTransmit(hCard, sendPci.ps, pbSendBuffer, pcsc_cbSendLength, recvPci.ps, |
2235 | 0 | pbRecvBuffer, &pcsc_cbRecvLength); |
2236 | |
|
2237 | 0 | *pcbRecvLength = (DWORD)pcsc_cbRecvLength; |
2238 | |
|
2239 | 0 | if (inSendPci.lpcs) |
2240 | 0 | free(sendPci.ps); /* pcsc_pioSendPci is dynamically allocated only when pioSendPci is |
2241 | | non null */ |
2242 | |
|
2243 | 0 | if (inRecvPci.lps) |
2244 | 0 | { |
2245 | 0 | cbExtraBytes = inRecvPci.lps->cbPciLength - sizeof(SCARD_IO_REQUEST); |
2246 | 0 | pbExtraBytes = &(inRecvPci.pb)[sizeof(SCARD_IO_REQUEST)]; |
2247 | 0 | pcsc_pbExtraBytes = &(recvPci.pb)[sizeof(PCSC_SCARD_IO_REQUEST)]; |
2248 | 0 | CopyMemory(pbExtraBytes, pcsc_pbExtraBytes, cbExtraBytes); /* copy extra bytes */ |
2249 | 0 | free(recvPci.ps); /* pcsc_pioRecvPci is dynamically allocated only when pioRecvPci is |
2250 | | non null */ |
2251 | 0 | } |
2252 | |
|
2253 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
2254 | 0 | } |
2255 | | |
2256 | | static LONG WINAPI PCSC_SCardGetTransmitCount(SCARDHANDLE hCard, LPDWORD pcTransmitCount) |
2257 | 0 | { |
2258 | 0 | WINPR_UNUSED(pcTransmitCount); |
2259 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
2260 | |
|
2261 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
2262 | |
|
2263 | 0 | if (!pCard) |
2264 | 0 | return SCARD_E_INVALID_VALUE; |
2265 | | |
2266 | 0 | PCSC_WaitForCardAccess(0, hCard, pCard->shared); |
2267 | 0 | return SCARD_S_SUCCESS; |
2268 | 0 | } |
2269 | | |
2270 | | static LONG WINAPI PCSC_SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID lpInBuffer, |
2271 | | DWORD cbInBufferSize, LPVOID lpOutBuffer, |
2272 | | DWORD cbOutBufferSize, LPDWORD lpBytesReturned) |
2273 | 0 | { |
2274 | 0 | DWORD IoCtlFunction = 0; |
2275 | 0 | DWORD IoCtlDeviceType = 0; |
2276 | 0 | BOOL getFeatureRequest = FALSE; |
2277 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
2278 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
2279 | 0 | PCSC_DWORD pcsc_dwControlCode = 0; |
2280 | 0 | PCSC_DWORD pcsc_cbInBufferSize = (PCSC_DWORD)cbInBufferSize; |
2281 | 0 | PCSC_DWORD pcsc_cbOutBufferSize = (PCSC_DWORD)cbOutBufferSize; |
2282 | 0 | PCSC_DWORD pcsc_BytesReturned = 0; |
2283 | |
|
2284 | 0 | if (!g_PCSC.pfnSCardControl) |
2285 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardControl"); |
2286 | | |
2287 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
2288 | |
|
2289 | 0 | if (!pCard) |
2290 | 0 | return SCARD_E_INVALID_VALUE; |
2291 | | |
2292 | 0 | PCSC_WaitForCardAccess(0, hCard, pCard->shared); |
2293 | | /** |
2294 | | * PCSCv2 Part 10: |
2295 | | * http://www.pcscworkgroup.com/specifications/files/pcsc10_v2.02.09.pdf |
2296 | | * |
2297 | | * Smart Card Driver IOCTLs: |
2298 | | * http://msdn.microsoft.com/en-us/library/windows/hardware/ff548988/ |
2299 | | * |
2300 | | * Converting Windows Feature Request IOCTL code to the pcsc-lite control code: |
2301 | | * http://musclecard.996296.n3.nabble.com/Converting-Windows-Feature-Request-IOCTL-code-to-the-pcsc-lite-control-code-td4906.html |
2302 | | */ |
2303 | 0 | IoCtlFunction = FUNCTION_FROM_CTL_CODE(dwControlCode); |
2304 | 0 | IoCtlDeviceType = DEVICE_TYPE_FROM_CTL_CODE(dwControlCode); |
2305 | |
|
2306 | 0 | if (dwControlCode == IOCTL_SMARTCARD_GET_FEATURE_REQUEST) |
2307 | 0 | getFeatureRequest = TRUE; |
2308 | |
|
2309 | 0 | if (IoCtlDeviceType == FILE_DEVICE_SMARTCARD) |
2310 | 0 | dwControlCode = PCSC_SCARD_CTL_CODE(IoCtlFunction); |
2311 | |
|
2312 | 0 | pcsc_dwControlCode = (PCSC_DWORD)dwControlCode; |
2313 | 0 | status = g_PCSC.pfnSCardControl(hCard, pcsc_dwControlCode, lpInBuffer, pcsc_cbInBufferSize, |
2314 | 0 | lpOutBuffer, pcsc_cbOutBufferSize, &pcsc_BytesReturned); |
2315 | |
|
2316 | 0 | *lpBytesReturned = (DWORD)pcsc_BytesReturned; |
2317 | |
|
2318 | 0 | if (getFeatureRequest) |
2319 | 0 | { |
2320 | 0 | UINT32 count = 0; |
2321 | 0 | PCSC_TLV_STRUCTURE* tlv = (PCSC_TLV_STRUCTURE*)lpOutBuffer; |
2322 | |
|
2323 | 0 | if ((*lpBytesReturned % sizeof(PCSC_TLV_STRUCTURE)) != 0) |
2324 | 0 | return SCARD_E_UNEXPECTED; |
2325 | | |
2326 | 0 | count = *lpBytesReturned / sizeof(PCSC_TLV_STRUCTURE); |
2327 | |
|
2328 | 0 | for (DWORD index = 0; index < count; index++) |
2329 | 0 | { |
2330 | 0 | if (tlv[index].length != 4) |
2331 | 0 | return SCARD_E_UNEXPECTED; |
2332 | 0 | } |
2333 | 0 | } |
2334 | | |
2335 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
2336 | 0 | } |
2337 | | |
2338 | | static LONG WINAPI PCSC_SCardGetAttrib_Internal(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, |
2339 | | LPDWORD pcbAttrLen) |
2340 | 0 | { |
2341 | 0 | SCARDCONTEXT hContext = 0; |
2342 | 0 | BOOL pcbAttrLenAlloc = FALSE; |
2343 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
2344 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
2345 | 0 | PCSC_DWORD pcsc_dwAttrId = (PCSC_DWORD)dwAttrId; |
2346 | 0 | PCSC_DWORD pcsc_cbAttrLen = 0; |
2347 | |
|
2348 | 0 | if (!g_PCSC.pfnSCardGetAttrib) |
2349 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardGetAttrib"); |
2350 | | |
2351 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
2352 | |
|
2353 | 0 | if (!pCard) |
2354 | 0 | return SCARD_E_INVALID_VALUE; |
2355 | | |
2356 | 0 | PCSC_WaitForCardAccess(0, hCard, pCard->shared); |
2357 | 0 | hContext = PCSC_GetCardContextFromHandle(hCard); |
2358 | |
|
2359 | 0 | if (!hContext) |
2360 | 0 | return SCARD_E_INVALID_HANDLE; |
2361 | | |
2362 | 0 | if (!pcbAttrLen) |
2363 | 0 | return SCARD_E_INVALID_PARAMETER; |
2364 | | |
2365 | 0 | if (*pcbAttrLen == SCARD_AUTOALLOCATE) |
2366 | 0 | { |
2367 | 0 | if (!pbAttr) |
2368 | 0 | return SCARD_E_INVALID_PARAMETER; |
2369 | 0 | pcbAttrLenAlloc = TRUE; |
2370 | 0 | } |
2371 | | |
2372 | 0 | pcsc_cbAttrLen = pcbAttrLenAlloc ? PCSC_SCARD_AUTOALLOCATE : (PCSC_DWORD)*pcbAttrLen; |
2373 | |
|
2374 | 0 | if (pcbAttrLenAlloc && !g_SCardAutoAllocate) |
2375 | 0 | { |
2376 | 0 | pcsc_cbAttrLen = 0; |
2377 | 0 | status = g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, NULL, &pcsc_cbAttrLen); |
2378 | |
|
2379 | 0 | if (status == SCARD_S_SUCCESS) |
2380 | 0 | { |
2381 | 0 | BYTE* tmp = (BYTE*)calloc(1, pcsc_cbAttrLen); |
2382 | |
|
2383 | 0 | if (!tmp) |
2384 | 0 | return SCARD_E_NO_MEMORY; |
2385 | | |
2386 | 0 | status = g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, tmp, &pcsc_cbAttrLen); |
2387 | |
|
2388 | 0 | if (status != SCARD_S_SUCCESS) |
2389 | 0 | { |
2390 | 0 | free(tmp); |
2391 | 0 | tmp = NULL; |
2392 | 0 | } |
2393 | 0 | else |
2394 | 0 | PCSC_AddMemoryBlock(hContext, tmp); |
2395 | 0 | *(BYTE**)pbAttr = tmp; |
2396 | 0 | } |
2397 | 0 | } |
2398 | 0 | else |
2399 | 0 | { |
2400 | 0 | status = g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, pbAttr, &pcsc_cbAttrLen); |
2401 | 0 | } |
2402 | | |
2403 | 0 | if (status == SCARD_S_SUCCESS) |
2404 | 0 | *pcbAttrLen = (DWORD)pcsc_cbAttrLen; |
2405 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
2406 | 0 | } |
2407 | | |
2408 | | static LONG WINAPI PCSC_SCardGetAttrib_FriendlyName(SCARDHANDLE hCard, DWORD dwAttrId, |
2409 | | LPBYTE pbAttr, LPDWORD pcbAttrLen) |
2410 | 0 | { |
2411 | 0 | size_t length = 0; |
2412 | 0 | char* namePCSC = NULL; |
2413 | 0 | char* pbAttrA = NULL; |
2414 | 0 | DWORD cbAttrLen = 0; |
2415 | 0 | WCHAR* pbAttrW = NULL; |
2416 | 0 | SCARDCONTEXT hContext = 0; |
2417 | 0 | LONG status = SCARD_S_SUCCESS; |
2418 | |
|
2419 | 0 | hContext = PCSC_GetCardContextFromHandle(hCard); |
2420 | |
|
2421 | 0 | if (!hContext) |
2422 | 0 | return SCARD_E_INVALID_HANDLE; |
2423 | | |
2424 | 0 | if (!pcbAttrLen) |
2425 | 0 | return SCARD_E_INVALID_PARAMETER; |
2426 | 0 | cbAttrLen = *pcbAttrLen; |
2427 | 0 | *pcbAttrLen = SCARD_AUTOALLOCATE; |
2428 | 0 | status = PCSC_SCardGetAttrib_Internal(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME_A, |
2429 | 0 | (LPBYTE)&pbAttrA, pcbAttrLen); |
2430 | |
|
2431 | 0 | if (status != SCARD_S_SUCCESS) |
2432 | 0 | { |
2433 | 0 | *pcbAttrLen = SCARD_AUTOALLOCATE; |
2434 | 0 | status = PCSC_SCardGetAttrib_Internal(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME_W, |
2435 | 0 | (LPBYTE)&pbAttrW, pcbAttrLen); |
2436 | |
|
2437 | 0 | if (status != SCARD_S_SUCCESS) |
2438 | 0 | return status; |
2439 | | |
2440 | 0 | namePCSC = ConvertMszWCharNToUtf8Alloc(pbAttrW, *pcbAttrLen, NULL); |
2441 | 0 | PCSC_SCardFreeMemory_Internal(hContext, pbAttrW); |
2442 | 0 | } |
2443 | 0 | else |
2444 | 0 | { |
2445 | 0 | namePCSC = _strdup(pbAttrA); |
2446 | |
|
2447 | 0 | if (!namePCSC) |
2448 | 0 | return SCARD_E_NO_MEMORY; |
2449 | | |
2450 | 0 | PCSC_SCardFreeMemory_Internal(hContext, pbAttrA); |
2451 | 0 | } |
2452 | | |
2453 | 0 | length = strlen(namePCSC); |
2454 | |
|
2455 | 0 | if (dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_W) |
2456 | 0 | { |
2457 | 0 | size_t size = 0; |
2458 | 0 | WCHAR* friendlyNameW = ConvertUtf8ToWCharAlloc(namePCSC, &size); |
2459 | | /* length here includes null terminator */ |
2460 | |
|
2461 | 0 | if (!friendlyNameW) |
2462 | 0 | status = SCARD_E_NO_MEMORY; |
2463 | 0 | else |
2464 | 0 | { |
2465 | 0 | length = size; |
2466 | |
|
2467 | 0 | if (cbAttrLen == SCARD_AUTOALLOCATE) |
2468 | 0 | { |
2469 | 0 | WINPR_ASSERT(length <= UINT32_MAX / sizeof(WCHAR)); |
2470 | 0 | *(WCHAR**)pbAttr = friendlyNameW; |
2471 | 0 | *pcbAttrLen = (UINT32)length * sizeof(WCHAR); |
2472 | 0 | PCSC_AddMemoryBlock(hContext, friendlyNameW); |
2473 | 0 | } |
2474 | 0 | else |
2475 | 0 | { |
2476 | 0 | if ((length * 2) > cbAttrLen) |
2477 | 0 | status = SCARD_E_INSUFFICIENT_BUFFER; |
2478 | 0 | else |
2479 | 0 | { |
2480 | 0 | WINPR_ASSERT(length <= UINT32_MAX / sizeof(WCHAR)); |
2481 | 0 | CopyMemory(pbAttr, (BYTE*)friendlyNameW, (length * sizeof(WCHAR))); |
2482 | 0 | *pcbAttrLen = (UINT32)length * sizeof(WCHAR); |
2483 | 0 | } |
2484 | 0 | free(friendlyNameW); |
2485 | 0 | } |
2486 | 0 | } |
2487 | 0 | free(namePCSC); |
2488 | 0 | } |
2489 | 0 | else |
2490 | 0 | { |
2491 | 0 | if (cbAttrLen == SCARD_AUTOALLOCATE) |
2492 | 0 | { |
2493 | 0 | *(CHAR**)pbAttr = namePCSC; |
2494 | 0 | WINPR_ASSERT(length <= UINT32_MAX); |
2495 | 0 | *pcbAttrLen = (UINT32)length; |
2496 | 0 | PCSC_AddMemoryBlock(hContext, namePCSC); |
2497 | 0 | } |
2498 | 0 | else |
2499 | 0 | { |
2500 | 0 | if ((length + 1) > cbAttrLen) |
2501 | 0 | status = SCARD_E_INSUFFICIENT_BUFFER; |
2502 | 0 | else |
2503 | 0 | { |
2504 | 0 | CopyMemory(pbAttr, namePCSC, length + 1); |
2505 | 0 | WINPR_ASSERT(length <= UINT32_MAX); |
2506 | 0 | *pcbAttrLen = (UINT32)length; |
2507 | 0 | } |
2508 | 0 | free(namePCSC); |
2509 | 0 | } |
2510 | 0 | } |
2511 | | |
2512 | 0 | return status; |
2513 | 0 | } |
2514 | | |
2515 | | static LONG WINAPI PCSC_SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, |
2516 | | LPDWORD pcbAttrLen) |
2517 | 0 | { |
2518 | 0 | DWORD cbAttrLen = 0; |
2519 | 0 | SCARDCONTEXT hContext = 0; |
2520 | 0 | BOOL pcbAttrLenAlloc = FALSE; |
2521 | 0 | LONG status = SCARD_S_SUCCESS; |
2522 | |
|
2523 | 0 | if (NULL == pcbAttrLen) |
2524 | 0 | return SCARD_E_INVALID_PARAMETER; |
2525 | | |
2526 | 0 | cbAttrLen = *pcbAttrLen; |
2527 | |
|
2528 | 0 | if (*pcbAttrLen == SCARD_AUTOALLOCATE) |
2529 | 0 | { |
2530 | 0 | if (NULL == pbAttr) |
2531 | 0 | return SCARD_E_INVALID_PARAMETER; |
2532 | | |
2533 | 0 | pcbAttrLenAlloc = TRUE; |
2534 | 0 | *(BYTE**)pbAttr = NULL; |
2535 | 0 | } |
2536 | 0 | else |
2537 | 0 | { |
2538 | | /** |
2539 | | * pcsc-lite returns SCARD_E_INSUFFICIENT_BUFFER if the given |
2540 | | * buffer size is larger than PCSC_MAX_BUFFER_SIZE (264) |
2541 | | */ |
2542 | 0 | if (*pcbAttrLen > PCSC_MAX_BUFFER_SIZE) |
2543 | 0 | *pcbAttrLen = PCSC_MAX_BUFFER_SIZE; |
2544 | 0 | } |
2545 | | |
2546 | 0 | hContext = PCSC_GetCardContextFromHandle(hCard); |
2547 | |
|
2548 | 0 | if (!hContext) |
2549 | 0 | return SCARD_E_INVALID_HANDLE; |
2550 | | |
2551 | 0 | if ((dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_A) || |
2552 | 0 | (dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_W)) |
2553 | 0 | { |
2554 | 0 | status = PCSC_SCardGetAttrib_FriendlyName(hCard, dwAttrId, pbAttr, pcbAttrLen); |
2555 | 0 | return status; |
2556 | 0 | } |
2557 | | |
2558 | 0 | status = PCSC_SCardGetAttrib_Internal(hCard, dwAttrId, pbAttr, pcbAttrLen); |
2559 | |
|
2560 | 0 | if (status == SCARD_S_SUCCESS) |
2561 | 0 | { |
2562 | 0 | if (dwAttrId == SCARD_ATTR_VENDOR_NAME) |
2563 | 0 | { |
2564 | 0 | if (pbAttr) |
2565 | 0 | { |
2566 | 0 | const char* vendorName = NULL; |
2567 | | |
2568 | | /** |
2569 | | * pcsc-lite adds a null terminator to the vendor name, |
2570 | | * while WinSCard doesn't. Strip the null terminator. |
2571 | | */ |
2572 | |
|
2573 | 0 | if (pcbAttrLenAlloc) |
2574 | 0 | vendorName = (char*)*(BYTE**)pbAttr; |
2575 | 0 | else |
2576 | 0 | vendorName = (char*)pbAttr; |
2577 | |
|
2578 | 0 | if (vendorName) |
2579 | 0 | { |
2580 | 0 | size_t len = strnlen(vendorName, *pcbAttrLen); |
2581 | 0 | WINPR_ASSERT(len <= UINT32_MAX); |
2582 | 0 | *pcbAttrLen = (DWORD)len; |
2583 | 0 | } |
2584 | 0 | else |
2585 | 0 | *pcbAttrLen = 0; |
2586 | 0 | } |
2587 | 0 | } |
2588 | 0 | } |
2589 | 0 | else |
2590 | 0 | { |
2591 | |
|
2592 | 0 | if (dwAttrId == SCARD_ATTR_CURRENT_PROTOCOL_TYPE) |
2593 | 0 | { |
2594 | 0 | if (!pcbAttrLenAlloc) |
2595 | 0 | { |
2596 | 0 | PCSC_DWORD dwState = 0; |
2597 | 0 | PCSC_DWORD cbAtrLen = 0; |
2598 | 0 | PCSC_DWORD dwProtocol = 0; |
2599 | 0 | PCSC_DWORD cchReaderLen = 0; |
2600 | 0 | status = (LONG)g_PCSC.pfnSCardStatus(hCard, NULL, &cchReaderLen, &dwState, |
2601 | 0 | &dwProtocol, NULL, &cbAtrLen); |
2602 | |
|
2603 | 0 | if (status == SCARD_S_SUCCESS) |
2604 | 0 | { |
2605 | 0 | if (cbAttrLen < sizeof(DWORD)) |
2606 | 0 | return SCARD_E_INSUFFICIENT_BUFFER; |
2607 | | |
2608 | 0 | *(DWORD*)pbAttr = PCSC_ConvertProtocolsToWinSCard(dwProtocol); |
2609 | 0 | *pcbAttrLen = sizeof(DWORD); |
2610 | 0 | } |
2611 | 0 | } |
2612 | 0 | } |
2613 | 0 | else if (dwAttrId == SCARD_ATTR_CHANNEL_ID) |
2614 | 0 | { |
2615 | 0 | if (!pcbAttrLenAlloc) |
2616 | 0 | { |
2617 | 0 | UINT32 channelType = 0x20; /* USB */ |
2618 | 0 | UINT32 channelNumber = 0; |
2619 | |
|
2620 | 0 | if (cbAttrLen < sizeof(DWORD)) |
2621 | 0 | return SCARD_E_INSUFFICIENT_BUFFER; |
2622 | | |
2623 | 0 | status = SCARD_S_SUCCESS; |
2624 | 0 | *(DWORD*)pbAttr = (channelType << 16u) | channelNumber; |
2625 | 0 | *pcbAttrLen = sizeof(DWORD); |
2626 | 0 | } |
2627 | 0 | } |
2628 | 0 | else if (dwAttrId == SCARD_ATTR_VENDOR_IFD_TYPE) |
2629 | 0 | { |
2630 | 0 | } |
2631 | 0 | else if (dwAttrId == SCARD_ATTR_DEFAULT_CLK) |
2632 | 0 | { |
2633 | 0 | } |
2634 | 0 | else if (dwAttrId == SCARD_ATTR_DEFAULT_DATA_RATE) |
2635 | 0 | { |
2636 | 0 | } |
2637 | 0 | else if (dwAttrId == SCARD_ATTR_MAX_CLK) |
2638 | 0 | { |
2639 | 0 | } |
2640 | 0 | else if (dwAttrId == SCARD_ATTR_MAX_DATA_RATE) |
2641 | 0 | { |
2642 | 0 | } |
2643 | 0 | else if (dwAttrId == SCARD_ATTR_MAX_IFSD) |
2644 | 0 | { |
2645 | 0 | } |
2646 | 0 | else if (dwAttrId == SCARD_ATTR_CHARACTERISTICS) |
2647 | 0 | { |
2648 | 0 | } |
2649 | 0 | else if (dwAttrId == SCARD_ATTR_DEVICE_SYSTEM_NAME_A) |
2650 | 0 | { |
2651 | 0 | } |
2652 | 0 | else if (dwAttrId == SCARD_ATTR_DEVICE_UNIT) |
2653 | 0 | { |
2654 | 0 | } |
2655 | 0 | else if (dwAttrId == SCARD_ATTR_POWER_MGMT_SUPPORT) |
2656 | 0 | { |
2657 | 0 | } |
2658 | 0 | else if (dwAttrId == SCARD_ATTR_CURRENT_CLK) |
2659 | 0 | { |
2660 | 0 | } |
2661 | 0 | else if (dwAttrId == SCARD_ATTR_CURRENT_F) |
2662 | 0 | { |
2663 | 0 | } |
2664 | 0 | else if (dwAttrId == SCARD_ATTR_CURRENT_D) |
2665 | 0 | { |
2666 | 0 | } |
2667 | 0 | else if (dwAttrId == SCARD_ATTR_CURRENT_N) |
2668 | 0 | { |
2669 | 0 | } |
2670 | 0 | else if (dwAttrId == SCARD_ATTR_CURRENT_CWT) |
2671 | 0 | { |
2672 | 0 | } |
2673 | 0 | else if (dwAttrId == SCARD_ATTR_CURRENT_BWT) |
2674 | 0 | { |
2675 | 0 | } |
2676 | 0 | else if (dwAttrId == SCARD_ATTR_CURRENT_IFSC) |
2677 | 0 | { |
2678 | 0 | } |
2679 | 0 | else if (dwAttrId == SCARD_ATTR_CURRENT_EBC_ENCODING) |
2680 | 0 | { |
2681 | 0 | } |
2682 | 0 | else if (dwAttrId == SCARD_ATTR_CURRENT_IFSD) |
2683 | 0 | { |
2684 | 0 | } |
2685 | 0 | else if (dwAttrId == SCARD_ATTR_ICC_TYPE_PER_ATR) |
2686 | 0 | { |
2687 | 0 | } |
2688 | 0 | } |
2689 | | |
2690 | 0 | return status; |
2691 | 0 | } |
2692 | | |
2693 | | static LONG WINAPI PCSC_SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, |
2694 | | DWORD cbAttrLen) |
2695 | 0 | { |
2696 | 0 | PCSC_LONG status = SCARD_S_SUCCESS; |
2697 | 0 | PCSC_SCARDHANDLE* pCard = NULL; |
2698 | 0 | PCSC_DWORD pcsc_dwAttrId = (PCSC_DWORD)dwAttrId; |
2699 | 0 | PCSC_DWORD pcsc_cbAttrLen = (PCSC_DWORD)cbAttrLen; |
2700 | |
|
2701 | 0 | if (!g_PCSC.pfnSCardSetAttrib) |
2702 | 0 | return PCSC_SCard_LogError("g_PCSC.pfnSCardSetAttrib"); |
2703 | | |
2704 | 0 | pCard = PCSC_GetCardHandleData(hCard); |
2705 | |
|
2706 | 0 | if (!pCard) |
2707 | 0 | return SCARD_E_INVALID_VALUE; |
2708 | | |
2709 | 0 | PCSC_WaitForCardAccess(0, hCard, pCard->shared); |
2710 | 0 | status = g_PCSC.pfnSCardSetAttrib(hCard, pcsc_dwAttrId, pbAttr, pcsc_cbAttrLen); |
2711 | 0 | return PCSC_MapErrorCodeToWinSCard(status); |
2712 | 0 | } |
2713 | | |
2714 | | static LONG WINAPI PCSC_SCardUIDlgSelectCardA(LPOPENCARDNAMEA_EX pDlgStruc) |
2715 | 0 | { |
2716 | 0 | WINPR_UNUSED(pDlgStruc); |
2717 | |
|
2718 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
2719 | 0 | } |
2720 | | |
2721 | | static LONG WINAPI PCSC_SCardUIDlgSelectCardW(LPOPENCARDNAMEW_EX pDlgStruc) |
2722 | 0 | { |
2723 | 0 | WINPR_UNUSED(pDlgStruc); |
2724 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
2725 | 0 | } |
2726 | | |
2727 | | static LONG WINAPI PCSC_GetOpenCardNameA(LPOPENCARDNAMEA pDlgStruc) |
2728 | 0 | { |
2729 | 0 | WINPR_UNUSED(pDlgStruc); |
2730 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
2731 | 0 | } |
2732 | | |
2733 | | static LONG WINAPI PCSC_GetOpenCardNameW(LPOPENCARDNAMEW pDlgStruc) |
2734 | 0 | { |
2735 | 0 | WINPR_UNUSED(pDlgStruc); |
2736 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
2737 | 0 | } |
2738 | | |
2739 | | static LONG WINAPI PCSC_SCardDlgExtendedError(void) |
2740 | 0 | { |
2741 | |
|
2742 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
2743 | 0 | } |
2744 | | |
2745 | | static char* card_id_and_name_a(const UUID* CardIdentifier, LPCSTR LookupName) |
2746 | 0 | { |
2747 | 0 | WINPR_ASSERT(CardIdentifier); |
2748 | 0 | WINPR_ASSERT(LookupName); |
2749 | | |
2750 | 0 | size_t len = strlen(LookupName) + 34; |
2751 | 0 | char* id = malloc(len); |
2752 | 0 | if (!id) |
2753 | 0 | return NULL; |
2754 | | |
2755 | 0 | snprintf(id, len, "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X\\%s", CardIdentifier->Data1, |
2756 | 0 | CardIdentifier->Data2, CardIdentifier->Data3, CardIdentifier->Data4[0], |
2757 | 0 | CardIdentifier->Data4[1], CardIdentifier->Data4[2], CardIdentifier->Data4[3], |
2758 | 0 | CardIdentifier->Data4[4], CardIdentifier->Data4[5], CardIdentifier->Data4[6], |
2759 | 0 | CardIdentifier->Data4[7], LookupName); |
2760 | 0 | return id; |
2761 | 0 | } |
2762 | | |
2763 | | static char* card_id_and_name_w(const UUID* CardIdentifier, LPCWSTR LookupName) |
2764 | 0 | { |
2765 | 0 | char* res = NULL; |
2766 | 0 | char* tmp = ConvertWCharToUtf8Alloc(LookupName, NULL); |
2767 | 0 | if (!tmp) |
2768 | 0 | return NULL; |
2769 | 0 | res = card_id_and_name_a(CardIdentifier, tmp); |
2770 | 0 | free(tmp); |
2771 | 0 | return res; |
2772 | 0 | } |
2773 | | |
2774 | | static LONG WINAPI PCSC_SCardReadCacheA(SCARDCONTEXT hContext, UUID* CardIdentifier, |
2775 | | DWORD FreshnessCounter, LPSTR LookupName, PBYTE Data, |
2776 | | DWORD* DataLen) |
2777 | 0 | { |
2778 | 0 | PCSC_CACHE_ITEM* data = NULL; |
2779 | 0 | PCSC_SCARDCONTEXT* ctx = PCSC_GetCardContextData(hContext); |
2780 | 0 | if (!ctx) |
2781 | 0 | return SCARD_E_INVALID_HANDLE; |
2782 | | |
2783 | 0 | char* id = card_id_and_name_a(CardIdentifier, LookupName); |
2784 | |
|
2785 | 0 | data = HashTable_GetItemValue(ctx->cache, id); |
2786 | 0 | free(id); |
2787 | 0 | if (!data) |
2788 | 0 | { |
2789 | 0 | *DataLen = 0; |
2790 | 0 | return SCARD_W_CACHE_ITEM_NOT_FOUND; |
2791 | 0 | } |
2792 | | |
2793 | 0 | if (FreshnessCounter != data->freshness) |
2794 | 0 | { |
2795 | 0 | *DataLen = 0; |
2796 | 0 | return SCARD_W_CACHE_ITEM_STALE; |
2797 | 0 | } |
2798 | | |
2799 | 0 | if (*DataLen == SCARD_AUTOALLOCATE) |
2800 | 0 | { |
2801 | 0 | BYTE* mem = calloc(1, data->len); |
2802 | 0 | if (!mem) |
2803 | 0 | return SCARD_E_NO_MEMORY; |
2804 | | |
2805 | 0 | if (!PCSC_AddMemoryBlock(hContext, mem)) |
2806 | 0 | { |
2807 | 0 | free(mem); |
2808 | 0 | return SCARD_E_NO_MEMORY; |
2809 | 0 | } |
2810 | | |
2811 | 0 | memcpy(mem, data->data, data->len); |
2812 | 0 | *(BYTE**)Data = mem; |
2813 | 0 | } |
2814 | 0 | else |
2815 | 0 | memcpy(Data, data->data, data->len); |
2816 | 0 | *DataLen = data->len; |
2817 | 0 | return SCARD_S_SUCCESS; |
2818 | 0 | } |
2819 | | |
2820 | | static LONG WINAPI PCSC_SCardReadCacheW(SCARDCONTEXT hContext, UUID* CardIdentifier, |
2821 | | DWORD FreshnessCounter, LPWSTR LookupName, PBYTE Data, |
2822 | | DWORD* DataLen) |
2823 | 0 | { |
2824 | 0 | PCSC_CACHE_ITEM* data = NULL; |
2825 | 0 | PCSC_SCARDCONTEXT* ctx = PCSC_GetCardContextData(hContext); |
2826 | 0 | if (!ctx) |
2827 | 0 | return SCARD_E_INVALID_HANDLE; |
2828 | | |
2829 | 0 | char* id = card_id_and_name_w(CardIdentifier, LookupName); |
2830 | |
|
2831 | 0 | data = HashTable_GetItemValue(ctx->cache, id); |
2832 | 0 | free(id); |
2833 | |
|
2834 | 0 | if (!data) |
2835 | 0 | { |
2836 | 0 | *DataLen = 0; |
2837 | 0 | return SCARD_W_CACHE_ITEM_NOT_FOUND; |
2838 | 0 | } |
2839 | | |
2840 | 0 | if (FreshnessCounter != data->freshness) |
2841 | 0 | { |
2842 | 0 | *DataLen = 0; |
2843 | 0 | return SCARD_W_CACHE_ITEM_STALE; |
2844 | 0 | } |
2845 | | |
2846 | 0 | if (*DataLen == SCARD_AUTOALLOCATE) |
2847 | 0 | { |
2848 | 0 | BYTE* mem = calloc(1, data->len); |
2849 | 0 | if (!mem) |
2850 | 0 | return SCARD_E_NO_MEMORY; |
2851 | | |
2852 | 0 | if (!PCSC_AddMemoryBlock(hContext, mem)) |
2853 | 0 | { |
2854 | 0 | free(mem); |
2855 | 0 | return SCARD_E_NO_MEMORY; |
2856 | 0 | } |
2857 | | |
2858 | 0 | memcpy(mem, data->data, data->len); |
2859 | 0 | *(BYTE**)Data = mem; |
2860 | 0 | } |
2861 | 0 | else |
2862 | 0 | memcpy(Data, data->data, data->len); |
2863 | 0 | *DataLen = data->len; |
2864 | 0 | return SCARD_S_SUCCESS; |
2865 | 0 | } |
2866 | | |
2867 | | static LONG WINAPI PCSC_SCardWriteCacheA(SCARDCONTEXT hContext, UUID* CardIdentifier, |
2868 | | DWORD FreshnessCounter, LPSTR LookupName, PBYTE Data, |
2869 | | DWORD DataLen) |
2870 | 0 | { |
2871 | 0 | PCSC_CACHE_ITEM* data = NULL; |
2872 | 0 | PCSC_SCARDCONTEXT* ctx = PCSC_GetCardContextData(hContext); |
2873 | 0 | char* id = NULL; |
2874 | |
|
2875 | 0 | if (!ctx) |
2876 | 0 | return SCARD_E_FILE_NOT_FOUND; |
2877 | | |
2878 | 0 | id = card_id_and_name_a(CardIdentifier, LookupName); |
2879 | |
|
2880 | 0 | if (!id) |
2881 | 0 | return SCARD_E_NO_MEMORY; |
2882 | | |
2883 | 0 | data = malloc(sizeof(PCSC_CACHE_ITEM)); |
2884 | 0 | if (!data) |
2885 | 0 | { |
2886 | 0 | free(id); |
2887 | 0 | return SCARD_E_NO_MEMORY; |
2888 | 0 | } |
2889 | 0 | data->data = calloc(DataLen, 1); |
2890 | 0 | if (!data->data) |
2891 | 0 | { |
2892 | 0 | free(id); |
2893 | 0 | free(data); |
2894 | 0 | return SCARD_E_NO_MEMORY; |
2895 | 0 | } |
2896 | 0 | data->len = DataLen; |
2897 | 0 | data->freshness = FreshnessCounter; |
2898 | 0 | memcpy(data->data, Data, data->len); |
2899 | |
|
2900 | 0 | HashTable_Remove(ctx->cache, id); |
2901 | 0 | const BOOL rc = HashTable_Insert(ctx->cache, id, data); |
2902 | 0 | free(id); |
2903 | |
|
2904 | 0 | if (!rc) |
2905 | 0 | { |
2906 | 0 | pcsc_cache_item_free(data); |
2907 | 0 | return SCARD_E_NO_MEMORY; |
2908 | 0 | } |
2909 | | |
2910 | | // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert owns data |
2911 | 0 | return SCARD_S_SUCCESS; |
2912 | 0 | } |
2913 | | |
2914 | | static LONG WINAPI PCSC_SCardWriteCacheW(SCARDCONTEXT hContext, UUID* CardIdentifier, |
2915 | | DWORD FreshnessCounter, LPWSTR LookupName, PBYTE Data, |
2916 | | DWORD DataLen) |
2917 | 0 | { |
2918 | 0 | PCSC_CACHE_ITEM* data = NULL; |
2919 | 0 | PCSC_SCARDCONTEXT* ctx = PCSC_GetCardContextData(hContext); |
2920 | 0 | char* id = NULL; |
2921 | 0 | if (!ctx) |
2922 | 0 | return SCARD_E_FILE_NOT_FOUND; |
2923 | | |
2924 | 0 | id = card_id_and_name_w(CardIdentifier, LookupName); |
2925 | |
|
2926 | 0 | if (!id) |
2927 | 0 | return SCARD_E_NO_MEMORY; |
2928 | | |
2929 | 0 | data = malloc(sizeof(PCSC_CACHE_ITEM)); |
2930 | 0 | if (!data) |
2931 | 0 | { |
2932 | 0 | free(id); |
2933 | 0 | return SCARD_E_NO_MEMORY; |
2934 | 0 | } |
2935 | 0 | data->data = malloc(DataLen); |
2936 | 0 | if (!data->data) |
2937 | 0 | { |
2938 | 0 | free(id); |
2939 | 0 | free(data); |
2940 | 0 | return SCARD_E_NO_MEMORY; |
2941 | 0 | } |
2942 | 0 | data->len = DataLen; |
2943 | 0 | data->freshness = FreshnessCounter; |
2944 | 0 | memcpy(data->data, Data, data->len); |
2945 | |
|
2946 | 0 | HashTable_Remove(ctx->cache, id); |
2947 | 0 | const BOOL rc = HashTable_Insert(ctx->cache, id, data); |
2948 | 0 | free(id); |
2949 | |
|
2950 | 0 | if (!rc) |
2951 | 0 | { |
2952 | 0 | pcsc_cache_item_free(data); |
2953 | 0 | return SCARD_E_NO_MEMORY; |
2954 | 0 | } |
2955 | | |
2956 | | // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert owns data |
2957 | 0 | return SCARD_S_SUCCESS; |
2958 | 0 | } |
2959 | | |
2960 | | static LONG WINAPI PCSC_SCardGetReaderIconA(SCARDCONTEXT hContext, LPCSTR szReaderName, |
2961 | | LPBYTE pbIcon, LPDWORD pcbIcon) |
2962 | 0 | { |
2963 | 0 | WINPR_UNUSED(hContext); |
2964 | 0 | WINPR_UNUSED(szReaderName); |
2965 | 0 | WINPR_UNUSED(pbIcon); |
2966 | 0 | WINPR_UNUSED(pcbIcon); |
2967 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
2968 | 0 | } |
2969 | | |
2970 | | static LONG WINAPI PCSC_SCardGetReaderIconW(SCARDCONTEXT hContext, LPCWSTR szReaderName, |
2971 | | LPBYTE pbIcon, LPDWORD pcbIcon) |
2972 | 0 | { |
2973 | 0 | WINPR_UNUSED(hContext); |
2974 | 0 | WINPR_UNUSED(szReaderName); |
2975 | 0 | WINPR_UNUSED(pbIcon); |
2976 | 0 | WINPR_UNUSED(pcbIcon); |
2977 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
2978 | 0 | } |
2979 | | |
2980 | | static LONG WINAPI PCSC_SCardGetDeviceTypeIdA(SCARDCONTEXT hContext, LPCSTR szReaderName, |
2981 | | LPDWORD pdwDeviceTypeId) |
2982 | 0 | { |
2983 | 0 | WINPR_UNUSED(hContext); |
2984 | 0 | WINPR_UNUSED(szReaderName); |
2985 | 0 | WINPR_UNUSED(pdwDeviceTypeId); |
2986 | 0 | if (pdwDeviceTypeId) |
2987 | 0 | *pdwDeviceTypeId = SCARD_READER_TYPE_USB; |
2988 | 0 | return SCARD_S_SUCCESS; |
2989 | 0 | } |
2990 | | |
2991 | | static LONG WINAPI PCSC_SCardGetDeviceTypeIdW(SCARDCONTEXT hContext, LPCWSTR szReaderName, |
2992 | | LPDWORD pdwDeviceTypeId) |
2993 | 0 | { |
2994 | 0 | WINPR_UNUSED(hContext); |
2995 | 0 | WINPR_UNUSED(szReaderName); |
2996 | 0 | if (pdwDeviceTypeId) |
2997 | 0 | *pdwDeviceTypeId = SCARD_READER_TYPE_USB; |
2998 | 0 | return SCARD_S_SUCCESS; |
2999 | 0 | } |
3000 | | |
3001 | | static LONG WINAPI PCSC_SCardGetReaderDeviceInstanceIdA(SCARDCONTEXT hContext, LPCSTR szReaderName, |
3002 | | LPSTR szDeviceInstanceId, |
3003 | | LPDWORD pcchDeviceInstanceId) |
3004 | 0 | { |
3005 | 0 | WINPR_UNUSED(hContext); |
3006 | 0 | WINPR_UNUSED(szReaderName); |
3007 | 0 | WINPR_UNUSED(szDeviceInstanceId); |
3008 | 0 | WINPR_UNUSED(pcchDeviceInstanceId); |
3009 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
3010 | 0 | } |
3011 | | |
3012 | | static LONG WINAPI PCSC_SCardGetReaderDeviceInstanceIdW(SCARDCONTEXT hContext, LPCWSTR szReaderName, |
3013 | | LPWSTR szDeviceInstanceId, |
3014 | | LPDWORD pcchDeviceInstanceId) |
3015 | 0 | { |
3016 | 0 | WINPR_UNUSED(hContext); |
3017 | 0 | WINPR_UNUSED(szReaderName); |
3018 | 0 | WINPR_UNUSED(szDeviceInstanceId); |
3019 | 0 | WINPR_UNUSED(pcchDeviceInstanceId); |
3020 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
3021 | 0 | } |
3022 | | |
3023 | | static LONG WINAPI PCSC_SCardListReadersWithDeviceInstanceIdA(SCARDCONTEXT hContext, |
3024 | | LPCSTR szDeviceInstanceId, |
3025 | | LPSTR mszReaders, LPDWORD pcchReaders) |
3026 | 0 | { |
3027 | 0 | WINPR_UNUSED(hContext); |
3028 | 0 | WINPR_UNUSED(szDeviceInstanceId); |
3029 | 0 | WINPR_UNUSED(mszReaders); |
3030 | 0 | WINPR_UNUSED(pcchReaders); |
3031 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
3032 | 0 | } |
3033 | | |
3034 | | static LONG WINAPI PCSC_SCardListReadersWithDeviceInstanceIdW(SCARDCONTEXT hContext, |
3035 | | LPCWSTR szDeviceInstanceId, |
3036 | | LPWSTR mszReaders, |
3037 | | LPDWORD pcchReaders) |
3038 | 0 | { |
3039 | 0 | WINPR_UNUSED(hContext); |
3040 | 0 | WINPR_UNUSED(szDeviceInstanceId); |
3041 | 0 | WINPR_UNUSED(mszReaders); |
3042 | 0 | WINPR_UNUSED(pcchReaders); |
3043 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
3044 | 0 | } |
3045 | | |
3046 | | static LONG WINAPI PCSC_SCardAudit(SCARDCONTEXT hContext, DWORD dwEvent) |
3047 | 0 | { |
3048 | |
|
3049 | 0 | WINPR_UNUSED(hContext); |
3050 | 0 | WINPR_UNUSED(dwEvent); |
3051 | 0 | return SCARD_E_UNSUPPORTED_FEATURE; |
3052 | 0 | } |
3053 | | |
3054 | | #ifdef __MACOSX__ |
3055 | | unsigned int determineMacOSXVersion(void) |
3056 | | { |
3057 | | int mib[2]; |
3058 | | size_t len = 0; |
3059 | | char* kernelVersion = NULL; |
3060 | | char* tok = NULL; |
3061 | | unsigned int version = 0; |
3062 | | long majorVersion = 0; |
3063 | | long minorVersion = 0; |
3064 | | long patchVersion = 0; |
3065 | | int count = 0; |
3066 | | char* context = NULL; |
3067 | | mib[0] = CTL_KERN; |
3068 | | mib[1] = KERN_OSRELEASE; |
3069 | | |
3070 | | if (sysctl(mib, 2, NULL, &len, NULL, 0) != 0) |
3071 | | return 0; |
3072 | | |
3073 | | kernelVersion = calloc(len, sizeof(char)); |
3074 | | |
3075 | | if (!kernelVersion) |
3076 | | return 0; |
3077 | | |
3078 | | if (sysctl(mib, 2, kernelVersion, &len, NULL, 0) != 0) |
3079 | | { |
3080 | | free(kernelVersion); |
3081 | | return 0; |
3082 | | } |
3083 | | |
3084 | | tok = strtok_s(kernelVersion, ".", &context); |
3085 | | errno = 0; |
3086 | | |
3087 | | while (tok) |
3088 | | { |
3089 | | switch (count) |
3090 | | { |
3091 | | case 0: |
3092 | | majorVersion = strtol(tok, NULL, 0); |
3093 | | |
3094 | | if (errno != 0) |
3095 | | goto fail; |
3096 | | |
3097 | | break; |
3098 | | |
3099 | | case 1: |
3100 | | minorVersion = strtol(tok, NULL, 0); |
3101 | | |
3102 | | if (errno != 0) |
3103 | | goto fail; |
3104 | | |
3105 | | break; |
3106 | | |
3107 | | case 2: |
3108 | | patchVersion = strtol(tok, NULL, 0); |
3109 | | |
3110 | | if (errno != 0) |
3111 | | goto fail; |
3112 | | |
3113 | | break; |
3114 | | } |
3115 | | |
3116 | | tok = strtok_s(NULL, ".", &context); |
3117 | | count++; |
3118 | | } |
3119 | | |
3120 | | /** |
3121 | | * Source : http://en.wikipedia.org/wiki/Darwin_(operating_system) |
3122 | | **/ |
3123 | | if (majorVersion < 5) |
3124 | | { |
3125 | | if (minorVersion < 4) |
3126 | | version = 0x10000000; |
3127 | | else |
3128 | | version = 0x10010000; |
3129 | | } |
3130 | | else |
3131 | | { |
3132 | | switch (majorVersion) |
3133 | | { |
3134 | | case 5: |
3135 | | version = 0x10010000; |
3136 | | break; |
3137 | | |
3138 | | case 6: |
3139 | | version = 0x10020000; |
3140 | | break; |
3141 | | |
3142 | | case 7: |
3143 | | version = 0x10030000; |
3144 | | break; |
3145 | | |
3146 | | case 8: |
3147 | | version = 0x10040000; |
3148 | | break; |
3149 | | |
3150 | | case 9: |
3151 | | version = 0x10050000; |
3152 | | break; |
3153 | | |
3154 | | case 10: |
3155 | | version = 0x10060000; |
3156 | | break; |
3157 | | |
3158 | | case 11: |
3159 | | version = 0x10070000; |
3160 | | break; |
3161 | | |
3162 | | case 12: |
3163 | | version = 0x10080000; |
3164 | | break; |
3165 | | |
3166 | | case 13: |
3167 | | version = 0x10090000; |
3168 | | break; |
3169 | | |
3170 | | default: |
3171 | | version = 0x10100000; |
3172 | | break; |
3173 | | } |
3174 | | |
3175 | | version |= (minorVersion << 8) | (patchVersion); |
3176 | | } |
3177 | | |
3178 | | fail: |
3179 | | free(kernelVersion); |
3180 | | return version; |
3181 | | } |
3182 | | #endif |
3183 | | |
3184 | | static const SCardApiFunctionTable PCSC_SCardApiFunctionTable = { |
3185 | | 0, /* dwVersion */ |
3186 | | 0, /* dwFlags */ |
3187 | | |
3188 | | PCSC_SCardEstablishContext, /* SCardEstablishContext */ |
3189 | | PCSC_SCardReleaseContext, /* SCardReleaseContext */ |
3190 | | PCSC_SCardIsValidContext, /* SCardIsValidContext */ |
3191 | | PCSC_SCardListReaderGroupsA, /* SCardListReaderGroupsA */ |
3192 | | PCSC_SCardListReaderGroupsW, /* SCardListReaderGroupsW */ |
3193 | | PCSC_SCardListReadersA, /* SCardListReadersA */ |
3194 | | PCSC_SCardListReadersW, /* SCardListReadersW */ |
3195 | | PCSC_SCardListCardsA, /* SCardListCardsA */ |
3196 | | PCSC_SCardListCardsW, /* SCardListCardsW */ |
3197 | | PCSC_SCardListInterfacesA, /* SCardListInterfacesA */ |
3198 | | PCSC_SCardListInterfacesW, /* SCardListInterfacesW */ |
3199 | | PCSC_SCardGetProviderIdA, /* SCardGetProviderIdA */ |
3200 | | PCSC_SCardGetProviderIdW, /* SCardGetProviderIdW */ |
3201 | | PCSC_SCardGetCardTypeProviderNameA, /* SCardGetCardTypeProviderNameA */ |
3202 | | PCSC_SCardGetCardTypeProviderNameW, /* SCardGetCardTypeProviderNameW */ |
3203 | | PCSC_SCardIntroduceReaderGroupA, /* SCardIntroduceReaderGroupA */ |
3204 | | PCSC_SCardIntroduceReaderGroupW, /* SCardIntroduceReaderGroupW */ |
3205 | | PCSC_SCardForgetReaderGroupA, /* SCardForgetReaderGroupA */ |
3206 | | PCSC_SCardForgetReaderGroupW, /* SCardForgetReaderGroupW */ |
3207 | | PCSC_SCardIntroduceReaderA, /* SCardIntroduceReaderA */ |
3208 | | PCSC_SCardIntroduceReaderW, /* SCardIntroduceReaderW */ |
3209 | | PCSC_SCardForgetReaderA, /* SCardForgetReaderA */ |
3210 | | PCSC_SCardForgetReaderW, /* SCardForgetReaderW */ |
3211 | | PCSC_SCardAddReaderToGroupA, /* SCardAddReaderToGroupA */ |
3212 | | PCSC_SCardAddReaderToGroupW, /* SCardAddReaderToGroupW */ |
3213 | | PCSC_SCardRemoveReaderFromGroupA, /* SCardRemoveReaderFromGroupA */ |
3214 | | PCSC_SCardRemoveReaderFromGroupW, /* SCardRemoveReaderFromGroupW */ |
3215 | | PCSC_SCardIntroduceCardTypeA, /* SCardIntroduceCardTypeA */ |
3216 | | PCSC_SCardIntroduceCardTypeW, /* SCardIntroduceCardTypeW */ |
3217 | | PCSC_SCardSetCardTypeProviderNameA, /* SCardSetCardTypeProviderNameA */ |
3218 | | PCSC_SCardSetCardTypeProviderNameW, /* SCardSetCardTypeProviderNameW */ |
3219 | | PCSC_SCardForgetCardTypeA, /* SCardForgetCardTypeA */ |
3220 | | PCSC_SCardForgetCardTypeW, /* SCardForgetCardTypeW */ |
3221 | | PCSC_SCardFreeMemory, /* SCardFreeMemory */ |
3222 | | PCSC_SCardAccessStartedEvent, /* SCardAccessStartedEvent */ |
3223 | | PCSC_SCardReleaseStartedEvent, /* SCardReleaseStartedEvent */ |
3224 | | PCSC_SCardLocateCardsA, /* SCardLocateCardsA */ |
3225 | | PCSC_SCardLocateCardsW, /* SCardLocateCardsW */ |
3226 | | PCSC_SCardLocateCardsByATRA, /* SCardLocateCardsByATRA */ |
3227 | | PCSC_SCardLocateCardsByATRW, /* SCardLocateCardsByATRW */ |
3228 | | PCSC_SCardGetStatusChangeA, /* SCardGetStatusChangeA */ |
3229 | | PCSC_SCardGetStatusChangeW, /* SCardGetStatusChangeW */ |
3230 | | PCSC_SCardCancel, /* SCardCancel */ |
3231 | | PCSC_SCardConnectA, /* SCardConnectA */ |
3232 | | PCSC_SCardConnectW, /* SCardConnectW */ |
3233 | | PCSC_SCardReconnect, /* SCardReconnect */ |
3234 | | PCSC_SCardDisconnect, /* SCardDisconnect */ |
3235 | | PCSC_SCardBeginTransaction, /* SCardBeginTransaction */ |
3236 | | PCSC_SCardEndTransaction, /* SCardEndTransaction */ |
3237 | | PCSC_SCardCancelTransaction, /* SCardCancelTransaction */ |
3238 | | PCSC_SCardState, /* SCardState */ |
3239 | | PCSC_SCardStatusA, /* SCardStatusA */ |
3240 | | PCSC_SCardStatusW, /* SCardStatusW */ |
3241 | | PCSC_SCardTransmit, /* SCardTransmit */ |
3242 | | PCSC_SCardGetTransmitCount, /* SCardGetTransmitCount */ |
3243 | | PCSC_SCardControl, /* SCardControl */ |
3244 | | PCSC_SCardGetAttrib, /* SCardGetAttrib */ |
3245 | | PCSC_SCardSetAttrib, /* SCardSetAttrib */ |
3246 | | PCSC_SCardUIDlgSelectCardA, /* SCardUIDlgSelectCardA */ |
3247 | | PCSC_SCardUIDlgSelectCardW, /* SCardUIDlgSelectCardW */ |
3248 | | PCSC_GetOpenCardNameA, /* GetOpenCardNameA */ |
3249 | | PCSC_GetOpenCardNameW, /* GetOpenCardNameW */ |
3250 | | PCSC_SCardDlgExtendedError, /* SCardDlgExtendedError */ |
3251 | | PCSC_SCardReadCacheA, /* SCardReadCacheA */ |
3252 | | PCSC_SCardReadCacheW, /* SCardReadCacheW */ |
3253 | | PCSC_SCardWriteCacheA, /* SCardWriteCacheA */ |
3254 | | PCSC_SCardWriteCacheW, /* SCardWriteCacheW */ |
3255 | | PCSC_SCardGetReaderIconA, /* SCardGetReaderIconA */ |
3256 | | PCSC_SCardGetReaderIconW, /* SCardGetReaderIconW */ |
3257 | | PCSC_SCardGetDeviceTypeIdA, /* SCardGetDeviceTypeIdA */ |
3258 | | PCSC_SCardGetDeviceTypeIdW, /* SCardGetDeviceTypeIdW */ |
3259 | | PCSC_SCardGetReaderDeviceInstanceIdA, /* SCardGetReaderDeviceInstanceIdA */ |
3260 | | PCSC_SCardGetReaderDeviceInstanceIdW, /* SCardGetReaderDeviceInstanceIdW */ |
3261 | | PCSC_SCardListReadersWithDeviceInstanceIdA, /* SCardListReadersWithDeviceInstanceIdA */ |
3262 | | PCSC_SCardListReadersWithDeviceInstanceIdW, /* SCardListReadersWithDeviceInstanceIdW */ |
3263 | | PCSC_SCardAudit /* SCardAudit */ |
3264 | | }; |
3265 | | |
3266 | | const SCardApiFunctionTable* PCSC_GetSCardApiFunctionTable(void) |
3267 | 0 | { |
3268 | 0 | return &PCSC_SCardApiFunctionTable; |
3269 | 0 | } |
3270 | | |
3271 | | int PCSC_InitializeSCardApi(void) |
3272 | 0 | { |
3273 | | /* Disable pcsc-lite's (poor) blocking so we can handle it ourselves */ |
3274 | 0 | SetEnvironmentVariableA("PCSCLITE_NO_BLOCKING", "1"); |
3275 | | #ifdef __MACOSX__ |
3276 | | g_PCSCModule = LoadLibraryX("/System/Library/Frameworks/PCSC.framework/PCSC"); |
3277 | | OSXVersion = determineMacOSXVersion(); |
3278 | | |
3279 | | if (OSXVersion == 0) |
3280 | | return -1; |
3281 | | |
3282 | | #else |
3283 | 0 | g_PCSCModule = LoadLibraryA("libpcsclite.so.1"); |
3284 | |
|
3285 | 0 | if (!g_PCSCModule) |
3286 | 0 | g_PCSCModule = LoadLibraryA("libpcsclite.so"); |
3287 | |
|
3288 | 0 | #endif |
3289 | |
|
3290 | 0 | if (!g_PCSCModule) |
3291 | 0 | return -1; |
3292 | | |
3293 | | /* symbols defined in winpr/smartcard.h, might pose an issue with the GetProcAddress macro |
3294 | | * below. therefore undefine them here */ |
3295 | 0 | #undef SCardListReaderGroups |
3296 | 0 | #undef SCardListReaders |
3297 | 0 | #undef SCardListCards |
3298 | 0 | #undef SCardListInterfaces |
3299 | 0 | #undef SCardGetProviderId |
3300 | 0 | #undef SCardGetCardTypeProviderName |
3301 | 0 | #undef SCardIntroduceReaderGroup |
3302 | 0 | #undef SCardForgetReaderGroup |
3303 | 0 | #undef SCardIntroduceReader |
3304 | 0 | #undef SCardForgetReader |
3305 | 0 | #undef SCardAddReaderToGroup |
3306 | 0 | #undef SCardRemoveReaderFromGroup |
3307 | 0 | #undef SCardIntroduceCardType |
3308 | 0 | #undef SCardSetCardTypeProviderName |
3309 | 0 | #undef SCardForgetCardType |
3310 | 0 | #undef SCardLocateCards |
3311 | 0 | #undef SCardLocateCardsByATR |
3312 | 0 | #undef SCardGetStatusChange |
3313 | 0 | #undef SCardConnect |
3314 | 0 | #undef SCardStatus |
3315 | 0 | #undef SCardUIDlgSelectCard |
3316 | 0 | #undef GetOpenCardName |
3317 | 0 | #undef SCardReadCache |
3318 | 0 | #undef SCardWriteCache |
3319 | 0 | #undef SCardGetReaderIcon |
3320 | 0 | #undef SCardGetDeviceTypeId |
3321 | 0 | #undef SCardGetReaderDeviceInstanceId |
3322 | 0 | #undef SCardListReadersWithDeviceInstanceId |
3323 | | |
3324 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardEstablishContext); |
3325 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardReleaseContext); |
3326 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardIsValidContext); |
3327 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardConnect); |
3328 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardReconnect); |
3329 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardDisconnect); |
3330 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardBeginTransaction); |
3331 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardEndTransaction); |
3332 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardStatus); |
3333 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardGetStatusChange); |
3334 | |
|
3335 | | #ifdef __MACOSX__ |
3336 | | |
3337 | | if (OSXVersion >= 0x10050600) |
3338 | | { |
3339 | | WINSCARD_LOAD_PROC_EX(g_PCSCModule, g_PCSC, SCardControl, SCardControl132); |
3340 | | } |
3341 | | else |
3342 | | { |
3343 | | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardControl); |
3344 | | } |
3345 | | #else |
3346 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardControl); |
3347 | 0 | #endif |
3348 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardTransmit); |
3349 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardListReaderGroups); |
3350 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardListReaders); |
3351 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardCancel); |
3352 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardGetAttrib); |
3353 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardSetAttrib); |
3354 | 0 | g_PCSC.pfnSCardFreeMemory = NULL; |
3355 | 0 | #ifndef __APPLE__ |
3356 | 0 | WINSCARD_LOAD_PROC(g_PCSCModule, g_PCSC, SCardFreeMemory); |
3357 | 0 | #endif |
3358 | |
|
3359 | 0 | if (g_PCSC.pfnSCardFreeMemory) |
3360 | 0 | g_SCardAutoAllocate = TRUE; |
3361 | |
|
3362 | | #ifdef DISABLE_PCSC_SCARD_AUTOALLOCATE |
3363 | | g_PCSC.pfnSCardFreeMemory = NULL; |
3364 | | g_SCardAutoAllocate = FALSE; |
3365 | | #endif |
3366 | | #ifdef __APPLE__ |
3367 | | g_PnP_Notification = FALSE; |
3368 | | #endif |
3369 | 0 | return 1; |
3370 | 0 | } |
3371 | | |
3372 | | #endif |