1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/*
* Sample program to use PC/SC API.
*
* MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
*
* Copyright (C) 2003-2021
* Ludovic Rousseau <ludovic.rousseau@free.fr>
*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <winscard.h>
#include <reader.h>
/* 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 = 0;
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] = { 0 };
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")
/* get Attribute */
dwAtrLen = sizeof(pbAtr);
rv = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, pbAtr, &dwAtrLen);
printf("Received: ");
for (i=0; i<dwAtrLen; i++)
printf("%02X ", pbAtr[i]);
printf("\n");
PCSC_ERROR(rv, "SCardGetAttrib")
/* control */
rv = SCardControl(hCard, CM_IOCTL_GET_FEATURE_REQUEST, NULL, 0,
pbRecvBuffer, sizeof pbRecvBuffer, &dwRecvLength);
printf("Received: ");
for (i=0; i<dwRecvLength; i++)
printf("%02X ", pbRecvBuffer[i]);
printf("\n");
PCSC_ERROR(rv, "SCardControl")
/* 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 */
|