36 #define SCardControl SCardControl132
38 PCSC_API int32_t SCardControl132(
SCARDHANDLE hCard, uint32_t dwControlCode,
39 const void *pbSendBuffer, uint32_t cbSendLength,
40 void *pbRecvBuffer, uint32_t cbRecvLength, uint32_t *lpBytesReturned);
45 #define p_SCardEstablishContext(fct) LONG(fct)(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
47 #define p_SCardReleaseContext(fct) LONG(fct)(SCARDCONTEXT hContext)
49 #define p_SCardIsValidContext(fct) LONG(fct) (SCARDCONTEXT hContext)
51 #define p_SCardConnect(fct) LONG(fct) (SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
53 #define p_SCardReconnect(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol)
55 #define p_SCardDisconnect(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwDisposition)
57 #define p_SCardBeginTransaction(fct) LONG(fct) (SCARDHANDLE hCard)
59 #define p_SCardEndTransaction(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwDisposition)
61 #define p_SCardStatus(fct) LONG(fct) (SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
63 #define p_SCardGetStatusChange(fct) LONG(fct) (SCARDCONTEXT hContext, DWORD dwTimeout, LPSCARD_READERSTATE rgReaderStates, DWORD cReaders)
65 #define p_SCardControl(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
67 #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)
69 #define p_SCardListReaderGroups(fct) LONG(fct) (SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
71 #define p_SCardListReaders(fct) LONG(fct) (SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
73 #define p_SCardFreeMemory(fct) LONG(fct) (SCARDCONTEXT hContext, LPCVOID pvMem)
75 #define p_SCardCancel(fct) LONG(fct) (SCARDCONTEXT hContext)
77 #define p_SCardGetAttrib(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
79 #define p_SCardSetAttrib(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
81 #define p_pcsc_stringify_error(fct) const char *(fct)(const LONG pcscError)
84 static LONG internal_error(
void)
89 static const char * internal_stringify_error(
void)
91 return "No spy pcsc_stringify_error() function";
94 #pragma GCC diagnostic push
95 #pragma GCC diagnostic ignored "-Wcast-function-type"
120 .SCardEstablishContext = (p_SCardEstablishContext(*))internal_error,
140 #pragma GCC diagnostic pop
142 #define LOG log_line("%s:%d", __FILE__, __LINE__)
144 static int Log_fd = -1;
145 static void *Lib_handle = NULL;
146 static pthread_mutex_t Log_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
149 static void log_line(
const char *fmt, ...)
159 static void log_line(
const char *fmt, ...)
164 static void spy_line_direct(
char *line)
173 snprintf(threadid,
sizeof threadid,
"%lX@", pthread_self());
174 pthread_mutex_lock(&Log_fd_mutex);
175 r = write(Log_fd, threadid, strlen(threadid));
176 r = write(Log_fd, line, strlen(line));
177 r = write(Log_fd,
"\n", 1);
179 pthread_mutex_unlock(&Log_fd_mutex);
182 static void spy_line(
const char *fmt, ...)
195 size = vsnprintf(line,
sizeof line, fmt, args);
197 if ((
size_t)size >=
sizeof line)
199 printf(
"libpcsc-spy: Buffer is too small!\n");
202 snprintf(threadid,
sizeof threadid,
"%lX@", pthread_self());
203 pthread_mutex_lock(&Log_fd_mutex);
204 r = write(Log_fd, threadid, strlen(threadid));
205 r = write(Log_fd, line, size);
206 r = write(Log_fd,
"\n", 1);
208 pthread_mutex_unlock(&Log_fd_mutex);
211 static void spy_enter(
const char *fname)
213 struct timeval profile_time;
215 gettimeofday(&profile_time, NULL);
216 spy_line(
">|%ld|%ld|%s", profile_time.tv_sec, profile_time.tv_usec, fname);
219 static void spy_quit(
const char *fname, LONG rv)
221 struct timeval profile_time;
223 gettimeofday(&profile_time, NULL);
224 spy_line(
"<|%ld|%ld|%s|%s|0x%08lX", profile_time.tv_sec,
225 profile_time.tv_usec, fname, spy.pcsc_stringify_error(rv), rv);
228 #define Enter() spy_enter(__FUNCTION__)
229 #define Quit() spy_quit(__FUNCTION__, rv)
231 static void spy_long(
long arg)
233 spy_line(
"0x%08lX", arg);
236 static void spy_ptr_long(LONG *arg)
239 spy_line(
"0x%08lX", *arg);
244 static void spy_ptr_ulong(ULONG *arg)
247 spy_line(
"0x%08lX", *arg);
252 static void spy_pvoid(
const void *ptr)
257 static void spy_buffer(
const unsigned char *buffer,
size_t length)
266 char log_buffer[length * 3 +1], *p;
270 log_buffer[0] =
'\0';
271 for (i=0; i<length; i++)
273 snprintf(p, 4,
"%02X ", buffer[i]);
278 spy_line_direct(log_buffer);
282 static void spy_str(
const char *str)
287 static void spy_n_str(
const char *str, ULONG *len,
int autoallocate)
303 unsigned int length = 0;
311 length += strlen(s)+1;
313 }
while(length < *len);
323 for (i=0; i<cReaders; i++)
325 spy_str(rgReaderStates[i].szReader);
326 spy_long(rgReaderStates[i].dwCurrentState);
327 spy_long(rgReaderStates[i].dwEventState);
329 spy_buffer(rgReaderStates[i].rgbAtr, rgReaderStates[i].cbAtr);
331 spy_buffer(NULL, rgReaderStates[i].cbAtr);
335 static LONG load_lib(
void)
345 #define LIBPCSC_NOSPY "/tmp/PCSC"
346 #define LIBPCSC "/tmp/PCSC"
348 #define LIBPCSC_NOSPY "libpcsclite_nospy.so.1"
349 #define LIBPCSC "libpcsclite.so.1"
355 Lib_handle = dlopen(LIBPCSC_NOSPY, RTLD_LAZY);
356 if (NULL == Lib_handle)
358 log_line(
"%s", dlerror());
361 Lib_handle = dlopen(LIBPCSC, RTLD_LAZY);
362 if (NULL == Lib_handle)
364 log_line(
"%s", dlerror());
369 #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)
373 log_line(
"Symbols dlsym error");
392 if (dlsym(Lib_handle,
"SCardFreeMemory"))
422 home = getenv(
"HOME");
426 snprintf(log_pipe,
sizeof log_pipe,
"%s/pcsc-spy", home);
427 Log_fd = open(log_pipe, O_WRONLY);
430 log_line(
"open %s failed: %s", log_pipe, strerror(errno));
436 rv = spy.SCardEstablishContext(dwScope, pvReserved1, pvReserved2,
438 spy_ptr_long(phContext);
449 rv = spy.SCardReleaseContext(hContext);
460 rv = spy.SCardIsValidContext(hContext);
472 spy_long(dwShareMode);
473 spy_long(dwPreferredProtocols);
474 spy_ptr_long(phCard);
475 spy_ptr_ulong(pdwActiveProtocol);
476 rv = spy.SCardConnect(hContext, szReader, dwShareMode,
477 dwPreferredProtocols, phCard, pdwActiveProtocol);
478 spy_ptr_long(phCard);
479 spy_ptr_ulong(pdwActiveProtocol);
490 spy_long(dwShareMode);
491 spy_long(dwPreferredProtocols);
492 spy_long(dwInitialization);
493 rv = spy.SCardReconnect(hCard, dwShareMode, dwPreferredProtocols,
494 dwInitialization, pdwActiveProtocol);
495 spy_ptr_ulong(pdwActiveProtocol);
506 spy_long(dwDisposition);
507 rv = spy.SCardDisconnect(hCard, dwDisposition);
518 rv = spy.SCardBeginTransaction(hCard);
529 spy_long(dwDisposition);
530 rv = spy.SCardEndTransaction(hCard, dwDisposition);
538 int autoallocate_ReaderName = 0, autoallocate_Atr = 0;
548 spy_ptr_ulong(pcchReaderLen);
549 spy_ptr_ulong(pcbAtrLen);
550 rv = spy.SCardStatus(hCard, mszReaderName, pcchReaderLen, pdwState,
551 pdwProtocol, pbAtr, pcbAtrLen);
552 spy_n_str(mszReaderName, pcchReaderLen, autoallocate_ReaderName);
553 spy_ptr_ulong(pdwState);
554 spy_ptr_ulong(pdwProtocol);
555 if (NULL == pcbAtrLen)
561 if (autoallocate_Atr)
562 buffer = *(LPBYTE *)pbAtr;
566 spy_buffer(buffer, *pcbAtrLen);
580 spy_readerstate(rgReaderStates, cReaders);
581 rv = spy.SCardGetStatusChange(hContext, dwTimeout, rgReaderStates,
583 spy_readerstate(rgReaderStates, cReaders);
594 spy_long(dwControlCode);
595 spy_buffer(pbSendBuffer, cbSendLength);
596 rv = spy.SCardControl(hCard, dwControlCode, pbSendBuffer, cbSendLength,
597 pbRecvBuffer, cbRecvLength, lpBytesReturned);
599 spy_buffer(pbRecvBuffer, *lpBytesReturned);
612 spy_buffer(pbSendBuffer, cbSendLength);
613 rv = spy.SCardTransmit(hCard, pioSendPci, pbSendBuffer, cbSendLength,
614 pioRecvPci, pbRecvBuffer, pcbRecvLength);
616 spy_buffer(pbRecvBuffer, *pcbRecvLength);
626 int autoallocate = 0;
633 spy_ptr_ulong(pcchGroups);
634 rv = spy.SCardListReaderGroups(hContext, mszGroups, pcchGroups);
636 spy_n_str(mszGroups, pcchGroups, autoallocate);
638 spy_n_str(NULL, pcchGroups, 0);
646 int autoallocate = 0;
654 rv = spy.SCardListReaders(hContext, mszGroups, mszReaders, pcchReaders);
656 spy_n_str(mszReaders, pcchReaders, autoallocate);
658 spy_n_str(NULL, pcchReaders, 0);
670 rv = spy.SCardFreeMemory(hContext, pvMem);
681 rv = spy.SCardCancel(hContext);
689 int autoallocate = 0;
697 rv = spy.SCardGetAttrib(hCard, dwAttrId, pbAttr, pcbAttrLen);
698 if (NULL == pcbAttrLen)
705 buffer = *(LPBYTE *)pbAttr;
709 spy_buffer(buffer, *pcbAttrLen);
722 spy_buffer(pbAttr, cbAttrLen);
723 rv = spy.SCardSetAttrib(hCard, dwAttrId, pbAttr, cbAttrLen);
730 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.
LONG SCARDHANDLE
hCard returned by SCardConnect()
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.