I am working on SCM Contactless RFID Card Reader using the Microsoft SmartCard APIs.
The usual process to ineract with the smart card, what I have understood, is:
1. Establish the Context using SCardEstablishContext()
2. List all the readers attached to the system using SCardListReaders()
3. Select a reader and wait for a card to be inserted into it.
4. Once a card is detected the function SCardGetStatusChange() returns.
5. Connect to the card through the selected reader using SCardConnect()
6. Talk to the card using SCardTransmit()
All the steps till 5 is working fine which means that I got the correct card handle. The card can be verified further by issuing SCardStatus() function which returns the negotiated protocol and the card's ATR, which is also correct. But when I am trying to send any APDU command using SCardTransmit() it returns an error (error code 14, which I guess is SCARD_E_CANT_DISPOSE).
I have no clue what is going wrong or what is missing. I tried different APDUs for e.g. the very basic APDU to read the card UID by issuing {0xFF, 0xCA, 0x00, 0x00, 0x00}. I verified this APDU using the SCM TestResMan debug tool and it returns me the correct UID of the card. I am using a MiFARE 1k card and SCM Contactless SCL011 Reader.
Please find the SCardTransmit() code snippet below (All the previous functions returned success).
SCARD_IO_REQUEST sioreq;
sioreq.dwProtocol = 2; // SCARD_PROTOCOL_T1
sioreq.cbPciLength = 8;
SCARD_IO_REQUEST rioreq;
rioreq.dwProtocol = 2; // SCARD_PROTOCOL_T1
rioreq.cbPciLength = 8;
byte sendbuffer[256], receivebuffer[256];
ULONG sendbufferLen, receivebufferLen, sizeofUID;
sendbuffer[0] = 0xFF; // CLA - Instruction Class
sendbuffer[1] = 0xCA; // Instruction code
sendbuffer[2] = 0x00; // P1 - 1st parameter
sendbuffer[3] = 0x00; // P2 - 2nd parameter
sendbuffer[4] = 0x00;
sendbufferLen = 0x05;
retval = objScWrapper->SCardTransmit(hCardHandle, &sioreq, sendbuffer,
sendbufferLen, NULL, receivebuffer,
&receivebufferLen);
Please let me know if anyone has any clue or pointers...
TIA !!
Your receive buffer size is 256 bytes, but you are requesting 256 bytes (Le = 00h), and the receive buffer also expects the status words (consisting of two bytes, SW1 and SW2).
So I think your 14 error code is the generic OUT_OF_MEMORY for the receive buffer.
Your command buffer is also too small to fit 5 bytes header + max 255 bytes command data. Of course, both are certainly too small to handle extended length ADPU's.
Windows is currently pretty picky regarding smart card reader buffer sizes for PCSC compatible readers. Use at least 5 + 255 = 260 bytes for the command buffer and 256 + 2 bytes for the receive buffer. Reuse the buffers or safely dispose of them or you will have a memory leak.