Skip to content

Commit

Permalink
Updated SCardGetAttrib()
Browse files Browse the repository at this point in the history
Also removed unimplementable SCARD_CANCEL_TRANSACTION
  • Loading branch information
matt335672 committed Mar 30, 2024
1 parent 8ac242a commit 83a9be2
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 42 deletions.
100 changes: 97 additions & 3 deletions sesman/chansrv/pcsc/xrdp_pcsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ PCSC_API LONG
SCardListReaders(SCARDCONTEXT hContext, /* @unused */ LPCSTR mszGroups,
LPSTR mszReaders, LPDWORD pcchReaders)
{
char msg[8192];
char msg[256];
unsigned int code;
unsigned int bytes;
unsigned int offset;
Expand Down Expand Up @@ -1209,7 +1209,7 @@ SCardListReaders(SCARDCONTEXT hContext, /* @unused */ LPCSTR mszGroups,

bytes = sizeof(msg);
code = SCARD_LIST_READERS;
if (get_message(&code, msg, &bytes) != 0)
if (get_message(&code, msg, &bytes) != 0 || bytes < 8)
{
LLOGLN(0, ("SCardListReaders: error, get_message"));
return SCARD_F_INTERNAL_ERROR;
Expand Down Expand Up @@ -1249,6 +1249,11 @@ SCardListReaders(SCARDCONTEXT hContext, /* @unused */ LPCSTR mszGroups,
{
ReturnCode = SCARD_E_INSUFFICIENT_BUFFER;
}
else if ((bytes - offset) < cBytes)
{
LLOGLN(0, ("SCardListReaders: error, missing buffer"));
ReturnCode = SCARD_F_INTERNAL_ERROR;
}
else
{
memcpy(mszReaders, msg + offset, cBytes);
Expand Down Expand Up @@ -1284,13 +1289,102 @@ PCSC_API LONG
SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr,
LPDWORD pcbAttrLen)
{
char msg[256];
unsigned int code;
unsigned int bytes;
unsigned int offset;
LONG ReturnCode;
unsigned int fpbAttrIsNULL = (pbAttr == NULL);
unsigned int cbAttrLen;

LLOGLN(0, ("SCardGetAttrib:"));
if (g_sck == -1)
{
LLOGLN(0, ("SCardGetAttrib: error, not connected"));
return SCARD_F_INTERNAL_ERROR;
}
return SCARD_S_SUCCESS;

if (pcbAttrLen == NULL)
{
return SCARD_E_INVALID_PARAMETER;
}

if (*pcbAttrLen == SCARD_AUTOALLOCATE && fpbAttrIsNULL)
{
return SCARD_E_INVALID_PARAMETER;
}

offset = 0;
SET_UINT32(msg, offset, hCard);
offset += 4;
SET_UINT32(msg, offset, dwAttrId);
offset += 4;
SET_UINT32(msg, offset, fpbAttrIsNULL);
offset += 4;
SET_UINT32(msg, offset, *pcbAttrLen);
offset += 4;

if (send_message(SCARD_GET_ATTRIB, msg, offset) != 0)
{
LLOGLN(0, ("SCardGetAttrib: error, send_message"));
return SCARD_F_INTERNAL_ERROR;
}

bytes = sizeof(msg);
code = SCARD_GET_ATTRIB;
if (get_message(&code, msg, &bytes) != 0 || bytes < 8)
{
LLOGLN(0, ("SCardGetAttrib: error, get_message"));
return SCARD_F_INTERNAL_ERROR;
}
if (code != SCARD_GET_ATTRIB)
{
LLOGLN(0, ("SCardGetAttrib: error, bad code"));
return SCARD_F_INTERNAL_ERROR;
}
offset = 0;
ReturnCode = GET_UINT32(msg, offset);
LLOGLN(10, ("SCardListReaders: status 0x%8.8x", (int)ReturnCode));
offset += 4;
cbAttrLen = GET_UINT32(msg, offset);
offset += 4;

if (ReturnCode == SCARD_S_SUCCESS)
{
// auto-allocate memory, if the user has requested it
if (*pcbAttrLen == SCARD_AUTOALLOCATE)
{
LPBYTE attr_out;
if ((attr_out = (LPBYTE)malloc(cbAttrLen)) == NULL)
{
return SCARD_E_NO_MEMORY;
}
*(LPBYTE *)pbAttr = attr_out; // Pass pointer to user
pbAttr = attr_out; // Use pointer ourselves
*pcbAttrLen = cbAttrLen;
}

if (fpbAttrIsNULL)
{
// Do nothing - user wants length
}
else if (*pcbAttrLen < cbAttrLen)
{
ReturnCode = SCARD_E_INSUFFICIENT_BUFFER;
}
else if ((bytes - offset) < cbAttrLen)
{
LLOGLN(0, ("SCardGetAttrib: error, missing buffer"));
ReturnCode = SCARD_F_INTERNAL_ERROR;
}
else
{
memcpy(pbAttr, msg + offset, cbAttrLen);
}
*pcbAttrLen = cbAttrLen;
}

return ReturnCode;
}

/*****************************************************************************/
Expand Down
26 changes: 21 additions & 5 deletions sesman/chansrv/pcsc/xrdp_pcsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ enum pcsc_message_code
SCARD_STATUS = 0x0B,
SCARD_GET_STATUS_CHANGE = 0x0C,
SCARD_CANCEL = 0x0D,
SCARD_CANCEL_TRANSACTION = 0x0E,
SCARD_GET_ATTRIB = 0x0F,
SCARD_SET_ATTRIB = 0x10,
SCARD_IS_VALID_CONTEXT = 0x11
SCARD_GET_ATTRIB = 0x0E,
SCARD_SET_ATTRIB = 0x0F,
SCARD_IS_VALID_CONTEXT = 0x10
};

/*
Expand Down Expand Up @@ -282,7 +281,6 @@ enum pcsc_message_code
// | xx+12 ATR
// + xx+48 <reader-end>
//
//
// *****************************************************************************
// C A N C E L
// *****************************************************************************
Expand All @@ -296,5 +294,23 @@ enum pcsc_message_code
// 0 Header, code SCARD_CANCEL
// 8 ReturnCode
//
// *****************************************************************************
// G E T A T T R I B
// *****************************************************************************
// Request (See [MS-RDPESC] 2.2.2.21) :-
// Offset Value
// 0 Header, code SCARD_GET_ATTRIB
// 8 hCard
// 12 dwAttrId
// 16 fpbAttrIsNULL
// 20 cbAttrLen
//
// Response (See [MS-RDPESC] 2.2.3.12) :-
// Offset Value
// 0 Header, code SCARD_GET_ATTRIB
// 8 ReturnCode
// 12 cbAttrLen
// 16 pbAttr (if fpbAttrIsNULL is not set)
//

#endif // XRDP_PCSC_H
Loading

0 comments on commit 83a9be2

Please sign in to comment.