38 #define SCardControl SCardControl132
40 PCSC_API int32_t SCardControl132(
SCARDHANDLE hCard, uint32_t dwControlCode,
41 const void *pbSendBuffer, uint32_t cbSendLength,
42 void *pbRecvBuffer, uint32_t cbRecvLength, uint32_t *lpBytesReturned);
47 #define p_SCardEstablishContext(fct) LONG(fct)(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
49 #define p_SCardReleaseContext(fct) LONG(fct)(SCARDCONTEXT hContext)
51 #define p_SCardIsValidContext(fct) LONG(fct) (SCARDCONTEXT hContext)
53 #define p_SCardConnect(fct) LONG(fct) (SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
55 #define p_SCardReconnect(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol)
57 #define p_SCardDisconnect(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwDisposition)
59 #define p_SCardBeginTransaction(fct) LONG(fct) (SCARDHANDLE hCard)
61 #define p_SCardEndTransaction(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwDisposition)
63 #define p_SCardStatus(fct) LONG(fct) (SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
65 #define p_SCardGetStatusChange(fct) LONG(fct) (SCARDCONTEXT hContext, DWORD dwTimeout, LPSCARD_READERSTATE rgReaderStates, DWORD cReaders)
67 #define p_SCardControl(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
69 #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)
71 #define p_SCardListReaderGroups(fct) LONG(fct) (SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
73 #define p_SCardListReaders(fct) LONG(fct) (SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
75 #define p_SCardFreeMemory(fct) LONG(fct) (SCARDCONTEXT hContext, LPCVOID pvMem)
77 #define p_SCardCancel(fct) LONG(fct) (SCARDCONTEXT hContext)
79 #define p_SCardGetAttrib(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
81 #define p_SCardSetAttrib(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
83 #define p_pcsc_stringify_error(fct) char *(fct)(const LONG pcscError)
86 static LONG internal_error(
void)
91 static const char * internal_stringify_error(
void)
93 return "No spy pcsc_stringify_error() function";
120 .SCardEstablishContext = (p_SCardEstablishContext(*))internal_error,
141 #define LOG log_line("%s:%d", __FILE__, __LINE__)
143 static int Log_fd = -1;
144 static void *Lib_handle = NULL;
145 static pthread_mutex_t Log_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
148 static void log_line(
const char *fmt, ...)
158 static void log_line(
const char *fmt, ...)
163 static void spy_line_direct(
char *line)
171 snprintf(threadid,
sizeof threadid,
"%lX@", pthread_self());
172 pthread_mutex_lock(&Log_fd_mutex);
173 write(Log_fd, threadid, strlen(threadid));
174 write(Log_fd, line, strlen(line));
175 write(Log_fd,
"\n", 1);
176 pthread_mutex_unlock(&Log_fd_mutex);
179 static void spy_line(
const char *fmt, ...)
191 size = vsnprintf(line,
sizeof line, fmt, args);
193 if ((
size_t)size >=
sizeof line)
195 printf(
"libpcsc-spy: Buffer is too small!\n");
198 snprintf(threadid,
sizeof threadid,
"%lX@", pthread_self());
199 pthread_mutex_lock(&Log_fd_mutex);
200 write(Log_fd, threadid, strlen(threadid));
201 write(Log_fd, line, size);
202 write(Log_fd,
"\n", 1);
203 pthread_mutex_unlock(&Log_fd_mutex);
206 static void spy_enter(
const char *fname)
208 struct timeval profile_time;
210 gettimeofday(&profile_time, NULL);
211 spy_line(
">|%d|%d|%s", profile_time.tv_sec, profile_time.tv_usec, fname);
214 static void spy_quit(
const char *fname, LONG rv)
216 struct timeval profile_time;
218 gettimeofday(&profile_time, NULL);
219 spy_line(
"<|%d|%d|%s|%s|0x%08X", profile_time.tv_sec,
220 profile_time.tv_usec, fname, spy.pcsc_stringify_error(rv), rv);
223 #define Enter() spy_enter(__FUNCTION__)
224 #define Quit() spy_quit(__FUNCTION__, rv)
226 static void spy_long(
long arg)
228 spy_line(
"0x%08lX", arg);
231 static void spy_ptr_long(LONG *arg)
234 spy_line(
"0x%08lX", *arg);
239 static void spy_ptr_ulong(ULONG *arg)
242 spy_line(
"0x%08lX", *arg);
247 static void spy_pvoid(
const void *ptr)
252 static void spy_buffer(
const unsigned char *buffer,
size_t length)
261 char log_buffer[length * 3 +1], *p;
265 log_buffer[0] =
'\0';
266 for (i=0; i<length; i++)
268 snprintf(p, 4,
"%02X ", buffer[i]);
273 spy_line_direct(log_buffer);
277 static void spy_str(
const char *str)
282 static void spy_n_str(
const char *str, ULONG *len,
int autoallocate)
298 unsigned int length = 0;
306 length += strlen(s)+1;
308 }
while(length < *len);
318 for (i=0; i<cReaders; i++)
320 spy_str(rgReaderStates[i].szReader);
321 spy_long(rgReaderStates[i].dwCurrentState);
322 spy_long(rgReaderStates[i].dwEventState);
324 spy_buffer(rgReaderStates[i].rgbAtr, rgReaderStates[i].cbAtr);
326 spy_buffer(NULL, rgReaderStates[i].cbAtr);
330 static LONG load_lib(
void)
340 #define LIBPCSC_NOSPY "/tmp/PCSC"
341 #define LIBPCSC "/tmp/PCSC"
343 #define LIBPCSC_NOSPY "libpcsclite_nospy.so.1"
344 #define LIBPCSC "libpcsclite.so.1"
350 Lib_handle = dlopen(LIBPCSC_NOSPY, RTLD_LAZY);
351 if (NULL == Lib_handle)
353 log_line(
"%s", dlerror());
356 Lib_handle = dlopen(LIBPCSC, RTLD_LAZY);
357 if (NULL == Lib_handle)
359 log_line(
"%s", dlerror());
364 #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)
368 log_line(
"Symbols dlsym error");
387 if (dlsym(Lib_handle,
"SCardFreeMemory"))
417 home = getenv(
"HOME");
421 snprintf(log_pipe,
sizeof log_pipe,
"%s/pcsc-spy", home);
422 Log_fd = open(log_pipe, O_WRONLY);
425 log_line(
"open %s failed: %s", log_pipe, strerror(errno));
431 rv = spy.SCardEstablishContext(dwScope, pvReserved1, pvReserved2,
433 spy_ptr_long(phContext);
444 rv = spy.SCardReleaseContext(hContext);
455 rv = spy.SCardIsValidContext(hContext);
467 spy_long(dwShareMode);
468 spy_long(dwPreferredProtocols);
469 spy_ptr_long(phCard);
470 spy_ptr_ulong(pdwActiveProtocol);
471 rv = spy.SCardConnect(hContext, szReader, dwShareMode,
472 dwPreferredProtocols, phCard, pdwActiveProtocol);
473 spy_ptr_long(phCard);
474 spy_ptr_ulong(pdwActiveProtocol);
485 spy_long(dwShareMode);
486 spy_long(dwPreferredProtocols);
487 spy_long(dwInitialization);
488 rv = spy.SCardReconnect(hCard, dwShareMode, dwPreferredProtocols,
489 dwInitialization, pdwActiveProtocol);
490 spy_ptr_ulong(pdwActiveProtocol);
501 spy_long(dwDisposition);
502 rv = spy.SCardDisconnect(hCard, dwDisposition);
513 rv = spy.SCardBeginTransaction(hCard);
524 spy_long(dwDisposition);
525 rv = spy.SCardEndTransaction(hCard, dwDisposition);
533 int autoallocate_ReaderName = 0, autoallocate_Atr = 0;
543 spy_ptr_ulong(pcchReaderLen);
544 spy_ptr_ulong(pcbAtrLen);
545 rv = spy.SCardStatus(hCard, mszReaderName, pcchReaderLen, pdwState,
546 pdwProtocol, pbAtr, pcbAtrLen);
547 spy_n_str(mszReaderName, pcchReaderLen, autoallocate_ReaderName);
548 spy_ptr_ulong(pdwState);
549 spy_ptr_ulong(pdwProtocol);
550 if (NULL == pcbAtrLen)
554 if (autoallocate_Atr)
556 const unsigned char *b = *(
unsigned char **)pbAtr;
558 spy_buffer(b, *pcbAtrLen);
561 spy_buffer(pbAtr, *pcbAtrLen);
575 spy_readerstate(rgReaderStates, cReaders);
576 rv = spy.SCardGetStatusChange(hContext, dwTimeout, rgReaderStates,
578 spy_readerstate(rgReaderStates, cReaders);
589 spy_long(dwControlCode);
590 spy_buffer(pbSendBuffer, cbSendLength);
591 rv = spy.SCardControl(hCard, dwControlCode, pbSendBuffer, cbSendLength,
592 pbRecvBuffer, cbRecvLength, lpBytesReturned);
594 spy_buffer(pbRecvBuffer, *lpBytesReturned);
607 spy_buffer(pbSendBuffer, cbSendLength);
608 rv = spy.SCardTransmit(hCard, pioSendPci, pbSendBuffer, cbSendLength,
609 pioRecvPci, pbRecvBuffer, pcbRecvLength);
611 spy_buffer(pbRecvBuffer, *pcbRecvLength);
621 int autoallocate = 0;
628 spy_ptr_ulong(pcchGroups);
629 rv = spy.SCardListReaderGroups(hContext, mszGroups, pcchGroups);
630 spy_n_str(mszGroups, pcchGroups, autoallocate);
638 int autoallocate = 0;
646 rv = spy.SCardListReaders(hContext, mszGroups, mszReaders, pcchReaders);
647 spy_n_str(mszReaders, pcchReaders, autoallocate);
659 rv = spy.SCardFreeMemory(hContext, pvMem);
670 rv = spy.SCardCancel(hContext);
678 int autoallocate = 0;
686 rv = spy.SCardGetAttrib(hCard, dwAttrId, pbAttr, pcbAttrLen);
687 if (NULL == pcbAttrLen)
691 const unsigned char *s = pbAttr;
694 s = *(
unsigned char **)pbAttr;
696 spy_buffer(s, *pcbAttrLen);
709 spy_buffer(pbAttr, cbAttrLen);
710 rv = spy.SCardSetAttrib(hCard, dwAttrId, pbAttr, cbAttrLen);
717 return spy.pcsc_stringify_error(pcscError);
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 SCardListReaderGroups(SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
Returns a list of currently available reader groups on the system.
PCSC_API char * pcsc_stringify_error(const LONG pcscError)
Returns a human readable text for the given PC/SC error code.
PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
Creates an Application Context to the PC/SC Resource Manager.
#define SCARD_PROTOCOL_T1
T=1 active protocol.
PCSC_API LONG SCardFreeMemory(SCARDCONTEXT hContext, LPCVOID pvMem)
Releases memory that has been returned from the resource manager using the SCARD_AUTOALLOCATE length ...
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 SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
Get an attribute from the IFD Handler (reader driver).
PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
Terminates a connection made through SCardConnect().
PCSC_API LONG SCardCancel(SCARDCONTEXT hContext)
Cancels all pending blocking requests on the SCardGetStatusChange() function.
const SCARD_IO_REQUEST g_rgSCardRawPci
Protocol Control Information for raw access.
PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext)
Check if a SCARDCONTEXT is valid.
#define SCARD_AUTOALLOCATE
see SCardFreeMemory()
#define SCARD_PROTOCOL_T0
T=0 active protocol.
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 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 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 SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
Set an attribute of the IFD Handler.
LONG SCARDHANDLE
hCard returned by SCardConnect()
PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard)
Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.
#define SCARD_PROTOCOL_RAW
Raw active protocol.
Protocol Control Information (PCI)
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...
#define MAX_ATR_SIZE
Maximum ATR size.
PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
Ends a previously begun transaction.
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().
const SCARD_IO_REQUEST g_rgSCardT1Pci
Protocol Control Information for T=1.
#define SCARD_S_SUCCESS
error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx
const SCARD_IO_REQUEST g_rgSCardT0Pci
Protocol Control Information for T=0.
This handles smart card reader communications.
PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext)
Destroys a communication context to the PC/SC Resource Manager.
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.