Skip to content

Commit

Permalink
Add SCardReconnect and SCardControl to stub only
Browse files Browse the repository at this point in the history
  • Loading branch information
matt335672 committed Mar 7, 2024
1 parent 050834a commit fc81f61
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 27 deletions.
121 changes: 94 additions & 27 deletions sesman/chansrv/pcsc/xrdp_pcsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,55 @@ SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode,
DWORD dwPreferredProtocols, DWORD dwInitialization,
LPDWORD pdwActiveProtocol)
{
LLOGLN(0, ("SCardReconnect:"));
char msg[256];
unsigned int code;
unsigned int bytes;
int status;
int offset;

LLOGLN(10, ("SCardReconnect:"));
LLOGLN(10, ("SCardReconnect: hCard 0x%8.8x dwShareMode %d "
"dwPreferredProtocols %d dwInitialization %d",
(int)hCard, (int)dwShareMode,
(int)dwPreferredProtocols, (int)dwInitialization));
if (g_sck == -1)
{
LLOGLN(0, ("SCardReconnect: error, not connected"));
return SCARD_F_INTERNAL_ERROR;
}
return SCARD_S_SUCCESS;
offset = 0;
SET_UINT32(msg, offset, hCard);
offset += 4;
SET_UINT32(msg, offset, dwShareMode);
offset += 4;
SET_UINT32(msg, offset, dwPreferredProtocols);
offset += 4;
SET_UINT32(msg, offset, dwInitialization);
offset += 4;

if (send_message(SCARD_RECONNECT, msg, offset) != 0)
{
LLOGLN(0, ("SCardReconnect: error, send_message"));
return SCARD_F_INTERNAL_ERROR;
}
bytes = sizeof(msg);
code = SCARD_RECONNECT;
if (get_message(&code, msg, &bytes) != 0)
{
LLOGLN(0, ("SCardReconnect: error, get_message"));
return SCARD_F_INTERNAL_ERROR;
}
if (code != SCARD_RECONNECT || (bytes != 8))
{
LLOGLN(0, ("SCardReconnect: error, bad code"));
return SCARD_F_INTERNAL_ERROR;
}
status = GET_UINT32(msg, 0);
*pdwActiveProtocol = GET_UINT32(msg, 8);
LLOGLN(10, ("SCardReconnect: got status 0x%8.8x "
"dwActiveProtocol %d",
status, (int)*pdwActiveProtocol));
return status;
}

/*****************************************************************************/
Expand Down Expand Up @@ -577,11 +619,6 @@ SCardStatus(SCARDHANDLE hCard, LPSTR szReaderName, LPDWORD pcchReaderLen,
char *reader_out = NULL;

LLOGLN(10, ("SCardStatus:"));
if (hCard == 0)
{
LLOGLN(10, ("SCardStatus: error, bad hCard"));
return SCARD_F_INTERNAL_ERROR;
}
if (g_sck == -1)
{
LLOGLN(0, ("SCardStatus: error, not connected"));
Expand Down Expand Up @@ -861,40 +898,57 @@ SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer,
LLOGLN(0, ("SCardControl: error, not connected"));
return SCARD_F_INTERNAL_ERROR;
}
LLOGLN(10, (" hCard 0x%8.8x", (int)hCard));
LLOGLN(10, (" dwControlCode 0x%8.8x", (int)dwControlCode));
LLOGLN(10, (" cbSendLength %d", (int)cbSendLength));
LLOGLN(10, (" cbRecvLength %d", (int)cbRecvLength));

/* #define SCARD_CTL_CODE(code) (0x42000000 + (code))
control_code = (control_code & 0x3ffc) >> 2;
control_code = SCARD_CTL_CODE(control_code); */

/* PCSC to Windows control code conversion */
dwControlCode = dwControlCode - 0x42000000;
if (pbSendBuffer == NULL)
{
return SCARD_E_INVALID_PARAMETER;
}
/* PCSC to Windows control code conversion
*
* Cater for the different definitions of SCARD_CTL_CODE().
* PCSC-Lite : SCARD_CTL_CODE(x) -> 0x42000000 + x
* Windows : SCARD_CTL_CODE(x) -> CTL_CODE(FILE_DEVICE_SMARTCARD,
* x,
* METHOD_BUFFERED,
* FILE_ANY_ACCESS)
* -> ( (FILE_DEVICE_SMARTCODE << 16) |
* (FILE_ANY_ACCESS << 14) |
* (x << 2) |
* METHOD_BUFFERED )
* -> ( (0x31 << 16) | (0 << 14) |
* (x << 2) | 0)
*/

dwControlCode = dwControlCode - 0x42000000; /* Get the faw code */
dwControlCode = dwControlCode << 2;
dwControlCode = dwControlCode | (49 << 16);
dwControlCode = dwControlCode | (0x31 << 16);
LLOGLN(10, (" MS dwControlCode 0x%8.8d", (int)dwControlCode));

msg = (char *) malloc(8192);
/* Use the larger of the send and receive buffer sizes to
* allocate memory for the message */
bytes = 64 + LMAX(cbSendLength, cbRecvLength);
msg = (char *)malloc(bytes);
if (msg == NULL)
{
return SCARD_E_NO_MEMORY;
}

offset = 0;
SET_UINT32(msg, offset, hCard);
offset += 4;
SET_UINT32(msg, offset, dwControlCode);
offset += 4;
SET_UINT32(msg, offset, cbSendLength);
offset += 4;
memcpy(msg + offset, pbSendBuffer, cbSendLength);
offset += cbSendLength;
SET_UINT32(msg, offset, cbRecvLength);
offset += 4;
memcpy(msg + offset, pbSendBuffer, cbSendLength);
offset += cbSendLength;
if (send_message(SCARD_CONTROL, msg, offset) != 0)
{
LLOGLN(0, ("SCardControl: error, send_message"));
free(msg);
return SCARD_F_INTERNAL_ERROR;
}
bytes = 8192;
code = SCARD_CONTROL;
if (get_message(&code, msg, &bytes) != 0)
{
Expand All @@ -909,12 +963,25 @@ SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer,
return SCARD_F_INTERNAL_ERROR;
}
offset = 0;
status = GET_UINT32(msg, offset);
offset += 4;
*lpBytesReturned = GET_UINT32(msg, offset);
LLOGLN(10, (" cbRecvLength %d", (int)*lpBytesReturned));
offset += 4;
memcpy(pbRecvBuffer, msg + offset, *lpBytesReturned);
offset += *lpBytesReturned;
status = GET_UINT32(msg, offset);

if (status == SCARD_S_SUCCESS)
{
// Sanity-check the returned length
if (*lpBytesReturned > cbRecvLength ||
*lpBytesReturned > (bytes - offset))
{
status = SCARD_F_INTERNAL_ERROR;
}
else
{
memcpy(pbRecvBuffer, msg + offset, *lpBytesReturned);
}
}

free(msg);
return status;
}
Expand Down
35 changes: 35 additions & 0 deletions sesman/chansrv/pcsc/xrdp_pcsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,41 @@ enum pcsc_message_code
// Offset Value
// 0 Header, code SCARD_CANCEL
// 8 ReturnCode
//
// *****************************************************************************
// C O N T R O L
// *****************************************************************************
// Request (See [MS-RDPESC] 2.2.2.20) :-
// Offset Value
// 0 Header, code SCARD_CONTROL
// 8 hCard
// 12 dwControlCode (PCSC-Lite definition)
// 16 cbSendLength
// 20 cbRecvLength
// 24 *pbSendBuffer
//
// Response (See [MS-RDPESC] 2.2.3.6) :-
// Offset Value
// 0 Header, code SCARD_CONTROL
// 8 ReturnCode
// 12 cbOutBufferSize
// 16 *pvOutBuffer (only if ReturnCode == SCARD_S_SUCCESS)
//
// *****************************************************************************
// R E C O N N E C T
// *****************************************************************************
// Request (See [MS-RDPESC] 2.2.2.15)
// 0 Header, code SCARD_RECONNECT
// 8 hCard
// 12 dwShareMode
// 16 dwPreferredProtocols
// 20 dwInitialization
//
// Response (See [MS-RDPESC] 2.2.3.7) :-
// Offset Value
// 0 Header, code SCARD_RECONNECT
// 8 ReturnCode
// 12 dwActiveProtocol

//

Expand Down

0 comments on commit fc81f61

Please sign in to comment.