107 #include <sys/time.h>
133 #define PROFILE_FILE "/tmp/pcscd_profile"
135 #include <sys/time.h>
139 struct timeval profile_time_start;
143 #define PROFILE_START profile_start(__FUNCTION__);
144 #define PROFILE_END profile_end(__FUNCTION__, __LINE__);
146 static void profile_start(
const char *f)
148 static char initialized = FALSE;
153 fd = fopen(PROFILE_FILE,
"a+");
156 fprintf(stderr,
"\33[01;31mCan't open %s: %s\33[0m\n",
157 PROFILE_FILE, strerror(errno));
160 fprintf(fd,
"\nStart a new profile\n");
163 if (isatty(fileno(stderr)))
169 gettimeofday(&profile_time_start, NULL);
173 static void profile_end(
const char *f,
int line)
175 struct timeval profile_time_end;
178 gettimeofday(&profile_time_end, NULL);
179 d =
time_sub(&profile_time_end, &profile_time_start);
182 fprintf(stderr,
"\33[01;31mRESULT %s \33[35m%ld\33[0m (%d)\n", f, d,
184 fprintf(fd,
"%s %ld\n", f, d);
189 #define PROFILE_START
194 #define SCARD_PROTOCOL_ANY_OLD 0x1000
196 LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1,
197 LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
214 *phContext = SYS_RandomInt(0, -1);
216 Log2(PCSC_LOG_DEBUG,
"Establishing Context: 0x%lX", *phContext);
227 Log2(PCSC_LOG_DEBUG,
"Releasing Context: 0x%lX", hContext);
233 DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard,
234 LPDWORD pdwActiveProtocol)
238 uint32_t readerState;
257 Log3(PCSC_LOG_DEBUG,
"Attempting Connect to %s using protocol: %ld",
258 szReader, dwPreferredProtocols);
260 rv = RFReaderInfo((LPSTR) szReader, &rContext);
263 Log2(PCSC_LOG_ERROR,
"Reader %s Not Found", szReader);
270 rv = RFCheckReaderStatus(rContext);
285 Log1(PCSC_LOG_ERROR,
"Error Reader Exclusive");
295 Log1(PCSC_LOG_INFO,
"Waiting for release of lock");
298 Log1(PCSC_LOG_INFO,
"Lock released");
313 Log1(PCSC_LOG_DEBUG,
"Card Not Inserted");
333 Log1(PCSC_LOG_DEBUG,
"power up complete.");
334 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
339 Log3(PCSC_LOG_ERROR,
"Error powering up card: %ld 0x%04lX",
345 Log1(PCSC_LOG_ERROR,
"Card Not Powered");
353 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_INUSE");
371 (void)pthread_mutex_lock(rContext->
mMutex);
376 int availableProtocols, defaultProtocol;
384 if (dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD)
388 availableProtocols, defaultProtocol);
391 if (SET_PROTOCOL_PPS_FAILED == ret)
393 (void)pthread_mutex_unlock(rContext->
mMutex);
398 if (SET_PROTOCOL_WRONG_ARGUMENT == ret)
400 (void)pthread_mutex_unlock(rContext->
mMutex);
408 (void)pthread_mutex_unlock(rContext->
mMutex);
412 (void)pthread_mutex_unlock(rContext->
mMutex);
427 switch (*pdwActiveProtocol)
430 case SCARD_PROTOCOL_T1:
431 Log2(PCSC_LOG_DEBUG,
"Active Protocol: T=%d",
435 case SCARD_PROTOCOL_RAW:
436 Log1(PCSC_LOG_DEBUG,
"Active Protocol: RAW");
440 Log2(PCSC_LOG_ERROR,
"Active Protocol: unknown %ld",
445 Log1(PCSC_LOG_DEBUG,
"Direct access: no protocol selected");
450 *phCard = RFCreateReaderHandle(rContext);
452 Log2(PCSC_LOG_DEBUG,
"hCard Identity: %lx", *phCard);
466 (void)RFLockSharing(*phCard, rContext);
470 (void)RFDestroyReaderHandle(*phCard);
487 rv = RFAddReaderHandle(rContext, *phCard);
494 (void)RFDestroyReaderHandle(*phCard);
513 UNREF_READER(rContext)
521 DWORD dwPreferredProtocols, DWORD dwInitialization,
522 LPDWORD pdwActiveProtocol)
527 Log1(PCSC_LOG_DEBUG,
"Attempting reconnect to token.");
553 rv = RFReaderInfoById(hCard, &rContext);
560 rv = RFCheckReaderStatus(rContext);
567 rv = RFCheckSharing(hCard, rContext);
579 (void)RFSetReaderEventState(rContext,
SCARD_RESET);
607 Log1(PCSC_LOG_DEBUG,
"Reset complete.");
608 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
615 Log1(PCSC_LOG_ERROR,
"Error resetting card.");
657 if (dwPreferredProtocols & SCARD_PROTOCOL_RAW)
665 (void)pthread_mutex_lock(rContext->
mMutex);
670 int availableProtocols, defaultProtocol;
678 if (dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD)
682 availableProtocols, defaultProtocol);
685 if (SET_PROTOCOL_PPS_FAILED == ret)
687 (void)pthread_mutex_unlock(rContext->
mMutex);
692 if (SET_PROTOCOL_WRONG_ARGUMENT == ret)
694 (void)pthread_mutex_unlock(rContext->
mMutex);
702 (void)pthread_mutex_unlock(rContext->
mMutex);
706 (void)pthread_mutex_unlock(rContext->
mMutex);
721 switch (*pdwActiveProtocol)
723 case SCARD_PROTOCOL_T0:
724 case SCARD_PROTOCOL_T1:
725 Log2(PCSC_LOG_DEBUG,
"Active Protocol: T=%d",
726 (*pdwActiveProtocol == SCARD_PROTOCOL_T0) ? 0 : 1);
729 case SCARD_PROTOCOL_RAW:
730 Log1(PCSC_LOG_DEBUG,
"Active Protocol: RAW");
734 Log2(PCSC_LOG_ERROR,
"Active Protocol: unknown %ld",
739 Log1(PCSC_LOG_DEBUG,
"Direct access: no protocol selected");
754 (void)RFLockSharing(hCard, rContext);
776 (void)RFUnlockSharing(hCard, rContext);
793 (void)RFUnlockSharing(hCard, rContext);
806 (void)RFClearReaderEventState(rContext, hCard);
816 UNREF_READER(rContext)
836 rv = RFReaderInfoById(hCard, &rContext);
844 && (rContext->
hLockId != hCard))
846 Log1(PCSC_LOG_INFO,
"Waiting for release of lock");
849 Log1(PCSC_LOG_INFO,
"Lock released");
859 rv = RFUnlockAllSharing(hCard, rContext);
873 Log2(PCSC_LOG_DEBUG,
"Active Contexts: %d", rContext->
contexts);
874 Log2(PCSC_LOG_DEBUG,
"dwDisposition: %ld", dwDisposition);
884 (void)RFSetReaderEventState(rContext,
SCARD_RESET);
898 #ifdef DISABLE_AUTO_POWER_ON
900 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_UNPOWERED");
910 #ifdef DISABLE_AUTO_POWER_ON
918 Log3(PCSC_LOG_ERROR,
"Error powering down card: %d 0x%04X",
926 Log1(PCSC_LOG_INFO,
"Skip card power on");
940 Log1(PCSC_LOG_DEBUG,
"Reset complete.");
941 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
948 Log1(PCSC_LOG_ERROR,
"Error resetting card.");
960 UCHAR controlBuffer[5];
967 controlBuffer[0] = 0x20;
968 controlBuffer[1] = 0x15;
969 controlBuffer[2] = (rContext->
slot & 0x0000FFFF) + 1;
970 controlBuffer[3] = 0x00;
971 controlBuffer[4] = 0x00;
973 rv = IFDControl_v2(rContext, controlBuffer, 5, receiveBuffer,
978 if (receiveLength == 2 && receiveBuffer[0] == 0x90)
980 Log1(PCSC_LOG_DEBUG,
"Card ejected successfully.");
986 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
989 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
1002 (void)RFRemoveReaderHandle(rContext, hCard);
1003 (void)RFDestroyReaderHandle(hCard);
1023 RESPONSECODE (*fct)(DWORD) = NULL;
1029 if (POWER_STATE_POWERED <= rContext->powerState)
1031 #ifdef DISABLE_AUTO_POWER_ON
1035 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_GRACE_PERIOD");
1039 Log1(PCSC_LOG_DEBUG,
"powerState: POWER_STATE_GRACE_PERIOD");
1047 dwGetSize =
sizeof(fct);
1049 &dwGetSize, (PUCHAR)&fct);
1051 if ((
IFD_SUCCESS == rv) && (dwGetSize ==
sizeof(fct)))
1053 Log1(PCSC_LOG_INFO,
"Stopping polling thread");
1054 fct(rContext->
slot);
1066 UNREF_READER(rContext)
1080 rv = RFReaderInfoById(hCard, &rContext);
1087 rv = RFCheckReaderStatus(rContext);
1094 rv = RFCheckReaderEventState(rContext, hCard);
1098 rv = RFLockSharing(hCard, rContext);
1105 Log2(PCSC_LOG_DEBUG,
"Status: 0x%08lX", rv);
1108 UNREF_READER(rContext)
1132 rv = RFReaderInfoById(hCard, &rContext);
1139 rv = RFCheckReaderEventState(rContext, hCard);
1168 (void)RFSetReaderEventState(rContext,
SCARD_RESET);
1179 Log1(PCSC_LOG_DEBUG,
"Reset complete.");
1180 LogXxd(PCSC_LOG_DEBUG,
"Card ATR: ",
1187 Log1(PCSC_LOG_ERROR,
"Error resetting card.");
1198 UCHAR controlBuffer[5];
1200 DWORD receiveLength;
1205 controlBuffer[0] = 0x20;
1206 controlBuffer[1] = 0x15;
1207 controlBuffer[2] = (rContext->
slot & 0x0000FFFF) + 1;
1208 controlBuffer[3] = 0x00;
1209 controlBuffer[4] = 0x00;
1211 rv = IFDControl_v2(rContext, controlBuffer, 5, receiveBuffer,
1216 if (receiveLength == 2 && receiveBuffer[0] == 0x90)
1218 Log1(PCSC_LOG_DEBUG,
"Card ejected successfully.");
1224 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
1227 Log1(PCSC_LOG_ERROR,
"Error ejecting card.");
1242 rv2 = RFUnlockSharing(hCard, rContext);
1248 Log2(PCSC_LOG_DEBUG,
"Status: 0x%08lX", rv);
1251 UNREF_READER(rContext)
1257 LPDWORD pcchReaderLen, LPDWORD pdwState,
1258 LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
1265 (void)mszReaderNames;
1266 (void)pcchReaderLen;
1276 rv = RFReaderInfoById(hCard, &rContext);
1283 rv = RFCheckSharing(hCard, rContext);
1302 rv = RFCheckReaderEventState(rContext, hCard);
1309 rv = RFCheckReaderStatus(rContext);
1314 UNREF_READER(rContext)
1320 LPCVOID pbSendBuffer, DWORD cbSendLength,
1321 LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
1327 *lpBytesReturned = 0;
1333 rv = RFReaderInfoById(hCard, &rContext);
1340 rv = RFCheckSharing(hCard, rContext);
1344 if (IFD_HVERSION_2_0 == rContext->
version)
1345 if (NULL == pbSendBuffer || 0 == cbSendLength)
1354 rv = RFCheckReaderStatus(rContext);
1358 if (IFD_HVERSION_2_0 == rContext->
version)
1361 *lpBytesReturned = cbRecvLength;
1362 rv = IFDControl_v2(rContext, (PUCHAR)pbSendBuffer,
1363 cbSendLength, pbRecvBuffer, lpBytesReturned);
1366 if (IFD_HVERSION_3_0 == rContext->
version)
1367 rv =
IFDControl(rContext, dwControlCode, pbSendBuffer,
1368 cbSendLength, pbRecvBuffer, cbRecvLength, lpBytesReturned);
1373 UNREF_READER(rContext)
1379 LPBYTE pbAttr, LPDWORD pcbAttrLen)
1388 rv = RFReaderInfoById(hCard, &rContext);
1395 rv = RFCheckSharing(hCard, rContext);
1402 rv = RFCheckReaderStatus(rContext);
1409 rv = RFCheckReaderEventState(rContext, hCard);
1428 if (len > *pcbAttrLen)
1432 (void)strlcpy((
char *)pbAttr,
1449 UNREF_READER(rContext)
1455 LPCBYTE pbAttr, DWORD cbAttrLen)
1464 rv = RFReaderInfoById(hCard, &rContext);
1471 rv = RFCheckSharing(hCard, rContext);
1478 rv = RFCheckReaderStatus(rContext);
1485 rv = RFCheckReaderEventState(rContext, hCard);
1499 UNREF_READER(rContext)
1505 LPCBYTE pbSendBuffer, DWORD cbSendLength,
1507 LPDWORD pcbRecvLength)
1512 DWORD dwRxLength, tempRxLength;
1514 dwRxLength = *pcbRecvLength;
1527 rv = RFReaderInfoById(hCard, &rContext);
1534 rv = RFCheckSharing(hCard, rContext);
1541 rv = RFCheckReaderStatus(rContext);
1548 rv = RFCheckReaderEventState(rContext, hCard);
1581 sSendPci.Protocol = 0;
1585 sSendPci.Protocol = 1;
1598 for (i = 0 ; prot != 1 ; i++)
1601 sSendPci.Protocol = i;
1610 Log2(PCSC_LOG_DEBUG,
"Send Protocol: T=%ld", sSendPci.Protocol);
1612 tempRxLength = dwRxLength;
1615 && (rContext->
version == IFD_HVERSION_2_0))
1617 rv = IFDControl_v2(rContext, (PUCHAR) pbSendBuffer, cbSendLength,
1618 pbRecvBuffer, &dwRxLength);
1621 rv =
IFDTransmit(rContext, sSendPci, (PUCHAR) pbSendBuffer,
1622 cbSendLength, pbRecvBuffer, &dwRxLength, &sRecvPci);
1635 Log2(PCSC_LOG_ERROR,
"Card not transacted: 0x%08lX", rv);
1642 if (tempRxLength < dwRxLength)
1652 *pcbRecvLength = dwRxLength;
1655 UNREF_READER(rContext)
struct pubReaderStatesList * readerState
link to the reader state
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
#define SCARD_E_PROTO_MISMATCH
The requested protocols are incompatible with the protocol currently in use with the smart card...
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 PCSCLITE_SHARING_NO_CONTEXT
No application is using the reader.
#define MAX_BUFFER_SIZE
Maximum Tx/Rx Buffer for short APDU.
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_E_INVALID_PARAMETER
One or more of the supplied parameters could not be properly interpreted.
#define SCARD_E_NOT_TRANSACTED
An attempt was made to end a non-existent transaction.
#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.
This wraps the dynamic ifdhandler functions.
#define SCARD_PROTOCOL_T1
T=1 active protocol.
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
#define SCARD_SCOPE_TERMINAL
Scope in terminal.
#define SCARD_W_UNRESPONSIVE_CARD
The smart card is not responding to a reset.
#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_E_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
#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.
prototypes of strlcpy()/strlcat() imported from OpenBSD
#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_W_REMOVED_CARD
The smart card has been removed, so further communication is not possible.
#define SCARD_SWALLOWED
Card not powered.
#define SCARD_E_UNSUPPORTED_FEATURE
This smart card does not support the requested feature.
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_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_E_SHARING_VIOLATION
The smart card cannot be accessed because of other connections outstanding.
#define SCARD_SCOPE_GLOBAL
Scope is global.
#define SCARD_W_UNPOWERED_CARD
Power has been removed from the smart card, so that further communication is not possible.
#define SCARD_ABSENT
Card is absent.
uint32_t cardProtocol
SCARD_PROTOCOL_* value.
#define SCARD_E_NO_SMARTCARD
The operation requires a Smart Card, but no Smart Card is currently in the device.
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_E_INSUFFICIENT_BUFFER
The data buffer to receive returned data is too small for the returned data.
#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 IFD_RESET
warm reset
#define SCARD_S_SUCCESS
error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx
#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_F_INTERNAL_ERROR
An internal consistency check failed.