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 LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1,
194 LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
211 *phContext = SYS_RandomInt(0, -1);
213 Log2(PCSC_LOG_DEBUG,
"Establishing Context: 0x%lX", *phContext);
224 Log2(PCSC_LOG_DEBUG,
"Releasing Context: 0x%lX", hContext);
230 DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
231 LPDWORD pdwActiveProtocol)
235 uint32_t readerState;
254 Log3(PCSC_LOG_DEBUG,
"Attempting Connect to %s using protocol: %ld",
255 szReader, dwPreferredProtocols);
257 rv = RFReaderInfo((LPSTR) szReader, &rContext);
260 Log2(PCSC_LOG_ERROR,
"Reader %s Not Found", szReader);
267 rv = RFCheckReaderStatus(rContext);
282 Log1(PCSC_LOG_ERROR,
"Error Reader Exclusive");
292 Log1(PCSC_LOG_INFO,
"Waiting for release of lock");
295 Log1(PCSC_LOG_INFO,
"Lock released");
310 Log1(PCSC_LOG_DEBUG,
"Card Not Inserted");
330 Log1(PCSC_LOG_DEBUG,
"power up complete.");
331 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
336 Log3(PCSC_LOG_ERROR,
"Error powering up card: %ld 0x%04lX",
342 Log1(PCSC_LOG_ERROR,
"Card Not Powered");
350 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_INUSE");
368 (void)pthread_mutex_lock(rContext->
mMutex);
373 int availableProtocols, defaultProtocol;
381 if (dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD)
385 availableProtocols, defaultProtocol);
388 if (SET_PROTOCOL_PPS_FAILED == ret)
390 (void)pthread_mutex_unlock(rContext->
mMutex);
395 if (SET_PROTOCOL_WRONG_ARGUMENT == ret)
397 (void)pthread_mutex_unlock(rContext->
mMutex);
405 (void)pthread_mutex_unlock(rContext->
mMutex);
409 (void)pthread_mutex_unlock(rContext->
mMutex);
424 switch (*pdwActiveProtocol)
427 case SCARD_PROTOCOL_T1:
428 Log2(PCSC_LOG_DEBUG,
"Active Protocol: T=%d",
432 case SCARD_PROTOCOL_RAW:
433 Log1(PCSC_LOG_DEBUG,
"Active Protocol: RAW");
437 Log2(PCSC_LOG_ERROR,
"Active Protocol: unknown %ld",
442 Log1(PCSC_LOG_DEBUG,
"Direct access: no protocol selected");
447 *phCard = RFCreateReaderHandle(rContext);
449 Log2(PCSC_LOG_DEBUG,
"hCard Identity: %lx", *phCard);
463 (void)RFLockSharing(*phCard, rContext);
467 (void)RFDestroyReaderHandle(*phCard);
484 rv = RFAddReaderHandle(rContext, *phCard);
491 (void)RFDestroyReaderHandle(*phCard);
510 UNREF_READER(rContext)
518 DWORD dwPreferredProtocols, DWORD dwInitialization,
519 LPDWORD pdwActiveProtocol)
524 Log1(PCSC_LOG_DEBUG,
"Attempting reconnect to token.");
550 rv = RFReaderInfoById(hCard, &rContext);
557 rv = RFCheckReaderStatus(rContext);
564 rv = RFCheckSharing(hCard, rContext);
576 (void)RFSetReaderEventState(rContext,
SCARD_RESET);
604 Log1(PCSC_LOG_DEBUG,
"Reset complete.");
605 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
612 Log1(PCSC_LOG_ERROR,
"Error resetting card.");
654 if (dwPreferredProtocols & SCARD_PROTOCOL_RAW)
662 (void)pthread_mutex_lock(rContext->
mMutex);
667 int availableProtocols, defaultProtocol;
675 if (dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD)
679 availableProtocols, defaultProtocol);
682 if (SET_PROTOCOL_PPS_FAILED == ret)
684 (void)pthread_mutex_unlock(rContext->
mMutex);
689 if (SET_PROTOCOL_WRONG_ARGUMENT == ret)
691 (void)pthread_mutex_unlock(rContext->
mMutex);
699 (void)pthread_mutex_unlock(rContext->
mMutex);
703 (void)pthread_mutex_unlock(rContext->
mMutex);
718 switch (*pdwActiveProtocol)
720 case SCARD_PROTOCOL_T0:
721 case SCARD_PROTOCOL_T1:
722 Log2(PCSC_LOG_DEBUG,
"Active Protocol: T=%d",
723 (*pdwActiveProtocol == SCARD_PROTOCOL_T0) ? 0 : 1);
726 case SCARD_PROTOCOL_RAW:
727 Log1(PCSC_LOG_DEBUG,
"Active Protocol: RAW");
731 Log2(PCSC_LOG_ERROR,
"Active Protocol: unknown %ld",
736 Log1(PCSC_LOG_DEBUG,
"Direct access: no protocol selected");
751 (void)RFLockSharing(hCard, rContext);
773 (void)RFUnlockSharing(hCard, rContext);
790 (void)RFUnlockSharing(hCard, rContext);
803 (void)RFClearReaderEventState(rContext, hCard);
813 UNREF_READER(rContext)
833 rv = RFReaderInfoById(hCard, &rContext);
841 && (rContext->
hLockId != hCard))
843 Log1(PCSC_LOG_INFO,
"Waiting for release of lock");
846 Log1(PCSC_LOG_INFO,
"Lock released");
856 rv = RFUnlockAllSharing(hCard, rContext);
870 Log2(PCSC_LOG_DEBUG,
"Active Contexts: %d", rContext->
contexts);
871 Log2(PCSC_LOG_DEBUG,
"dwDisposition: %ld", dwDisposition);
881 (void)RFSetReaderEventState(rContext,
SCARD_RESET);
895 #ifdef DISABLE_AUTO_POWER_ON
897 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_UNPOWERED");
907 #ifdef DISABLE_AUTO_POWER_ON
915 Log3(PCSC_LOG_ERROR,
"Error powering down card: %d 0x%04X",
923 Log1(PCSC_LOG_INFO,
"Skip card power on");
937 Log1(PCSC_LOG_DEBUG,
"Reset complete.");
938 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
945 Log1(PCSC_LOG_ERROR,
"Error resetting card.");
957 UCHAR controlBuffer[5];
964 controlBuffer[0] = 0x20;
965 controlBuffer[1] = 0x15;
966 controlBuffer[2] = (rContext->
slot & 0x0000FFFF) + 1;
967 controlBuffer[3] = 0x00;
968 controlBuffer[4] = 0x00;
970 rv = IFDControl_v2(rContext, controlBuffer, 5, receiveBuffer,
975 if (receiveLength == 2 && receiveBuffer[0] == 0x90)
977 Log1(PCSC_LOG_DEBUG,
"Card ejected successfully.");
983 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
986 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
999 (void)RFRemoveReaderHandle(rContext, hCard);
1000 (void)RFDestroyReaderHandle(hCard);
1020 RESPONSECODE (*fct)(DWORD) = NULL;
1026 if (POWER_STATE_POWERED <= rContext->powerState)
1028 #ifdef DISABLE_AUTO_POWER_ON
1032 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_GRACE_PERIOD");
1036 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_GRACE_PERIOD");
1044 dwGetSize =
sizeof(fct);
1046 &dwGetSize, (PUCHAR)&fct);
1048 if ((
IFD_SUCCESS == rv) && (dwGetSize ==
sizeof(fct)))
1050 Log1(PCSC_LOG_INFO,
"Stopping polling thread");
1051 fct(rContext->
slot);
1063 UNREF_READER(rContext)
1077 rv = RFReaderInfoById(hCard, &rContext);
1084 rv = RFCheckReaderStatus(rContext);
1091 rv = RFCheckReaderEventState(rContext, hCard);
1095 rv = RFLockSharing(hCard, rContext);
1102 Log2(PCSC_LOG_DEBUG,
"Status: 0x%08lX", rv);
1105 UNREF_READER(rContext)
1129 rv = RFReaderInfoById(hCard, &rContext);
1136 rv = RFCheckReaderEventState(rContext, hCard);
1165 (void)RFSetReaderEventState(rContext,
SCARD_RESET);
1176 Log1(PCSC_LOG_DEBUG,
"Reset complete.");
1177 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
1184 Log1(PCSC_LOG_ERROR,
"Error resetting card.");
1195 UCHAR controlBuffer[5];
1197 DWORD receiveLength;
1202 controlBuffer[0] = 0x20;
1203 controlBuffer[1] = 0x15;
1204 controlBuffer[2] = (rContext->
slot & 0x0000FFFF) + 1;
1205 controlBuffer[3] = 0x00;
1206 controlBuffer[4] = 0x00;
1208 rv = IFDControl_v2(rContext, controlBuffer, 5, receiveBuffer,
1213 if (receiveLength == 2 && receiveBuffer[0] == 0x90)
1215 Log1(PCSC_LOG_DEBUG,
"Card ejected successfully.");
1221 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
1224 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
1239 rv2 = RFUnlockSharing(hCard, rContext);
1245 Log2(PCSC_LOG_DEBUG,
"Status: 0x%08lX", rv);
1248 UNREF_READER(rContext)
1254 LPDWORD pcchReaderLen, LPDWORD pdwState,
1255 LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
1262 (void)szReaderNames;
1263 (void)pcchReaderLen;
1273 rv = RFReaderInfoById(hCard, &rContext);
1280 rv = RFCheckSharing(hCard, rContext);
1299 rv = RFCheckReaderEventState(rContext, hCard);
1306 rv = RFCheckReaderStatus(rContext);
1311 UNREF_READER(rContext)
1317 LPCVOID pbSendBuffer, DWORD cbSendLength,
1318 LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
1324 *lpBytesReturned = 0;
1330 rv = RFReaderInfoById(hCard, &rContext);
1337 rv = RFCheckSharing(hCard, rContext);
1341 if (IFD_HVERSION_2_0 == rContext->
version)
1342 if (NULL == pbSendBuffer || 0 == cbSendLength)
1351 rv = RFCheckReaderStatus(rContext);
1355 if (IFD_HVERSION_2_0 == rContext->
version)
1358 *lpBytesReturned = cbRecvLength;
1359 rv = IFDControl_v2(rContext, (PUCHAR)pbSendBuffer,
1360 cbSendLength, pbRecvBuffer, lpBytesReturned);
1363 if (IFD_HVERSION_3_0 == rContext->
version)
1364 rv =
IFDControl(rContext, dwControlCode, pbSendBuffer,
1365 cbSendLength, pbRecvBuffer, cbRecvLength, lpBytesReturned);
1370 UNREF_READER(rContext)
1376 LPBYTE pbAttr, LPDWORD pcbAttrLen)
1385 rv = RFReaderInfoById(hCard, &rContext);
1392 rv = RFCheckSharing(hCard, rContext);
1399 rv = RFCheckReaderStatus(rContext);
1406 rv = RFCheckReaderEventState(rContext, hCard);
1425 if (len > *pcbAttrLen)
1445 UNREF_READER(rContext)
1451 LPCBYTE pbAttr, DWORD cbAttrLen)
1460 rv = RFReaderInfoById(hCard, &rContext);
1467 rv = RFCheckSharing(hCard, rContext);
1474 rv = RFCheckReaderStatus(rContext);
1481 rv = RFCheckReaderEventState(rContext, hCard);
1495 UNREF_READER(rContext)
1501 LPCBYTE pbSendBuffer, DWORD cbSendLength,
1503 LPDWORD pcbRecvLength)
1508 DWORD dwRxLength, tempRxLength;
1510 dwRxLength = *pcbRecvLength;
1523 rv = RFReaderInfoById(hCard, &rContext);
1530 rv = RFCheckSharing(hCard, rContext);
1537 rv = RFCheckReaderStatus(rContext);
1544 rv = RFCheckReaderEventState(rContext, hCard);
1577 sSendPci.Protocol = 0;
1581 sSendPci.Protocol = 1;
1594 for (i = 0 ; prot != 1 ; i++)
1597 sSendPci.Protocol = i;
1606 Log2(PCSC_LOG_DEBUG,
"Send Protocol: T=%ld", sSendPci.Protocol);
1608 tempRxLength = dwRxLength;
1611 && (rContext->
version == IFD_HVERSION_2_0))
1613 rv = IFDControl_v2(rContext, (PUCHAR) pbSendBuffer, cbSendLength,
1614 pbRecvBuffer, &dwRxLength);
1617 rv =
IFDTransmit(rContext, sSendPci, (PUCHAR) pbSendBuffer,
1618 cbSendLength, pbRecvBuffer, &dwRxLength, &sRecvPci);
1631 Log2(PCSC_LOG_ERROR,
"Card not transacted: 0x%08lX", rv);
1638 if (tempRxLength < dwRxLength)
1648 *pcbRecvLength = dwRxLength;
1651 UNREF_READER(rContext)
struct pubReaderStatesList * readerState
link to the reader state
#define SCARD_E_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
uint32_t cardAtrLength
ATR length.
#define SCARD_ATTR_DEVICE_FRIENDLY_NAME
Reader's display name.
int32_t contexts
Number of open contexts.
#define IFD_ERROR_TAG
tag unknown
volatile SCARDHANDLE hLockId
Lock Id.
#define SCARD_SCOPE_USER
Scope in user space.
#define TAG_IFD_STOP_POLLING_THREAD
method used to stop the polling thread (instead of just pthread_kill())
#define SCARD_ATTR_DEVICE_SYSTEM_NAME
Reader's system name.
#define SCARD_S_SUCCESS
No error was encountered.
#define PCSCLITE_SHARING_NO_CONTEXT
No application is using the reader.
#define SCARD_SCOPE_GLOBAL
Scope is global.
#define MAX_BUFFER_SIZE
Maximum Tx/Rx Buffer for short APDU.
#define SCARD_E_INVALID_PARAMETER
One or more of the supplied parameters could not be properly interpreted.
LONG IFDGetCapabilities(READER_CONTEXT *rContext, DWORD dwTag, PDWORD pdwLength, PUCHAR pucValue)
Get's capabilities in the reader.
This keeps a list of defines shared between the driver and the application.
char readerName[MAX_READERNAME]
reader name
unsigned long cbPciLength
Protocol Control Inf Length.
int32_t readerSharing
PCSCLITE_SHARING_* sharing status.
#define SCARD_LEAVE_CARD
Do nothing on close.
This handles protocol defaults, PTS, etc.
This handles abstract system level calls.
int slot
Current Reader Slot.
#define SCARD_E_NO_SMARTCARD
The operation requires a Smart Card, but no Smart Card is currently in the device.
This wraps the dynamic ifdhandler functions.
#define SCARD_W_UNRESPONSIVE_CARD
The smart card is not responding to a reset.
#define SCARD_E_NOT_TRANSACTED
An attempt was made to end a non-existent transaction.
#define SCARD_PROTOCOL_T1
T=1 active protocol.
#define SCARD_SCOPE_TERMINAL
Scope in terminal.
#define SCARD_E_PROTO_MISMATCH
The requested protocols are incompatible with the protocol currently in use with the smart card...
#define SCARD_PROTOCOL_ANY_OLD
used for backward compatibility
#define SCARD_PRESENT
Card is present.
int SYS_USleep(int)
Makes the current process sleep for some microseconds.
#define SCARD_NEGOTIABLE
Ready for PTS.
#define PCSCLITE_SHARING_EXCLUSIVE_CONTEXT
Reader used in exclusive mode.
#define PCSCLITE_LOCK_POLL_RATE
Lock polling rate.
LONG SCARDCONTEXT
hContext returned by SCardEstablishContext()
This keeps track of smart card protocols, timing issues and Answer to Reset ATR handling.
#define SCARD_RESET
Card was reset.
#define SCARD_SHARE_SHARED
Shared mode only.
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.
unsigned long dwProtocol
Protocol identifier.
#define IFD_POWER_DOWN
power down the card
int version
IFD Handler version number.
#define SCARD_PROTOCOL_T0
T=0 active protocol.
pthread_mutex_t * mMutex
Mutex for this connection.
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
short ATRDecodeAtr(int *availableProtocols, int *currentProtocol, PUCHAR pucAtr, DWORD dwLength)
parse an ATR
This handles card insertion/removal events, updates ATR, protocol, and status information.
#define SCARD_UNPOWER_CARD
Power down on close.
LONG IFDSetCapabilities(READER_CONTEXT *rContext, DWORD dwTag, DWORD dwLength, PUCHAR pucValue)
Set capabilities in the reader.
#define SCARD_SWALLOWED
Card not powered.
#define SCARD_W_REMOVED_CARD
The smart card has been removed, so further communication is not possible.
int powerState
auto power off state
#define SCARD_PROTOCOL_UNDEFINED
protocol not set
#define SCARD_RESET_CARD
Reset on close.
UCHAR cardAtr[MAX_ATR_SIZE]
ATR.
LONG SCARDHANDLE
hCard returned by SCardConnect()
LONG IFDPowerICC(READER_CONTEXT *rContext, DWORD dwAction, PUCHAR pucAtr, PDWORD pdwAtrLen)
Power up/down or reset's an ICC located in the IFD.
#define SCARD_E_INSUFFICIENT_BUFFER
The data buffer to receive returned data is too small for the returned data.
#define SCARD_SHARE_EXCLUSIVE
Exclusive mode only.
#define SCARD_POWERED
Card is powered.
DWORD PHSetProtocol(struct ReaderContext *rContext, DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault)
Determine which protocol to use.
This keeps a list of defines for pcsc-lite.
#define SCARD_PROTOCOL_RAW
Raw active protocol.
#define SCARD_EJECT_CARD
Eject on close.
Protocol Control Information (PCI)
#define SCARD_ABSENT
Card is absent.
uint32_t cardProtocol
SCARD_PROTOCOL_* value.
#define SCARD_E_SHARING_VIOLATION
The smart card cannot be accessed because of other connections outstanding.
This keeps track of a list of currently available reader structures.
#define MAX_ATR_SIZE
Maximum ATR size.
#define IFD_ERROR_INSUFFICIENT_BUFFER
buffer is too small
uint32_t readerState
SCARD_* bit field.
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.
#define SCARD_SHARE_DIRECT
Raw mode only.
#define PCSCLITE_SHARING_LAST_CONTEXT
One application is using the reader.
pthread_mutex_t powerState_lock
powerState mutex
#define SCARD_E_UNSUPPORTED_FEATURE
This smart card does not support the requested feature.
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
#define IFD_RESET
warm reset
#define SCARD_SCOPE_SYSTEM
Scope in system.
This handles smart card reader communications.
#define IFD_POWER_UP
power up the card
#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, biometric.
#define SCARD_W_UNPOWERED_CARD
Power has been removed from the smart card, so that further communication is not possible.