37 #define p_SCardEstablishContext(fct) LONG(fct)(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
39 #define p_SCardReleaseContext(fct) LONG(fct)(SCARDCONTEXT hContext)
41 #define p_SCardIsValidContext(fct) LONG(fct) (SCARDCONTEXT hContext)
43 #define p_SCardConnect(fct) LONG(fct) (SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
45 #define p_SCardReconnect(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol)
47 #define p_SCardDisconnect(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwDisposition)
49 #define p_SCardBeginTransaction(fct) LONG(fct) (SCARDHANDLE hCard)
51 #define p_SCardEndTransaction(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwDisposition)
53 #define p_SCardStatus(fct) LONG(fct) (SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
55 #define p_SCardGetStatusChange(fct) LONG(fct) (SCARDCONTEXT hContext, DWORD dwTimeout, LPSCARD_READERSTATE rgReaderStates, DWORD cReaders)
57 #define p_SCardControl(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
59 #define p_SCardTransmit(fct) LONG(fct) (SCARDHANDLE hCard, const SCARD_IO_REQUEST * pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST * pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength)
61 #define p_SCardListReaderGroups(fct) LONG(fct) (SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
63 #define p_SCardListReaders(fct) LONG(fct) (SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
65 #define p_SCardFreeMemory(fct) LONG(fct) (SCARDCONTEXT hContext, LPCVOID pvMem)
67 #define p_SCardCancel(fct) LONG(fct) (SCARDCONTEXT hContext)
69 #define p_SCardGetAttrib(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
71 #define p_SCardSetAttrib(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
73 #define p_pcsc_stringify_error(fct) const char *(fct)(const LONG pcscError)
76 static LONG internal_error(
void)
81 static const char * internal_stringify_error(
void)
83 return "No spy pcsc_stringify_error() function";
86 #pragma GCC diagnostic push
87 #pragma GCC diagnostic ignored "-Wcast-function-type"
112 .SCardEstablishContext = (p_SCardEstablishContext(*))internal_error,
132 #pragma GCC diagnostic pop
134 #define LOG log_line("%s:%d", __FILE__, __LINE__)
136 static int Log_fd = -1;
137 static void *Lib_handle = NULL;
138 static pthread_mutex_t Log_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
141 static void log_line(
const char *fmt, ...)
151 static void log_line(
const char *fmt, ...)
156 static void spy_line_direct(
char *line)
165 snprintf(threadid,
sizeof threadid,
"%lX@", pthread_self());
166 pthread_mutex_lock(&Log_fd_mutex);
167 r = write(Log_fd, threadid, strlen(threadid));
168 r = write(Log_fd, line, strlen(line));
169 r = write(Log_fd,
"\n", 1);
171 pthread_mutex_unlock(&Log_fd_mutex);
174 static void spy_line(
const char *fmt, ...)
187 size = vsnprintf(line,
sizeof line, fmt, args);
189 if ((
size_t)size >=
sizeof line)
191 printf(
"libpcsc-spy: Buffer is too small!\n");
194 snprintf(threadid,
sizeof threadid,
"%lX@", pthread_self());
195 pthread_mutex_lock(&Log_fd_mutex);
196 r = write(Log_fd, threadid, strlen(threadid));
197 r = write(Log_fd, line, size);
198 r = write(Log_fd,
"\n", 1);
200 pthread_mutex_unlock(&Log_fd_mutex);
203 static void spy_enter(
const char *fname)
205 struct timeval profile_time;
207 gettimeofday(&profile_time, NULL);
208 spy_line(
">|%ld|%ld|%s", profile_time.tv_sec, profile_time.tv_usec, fname);
211 static void spy_quit(
const char *fname, LONG rv)
213 struct timeval profile_time;
215 gettimeofday(&profile_time, NULL);
216 spy_line(
"<|%ld|%ld|%s|%s|0x%08lX", profile_time.tv_sec,
217 profile_time.tv_usec, fname, spy.pcsc_stringify_error(rv), rv);
220 #define Enter() spy_enter(__FUNCTION__)
221 #define Quit() spy_quit(__FUNCTION__, rv)
223 static void spy_long(
long arg)
225 spy_line(
"0x%08lX", arg);
228 static void spy_ptr_long(LONG *arg)
231 spy_line(
"0x%08lX", *arg);
236 static void spy_ptr_ulong(ULONG *arg)
239 spy_line(
"0x%08lX", *arg);
244 static void spy_pvoid(
const void *ptr)
249 static void spy_buffer(
const unsigned char *buffer,
size_t length)
258 char log_buffer[length * 3 +1], *p;
262 log_buffer[0] =
'\0';
263 for (i=0; i<length; i++)
265 snprintf(p, 4,
"%02X ", buffer[i]);
270 spy_line_direct(log_buffer);
274 static void spy_str(
const char *str)
279 static void spy_n_str(
const char *str, ULONG *len,
int autoallocate)
295 unsigned int length = 0;
303 length += strlen(s)+1;
305 }
while(length < *len);
315 for (i=0; i<cReaders; i++)
317 spy_str(rgReaderStates[i].szReader);
318 spy_long(rgReaderStates[i].dwCurrentState);
319 spy_long(rgReaderStates[i].dwEventState);
321 spy_buffer(rgReaderStates[i].rgbAtr, rgReaderStates[i].cbAtr);
323 spy_buffer(NULL, rgReaderStates[i].cbAtr);
327 static LONG load_lib(
void)
330 #define LIBPCSC_NOSPY "libpcsclite_nospy.so.1"
331 #define LIBPCSC "libpcsclite.so.1"
336 Lib_handle = dlopen(LIBPCSC_NOSPY, RTLD_LAZY);
337 if (NULL == Lib_handle)
339 log_line(
"%s", dlerror());
342 Lib_handle = dlopen(LIBPCSC, RTLD_LAZY);
343 if (NULL == Lib_handle)
345 log_line(
"%s", dlerror());
350 #define get_symbol(s) do { spy.s = dlsym(Lib_handle, #s); if (NULL == spy.s) { log_line("%s", dlerror()); return SCARD_F_INTERNAL_ERROR; } } while (0)
354 log_line(
"Symbols dlsym error");
373 if (dlsym(Lib_handle,
"SCardFreeMemory"))
403 home = getenv(
"HOME");
407 snprintf(log_pipe,
sizeof log_pipe,
"%s/pcsc-spy", home);
408 Log_fd = open(log_pipe, O_WRONLY);
411 log_line(
"open %s failed: %s", log_pipe, strerror(errno));
417 rv = spy.SCardEstablishContext(dwScope, pvReserved1, pvReserved2,
419 spy_ptr_long(phContext);
430 rv = spy.SCardReleaseContext(hContext);
441 rv = spy.SCardIsValidContext(hContext);
453 spy_long(dwShareMode);
454 spy_long(dwPreferredProtocols);
455 spy_ptr_long(phCard);
456 spy_ptr_ulong(pdwActiveProtocol);
457 rv = spy.SCardConnect(hContext, szReader, dwShareMode,
458 dwPreferredProtocols, phCard, pdwActiveProtocol);
459 spy_ptr_long(phCard);
460 spy_ptr_ulong(pdwActiveProtocol);
471 spy_long(dwShareMode);
472 spy_long(dwPreferredProtocols);
473 spy_long(dwInitialization);
474 rv = spy.SCardReconnect(hCard, dwShareMode, dwPreferredProtocols,
475 dwInitialization, pdwActiveProtocol);
476 spy_ptr_ulong(pdwActiveProtocol);
487 spy_long(dwDisposition);
488 rv = spy.SCardDisconnect(hCard, dwDisposition);
499 rv = spy.SCardBeginTransaction(hCard);
510 spy_long(dwDisposition);
511 rv = spy.SCardEndTransaction(hCard, dwDisposition);
519 int autoallocate_ReaderName = 0, autoallocate_Atr = 0;
529 spy_ptr_ulong(pcchReaderLen);
530 spy_ptr_ulong(pcbAtrLen);
531 rv = spy.SCardStatus(hCard, mszReaderName, pcchReaderLen, pdwState,
532 pdwProtocol, pbAtr, pcbAtrLen);
533 spy_n_str(mszReaderName, pcchReaderLen, autoallocate_ReaderName);
534 spy_ptr_ulong(pdwState);
535 spy_ptr_ulong(pdwProtocol);
536 if (NULL == pcbAtrLen)
542 if (autoallocate_Atr)
543 buffer = *(LPBYTE *)pbAtr;
547 spy_buffer(buffer, *pcbAtrLen);
561 spy_readerstate(rgReaderStates, cReaders);
562 rv = spy.SCardGetStatusChange(hContext, dwTimeout, rgReaderStates,
564 spy_readerstate(rgReaderStates, cReaders);
575 spy_long(dwControlCode);
576 spy_buffer(pbSendBuffer, cbSendLength);
577 rv = spy.SCardControl(hCard, dwControlCode, pbSendBuffer, cbSendLength,
578 pbRecvBuffer, cbRecvLength, lpBytesReturned);
580 spy_buffer(pbRecvBuffer, *lpBytesReturned);
593 spy_buffer(pbSendBuffer, cbSendLength);
594 rv = spy.SCardTransmit(hCard, pioSendPci, pbSendBuffer, cbSendLength,
595 pioRecvPci, pbRecvBuffer, pcbRecvLength);
597 spy_buffer(pbRecvBuffer, *pcbRecvLength);
607 int autoallocate = 0;
614 spy_ptr_ulong(pcchGroups);
615 rv = spy.SCardListReaderGroups(hContext, mszGroups, pcchGroups);
617 spy_n_str(mszGroups, pcchGroups, autoallocate);
619 spy_n_str(NULL, pcchGroups, 0);
627 int autoallocate = 0;
635 rv = spy.SCardListReaders(hContext, mszGroups, mszReaders, pcchReaders);
637 spy_n_str(mszReaders, pcchReaders, autoallocate);
639 spy_n_str(NULL, pcchReaders, 0);
651 rv = spy.SCardFreeMemory(hContext, pvMem);
662 rv = spy.SCardCancel(hContext);
670 int autoallocate = 0;
678 rv = spy.SCardGetAttrib(hCard, dwAttrId, pbAttr, pcbAttrLen);
679 if (NULL == pcbAttrLen)
686 buffer = *(LPBYTE *)pbAttr;
690 spy_buffer(buffer, *pcbAttrLen);
703 spy_buffer(pbAttr, cbAttrLen);
704 rv = spy.SCardSetAttrib(hCard, dwAttrId, pbAttr, cbAttrLen);
711 return spy.pcsc_stringify_error(pcscError);
PCSC_API LONG SCardFreeMemory(SCARDCONTEXT hContext, LPCVOID pvMem)
Releases memory that has been returned from the resource manager using the SCARD_AUTOALLOCATE length ...
PCSC_API LONG SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
Set an attribute of the IFD Handler.
PCSC_API LONG SCardGetStatusChange(SCARDCONTEXT hContext, DWORD dwTimeout, SCARD_READERSTATE *rgReaderStates, DWORD cReaders)
Blocks execution until the current availability of the cards in a specific set of readers changes.
PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
Terminates a connection made through SCardConnect().
PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
Establishes a connection to the reader specified in * szReader.
PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext)
Destroys a communication context to the PC/SC Resource Manager.
PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext)
Check if a SCARDCONTEXT is valid.
PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
Returns a list of currently available readers on the system.
PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength)
Sends an APDU to the smart card contained in the reader connected to by SCardConnect().
PCSC_API LONG SCardListReaderGroups(SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
Returns a list of currently available reader groups on the system.
PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
Creates an Application Context to the PC/SC Resource Manager.
PCSC_API LONG SCardCancel(SCARDCONTEXT hContext)
Cancels a specific blocking SCardGetStatusChange() function.
PCSC_API const char * pcsc_stringify_error(const LONG pcscError)
Returns a human readable text for the given PC/SC error code.
PCSC_API LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
Get an attribute from the IFD Handler (reader driver).
PCSC_API LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.
PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol)
Reestablishes a connection to a reader that was previously connected to using SCardConnect().
PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard)
Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.
PCSC_API LONG SCardStatus(SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
Returns the current status of the reader connected to by hCard.
PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
Ends a previously begun transaction.
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.
#define SCARD_S_SUCCESS
No error was encountered.
#define SCARD_PROTOCOL_T1
T=1 active protocol.
#define SCARD_PROTOCOL_T0
T=0 active protocol.
#define SCARD_AUTOALLOCATE
see SCardFreeMemory()
#define MAX_ATR_SIZE
Maximum ATR size.
#define SCARD_PROTOCOL_RAW
Raw active protocol.
const SCARD_IO_REQUEST g_rgSCardT0Pci
Protocol Control Information for T=0.
Protocol Control Information (PCI)
This handles smart card reader communications.
PCSC_API const SCARD_IO_REQUEST g_rgSCardRawPci
Protocol Control Information for raw access.
PCSC_API const SCARD_IO_REQUEST g_rgSCardT1Pci
Protocol Control Information for T=1.