/*
* Sample program to use PC/SC API.
*
* MUSCLE SmartCard Development ( http://www.linuxnet.com )
*
* Copyright (C) 2003-2011
* Ludovic Rousseau <ludovic.rousseau@free.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>.
*
* $Id: pcsc_demo.c 6003 2011-10-05 13:22:23Z rousseau $
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>

#include <PCSC/wintypes.h>
#include <PCSC/winscard.h>
/** error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx
*/
#define SCARD_S_SUCCESS ((LONG)0x00000000) /**< No error was encountered. */
#define SCARD_F_INTERNAL_ERROR ((LONG)0x80100001) /**< An internal consistency check failed. */
#define SCARD_E_CANCELLED ((LONG)0x80100002) /**< The action was cancelled by an SCardCancel request. */
#define SCARD_E_INVALID_HANDLE ((LONG)0x80100003) /**< The supplied handle was invalid. */
#define SCARD_E_INVALID_PARAMETER ((LONG)0x80100004) /**< One or more of the supplied parameters could not be properly interpreted. */
#define SCARD_E_INVALID_TARGET ((LONG)0x80100005) /**< Registry startup information is missing or invalid. */
#define SCARD_E_NO_MEMORY ((LONG)0x80100006) /**< Not enough memory available to complete this command. */
#define SCARD_F_WAITED_TOO_LONG ((LONG)0x80100007) /**< An internal consistency timer has expired. */
#define SCARD_E_INSUFFICIENT_BUFFER ((LONG)0x80100008) /**< The data buffer to receive returned data is too small for the returned data. */
#define SCARD_E_UNKNOWN_READER ((LONG)0x80100009) /**< The specified reader name is not recognized. */
#define SCARD_E_TIMEOUT ((LONG)0x8010000A) /**< The user-specified timeout value has expired. */
#define SCARD_E_SHARING_VIOLATION ((LONG)0x8010000B) /**< The smart card cannot be accessed because of other connections outstanding. */
#define SCARD_E_NO_SMARTCARD ((LONG)0x8010000C) /**< The operation requires a Smart Card, but no Smart Card is currently in the device. */
#define SCARD_E_UNKNOWN_CARD ((LONG)0x8010000D) /**< The specified smart card name is not recognized. */
#define SCARD_E_CANT_DISPOSE ((LONG)0x8010000E) /**< The system could not dispose of the media in the requested manner. */
#define SCARD_E_PROTO_MISMATCH ((LONG)0x8010000F) /**< The requested protocols are incompatible with the protocol currently in use with the smart card. */
#define SCARD_E_NOT_READY ((LONG)0x80100010) /**< The reader or smart card is not ready to accept commands. */
#define SCARD_E_INVALID_VALUE ((LONG)0x80100011) /**< One or more of the supplied parameters values could not be properly interpreted. */
#define SCARD_E_SYSTEM_CANCELLED ((LONG)0x80100012) /**< The action was cancelled by the system, presumably to log off or shut down. */
#define SCARD_F_COMM_ERROR ((LONG)0x80100013) /**< An internal communications error has been detected. */
#define SCARD_F_UNKNOWN_ERROR ((LONG)0x80100014) /**< An internal error has been detected, but the source is unknown. */
#define SCARD_E_INVALID_ATR ((LONG)0x80100015) /**< An ATR obtained from the registry is not a valid ATR string. */
#define SCARD_E_NOT_TRANSACTED ((LONG)0x80100016) /**< An attempt was made to end a non-existent transaction. */
#define SCARD_E_READER_UNAVAILABLE ((LONG)0x80100017) /**< The specified reader is not currently available for use. */
#define SCARD_P_SHUTDOWN ((LONG)0x80100018) /**< The operation has been aborted to allow the server application to exit. */
#define SCARD_E_PCI_TOO_SMALL ((LONG)0x80100019) /**< The PCI Receive buffer was too small. */
#define SCARD_E_READER_UNSUPPORTED ((LONG)0x8010001A) /**< The reader driver does not meet minimal requirements for support. */
#define SCARD_E_DUPLICATE_READER ((LONG)0x8010001B) /**< The reader driver did not produce a unique reader name. */
#define SCARD_E_CARD_UNSUPPORTED ((LONG)0x8010001C) /**< The smart card does not meet minimal requirements for support. */
#define SCARD_E_NO_SERVICE ((LONG)0x8010001D) /**< The Smart card resource manager is not running. */
#define SCARD_E_SERVICE_STOPPED ((LONG)0x8010001E) /**< The Smart card resource manager has shut down. */
#define SCARD_E_UNEXPECTED ((LONG)0x8010001F) /**< An unexpected card error has occurred. */
#define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x8010001F) /**< This smart card does not support the requested feature. */
#define SCARD_E_ICC_INSTALLATION ((LONG)0x80100020) /**< No primary provider can be found for the smart card. */
#define SCARD_E_ICC_CREATEORDER ((LONG)0x80100021) /**< The requested order of object creation is not supported. */
/* #define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x80100022) / **< This smart card does not support the requested feature. */
#define SCARD_E_DIR_NOT_FOUND ((LONG)0x80100023) /**< The identified directory does not exist in the smart card. */
#define SCARD_E_FILE_NOT_FOUND ((LONG)0x80100024) /**< The identified file does not exist in the smart card. */
#define SCARD_E_NO_DIR ((LONG)0x80100025) /**< The supplied path does not represent a smart card directory. */
#define SCARD_E_NO_FILE ((LONG)0x80100026) /**< The supplied path does not represent a smart card file. */
#define SCARD_E_NO_ACCESS ((LONG)0x80100027) /**< Access is denied to this file. */
#define SCARD_E_WRITE_TOO_MANY ((LONG)0x80100028) /**< The smart card does not have enough memory to store the information. */
#define SCARD_E_BAD_SEEK ((LONG)0x80100029) /**< There was an error trying to set the smart card file object pointer. */
#define SCARD_E_INVALID_CHV ((LONG)0x8010002A) /**< The supplied PIN is incorrect. */
#define SCARD_E_UNKNOWN_RES_MNG ((LONG)0x8010002B) /**< An unrecognized error code was returned from a layered component. */
#define SCARD_E_NO_SUCH_CERTIFICATE ((LONG)0x8010002C) /**< The requested certificate does not exist. */
#define SCARD_E_CERTIFICATE_UNAVAILABLE ((LONG)0x8010002D) /**< The requested certificate could not be obtained. */
#define SCARD_E_NO_READERS_AVAILABLE ((LONG)0x8010002E) /**< Cannot find a smart card reader. */
#define SCARD_E_COMM_DATA_LOST ((LONG)0x8010002F) /**< A communications error with the smart card has been detected. Retry the operation. */
#define SCARD_E_NO_KEY_CONTAINER ((LONG)0x80100030) /**< The requested key container does not exist on the smart card. */
#define SCARD_E_SERVER_TOO_BUSY ((LONG)0x80100031) /**< The Smart Card Resource Manager is too busy to complete this operation. */

#define SCARD_W_UNSUPPORTED_CARD ((LONG)0x80100065) /**< The reader cannot communicate with the card, due to ATR string configuration conflicts. */
#define SCARD_W_UNRESPONSIVE_CARD ((LONG)0x80100066) /**< The smart card is not responding to a reset. */
#define SCARD_W_UNPOWERED_CARD ((LONG)0x80100067) /**< Power has been removed from the smart card, so that further communication is not possible. */
#define SCARD_W_RESET_CARD ((LONG)0x80100068) /**< The smart card has been reset, so any shared state information is invalid. */
#define SCARD_W_REMOVED_CARD ((LONG)0x80100069) /**< The smart card has been removed, so further communication is not possible. */

#define SCARD_W_SECURITY_VIOLATION ((LONG)0x8010006A) /**< Access was denied because of a security violation. */
#define SCARD_W_WRONG_CHV ((LONG)0x8010006B) /**< The card cannot be accessed because the wrong PIN was presented. */
#define SCARD_W_CHV_BLOCKED ((LONG)0x8010006C) /**< The card cannot be accessed because the maximum number of PIN entry attempts has been reached. */
#define SCARD_W_EOF ((LONG)0x8010006D) /**< The end of the smart card file has been reached. */
#define SCARD_W_CANCELLED_BY_USER ((LONG)0x8010006E) /**< The user pressed "Cancel" on a Smart Card Selection Dialog. */
#define SCARD_W_CARD_NOT_AUTHENTICATED ((LONG)0x8010006F) /**< No PIN was presented to the smart card. */

#define SCARD_AUTOALLOCATE (DWORD)(-1) /**< see SCardFreeMemory() */
#define SCARD_SCOPE_USER 0x0000 /**< Scope in user space */
#define SCARD_SCOPE_TERMINAL 0x0001 /**< Scope in terminal */
#define SCARD_SCOPE_SYSTEM 0x0002 /**< Scope in system */

#define SCARD_PROTOCOL_UNDEFINED 0x0000 /**< protocol not set */
#define SCARD_PROTOCOL_UNSET SCARD_PROTOCOL_UNDEFINED /* backward compat */
#define SCARD_PROTOCOL_T0 0x0001 /**< T=0 active protocol. */
#define SCARD_PROTOCOL_T1 0x0002 /**< T=1 active protocol. */
#define SCARD_PROTOCOL_RAW 0x0004 /**< Raw active protocol. */
#define SCARD_PROTOCOL_T15 0x0008 /**< T=15 protocol. */

#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1) /**< IFD determines prot. */

#define SCARD_SHARE_EXCLUSIVE 0x0001 /**< Exclusive mode only */
#define SCARD_SHARE_SHARED 0x0002 /**< Shared mode only */
#define SCARD_SHARE_DIRECT 0x0003 /**< Raw mode only */

#define SCARD_LEAVE_CARD 0x0000 /**< Do nothing on close */
#define SCARD_RESET_CARD 0x0001 /**< Reset on close */
#define SCARD_UNPOWER_CARD 0x0002 /**< Power down on close */
#define SCARD_EJECT_CARD 0x0003 /**< Eject on close */

#define SCARD_UNKNOWN 0x0001 /**< Unknown state */
#define SCARD_ABSENT 0x0002 /**< Card is absent */
#define SCARD_PRESENT 0x0004 /**< Card is present */
#define SCARD_SWALLOWED 0x0008 /**< Card not powered */
#define SCARD_POWERED 0x0010 /**< Card is powered */
#define SCARD_NEGOTIABLE 0x0020 /**< Ready for PTS */
#define SCARD_SPECIFIC 0x0040 /**< PTS has been set */

#define SCARD_STATE_UNAWARE 0x0000 /**< App wants status */
#define SCARD_STATE_IGNORE 0x0001 /**< Ignore this reader */
#define SCARD_STATE_CHANGED 0x0002 /**< State has changed */
#define SCARD_STATE_UNKNOWN 0x0004 /**< Reader unknown */
#define SCARD_STATE_UNAVAILABLE 0x0008 /**< Status unavailable */
#define SCARD_STATE_EMPTY 0x0010 /**< Card removed */
#define SCARD_STATE_PRESENT 0x0020 /**< Card inserted */
#define SCARD_STATE_ATRMATCH 0x0040 /**< ATR matches card */
#define SCARD_STATE_EXCLUSIVE 0x0080 /**< Exclusive Mode */
#define SCARD_STATE_INUSE 0x0100 /**< Shared Mode */
#define SCARD_STATE_MUTE 0x0200 /**< Unresponsive card */
#define SCARD_STATE_UNPOWERED 0x0400 /**< Unpowered card */

#ifndef INFINITE
#define INFINITE 0xFFFFFFFF /**< Infinite timeout */
#endif
#define MAX_ATR_SIZE 33 /**< Maximum ATR size */
#define MAX_READERNAME 100

extern const SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci, g_rgSCardRawPci;
#define SCARD_PCI_T0 (&g_rgSCardT0Pci) /**< protocol control information (PCI) for T=0 */
#define SCARD_PCI_T1 (&g_rgSCardT1Pci) /**< protocol control information (PCI) for T=1 */
#define SCARD_PCI_RAW (&g_rgSCardRawPci) /**< protocol control information (PCI) for RAW protocol */


#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif

/* PCSC error message pretty print */
#define PCSC_ERROR(rv, text) \
if (rv != SCARD_S_SUCCESS) \
{ \
printf(text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
goto end; \
} \
else \
{ \
printf(text ": OK\n\n"); \
}

int main(int argc, char *argv[])
{
LONG rv;
SCARDCONTEXT hContext;
DWORD dwReaders;
LPSTR mszReaders = NULL;
char *ptr, **readers = NULL;
int nbReaders;
SCARDHANDLE hCard;
DWORD dwActiveProtocol, dwReaderLen, dwState, dwProt, dwAtrLen;
BYTE pbAtr[MAX_ATR_SIZE] = "";
char pbReader[MAX_READERNAME] = "";
int reader_nb;
unsigned int i;
const SCARD_IO_REQUEST *pioSendPci;
SCARD_IO_REQUEST pioRecvPci;
BYTE pbRecvBuffer[10];
BYTE pbSendBuffer[] = { 0x00, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00 };
DWORD dwSendLength, dwRecvLength;

printf("PC/SC sample code\n");
printf("V 1.4 2003-2009, Ludovic Rousseau <ludovic.rousseau@free.fr>\n");

printf("\nTHIS PROGRAM IS NOT DESIGNED AS A TESTING TOOL FOR END USERS!\n");
printf("Do NOT use it unless you really know what you do.\n\n");

rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
if (rv != SCARD_S_SUCCESS)
{
printf("SCardEstablishContext: Cannot Connect to Resource Manager %lX\n", rv);
return EXIT_FAILURE;
}

/* Retrieve the available readers list. */
dwReaders = SCARD_AUTOALLOCATE;
rv = SCardListReaders(hContext, NULL, (LPSTR)&mszReaders, &dwReaders);
PCSC_ERROR(rv, "SCardListReaders")

/* Extract readers from the null separated string and get the total
* number of readers */
nbReaders = 0;
ptr = mszReaders;
while (*ptr != '\0')
{
ptr += strlen(ptr)+1;
nbReaders++;
}

if (nbReaders == 0)
{
printf("No reader found\n");
goto end;
}

/* allocate the readers table */
readers = calloc(nbReaders, sizeof(char *));
if (NULL == readers)
{
printf("Not enough memory for readers[]\n");
goto end;
}

/* fill the readers table */
nbReaders = 0;
ptr = mszReaders;
while (*ptr != '\0')
{
printf("%d: %s\n", nbReaders, ptr);
readers[nbReaders] = ptr;
ptr += strlen(ptr)+1;
nbReaders++;
}

if (argc > 1)
{
reader_nb = atoi(argv[1]);
if (reader_nb < 0 || reader_nb >= nbReaders)
{
printf("Wrong reader index: %d\n", reader_nb);
goto end;
}
}
else
reader_nb = 0;

/* connect to a card */
dwActiveProtocol = -1;
rv = SCardConnect(hContext, readers[reader_nb], SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
printf(" Protocol: %ld\n", dwActiveProtocol);
PCSC_ERROR(rv, "SCardConnect")

/* get card status */
dwAtrLen = sizeof(pbAtr);
dwReaderLen = sizeof(pbReader);
rv = SCardStatus(hCard, /*NULL*/ pbReader, &dwReaderLen, &dwState, &dwProt,
pbAtr, &dwAtrLen);
printf(" Reader: %s (length %ld bytes)\n", pbReader, dwReaderLen);
printf(" State: 0x%lX\n", dwState);
printf(" Prot: %ld\n", dwProt);
printf(" ATR (length %ld bytes):", dwAtrLen);
for (i=0; i<dwAtrLen; i++)
printf(" %02X", pbAtr[i]);
printf("\n");
PCSC_ERROR(rv, "SCardStatus")

switch(dwActiveProtocol)
{
case SCARD_PROTOCOL_T0:
pioSendPci = SCARD_PCI_T0;
break;
case SCARD_PROTOCOL_T1:
pioSendPci = SCARD_PCI_T1;
break;
default:
printf("Unknown protocol\n");
goto end;
}

/* exchange APDU */
dwSendLength = sizeof(pbSendBuffer);
dwRecvLength = sizeof(pbRecvBuffer);
printf("Sending: ");
for (i=0; i<dwSendLength; i++)
printf("%02X ", pbSendBuffer[i]);
printf("\n");
rv = SCardTransmit(hCard, pioSendPci, pbSendBuffer, dwSendLength,
&pioRecvPci, pbRecvBuffer, &dwRecvLength);
printf("Received: ");
for (i=0; i<dwRecvLength; i++)
printf("%02X ", pbRecvBuffer[i]);
printf("\n");
PCSC_ERROR(rv, "SCardTransmit")

/* card disconnect */
rv = SCardDisconnect(hCard, SCARD_LEAVE_CARD);
PCSC_ERROR(rv, "SCardDisconnect")

/* connect to a card */
dwActiveProtocol = -1;
rv = SCardConnect(hContext, readers[reader_nb], SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
printf(" Protocol: %ld\n", dwActiveProtocol);
PCSC_ERROR(rv, "SCardConnect")

/* exchange APDU */
dwSendLength = sizeof(pbSendBuffer);
dwRecvLength = sizeof(pbRecvBuffer);
printf("Sending: ");
for (i=0; i<dwSendLength; i++)
printf("%02X ", pbSendBuffer[i]);
printf("\n");
rv = SCardTransmit(hCard, pioSendPci, pbSendBuffer, dwSendLength,
&pioRecvPci, pbRecvBuffer, &dwRecvLength);
printf("Received: ");
for (i=0; i<dwRecvLength; i++)
printf("%02X ", pbRecvBuffer[i]);
printf("\n");
PCSC_ERROR(rv, "SCardTransmit")

/* card reconnect */
rv = SCardReconnect(hCard, SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, SCARD_LEAVE_CARD,
&dwActiveProtocol);
PCSC_ERROR(rv, "SCardReconnect")

/* get card status */
dwAtrLen = sizeof(pbAtr);
dwReaderLen = sizeof(pbReader);
rv = SCardStatus(hCard, /*NULL*/ pbReader, &dwReaderLen, &dwState, &dwProt,
pbAtr, &dwAtrLen);
printf(" Reader: %s (length %ld bytes)\n", pbReader, dwReaderLen);
printf(" State: 0x%lX\n", dwState);
printf(" Prot: %ld\n", dwProt);
printf(" ATR (length %ld bytes):", dwAtrLen);
for (i=0; i<dwAtrLen; i++)
printf(" %02X", pbAtr[i]);
printf("\n");
PCSC_ERROR(rv, "SCardStatus")

/* get card status change */
{
/* check only one reader */
SCARD_READERSTATE rgReaderStates[1];

rgReaderStates[0].szReader = pbReader;
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;

rv = SCardGetStatusChange(hContext, 0, rgReaderStates, 1);
printf(" state: 0x%04lX\n", rgReaderStates[0].dwEventState);
PCSC_ERROR(rv, "SCardGetStatusChange")
}

/* begin transaction */
rv = SCardBeginTransaction(hCard);
PCSC_ERROR(rv, "SCardBeginTransaction")

/* exchange APDU */
dwSendLength = sizeof(pbSendBuffer);
dwRecvLength = sizeof(pbRecvBuffer);
printf("Sending: ");
for (i=0; i<dwSendLength; i++)
printf("%02X ", pbSendBuffer[i]);
printf("\n");
rv = SCardTransmit(hCard, pioSendPci, pbSendBuffer, dwSendLength,
&pioRecvPci, pbRecvBuffer, &dwRecvLength);
printf("Received: ");
for (i=0; i<dwRecvLength; i++)
printf("%02X ", pbRecvBuffer[i]);
printf("\n");
PCSC_ERROR(rv, "SCardTransmit")

/* end transaction */
rv = SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
PCSC_ERROR(rv, "SCardEndTransaction")

/* card disconnect */
rv = SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
PCSC_ERROR(rv, "SCardDisconnect")

end:
/* free allocated memory */
if (mszReaders)
SCardFreeMemory(hContext, mszReaders);

/* We try to leave things as clean as possible */
rv = SCardReleaseContext(hContext);
if (rv != SCARD_S_SUCCESS)
printf("SCardReleaseContext: %s (0x%lX)\n", pcsc_stringify_error(rv),
rv);

if (readers)
free(readers);

return EXIT_SUCCESS;
} /* main */

结果输出:

PC/SC sample code
V 1.4 2003-2009, Ludovic Rousseau <ludovic.rousseau@free.fr>

THIS PROGRAM IS NOT DESIGNED AS A TESTING TOOL FOR END USERS!
Do NOT use it unless you really know what you do.

SCardListReaders: OK

0: ACS ACR1251 1S CL Reader [ACR1251 Dual Reader SAM] 00 00
1: ACS ACR1251 1S CL Reader [ACR1251 Dual Reader SAM] 00 01
Protocol: 2
SCardConnect: OK

Reader: ACS ACR1251 1S CL Reader [ACR1251 Dual Reader SAM] 00 00 (length 57 bytes)
State: 0xA0034
Prot: 2
ATR (length 20 bytes): 3B 8F 80 01 80 4F 0C A0 00 00 03 06 03 00 01 00 00 00 00 6A
SCardStatus: OK

Sending: 00 A4 00 00 02 3F 00
Received: 6B 80
SCardTransmit: OK

SCardDisconnect: OK

Protocol: 2
SCardConnect: OK

Sending: 00 A4 00 00 02 3F 00
Received: 6B 80
SCardTransmit: OK

SCardReconnect: OK

Reader: ACS ACR1251 1S CL Reader [ACR1251 Dual Reader SAM] 00 00 (length 57 bytes)
State: 0xA0034
Prot: 2
ATR (length 20 bytes): 3B 8F 80 01 80 4F 0C A0 00 00 03 06 03 00 01 00 00 00 00 6A
SCardStatus: OK

state: 0xA0122
SCardGetStatusChange: OK

SCardBeginTransaction: OK

Sending: 00 A4 00 00 02 3F 00
Received: 6B 80
SCardTransmit: OK

SCardEndTransaction: OK

SCardDisconnect: OK