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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
|
/*****************************************************************
/
/ File : ifdhandler.c
/ Author : David Corcoran
/ Date : November 7, 1998
/ Purpose: This provides reader specific low-level calls.
/ Alot of the functionality listed in the specs is
/ not done. I've done a minimum to get started.
/ See http://www.linuxnet.com for more information.
/ License: See file LICENSE
/
******************************************************************/
#include "ctapi.h"
#include "ctbcs.h"
#include "ifdhandler.h"
#include "LinuxDefines.h"
#include "T1Hndlr.h"
#include <string.h>
#define PCSC_DEBUG 1
static DWORD dwCurrentProtocol = 0;
struct DEVICE_CAPABILITIES {
STR Vendor_Name; /* Tag 0x0100 */
STR IFD_Type; /* Tag 0x0101 */
DWORD IFD_Version; /* Tag 0x0102 */
STR IFD_Serial; /* Tag 0x0103 */
DWORD IFD_Channel_ID; /* Tag 0x0110 */
DWORD Asynch_Supported; /* Tag 0x0120 */
DWORD Default_Clock; /* Tag 0x0121 */
DWORD Max_Clock; /* Tag 0x0122 */
DWORD Default_Data_Rate; /* Tag 0x0123 */
DWORD Max_Data_Rate; /* Tag 0x0124 */
DWORD Max_IFSD; /* Tag 0x0125 */
DWORD Synch_Supported; /* Tag 0x0126 */
DWORD Power_Mgmt; /* Tag 0x0131 */
DWORD Card_Auth_Devices; /* Tag 0x0140 */
DWORD User_Auth_Device; /* Tag 0x0142 */
DWORD Mechanics_Supported; /* Tag 0x0150 */
DWORD Vendor_Features; /* Tag 0x0180 - 0x01F0 User Defined. */
} Device;
struct ICC_STATE {
BYTE ICC_Presence; /* Tag 0x0300 */
BYTE ICC_Interface_Status; /* Tag 0x0301 */
BYTE ATR[32]; /* Tag 0x0303 */
BYTE ICC_Type; /* Tag 0x0304 */
} ICC;
struct PROTOCOL_OPTIONS {
DWORD Protocol_Type; /* Tag 0x0201 */
DWORD Current_Clock; /* Tag 0x0202 */
DWORD Current_F; /* Tag 0x0203 */
DWORD Current_D; /* Tag 0x0204 */
DWORD Current_N; /* Tag 0x0205 */
DWORD Current_W; /* Tag 0x0206 */
DWORD Current_IFSC; /* Tag 0x0207 */
DWORD Current_IFSD; /* Tag 0x0208 */
DWORD Current_BWT; /* Tag 0x0209 */
DWORD Current_CWT; /* Tag 0x020A */
DWORD Current_EBC; /* Tag 0x020B */
} Protocol;
RESPONSECODE IO_Create_Channel ( DWORD ChannelId ) {
RESPONSECODE lRetVal = IFD_SUCCESS;
BYTE dad, sad;
BYTE Response[MAX_RESPONSE_SIZE];
unsigned short Unibble; /* Upper 16bits of DWORD. (DDDD) */
unsigned short Lnibble; /* Lower 16bits of DWORD. (CCCC) */
BYTE pcPowerUp[] = {0x20, 0x12, 0x00, 0x00, 0x00};
unsigned int iTerm = 1;
unsigned int lr;
int iPort;
int lc;
int i;
sad = 2;
dad = 1;
lc = 5;
lr = 2;
Unibble = ChannelId >> 16; /* Shift right 16 bits. */
Lnibble = ChannelId - ( Unibble << 16 ); /* Shift left and subtract. */
switch ( Unibble ) {
case 0x01: /* Serial Port. */
switch ( Lnibble ) {
case 0x3F8:
iPort = PORT_COM1;
break;
case 0x2F8:
iPort = PORT_COM2;
break;
case 0x3E8:
iPort = PORT_COM3;
break;
case 0x2E8:
iPort = PORT_COM4;
break;
default:
lRetVal = IFD_NOT_SUPPORTED;
break;
}
break;
case 0x02: /* Parallel Port. */
lRetVal = IFD_NOT_SUPPORTED;
break;
case 0x04: /* PS 2 Port. */
lRetVal = IFD_NOT_SUPPORTED;
break;
case 0x08: /* SCSI Port. */
lRetVal = IFD_NOT_SUPPORTED;
break;
case 0x10: /* IDE Port. */
lRetVal = IFD_NOT_SUPPORTED;
break;
case 0x20: /* USB Port. */
lRetVal = IFD_NOT_SUPPORTED;
break;
/* case 0xFy where y is vendor defined 0 - F is NOT implemented */
default: /* Port Not Found. */
lRetVal = IFD_NOT_SUPPORTED;
break;
}
if ( lRetVal == IFD_SUCCESS ) {
if ( CT_init( iTerm, iPort ) == OK ) {
if ( CT_data( iTerm, &dad, &sad, lc, pcPowerUp, &lr, Response ) ==
OK ) {
lRetVal = IFD_SUCCESS;
} else {
lRetVal = IFD_COMMUNICATION_ERROR;
}
} else {
lRetVal = IFD_COMMUNICATION_ERROR;
}
}
return lRetVal;
}
RESPONSECODE IO_Close_Channel () {
RESPONSECODE lRetVal = IFD_SUCCESS;
unsigned int iTerm = 0;
if ( CT_close( iTerm ) == OK ) {
lRetVal = IFD_SUCCESS;
} else {
lRetVal = IFD_COMMUNICATION_ERROR;
}
return lRetVal;
}
RESPONSECODE IFD_Get_Capabilities ( DWORD Tag, BYTE Value[] ) {
RESPONSECODE lRetVal;
DWORD HighNibble;
DWORD LowNibble;
int i;
HighNibble = Tag >> 8;
LowNibble = Tag - ( HighNibble << 8 ); /* Shift left and subtract. */
if ( HighNibble == 0x02 ) {
switch( LowNibble ) {
case 0x01:
lRetVal = IFD_SUCCESS;
break;
}
} else if ( HighNibble == 0x03 ) { /* This is the ICC_STATE */
switch( LowNibble ) {
case 0x00:
break;
case 0x01:
if ( IFD_Is_ICC_Present() == IFD_SUCCESS ) {
*(DWORD*)Value = IFD_ICC_PRESENT;
} else {
*(DWORD*)Value = IFD_ICC_NOT_PRESENT;
}
lRetVal = IFD_SUCCESS;
break;
case 0x03:
memcpy(Value, ICC.ATR, MAX_ATR_SIZE);
lRetVal = IFD_SUCCESS;
break;
case 0x04:
break;
default:
break;
}
}
return lRetVal;
}
RESPONSECODE IFD_Set_Capabilities ( DWORD Tag, BYTE Value[] ) {
RESPONSECODE lRetVal;
DWORD HighNibble;
DWORD LowNibble;
HighNibble = Tag >> 8;
LowNibble = Tag - ( HighNibble << 8 ); /* Shift left and subtract. */
if ( HighNibble == 0x03 ) { /* This is the ICC_STATE */
switch( LowNibble ) {
case 0x00:
break;
case 0x01:
break;
case 0x03:
break;
case 0x04:
break;
default:
break;
}
}
return lRetVal;
}
RESPONSECODE IFD_Set_Protocol_Parameters ( DWORD ProtocolType,
BYTE SelectionFlags,
BYTE PTS1, BYTE PTS2,
BYTE PTS3 ) {
RESPONSECODE lRetVal;
lRetVal = IFD_NOT_SUPPORTED;
return lRetVal;
}
RESPONSECODE IFD_Power_ICC ( DWORD ActionRequested ) {
RESPONSECODE lRetVal;
RESPONSECODE TestRsp;
int i;
int lc;
unsigned int lr;
unsigned int iTerm;
BYTE sad;
BYTE dad;
BYTE Response[MAX_RESPONSE_SIZE];
BYTE pcPowerUp[] = {0x20, 0x12, 0x00, 0x00, 0x00};
BYTE pcPowerDown[] = {0x20, 0x15, 0x00, 0x00, 0x00};
BYTE pcReset[] = {0x20, 0x11, 0x00, 0x01, 0x00};
iTerm = 1;
sad = 2;
dad = 1;
lc = 5;
lr = 2;
if ( ActionRequested == IFD_POWER_UP ) {
/*
if ( CT_data( iTerm, &dad, &sad, lc, pcPowerUp, &lr, Response ) ==
OK ) {
*/
sad = 2;
dad = 1;
lr = MAX_ATR_SIZE;
TestRsp = CT_data( iTerm, &dad, &sad, lc, pcReset, &lr, Response );
if (TestRsp == OK ) {
memcpy( ICC.ATR, Response, lr ); /* Copies the ATR. */
for ( i=lr-2; i < MAX_ATR_SIZE; i++ ) { /* Copies zeros to rest. */
ICC.ATR[i] = 0x00;
}
lRetVal = IFD_SUCCESS;
} else {
lRetVal = IFD_ERROR_POWER_ACTION;
}
/*
} else {
lRetVal = IFD_ERROR_POWER_ACTION;
}
*/
} else if ( ActionRequested == IFD_POWER_DOWN ) {
if ( CT_data( iTerm, &dad, &sad, lc, pcPowerDown, &lr, Response ) ==
OK ) {
lRetVal = IFD_SUCCESS;
} else {
lRetVal = IFD_ERROR_POWER_ACTION;
}
} else if ( ActionRequested == IFD_RESET ) {
if ( CT_data( iTerm, &dad, &sad, lc, pcReset, &lr, Response ) ==
OK ) {
memcpy( ICC.ATR, Response, lr ); /* Copies the ATR. */
for ( i=lr; i < sizeof(ICC.ATR); i++ ) { /* Copies zeros to rest. */
ICC.ATR[i] = 0x00;
}
lRetVal = IFD_SUCCESS;
} else {
lRetVal = IFD_ERROR_POWER_ACTION;
}
} else {
lRetVal = IFD_NOT_SUPPORTED;
}
return lRetVal;
}
RESPONSECODE IFD_Swallow_ICC() {
RESPONSECODE lRetVal;
lRetVal = IFD_ERROR_NOT_SUPPORTED; /* This is not supported. */
return lRetVal;
}
RESPONSECODE IFD_Eject_ICC() {
RESPONSECODE lRetVal;
lRetVal = IFD_ERROR_NOT_SUPPORTED; /* This is not supported. */
return lRetVal;
}
RESPONSECODE IFD_Confiscate_ICC() {
RESPONSECODE lRetVal;
lRetVal = IFD_ERROR_NOT_SUPPORTED; /* This is not supported. */
return lRetVal;
}
RESPONSECODE IFD_Transmit_to_ICC ( struct SCARD_IO_HEADER SendPci,
BYTE CommandData[], DWORD CommandSize,
BYTE ResponseData[], DWORD *ResponseSize,
struct SCARD_IO_HEADER *RecvPci ) {
RESPONSECODE lRetVal;
int i;
DWORD lc;
DWORD Protocol;
unsigned int lr;
unsigned int iTerm;
BYTE sad;
BYTE dad;
iTerm = 1;
sad = 2;
dad = 0;
lc = CommandSize;
/* Print Transmit Error Messaging */
#ifdef PCSC_DEBUG
printf("[%04x] -> ", lc );
for ( i=0; i < lc; i++ ) {
printf("%02x ", CommandData[i]);
} printf("\n");
#endif
if ( lc > 5 ) {
lr = 2;
} else {
lr = CommandData[4] + 2;
}
if ( SendPci.Protocol == 0 ) {
/* Switch modes on the 60 */
if ( dwCurrentProtocol != 0 ) {
Adm_SetMode( 0, 0 );
dwCurrentProtocol = 0;
}
lRetVal = CT_data( iTerm, &dad, &sad, lc, CommandData, &lr, ResponseData );
if ( lRetVal != IFD_SUCCESS ) {
*ResponseSize = 0;
return IFD_COMMUNICATION_ERROR;
} else {
*ResponseSize = lr;
lRetVal = IFD_SUCCESS;
}
} else if ( SendPci.Protocol == 1 ) {
/* Switch modes on the 60 */
if ( dwCurrentProtocol != 1 ) {
Adm_SetMode( 1, 0 );
dwCurrentProtocol = 1;
}
lRetVal = T1_ExchangeData( CommandData, lc, ResponseData, ResponseSize );
if ( lRetVal != STATUS_SUCCESS ) {
*ResponseSize = 0;
return IFD_COMMUNICATION_ERROR;
} else {
lRetVal = IFD_SUCCESS;
}
} else {
*ResponseSize = 0;
return IFD_ERROR_NOT_SUPPORTED;
}
/* Print Receive Error Messaging */
#ifdef PCSC_DEBUG
printf("[%04x] <- ", *ResponseSize );
for ( i=0; i < lr; i++ ) {
printf("%02x ", ResponseData[i]);
} printf("\n");
#endif
return lRetVal;
}
RESPONSECODE IFD_Is_ICC_Present() {
RESPONSECODE lRetVal;
RESPONSECODE TestRsp;
int lc;
unsigned int lr;
unsigned int iTerm;
int i;
BYTE sad;
BYTE dad;
BYTE Response[MAX_RESPONSE_SIZE];
BYTE pcStatus[5] = {0x20, 0x13, 0x00, 0x00, 0x00};
iTerm = 0;
i=0;
sad = 2;
dad = 1;
lc = 5;
lr = 7;
TestRsp = CT_data( iTerm, &dad, &sad, lc, pcStatus, &lr, Response );
if ( TestRsp == OK ) {
if ( Response[0] & CTBCS_DATA_STATUS_CARD ) { /* Card Inserted */
return IFD_ICC_PRESENT;
} else {
return IFD_ICC_NOT_PRESENT;
}
}
return IFD_COMMUNICATION_ERROR;
}
RESPONSECODE IFD_Is_ICC_Absent() {
RESPONSECODE lRetVal;
int lc;
unsigned int lr;
unsigned int iTerm;
BYTE sad;
BYTE dad;
BYTE Response[MAX_RESPONSE_SIZE];
BYTE pcStatus[5] = {0x20, 0x13, 0x00, 0x00, 0x00};
iTerm = 0;
sad = 2;
dad = 1;
lc = 5;
lr = 7;
if ( CT_data( iTerm, &dad, &sad, lc, pcStatus, &lr, Response ) ==
OK ) {
if ( ! Response[0] & CTBCS_DATA_STATUS_CARD ) { /* Card Removed */
return IFD_ICC_NOT_PRESENT;
} else {
return IFD_ICC_PRESENT;
}
}
return IFD_COMMUNICATION_ERROR;
}
|