From b3114ff4d8649f024f84b160a0e7b50adb073c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andre=CC=81=20Moreau?= Date: Tue, 6 May 2014 10:00:44 -0700 Subject: [PATCH] channels/smartcard: improve debug output, fix GetStatusChange SCARD_E_CANCELLED, fix SCardGetAttrib current protocol type --- channels/smartcard/client/smartcard_operations.c | 16 ++++++++++++++-- channels/smartcard/client/smartcard_pack.c | 16 +++++++++++++++- channels/smartcard/client/smartcard_pack.h | 2 ++ winpr/libwinpr/smartcard/smartcard_pcsc.c | 3 ++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/channels/smartcard/client/smartcard_operations.c b/channels/smartcard/client/smartcard_operations.c index a80e39e11..a188e6fa1 100644 --- a/channels/smartcard/client/smartcard_operations.c +++ b/channels/smartcard/client/smartcard_operations.c @@ -194,6 +194,8 @@ static UINT32 smartcard_ReleaseContext(SMARTCARD_DEVICE* smartcard, IRP* irp) status = ret.ReturnCode = SCardReleaseContext(hContext); + smartcard_trace_long_return(smartcard, &ret, "ReleaseContext"); + return ret.ReturnCode; } @@ -215,6 +217,8 @@ static UINT32 smartcard_IsValidContext(SMARTCARD_DEVICE* smartcard, IRP* irp) status = ret.ReturnCode = SCardIsValidContext(hContext); + smartcard_trace_long_return(smartcard, &ret, "IsValidContext"); + return ret.ReturnCode; } @@ -326,7 +330,7 @@ static UINT32 smartcard_GetStatusChangeA(SMARTCARD_DEVICE* smartcard, IRP* irp) status = ret.ReturnCode = SCardGetStatusChangeA(hContext, call.dwTimeOut, call.rgReaderStates, call.cReaders); - if (status && (status != SCARD_E_TIMEOUT)) + if (status && (status != SCARD_E_TIMEOUT) && (status != SCARD_E_CANCELLED)) return status; ret.cReaders = call.cReaders; @@ -384,7 +388,7 @@ static UINT32 smartcard_GetStatusChangeW(SMARTCARD_DEVICE* smartcard, IRP* irp) status = ret.ReturnCode = SCardGetStatusChangeW(hContext, call.dwTimeOut, call.rgReaderStates, call.cReaders); - if (status && (status != SCARD_E_TIMEOUT)) + if (status && (status != SCARD_E_TIMEOUT) && (status != SCARD_E_CANCELLED)) return status; ret.cReaders = call.cReaders; @@ -440,6 +444,8 @@ static UINT32 smartcard_Cancel(SMARTCARD_DEVICE* smartcard, IRP* irp) status = ret.ReturnCode = SCardCancel(hContext); + smartcard_trace_long_return(smartcard, &ret, "Cancel"); + return ret.ReturnCode; } @@ -591,6 +597,8 @@ static UINT32 smartcard_Disconnect(SMARTCARD_DEVICE* smartcard, IRP* irp) status = ret.ReturnCode = SCardDisconnect(hCard, call.dwDisposition); + smartcard_trace_long_return(smartcard, &ret, "Disconnect"); + if (status) return status; @@ -617,6 +625,8 @@ static UINT32 smartcard_BeginTransaction(SMARTCARD_DEVICE* smartcard, IRP* irp) status = ret.ReturnCode = SCardBeginTransaction(hCard); + smartcard_trace_long_return(smartcard, &ret, "BeginTransaction"); + if (status) return status; @@ -643,6 +653,8 @@ static UINT32 smartcard_EndTransaction(SMARTCARD_DEVICE* smartcard, IRP* irp) status = ret.ReturnCode = SCardEndTransaction(hCard, call.dwDisposition); + smartcard_trace_long_return(smartcard, &ret, "EndTransaction"); + if (status) return status; diff --git a/channels/smartcard/client/smartcard_pack.c b/channels/smartcard/client/smartcard_pack.c index 322333c95..88373eaac 100644 --- a/channels/smartcard/client/smartcard_pack.c +++ b/channels/smartcard/client/smartcard_pack.c @@ -459,7 +459,8 @@ void smartcard_trace_establish_context_return(SMARTCARD_DEVICE* smartcard, Estab WLog_Print(smartcard->log, WLOG_DEBUG, "EstablishContext_Return {"); - WLog_Print(smartcard->log, WLOG_DEBUG, "ReturnCode: 0x%08X", ret->ReturnCode); + WLog_Print(smartcard->log, WLOG_DEBUG, "ReturnCode: %s (0x%08X)", + SCardGetErrorString(ret->ReturnCode), ret->ReturnCode); pb = (BYTE*) &(ret->hContext.pbContext); @@ -519,6 +520,19 @@ void smartcard_trace_context_call(SMARTCARD_DEVICE* smartcard, Context_Call* cal WLog_Print(smartcard->log, WLOG_DEBUG, "}"); } +void smartcard_trace_long_return(SMARTCARD_DEVICE* smartcard, Long_Return* ret, const char* name) +{ + if (!WLog_IsLevelActive(smartcard->log, WLOG_DEBUG)) + return; + + WLog_Print(smartcard->log, WLOG_DEBUG, "%s_Return {", name); + + WLog_Print(smartcard->log, WLOG_DEBUG, "ReturnCode: %s (0x%08X)", + SCardGetErrorString(ret->ReturnCode), ret->ReturnCode); + + WLog_Print(smartcard->log, WLOG_DEBUG, "}"); +} + UINT32 smartcard_unpack_list_readers_call(SMARTCARD_DEVICE* smartcard, wStream* s, ListReaders_Call* call) { UINT32 status; diff --git a/channels/smartcard/client/smartcard_pack.h b/channels/smartcard/client/smartcard_pack.h index f041f1e3c..fdf17087c 100644 --- a/channels/smartcard/client/smartcard_pack.h +++ b/channels/smartcard/client/smartcard_pack.h @@ -465,6 +465,8 @@ void smartcard_trace_establish_context_return(SMARTCARD_DEVICE* smartcard, Estab UINT32 smartcard_unpack_context_call(SMARTCARD_DEVICE* smartcard, wStream* s, Context_Call* call); void smartcard_trace_context_call(SMARTCARD_DEVICE* smartcard, Context_Call* call, const char* name); +void smartcard_trace_long_return(SMARTCARD_DEVICE* smartcard, Long_Return* ret, const char* name); + UINT32 smartcard_unpack_list_readers_call(SMARTCARD_DEVICE* smartcard, wStream* s, ListReaders_Call* call); void smartcard_trace_list_readers_call(SMARTCARD_DEVICE* smartcard, ListReaders_Call* call, BOOL unicode); diff --git a/winpr/libwinpr/smartcard/smartcard_pcsc.c b/winpr/libwinpr/smartcard/smartcard_pcsc.c index fd3fec1f6..7411c4f8f 100644 --- a/winpr/libwinpr/smartcard/smartcard_pcsc.c +++ b/winpr/libwinpr/smartcard/smartcard_pcsc.c @@ -2118,10 +2118,11 @@ WINSCARDAPI LONG WINAPI PCSC_SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, L { LPDWORD pdwProtocol = (LPDWORD) pbAttr; - if (cbAttrLen < 4) + if (cbAttrLen < sizeof(DWORD)) return SCARD_E_INSUFFICIENT_BUFFER; *pdwProtocol = PCSC_ConvertProtocolsToWinSCard(dwProtocol); + *pcbAttrLen = sizeof(DWORD); } } else if (dwAttrId == SCARD_ATTR_VENDOR_IFD_TYPE)