2014-04-04 23:30:03 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Smart Card API
|
|
|
|
*
|
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2020-02-17 16:07:20 +03:00
|
|
|
* Copyright 2020 Armin Novak <armin.novak@thincast.com>
|
|
|
|
* Copyright 2020 Thincast Technologies GmbH
|
2014-04-04 23:30:03 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-04-26 02:49:57 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2014-12-13 16:27:57 +03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <string.h>
|
2017-12-11 17:49:03 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2014-12-13 16:27:57 +03:00
|
|
|
#endif
|
|
|
|
|
2014-04-13 07:38:15 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-04-04 23:30:03 +04:00
|
|
|
#include <winpr/crt.h>
|
2014-04-09 23:32:18 +04:00
|
|
|
#include <winpr/synch.h>
|
2014-04-04 23:30:03 +04:00
|
|
|
#include <winpr/library.h>
|
|
|
|
#include <winpr/smartcard.h>
|
2014-04-07 08:10:27 +04:00
|
|
|
#include <winpr/collections.h>
|
2014-05-11 13:23:30 +04:00
|
|
|
#include <winpr/environment.h>
|
2014-04-04 23:30:03 +04:00
|
|
|
|
|
|
|
#include "smartcard_pcsc.h"
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("smartcard")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-12 18:17:49 +04:00
|
|
|
/**
|
|
|
|
* PC/SC transactions:
|
|
|
|
* http://developersblog.wwpass.com/?p=180
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Smart Card Logon on Windows Vista:
|
|
|
|
* http://blogs.msdn.com/b/shivaram/archive/2007/02/26/smart-card-logon-on-windows-vista.aspx
|
|
|
|
*/
|
|
|
|
|
2014-05-11 13:23:30 +04:00
|
|
|
/**
|
|
|
|
* The Smart Card Cryptographic Service Provider Cookbook:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms953432.aspx
|
|
|
|
*
|
|
|
|
* SCARDCONTEXT
|
|
|
|
*
|
|
|
|
* The context is a communication channel with the smart card resource manager and
|
|
|
|
* all calls to the resource manager must go through this link.
|
|
|
|
*
|
|
|
|
* All functions that take a context as a parameter or a card handle as parameter,
|
|
|
|
* which is indirectly associated with a particular context, may be blocking calls.
|
|
|
|
* Examples of these are SCardGetStatusChange and SCardBeginTransaction, which takes
|
|
|
|
* a card handle as a parameter. If such a function blocks then all operations wanting
|
|
|
|
* to use the context are blocked as well. So, it is recommended that a CSP using
|
|
|
|
* monitoring establishes at least two contexts with the resource manager; one for
|
|
|
|
* monitoring (with SCardGetStatusChange) and one for other operations.
|
|
|
|
*
|
|
|
|
* If multiple cards are present, it is recommended that a separate context or pair
|
|
|
|
* of contexts be established for each card to prevent operations on one card from
|
|
|
|
* blocking operations on another.
|
|
|
|
*
|
|
|
|
* Example one
|
|
|
|
*
|
|
|
|
* The example below shows what can happen if a CSP using SCardGetStatusChange for
|
|
|
|
* monitoring does not establish two contexts with the resource manager.
|
|
|
|
* The context becomes unusable until SCardGetStatusChange unblocks.
|
|
|
|
*
|
|
|
|
* In this example, there is one process running called P1.
|
|
|
|
* P1 calls SCardEstablishContext, which returns the context hCtx.
|
|
|
|
* P1 calls SCardConnect (with the hCtx context) which returns a handle to the card, hCard.
|
|
|
|
* P1 calls SCardGetStatusChange (with the hCtx context) which blocks because
|
|
|
|
* there are no status changes to report.
|
|
|
|
* Until the thread running SCardGetStatusChange unblocks, another thread in P1 trying to
|
|
|
|
* perform an operation using the context hCtx (or the card hCard) will also be blocked.
|
|
|
|
*
|
|
|
|
* Example two
|
|
|
|
*
|
|
|
|
* The example below shows how transaction control ensures that operations meant to be
|
|
|
|
* performed without interruption can do so safely within a transaction.
|
|
|
|
*
|
|
|
|
* In this example, there are two different processes running; P1 and P2.
|
|
|
|
* P1 calls SCardEstablishContext, which returns the context hCtx1.
|
|
|
|
* P2 calls SCardEstablishContext, which returns the context hCtx2.
|
|
|
|
* P1 calls SCardConnect (with the hCtx1 context) which returns a handle to the card, hCard1.
|
|
|
|
* P2 calls SCardConnect (with the hCtx2 context) which returns a handle to the same card, hCard2.
|
|
|
|
* P1 calls SCardBeginTransaction (with the hCard 1 context).
|
|
|
|
* Until P1 calls SCardEndTransaction (with the hCard1 context),
|
|
|
|
* any operation using hCard2 will be blocked.
|
|
|
|
* Once an operation using hCard2 is blocked and until it's returning,
|
|
|
|
* any operation using hCtx2 (and hCard2) will also be blocked.
|
|
|
|
*/
|
|
|
|
|
2014-04-09 05:02:30 +04:00
|
|
|
//#define DISABLE_PCSC_SCARD_AUTOALLOCATE
|
2020-02-17 12:53:02 +03:00
|
|
|
#include "smartcard_pcsc.h"
|
|
|
|
|
|
|
|
#define PCSC_SCARD_PCI_T0 (&g_PCSC_rgSCardT0Pci)
|
|
|
|
#define PCSC_SCARD_PCI_T1 (&g_PCSC_rgSCardT1Pci)
|
|
|
|
#define PCSC_SCARD_PCI_RAW (&g_PCSC_rgSCardRawPci)
|
|
|
|
|
2020-02-17 13:34:09 +03:00
|
|
|
typedef PCSC_LONG (*fnPCSCSCardEstablishContext)(PCSC_DWORD dwScope, LPCVOID pvReserved1,
|
|
|
|
LPCVOID pvReserved2, LPSCARDCONTEXT phContext);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardReleaseContext)(SCARDCONTEXT hContext);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardIsValidContext)(SCARDCONTEXT hContext);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardConnect)(SCARDCONTEXT hContext, LPCSTR szReader,
|
|
|
|
PCSC_DWORD dwShareMode, PCSC_DWORD dwPreferredProtocols,
|
|
|
|
LPSCARDHANDLE phCard, PCSC_LPDWORD pdwActiveProtocol);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardReconnect)(SCARDHANDLE hCard, PCSC_DWORD dwShareMode,
|
|
|
|
PCSC_DWORD dwPreferredProtocols,
|
|
|
|
PCSC_DWORD dwInitialization,
|
|
|
|
PCSC_LPDWORD pdwActiveProtocol);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardDisconnect)(SCARDHANDLE hCard, PCSC_DWORD dwDisposition);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardBeginTransaction)(SCARDHANDLE hCard);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardEndTransaction)(SCARDHANDLE hCard, PCSC_DWORD dwDisposition);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardStatus)(SCARDHANDLE hCard, LPSTR mszReaderName,
|
|
|
|
PCSC_LPDWORD pcchReaderLen, PCSC_LPDWORD pdwState,
|
|
|
|
PCSC_LPDWORD pdwProtocol, LPBYTE pbAtr,
|
|
|
|
PCSC_LPDWORD pcbAtrLen);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardGetStatusChange)(SCARDCONTEXT hContext, PCSC_DWORD dwTimeout,
|
|
|
|
PCSC_SCARD_READERSTATE* rgReaderStates,
|
|
|
|
PCSC_DWORD cReaders);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardControl)(SCARDHANDLE hCard, PCSC_DWORD dwControlCode,
|
|
|
|
LPCVOID pbSendBuffer, PCSC_DWORD cbSendLength,
|
|
|
|
LPVOID pbRecvBuffer, PCSC_DWORD cbRecvLength,
|
|
|
|
PCSC_LPDWORD lpBytesReturned);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardTransmit)(SCARDHANDLE hCard, const PCSC_SCARD_IO_REQUEST* pioSendPci,
|
|
|
|
LPCBYTE pbSendBuffer, PCSC_DWORD cbSendLength,
|
|
|
|
PCSC_SCARD_IO_REQUEST* pioRecvPci, LPBYTE pbRecvBuffer,
|
|
|
|
PCSC_LPDWORD pcbRecvLength);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardListReaderGroups)(SCARDCONTEXT hContext, LPSTR mszGroups,
|
|
|
|
PCSC_LPDWORD pcchGroups);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardListReaders)(SCARDCONTEXT hContext, LPCSTR mszGroups,
|
|
|
|
LPSTR mszReaders, PCSC_LPDWORD pcchReaders);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardFreeMemory)(SCARDCONTEXT hContext, LPCVOID pvMem);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardCancel)(SCARDCONTEXT hContext);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardGetAttrib)(SCARDHANDLE hCard, PCSC_DWORD dwAttrId, LPBYTE pbAttr,
|
|
|
|
PCSC_LPDWORD pcbAttrLen);
|
|
|
|
typedef PCSC_LONG (*fnPCSCSCardSetAttrib)(SCARDHANDLE hCard, PCSC_DWORD dwAttrId, LPCBYTE pbAttr,
|
|
|
|
PCSC_DWORD cbAttrLen);
|
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
struct _PCSCFunctionTable
|
|
|
|
{
|
2020-02-17 13:34:09 +03:00
|
|
|
fnPCSCSCardEstablishContext pfnSCardEstablishContext;
|
|
|
|
fnPCSCSCardReleaseContext pfnSCardReleaseContext;
|
|
|
|
fnPCSCSCardIsValidContext pfnSCardIsValidContext;
|
|
|
|
fnPCSCSCardConnect pfnSCardConnect;
|
|
|
|
fnPCSCSCardReconnect pfnSCardReconnect;
|
|
|
|
fnPCSCSCardDisconnect pfnSCardDisconnect;
|
|
|
|
fnPCSCSCardBeginTransaction pfnSCardBeginTransaction;
|
|
|
|
fnPCSCSCardEndTransaction pfnSCardEndTransaction;
|
|
|
|
fnPCSCSCardStatus pfnSCardStatus;
|
|
|
|
fnPCSCSCardGetStatusChange pfnSCardGetStatusChange;
|
|
|
|
fnPCSCSCardControl pfnSCardControl;
|
|
|
|
fnPCSCSCardTransmit pfnSCardTransmit;
|
|
|
|
fnPCSCSCardListReaderGroups pfnSCardListReaderGroups;
|
|
|
|
fnPCSCSCardListReaders pfnSCardListReaders;
|
|
|
|
fnPCSCSCardFreeMemory pfnSCardFreeMemory;
|
|
|
|
fnPCSCSCardCancel pfnSCardCancel;
|
|
|
|
fnPCSCSCardGetAttrib pfnSCardGetAttrib;
|
|
|
|
fnPCSCSCardSetAttrib pfnSCardSetAttrib;
|
2020-02-17 12:53:02 +03:00
|
|
|
};
|
|
|
|
typedef struct _PCSCFunctionTable PCSCFunctionTable;
|
2014-04-09 02:55:50 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
typedef struct _PCSC_SCARDCONTEXT PCSC_SCARDCONTEXT;
|
|
|
|
typedef struct _PCSC_SCARDHANDLE PCSC_SCARDHANDLE;
|
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
DWORD len;
|
2020-04-20 14:42:02 +03:00
|
|
|
DWORD freshness;
|
2020-02-17 12:53:02 +03:00
|
|
|
BYTE* data;
|
|
|
|
} PCSC_CACHE_ITEM;
|
|
|
|
|
2014-04-09 23:32:18 +04:00
|
|
|
struct _PCSC_SCARDCONTEXT
|
|
|
|
{
|
2015-01-21 05:40:45 +03:00
|
|
|
SCARDHANDLE owner;
|
2014-04-09 23:32:18 +04:00
|
|
|
CRITICAL_SECTION lock;
|
2014-06-03 01:20:01 +04:00
|
|
|
SCARDCONTEXT hContext;
|
|
|
|
DWORD dwCardHandleCount;
|
2014-06-03 05:49:49 +04:00
|
|
|
BOOL isTransactionLocked;
|
2020-02-17 12:53:02 +03:00
|
|
|
wHashTable* cache;
|
2014-04-09 23:32:18 +04:00
|
|
|
};
|
|
|
|
|
2014-05-11 13:23:30 +04:00
|
|
|
struct _PCSC_SCARDHANDLE
|
|
|
|
{
|
2015-01-21 05:40:45 +03:00
|
|
|
BOOL shared;
|
2014-06-03 01:20:01 +04:00
|
|
|
SCARDCONTEXT hSharedContext;
|
2014-05-11 13:23:30 +04:00
|
|
|
};
|
|
|
|
|
2014-04-04 23:30:03 +04:00
|
|
|
static HMODULE g_PCSCModule = NULL;
|
|
|
|
static PCSCFunctionTable g_PCSC = { 0 };
|
|
|
|
|
2014-05-11 07:43:25 +04:00
|
|
|
static HANDLE g_StartedEvent = NULL;
|
|
|
|
static int g_StartedEventRefCount = 0;
|
|
|
|
|
2014-04-09 05:02:30 +04:00
|
|
|
static BOOL g_SCardAutoAllocate = FALSE;
|
2014-04-28 00:43:09 +04:00
|
|
|
static BOOL g_PnP_Notification = TRUE;
|
2015-01-21 05:40:45 +03:00
|
|
|
|
2014-12-14 17:15:33 +03:00
|
|
|
#ifdef __MACOSX__
|
2014-12-13 16:27:57 +03:00
|
|
|
static unsigned int OSXVersion = 0;
|
2014-12-14 17:15:33 +03:00
|
|
|
#endif
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static wListDictionary* g_CardHandles = NULL;
|
|
|
|
static wListDictionary* g_CardContexts = NULL;
|
|
|
|
static wListDictionary* g_MemoryBlocks = NULL;
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
static const char SMARTCARD_PNP_NOTIFICATION_A[] = "\\\\?PnP?\\Notification";
|
2014-04-28 00:43:09 +04:00
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
static const PCSC_SCARD_IO_REQUEST g_PCSC_rgSCardT0Pci = { SCARD_PROTOCOL_T0,
|
|
|
|
sizeof(PCSC_SCARD_IO_REQUEST) };
|
|
|
|
static const PCSC_SCARD_IO_REQUEST g_PCSC_rgSCardT1Pci = { SCARD_PROTOCOL_T1,
|
|
|
|
sizeof(PCSC_SCARD_IO_REQUEST) };
|
|
|
|
static const PCSC_SCARD_IO_REQUEST g_PCSC_rgSCardRawPci = { PCSC_SCARD_PROTOCOL_RAW,
|
|
|
|
sizeof(PCSC_SCARD_IO_REQUEST) };
|
2014-05-03 22:20:17 +04:00
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
static LONG WINAPI PCSC_SCardFreeMemory_Internal(SCARDCONTEXT hContext, LPVOID pvMem);
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardEstablishContext_Internal(DWORD dwScope, LPCVOID pvReserved1,
|
|
|
|
LPCVOID pvReserved2,
|
|
|
|
LPSCARDCONTEXT phContext);
|
|
|
|
static LONG WINAPI PCSC_SCardReleaseContext_Internal(SCARDCONTEXT hContext);
|
2014-04-09 07:52:24 +04:00
|
|
|
|
2020-01-09 10:20:42 +03:00
|
|
|
static LONG PCSC_SCard_LogError(const char* what)
|
|
|
|
{
|
2020-02-17 12:53:02 +03:00
|
|
|
WLog_WARN(TAG, "Missing function pointer %s=NULL", what);
|
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2020-01-09 10:20:42 +03:00
|
|
|
}
|
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
static LONG PCSC_MapErrorCodeToWinSCard(PCSC_LONG errorCode)
|
2014-04-06 00:06:46 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* pcsc-lite returns SCARD_E_UNEXPECTED when it
|
|
|
|
* should return SCARD_E_UNSUPPORTED_FEATURE.
|
|
|
|
*
|
|
|
|
* Additionally, the pcsc-lite headers incorrectly
|
|
|
|
* define SCARD_E_UNSUPPORTED_FEATURE to 0x8010001F,
|
|
|
|
* when the real value should be 0x80100022.
|
|
|
|
*/
|
2020-02-17 12:53:02 +03:00
|
|
|
if (errorCode != SCARD_S_SUCCESS)
|
|
|
|
{
|
|
|
|
if (errorCode == SCARD_E_UNEXPECTED)
|
|
|
|
errorCode = SCARD_E_UNSUPPORTED_FEATURE;
|
|
|
|
}
|
2020-02-17 16:07:20 +03:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
return (LONG)errorCode;
|
2014-04-06 00:06:46 +04:00
|
|
|
}
|
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
static DWORD PCSC_ConvertCardStateToWinSCard(DWORD dwCardState, PCSC_LONG status)
|
2014-04-10 01:44:39 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* pcsc-lite's SCardStatus returns a bit-field, not an enumerated value.
|
|
|
|
*
|
|
|
|
* State WinSCard pcsc-lite
|
|
|
|
*
|
|
|
|
* SCARD_UNKNOWN 0 0x0001
|
|
|
|
* SCARD_ABSENT 1 0x0002
|
|
|
|
* SCARD_PRESENT 2 0x0004
|
|
|
|
* SCARD_SWALLOWED 3 0x0008
|
|
|
|
* SCARD_POWERED 4 0x0010
|
|
|
|
* SCARD_NEGOTIABLE 5 0x0020
|
|
|
|
* SCARD_SPECIFIC 6 0x0040
|
2014-04-13 00:45:06 +04:00
|
|
|
*
|
|
|
|
* pcsc-lite also never sets SCARD_SPECIFIC,
|
|
|
|
* which is expected by some windows applications.
|
2014-04-10 01:44:39 +04:00
|
|
|
*/
|
2014-04-13 00:45:06 +04:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
|
|
|
if ((dwCardState & PCSC_SCARD_NEGOTIABLE) || (dwCardState & PCSC_SCARD_SPECIFIC))
|
|
|
|
return SCARD_SPECIFIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dwCardState & PCSC_SCARD_POWERED)
|
2014-04-10 01:44:39 +04:00
|
|
|
return SCARD_POWERED;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-04-13 00:45:06 +04:00
|
|
|
if (dwCardState & PCSC_SCARD_NEGOTIABLE)
|
2014-04-10 01:44:39 +04:00
|
|
|
return SCARD_NEGOTIABLE;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-04-13 00:45:06 +04:00
|
|
|
if (dwCardState & PCSC_SCARD_SPECIFIC)
|
2014-04-10 01:44:39 +04:00
|
|
|
return SCARD_SPECIFIC;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-04-13 00:45:06 +04:00
|
|
|
if (dwCardState & PCSC_SCARD_ABSENT)
|
|
|
|
return SCARD_ABSENT;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-04-13 00:45:06 +04:00
|
|
|
if (dwCardState & PCSC_SCARD_PRESENT)
|
|
|
|
return SCARD_PRESENT;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-04-13 00:45:06 +04:00
|
|
|
if (dwCardState & PCSC_SCARD_SWALLOWED)
|
|
|
|
return SCARD_SWALLOWED;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-04-13 00:45:06 +04:00
|
|
|
if (dwCardState & PCSC_SCARD_UNKNOWN)
|
|
|
|
return SCARD_UNKNOWN;
|
2014-04-10 01:44:39 +04:00
|
|
|
|
|
|
|
return SCARD_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
static DWORD PCSC_ConvertProtocolsToWinSCard(PCSC_DWORD dwProtocols)
|
2014-04-13 02:13:18 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* pcsc-lite uses a different value for SCARD_PROTOCOL_RAW,
|
|
|
|
* and also has SCARD_PROTOCOL_T15 which is not in WinSCard.
|
|
|
|
*/
|
|
|
|
if (dwProtocols & PCSC_SCARD_PROTOCOL_RAW)
|
|
|
|
{
|
|
|
|
dwProtocols &= ~PCSC_SCARD_PROTOCOL_RAW;
|
|
|
|
dwProtocols |= SCARD_PROTOCOL_RAW;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dwProtocols & PCSC_SCARD_PROTOCOL_T15)
|
|
|
|
{
|
|
|
|
dwProtocols &= ~PCSC_SCARD_PROTOCOL_T15;
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
return (DWORD)dwProtocols;
|
2014-04-13 02:13:18 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static DWORD PCSC_ConvertProtocolsFromWinSCard(DWORD dwProtocols)
|
2014-04-13 02:13:18 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* pcsc-lite uses a different value for SCARD_PROTOCOL_RAW,
|
|
|
|
* and it does not define WinSCard's SCARD_PROTOCOL_DEFAULT.
|
|
|
|
*/
|
|
|
|
if (dwProtocols & SCARD_PROTOCOL_RAW)
|
|
|
|
{
|
|
|
|
dwProtocols &= ~SCARD_PROTOCOL_RAW;
|
|
|
|
dwProtocols |= PCSC_SCARD_PROTOCOL_RAW;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dwProtocols & SCARD_PROTOCOL_DEFAULT)
|
|
|
|
{
|
|
|
|
dwProtocols &= ~SCARD_PROTOCOL_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2014-04-16 01:51:39 +04:00
|
|
|
if (dwProtocols == SCARD_PROTOCOL_UNDEFINED)
|
|
|
|
{
|
|
|
|
dwProtocols = SCARD_PROTOCOL_Tx;
|
|
|
|
}
|
|
|
|
|
2014-04-13 02:13:18 +04:00
|
|
|
return dwProtocols;
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static PCSC_SCARDCONTEXT* PCSC_GetCardContextData(SCARDCONTEXT hContext)
|
2014-04-09 23:32:18 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDCONTEXT* pContext;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
|
|
|
if (!g_CardContexts)
|
2014-05-11 13:23:30 +04:00
|
|
|
return NULL;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pContext = (PCSC_SCARDCONTEXT*)ListDictionary_GetItemValue(g_CardContexts, (void*)hContext);
|
2014-04-09 23:32:18 +04:00
|
|
|
|
|
|
|
if (!pContext)
|
2014-05-11 13:23:30 +04:00
|
|
|
return NULL;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
|
|
|
return pContext;
|
|
|
|
}
|
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
static void pcsc_cache_item_free(void* ptr)
|
|
|
|
{
|
|
|
|
PCSC_CACHE_ITEM* data = ptr;
|
|
|
|
if (data)
|
|
|
|
free(data->data);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static PCSC_SCARDCONTEXT* PCSC_EstablishCardContext(SCARDCONTEXT hContext)
|
2014-04-09 23:32:18 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDCONTEXT* pContext;
|
2019-11-06 17:24:51 +03:00
|
|
|
pContext = (PCSC_SCARDCONTEXT*)calloc(1, sizeof(PCSC_SCARDCONTEXT));
|
2014-04-09 23:32:18 +04:00
|
|
|
|
|
|
|
if (!pContext)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pContext->hContext = hContext;
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&(pContext->lock), 4000))
|
|
|
|
goto error_spinlock;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
pContext->cache = HashTable_New(FALSE);
|
|
|
|
if (!pContext->cache)
|
|
|
|
goto errors;
|
|
|
|
|
2020-04-20 14:42:02 +03:00
|
|
|
pContext->cache->hash = HashTable_StringHash;
|
|
|
|
pContext->cache->keyCompare = HashTable_StringCompare;
|
|
|
|
pContext->cache->keyClone = HashTable_StringClone;
|
2020-02-17 12:53:02 +03:00
|
|
|
pContext->cache->keyFree = free;
|
|
|
|
pContext->cache->valueFree = pcsc_cache_item_free;
|
2014-04-09 23:32:18 +04:00
|
|
|
if (!g_CardContexts)
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
2014-04-09 23:32:18 +04:00
|
|
|
g_CardContexts = ListDictionary_New(TRUE);
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!g_CardContexts)
|
|
|
|
goto errors;
|
|
|
|
}
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ListDictionary_Add(g_CardContexts, (void*)hContext, (void*)pContext))
|
2015-05-18 12:28:00 +03:00
|
|
|
goto errors;
|
|
|
|
|
2015-06-30 18:42:01 +03:00
|
|
|
return pContext;
|
2015-05-18 12:28:00 +03:00
|
|
|
errors:
|
|
|
|
DeleteCriticalSection(&(pContext->lock));
|
|
|
|
error_spinlock:
|
|
|
|
free(pContext);
|
|
|
|
return NULL;
|
2014-04-09 23:32:18 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static void PCSC_ReleaseCardContext(SCARDCONTEXT hContext)
|
2014-04-09 23:32:18 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDCONTEXT* pContext;
|
2014-04-09 23:32:18 +04:00
|
|
|
pContext = PCSC_GetCardContextData(hContext);
|
|
|
|
|
|
|
|
if (!pContext)
|
2014-06-03 01:20:01 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "PCSC_ReleaseCardContext: null pContext!");
|
2014-04-09 23:32:18 +04:00
|
|
|
return;
|
2014-06-03 01:20:01 +04:00
|
|
|
}
|
2014-04-09 23:32:18 +04:00
|
|
|
|
|
|
|
DeleteCriticalSection(&(pContext->lock));
|
2020-02-17 12:53:02 +03:00
|
|
|
HashTable_Free(pContext->cache);
|
2014-04-09 23:32:18 +04:00
|
|
|
free(pContext);
|
|
|
|
|
|
|
|
if (!g_CardContexts)
|
|
|
|
return;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
ListDictionary_Remove(g_CardContexts, (void*)hContext);
|
2014-04-09 23:32:18 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static BOOL PCSC_LockCardContext(SCARDCONTEXT hContext)
|
2014-04-09 23:32:18 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDCONTEXT* pContext;
|
2014-04-09 23:32:18 +04:00
|
|
|
pContext = PCSC_GetCardContextData(hContext);
|
|
|
|
|
|
|
|
if (!pContext)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "PCSC_LockCardContext: invalid context (%p)", (void*)hContext);
|
2014-04-16 22:03:22 +04:00
|
|
|
return FALSE;
|
2014-04-09 23:32:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
EnterCriticalSection(&(pContext->lock));
|
2014-04-16 22:03:22 +04:00
|
|
|
return TRUE;
|
2014-04-09 23:32:18 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static BOOL PCSC_UnlockCardContext(SCARDCONTEXT hContext)
|
2014-04-07 08:58:34 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDCONTEXT* pContext;
|
2014-04-09 23:32:18 +04:00
|
|
|
pContext = PCSC_GetCardContextData(hContext);
|
2014-04-07 08:58:34 +04:00
|
|
|
|
2014-04-09 23:32:18 +04:00
|
|
|
if (!pContext)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "PCSC_UnlockCardContext: invalid context (%p)", (void*)hContext);
|
2014-04-16 22:03:22 +04:00
|
|
|
return FALSE;
|
2014-04-09 23:32:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&(pContext->lock));
|
2014-04-16 22:03:22 +04:00
|
|
|
return TRUE;
|
2014-04-09 23:32:18 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static PCSC_SCARDHANDLE* PCSC_GetCardHandleData(SCARDHANDLE hCard)
|
2014-05-11 13:23:30 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDHANDLE* pCard;
|
2014-05-11 13:23:30 +04:00
|
|
|
|
|
|
|
if (!g_CardHandles)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pCard = (PCSC_SCARDHANDLE*)ListDictionary_GetItemValue(g_CardHandles, (void*)hCard);
|
2014-05-11 13:23:30 +04:00
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return pCard;
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static SCARDCONTEXT PCSC_GetCardContextFromHandle(SCARDHANDLE hCard)
|
2014-05-11 13:23:30 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDHANDLE* pCard;
|
2014-05-11 13:23:30 +04:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return 0;
|
|
|
|
|
2015-07-17 18:13:41 +03:00
|
|
|
return pCard->hSharedContext;
|
2014-05-11 13:23:30 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static BOOL PCSC_WaitForCardAccess(SCARDCONTEXT hContext, SCARDHANDLE hCard, BOOL shared)
|
2015-01-21 05:40:45 +03:00
|
|
|
{
|
|
|
|
BOOL status = TRUE;
|
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
|
|
|
PCSC_SCARDCONTEXT* pContext = NULL;
|
|
|
|
|
|
|
|
if (!hCard)
|
|
|
|
{
|
|
|
|
/* SCardConnect */
|
|
|
|
pContext = PCSC_GetCardContextData(hContext);
|
|
|
|
|
|
|
|
if (!pContext)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!pContext->owner)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* wait for card ownership */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
shared = pCard->shared;
|
|
|
|
hContext = pCard->hSharedContext;
|
|
|
|
pContext = PCSC_GetCardContextData(hContext);
|
|
|
|
|
|
|
|
if (!pContext)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!pContext->owner)
|
|
|
|
{
|
|
|
|
/* card is not owned */
|
|
|
|
if (!shared)
|
|
|
|
pContext->owner = hCard;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pContext->owner == hCard)
|
|
|
|
{
|
|
|
|
/* already card owner */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* wait for card ownership */
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static BOOL PCSC_ReleaseCardAccess(SCARDCONTEXT hContext, SCARDHANDLE hCard)
|
2015-01-21 05:40:45 +03:00
|
|
|
{
|
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
|
|
|
PCSC_SCARDCONTEXT* pContext = NULL;
|
|
|
|
|
|
|
|
if (!hCard)
|
|
|
|
{
|
|
|
|
/* release current owner */
|
|
|
|
pContext = PCSC_GetCardContextData(hContext);
|
|
|
|
|
|
|
|
if (!pContext)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hCard = pContext->owner;
|
|
|
|
|
|
|
|
if (!hCard)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* release card ownership */
|
|
|
|
pContext->owner = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
hContext = pCard->hSharedContext;
|
|
|
|
pContext = PCSC_GetCardContextData(hContext);
|
|
|
|
|
|
|
|
if (!pContext)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (pContext->owner == hCard)
|
|
|
|
{
|
|
|
|
/* release card ownership */
|
|
|
|
pContext->owner = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static PCSC_SCARDHANDLE* PCSC_ConnectCardHandle(SCARDCONTEXT hSharedContext, SCARDHANDLE hCard)
|
2014-06-02 19:08:32 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDHANDLE* pCard;
|
|
|
|
PCSC_SCARDCONTEXT* pContext;
|
2014-06-03 01:20:01 +04:00
|
|
|
pContext = PCSC_GetCardContextData(hSharedContext);
|
2014-06-02 19:08:32 +04:00
|
|
|
|
|
|
|
if (!pContext)
|
2014-06-03 01:20:01 +04:00
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "PCSC_ConnectCardHandle: null pContext!");
|
2014-06-03 01:20:01 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-11 13:23:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pCard = (PCSC_SCARDHANDLE*)calloc(1, sizeof(PCSC_SCARDHANDLE));
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2014-05-11 13:23:30 +04:00
|
|
|
if (!pCard)
|
|
|
|
return NULL;
|
|
|
|
|
2014-06-03 01:20:01 +04:00
|
|
|
pCard->hSharedContext = hSharedContext;
|
2014-06-02 19:08:32 +04:00
|
|
|
|
2014-05-11 13:23:30 +04:00
|
|
|
if (!g_CardHandles)
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
2014-05-11 13:23:30 +04:00
|
|
|
g_CardHandles = ListDictionary_New(TRUE);
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!g_CardHandles)
|
|
|
|
goto error;
|
|
|
|
}
|
2014-05-11 13:23:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ListDictionary_Add(g_CardHandles, (void*)hCard, (void*)pCard))
|
2015-05-18 12:28:00 +03:00
|
|
|
goto error;
|
2015-01-21 05:40:45 +03:00
|
|
|
|
2015-05-20 20:19:50 +03:00
|
|
|
pContext->dwCardHandleCount++;
|
2014-05-11 13:23:30 +04:00
|
|
|
return pCard;
|
2015-05-18 12:28:00 +03:00
|
|
|
error:
|
|
|
|
free(pCard);
|
|
|
|
return NULL;
|
2014-05-11 13:23:30 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static void PCSC_DisconnectCardHandle(SCARDHANDLE hCard)
|
2014-05-11 13:23:30 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDHANDLE* pCard;
|
|
|
|
PCSC_SCARDCONTEXT* pContext;
|
2014-05-11 13:23:30 +04:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return;
|
|
|
|
|
2014-06-03 01:20:01 +04:00
|
|
|
pContext = PCSC_GetCardContextData(pCard->hSharedContext);
|
2014-05-11 13:23:30 +04:00
|
|
|
free(pCard);
|
|
|
|
|
|
|
|
if (!g_CardHandles)
|
|
|
|
return;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
ListDictionary_Remove(g_CardHandles, (void*)hCard);
|
2014-06-03 01:20:01 +04:00
|
|
|
|
|
|
|
if (!pContext)
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "PCSC_DisconnectCardHandle: null pContext!");
|
2014-06-03 01:20:01 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pContext->dwCardHandleCount--;
|
2014-05-11 13:23:30 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static BOOL PCSC_AddMemoryBlock(SCARDCONTEXT hContext, void* pvMem)
|
2014-04-07 08:10:27 +04:00
|
|
|
{
|
|
|
|
if (!g_MemoryBlocks)
|
2015-05-18 12:28:00 +03:00
|
|
|
{
|
2014-04-07 08:10:27 +04:00
|
|
|
g_MemoryBlocks = ListDictionary_New(TRUE);
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!g_MemoryBlocks)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-04-07 08:10:27 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return ListDictionary_Add(g_MemoryBlocks, pvMem, (void*)hContext);
|
2014-04-07 08:10:27 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
static void* PCSC_RemoveMemoryBlock(SCARDCONTEXT hContext, void* pvMem)
|
2014-04-07 08:10:27 +04:00
|
|
|
{
|
2017-12-11 17:49:03 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
|
2014-04-07 08:10:27 +04:00
|
|
|
if (!g_MemoryBlocks)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ListDictionary_Remove(g_MemoryBlocks, pvMem);
|
|
|
|
}
|
|
|
|
|
2014-04-04 23:30:03 +04:00
|
|
|
/**
|
|
|
|
* Standard Windows Smart Card API (PCSC)
|
|
|
|
*/
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardEstablishContext_Internal(DWORD dwScope, LPCVOID pvReserved1,
|
|
|
|
LPCVOID pvReserved2,
|
|
|
|
LPSCARDCONTEXT phContext)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2017-12-11 17:49:03 +03:00
|
|
|
WINPR_UNUSED(dwScope); /* SCARD_SCOPE_SYSTEM is the only scope supported by pcsc-lite */
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardEstablishContext)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardEstablishContext");
|
2014-04-09 22:27:44 +04:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status =
|
|
|
|
g_PCSC.pfnSCardEstablishContext(SCARD_SCOPE_SYSTEM, pvReserved1, pvReserved2, phContext);
|
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-06-03 01:20:01 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1,
|
|
|
|
LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
|
2014-06-03 01:20:01 +04:00
|
|
|
{
|
2017-12-11 17:49:03 +03:00
|
|
|
LONG status;
|
2020-02-17 12:53:02 +03:00
|
|
|
|
2014-06-03 01:20:01 +04:00
|
|
|
status = PCSC_SCardEstablishContext_Internal(dwScope, pvReserved1, pvReserved2, phContext);
|
|
|
|
|
2014-05-04 01:07:30 +04:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
2014-04-26 06:26:55 +04:00
|
|
|
PCSC_EstablishCardContext(*phContext);
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardReleaseContext_Internal(SCARDCONTEXT hContext)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardReleaseContext)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardReleaseContext");
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-05-12 18:17:49 +04:00
|
|
|
if (!hContext)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "SCardReleaseContext: null hContext");
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-05-12 18:17:49 +04:00
|
|
|
}
|
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardReleaseContext(hContext);
|
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-06-03 01:20:01 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardReleaseContext(SCARDCONTEXT hContext)
|
2014-06-03 01:20:01 +04:00
|
|
|
{
|
|
|
|
LONG status = SCARD_S_SUCCESS;
|
2020-02-17 12:53:02 +03:00
|
|
|
|
2014-06-03 01:20:01 +04:00
|
|
|
status = PCSC_SCardReleaseContext_Internal(hContext);
|
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
2014-04-26 06:26:55 +04:00
|
|
|
PCSC_ReleaseCardContext(hContext);
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardIsValidContext(SCARDCONTEXT hContext)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardIsValidContext)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardIsValidContext");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardIsValidContext(hContext);
|
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListReaderGroups_Internal(SCARDCONTEXT hContext, LPSTR mszGroups,
|
|
|
|
LPDWORD pcchGroups)
|
2015-06-30 18:42:01 +03:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2015-06-30 18:42:01 +03:00
|
|
|
BOOL pcchGroupsAlloc = FALSE;
|
|
|
|
PCSC_DWORD pcsc_cchGroups = 0;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
LPSTR lpstr;
|
|
|
|
LPSTR* lppstr;
|
|
|
|
} conv;
|
|
|
|
conv.lpstr = mszGroups;
|
2015-06-30 18:42:01 +03:00
|
|
|
|
|
|
|
if (!pcchGroups)
|
|
|
|
return SCARD_E_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!g_PCSC.pfnSCardListReaderGroups)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaderGroups");
|
2015-06-30 18:42:01 +03:00
|
|
|
|
|
|
|
if (*pcchGroups == SCARD_AUTOALLOCATE)
|
|
|
|
pcchGroupsAlloc = TRUE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pcsc_cchGroups = pcchGroupsAlloc ? PCSC_SCARD_AUTOALLOCATE : (PCSC_DWORD)*pcchGroups;
|
2015-06-30 18:42:01 +03:00
|
|
|
|
|
|
|
if (pcchGroupsAlloc && !g_SCardAutoAllocate)
|
|
|
|
{
|
|
|
|
pcsc_cchGroups = 0;
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardListReaderGroups(hContext, NULL, &pcsc_cchGroups);
|
2015-06-30 18:42:01 +03:00
|
|
|
|
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.lppstr = calloc(1, pcsc_cchGroups);
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if (!*conv.lppstr)
|
2015-06-30 18:42:01 +03:00
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
status = g_PCSC.pfnSCardListReaderGroups(hContext, *conv.lppstr, &pcsc_cchGroups);
|
2015-06-30 18:42:01 +03:00
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
2020-02-20 15:42:54 +03:00
|
|
|
free(*conv.lppstr);
|
2015-06-30 18:42:01 +03:00
|
|
|
else
|
2020-02-20 15:42:54 +03:00
|
|
|
PCSC_AddMemoryBlock(hContext, *conv.lppstr);
|
2015-06-30 18:42:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardListReaderGroups(hContext, mszGroups, &pcsc_cchGroups);
|
2015-06-30 18:42:01 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
*pcchGroups = (DWORD)pcsc_cchGroups;
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2015-06-30 18:42:01 +03:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListReaderGroupsA(SCARDCONTEXT hContext, LPSTR mszGroups,
|
|
|
|
LPDWORD pcchGroups)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-04-06 00:06:46 +04:00
|
|
|
LONG status = SCARD_S_SUCCESS;
|
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardListReaderGroups)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaderGroups");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_LockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2015-06-30 18:42:01 +03:00
|
|
|
status = PCSC_SCardListReaderGroups_Internal(hContext, mszGroups, pcchGroups);
|
2014-05-03 21:49:50 +04:00
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_UnlockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListReaderGroupsW(SCARDCONTEXT hContext, LPWSTR mszGroups,
|
|
|
|
LPDWORD pcchGroups)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2015-06-30 18:42:01 +03:00
|
|
|
LPSTR mszGroupsA = NULL;
|
|
|
|
LPSTR* pMszGroupsA = &mszGroupsA;
|
2014-04-06 00:06:46 +04:00
|
|
|
LONG status = SCARD_S_SUCCESS;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
LPWSTR lpstr;
|
|
|
|
LPWSTR* lppstr;
|
|
|
|
} conv;
|
|
|
|
conv.lpstr = mszGroups;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardListReaderGroups)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaderGroups");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_LockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status = PCSC_SCardListReaderGroups_Internal(hContext, (LPSTR)&mszGroupsA, pcchGroups);
|
2015-06-30 18:42:01 +03:00
|
|
|
|
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
int rc = ConvertToUnicode(CP_UTF8, 0, *pMszGroupsA, (int)*pcchGroups, conv.lppstr, 0);
|
|
|
|
if (rc < 0)
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
*pcchGroups = (DWORD)rc;
|
|
|
|
PCSC_AddMemoryBlock(hContext, conv.lpstr);
|
2015-06-30 18:42:01 +03:00
|
|
|
PCSC_SCardFreeMemory_Internal(hContext, *pMszGroupsA);
|
|
|
|
}
|
2014-05-03 21:49:50 +04:00
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_UnlockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListReaders_Internal(SCARDCONTEXT hContext, LPCSTR mszGroups,
|
|
|
|
LPSTR mszReaders, LPDWORD pcchReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2014-05-03 21:49:50 +04:00
|
|
|
BOOL pcchReadersAlloc = FALSE;
|
|
|
|
PCSC_DWORD pcsc_cchReaders = 0;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
LPSTR lpstr;
|
|
|
|
LPSTR* lppstr;
|
|
|
|
} conv;
|
|
|
|
conv.lpstr = mszReaders;
|
2014-05-03 21:49:50 +04:00
|
|
|
|
|
|
|
if (!pcchReaders)
|
|
|
|
return SCARD_E_INVALID_PARAMETER;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardListReaders)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaders");
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
mszGroups = NULL; /* mszGroups is not supported by pcsc-lite */
|
2014-04-09 22:27:44 +04:00
|
|
|
|
2014-05-03 21:49:50 +04:00
|
|
|
if (*pcchReaders == SCARD_AUTOALLOCATE)
|
|
|
|
pcchReadersAlloc = TRUE;
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pcsc_cchReaders = pcchReadersAlloc ? PCSC_SCARD_AUTOALLOCATE : (PCSC_DWORD)*pcchReaders;
|
2014-05-03 21:49:50 +04:00
|
|
|
|
|
|
|
if (pcchReadersAlloc && !g_SCardAutoAllocate)
|
2014-04-26 06:26:55 +04:00
|
|
|
{
|
2014-05-03 21:49:50 +04:00
|
|
|
pcsc_cchReaders = 0;
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardListReaders(hContext, mszGroups, NULL, &pcsc_cchReaders);
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.lppstr = calloc(1, pcsc_cchReaders);
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if (!*conv.lppstr)
|
2014-04-26 06:26:55 +04:00
|
|
|
return SCARD_E_NO_MEMORY;
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status =
|
2020-02-20 15:42:54 +03:00
|
|
|
g_PCSC.pfnSCardListReaders(hContext, mszGroups, *conv.lppstr, &pcsc_cchReaders);
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (status != SCARD_S_SUCCESS)
|
2020-02-20 15:42:54 +03:00
|
|
|
free(*conv.lppstr);
|
2014-04-26 06:26:55 +04:00
|
|
|
else
|
2020-02-20 15:42:54 +03:00
|
|
|
PCSC_AddMemoryBlock(hContext, *conv.lppstr);
|
2014-04-09 05:02:30 +04:00
|
|
|
}
|
2014-04-26 06:26:55 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardListReaders(hContext, mszGroups, mszReaders, &pcsc_cchReaders);
|
2014-04-26 06:26:55 +04:00
|
|
|
}
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
*pcchReaders = (DWORD)pcsc_cchReaders;
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-13 07:38:15 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListReadersA(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders,
|
|
|
|
LPDWORD pcchReaders)
|
2014-04-13 07:38:15 +04:00
|
|
|
{
|
2014-05-04 01:07:30 +04:00
|
|
|
BOOL nullCardContext = FALSE;
|
2014-04-13 07:38:15 +04:00
|
|
|
LONG status = SCARD_S_SUCCESS;
|
|
|
|
|
2014-04-28 04:11:31 +04:00
|
|
|
if (!g_PCSC.pfnSCardListReaders)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaders");
|
2014-04-28 04:11:31 +04:00
|
|
|
|
2014-05-04 01:07:30 +04:00
|
|
|
if (!hContext)
|
|
|
|
{
|
|
|
|
status = PCSC_SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
|
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
nullCardContext = TRUE;
|
|
|
|
}
|
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_LockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-13 07:38:15 +04:00
|
|
|
|
2014-04-28 04:11:31 +04:00
|
|
|
status = PCSC_SCardListReaders_Internal(hContext, mszGroups, mszReaders, pcchReaders);
|
2014-04-13 07:38:15 +04:00
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_UnlockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-05-04 01:07:30 +04:00
|
|
|
if (nullCardContext)
|
|
|
|
{
|
|
|
|
status = PCSC_SCardReleaseContext(hContext);
|
|
|
|
}
|
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListReadersW(SCARDCONTEXT hContext, LPCWSTR mszGroups,
|
|
|
|
LPWSTR mszReaders, LPDWORD pcchReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-04-28 04:11:31 +04:00
|
|
|
LPSTR mszGroupsA = NULL;
|
|
|
|
LPSTR mszReadersA = NULL;
|
2014-08-18 21:34:47 +04:00
|
|
|
LPSTR* pMszReadersA = &mszReadersA;
|
2014-04-06 00:06:46 +04:00
|
|
|
LONG status = SCARD_S_SUCCESS;
|
2014-05-04 01:07:30 +04:00
|
|
|
BOOL nullCardContext = FALSE;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
LPWSTR lpstr;
|
|
|
|
LPWSTR* lppstr;
|
|
|
|
} conv;
|
|
|
|
conv.lpstr = mszReaders;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-28 04:11:31 +04:00
|
|
|
if (!g_PCSC.pfnSCardListReaders)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardListReaders");
|
2014-04-28 04:11:31 +04:00
|
|
|
|
2014-05-04 01:07:30 +04:00
|
|
|
if (!hContext)
|
|
|
|
{
|
|
|
|
status = PCSC_SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
|
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
nullCardContext = TRUE;
|
|
|
|
}
|
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_LockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-04-28 04:11:31 +04:00
|
|
|
mszGroups = NULL; /* mszGroups is not supported by pcsc-lite */
|
2014-04-09 22:27:44 +04:00
|
|
|
|
2014-04-28 04:11:31 +04:00
|
|
|
if (mszGroups)
|
2019-11-06 17:24:51 +03:00
|
|
|
ConvertFromUnicode(CP_UTF8, 0, mszGroups, -1, (char**)&mszGroupsA, 0, NULL, NULL);
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status = PCSC_SCardListReaders_Internal(hContext, mszGroupsA, (LPSTR)&mszReadersA, pcchReaders);
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2014-04-28 04:11:31 +04:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
int rc = ConvertToUnicode(CP_UTF8, 0, *pMszReadersA, (int)*pcchReaders, conv.lppstr, 0);
|
|
|
|
if (rc < 0)
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
*pcchReaders = (DWORD)rc;
|
|
|
|
PCSC_AddMemoryBlock(hContext, conv.lpstr);
|
2014-04-28 04:11:31 +04:00
|
|
|
PCSC_SCardFreeMemory_Internal(hContext, *pMszReadersA);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2014-04-28 04:11:31 +04:00
|
|
|
free(mszGroupsA);
|
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_UnlockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-05-04 01:07:30 +04:00
|
|
|
if (nullCardContext)
|
|
|
|
{
|
|
|
|
status = PCSC_SCardReleaseContext(hContext);
|
|
|
|
}
|
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListCardsA(SCARDCONTEXT hContext, LPCBYTE pbAtr,
|
|
|
|
LPCGUID rgquidInterfaces, DWORD cguidInterfaceCount,
|
|
|
|
CHAR* mszCards, LPDWORD pcchCards)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(pbAtr);
|
|
|
|
WINPR_UNUSED(rgquidInterfaces);
|
|
|
|
WINPR_UNUSED(cguidInterfaceCount);
|
|
|
|
WINPR_UNUSED(mszCards);
|
|
|
|
WINPR_UNUSED(pcchCards);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListCardsW(SCARDCONTEXT hContext, LPCBYTE pbAtr,
|
|
|
|
LPCGUID rgquidInterfaces, DWORD cguidInterfaceCount,
|
|
|
|
WCHAR* mszCards, LPDWORD pcchCards)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(pbAtr);
|
|
|
|
WINPR_UNUSED(rgquidInterfaces);
|
|
|
|
WINPR_UNUSED(cguidInterfaceCount);
|
|
|
|
WINPR_UNUSED(mszCards);
|
|
|
|
WINPR_UNUSED(pcchCards);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListInterfacesA(SCARDCONTEXT hContext, LPCSTR szCard,
|
|
|
|
LPGUID pguidInterfaces, LPDWORD pcguidInterfaces)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCard);
|
|
|
|
WINPR_UNUSED(pguidInterfaces);
|
|
|
|
WINPR_UNUSED(pcguidInterfaces);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListInterfacesW(SCARDCONTEXT hContext, LPCWSTR szCard,
|
|
|
|
LPGUID pguidInterfaces, LPDWORD pcguidInterfaces)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCard);
|
|
|
|
WINPR_UNUSED(pguidInterfaces);
|
|
|
|
WINPR_UNUSED(pcguidInterfaces);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetProviderIdA(SCARDCONTEXT hContext, LPCSTR szCard,
|
|
|
|
LPGUID pguidProviderId)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCard);
|
|
|
|
WINPR_UNUSED(pguidProviderId);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetProviderIdW(SCARDCONTEXT hContext, LPCWSTR szCard,
|
|
|
|
LPGUID pguidProviderId)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCard);
|
|
|
|
WINPR_UNUSED(pguidProviderId);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetCardTypeProviderNameA(SCARDCONTEXT hContext, LPCSTR szCardName,
|
|
|
|
DWORD dwProviderId, CHAR* szProvider,
|
|
|
|
LPDWORD pcchProvider)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCardName);
|
|
|
|
WINPR_UNUSED(dwProviderId);
|
|
|
|
WINPR_UNUSED(szProvider);
|
|
|
|
WINPR_UNUSED(pcchProvider);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetCardTypeProviderNameW(SCARDCONTEXT hContext, LPCWSTR szCardName,
|
|
|
|
DWORD dwProviderId, WCHAR* szProvider,
|
|
|
|
LPDWORD pcchProvider)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCardName);
|
|
|
|
WINPR_UNUSED(dwProviderId);
|
|
|
|
WINPR_UNUSED(szProvider);
|
|
|
|
WINPR_UNUSED(pcchProvider);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardIntroduceReaderGroupA(SCARDCONTEXT hContext, LPCSTR szGroupName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szGroupName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardIntroduceReaderGroupW(SCARDCONTEXT hContext, LPCWSTR szGroupName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szGroupName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardForgetReaderGroupA(SCARDCONTEXT hContext, LPCSTR szGroupName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szGroupName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardForgetReaderGroupW(SCARDCONTEXT hContext, LPCWSTR szGroupName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szGroupName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardIntroduceReaderA(SCARDCONTEXT hContext, LPCSTR szReaderName,
|
|
|
|
LPCSTR szDeviceName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(szDeviceName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardIntroduceReaderW(SCARDCONTEXT hContext, LPCWSTR szReaderName,
|
|
|
|
LPCWSTR szDeviceName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(szDeviceName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardForgetReaderA(SCARDCONTEXT hContext, LPCSTR szReaderName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardForgetReaderW(SCARDCONTEXT hContext, LPCWSTR szReaderName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardAddReaderToGroupA(SCARDCONTEXT hContext, LPCSTR szReaderName,
|
|
|
|
LPCSTR szGroupName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(szGroupName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardAddReaderToGroupW(SCARDCONTEXT hContext, LPCWSTR szReaderName,
|
|
|
|
LPCWSTR szGroupName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(szGroupName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardRemoveReaderFromGroupA(SCARDCONTEXT hContext, LPCSTR szReaderName,
|
|
|
|
LPCSTR szGroupName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(szGroupName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardRemoveReaderFromGroupW(SCARDCONTEXT hContext, LPCWSTR szReaderName,
|
|
|
|
LPCWSTR szGroupName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(szGroupName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardIntroduceCardTypeA(SCARDCONTEXT hContext, LPCSTR szCardName,
|
|
|
|
LPCGUID pguidPrimaryProvider,
|
|
|
|
LPCGUID rgguidInterfaces, DWORD dwInterfaceCount,
|
|
|
|
LPCBYTE pbAtr, LPCBYTE pbAtrMask, DWORD cbAtrLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCardName);
|
|
|
|
WINPR_UNUSED(pguidPrimaryProvider);
|
|
|
|
WINPR_UNUSED(rgguidInterfaces);
|
|
|
|
WINPR_UNUSED(dwInterfaceCount);
|
|
|
|
WINPR_UNUSED(pbAtr);
|
|
|
|
WINPR_UNUSED(pbAtrMask);
|
|
|
|
WINPR_UNUSED(cbAtrLen);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardIntroduceCardTypeW(SCARDCONTEXT hContext, LPCWSTR szCardName,
|
|
|
|
LPCGUID pguidPrimaryProvider,
|
|
|
|
LPCGUID rgguidInterfaces, DWORD dwInterfaceCount,
|
|
|
|
LPCBYTE pbAtr, LPCBYTE pbAtrMask, DWORD cbAtrLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCardName);
|
|
|
|
WINPR_UNUSED(pguidPrimaryProvider);
|
|
|
|
WINPR_UNUSED(rgguidInterfaces);
|
|
|
|
WINPR_UNUSED(dwInterfaceCount);
|
|
|
|
WINPR_UNUSED(pbAtr);
|
|
|
|
WINPR_UNUSED(pbAtrMask);
|
|
|
|
WINPR_UNUSED(cbAtrLen);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardSetCardTypeProviderNameA(SCARDCONTEXT hContext, LPCSTR szCardName,
|
|
|
|
DWORD dwProviderId, LPCSTR szProvider)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCardName);
|
|
|
|
WINPR_UNUSED(dwProviderId);
|
|
|
|
WINPR_UNUSED(szProvider);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardSetCardTypeProviderNameW(SCARDCONTEXT hContext, LPCWSTR szCardName,
|
|
|
|
DWORD dwProviderId, LPCWSTR szProvider)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCardName);
|
|
|
|
WINPR_UNUSED(dwProviderId);
|
|
|
|
WINPR_UNUSED(szProvider);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardForgetCardTypeA(SCARDCONTEXT hContext, LPCSTR szCardName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCardName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardForgetCardTypeW(SCARDCONTEXT hContext, LPCWSTR szCardName)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szCardName);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
static LONG WINAPI PCSC_SCardFreeMemory_Internal(SCARDCONTEXT hContext, LPVOID pvMem)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
if (PCSC_RemoveMemoryBlock(hContext, pvMem))
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
free((void*)pvMem);
|
2014-04-09 07:52:24 +04:00
|
|
|
status = SCARD_S_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (g_PCSC.pfnSCardFreeMemory)
|
2014-04-07 08:10:27 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardFreeMemory(hContext, pvMem);
|
2014-04-07 08:10:27 +04:00
|
|
|
}
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-12 19:24:47 +04:00
|
|
|
}
|
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
static LONG WINAPI PCSC_SCardFreeMemory(SCARDCONTEXT hContext, LPVOID pvMem)
|
2014-04-12 19:24:47 +04:00
|
|
|
{
|
|
|
|
LONG status = SCARD_S_SUCCESS;
|
|
|
|
|
2014-05-04 01:07:30 +04:00
|
|
|
if (hContext)
|
|
|
|
{
|
|
|
|
if (!PCSC_LockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
|
|
|
}
|
2014-04-12 19:24:47 +04:00
|
|
|
|
|
|
|
status = PCSC_SCardFreeMemory_Internal(hContext, pvMem);
|
|
|
|
|
2014-05-04 01:07:30 +04:00
|
|
|
if (hContext)
|
|
|
|
{
|
|
|
|
if (!PCSC_UnlockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
|
|
|
}
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static HANDLE WINAPI PCSC_SCardAccessStartedEvent(void)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-05-11 07:43:25 +04:00
|
|
|
LONG status = 0;
|
|
|
|
SCARDCONTEXT hContext = 0;
|
2020-02-17 12:53:02 +03:00
|
|
|
|
2014-05-11 07:43:25 +04:00
|
|
|
status = PCSC_SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
|
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
status = PCSC_SCardReleaseContext(hContext);
|
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!g_StartedEvent)
|
|
|
|
{
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(g_StartedEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!SetEvent(g_StartedEvent))
|
|
|
|
{
|
|
|
|
CloseHandle(g_StartedEvent);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-11 07:43:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
g_StartedEventRefCount++;
|
|
|
|
return g_StartedEvent;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static void WINAPI PCSC_SCardReleaseStartedEvent(void)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-05-11 07:43:25 +04:00
|
|
|
g_StartedEventRefCount--;
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2014-05-11 07:43:25 +04:00
|
|
|
if (g_StartedEventRefCount == 0)
|
|
|
|
{
|
|
|
|
if (g_StartedEvent)
|
|
|
|
{
|
|
|
|
CloseHandle(g_StartedEvent);
|
|
|
|
g_StartedEvent = NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardLocateCardsA(SCARDCONTEXT hContext, LPCSTR mszCards,
|
|
|
|
LPSCARD_READERSTATEA rgReaderStates, DWORD cReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(mszCards);
|
|
|
|
WINPR_UNUSED(rgReaderStates);
|
|
|
|
WINPR_UNUSED(cReaders);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardLocateCardsW(SCARDCONTEXT hContext, LPCWSTR mszCards,
|
|
|
|
LPSCARD_READERSTATEW rgReaderStates, DWORD cReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(mszCards);
|
|
|
|
WINPR_UNUSED(rgReaderStates);
|
|
|
|
WINPR_UNUSED(cReaders);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardLocateCardsByATRA(SCARDCONTEXT hContext, LPSCARD_ATRMASK rgAtrMasks,
|
|
|
|
DWORD cAtrs, LPSCARD_READERSTATEA rgReaderStates,
|
|
|
|
DWORD cReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(rgAtrMasks);
|
|
|
|
WINPR_UNUSED(cAtrs);
|
|
|
|
WINPR_UNUSED(rgReaderStates);
|
|
|
|
WINPR_UNUSED(cReaders);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardLocateCardsByATRW(SCARDCONTEXT hContext, LPSCARD_ATRMASK rgAtrMasks,
|
|
|
|
DWORD cAtrs, LPSCARD_READERSTATEW rgReaderStates,
|
|
|
|
DWORD cReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(rgAtrMasks);
|
|
|
|
WINPR_UNUSED(cAtrs);
|
|
|
|
WINPR_UNUSED(rgReaderStates);
|
|
|
|
WINPR_UNUSED(cReaders);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetStatusChange_Internal(SCARDCONTEXT hContext, DWORD dwTimeout,
|
|
|
|
LPSCARD_READERSTATEA rgReaderStates,
|
|
|
|
DWORD cReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2018-10-24 15:36:12 +03:00
|
|
|
PCSC_DWORD i, j;
|
2020-02-17 16:07:20 +03:00
|
|
|
INT64* map;
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD cMappedReaders;
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARD_READERSTATE* states;
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_dwTimeout = (PCSC_DWORD)dwTimeout;
|
|
|
|
PCSC_DWORD pcsc_cReaders = (PCSC_DWORD)cReaders;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardGetStatusChange)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardGetStatusChange");
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-11 09:53:47 +04:00
|
|
|
if (!cReaders)
|
|
|
|
return SCARD_S_SUCCESS;
|
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
/* pcsc-lite interprets value 0 as INFINITE, work around the problem by using value 1 */
|
|
|
|
pcsc_dwTimeout = pcsc_dwTimeout ? pcsc_dwTimeout : 1;
|
2014-04-26 06:26:55 +04:00
|
|
|
/**
|
|
|
|
* Apple's SmartCard Services (not vanilla pcsc-lite) appears to have trouble with the
|
|
|
|
* "\\\\?PnP?\\Notification" reader name. I am always getting EXC_BAD_ACCESS with it.
|
|
|
|
*
|
|
|
|
* The SmartCard Services tarballs can be found here:
|
|
|
|
* http://opensource.apple.com/tarballs/SmartCardServices/
|
|
|
|
*
|
|
|
|
* The "\\\\?PnP?\\Notification" string cannot be found anywhere in the sources,
|
|
|
|
* while this string is present in the vanilla pcsc-lite sources.
|
|
|
|
*
|
2014-05-11 09:53:47 +04:00
|
|
|
* To work around this apparent lack of "\\\\?PnP?\\Notification" support,
|
2014-04-28 00:43:09 +04:00
|
|
|
* we have to filter rgReaderStates to exclude the special PnP reader name.
|
2014-04-26 06:26:55 +04:00
|
|
|
*/
|
2020-02-17 16:07:20 +03:00
|
|
|
map = (INT64*)calloc(pcsc_cReaders, sizeof(INT64));
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2014-04-28 00:43:09 +04:00
|
|
|
if (!map)
|
|
|
|
return SCARD_E_NO_MEMORY;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
states = (PCSC_SCARD_READERSTATE*)calloc(pcsc_cReaders, sizeof(PCSC_SCARD_READERSTATE));
|
2014-04-26 06:26:55 +04:00
|
|
|
|
|
|
|
if (!states)
|
2014-11-17 00:50:50 +03:00
|
|
|
{
|
2015-06-30 18:42:01 +03:00
|
|
|
free(map);
|
2014-04-26 06:26:55 +04:00
|
|
|
return SCARD_E_NO_MEMORY;
|
2014-11-17 00:50:50 +03:00
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-05-03 21:49:50 +04:00
|
|
|
for (i = j = 0; i < pcsc_cReaders; i++)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-04-28 00:43:09 +04:00
|
|
|
if (!g_PnP_Notification)
|
|
|
|
{
|
2017-12-11 17:49:03 +03:00
|
|
|
if (0 == _stricmp(rgReaderStates[i].szReader, SMARTCARD_PNP_NOTIFICATION_A))
|
2014-04-28 00:43:09 +04:00
|
|
|
{
|
|
|
|
map[i] = -1; /* unmapped */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
map[i] = (INT64)j;
|
2017-12-11 17:49:03 +03:00
|
|
|
states[j].szReader = rgReaderStates[i].szReader;
|
2014-04-26 06:26:55 +04:00
|
|
|
states[j].dwCurrentState = rgReaderStates[i].dwCurrentState;
|
2014-11-10 00:42:35 +03:00
|
|
|
states[j].pvUserData = rgReaderStates[i].pvUserData;
|
2014-04-26 06:26:55 +04:00
|
|
|
states[j].dwEventState = rgReaderStates[i].dwEventState;
|
|
|
|
states[j].cbAtr = rgReaderStates[i].cbAtr;
|
|
|
|
CopyMemory(&(states[j].rgbAtr), &(rgReaderStates[i].rgbAtr), PCSC_MAX_ATR_SIZE);
|
|
|
|
j++;
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-04-28 00:43:09 +04:00
|
|
|
cMappedReaders = j;
|
2014-04-13 07:38:15 +04:00
|
|
|
|
2014-04-28 00:43:09 +04:00
|
|
|
if (cMappedReaders > 0)
|
2014-04-26 06:26:55 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardGetStatusChange(hContext, pcsc_dwTimeout, states, cMappedReaders);
|
2014-04-26 06:26:55 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-28 00:43:09 +04:00
|
|
|
status = SCARD_S_SUCCESS;
|
2014-04-26 06:26:55 +04:00
|
|
|
}
|
2014-04-09 22:27:44 +04:00
|
|
|
|
2014-05-03 21:49:50 +04:00
|
|
|
for (i = 0; i < pcsc_cReaders; i++)
|
2014-04-26 06:26:55 +04:00
|
|
|
{
|
2014-04-28 00:43:09 +04:00
|
|
|
if (map[i] < 0)
|
|
|
|
continue; /* unmapped */
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
j = (PCSC_DWORD)map[i];
|
|
|
|
rgReaderStates[i].dwCurrentState = (DWORD)states[j].dwCurrentState;
|
|
|
|
rgReaderStates[i].cbAtr = (DWORD)states[j].cbAtr;
|
2014-04-26 06:26:55 +04:00
|
|
|
CopyMemory(&(rgReaderStates[i].rgbAtr), &(states[j].rgbAtr), PCSC_MAX_ATR_SIZE);
|
2020-02-17 16:07:20 +03:00
|
|
|
rgReaderStates[i].dwEventState = (DWORD)states[j].dwEventState;
|
2014-04-13 07:38:15 +04:00
|
|
|
}
|
2014-11-11 19:46:59 +03:00
|
|
|
|
2014-11-17 00:50:50 +03:00
|
|
|
free(map);
|
|
|
|
free(states);
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-13 07:38:15 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetStatusChangeA(SCARDCONTEXT hContext, DWORD dwTimeout,
|
|
|
|
LPSCARD_READERSTATEA rgReaderStates, DWORD cReaders)
|
2014-04-13 07:38:15 +04:00
|
|
|
{
|
|
|
|
LONG status = SCARD_S_SUCCESS;
|
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_LockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-13 07:38:15 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
status = PCSC_SCardGetStatusChange_Internal(hContext, dwTimeout, rgReaderStates, cReaders);
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_UnlockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetStatusChangeW(SCARDCONTEXT hContext, DWORD dwTimeout,
|
|
|
|
LPSCARD_READERSTATEW rgReaderStates, DWORD cReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-04-26 06:26:55 +04:00
|
|
|
DWORD index;
|
|
|
|
LPSCARD_READERSTATEA states;
|
2014-04-06 00:06:46 +04:00
|
|
|
LONG status = SCARD_S_SUCCESS;
|
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardGetStatusChange)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardGetStatusChange");
|
2014-04-09 22:27:44 +04:00
|
|
|
|
2014-05-12 18:17:49 +04:00
|
|
|
if (!PCSC_LockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
states = (LPSCARD_READERSTATEA)calloc(cReaders, sizeof(SCARD_READERSTATEA));
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!states)
|
|
|
|
{
|
|
|
|
PCSC_UnlockCardContext(hContext);
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
}
|
2014-04-07 00:29:11 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
for (index = 0; index < cReaders; index++)
|
|
|
|
{
|
|
|
|
states[index].szReader = NULL;
|
|
|
|
ConvertFromUnicode(CP_UTF8, 0, rgReaderStates[index].szReader, -1,
|
2019-11-06 17:24:51 +03:00
|
|
|
(char**)&(states[index].szReader), 0, NULL, NULL);
|
2014-04-26 06:26:55 +04:00
|
|
|
states[index].pvUserData = rgReaderStates[index].pvUserData;
|
|
|
|
states[index].dwCurrentState = rgReaderStates[index].dwCurrentState;
|
|
|
|
states[index].dwEventState = rgReaderStates[index].dwEventState;
|
|
|
|
states[index].cbAtr = rgReaderStates[index].cbAtr;
|
|
|
|
CopyMemory(&(states[index].rgbAtr), &(rgReaderStates[index].rgbAtr), 36);
|
|
|
|
}
|
2014-04-07 00:29:11 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
status = PCSC_SCardGetStatusChange_Internal(hContext, dwTimeout, states, cReaders);
|
2014-04-07 00:29:11 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
for (index = 0; index < cReaders; index++)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
free((void*)states[index].szReader);
|
2014-04-26 06:26:55 +04:00
|
|
|
rgReaderStates[index].pvUserData = states[index].pvUserData;
|
|
|
|
rgReaderStates[index].dwCurrentState = states[index].dwCurrentState;
|
|
|
|
rgReaderStates[index].dwEventState = states[index].dwEventState;
|
|
|
|
rgReaderStates[index].cbAtr = states[index].cbAtr;
|
|
|
|
CopyMemory(&(rgReaderStates[index].rgbAtr), &(states[index].rgbAtr), 36);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
free(states);
|
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_UnlockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardCancel(SCARDCONTEXT hContext)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardCancel)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardCancel");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardCancel(hContext);
|
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardConnect_Internal(SCARDCONTEXT hContext, LPCSTR szReader,
|
|
|
|
DWORD dwShareMode, DWORD dwPreferredProtocols,
|
|
|
|
LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2015-01-21 05:40:45 +03:00
|
|
|
BOOL shared;
|
2020-02-17 16:07:20 +03:00
|
|
|
const char* szReaderPCSC;
|
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_dwShareMode = (PCSC_DWORD)dwShareMode;
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD pcsc_dwPreferredProtocols = 0;
|
|
|
|
PCSC_DWORD pcsc_dwActiveProtocol = 0;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardConnect)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardConnect");
|
2014-04-13 07:38:15 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
shared = (dwShareMode == SCARD_SHARE_DIRECT) ? TRUE : FALSE;
|
2017-12-11 17:49:03 +03:00
|
|
|
PCSC_WaitForCardAccess(hContext, 0, shared);
|
2020-02-17 16:07:20 +03:00
|
|
|
szReaderPCSC = szReader;
|
2014-04-13 07:38:15 +04:00
|
|
|
|
2014-11-10 01:00:41 +03:00
|
|
|
/**
|
2019-11-06 17:24:51 +03:00
|
|
|
* As stated here :
|
|
|
|
* https://pcsclite.alioth.debian.org/api/group__API.html#ga4e515829752e0a8dbc4d630696a8d6a5
|
|
|
|
* SCARD_PROTOCOL_UNDEFINED is valid for dwPreferredProtocols (only) if dwShareMode ==
|
|
|
|
* SCARD_SHARE_DIRECT and allows to send control commands to the reader (with SCardControl())
|
|
|
|
* even if a card is not present in the reader
|
2014-11-10 01:00:41 +03:00
|
|
|
*/
|
2014-11-10 00:42:35 +03:00
|
|
|
if (pcsc_dwShareMode == SCARD_SHARE_DIRECT && dwPreferredProtocols == SCARD_PROTOCOL_UNDEFINED)
|
|
|
|
pcsc_dwPreferredProtocols = SCARD_PROTOCOL_UNDEFINED;
|
|
|
|
else
|
2019-11-06 17:24:51 +03:00
|
|
|
pcsc_dwPreferredProtocols =
|
|
|
|
(PCSC_DWORD)PCSC_ConvertProtocolsFromWinSCard(dwPreferredProtocols);
|
2014-12-23 00:25:59 +03:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardConnect(hContext, szReaderPCSC, pcsc_dwShareMode,
|
|
|
|
pcsc_dwPreferredProtocols, phCard, &pcsc_dwActiveProtocol);
|
2014-04-07 08:58:34 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
2015-07-17 18:13:41 +03:00
|
|
|
pCard = PCSC_ConnectCardHandle(hContext, *phCard);
|
2019-11-06 17:24:51 +03:00
|
|
|
*pdwActiveProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwActiveProtocol);
|
2015-01-21 05:40:45 +03:00
|
|
|
pCard->shared = shared;
|
|
|
|
PCSC_WaitForCardAccess(hContext, pCard->hSharedContext, shared);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-13 07:38:15 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardConnectA(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode,
|
|
|
|
DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
|
|
|
|
LPDWORD pdwActiveProtocol)
|
2014-04-13 07:38:15 +04:00
|
|
|
{
|
|
|
|
LONG status = SCARD_S_SUCCESS;
|
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_LockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-13 07:38:15 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status = PCSC_SCardConnect_Internal(hContext, szReader, dwShareMode, dwPreferredProtocols,
|
|
|
|
phCard, pdwActiveProtocol);
|
2014-04-13 07:38:15 +04:00
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_UnlockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardConnectW(SCARDCONTEXT hContext, LPCWSTR szReader, DWORD dwShareMode,
|
|
|
|
DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
|
|
|
|
LPDWORD pdwActiveProtocol)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-04-26 06:26:55 +04:00
|
|
|
LPSTR szReaderA = NULL;
|
2014-04-06 00:06:46 +04:00
|
|
|
LONG status = SCARD_S_SUCCESS;
|
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_LockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (szReader)
|
|
|
|
ConvertFromUnicode(CP_UTF8, 0, szReader, -1, &szReaderA, 0, NULL, NULL);
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status = PCSC_SCardConnect_Internal(hContext, szReaderA, dwShareMode, dwPreferredProtocols,
|
|
|
|
phCard, pdwActiveProtocol);
|
2014-04-26 06:26:55 +04:00
|
|
|
free(szReaderA);
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2014-04-16 22:03:22 +04:00
|
|
|
if (!PCSC_UnlockCardContext(hContext))
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 23:32:18 +04:00
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
return status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode,
|
|
|
|
DWORD dwPreferredProtocols, DWORD dwInitialization,
|
|
|
|
LPDWORD pdwActiveProtocol)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2015-01-21 05:40:45 +03:00
|
|
|
BOOL shared;
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_dwShareMode = (PCSC_DWORD)dwShareMode;
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD pcsc_dwPreferredProtocols = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_dwInitialization = (PCSC_DWORD)dwInitialization;
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD pcsc_dwActiveProtocol = 0;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardReconnect)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardReconnect");
|
2014-04-13 02:13:18 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
shared = (dwShareMode == SCARD_SHARE_DIRECT) ? TRUE : FALSE;
|
2017-12-11 17:49:03 +03:00
|
|
|
PCSC_WaitForCardAccess(0, hCard, shared);
|
2019-11-06 17:24:51 +03:00
|
|
|
pcsc_dwPreferredProtocols = (PCSC_DWORD)PCSC_ConvertProtocolsFromWinSCard(dwPreferredProtocols);
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardReconnect(hCard, pcsc_dwShareMode, pcsc_dwPreferredProtocols,
|
|
|
|
pcsc_dwInitialization, &pcsc_dwActiveProtocol);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
*pdwActiveProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwActiveProtocol);
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_dwDisposition = (PCSC_DWORD)dwDisposition;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardDisconnect)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardDisconnect");
|
2014-04-07 08:58:34 +04:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardDisconnect(hCard, pcsc_dwDisposition);
|
2014-04-26 06:26:55 +04:00
|
|
|
|
|
|
|
if (status == SCARD_S_SUCCESS)
|
2014-05-11 13:23:30 +04:00
|
|
|
{
|
|
|
|
PCSC_DisconnectCardHandle(hCard);
|
|
|
|
}
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_ReleaseCardAccess(0, hCard);
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardBeginTransaction(SCARDHANDLE hCard)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
|
|
|
PCSC_SCARDCONTEXT* pContext = NULL;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardBeginTransaction)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardBeginTransaction");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2014-06-03 05:49:49 +04:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
pContext = PCSC_GetCardContextData(pCard->hSharedContext);
|
|
|
|
|
|
|
|
if (!pContext)
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
if (pContext->isTransactionLocked)
|
|
|
|
return SCARD_S_SUCCESS; /* disable nested transactions */
|
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardBeginTransaction(hCard);
|
|
|
|
|
2014-06-03 05:49:49 +04:00
|
|
|
pContext->isTransactionLocked = TRUE;
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2014-08-18 21:34:47 +04:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
|
|
|
PCSC_SCARDCONTEXT* pContext = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_dwDisposition = (PCSC_DWORD)dwDisposition;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardEndTransaction)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardEndTransaction");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2014-06-03 05:49:49 +04:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
pContext = PCSC_GetCardContextData(pCard->hSharedContext);
|
|
|
|
|
|
|
|
if (!pContext)
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_ReleaseCardAccess(0, hCard);
|
|
|
|
|
2014-06-03 05:49:49 +04:00
|
|
|
if (!pContext->isTransactionLocked)
|
|
|
|
return SCARD_S_SUCCESS; /* disable nested transactions */
|
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardEndTransaction(hCard, pcsc_dwDisposition);
|
|
|
|
|
2014-06-03 05:49:49 +04:00
|
|
|
pContext->isTransactionLocked = FALSE;
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardCancelTransaction(SCARDHANDLE hCard)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hCard);
|
2015-01-21 05:40:45 +03:00
|
|
|
return SCARD_S_SUCCESS;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardState(SCARDHANDLE hCard, LPDWORD pdwState, LPDWORD pdwProtocol,
|
|
|
|
LPBYTE pbAtr, LPDWORD pcbAtrLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD cchReaderLen;
|
2014-04-26 06:26:55 +04:00
|
|
|
SCARDCONTEXT hContext = 0;
|
|
|
|
LPSTR mszReaderNames = NULL;
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD pcsc_dwState = 0;
|
|
|
|
PCSC_DWORD pcsc_dwProtocol = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_cbAtrLen = (PCSC_DWORD)*pcbAtrLen;
|
2014-04-07 09:19:58 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardStatus)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardStatus");
|
2014-04-07 09:19:58 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return SCARD_E_INVALID_VALUE;
|
|
|
|
|
|
|
|
PCSC_WaitForCardAccess(0, hCard, pCard->shared);
|
2014-04-26 06:26:55 +04:00
|
|
|
hContext = PCSC_GetCardContextFromHandle(hCard);
|
2014-04-07 09:19:58 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!hContext)
|
|
|
|
return SCARD_E_INVALID_VALUE;
|
2014-04-13 02:13:18 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
cchReaderLen = SCARD_AUTOALLOCATE;
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardStatus(hCard, (LPSTR)&mszReaderNames, &cchReaderLen, &pcsc_dwState,
|
|
|
|
&pcsc_dwProtocol, pbAtr, &pcsc_cbAtrLen);
|
2014-04-26 06:26:55 +04:00
|
|
|
|
|
|
|
if (mszReaderNames)
|
|
|
|
PCSC_SCardFreeMemory_Internal(hContext, mszReaderNames);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
*pdwState = (DWORD)pcsc_dwState;
|
|
|
|
*pdwProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwProtocol);
|
|
|
|
*pcbAtrLen = (DWORD)pcsc_cbAtrLen;
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
/*
|
|
|
|
* PCSC returns a string but Windows SCardStatus requires the return to be a multi string.
|
|
|
|
* Therefore extra length checks and additional buffer allocation is required
|
|
|
|
*/
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardStatus_Internal(SCARDHANDLE hCard, LPSTR mszReaderNames,
|
|
|
|
LPDWORD pcchReaderLen, LPDWORD pdwState,
|
|
|
|
LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen,
|
|
|
|
BOOL unicode)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
2017-12-11 17:49:03 +03:00
|
|
|
SCARDCONTEXT hContext;
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status;
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD pcsc_cchReaderLen = 0;
|
2017-12-11 17:49:03 +03:00
|
|
|
PCSC_DWORD pcsc_cbAtrLen = 0;
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD pcsc_dwState = 0;
|
|
|
|
PCSC_DWORD pcsc_dwProtocol = 0;
|
2017-12-11 17:49:03 +03:00
|
|
|
BOOL allocateReader = FALSE;
|
|
|
|
BOOL allocateAtr = FALSE;
|
|
|
|
LPSTR readerNames = mszReaderNames;
|
|
|
|
LPBYTE atr = pbAtr;
|
|
|
|
LPSTR tReader = NULL;
|
|
|
|
LPBYTE tATR = NULL;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardStatus)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardStatus");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return SCARD_E_INVALID_VALUE;
|
|
|
|
|
|
|
|
PCSC_WaitForCardAccess(0, hCard, pCard->shared);
|
2014-04-26 06:26:55 +04:00
|
|
|
hContext = PCSC_GetCardContextFromHandle(hCard);
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
if (!hContext)
|
2014-04-26 06:26:55 +04:00
|
|
|
return SCARD_E_INVALID_VALUE;
|
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status =
|
|
|
|
g_PCSC.pfnSCardStatus(hCard, NULL, &pcsc_cchReaderLen, NULL, NULL, NULL, &pcsc_cbAtrLen);
|
2014-05-03 21:49:50 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
pcsc_cchReaderLen++;
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2017-12-14 17:46:14 +03:00
|
|
|
if (unicode)
|
|
|
|
pcsc_cchReaderLen *= 2;
|
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
if (pcchReaderLen)
|
2014-04-26 06:26:55 +04:00
|
|
|
{
|
2017-12-11 17:49:03 +03:00
|
|
|
if (*pcchReaderLen == SCARD_AUTOALLOCATE)
|
|
|
|
allocateReader = TRUE;
|
|
|
|
else if (mszReaderNames && (*pcchReaderLen < pcsc_cchReaderLen))
|
|
|
|
return SCARD_E_INSUFFICIENT_BUFFER;
|
|
|
|
else
|
|
|
|
pcsc_cchReaderLen = *pcchReaderLen;
|
|
|
|
}
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
if (pcbAtrLen)
|
|
|
|
{
|
|
|
|
if (*pcbAtrLen == SCARD_AUTOALLOCATE)
|
|
|
|
allocateAtr = TRUE;
|
|
|
|
else if (pbAtr && (*pcbAtrLen < pcsc_cbAtrLen))
|
|
|
|
return SCARD_E_INSUFFICIENT_BUFFER;
|
|
|
|
else
|
|
|
|
pcsc_cbAtrLen = *pcbAtrLen;
|
|
|
|
}
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
if (allocateReader && pcsc_cchReaderLen > 0 && mszReaderNames)
|
|
|
|
{
|
2016-01-28 01:40:08 +03:00
|
|
|
#ifdef __MACOSX__
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
/**
|
2019-11-06 17:24:51 +03:00
|
|
|
* Workaround for SCardStatus Bug in MAC OS X Yosemite
|
|
|
|
*/
|
2017-12-11 17:49:03 +03:00
|
|
|
if (OSXVersion == 0x10100000)
|
|
|
|
pcsc_cchReaderLen++;
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2014-12-14 17:15:33 +03:00
|
|
|
#endif
|
2017-12-11 17:49:03 +03:00
|
|
|
tReader = calloc(1, pcsc_cchReaderLen);
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
if (!tReader)
|
|
|
|
{
|
|
|
|
status = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto out_fail;
|
|
|
|
}
|
2014-04-16 00:48:25 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
readerNames = tReader;
|
|
|
|
}
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
if (allocateAtr && pcsc_cbAtrLen > 0 && pbAtr)
|
|
|
|
{
|
|
|
|
tATR = calloc(1, pcsc_cbAtrLen);
|
2014-04-16 00:48:25 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
if (!tATR)
|
|
|
|
{
|
|
|
|
status = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto out_fail;
|
2014-04-09 05:02:30 +04:00
|
|
|
}
|
2017-12-11 17:49:03 +03:00
|
|
|
|
|
|
|
atr = tATR;
|
2014-04-26 06:26:55 +04:00
|
|
|
}
|
2017-12-11 17:49:03 +03:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardStatus(hCard, readerNames, &pcsc_cchReaderLen, &pcsc_dwState,
|
|
|
|
&pcsc_dwProtocol, atr, &pcsc_cbAtrLen);
|
2017-12-11 17:49:03 +03:00
|
|
|
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
goto out_fail;
|
|
|
|
|
|
|
|
if (tATR)
|
2014-04-26 06:26:55 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
BYTE* pb;
|
|
|
|
BYTE** ppb;
|
|
|
|
} conv;
|
|
|
|
conv.pb = pbAtr;
|
2017-12-11 17:49:03 +03:00
|
|
|
PCSC_AddMemoryBlock(hContext, tATR);
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.ppb = tATR;
|
2014-04-26 06:26:55 +04:00
|
|
|
}
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2017-12-11 17:49:03 +03:00
|
|
|
if (tReader)
|
2014-04-26 06:26:55 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
CHAR* pc;
|
|
|
|
CHAR** ppc;
|
|
|
|
WCHAR* pw;
|
|
|
|
WCHAR** ppw;
|
|
|
|
} conv, dst;
|
|
|
|
dst.pc = mszReaderNames;
|
|
|
|
|
2017-12-14 17:46:14 +03:00
|
|
|
if (unicode)
|
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
int pcsc_cchReaderLenW =
|
|
|
|
ConvertToUnicode(CP_UTF8, 0, tReader, (int)*pcchReaderLen, &conv.pw, 0);
|
2017-12-14 17:46:14 +03:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if ((pcsc_cchReaderLenW <= 0) || (conv.pw == NULL))
|
2017-12-14 17:46:14 +03:00
|
|
|
{
|
|
|
|
status = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto out_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(tReader);
|
2020-02-20 15:42:54 +03:00
|
|
|
conv.pw[pcsc_cchReaderLen - 1] = L'\0';
|
|
|
|
PCSC_AddMemoryBlock(hContext, conv.pw);
|
|
|
|
*dst.ppw = conv.pw;
|
2017-12-14 17:46:14 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
tReader[pcsc_cchReaderLen - 1] = '\0';
|
2017-12-14 17:46:14 +03:00
|
|
|
PCSC_AddMemoryBlock(hContext, tReader);
|
2020-02-20 15:42:54 +03:00
|
|
|
*dst.ppc = tReader;
|
2017-12-14 17:46:14 +03:00
|
|
|
}
|
2014-04-26 06:26:55 +04:00
|
|
|
}
|
2014-04-13 07:38:15 +04:00
|
|
|
|
2014-05-03 21:49:50 +04:00
|
|
|
pcsc_dwState &= 0xFFFF;
|
2017-12-11 17:49:03 +03:00
|
|
|
|
|
|
|
if (pdwState)
|
2019-11-06 17:24:51 +03:00
|
|
|
*pdwState = PCSC_ConvertCardStateToWinSCard((DWORD)pcsc_dwState, status);
|
2017-12-11 17:49:03 +03:00
|
|
|
|
|
|
|
if (pdwProtocol)
|
2019-11-06 17:24:51 +03:00
|
|
|
*pdwProtocol = PCSC_ConvertProtocolsToWinSCard((DWORD)pcsc_dwProtocol);
|
2017-12-11 17:49:03 +03:00
|
|
|
|
|
|
|
if (pcbAtrLen)
|
2019-11-06 17:24:51 +03:00
|
|
|
*pcbAtrLen = (DWORD)pcsc_cbAtrLen;
|
2017-12-11 17:49:03 +03:00
|
|
|
|
|
|
|
if (pcchReaderLen)
|
2020-02-20 15:42:54 +03:00
|
|
|
*pcchReaderLen = pcsc_cchReaderLen + 1u;
|
2017-12-11 17:49:03 +03:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
return (LONG)status;
|
2017-12-11 17:49:03 +03:00
|
|
|
out_fail:
|
|
|
|
free(tReader);
|
|
|
|
free(tATR);
|
2020-02-20 15:42:54 +03:00
|
|
|
return (LONG)status;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardStatusA(SCARDHANDLE hCard, LPSTR mszReaderNames, LPDWORD pcchReaderLen,
|
|
|
|
LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr,
|
|
|
|
LPDWORD pcbAtrLen)
|
2014-04-13 07:38:15 +04:00
|
|
|
{
|
2020-02-17 12:53:02 +03:00
|
|
|
|
2017-12-14 17:46:14 +03:00
|
|
|
return PCSC_SCardStatus_Internal(hCard, mszReaderNames, pcchReaderLen, pdwState, pdwProtocol,
|
|
|
|
pbAtr, pcbAtrLen, FALSE);
|
2014-04-13 07:38:15 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardStatusW(SCARDHANDLE hCard, LPWSTR mszReaderNames,
|
|
|
|
LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol,
|
|
|
|
LPBYTE pbAtr, LPDWORD pcbAtrLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 12:53:02 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return PCSC_SCardStatus_Internal(hCard, (LPSTR)mszReaderNames, pcchReaderLen, pdwState,
|
2017-12-14 17:46:14 +03:00
|
|
|
pdwProtocol, pbAtr, pcbAtrLen, TRUE);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardTransmit(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pioSendPci,
|
|
|
|
LPCBYTE pbSendBuffer, DWORD cbSendLength,
|
|
|
|
LPSCARD_IO_REQUEST pioRecvPci, LPBYTE pbRecvBuffer,
|
|
|
|
LPDWORD pcbRecvLength)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
2014-05-03 22:20:17 +04:00
|
|
|
PCSC_DWORD cbExtraBytes = 0;
|
2014-08-18 21:34:47 +04:00
|
|
|
BYTE* pbExtraBytes = NULL;
|
|
|
|
BYTE* pcsc_pbExtraBytes = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_cbSendLength = (PCSC_DWORD)cbSendLength;
|
2014-05-03 22:20:17 +04:00
|
|
|
PCSC_DWORD pcsc_cbRecvLength = 0;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
const PCSC_SCARD_IO_REQUEST* pcs;
|
|
|
|
PCSC_SCARD_IO_REQUEST* ps;
|
|
|
|
LPSCARD_IO_REQUEST lps;
|
|
|
|
LPCSCARD_IO_REQUEST lpcs;
|
|
|
|
BYTE* pb;
|
|
|
|
} sendPci, recvPci, inRecvPci, inSendPci;
|
|
|
|
|
|
|
|
sendPci.ps = NULL;
|
|
|
|
recvPci.ps = NULL;
|
|
|
|
inRecvPci.lps = pioRecvPci;
|
|
|
|
inSendPci.lpcs = pioSendPci;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardTransmit)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardTransmit");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return SCARD_E_INVALID_VALUE;
|
|
|
|
|
|
|
|
PCSC_WaitForCardAccess(0, hCard, pCard->shared);
|
|
|
|
|
2014-05-03 22:20:17 +04:00
|
|
|
if (!pcbRecvLength)
|
|
|
|
return SCARD_E_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (*pcbRecvLength == SCARD_AUTOALLOCATE)
|
|
|
|
return SCARD_E_INVALID_PARAMETER;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pcsc_cbRecvLength = (PCSC_DWORD)*pcbRecvLength;
|
2014-05-03 22:20:17 +04:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if (!inSendPci.lpcs)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD dwState = 0;
|
|
|
|
PCSC_DWORD cbAtrLen = 0;
|
|
|
|
PCSC_DWORD dwProtocol = 0;
|
|
|
|
PCSC_DWORD cchReaderLen = 0;
|
2014-04-26 06:26:55 +04:00
|
|
|
/**
|
|
|
|
* pcsc-lite cannot have a null pioSendPci parameter, unlike WinSCard.
|
|
|
|
* Query the current protocol and use default SCARD_IO_REQUEST for it.
|
|
|
|
*/
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardStatus(hCard, NULL, &cchReaderLen, &dwState, &dwProtocol, NULL,
|
|
|
|
&cbAtrLen);
|
2014-04-13 08:43:12 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
|
|
|
if (dwProtocol == SCARD_PROTOCOL_T0)
|
2020-02-20 15:42:54 +03:00
|
|
|
sendPci.pcs = PCSC_SCARD_PCI_T0;
|
2014-04-26 06:26:55 +04:00
|
|
|
else if (dwProtocol == SCARD_PROTOCOL_T1)
|
2020-02-20 15:42:54 +03:00
|
|
|
sendPci.pcs = PCSC_SCARD_PCI_T1;
|
2014-05-03 22:20:17 +04:00
|
|
|
else if (dwProtocol == PCSC_SCARD_PROTOCOL_RAW)
|
2020-02-20 15:42:54 +03:00
|
|
|
sendPci.pcs = PCSC_SCARD_PCI_RAW;
|
2014-04-13 08:43:12 +04:00
|
|
|
}
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
2014-05-03 22:20:17 +04:00
|
|
|
else
|
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
cbExtraBytes = inSendPci.lpcs->cbPciLength - sizeof(SCARD_IO_REQUEST);
|
|
|
|
sendPci.ps = (PCSC_SCARD_IO_REQUEST*)malloc(sizeof(PCSC_SCARD_IO_REQUEST) + cbExtraBytes);
|
2014-05-03 22:20:17 +04:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if (!sendPci.ps)
|
2014-05-03 22:20:17 +04:00
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
sendPci.ps->dwProtocol = (PCSC_DWORD)inSendPci.lpcs->dwProtocol;
|
|
|
|
sendPci.ps->cbPciLength = sizeof(PCSC_SCARD_IO_REQUEST) + cbExtraBytes;
|
|
|
|
pbExtraBytes = &(inSendPci.pb)[sizeof(SCARD_IO_REQUEST)];
|
|
|
|
pcsc_pbExtraBytes = &(sendPci.pb)[sizeof(PCSC_SCARD_IO_REQUEST)];
|
2014-05-03 22:20:17 +04:00
|
|
|
CopyMemory(pcsc_pbExtraBytes, pbExtraBytes, cbExtraBytes);
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if (inRecvPci.lps)
|
2014-05-03 22:20:17 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
cbExtraBytes = inRecvPci.lps->cbPciLength - sizeof(SCARD_IO_REQUEST);
|
|
|
|
recvPci.ps = (PCSC_SCARD_IO_REQUEST*)malloc(sizeof(PCSC_SCARD_IO_REQUEST) + cbExtraBytes);
|
2014-05-03 22:20:17 +04:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if (!recvPci.ps)
|
2014-11-17 00:50:50 +03:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
if (inSendPci.lpcs)
|
|
|
|
free(sendPci.ps);
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2014-05-03 22:20:17 +04:00
|
|
|
return SCARD_E_NO_MEMORY;
|
2014-11-17 00:50:50 +03:00
|
|
|
}
|
2014-05-03 22:20:17 +04:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
recvPci.ps->dwProtocol = (PCSC_DWORD)inRecvPci.lps->dwProtocol;
|
|
|
|
recvPci.ps->cbPciLength = sizeof(PCSC_SCARD_IO_REQUEST) + cbExtraBytes;
|
|
|
|
pbExtraBytes = &(inRecvPci.pb)[sizeof(SCARD_IO_REQUEST)];
|
|
|
|
pcsc_pbExtraBytes = &(recvPci.pb)[sizeof(PCSC_SCARD_IO_REQUEST)];
|
2014-05-03 22:20:17 +04:00
|
|
|
CopyMemory(pcsc_pbExtraBytes, pbExtraBytes, cbExtraBytes);
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
status = g_PCSC.pfnSCardTransmit(hCard, sendPci.ps, pbSendBuffer, pcsc_cbSendLength, recvPci.ps,
|
|
|
|
pbRecvBuffer, &pcsc_cbRecvLength);
|
2020-02-17 16:07:20 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
*pcbRecvLength = (DWORD)pcsc_cbRecvLength;
|
2014-05-03 21:49:50 +04:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if (inSendPci.lpcs)
|
|
|
|
free(sendPci.ps); /* pcsc_pioSendPci is dynamically allocated only when pioSendPci is
|
2019-11-06 17:24:51 +03:00
|
|
|
non null */
|
2014-05-03 22:20:17 +04:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if (inRecvPci.lps)
|
2015-03-13 21:20:57 +03:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
cbExtraBytes = inRecvPci.lps->cbPciLength - sizeof(SCARD_IO_REQUEST);
|
|
|
|
pbExtraBytes = &(inRecvPci.pb)[sizeof(SCARD_IO_REQUEST)];
|
|
|
|
pcsc_pbExtraBytes = &(recvPci.pb)[sizeof(PCSC_SCARD_IO_REQUEST)];
|
2015-03-13 21:20:57 +03:00
|
|
|
CopyMemory(pbExtraBytes, pcsc_pbExtraBytes, cbExtraBytes); /* copy extra bytes */
|
2020-02-20 15:42:54 +03:00
|
|
|
free(recvPci.ps); /* pcsc_pioRecvPci is dynamically allocated only when pioRecvPci is
|
2019-11-06 17:24:51 +03:00
|
|
|
non null */
|
2015-03-13 21:20:57 +03:00
|
|
|
}
|
2014-05-03 22:20:17 +04:00
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
|
|
|
;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetTransmitCount(SCARDHANDLE hCard, LPDWORD pcTransmitCount)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2017-12-11 17:49:03 +03:00
|
|
|
WINPR_UNUSED(pcTransmitCount);
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
2020-02-17 12:53:02 +03:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return SCARD_E_INVALID_VALUE;
|
|
|
|
|
|
|
|
PCSC_WaitForCardAccess(0, hCard, pCard->shared);
|
|
|
|
return SCARD_S_SUCCESS;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID lpInBuffer,
|
|
|
|
DWORD cbInBufferSize, LPVOID lpOutBuffer,
|
|
|
|
DWORD cbOutBufferSize, LPDWORD lpBytesReturned)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-05-04 03:02:17 +04:00
|
|
|
DWORD IoCtlMethod = 0;
|
|
|
|
DWORD IoCtlFunction = 0;
|
|
|
|
DWORD IoCtlAccess = 0;
|
|
|
|
DWORD IoCtlDeviceType = 0;
|
|
|
|
BOOL getFeatureRequest = FALSE;
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
2014-05-04 03:02:17 +04:00
|
|
|
PCSC_DWORD pcsc_dwControlCode = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_cbInBufferSize = (PCSC_DWORD)cbInBufferSize;
|
|
|
|
PCSC_DWORD pcsc_cbOutBufferSize = (PCSC_DWORD)cbOutBufferSize;
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD pcsc_BytesReturned = 0;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardControl)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardControl");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return SCARD_E_INVALID_VALUE;
|
|
|
|
|
|
|
|
PCSC_WaitForCardAccess(0, hCard, pCard->shared);
|
2014-12-23 00:25:59 +03:00
|
|
|
/**
|
|
|
|
* PCSCv2 Part 10:
|
|
|
|
* http://www.pcscworkgroup.com/specifications/files/pcsc10_v2.02.09.pdf
|
|
|
|
*
|
|
|
|
* Smart Card Driver IOCTLs:
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/hardware/ff548988/
|
|
|
|
*
|
|
|
|
* Converting Windows Feature Request IOCTL code to the pcsc-lite control code:
|
|
|
|
* http://musclecard.996296.n3.nabble.com/Converting-Windows-Feature-Request-IOCTL-code-to-the-pcsc-lite-control-code-td4906.html
|
|
|
|
*/
|
2014-05-04 03:02:17 +04:00
|
|
|
IoCtlMethod = METHOD_FROM_CTL_CODE(dwControlCode);
|
|
|
|
IoCtlFunction = FUNCTION_FROM_CTL_CODE(dwControlCode);
|
|
|
|
IoCtlAccess = ACCESS_FROM_CTL_CODE(dwControlCode);
|
|
|
|
IoCtlDeviceType = DEVICE_TYPE_FROM_CTL_CODE(dwControlCode);
|
|
|
|
|
2014-12-23 00:25:59 +03:00
|
|
|
if (dwControlCode == IOCTL_SMARTCARD_GET_FEATURE_REQUEST)
|
2014-05-04 03:02:17 +04:00
|
|
|
getFeatureRequest = TRUE;
|
|
|
|
|
|
|
|
if (IoCtlDeviceType == FILE_DEVICE_SMARTCARD)
|
|
|
|
dwControlCode = PCSC_SCARD_CTL_CODE(IoCtlFunction);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pcsc_dwControlCode = (PCSC_DWORD)dwControlCode;
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardControl(hCard, pcsc_dwControlCode, lpInBuffer, pcsc_cbInBufferSize,
|
|
|
|
lpOutBuffer, pcsc_cbOutBufferSize, &pcsc_BytesReturned);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
*lpBytesReturned = (DWORD)pcsc_BytesReturned;
|
2014-05-03 21:49:50 +04:00
|
|
|
|
2014-05-04 03:02:17 +04:00
|
|
|
if (getFeatureRequest)
|
|
|
|
{
|
2014-12-23 00:25:59 +03:00
|
|
|
UINT32 index;
|
|
|
|
UINT32 count;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_TLV_STRUCTURE* tlv = (PCSC_TLV_STRUCTURE*)lpOutBuffer;
|
2014-05-04 03:02:17 +04:00
|
|
|
|
2014-12-23 00:25:59 +03:00
|
|
|
if ((*lpBytesReturned % sizeof(PCSC_TLV_STRUCTURE)) != 0)
|
|
|
|
return SCARD_E_UNEXPECTED;
|
|
|
|
|
|
|
|
count = *lpBytesReturned / sizeof(PCSC_TLV_STRUCTURE);
|
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
2014-05-04 03:02:17 +04:00
|
|
|
{
|
2014-12-23 00:25:59 +03:00
|
|
|
if (tlv[index].length != 4)
|
|
|
|
return SCARD_E_UNEXPECTED;
|
2014-05-04 03:02:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
|
|
|
;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetAttrib_Internal(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr,
|
|
|
|
LPDWORD pcbAttrLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2014-04-26 06:26:55 +04:00
|
|
|
SCARDCONTEXT hContext = 0;
|
2014-05-03 21:49:50 +04:00
|
|
|
BOOL pcbAttrLenAlloc = FALSE;
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_dwAttrId = (PCSC_DWORD)dwAttrId;
|
2014-05-03 21:49:50 +04:00
|
|
|
PCSC_DWORD pcsc_cbAttrLen = 0;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
BYTE* pb;
|
|
|
|
BYTE** ppb;
|
|
|
|
} conv;
|
|
|
|
conv.pb = pbAttr;
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardGetAttrib)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardGetAttrib");
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return SCARD_E_INVALID_VALUE;
|
|
|
|
|
|
|
|
PCSC_WaitForCardAccess(0, hCard, pCard->shared);
|
2014-04-26 06:26:55 +04:00
|
|
|
hContext = PCSC_GetCardContextFromHandle(hCard);
|
2014-04-16 00:48:25 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!hContext)
|
2014-05-03 21:49:50 +04:00
|
|
|
return SCARD_E_INVALID_HANDLE;
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2014-05-03 21:49:50 +04:00
|
|
|
if (!pbAttr || !pcbAttrLen)
|
|
|
|
return SCARD_E_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (*pcbAttrLen == SCARD_AUTOALLOCATE)
|
|
|
|
pcbAttrLenAlloc = TRUE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pcsc_cbAttrLen = pcbAttrLenAlloc ? PCSC_SCARD_AUTOALLOCATE : (PCSC_DWORD)*pcbAttrLen;
|
2014-05-03 21:49:50 +04:00
|
|
|
|
|
|
|
if (pcbAttrLenAlloc && !g_SCardAutoAllocate)
|
2014-04-26 06:26:55 +04:00
|
|
|
{
|
2014-05-03 21:49:50 +04:00
|
|
|
pcsc_cbAttrLen = 0;
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, NULL, &pcsc_cbAttrLen);
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.ppb = (BYTE*)calloc(1, pcsc_cbAttrLen);
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
if (!*conv.ppb)
|
2014-04-26 06:26:55 +04:00
|
|
|
return SCARD_E_NO_MEMORY;
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
status = g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, *conv.ppb, &pcsc_cbAttrLen);
|
2014-04-09 05:02:30 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (status != SCARD_S_SUCCESS)
|
2020-02-20 15:42:54 +03:00
|
|
|
free(*conv.ppb);
|
2014-04-26 06:26:55 +04:00
|
|
|
else
|
2020-02-20 15:42:54 +03:00
|
|
|
PCSC_AddMemoryBlock(hContext, *conv.ppb);
|
2014-04-26 06:26:55 +04:00
|
|
|
}
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
2014-04-26 06:26:55 +04:00
|
|
|
else
|
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardGetAttrib(hCard, pcsc_dwAttrId, pbAttr, &pcsc_cbAttrLen);
|
2014-04-26 06:26:55 +04:00
|
|
|
}
|
|
|
|
|
2015-01-19 22:56:02 +03:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
2019-11-06 17:24:51 +03:00
|
|
|
*pcbAttrLen = (DWORD)pcsc_cbAttrLen;
|
2020-02-17 16:07:20 +03:00
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetAttrib_FriendlyName(SCARDHANDLE hCard, DWORD dwAttrId,
|
|
|
|
LPBYTE pbAttr, LPDWORD pcbAttrLen)
|
2014-05-04 01:07:30 +04:00
|
|
|
{
|
2019-02-07 16:36:17 +03:00
|
|
|
size_t length = 0;
|
2015-04-16 22:56:57 +03:00
|
|
|
char* namePCSC = NULL;
|
2014-05-06 01:27:29 +04:00
|
|
|
DWORD cbAttrLen = 0;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* pbAttrA = NULL;
|
|
|
|
WCHAR* pbAttrW = NULL;
|
2014-05-04 01:07:30 +04:00
|
|
|
SCARDCONTEXT hContext;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* friendlyNameA = NULL;
|
|
|
|
WCHAR* friendlyNameW = NULL;
|
2014-05-04 01:07:30 +04:00
|
|
|
LONG status = SCARD_S_SUCCESS;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
WCHAR** ppw;
|
|
|
|
BYTE* pb;
|
|
|
|
BYTE** ppb;
|
|
|
|
} conv;
|
|
|
|
conv.pb = pbAttr;
|
2014-05-04 01:07:30 +04:00
|
|
|
hContext = PCSC_GetCardContextFromHandle(hCard);
|
|
|
|
|
|
|
|
if (!hContext)
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
|
|
|
|
2014-05-06 01:27:29 +04:00
|
|
|
cbAttrLen = *pcbAttrLen;
|
2014-05-04 01:07:30 +04:00
|
|
|
*pcbAttrLen = SCARD_AUTOALLOCATE;
|
2019-11-06 17:24:51 +03:00
|
|
|
status = PCSC_SCardGetAttrib_Internal(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME_A,
|
|
|
|
(LPBYTE)&pbAttrA, pcbAttrLen);
|
2014-05-04 01:07:30 +04:00
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
|
|
|
{
|
|
|
|
pbAttrA = NULL;
|
|
|
|
*pcbAttrLen = SCARD_AUTOALLOCATE;
|
2019-11-06 17:24:51 +03:00
|
|
|
status = PCSC_SCardGetAttrib_Internal(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME_W,
|
|
|
|
(LPBYTE)&pbAttrW, pcbAttrLen);
|
2014-05-04 01:07:30 +04:00
|
|
|
|
|
|
|
if (status != SCARD_S_SUCCESS)
|
|
|
|
return status;
|
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
ConvertFromUnicode(CP_UTF8, 0, (WCHAR*)pbAttrW, (int)*pcbAttrLen, (char**)&pbAttrA, 0, NULL,
|
2017-12-11 17:49:03 +03:00
|
|
|
NULL);
|
2014-05-04 01:07:30 +04:00
|
|
|
namePCSC = pbAttrA;
|
|
|
|
PCSC_SCardFreeMemory_Internal(hContext, pbAttrW);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
namePCSC = _strdup(pbAttrA);
|
|
|
|
|
|
|
|
if (!namePCSC)
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
|
|
|
|
PCSC_SCardFreeMemory_Internal(hContext, pbAttrA);
|
|
|
|
}
|
|
|
|
|
|
|
|
length = strlen(namePCSC);
|
2017-12-11 17:49:03 +03:00
|
|
|
friendlyNameA = namePCSC;
|
|
|
|
namePCSC = NULL;
|
2014-05-04 01:07:30 +04:00
|
|
|
|
|
|
|
if (dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_W)
|
|
|
|
{
|
2014-12-28 22:56:13 +03:00
|
|
|
/* length here includes null terminator */
|
2020-02-20 15:42:54 +03:00
|
|
|
int rc = ConvertToUnicode(CP_UTF8, 0, (char*)friendlyNameA, -1, &friendlyNameW, 0);
|
2014-05-04 01:07:30 +04:00
|
|
|
free(friendlyNameA);
|
2020-02-20 15:42:54 +03:00
|
|
|
if ((rc < 0) || (!friendlyNameW))
|
2016-05-12 11:01:30 +03:00
|
|
|
{
|
|
|
|
free(namePCSC);
|
2014-05-04 01:07:30 +04:00
|
|
|
return SCARD_E_NO_MEMORY;
|
2016-05-12 11:01:30 +03:00
|
|
|
}
|
2020-02-20 15:42:54 +03:00
|
|
|
length = (size_t)rc;
|
2014-05-04 01:07:30 +04:00
|
|
|
|
2014-05-06 01:27:29 +04:00
|
|
|
if (cbAttrLen == SCARD_AUTOALLOCATE)
|
2014-05-04 01:07:30 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.ppw = friendlyNameW;
|
2014-05-04 01:07:30 +04:00
|
|
|
*pcbAttrLen = length * 2;
|
2020-02-20 15:42:54 +03:00
|
|
|
PCSC_AddMemoryBlock(hContext, *conv.ppb);
|
2014-05-04 01:07:30 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-28 22:56:13 +03:00
|
|
|
if ((length * 2) > cbAttrLen)
|
2014-05-04 01:07:30 +04:00
|
|
|
{
|
|
|
|
free(friendlyNameW);
|
2016-05-12 11:01:30 +03:00
|
|
|
free(namePCSC);
|
2014-05-04 01:07:30 +04:00
|
|
|
return SCARD_E_INSUFFICIENT_BUFFER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
CopyMemory(pbAttr, (BYTE*)friendlyNameW, (length * 2));
|
2014-05-04 01:07:30 +04:00
|
|
|
*pcbAttrLen = length * 2;
|
|
|
|
free(friendlyNameW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-06 01:27:29 +04:00
|
|
|
if (cbAttrLen == SCARD_AUTOALLOCATE)
|
2014-05-04 01:07:30 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.ppb = (BYTE*)friendlyNameA;
|
2014-05-04 01:07:30 +04:00
|
|
|
*pcbAttrLen = length;
|
2020-02-20 15:42:54 +03:00
|
|
|
PCSC_AddMemoryBlock(hContext, *conv.ppb);
|
2014-05-04 01:07:30 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-06 01:27:29 +04:00
|
|
|
if ((length + 1) > cbAttrLen)
|
2014-05-04 01:07:30 +04:00
|
|
|
{
|
|
|
|
free(friendlyNameA);
|
2016-05-12 11:01:30 +03:00
|
|
|
free(namePCSC);
|
2014-05-04 01:07:30 +04:00
|
|
|
return SCARD_E_INSUFFICIENT_BUFFER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
CopyMemory(pbAttr, (BYTE*)friendlyNameA, length + 1);
|
2014-05-04 01:07:30 +04:00
|
|
|
*pcbAttrLen = length;
|
|
|
|
free(friendlyNameA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-28 22:56:13 +03:00
|
|
|
free(namePCSC);
|
2014-05-04 01:07:30 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr,
|
|
|
|
LPDWORD pcbAttrLen)
|
2014-04-09 07:52:24 +04:00
|
|
|
{
|
2014-04-12 00:27:01 +04:00
|
|
|
DWORD cbAttrLen;
|
|
|
|
SCARDCONTEXT hContext;
|
2014-05-03 21:49:50 +04:00
|
|
|
BOOL pcbAttrLenAlloc = FALSE;
|
2014-04-09 07:52:24 +04:00
|
|
|
LONG status = SCARD_S_SUCCESS;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
BYTE* pb;
|
|
|
|
BYTE** ppb;
|
|
|
|
} conv;
|
|
|
|
|
|
|
|
conv.pb = pbAttr;
|
|
|
|
|
2014-04-12 00:27:01 +04:00
|
|
|
cbAttrLen = *pcbAttrLen;
|
2014-04-09 07:52:24 +04:00
|
|
|
|
2014-04-12 00:27:01 +04:00
|
|
|
if (*pcbAttrLen == SCARD_AUTOALLOCATE)
|
2014-12-22 21:28:16 +03:00
|
|
|
{
|
2014-05-03 21:49:50 +04:00
|
|
|
pcbAttrLenAlloc = TRUE;
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.ppb = NULL;
|
2014-12-22 21:28:16 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* pcsc-lite returns SCARD_E_INSUFFICIENT_BUFFER if the given
|
|
|
|
* buffer size is larger than PCSC_MAX_BUFFER_SIZE (264)
|
|
|
|
*/
|
|
|
|
if (*pcbAttrLen > PCSC_MAX_BUFFER_SIZE)
|
|
|
|
*pcbAttrLen = PCSC_MAX_BUFFER_SIZE;
|
|
|
|
}
|
2014-04-16 01:51:39 +04:00
|
|
|
|
2014-04-12 00:27:01 +04:00
|
|
|
hContext = PCSC_GetCardContextFromHandle(hCard);
|
2014-04-09 07:52:24 +04:00
|
|
|
|
2014-05-04 01:07:30 +04:00
|
|
|
if (!hContext)
|
|
|
|
return SCARD_E_INVALID_HANDLE;
|
|
|
|
|
2015-06-30 18:42:01 +03:00
|
|
|
if ((dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_A) ||
|
|
|
|
(dwAttrId == SCARD_ATTR_DEVICE_FRIENDLY_NAME_W))
|
2014-05-04 01:07:30 +04:00
|
|
|
{
|
2014-05-06 01:27:29 +04:00
|
|
|
status = PCSC_SCardGetAttrib_FriendlyName(hCard, dwAttrId, pbAttr, pcbAttrLen);
|
|
|
|
return status;
|
2014-05-04 01:07:30 +04:00
|
|
|
}
|
|
|
|
|
2014-04-12 00:27:01 +04:00
|
|
|
status = PCSC_SCardGetAttrib_Internal(hCard, dwAttrId, pbAttr, pcbAttrLen);
|
2014-04-09 07:52:24 +04:00
|
|
|
|
2014-04-12 00:27:01 +04:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
|
|
|
if (dwAttrId == SCARD_ATTR_VENDOR_NAME)
|
|
|
|
{
|
2014-12-22 21:28:16 +03:00
|
|
|
const char* vendorName;
|
|
|
|
|
2014-04-12 00:27:01 +04:00
|
|
|
/**
|
|
|
|
* pcsc-lite adds a null terminator to the vendor name,
|
|
|
|
* while WinSCard doesn't. Strip the null terminator.
|
|
|
|
*/
|
2014-12-22 21:28:16 +03:00
|
|
|
|
2014-05-03 21:49:50 +04:00
|
|
|
if (pcbAttrLenAlloc)
|
2020-02-20 15:42:54 +03:00
|
|
|
vendorName = (char*)*conv.ppb;
|
2014-12-22 21:28:16 +03:00
|
|
|
else
|
2019-11-06 17:24:51 +03:00
|
|
|
vendorName = (char*)pbAttr;
|
2014-12-22 21:28:16 +03:00
|
|
|
|
|
|
|
if (vendorName)
|
|
|
|
*pcbAttrLen = strlen(vendorName);
|
2014-04-12 00:27:01 +04:00
|
|
|
else
|
2014-12-22 21:28:16 +03:00
|
|
|
*pcbAttrLen = 0;
|
2014-04-12 00:27:01 +04:00
|
|
|
}
|
|
|
|
}
|
2014-04-16 01:51:39 +04:00
|
|
|
else
|
2014-04-12 00:27:01 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
|
2014-05-04 01:07:30 +04:00
|
|
|
if (dwAttrId == SCARD_ATTR_CURRENT_PROTOCOL_TYPE)
|
2014-04-16 02:01:26 +04:00
|
|
|
{
|
2015-01-19 22:56:02 +03:00
|
|
|
if (!pcbAttrLenAlloc)
|
|
|
|
{
|
|
|
|
PCSC_DWORD dwState = 0;
|
|
|
|
PCSC_DWORD cbAtrLen = 0;
|
|
|
|
PCSC_DWORD dwProtocol = 0;
|
|
|
|
PCSC_DWORD cchReaderLen = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
status = (LONG)g_PCSC.pfnSCardStatus(hCard, NULL, &cchReaderLen, &dwState,
|
|
|
|
&dwProtocol, NULL, &cbAtrLen);
|
2014-04-16 02:01:26 +04:00
|
|
|
|
2015-01-19 22:56:02 +03:00
|
|
|
if (status == SCARD_S_SUCCESS)
|
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
BYTE* pb;
|
|
|
|
LPDWORD pd;
|
|
|
|
} conv;
|
|
|
|
|
|
|
|
conv.pb = pbAttr;
|
2015-01-19 22:56:02 +03:00
|
|
|
|
|
|
|
if (cbAttrLen < sizeof(DWORD))
|
|
|
|
return SCARD_E_INSUFFICIENT_BUFFER;
|
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.pd = PCSC_ConvertProtocolsToWinSCard(dwProtocol);
|
2015-01-19 22:56:02 +03:00
|
|
|
*pcbAttrLen = sizeof(DWORD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CHANNEL_ID)
|
|
|
|
{
|
|
|
|
if (!pcbAttrLenAlloc)
|
2014-04-16 02:01:26 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
UINT32 channelType = 0x20; /* USB */
|
|
|
|
UINT32 channelNumber = 0;
|
|
|
|
union {
|
|
|
|
BYTE* pb;
|
|
|
|
BYTE** ppb;
|
|
|
|
DWORD* ppd;
|
|
|
|
} conv;
|
|
|
|
|
|
|
|
conv.pb = pbAttr;
|
2014-05-06 21:00:44 +04:00
|
|
|
if (cbAttrLen < sizeof(DWORD))
|
2014-04-16 02:01:26 +04:00
|
|
|
return SCARD_E_INSUFFICIENT_BUFFER;
|
|
|
|
|
2015-01-19 22:56:02 +03:00
|
|
|
status = SCARD_S_SUCCESS;
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.ppd = (channelType << 16u) | channelNumber;
|
2014-05-06 21:00:44 +04:00
|
|
|
*pcbAttrLen = sizeof(DWORD);
|
2014-04-16 02:01:26 +04:00
|
|
|
}
|
|
|
|
}
|
2014-04-16 02:45:38 +04:00
|
|
|
else if (dwAttrId == SCARD_ATTR_VENDOR_IFD_TYPE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_DEFAULT_CLK)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_DEFAULT_DATA_RATE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_MAX_CLK)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_MAX_DATA_RATE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_MAX_IFSD)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CHARACTERISTICS)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_DEVICE_SYSTEM_NAME_A)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_DEVICE_UNIT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_POWER_MGMT_SUPPORT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CURRENT_CLK)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CURRENT_F)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CURRENT_D)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CURRENT_N)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CURRENT_CWT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CURRENT_BWT)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CURRENT_IFSC)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CURRENT_EBC_ENCODING)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_CURRENT_IFSD)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (dwAttrId == SCARD_ATTR_ICC_TYPE_PER_ATR)
|
|
|
|
{
|
|
|
|
}
|
2014-04-09 07:52:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr,
|
|
|
|
DWORD cbAttrLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 16:07:20 +03:00
|
|
|
PCSC_LONG status = SCARD_S_SUCCESS;
|
2015-01-21 05:40:45 +03:00
|
|
|
PCSC_SCARDHANDLE* pCard = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_DWORD pcsc_dwAttrId = (PCSC_DWORD)dwAttrId;
|
|
|
|
PCSC_DWORD pcsc_cbAttrLen = (PCSC_DWORD)cbAttrLen;
|
2014-04-06 00:06:46 +04:00
|
|
|
|
2014-04-26 06:26:55 +04:00
|
|
|
if (!g_PCSC.pfnSCardSetAttrib)
|
2020-01-09 10:20:42 +03:00
|
|
|
return PCSC_SCard_LogError("g_PCSC.pfnSCardSetAttrib");
|
2014-04-26 06:26:55 +04:00
|
|
|
|
2015-01-21 05:40:45 +03:00
|
|
|
pCard = PCSC_GetCardHandleData(hCard);
|
|
|
|
|
|
|
|
if (!pCard)
|
|
|
|
return SCARD_E_INVALID_VALUE;
|
|
|
|
|
|
|
|
PCSC_WaitForCardAccess(0, hCard, pCard->shared);
|
2020-02-17 16:07:20 +03:00
|
|
|
status = g_PCSC.pfnSCardSetAttrib(hCard, pcsc_dwAttrId, pbAttr, pcsc_cbAttrLen);
|
|
|
|
return PCSC_MapErrorCodeToWinSCard(status);
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardUIDlgSelectCardA(LPOPENCARDNAMEA_EX pDlgStruc)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(pDlgStruc);
|
2020-02-17 12:53:02 +03:00
|
|
|
|
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardUIDlgSelectCardW(LPOPENCARDNAMEW_EX pDlgStruc)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(pDlgStruc);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_GetOpenCardNameA(LPOPENCARDNAMEA pDlgStruc)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(pDlgStruc);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_GetOpenCardNameW(LPOPENCARDNAMEW pDlgStruc)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(pDlgStruc);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardDlgExtendedError(void)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 12:53:02 +03:00
|
|
|
|
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* card_id_and_name_a(const UUID* CardIdentifier, LPCSTR LookupName)
|
|
|
|
{
|
|
|
|
size_t len = strlen(LookupName) + 34;
|
|
|
|
char* id = malloc(len);
|
|
|
|
if (!id)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
snprintf(id, len, "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X\\%s", CardIdentifier->Data1,
|
|
|
|
CardIdentifier->Data2, CardIdentifier->Data3, CardIdentifier->Data4[0],
|
|
|
|
CardIdentifier->Data4[1], CardIdentifier->Data4[2], CardIdentifier->Data4[3],
|
|
|
|
CardIdentifier->Data4[4], CardIdentifier->Data4[5], CardIdentifier->Data4[6],
|
|
|
|
CardIdentifier->Data4[7], LookupName);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* card_id_and_name_w(const UUID* CardIdentifier, LPCWSTR LookupName)
|
|
|
|
{
|
|
|
|
char* res;
|
|
|
|
char* tmp = NULL;
|
|
|
|
|
|
|
|
ConvertFromUnicode(CP_UTF8, 0, LookupName, -1, &tmp, 0, NULL, NULL);
|
|
|
|
res = card_id_and_name_a(CardIdentifier, tmp);
|
|
|
|
free(tmp);
|
|
|
|
return res;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardReadCacheA(SCARDCONTEXT hContext, UUID* CardIdentifier,
|
|
|
|
DWORD FreshnessCounter, LPSTR LookupName, PBYTE Data,
|
|
|
|
DWORD* DataLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 12:53:02 +03:00
|
|
|
PCSC_CACHE_ITEM* data;
|
|
|
|
PCSC_SCARDCONTEXT* ctx = PCSC_GetCardContextData(hContext);
|
|
|
|
char* id = card_id_and_name_a(CardIdentifier, LookupName);
|
|
|
|
|
|
|
|
data = HashTable_GetItemValue(ctx->cache, id);
|
|
|
|
free(id);
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
*DataLen = 0;
|
2020-04-20 14:42:02 +03:00
|
|
|
return SCARD_W_CACHE_ITEM_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FreshnessCounter != data->freshness)
|
|
|
|
{
|
|
|
|
*DataLen = 0;
|
|
|
|
return SCARD_W_CACHE_ITEM_STALE;
|
2020-02-17 12:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*DataLen == SCARD_AUTOALLOCATE)
|
|
|
|
{
|
|
|
|
BYTE* mem;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
BYTE* pb;
|
|
|
|
BYTE** ppb;
|
|
|
|
} conv;
|
|
|
|
|
|
|
|
conv.pb = Data;
|
2020-02-17 12:53:02 +03:00
|
|
|
mem = calloc(1, data->len);
|
|
|
|
if (!mem)
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
|
|
|
|
if (!PCSC_AddMemoryBlock(hContext, mem))
|
2020-02-18 14:50:29 +03:00
|
|
|
{
|
|
|
|
free(mem);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_NO_MEMORY;
|
2020-02-18 14:50:29 +03:00
|
|
|
}
|
2020-02-17 12:53:02 +03:00
|
|
|
|
|
|
|
memcpy(mem, data->data, data->len);
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.ppb = mem;
|
2020-02-17 12:53:02 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy(Data, data->data, data->len);
|
|
|
|
*DataLen = data->len;
|
|
|
|
return SCARD_S_SUCCESS;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardReadCacheW(SCARDCONTEXT hContext, UUID* CardIdentifier,
|
|
|
|
DWORD FreshnessCounter, LPWSTR LookupName, PBYTE Data,
|
|
|
|
DWORD* DataLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 12:53:02 +03:00
|
|
|
PCSC_CACHE_ITEM* data;
|
|
|
|
PCSC_SCARDCONTEXT* ctx = PCSC_GetCardContextData(hContext);
|
|
|
|
char* id = card_id_and_name_w(CardIdentifier, LookupName);
|
|
|
|
|
|
|
|
data = HashTable_GetItemValue(ctx->cache, id);
|
|
|
|
free(id);
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
*DataLen = 0;
|
2020-04-20 14:42:02 +03:00
|
|
|
return SCARD_W_CACHE_ITEM_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FreshnessCounter != data->freshness)
|
|
|
|
{
|
|
|
|
*DataLen = 0;
|
|
|
|
return SCARD_W_CACHE_ITEM_STALE;
|
2020-02-17 12:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*DataLen == SCARD_AUTOALLOCATE)
|
|
|
|
{
|
|
|
|
BYTE* mem;
|
2020-02-20 15:42:54 +03:00
|
|
|
union {
|
|
|
|
BYTE* pb;
|
|
|
|
BYTE** ppb;
|
|
|
|
} conv;
|
|
|
|
|
|
|
|
conv.pb = Data;
|
2020-02-17 12:53:02 +03:00
|
|
|
mem = calloc(1, data->len);
|
|
|
|
if (!mem)
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
|
|
|
|
if (!PCSC_AddMemoryBlock(hContext, mem))
|
2020-02-18 14:50:29 +03:00
|
|
|
{
|
|
|
|
free(mem);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_NO_MEMORY;
|
2020-02-18 14:50:29 +03:00
|
|
|
}
|
2020-02-17 12:53:02 +03:00
|
|
|
|
|
|
|
memcpy(mem, data->data, data->len);
|
2020-02-20 15:42:54 +03:00
|
|
|
*conv.ppb = mem;
|
2020-02-17 12:53:02 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy(Data, data->data, data->len);
|
|
|
|
*DataLen = data->len;
|
|
|
|
return SCARD_S_SUCCESS;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardWriteCacheA(SCARDCONTEXT hContext, UUID* CardIdentifier,
|
|
|
|
DWORD FreshnessCounter, LPSTR LookupName, PBYTE Data,
|
|
|
|
DWORD DataLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 12:53:02 +03:00
|
|
|
PCSC_CACHE_ITEM* data;
|
|
|
|
PCSC_SCARDCONTEXT* ctx = PCSC_GetCardContextData(hContext);
|
|
|
|
char* id = card_id_and_name_a(CardIdentifier, LookupName);
|
|
|
|
|
|
|
|
if (!id)
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
|
|
|
|
data = malloc(sizeof(PCSC_CACHE_ITEM));
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
free(id);
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
}
|
|
|
|
data->data = malloc(DataLen);
|
|
|
|
if (!data->data)
|
|
|
|
{
|
|
|
|
free(id);
|
|
|
|
free(data);
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
}
|
|
|
|
data->len = DataLen;
|
2020-04-20 14:42:02 +03:00
|
|
|
data->freshness = FreshnessCounter;
|
2020-02-17 12:53:02 +03:00
|
|
|
memcpy(data->data, Data, data->len);
|
|
|
|
|
|
|
|
HashTable_Remove(ctx->cache, id);
|
|
|
|
HashTable_Add(ctx->cache, id, data);
|
2020-02-17 16:07:20 +03:00
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_S_SUCCESS;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardWriteCacheW(SCARDCONTEXT hContext, UUID* CardIdentifier,
|
|
|
|
DWORD FreshnessCounter, LPWSTR LookupName, PBYTE Data,
|
|
|
|
DWORD DataLen)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 12:53:02 +03:00
|
|
|
PCSC_CACHE_ITEM* data;
|
|
|
|
PCSC_SCARDCONTEXT* ctx = PCSC_GetCardContextData(hContext);
|
|
|
|
char* id = card_id_and_name_w(CardIdentifier, LookupName);
|
|
|
|
|
|
|
|
if (!id)
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
|
|
|
|
data = malloc(sizeof(PCSC_CACHE_ITEM));
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
free(id);
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
}
|
|
|
|
data->data = malloc(DataLen);
|
|
|
|
if (!data->data)
|
|
|
|
{
|
|
|
|
free(id);
|
|
|
|
free(data);
|
|
|
|
return SCARD_E_NO_MEMORY;
|
|
|
|
}
|
|
|
|
data->len = DataLen;
|
2020-04-20 14:42:02 +03:00
|
|
|
data->freshness = FreshnessCounter;
|
2020-02-17 12:53:02 +03:00
|
|
|
memcpy(data->data, Data, data->len);
|
|
|
|
|
|
|
|
HashTable_Remove(ctx->cache, id);
|
|
|
|
HashTable_Add(ctx->cache, id, data);
|
2020-02-17 16:07:20 +03:00
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_S_SUCCESS;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetReaderIconA(SCARDCONTEXT hContext, LPCSTR szReaderName,
|
|
|
|
LPBYTE pbIcon, LPDWORD pcbIcon)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(pbIcon);
|
|
|
|
WINPR_UNUSED(pcbIcon);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetReaderIconW(SCARDCONTEXT hContext, LPCWSTR szReaderName,
|
|
|
|
LPBYTE pbIcon, LPDWORD pcbIcon)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(pbIcon);
|
|
|
|
WINPR_UNUSED(pcbIcon);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetDeviceTypeIdA(SCARDCONTEXT hContext, LPCSTR szReaderName,
|
|
|
|
LPDWORD pdwDeviceTypeId)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(pdwDeviceTypeId);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetDeviceTypeIdW(SCARDCONTEXT hContext, LPCWSTR szReaderName,
|
|
|
|
LPDWORD pdwDeviceTypeId)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
2020-02-17 12:53:02 +03:00
|
|
|
if (pdwDeviceTypeId)
|
|
|
|
*pdwDeviceTypeId = 0;
|
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetReaderDeviceInstanceIdA(SCARDCONTEXT hContext, LPCSTR szReaderName,
|
|
|
|
LPSTR szDeviceInstanceId,
|
|
|
|
LPDWORD pcchDeviceInstanceId)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(szDeviceInstanceId);
|
|
|
|
WINPR_UNUSED(pcchDeviceInstanceId);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardGetReaderDeviceInstanceIdW(SCARDCONTEXT hContext, LPCWSTR szReaderName,
|
|
|
|
LPWSTR szDeviceInstanceId,
|
|
|
|
LPDWORD pcchDeviceInstanceId)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szReaderName);
|
|
|
|
WINPR_UNUSED(szDeviceInstanceId);
|
|
|
|
WINPR_UNUSED(pcchDeviceInstanceId);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListReadersWithDeviceInstanceIdA(SCARDCONTEXT hContext,
|
|
|
|
LPCSTR szDeviceInstanceId,
|
|
|
|
LPSTR mszReaders, LPDWORD pcchReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szDeviceInstanceId);
|
|
|
|
WINPR_UNUSED(mszReaders);
|
|
|
|
WINPR_UNUSED(pcchReaders);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardListReadersWithDeviceInstanceIdW(SCARDCONTEXT hContext,
|
|
|
|
LPCWSTR szDeviceInstanceId,
|
|
|
|
LPWSTR mszReaders,
|
|
|
|
LPDWORD pcchReaders)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(szDeviceInstanceId);
|
|
|
|
WINPR_UNUSED(mszReaders);
|
|
|
|
WINPR_UNUSED(pcchReaders);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:42 +03:00
|
|
|
static LONG WINAPI PCSC_SCardAudit(SCARDCONTEXT hContext, DWORD dwEvent)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
2020-02-17 12:53:02 +03:00
|
|
|
|
2020-02-20 15:42:54 +03:00
|
|
|
WINPR_UNUSED(hContext);
|
|
|
|
WINPR_UNUSED(dwEvent);
|
2020-02-17 12:53:02 +03:00
|
|
|
return SCARD_E_UNSUPPORTED_FEATURE;
|
2014-04-04 23:30:03 +04:00
|
|
|
}
|
|
|
|
|
2014-12-13 16:27:57 +03:00
|
|
|
#ifdef __MACOSX__
|
2017-11-14 18:10:52 +03:00
|
|
|
unsigned int determineMacOSXVersion(void)
|
2014-12-13 16:27:57 +03:00
|
|
|
{
|
2015-06-30 18:42:01 +03:00
|
|
|
int mib[2];
|
|
|
|
size_t len = 0;
|
|
|
|
char* kernelVersion = NULL;
|
|
|
|
char* tok = NULL;
|
|
|
|
unsigned int version = 0;
|
2017-11-14 18:10:52 +03:00
|
|
|
long majorVersion = 0;
|
|
|
|
long minorVersion = 0;
|
|
|
|
long patchVersion = 0;
|
2015-06-30 18:42:01 +03:00
|
|
|
int count = 0;
|
2020-05-18 12:18:55 +03:00
|
|
|
char* context = NULL;
|
2015-06-30 18:42:01 +03:00
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_OSRELEASE;
|
|
|
|
|
|
|
|
if (sysctl(mib, 2, NULL, &len, NULL, 0) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
kernelVersion = calloc(len, sizeof(char));
|
|
|
|
|
|
|
|
if (!kernelVersion)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (sysctl(mib, 2, kernelVersion, &len, NULL, 0) != 0)
|
|
|
|
{
|
|
|
|
free(kernelVersion);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-18 12:18:55 +03:00
|
|
|
tok = strtok_s(kernelVersion, ".", &context);
|
2017-11-14 18:10:52 +03:00
|
|
|
errno = 0;
|
2015-06-30 18:42:01 +03:00
|
|
|
|
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
switch (count)
|
|
|
|
{
|
|
|
|
case 0:
|
2017-11-14 18:10:52 +03:00
|
|
|
majorVersion = strtol(tok, NULL, 0);
|
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
goto fail;
|
|
|
|
|
2015-06-30 18:42:01 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2017-11-14 18:10:52 +03:00
|
|
|
minorVersion = strtol(tok, NULL, 0);
|
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
goto fail;
|
|
|
|
|
2015-06-30 18:42:01 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2017-11-14 18:10:52 +03:00
|
|
|
patchVersion = strtol(tok, NULL, 0);
|
|
|
|
|
|
|
|
if (errno != 0)
|
|
|
|
goto fail;
|
|
|
|
|
2015-06-30 18:42:01 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-18 12:18:55 +03:00
|
|
|
tok = strtok_s(NULL, ".", &context);
|
2015-06-30 18:42:01 +03:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Source : http://en.wikipedia.org/wiki/Darwin_(operating_system)
|
|
|
|
**/
|
|
|
|
if (majorVersion < 5)
|
|
|
|
{
|
|
|
|
if (minorVersion < 4)
|
|
|
|
version = 0x10000000;
|
|
|
|
else
|
|
|
|
version = 0x10010000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (majorVersion)
|
|
|
|
{
|
|
|
|
case 5:
|
|
|
|
version = 0x10010000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
version = 0x10020000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
version = 0x10030000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
version = 0x10040000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 9:
|
|
|
|
version = 0x10050000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 10:
|
|
|
|
version = 0x10060000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
version = 0x10070000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 12:
|
|
|
|
version = 0x10080000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 13:
|
|
|
|
version = 0x10090000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
version = 0x10100000;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
version |= (minorVersion << 8) | (patchVersion);
|
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
fail:
|
2015-06-30 18:42:01 +03:00
|
|
|
free(kernelVersion);
|
|
|
|
return version;
|
2014-12-13 16:27:57 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
static const SCardApiFunctionTable PCSC_SCardApiFunctionTable = {
|
2014-04-04 23:30:03 +04:00
|
|
|
0, /* dwVersion */
|
|
|
|
0, /* dwFlags */
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_SCardEstablishContext, /* SCardEstablishContext */
|
|
|
|
PCSC_SCardReleaseContext, /* SCardReleaseContext */
|
|
|
|
PCSC_SCardIsValidContext, /* SCardIsValidContext */
|
|
|
|
PCSC_SCardListReaderGroupsA, /* SCardListReaderGroupsA */
|
|
|
|
PCSC_SCardListReaderGroupsW, /* SCardListReaderGroupsW */
|
|
|
|
PCSC_SCardListReadersA, /* SCardListReadersA */
|
|
|
|
PCSC_SCardListReadersW, /* SCardListReadersW */
|
|
|
|
PCSC_SCardListCardsA, /* SCardListCardsA */
|
|
|
|
PCSC_SCardListCardsW, /* SCardListCardsW */
|
|
|
|
PCSC_SCardListInterfacesA, /* SCardListInterfacesA */
|
|
|
|
PCSC_SCardListInterfacesW, /* SCardListInterfacesW */
|
|
|
|
PCSC_SCardGetProviderIdA, /* SCardGetProviderIdA */
|
|
|
|
PCSC_SCardGetProviderIdW, /* SCardGetProviderIdW */
|
|
|
|
PCSC_SCardGetCardTypeProviderNameA, /* SCardGetCardTypeProviderNameA */
|
|
|
|
PCSC_SCardGetCardTypeProviderNameW, /* SCardGetCardTypeProviderNameW */
|
|
|
|
PCSC_SCardIntroduceReaderGroupA, /* SCardIntroduceReaderGroupA */
|
|
|
|
PCSC_SCardIntroduceReaderGroupW, /* SCardIntroduceReaderGroupW */
|
|
|
|
PCSC_SCardForgetReaderGroupA, /* SCardForgetReaderGroupA */
|
|
|
|
PCSC_SCardForgetReaderGroupW, /* SCardForgetReaderGroupW */
|
|
|
|
PCSC_SCardIntroduceReaderA, /* SCardIntroduceReaderA */
|
|
|
|
PCSC_SCardIntroduceReaderW, /* SCardIntroduceReaderW */
|
|
|
|
PCSC_SCardForgetReaderA, /* SCardForgetReaderA */
|
|
|
|
PCSC_SCardForgetReaderW, /* SCardForgetReaderW */
|
|
|
|
PCSC_SCardAddReaderToGroupA, /* SCardAddReaderToGroupA */
|
|
|
|
PCSC_SCardAddReaderToGroupW, /* SCardAddReaderToGroupW */
|
|
|
|
PCSC_SCardRemoveReaderFromGroupA, /* SCardRemoveReaderFromGroupA */
|
|
|
|
PCSC_SCardRemoveReaderFromGroupW, /* SCardRemoveReaderFromGroupW */
|
|
|
|
PCSC_SCardIntroduceCardTypeA, /* SCardIntroduceCardTypeA */
|
|
|
|
PCSC_SCardIntroduceCardTypeW, /* SCardIntroduceCardTypeW */
|
|
|
|
PCSC_SCardSetCardTypeProviderNameA, /* SCardSetCardTypeProviderNameA */
|
|
|
|
PCSC_SCardSetCardTypeProviderNameW, /* SCardSetCardTypeProviderNameW */
|
|
|
|
PCSC_SCardForgetCardTypeA, /* SCardForgetCardTypeA */
|
|
|
|
PCSC_SCardForgetCardTypeW, /* SCardForgetCardTypeW */
|
|
|
|
PCSC_SCardFreeMemory, /* SCardFreeMemory */
|
|
|
|
PCSC_SCardAccessStartedEvent, /* SCardAccessStartedEvent */
|
|
|
|
PCSC_SCardReleaseStartedEvent, /* SCardReleaseStartedEvent */
|
|
|
|
PCSC_SCardLocateCardsA, /* SCardLocateCardsA */
|
|
|
|
PCSC_SCardLocateCardsW, /* SCardLocateCardsW */
|
|
|
|
PCSC_SCardLocateCardsByATRA, /* SCardLocateCardsByATRA */
|
|
|
|
PCSC_SCardLocateCardsByATRW, /* SCardLocateCardsByATRW */
|
|
|
|
PCSC_SCardGetStatusChangeA, /* SCardGetStatusChangeA */
|
|
|
|
PCSC_SCardGetStatusChangeW, /* SCardGetStatusChangeW */
|
|
|
|
PCSC_SCardCancel, /* SCardCancel */
|
|
|
|
PCSC_SCardConnectA, /* SCardConnectA */
|
|
|
|
PCSC_SCardConnectW, /* SCardConnectW */
|
|
|
|
PCSC_SCardReconnect, /* SCardReconnect */
|
|
|
|
PCSC_SCardDisconnect, /* SCardDisconnect */
|
|
|
|
PCSC_SCardBeginTransaction, /* SCardBeginTransaction */
|
|
|
|
PCSC_SCardEndTransaction, /* SCardEndTransaction */
|
|
|
|
PCSC_SCardCancelTransaction, /* SCardCancelTransaction */
|
|
|
|
PCSC_SCardState, /* SCardState */
|
|
|
|
PCSC_SCardStatusA, /* SCardStatusA */
|
|
|
|
PCSC_SCardStatusW, /* SCardStatusW */
|
|
|
|
PCSC_SCardTransmit, /* SCardTransmit */
|
|
|
|
PCSC_SCardGetTransmitCount, /* SCardGetTransmitCount */
|
|
|
|
PCSC_SCardControl, /* SCardControl */
|
|
|
|
PCSC_SCardGetAttrib, /* SCardGetAttrib */
|
|
|
|
PCSC_SCardSetAttrib, /* SCardSetAttrib */
|
|
|
|
PCSC_SCardUIDlgSelectCardA, /* SCardUIDlgSelectCardA */
|
|
|
|
PCSC_SCardUIDlgSelectCardW, /* SCardUIDlgSelectCardW */
|
|
|
|
PCSC_GetOpenCardNameA, /* GetOpenCardNameA */
|
|
|
|
PCSC_GetOpenCardNameW, /* GetOpenCardNameW */
|
|
|
|
PCSC_SCardDlgExtendedError, /* SCardDlgExtendedError */
|
|
|
|
PCSC_SCardReadCacheA, /* SCardReadCacheA */
|
|
|
|
PCSC_SCardReadCacheW, /* SCardReadCacheW */
|
|
|
|
PCSC_SCardWriteCacheA, /* SCardWriteCacheA */
|
|
|
|
PCSC_SCardWriteCacheW, /* SCardWriteCacheW */
|
|
|
|
PCSC_SCardGetReaderIconA, /* SCardGetReaderIconA */
|
|
|
|
PCSC_SCardGetReaderIconW, /* SCardGetReaderIconW */
|
|
|
|
PCSC_SCardGetDeviceTypeIdA, /* SCardGetDeviceTypeIdA */
|
|
|
|
PCSC_SCardGetDeviceTypeIdW, /* SCardGetDeviceTypeIdW */
|
|
|
|
PCSC_SCardGetReaderDeviceInstanceIdA, /* SCardGetReaderDeviceInstanceIdA */
|
|
|
|
PCSC_SCardGetReaderDeviceInstanceIdW, /* SCardGetReaderDeviceInstanceIdW */
|
2014-04-04 23:30:03 +04:00
|
|
|
PCSC_SCardListReadersWithDeviceInstanceIdA, /* SCardListReadersWithDeviceInstanceIdA */
|
|
|
|
PCSC_SCardListReadersWithDeviceInstanceIdW, /* SCardListReadersWithDeviceInstanceIdW */
|
2019-11-06 17:24:51 +03:00
|
|
|
PCSC_SCardAudit /* SCardAudit */
|
2014-04-04 23:30:03 +04:00
|
|
|
};
|
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
const SCardApiFunctionTable* PCSC_GetSCardApiFunctionTable(void)
|
2014-04-04 23:30:03 +04:00
|
|
|
{
|
|
|
|
return &PCSC_SCardApiFunctionTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PCSC_InitializeSCardApi(void)
|
2014-04-09 02:55:50 +04:00
|
|
|
{
|
2014-05-11 13:23:30 +04:00
|
|
|
/* Disable pcsc-lite's (poor) blocking so we can handle it ourselves */
|
2014-06-03 01:20:01 +04:00
|
|
|
SetEnvironmentVariableA("PCSCLITE_NO_BLOCKING", "1");
|
2014-04-09 02:55:50 +04:00
|
|
|
#ifdef __MACOSX__
|
|
|
|
g_PCSCModule = LoadLibraryA("/System/Library/Frameworks/PCSC.framework/PCSC");
|
2015-01-21 04:15:10 +03:00
|
|
|
OSXVersion = determineMacOSXVersion();
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (OSXVersion == 0)
|
|
|
|
return -1;
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2014-04-09 02:55:50 +04:00
|
|
|
#else
|
2014-04-26 06:26:55 +04:00
|
|
|
g_PCSCModule = LoadLibraryA("libpcsclite.so.1");
|
|
|
|
|
|
|
|
if (!g_PCSCModule)
|
|
|
|
g_PCSCModule = LoadLibraryA("libpcsclite.so");
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2014-04-09 02:55:50 +04:00
|
|
|
#endif
|
2014-04-28 04:11:31 +04:00
|
|
|
|
2014-04-04 23:30:03 +04:00
|
|
|
if (!g_PCSCModule)
|
|
|
|
return -1;
|
|
|
|
|
2020-02-17 12:53:02 +03:00
|
|
|
g_PCSC.pfnSCardEstablishContext =
|
2020-02-17 13:34:09 +03:00
|
|
|
(fnPCSCSCardEstablishContext)GetProcAddress(g_PCSCModule, "SCardEstablishContext");
|
2020-02-17 12:53:02 +03:00
|
|
|
g_PCSC.pfnSCardReleaseContext =
|
2020-02-17 13:34:09 +03:00
|
|
|
(fnPCSCSCardReleaseContext)GetProcAddress(g_PCSCModule, "SCardReleaseContext");
|
2020-02-17 12:53:02 +03:00
|
|
|
g_PCSC.pfnSCardIsValidContext =
|
2020-02-17 13:34:09 +03:00
|
|
|
(fnPCSCSCardIsValidContext)GetProcAddress(g_PCSCModule, "SCardIsValidContext");
|
|
|
|
g_PCSC.pfnSCardConnect = (fnPCSCSCardConnect)GetProcAddress(g_PCSCModule, "SCardConnect");
|
|
|
|
g_PCSC.pfnSCardReconnect = (fnPCSCSCardReconnect)GetProcAddress(g_PCSCModule, "SCardReconnect");
|
|
|
|
g_PCSC.pfnSCardDisconnect =
|
|
|
|
(fnPCSCSCardDisconnect)GetProcAddress(g_PCSCModule, "SCardDisconnect");
|
2020-02-17 12:53:02 +03:00
|
|
|
g_PCSC.pfnSCardBeginTransaction =
|
2020-02-17 13:34:09 +03:00
|
|
|
(fnPCSCSCardBeginTransaction)GetProcAddress(g_PCSCModule, "SCardBeginTransaction");
|
2020-02-17 12:53:02 +03:00
|
|
|
g_PCSC.pfnSCardEndTransaction =
|
2020-02-17 13:34:09 +03:00
|
|
|
(fnPCSCSCardEndTransaction)GetProcAddress(g_PCSCModule, "SCardEndTransaction");
|
|
|
|
g_PCSC.pfnSCardStatus = (fnPCSCSCardStatus)GetProcAddress(g_PCSCModule, "SCardStatus");
|
2020-02-17 12:53:02 +03:00
|
|
|
g_PCSC.pfnSCardGetStatusChange =
|
2020-02-17 13:34:09 +03:00
|
|
|
(fnPCSCSCardGetStatusChange)GetProcAddress(g_PCSCModule, "SCardGetStatusChange");
|
2014-12-14 17:15:33 +03:00
|
|
|
#ifdef __MACOSX__
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2014-12-13 16:27:57 +03:00
|
|
|
if (OSXVersion >= 0x10050600)
|
2020-02-17 13:34:09 +03:00
|
|
|
g_PCSC.pfnSCardControl =
|
|
|
|
(fnPCSCSCardControl)GetProcAddress(g_PCSCModule, "SCardControl132");
|
2014-12-13 16:27:57 +03:00
|
|
|
else
|
2020-02-17 13:34:09 +03:00
|
|
|
g_PCSC.pfnSCardControl = (fnPCSCSCardControl)GetProcAddress(g_PCSCModule, "SCardControl");
|
2015-06-30 18:42:01 +03:00
|
|
|
|
2014-12-14 17:15:33 +03:00
|
|
|
#else
|
2020-02-17 13:34:09 +03:00
|
|
|
g_PCSC.pfnSCardControl = (fnPCSCSCardControl)GetProcAddress(g_PCSCModule, "SCardControl");
|
2014-12-14 17:15:33 +03:00
|
|
|
#endif
|
2020-02-17 13:34:09 +03:00
|
|
|
g_PCSC.pfnSCardTransmit = (fnPCSCSCardTransmit)GetProcAddress(g_PCSCModule, "SCardTransmit");
|
2020-02-17 12:53:02 +03:00
|
|
|
g_PCSC.pfnSCardListReaderGroups =
|
2020-02-17 13:34:09 +03:00
|
|
|
(fnPCSCSCardListReaderGroups)GetProcAddress(g_PCSCModule, "SCardListReaderGroups");
|
2020-02-17 12:53:02 +03:00
|
|
|
g_PCSC.pfnSCardListReaders =
|
2020-02-17 13:34:09 +03:00
|
|
|
(fnPCSCSCardListReaders)GetProcAddress(g_PCSCModule, "SCardListReaders");
|
|
|
|
g_PCSC.pfnSCardCancel = (fnPCSCSCardCancel)GetProcAddress(g_PCSCModule, "SCardCancel");
|
|
|
|
g_PCSC.pfnSCardGetAttrib = (fnPCSCSCardGetAttrib)GetProcAddress(g_PCSCModule, "SCardGetAttrib");
|
|
|
|
g_PCSC.pfnSCardSetAttrib = (fnPCSCSCardSetAttrib)GetProcAddress(g_PCSCModule, "SCardSetAttrib");
|
2014-04-09 02:55:50 +04:00
|
|
|
g_PCSC.pfnSCardFreeMemory = NULL;
|
2014-04-28 00:43:09 +04:00
|
|
|
#ifndef __APPLE__
|
2020-02-17 13:34:09 +03:00
|
|
|
g_PCSC.pfnSCardFreeMemory =
|
|
|
|
(fnPCSCSCardFreeMemory)GetProcAddress(g_PCSCModule, "SCardFreeMemory");
|
2014-04-09 02:55:50 +04:00
|
|
|
#endif
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-04-09 02:55:50 +04:00
|
|
|
if (g_PCSC.pfnSCardFreeMemory)
|
|
|
|
g_SCardAutoAllocate = TRUE;
|
2014-04-04 23:30:03 +04:00
|
|
|
|
2014-04-09 05:02:30 +04:00
|
|
|
#ifdef DISABLE_PCSC_SCARD_AUTOALLOCATE
|
|
|
|
g_PCSC.pfnSCardFreeMemory = NULL;
|
|
|
|
g_SCardAutoAllocate = FALSE;
|
|
|
|
#endif
|
2014-04-28 00:43:09 +04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
g_PnP_Notification = FALSE;
|
|
|
|
#endif
|
2014-04-04 23:30:03 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2014-04-26 02:49:57 +04:00
|
|
|
|
|
|
|
#endif
|