105 #include <sys/time.h>
130 #define PROFILE_FILE "/tmp/pcscd_profile"
132 #include <sys/time.h>
136 struct timeval profile_time_start;
140 #define PROFILE_START profile_start(__FUNCTION__);
141 #define PROFILE_END profile_end(__FUNCTION__, __LINE__);
143 static void profile_start(
const char *f)
145 static char initialized = FALSE;
150 fd = fopen(PROFILE_FILE,
"a+");
153 fprintf(stderr,
"\33[01;31mCan't open %s: %s\33[0m\n",
154 PROFILE_FILE, strerror(errno));
157 fprintf(fd,
"\nStart a new profile\n");
160 if (isatty(fileno(stderr)))
166 gettimeofday(&profile_time_start, NULL);
170 static void profile_end(
const char *f,
int line)
172 struct timeval profile_time_end;
175 gettimeofday(&profile_time_end, NULL);
176 d =
time_sub(&profile_time_end, &profile_time_start);
179 fprintf(stderr,
"\33[01;31mRESULT %s \33[35m%ld\33[0m (%d)\n", f, d,
181 fprintf(fd,
"%s %ld\n", f, d);
186 #define PROFILE_START
191 #define SCARD_PROTOCOL_ANY_OLD 0x1000
193 static pthread_mutex_t LockMutex = PTHREAD_MUTEX_INITIALIZER;
195 LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1,
196 LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
215 Log2(PCSC_LOG_DEBUG,
"Establishing Context: 0x%lX", *phContext);
229 Log2(PCSC_LOG_DEBUG,
"Releasing Context: 0x%lX", hContext);
235 DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
236 LPDWORD pdwActiveProtocol)
258 Log3(PCSC_LOG_DEBUG,
"Attempting Connect to %s using protocol: %ld",
259 szReader, dwPreferredProtocols);
261 rv = RFReaderInfo((LPSTR) szReader, &rContext);
264 Log2(PCSC_LOG_ERROR,
"Reader %s Not Found", szReader);
271 rv = RFCheckReaderStatus(rContext);
286 Log1(PCSC_LOG_ERROR,
"Error Reader Exclusive");
296 Log1(PCSC_LOG_INFO,
"Waiting for release of lock");
299 Log1(PCSC_LOG_INFO,
"Lock released");
312 Log1(PCSC_LOG_DEBUG,
"Card Not Inserted");
332 Log1(PCSC_LOG_DEBUG,
"power up complete.");
333 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
338 Log2(PCSC_LOG_ERROR,
"Error powering up card: %s",
344 Log1(PCSC_LOG_ERROR,
"Card Not Powered");
352 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_IN_USE");
370 (void)pthread_mutex_lock(rContext->
mMutex);
375 int availableProtocols, defaultProtocol;
387 availableProtocols, defaultProtocol);
390 if (SET_PROTOCOL_PPS_FAILED == ret)
392 (void)pthread_mutex_unlock(rContext->
mMutex);
397 if (SET_PROTOCOL_WRONG_ARGUMENT == ret)
399 (void)pthread_mutex_unlock(rContext->
mMutex);
407 (void)pthread_mutex_unlock(rContext->
mMutex);
411 (void)pthread_mutex_unlock(rContext->
mMutex);
426 switch (*pdwActiveProtocol)
430 Log2(PCSC_LOG_DEBUG,
"Active Protocol: T=%d",
435 Log1(PCSC_LOG_DEBUG,
"Active Protocol: RAW");
439 Log2(PCSC_LOG_ERROR,
"Active Protocol: unknown %ld",
444 Log1(PCSC_LOG_DEBUG,
"Direct access: no protocol selected");
452 (void)pthread_mutex_lock(&LockMutex);
454 *phCard = RFCreateReaderHandle(rContext);
456 Log2(PCSC_LOG_DEBUG,
"hCard Identity: %lx", *phCard);
470 (void)RFLockSharing(*phCard, rContext);
476 (void)pthread_mutex_unlock(&LockMutex);
491 rv = RFAddReaderHandle(rContext, *phCard);
493 (void)pthread_mutex_unlock(&LockMutex);
518 UNREF_READER(rContext)
526 DWORD dwPreferredProtocols, DWORD dwInitialization,
527 LPDWORD pdwActiveProtocol)
532 Log1(PCSC_LOG_DEBUG,
"Attempting reconnect to token.");
558 rv = RFReaderInfoById(hCard, &rContext);
565 rv = RFCheckReaderStatus(rContext);
572 rv = RFCheckSharing(hCard, rContext);
609 Log1(PCSC_LOG_DEBUG,
"Reset complete.");
610 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
617 Log1(PCSC_LOG_ERROR,
"Error resetting card.");
667 (void)pthread_mutex_lock(rContext->
mMutex);
672 int availableProtocols, defaultProtocol;
684 availableProtocols, defaultProtocol);
687 if (SET_PROTOCOL_PPS_FAILED == ret)
689 (void)pthread_mutex_unlock(rContext->
mMutex);
694 if (SET_PROTOCOL_WRONG_ARGUMENT == ret)
696 (void)pthread_mutex_unlock(rContext->
mMutex);
704 (void)pthread_mutex_unlock(rContext->
mMutex);
708 (void)pthread_mutex_unlock(rContext->
mMutex);
719 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_IN_USE");
727 switch (*pdwActiveProtocol)
731 Log2(PCSC_LOG_DEBUG,
"Active Protocol: T=%d",
736 Log1(PCSC_LOG_DEBUG,
"Active Protocol: RAW");
740 Log2(PCSC_LOG_ERROR,
"Active Protocol: unknown %ld",
745 Log1(PCSC_LOG_DEBUG,
"Direct access: no protocol selected");
760 (void)RFLockSharing(hCard, rContext);
782 (void)RFUnlockSharing(hCard, rContext);
799 (void)RFUnlockSharing(hCard, rContext);
812 (void)RFClearReaderEventState(rContext, hCard);
822 UNREF_READER(rContext)
842 rv = RFReaderInfoById(hCard, &rContext);
853 && (rContext->
hLockId != hCard))
855 Log1(PCSC_LOG_INFO,
"Waiting for release of lock");
858 Log1(PCSC_LOG_INFO,
"Lock released");
868 rv = RFUnlockAllSharing(hCard, rContext);
882 Log2(PCSC_LOG_DEBUG,
"Active Contexts: %d", rContext->
contexts);
883 Log2(PCSC_LOG_DEBUG,
"dwDisposition: %ld", dwDisposition);
905 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_UNPOWERED");
921 Log1(PCSC_LOG_DEBUG,
"Reset complete.");
922 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
930 Log2(PCSC_LOG_ERROR,
"Error powering down card: %s",
935 Log1(PCSC_LOG_ERROR,
"Error resetting card.");
947 UCHAR controlBuffer[5];
954 controlBuffer[0] = 0x20;
955 controlBuffer[1] = 0x15;
956 controlBuffer[2] = (rContext->
slot & 0x0000FFFF) + 1;
957 controlBuffer[3] = 0x00;
958 controlBuffer[4] = 0x00;
960 rv = IFDControl_v2(rContext, controlBuffer, 5, receiveBuffer,
965 if (receiveLength == 2 && receiveBuffer[0] == 0x90)
967 Log1(PCSC_LOG_DEBUG,
"Card ejected successfully.");
973 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
976 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
989 (void)RFRemoveReaderHandle(rContext, hCard);
1009 RESPONSECODE (*fct)(DWORD) = NULL;
1015 if (POWER_STATE_POWERED <= rContext->powerState)
1018 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_GRACE_PERIOD");
1025 dwGetSize =
sizeof(fct);
1027 &dwGetSize, (PUCHAR)&fct);
1029 if ((
IFD_SUCCESS == rv) && (dwGetSize ==
sizeof(fct)))
1031 Log1(PCSC_LOG_INFO,
"Stopping polling thread");
1032 fct(rContext->
slot);
1044 UNREF_READER(rContext)
1058 rv = RFReaderInfoById(hCard, &rContext);
1065 rv = RFCheckReaderStatus(rContext);
1072 rv = RFCheckReaderEventState(rContext, hCard);
1076 rv = RFLockSharing(hCard, rContext);
1083 Log2(PCSC_LOG_DEBUG,
"Status: %s", rv2text(rv));
1086 UNREF_READER(rContext)
1110 rv = RFReaderInfoById(hCard, &rContext);
1117 rv = RFCheckReaderEventState(rContext, hCard);
1126 && (rContext->
hLockId != hCard))
1128 Log1(PCSC_LOG_INFO,
"No card reset within a transaction");
1166 Log1(PCSC_LOG_DEBUG,
"Reset complete.");
1167 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
1174 Log1(PCSC_LOG_ERROR,
"Error resetting card.");
1185 UCHAR controlBuffer[5];
1187 DWORD receiveLength;
1192 controlBuffer[0] = 0x20;
1193 controlBuffer[1] = 0x15;
1194 controlBuffer[2] = (rContext->
slot & 0x0000FFFF) + 1;
1195 controlBuffer[3] = 0x00;
1196 controlBuffer[4] = 0x00;
1198 rv = IFDControl_v2(rContext, controlBuffer, 5, receiveBuffer,
1203 if (receiveLength == 2 && receiveBuffer[0] == 0x90)
1205 Log1(PCSC_LOG_DEBUG,
"Card ejected successfully.");
1211 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
1214 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
1229 rv2 = RFUnlockSharing(hCard, rContext);
1235 Log2(PCSC_LOG_DEBUG,
"Status: %s", rv2text(rv));
1238 UNREF_READER(rContext)
1244 LPDWORD pcchReaderLen, LPDWORD pdwState,
1245 LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
1252 (void)szReaderNames;
1253 (void)pcchReaderLen;
1263 rv = RFReaderInfoById(hCard, &rContext);
1270 rv = RFCheckSharing(hCard, rContext);
1289 rv = RFCheckReaderEventState(rContext, hCard);
1296 rv = RFCheckReaderStatus(rContext);
1301 UNREF_READER(rContext)
1307 LPCVOID pbSendBuffer, DWORD cbSendLength,
1308 LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
1314 *lpBytesReturned = 0;
1320 rv = RFReaderInfoById(hCard, &rContext);
1327 rv = RFCheckSharing(hCard, rContext);
1331 if (IFD_HVERSION_2_0 == rContext->
version)
1332 if (NULL == pbSendBuffer || 0 == cbSendLength)
1341 rv = RFCheckReaderStatus(rContext);
1345 if (IFD_HVERSION_2_0 == rContext->
version)
1348 *lpBytesReturned = cbRecvLength;
1349 rv = IFDControl_v2(rContext, (PUCHAR)pbSendBuffer,
1350 cbSendLength, pbRecvBuffer, lpBytesReturned);
1353 if (IFD_HVERSION_3_0 == rContext->
version)
1354 rv =
IFDControl(rContext, dwControlCode, pbSendBuffer,
1355 cbSendLength, pbRecvBuffer, cbRecvLength, lpBytesReturned);
1360 UNREF_READER(rContext)
1366 LPBYTE pbAttr, LPDWORD pcbAttrLen)
1375 rv = RFReaderInfoById(hCard, &rContext);
1382 rv = RFCheckSharing(hCard, rContext);
1389 rv = RFCheckReaderStatus(rContext);
1396 rv = RFCheckReaderEventState(rContext, hCard);
1415 if (len > *pcbAttrLen)
1435 UNREF_READER(rContext)
1441 LPCBYTE pbAttr, DWORD cbAttrLen)
1450 rv = RFReaderInfoById(hCard, &rContext);
1457 rv = RFCheckSharing(hCard, rContext);
1464 rv = RFCheckReaderStatus(rContext);
1471 rv = RFCheckReaderEventState(rContext, hCard);
1485 UNREF_READER(rContext)
1491 LPCBYTE pbSendBuffer, DWORD cbSendLength,
1493 LPDWORD pcbRecvLength)
1498 DWORD dwRxLength, tempRxLength;
1500 dwRxLength = *pcbRecvLength;
1513 rv = RFReaderInfoById(hCard, &rContext);
1520 rv = RFCheckSharing(hCard, rContext);
1527 rv = RFCheckReaderStatus(rContext);
1534 rv = RFCheckReaderEventState(rContext, hCard);
1567 sSendPci.Protocol = 0;
1571 sSendPci.Protocol = 1;
1584 for (i = 0 ; prot != 1 && i < 16; i++)
1587 sSendPci.Protocol = i;
1596 Log2(PCSC_LOG_DEBUG,
"Send Protocol: T=%ld", sSendPci.Protocol);
1598 tempRxLength = dwRxLength;
1601 && (rContext->
version == IFD_HVERSION_2_0))
1603 rv = IFDControl_v2(rContext, (PUCHAR) pbSendBuffer, cbSendLength,
1604 pbRecvBuffer, &dwRxLength);
1607 rv =
IFDTransmit(rContext, sSendPci, (PUCHAR) pbSendBuffer,
1608 cbSendLength, pbRecvBuffer, &dwRxLength, &sRecvPci);
1621 Log2(PCSC_LOG_ERROR,
"Card not transacted: %s", rv2text(rv));
1636 if (tempRxLength < dwRxLength)
1646 *pcbRecvLength = dwRxLength;
1649 UNREF_READER(rContext)
short ATRDecodeAtr(int *availableProtocols, int *currentProtocol, PUCHAR pucAtr, DWORD dwLength)
parse an ATR
This keeps track of smart card protocols, timing issues and Answer to Reset ATR handling.
This handles card insertion/removal events, updates ATR, protocol, and status information.
#define PCSCLITE_SHARING_EXCLUSIVE_CONTEXT
Reader used in exclusive mode.
#define PCSCLITE_SHARING_NO_CONTEXT
No application is using the reader.
#define PCSCLITE_SHARING_LAST_CONTEXT
One application is using the reader.
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.
#define SCARD_W_UNRESPONSIVE_CARD
The smart card is not responding to a reset.
#define SCARD_E_PROTO_MISMATCH
The requested protocols are incompatible with the protocol currently in use with the smart card.
#define SCARD_E_INVALID_PARAMETER
One or more of the supplied parameters could not be properly interpreted.
#define SCARD_S_SUCCESS
No error was encountered.
#define SCARD_E_SHARING_VIOLATION
The smart card cannot be accessed because of other connections outstanding.
#define SCARD_E_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
#define SCARD_W_REMOVED_CARD
The smart card has been removed, so further communication is not possible.
#define SCARD_E_INSUFFICIENT_BUFFER
The data buffer to receive returned data is too small for the returned data.
#define SCARD_E_NO_SMARTCARD
The operation requires a Smart Card, but no Smart Card is currently in the device.
#define SCARD_E_NOT_TRANSACTED
An attempt was made to end a non-existent transaction.
#define SCARD_E_READER_UNAVAILABLE
The specified reader is not currently available for use.
#define SCARD_W_UNPOWERED_CARD
Power has been removed from the smart card, so that further communication is not possible.
#define SCARD_E_UNSUPPORTED_FEATURE
This smart card does not support the requested feature.
#define IFD_POWER_UP
power up the card
#define IFD_ERROR_TAG
tag unknown
#define TAG_IFD_STOP_POLLING_THREAD
method used to stop the polling thread (instead of just pthread_kill())
#define IFD_POWER_DOWN
power down the card
#define IFD_RESET
warm reset
#define IFD_ERROR_INSUFFICIENT_BUFFER
buffer is too small
#define IFD_SUCCESS
no error
LONG IFDControl(READER_CONTEXT *rContext, DWORD ControlCode, LPCVOID TxBuffer, DWORD TxLength, LPVOID RxBuffer, DWORD RxLength, LPDWORD BytesReturned)
Provide a means for toggling a specific action on the reader such as swallow, eject,...
RESPONSECODE IFDGetCapabilities(READER_CONTEXT *rContext, DWORD dwTag, PDWORD pdwLength, PUCHAR pucValue)
Get's capabilities in the reader.
LONG IFDTransmit(READER_CONTEXT *rContext, SCARD_IO_HEADER pioTxPci, PUCHAR pucTxBuffer, DWORD dwTxLength, PUCHAR pucRxBuffer, PDWORD pdwRxLength, PSCARD_IO_HEADER pioRxPci)
Transmit an APDU to the ICC.
RESPONSECODE IFDSetCapabilities(READER_CONTEXT *rContext, DWORD dwTag, DWORD dwLength, PUCHAR pucValue)
Set capabilities in the reader.
RESPONSECODE IFDPowerICC(READER_CONTEXT *rContext, DWORD dwAction, PUCHAR pucAtr, PDWORD pdwAtrLen)
Power up/down or reset's an ICC located in the IFD.
This wraps the dynamic ifdhandler functions.
This keeps a list of defines for pcsc-lite.
@ POWER_STATE_IN_USE
card is used
@ POWER_STATE_UNPOWERED
auto power off
@ POWER_STATE_GRACE_PERIOD
card was in use
#define SCARD_RESET
Card was reset.
#define PCSCLITE_LOCK_POLL_RATE
Lock polling rate.
#define SCARD_SCOPE_SYSTEM
Scope in system.
#define SCARD_RESET_CARD
Reset on close.
#define SCARD_SWALLOWED
Card not powered.
LONG SCARDCONTEXT
hContext returned by SCardEstablishContext()
#define SCARD_PROTOCOL_UNDEFINED
protocol not set
#define SCARD_PROTOCOL_T1
T=1 active protocol.
#define SCARD_SHARE_DIRECT
Raw mode only.
#define SCARD_SCOPE_USER
Scope in user space.
#define SCARD_SCOPE_GLOBAL
Scope is global.
#define SCARD_PRESENT
Card is present.
#define SCARD_PROTOCOL_T0
T=0 active protocol.
#define SCARD_EJECT_CARD
Eject on close.
#define SCARD_UNPOWER_CARD
Power down on close.
#define SCARD_SHARE_SHARED
Shared mode only.
#define SCARD_POWERED
Card is powered.
#define MAX_ATR_SIZE
Maximum ATR size.
#define SCARD_ABSENT
Card is absent.
#define SCARD_SCOPE_TERMINAL
Scope in terminal.
#define SCARD_PROTOCOL_RAW
Raw active protocol.
#define SCARD_LEAVE_CARD
Do nothing on close.
#define MAX_BUFFER_SIZE
Maximum Tx/Rx Buffer for short APDU.
#define SCARD_SHARE_EXCLUSIVE
Exclusive mode only.
#define SCARD_NEGOTIABLE
Ready for PTS.
LONG SCARDHANDLE
hCard returned by SCardConnect()
DWORD PHSetProtocol(struct ReaderContext *rContext, DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault)
Determine which protocol to use.
This handles protocol defaults, PTS, etc.
This keeps a list of defines shared between the driver and the application.
#define SCARD_ATTR_DEVICE_FRIENDLY_NAME
Reader's display name.
#define SCARD_ATTR_DEVICE_SYSTEM_NAME
Reader's system name.
This keeps track of a list of currently available reader structures.
pthread_mutex_t * mMutex
Mutex for this connection.
pthread_mutex_t powerState_lock
powerState mutex
_Atomic int32_t contexts
Number of open contexts.
int slot
Current Reader Slot.
_Atomic SCARDHANDLE hLockId
Lock Id.
int version
IFD Handler version number.
struct pubReaderStatesList * readerState
link to the reader state
int powerState
auto power off state
Protocol Control Information (PCI)
unsigned long dwProtocol
Protocol identifier.
unsigned long cbPciLength
Protocol Control Inf Length.
char readerName[MAX_READERNAME]
reader name
int32_t readerSharing
PCSCLITE_SHARING_* sharing status.
uint32_t cardProtocol
SCARD_PROTOCOL_* value.
UCHAR cardAtr[MAX_ATR_SIZE]
ATR.
uint32_t readerState
SCARD_* bit field.
uint32_t cardAtrLength
ATR length.
This handles abstract system level calls.
int SYS_RandomInt(void)
Generate a pseudo random number.
int SYS_USleep(int)
Makes the current process sleep for some microseconds.
long int time_sub(struct timeval *a, struct timeval *b)
return the difference (as long int) in µs between 2 struct timeval r = a - b
#define SCARD_PROTOCOL_ANY_OLD
used for backward compatibility
This handles smart card reader communications.