problem with rdesktop PC/SC handle conversion
Ludovic Rousseau <[email protected]>
| Newsgroups | gmane.network.rdesktop.devel |
|---|---|
| Message-ID | <[email protected]> |
Hello,
rdesktop scard.c file contains conversions functions
scHandleToMyPCSC() and scHandleToServer() with the comments included
bellow.
The comment is wrong with "recent" versions of pcsc-lite. The handle
returned by pcsc-lite is the same on 32 and 64-bits systems. The
functions are bogus since they corrupt the handles returned by
pcsc-lite.
The correction is to completely remove the two functions and use the
handles directly (without any conversion function).
The handle returned by SCardEstablishContext() is generated using:
*phContext = (PCSCLITE_SVC_IDENTITY + SYS_RandomInt(1, 65535));
with
#define PCSCLITE_SVC_IDENTITY 0x01030000 /**< Service ID */
This is a 32 bits value.
The handle returned by SCardConnect() is generated using:
randHandle = SYS_RandomInt(10, 65000);
return rContext->dwIdentity + randHandle;
and
dwIdentity = (dwContext + 1) << IDENTITY_SHIFT;
#define IDENTITY_SHIFT 16
dwContext is smaller than PCSCLITE_MAX_READERS_CONTEXTS (16 by default)
This is also a 32-bits value.
I can provide a patch to remove the 2 functions if needed. But that
should be very simple. Just tell me.
Regards,
from rdesktop/scard.c:
/* These two functions depend heavily on the actual implementation of the smart
* card handle in PC/SC Lite 1.3.1. Here are the salient bits:
*
* From winscard.c:331, in SCardConnect:
* *phCard = RFCreateReaderHandle(rContext);
*
* RFCreateReaderHandle (readerfactory.c:1161) creates a random short (16-bit
* integer) and makes sure it's unique. Then it adds it to
* rContext->dwIdentity.
*
* From readerfactory.c:173, in RFAddReader:
* (sReadersContexts[dwContext])->dwIdentity =
* (dwContext + 1) << (sizeof(DWORD) / 2) * 8;
*
* dwContext must be less than PCSCLITE_MAX_READERS_CONTEXTS, which is defined
* to be 16 in the 1.3.1 release.
*
* The use of "(sizeof(DWORD) / 2) * 8" is what makes conversion necessary in
* order to use 64-bit card handles when talking to PC/SC Lite, and 32-bit card
* handles when talking with the server, without losing any data: a card handle
* made by a 32-bit PC/SC Lite looks like 0x00014d32, where the 4d32 is the
* random 16 bits, 01 is the reader context index + 1, and it's left-shifted by
* 16 bits (sizeof(DWORD) == 4, divided by 2 is 2, times 8 is 16.) But a 64-bit
* PC/SC Lite makes a card handle that looks like 0x0000000100004d32. The
* reader context index+1 is left-shifted 32 bits because sizeof(DWORD) is 8,
* not 4. This means the handle won't fit in 32 bits. (The multiplication by 8
* is because sizeofs are in bytes, but saying how many places to left-shift is
* speaking in bits.)
* * So then. Maximum value of dwContext+1 is 17; we'll say this fits in a byte
* to be loose and have plenty of room. This is then left-shifted by
* sizeof(DWORD) / 2 * 8 - which in this file is sizeof(MYPCSC_DWORD) / 2 * 8.
*
* At any rate, if we take the handle as passed from PC/SC Lite, right-shift by
* sizeof(MYPCSC_DWORD) / 2, left-shift by sizeof(SERVER_DWORD) / 2, and add
* the lower two bytes of the value (the random number), we can fit all the
* information into 32 bits without losing any. Of course, any time we want to
* hand that back to PC/SC Lite, we'll have to expand it again. (And if
* sizeof(MYPCSC_DWORD) == sizeof(SERVER_DWORD), we're essentially doing
* nothing, which will not break anything.)
*
*
* - [email protected], 2 Aug 2006
*/
static MYPCSC_SCARDHANDLE
scHandleToMyPCSC(SERVER_SCARDHANDLE server)
{
return (((MYPCSC_SCARDHANDLE) server >> (sizeof(SERVER_DWORD) * 8 / 2) & 0xf
fff)
<< (sizeof(MYPCSC_DWORD) * 8 / 2)) + (server & 0xffff);
}
static SERVER_SCARDHANDLE
scHandleToServer(MYPCSC_SCARDHANDLE mypcsc)
{
return ((mypcsc >> (sizeof(MYPCSC_DWORD) * 8 / 2) & 0xffff)
<< (sizeof(SERVER_DWORD) * 8 / 2)) + (mypcsc & 0xffff);
}
--
Dr. Ludovic Rousseau
------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf