/[pcsclite]/trunk/Drivers/ccid/src/commands.c
ViewVC logotype

Contents of /trunk/Drivers/ccid/src/commands.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1858 - (show annotations) (download)
Mon Feb 6 15:23:41 2006 UTC (7 years, 3 months ago) by rousseau
File MIME type: text/plain
File size: 34930 byte(s)
SecurePINModify(): add support of T=1 cards with TPDU readers
1 /*
2 commands.c: Commands sent to the card
3 Copyright (C) 2003-2004 Ludovic Rousseau
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /*
21 * $Id$
22 */
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <pcsclite.h>
27 #include <ifdhandler.h>
28 #include <reader.h>
29
30 #include "commands.h"
31 #include "openct/proto-t1.h"
32 #include "ccid.h"
33 #include "defs.h"
34 #include "ccid_ifdhandler.h"
35 #include "config.h"
36 #include "debug.h"
37
38 /* All the pinpad readers I used are more or less bogus
39 * I use code to change the user command and make the firmware happy */
40 #define BOGUS_PINPAD_FIRMWARE
41
42 #define max( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
43 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
44
45 /* internal functions */
46 static RESPONSECODE CmdXfrBlockTPDU_T0(unsigned int reader_index,
47 unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length,
48 unsigned char rx_buffer[]);
49
50 static RESPONSECODE CmdXfrBlockCHAR_T0(unsigned int reader_index, unsigned int
51 tx_length, unsigned char tx_buffer[], unsigned int *rx_length, unsigned
52 char rx_buffer[]);
53
54 static RESPONSECODE CmdXfrBlockTPDU_T1(unsigned int reader_index,
55 unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length,
56 unsigned char rx_buffer[]);
57
58 static void i2dw(int value, unsigned char *buffer);
59
60
61 /*****************************************************************************
62 *
63 * CmdPowerOn
64 *
65 ****************************************************************************/
66 RESPONSECODE CmdPowerOn(unsigned int reader_index, unsigned int * nlength,
67 unsigned char buffer[], int voltage)
68 {
69 unsigned char cmd[10];
70 status_t res;
71 int length, count = 1;
72 unsigned int atr_len;
73 RESPONSECODE return_value = IFD_SUCCESS;
74 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
75
76 /* store length of buffer[] */
77 length = *nlength;
78
79 if (ccid_descriptor->dwFeatures & CCID_CLASS_AUTO_VOLTAGE)
80 voltage = 0; /* automatic voltage selection */
81
82 again:
83 cmd[0] = 0x62; /* IccPowerOn */
84 cmd[1] = cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */
85 cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */
86 cmd[6] = (*ccid_descriptor->pbSeq)++;
87 cmd[7] = voltage;
88 cmd[8] = cmd[9] = 0; /* RFU */
89
90 res = WritePort(reader_index, sizeof(cmd), cmd);
91 if (res != STATUS_SUCCESS)
92 return IFD_COMMUNICATION_ERROR;
93
94 /* reset available buffer size */
95 /* needed if we go back after a switch to ISO mode */
96 *nlength = length;
97
98 res = ReadPort(reader_index, nlength, buffer);
99 if (res != STATUS_SUCCESS)
100 return IFD_COMMUNICATION_ERROR;
101
102 if (buffer[STATUS_OFFSET] & CCID_COMMAND_FAILED)
103 {
104 ccid_error(buffer[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); /* bError */
105
106 if (0xBB == buffer[ERROR_OFFSET] && /* Protocol error in EMV mode */
107 ((GEMPC433 == ccid_descriptor->readerID)
108 || (CHERRYXX33 == ccid_descriptor->readerID)))
109 {
110 unsigned char cmd[] = {0x1F, 0x01};
111 unsigned char res[1];
112 unsigned int res_length = sizeof(res);
113
114 if ((return_value = CmdEscape(reader_index, cmd, sizeof(cmd), res,
115 &res_length)) != IFD_SUCCESS)
116 return return_value;
117
118 /* avoid looping if we can't switch mode */
119 if (count--)
120 goto again;
121 else
122 DEBUG_CRITICAL("Can't set reader in ISO mode");
123 }
124
125 /* continue with 3 volts and 5 volts */
126 if (voltage > 1)
127 {
128 char *voltage_code[] = { "auto", "5V", "3V", "1.8V" };
129
130 DEBUG_INFO3("Power up with %s failed. Try with %s.",
131 voltage_code[voltage], voltage_code[voltage-1]);
132 voltage--;
133 goto again;
134 }
135
136 return IFD_COMMUNICATION_ERROR;
137 }
138
139 /* extract the ATR */
140 atr_len = dw2i(buffer, 1); /* ATR length */
141 if (atr_len > *nlength)
142 atr_len = *nlength;
143 else
144 *nlength = atr_len;
145
146 memmove(buffer, buffer+10, atr_len);
147
148 return return_value;
149 } /* CmdPowerOn */
150
151
152 /*****************************************************************************
153 *
154 * SecurePINVerify
155 *
156 ****************************************************************************/
157 RESPONSECODE SecurePINVerify(unsigned int reader_index,
158 unsigned char TxBuffer[], unsigned int TxLength,
159 unsigned char RxBuffer[], unsigned int *RxLength)
160 {
161 unsigned char cmd[11+14+CMD_BUF_SIZE];
162 unsigned int a, b;
163 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
164 int old_read_timeout;
165 RESPONSECODE ret;
166
167 cmd[0] = 0x69; /* Secure */
168 cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */
169 cmd[6] = (*ccid_descriptor->pbSeq)++;
170 cmd[7] = 0; /* bBWI */
171 cmd[8] = 0; /* wLevelParameter */
172 cmd[9] = 0;
173 cmd[10] = 0; /* bPINOperation: PIN Verification */
174
175 /* 19 is the size of the PCSCv2 PIN verify structure
176 * The equivalent CCID structure is only 14-bytes long */
177 if (TxLength > 19+CMD_BUF_SIZE) /* command too large? */
178 {
179 DEBUG_INFO3("Command too long: %d > %d", TxLength, 16+CMD_BUF_SIZE);
180 *RxLength = 0;
181 return IFD_NOT_SUPPORTED;
182 }
183
184 if (TxLength < 19+4 /* 4 = APDU size */) /* command too short? */
185 {
186 DEBUG_INFO3("Command too short: %d < %d", TxLength, 19+4);
187 *RxLength = 0;
188 return IFD_NOT_SUPPORTED;
189 }
190
191 if (dw2i(TxBuffer, 15) + 19 != TxLength) /* ulDataLength field coherency */
192 {
193 DEBUG_INFO3("Wrong lengths: %d %d", TxBuffer[15] + 19, TxLength);
194 *RxLength = 0;
195 return IFD_NOT_SUPPORTED;
196 }
197
198 #ifdef BOGUS_PINPAD_FIRMWARE
199 /* bug circumvention for the GemPC Pinpad */
200 if (GEMPCPINPAD == ccid_descriptor->readerID)
201 {
202 /* the firmware reject the cases: 00h No string and FFh default
203 * CCID message. The only value supported is 01h (display 1 message) */
204 if (0x01 != TxBuffer[8])
205 {
206 DEBUG_INFO2("Correct bNumberMessage for GemPC Pinpad (was %d)",
207 TxBuffer[8]);
208 TxBuffer[8] = 0x01;
209 }
210 }
211 #endif
212
213 /* Build a CCID block from a PC/SC V2.1.2 Part 10 block */
214 for (a = 11, b = 0; b < TxLength; b++)
215 {
216 if (1 == b) /* bTimeOut2 field */
217 /* Ignore the second timeout as there's nothing we can do with
218 * it currently */
219 continue;
220
221 if ((b >= 15) && (b <= 18)) /* ulDataLength field (4 bytes) */
222 /* the ulDataLength field is not present in the CCID frame
223 * so do not copy */
224 continue;
225
226 /* copy the CCID block 'verbatim' */
227 cmd[a] = TxBuffer[b];
228 a++;
229 }
230
231 /* SPR532 and Case 1 APDU */
232 if ((SPR532 == ccid_descriptor->readerID) && (TxBuffer[15] == 4))
233 {
234 RESPONSECODE return_value;
235 unsigned char cmd[] = { 0x80, 0x02, 0x00 };
236 unsigned char res[1];
237 unsigned int res_length = sizeof(res);
238
239 /* the SPR532 will append the PIN code without any padding */
240 return_value = CmdEscape(reader_index, cmd, sizeof(cmd), res,
241 &res_length);
242 if (return_value != IFD_SUCCESS)
243 {
244 ccid_error(res[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__);
245 return return_value;
246 }
247 }
248
249 /* T=1 Protocol Management for a TPDU reader */
250 if ((SCARD_PROTOCOL_T1 == ccid_descriptor->cardProtocol)
251 && (CCID_CLASS_TPDU == (ccid_descriptor->dwFeatures & CCID_CLASS_EXCHANGE_MASK)))
252 {
253 ct_buf_t sbuf;
254 unsigned char sdata[T1_BUFFER_SIZE];
255
256 /* Initialize send buffer with the APDU */
257 ct_buf_set(&sbuf,
258 (void *)(TxBuffer + offsetof(PIN_VERIFY_STRUCTURE, abData)),
259 TxLength - offsetof(PIN_VERIFY_STRUCTURE, abData));
260
261 /* Create T=1 block */
262 ret = t1_build(&((get_ccid_slot(reader_index))->t1),
263 sdata, 0, T1_I_BLOCK, &sbuf, NULL);
264
265 /* Increment the sequence numbers */
266 get_ccid_slot(reader_index)->t1.ns ^= 1;
267 get_ccid_slot(reader_index)->t1.nr ^= 1;
268
269 /* Copy the generated T=1 block prologue into the teoprologue
270 * of the CCID command */
271 memcpy(cmd+22, sdata, 3);
272 }
273
274 i2dw(a - 10, cmd + 1); /* CCID message length */
275
276 old_read_timeout = ccid_descriptor -> readTimeout;
277 ccid_descriptor -> readTimeout = max(30, TxBuffer[0]); /* at least 30 seconds */
278
279 if (WritePort(reader_index, a, cmd) != STATUS_SUCCESS)
280 {
281 *RxLength = 0;
282 return IFD_COMMUNICATION_ERROR;
283 }
284
285 ret = CCID_Receive(reader_index, RxLength, RxBuffer);
286
287 /* T=1 Protocol Management for a TPDU reader */
288 if ((SCARD_PROTOCOL_T1 == ccid_descriptor->cardProtocol)
289 && (CCID_CLASS_TPDU == (ccid_descriptor->dwFeatures & CCID_CLASS_EXCHANGE_MASK)))
290 {
291 /* get only the T=1 data */
292 /* FIXME: manage T=1 error blocks */
293 memmove(RxBuffer, RxBuffer+3, *RxLength -4);
294 *RxLength -= 4; /* remove NAD, PCB, LEN and CRC */
295 }
296
297 ccid_descriptor -> readTimeout = old_read_timeout;
298 return ret;
299 } /* SecurePINVerify */
300
301
302 /*****************************************************************************
303 *
304 * SecurePINModify
305 *
306 ****************************************************************************/
307 RESPONSECODE SecurePINModify(unsigned int reader_index,
308 unsigned char TxBuffer[], unsigned int TxLength,
309 unsigned char RxBuffer[], unsigned int *RxLength)
310 {
311 unsigned char cmd[11+19+CMD_BUF_SIZE];
312 unsigned int a, b;
313 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
314 int old_read_timeout;
315 RESPONSECODE ret;
316
317 cmd[0] = 0x69; /* Secure */
318 cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */
319 cmd[6] = (*ccid_descriptor->pbSeq)++;
320 cmd[7] = 0; /* bBWI */
321 cmd[8] = 0; /* wLevelParameter */
322 cmd[9] = 0;
323 cmd[10] = 1; /* bPINOperation: PIN Modification */
324
325 /* 24 is the size of the PCSC PIN modify structure
326 * The equivalent CCID structure is only 18 or 19-bytes long */
327 if ((TxLength > 19+CMD_BUF_SIZE) /* command too large? */
328 || (TxLength < 18+4 /* 4 = APDU size */) /* command too short? */
329 || (TxBuffer[20] + 24 != TxLength)) /* ulDataLength field coherency */
330 {
331 *RxLength = 0;
332 return IFD_NOT_SUPPORTED;
333 }
334
335 /* Make sure in the beginning if bNumberMessage is valid or not */
336 if (TxBuffer[11] > 3)
337 {
338 *RxLength = 0;
339 return IFD_NOT_SUPPORTED;
340 }
341
342 #ifdef BOGUS_PINPAD_FIRMWARE
343 /* some firmwares are buggy so we try to "correct" the frame */
344 /*
345 * SPR 532 and Cherry ST 2000C has no display but requires _all_
346 * bMsgIndex fields with bNumberMessage set to 0.
347 */
348 if ((SPR532 == ccid_descriptor->readerID)
349 || (CHERRYST2000 == ccid_descriptor->readerID))
350 {
351 TxBuffer[11] = 0x03; /* set bNumberMessages to 3 so that
352 all bMsgIndex123 are filled */
353 TxBuffer[14] = TxBuffer[15] = TxBuffer[16] = 0; /* bMsgIndex123 */
354 }
355
356 /* bug circumvention for the GemPC Pinpad */
357 if (GEMPCPINPAD == ccid_descriptor->readerID)
358 {
359 /* The reader does not support, and actively reject, "max size reached"
360 * and "timeout occured" validation conditions */
361 if (0x02 != TxBuffer[10])
362 {
363 DEBUG_INFO2("Correct bEntryValidationCondition for GemPC Pinpad (was %d)",
364 TxBuffer[10]);
365 TxBuffer[10] = 0x02; /* validation key pressed */
366 }
367
368 /* the reader does not support any other value than 3 for the number
369 * of messages */
370 if (0x03 != TxBuffer[11])
371 {
372 DEBUG_INFO2("Correct bNumberMessages for GemPC Pinpad (was %d)",
373 TxBuffer[11]);
374 TxBuffer[11] = 0x03; /* 3 messages */
375 }
376 }
377 #endif
378
379 /* T=1 Protocol Management for a TPDU reader */
380 if ((SCARD_PROTOCOL_T1 == ccid_descriptor->cardProtocol)
381 && (CCID_CLASS_TPDU == (ccid_descriptor->dwFeatures & CCID_CLASS_EXCHANGE_MASK)))
382 {
383 ct_buf_t sbuf;
384 unsigned char sdata[T1_BUFFER_SIZE];
385
386 /* Initialize send buffer with the APDU */
387 ct_buf_set(&sbuf,
388 (void *)(TxBuffer + offsetof(PIN_MODIFY_STRUCTURE, abData)),
389 TxLength - offsetof(PIN_MODIFY_STRUCTURE, abData));
390
391 /* Create T=1 block */
392 ret = t1_build(&((get_ccid_slot(reader_index))->t1),
393 sdata, 0, T1_I_BLOCK, &sbuf, NULL);
394
395 /* Increment the sequence numbers */
396 get_ccid_slot(reader_index)->t1.ns ^= 1;
397 get_ccid_slot(reader_index)->t1.nr ^= 1;
398
399 /* Copy the generated T=1 block prologue into the teoprologue
400 * of the CCID command */
401 memcpy(TxBuffer + offsetof(PIN_MODIFY_STRUCTURE, bTeoPrologue),
402 sdata, 3);
403 }
404
405 /* Build a CCID block from a PC/SC V2.1.2 Part 10 block */
406
407 /* Do adjustments as needed - CCID spec is not exact with some
408 * details in the format of the structure, per-reader adaptions
409 * might be needed.
410 */
411 for (a = 11, b = 0; b < TxLength; b++)
412 {
413 if (1 == b) /* bTimeOut2 */
414 /* Ignore the second timeout as there's nothing we can do with it
415 * currently */
416 continue;
417
418 if (15 == b) /* bMsgIndex2 */
419 {
420 /* in CCID the bMsgIndex2 is present only if bNumberMessage != 0 */
421 if (0 == TxBuffer[11])
422 continue;
423 }
424
425 if (16 == b) /* bMsgIndex3 */
426 {
427 /* in CCID the bMsgIndex3 is present only if bNumberMessage == 3 */
428 if (TxBuffer[11] < 3)
429 continue;
430 }
431
432 if ((b >= 20) && (b <= 23)) /* ulDataLength field (4 bytes) */
433 /* the ulDataLength field is not present in the CCID frame
434 * so do not copy */
435 continue;
436
437 /* copy to the CCID block 'verbatim' */
438 cmd[a] = TxBuffer[b];
439 a++;
440 }
441
442 #ifdef BOGUS_PINPAD_FIRMWARE
443 if ((SPR532 == ccid_descriptor->readerID)
444 || (CHERRYST2000 == ccid_descriptor->readerID))
445 {
446 cmd[21] = 0x00; /* set bNumberMessages to 0 */
447 }
448 #endif
449
450 /* We know the size of the CCID message now */
451 i2dw(a - 10, cmd + 1); /* command length (includes bPINOperation) */
452
453 old_read_timeout = ccid_descriptor -> readTimeout;
454 ccid_descriptor -> readTimeout = max(30, TxBuffer[0]); /* at least 30 seconds */
455
456 if (WritePort(reader_index, a, cmd) != STATUS_SUCCESS)
457 {
458 *RxLength = 0;
459 return IFD_COMMUNICATION_ERROR;
460 }
461
462 ret = CCID_Receive(reader_index, RxLength, RxBuffer);
463
464 /* T=1 Protocol Management for a TPDU reader */
465 if ((SCARD_PROTOCOL_T1 == ccid_descriptor->cardProtocol)
466 && (CCID_CLASS_TPDU == (ccid_descriptor->dwFeatures & CCID_CLASS_EXCHANGE_MASK)))
467 {
468 /* get only the T=1 data */
469 /* FIXME: manage T=1 error blocks */
470 memmove(RxBuffer, RxBuffer+3, *RxLength -4);
471 *RxLength -= 4; /* remove NAD, PCB, LEN and CRC */
472 }
473
474 ccid_descriptor -> readTimeout = old_read_timeout;
475 return ret;
476 } /* SecurePINModify */
477
478
479 /*****************************************************************************
480 *
481 * Escape
482 *
483 ****************************************************************************/
484 RESPONSECODE CmdEscape(unsigned int reader_index,
485 const unsigned char TxBuffer[], unsigned int TxLength,
486 unsigned char RxBuffer[], unsigned int *RxLength)
487 {
488 unsigned char *cmd_in, *cmd_out;
489 status_t res;
490 unsigned int length_in, length_out;
491 RESPONSECODE return_value = IFD_SUCCESS;
492 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
493
494 again:
495 /* allocate buffers */
496 length_in = 10 + TxLength;
497 if (NULL == (cmd_in = malloc(length_in)))
498 return IFD_COMMUNICATION_ERROR;
499
500 length_out = 10 + *RxLength;
501 if (NULL == (cmd_out = malloc(length_out)))
502 {
503 free(cmd_in);
504 return IFD_COMMUNICATION_ERROR;
505 }
506
507 cmd_in[0] = 0x6B; /* PC_to_RDR_Escape */
508 i2dw(length_in - 10, cmd_in+1); /* dwLength */
509 cmd_in[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */
510 cmd_in[6] = (*ccid_descriptor->pbSeq)++;
511 cmd_in[7] = cmd_in[8] = cmd_in[9] = 0; /* RFU */
512
513 /* copy the command */
514 memcpy(&cmd_in[10], TxBuffer, TxLength);
515
516 res = WritePort(reader_index, length_in, cmd_in);
517 free(cmd_in);
518 if (res != STATUS_SUCCESS)
519 {
520 free(cmd_out);
521 return IFD_COMMUNICATION_ERROR;
522 }
523
524 res = ReadPort(reader_index, &length_out, cmd_out);
525
526 /* replay the command if NAK
527 * This (generally) happens only for the first command sent to the reader
528 * with the serial protocol so it is not really needed for all the other
529 * ReadPort() calls */
530 if (STATUS_COMM_NAK == res)
531 {
532 free(cmd_out);
533 goto again;
534 }
535
536 if (res != STATUS_SUCCESS)
537 {
538 free(cmd_out);
539 return IFD_COMMUNICATION_ERROR;
540 }
541
542 if (cmd_out[STATUS_OFFSET] & CCID_COMMAND_FAILED)
543 {
544 ccid_error(cmd_out[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); /* bError */
545 return_value = IFD_COMMUNICATION_ERROR;
546 }
547
548 /* copy the response */
549 length_out = dw2i(cmd_out, 1);
550 if (length_out > *RxLength)
551 length_out = *RxLength;
552 *RxLength = length_out;
553 memcpy(RxBuffer, &cmd_out[10], length_out);
554
555 free(cmd_out);
556
557 return return_value;
558 } /* Escape */
559
560
561 /*****************************************************************************
562 *
563 * CmdPowerOff
564 *
565 ****************************************************************************/
566 RESPONSECODE CmdPowerOff(unsigned int reader_index)
567 {
568 unsigned char cmd[10];
569 status_t res;
570 unsigned int length;
571 RESPONSECODE return_value = IFD_SUCCESS;
572 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
573
574 cmd[0] = 0x63; /* IccPowerOff */
575 cmd[1] = cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */
576 cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */
577 cmd[6] = (*ccid_descriptor->pbSeq)++;
578 cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */
579
580 res = WritePort(reader_index, sizeof(cmd), cmd);
581 if (res != STATUS_SUCCESS)
582 return IFD_COMMUNICATION_ERROR;
583
584 length = sizeof(cmd);
585 res = ReadPort(reader_index, &length, cmd);
586 if (res != STATUS_SUCCESS)
587 return IFD_COMMUNICATION_ERROR;
588
589 if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED)
590 {
591 ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); /* bError */
592 return_value = IFD_COMMUNICATION_ERROR;
593 }
594
595 return return_value;
596 } /* CmdPowerOff */
597
598
599 /*****************************************************************************
600 *
601 * CmdGetSlotStatus
602 *
603 ****************************************************************************/
604 RESPONSECODE CmdGetSlotStatus(unsigned int reader_index, unsigned char buffer[])
605 {
606 unsigned char cmd[10];
607 status_t res;
608 unsigned int length;
609 RESPONSECODE return_value = IFD_SUCCESS;
610 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
611
612 cmd[0] = 0x65; /* GetSlotStatus */
613 cmd[1] = cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */
614 cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */
615 cmd[6] = (*ccid_descriptor->pbSeq)++;
616 cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */
617
618 res = WritePort(reader_index, sizeof(cmd), cmd);
619 if (res != STATUS_SUCCESS)
620 return IFD_COMMUNICATION_ERROR;
621
622 length = SIZE_GET_SLOT_STATUS;
623 res = ReadPort(reader_index, &length, buffer);
624 if (res != STATUS_SUCCESS)
625 return IFD_COMMUNICATION_ERROR;
626
627 if (buffer[STATUS_OFFSET] & CCID_COMMAND_FAILED)
628 {
629 ccid_error(buffer[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); /* bError */
630
631 /* card absent or mute is not an communication error */
632 if (buffer[ERROR_OFFSET] != 0xFE)
633 return_value = IFD_COMMUNICATION_ERROR;
634 }
635
636 return return_value;
637 } /* CmdGetSlotStatus */
638
639
640 /*****************************************************************************
641 *
642 * CmdXfrBlock
643 *
644 ****************************************************************************/
645 RESPONSECODE CmdXfrBlock(unsigned int reader_index, unsigned int tx_length,
646 unsigned char tx_buffer[], unsigned int *rx_length,
647 unsigned char rx_buffer[], int protocol)
648 {
649 RESPONSECODE return_value = IFD_SUCCESS;
650 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
651
652 /* command length too big for CCID reader? */
653 if (tx_length > ccid_descriptor->dwMaxCCIDMessageLength)
654 {
655 DEBUG_CRITICAL3("Command too long (%d bytes) for max: %d bytes",
656 tx_length, ccid_descriptor->dwMaxCCIDMessageLength);
657 return_value = IFD_COMMUNICATION_ERROR;
658 goto clean_up_and_return;
659 }
660
661 /* command length too big for CCID driver? */
662 if (tx_length > CMD_BUF_SIZE)
663 {
664 DEBUG_CRITICAL3("Command too long (%d bytes) for max: %d bytes",
665 tx_length, CMD_BUF_SIZE);
666 return_value = IFD_COMMUNICATION_ERROR;
667 goto clean_up_and_return;
668 }
669
670 /* APDU or TPDU? */
671 switch (ccid_descriptor->dwFeatures & CCID_CLASS_EXCHANGE_MASK)
672 {
673 case CCID_CLASS_TPDU:
674 if (protocol == T_0)
675 return_value = CmdXfrBlockTPDU_T0(reader_index,
676 tx_length, tx_buffer, rx_length, rx_buffer);
677 else
678 if (protocol == T_1)
679 return_value = CmdXfrBlockTPDU_T1(reader_index, tx_length,
680 tx_buffer, rx_length, rx_buffer);
681 else
682 return_value = IFD_PROTOCOL_NOT_SUPPORTED;
683 break;
684
685 case CCID_CLASS_SHORT_APDU:
686 case CCID_CLASS_EXTENDED_APDU:
687 /* We only support extended APDU if the reader can support the
688 * command length. See test above */
689 return_value = CmdXfrBlockTPDU_T0(reader_index,
690 tx_length, tx_buffer, rx_length, rx_buffer);
691 break;
692
693 case CCID_CLASS_CHARACTER:
694 if (protocol == T_0)
695 return_value = CmdXfrBlockCHAR_T0(reader_index, tx_length,
696 tx_buffer, rx_length, rx_buffer);
697 else
698 if (protocol == T_1)
699 return_value = CmdXfrBlockTPDU_T1(reader_index, tx_length,
700 tx_buffer, rx_length, rx_buffer);
701 else
702 return_value = IFD_PROTOCOL_NOT_SUPPORTED;
703 break;
704
705 default:
706 *rx_length = 0;
707 return_value = IFD_COMMUNICATION_ERROR;
708 }
709
710 clean_up_and_return:
711 return return_value;
712 } /* CmdXfrBlock */
713
714
715 /*****************************************************************************
716 *
717 * CCID_Transmit
718 *
719 ****************************************************************************/
720 RESPONSECODE CCID_Transmit(unsigned int reader_index, unsigned int tx_length,
721 const unsigned char tx_buffer[], unsigned short rx_length, unsigned char bBWI)
722 {
723 unsigned char cmd[10+CMD_BUF_SIZE]; /* CCID + APDU buffer */
724 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
725
726 cmd[0] = 0x6F; /* XfrBlock */
727 i2dw(tx_length, cmd+1); /* APDU length */
728 cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */
729 cmd[6] = (*ccid_descriptor->pbSeq)++;
730 cmd[7] = bBWI; /* extend block waiting timeout */
731 cmd[8] = rx_length & 0xFF; /* Expected length, in character mode only */
732 cmd[9] = (rx_length >> 8) & 0xFF;
733
734 /* check that the command is not too large */
735 if (tx_length > CMD_BUF_SIZE)
736 return IFD_NOT_SUPPORTED;
737
738 memcpy(cmd+10, tx_buffer, tx_length);
739
740 if (WritePort(reader_index, 10+tx_length, cmd) != STATUS_SUCCESS)
741 return IFD_COMMUNICATION_ERROR;
742
743 return IFD_SUCCESS;
744 } /* CCID_Transmit */
745
746
747 /*****************************************************************************
748 *
749 * CCID_Receive
750 *
751 ****************************************************************************/
752 RESPONSECODE CCID_Receive(unsigned int reader_index, unsigned int *rx_length,
753 unsigned char rx_buffer[])
754 {
755 unsigned char cmd[10+CMD_BUF_SIZE]; /* CCID + APDU buffer */
756 unsigned int length;
757
758 time_request:
759 length = sizeof(cmd);
760 if (ReadPort(reader_index, &length, cmd) != STATUS_SUCCESS)
761 {
762 *rx_length = 0;
763 return IFD_COMMUNICATION_ERROR;
764 }
765
766 if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED)
767 {
768 ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); /* bError */
769 switch (cmd[ERROR_OFFSET])
770 {
771 case 0xEF: /* cancel */
772 if (*rx_length < 2)
773 return IFD_COMMUNICATION_ERROR;
774 rx_buffer[0]= 0x64;
775 rx_buffer[1]= 0x01;
776 *rx_length = 2;
777 return IFD_SUCCESS;
778
779 case 0xF0: /* timeout */
780 if (*rx_length < 2)
781 return IFD_COMMUNICATION_ERROR;
782 rx_buffer[0]= 0x64;
783 rx_buffer[1]= 0x00;
784 *rx_length = 2;
785 return IFD_SUCCESS;
786
787 case 0xFD: /* Parity error during exchange */
788 *rx_length = 0; /* nothing received */
789 return IFD_PARITY_ERROR;
790
791 default:
792 *rx_length = 0; /* nothing received */
793 return IFD_COMMUNICATION_ERROR;
794 }
795 }
796
797 if (cmd[STATUS_OFFSET] & CCID_TIME_EXTENSION)
798 {
799 DEBUG_COMM2("Time extension requested: 0x%02X", cmd[ERROR_OFFSET]);
800 goto time_request;
801 }
802
803 length = dw2i(cmd, 1);
804 if (length < *rx_length)
805 *rx_length = length;
806 else
807 length = *rx_length;
808 memcpy(rx_buffer, cmd+10, length);
809
810 return IFD_SUCCESS;
811 } /* CCID_Receive */
812
813
814 /*****************************************************************************
815 *
816 * CmdXfrBlockTPDU_T0
817 *
818 ****************************************************************************/
819 static RESPONSECODE CmdXfrBlockTPDU_T0(unsigned int reader_index,
820 unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length,
821 unsigned char rx_buffer[])
822 {
823 RESPONSECODE return_value = IFD_SUCCESS;
824
825 DEBUG_COMM2("T=0: %d bytes", tx_length);
826
827 return_value = CCID_Transmit(reader_index, tx_length, tx_buffer, 0, 0);
828 if (return_value != IFD_SUCCESS)
829 return return_value;
830
831 return CCID_Receive(reader_index, rx_length, rx_buffer);
832 } /* CmdXfrBlockTPDU_T0 */
833
834
835 /*****************************************************************************
836 *
837 * T0CmdParsing
838 *
839 ****************************************************************************/
840 static RESPONSECODE T0CmdParsing(unsigned char *cmd, unsigned int cmd_len,
841 unsigned int *exp_len)
842 {
843 *exp_len = 0;
844
845 /* Ref: 7816-4 Annex A */
846 switch (cmd_len)
847 {
848 case 4: /* Case 1 */
849 *exp_len = 2; /* SW1 and SW2 only */
850 break;
851
852 case 5: /* Case 2 */
853 if (cmd[4] != 0)
854 *exp_len = cmd[4] + 2;
855 else
856 *exp_len = 256 + 2;
857 break;
858
859 default: /* Case 3 */
860 if (cmd_len > 5 && cmd_len == (unsigned int)(cmd[4] + 5))
861 *exp_len = 2; /* SW1 and SW2 only */
862 else
863 return IFD_COMMUNICATION_ERROR; /* situation not supported */
864 break;
865 }
866
867 return IFD_SUCCESS;
868 } /* T0CmdParsing */
869
870
871 /*****************************************************************************
872 *
873 * T0ProcACK
874 *
875 ****************************************************************************/
876 static RESPONSECODE T0ProcACK(unsigned int reader_index,
877 unsigned char **snd_buf, unsigned int *snd_len,
878 unsigned char **rcv_buf, unsigned int *rcv_len,
879 unsigned char **in_buf, unsigned int *in_len,
880 unsigned int proc_len, int is_rcv)
881 {
882 RESPONSECODE return_value;
883 unsigned int remain_len;
884 unsigned char tmp_buf[512];
885 unsigned int ret_len;
886
887 DEBUG_COMM2("Enter, is_rcv = %d", is_rcv);
888
889 if (is_rcv == 1)
890 { /* Receiving mode */
891 if (*in_len > 0)
892 { /* There are still available data in our buffer */
893 if (*in_len >= proc_len)
894 {
895 /* We only need to get the data from our buffer */
896 memcpy(*rcv_buf, *in_buf, proc_len);
897 *rcv_buf += proc_len;
898 *in_buf += proc_len;
899 *rcv_len += proc_len;
900 *in_len -= proc_len;
901
902 return IFD_SUCCESS;
903 }
904 else
905 {
906 /* Move all data in the input buffer to the reply buffer */
907 remain_len = proc_len - *in_len;
908 memcpy(*rcv_buf, *in_buf, *in_len);
909 *rcv_buf += *in_len;
910 *in_buf += *in_len;
911 *rcv_len += *in_len;
912 *in_len = 0;
913 }
914 }
915 else
916 /* There is no data in our tmp_buf,
917 * we have to read all data we needed */
918 remain_len = proc_len;
919
920 /* Read the expected data from the smartcard */
921 if (*in_len != 0)
922 {
923 DEBUG_CRITICAL("*in_len != 0");
924 return IFD_COMMUNICATION_ERROR;
925 }
926
927 memset(tmp_buf, 0, sizeof(tmp_buf));
928
929 ret_len = remain_len;
930 return_value = CCID_Transmit(reader_index, 0, *snd_buf, ret_len, 0);
931 if (return_value != IFD_SUCCESS)
932 return return_value;
933
934 return_value = CCID_Receive(reader_index, &ret_len, tmp_buf);
935 if (return_value != IFD_SUCCESS)
936 return return_value;
937
938 memcpy(*rcv_buf, tmp_buf, remain_len);
939 *rcv_buf += remain_len, *rcv_len += remain_len;
940
941 /* If ret_len != remain_len, our logic is erroneous */
942 if (ret_len != remain_len)
943 {
944 DEBUG_CRITICAL("ret_len != remain_len");
945 return IFD_COMMUNICATION_ERROR;
946 }
947 }
948 else
949 { /* Sending mode */
950
951 return_value = CCID_Transmit(reader_index, proc_len, *snd_buf, 1, 0);
952 if (return_value != IFD_SUCCESS)
953 return return_value;
954
955 *snd_len -= proc_len;
956 *snd_buf += proc_len;
957 }
958
959 DEBUG_COMM("Exit");
960
961 return IFD_SUCCESS;
962 } /* T0ProcACK */
963
964
965 /*****************************************************************************
966 *
967 * T0ProcSW1
968 *
969 ****************************************************************************/
970 static RESPONSECODE T0ProcSW1(unsigned int reader_index,
971 unsigned char *rcv_buf, unsigned int *rcv_len,
972 unsigned char *in_buf, unsigned int in_len)
973 {
974 RESPONSECODE return_value = IFD_SUCCESS;
975 UCHAR tmp_buf[512];
976 unsigned char *rcv_buf_tmp = rcv_buf;
977 const unsigned int rcv_len_tmp = *rcv_len;
978 unsigned char sw1, sw2;
979
980 /* store the SW1 */
981 sw1 = *rcv_buf = *in_buf;
982 rcv_buf++;
983 in_buf++;
984 in_len--;
985 (*rcv_len)++;
986
987 /* store the SW2 */
988 if (0 == in_len)
989 {
990 return_value = CCID_Transmit(reader_index, 0, rcv_buf, 1, 0);
991 if (return_value != IFD_SUCCESS)
992 return return_value;
993
994 in_len = 1;
995
996 return_value = CCID_Receive(reader_index, &in_len, tmp_buf);
997 if (return_value != IFD_SUCCESS)
998 return return_value;
999
1000 in_buf = tmp_buf;
1001 }
1002 sw2 = *rcv_buf = *in_buf;
1003 rcv_buf++;
1004 in_buf++;
1005 in_len--;
1006 (*rcv_len)++;
1007
1008 if (return_value != IFD_SUCCESS)
1009 {
1010 rcv_buf_tmp[0] = rcv_buf_tmp[1] = 0;
1011 *rcv_len = rcv_len_tmp;
1012 }
1013
1014 DEBUG_COMM3("Exit: SW=%02X %02X", sw1, sw2);
1015
1016 return return_value;
1017 } /* T0ProcSW1 */
1018
1019
1020 /*****************************************************************************
1021 *
1022 * CmdXfrBlockCHAR_T0
1023 *
1024 ****************************************************************************/
1025 static RESPONSECODE CmdXfrBlockCHAR_T0(unsigned int reader_index,
1026 unsigned int snd_len, unsigned char snd_buf[], unsigned int *rcv_len,
1027 unsigned char rcv_buf[])
1028 {
1029 int is_rcv;
1030 unsigned char cmd[5];
1031 unsigned char tmp_buf[512];
1032 unsigned int exp_len, in_len;
1033 unsigned char ins, *in_buf;
1034 RESPONSECODE return_value = IFD_SUCCESS;
1035
1036 DEBUG_COMM2("T=0: %d bytes", snd_len);
1037
1038 in_buf = tmp_buf;
1039 in_len = 0;
1040 *rcv_len = 0;
1041
1042 return_value = T0CmdParsing(snd_buf, snd_len, &exp_len);
1043 if (return_value != IFD_SUCCESS)
1044 {
1045 DEBUG_CRITICAL("T0CmdParsing failed");
1046 return IFD_COMMUNICATION_ERROR;
1047 }
1048
1049 if (snd_len == 5 || snd_len == 4)
1050 is_rcv = 1;
1051 else
1052 is_rcv = 0;
1053
1054 /* Command to send to the smart card (must be 5 bytes, from 7816 p.15) */
1055 memset(cmd, 0, sizeof(cmd));
1056 if (snd_len == 4)
1057 {
1058 memcpy(cmd, snd_buf, 4);
1059 snd_buf += 4;
1060 snd_len -= 4;
1061 }
1062 else
1063 {
1064 memcpy(cmd, snd_buf, 5);
1065 snd_buf += 5;
1066 snd_len -= 5;
1067 }
1068
1069 /* Make sure this is a valid command by checking the INS field */
1070 ins = cmd[1];
1071 if ((ins & 0xF0) == 0x60 || /* 7816-3 8.3.2 */
1072 (ins & 0xF0) == 0x90)
1073 {
1074 DEBUG_CRITICAL2("fatal: INS (0x%02X) = 0x6X or 0x9X", ins);
1075 return IFD_COMMUNICATION_ERROR;
1076 }
1077
1078 return_value = CCID_Transmit(reader_index, 5, cmd, 1, 0);
1079 if (return_value != IFD_SUCCESS)
1080 return return_value;
1081
1082 while (1)
1083 {
1084 if (in_len == 0)
1085 {
1086 in_len = 1;
1087 return_value = CCID_Receive(reader_index, &in_len, tmp_buf);
1088 if (return_value != IFD_SUCCESS)
1089 {
1090 DEBUG_CRITICAL("CCID_Receive failed");
1091 return return_value;
1092 }
1093 in_buf = tmp_buf;
1094 }
1095 if (in_len == 0)
1096 {
1097 /* Suppose we should be able to get data.
1098 * If not, error. Set the time-out error */
1099 DEBUG_CRITICAL("error: in_len = 0");
1100 return IFD_RESPONSE_TIMEOUT;
1101 }
1102
1103 /* Start to process the procedure bytes */
1104 if (*in_buf == 0x60)
1105 {
1106 in_len = 0;
1107 return_value = CCID_Transmit(reader_index, 0, cmd, 1, 0);
1108
1109 if (return_value != IFD_SUCCESS)
1110 return return_value;
1111
1112 continue;
1113 }
1114 else if (*in_buf == ins || *in_buf == (ins ^ 0x01))
1115 {
1116 /* ACK => To transfer all remaining data bytes */
1117 in_buf++, in_len--;
1118 if (is_rcv)
1119 return_value = T0ProcACK(reader_index, &snd_buf, &snd_len,
1120 &rcv_buf, rcv_len, &in_buf, &in_len, exp_len - *rcv_len, 1);
1121 else
1122 return_value = T0ProcACK(reader_index, &snd_buf, &snd_len,
1123 &rcv_buf, rcv_len, &in_buf, &in_len, snd_len, 0);
1124
1125 if (*rcv_len == exp_len)
1126 return return_value;
1127
1128 continue;
1129 }
1130 else if (*in_buf == (ins ^ 0xFF) || *in_buf == (ins ^ 0xFE))
1131 {
1132 /* ACK => To transfer 1 remaining bytes */
1133 in_buf++, in_len--;
1134 return_value = T0ProcACK(reader_index, &snd_buf, &snd_len,
1135 &rcv_buf, rcv_len, &in_buf, &in_len, 1, is_rcv);
1136
1137 if (return_value != IFD_SUCCESS)
1138 return return_value;
1139
1140 continue;
1141 }
1142 else if ((*in_buf & 0xF0) == 0x60 || (*in_buf & 0xF0) == 0x90)
1143 /* SW1 */
1144 return T0ProcSW1(reader_index, rcv_buf, rcv_len, in_buf, in_len);
1145
1146 /* Error, unrecognized situation found */
1147 DEBUG_CRITICAL2("Unrecognized Procedure byte (0x%02X) found!", *in_buf);
1148 return return_value;
1149 }
1150
1151 return return_value;
1152 } /* CmdXfrBlockCHAR_T0 */
1153
1154
1155 /*****************************************************************************
1156 *
1157 * CmdXfrBlockTPDU_T1
1158 *
1159 ****************************************************************************/
1160 static RESPONSECODE CmdXfrBlockTPDU_T1(unsigned int reader_index,
1161 unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length,
1162 unsigned char rx_buffer[])
1163 {
1164 RESPONSECODE return_value = IFD_SUCCESS;
1165 int ret;
1166
1167 DEBUG_COMM2("T=1: %d bytes", tx_length);
1168
1169 ret = t1_transceive(&((get_ccid_slot(reader_index)) -> t1), 0,
1170 tx_buffer, tx_length, rx_buffer, *rx_length);
1171
1172 if (ret < 0)
1173 {
1174 *rx_length = 0;
1175 return_value = IFD_COMMUNICATION_ERROR;
1176 }
1177 else
1178 *rx_length = ret;
1179
1180 return return_value;
1181 } /* CmdXfrBlockTPDU_T1 */
1182
1183
1184 /*****************************************************************************
1185 *
1186 * SetParameters
1187 *
1188 ****************************************************************************/
1189 RESPONSECODE SetParameters(unsigned int reader_index, char protocol,
1190 unsigned int length, unsigned char buffer[])
1191 {
1192 unsigned char cmd[10+CMD_BUF_SIZE]; /* CCID + APDU buffer */
1193 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
1194
1195 DEBUG_COMM2("length: %d bytes", length);
1196
1197 cmd[0] = 0x61; /* SetParameters */
1198 i2dw(length, cmd+1); /* APDU length */
1199 cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */
1200 cmd[6] = (*ccid_descriptor->pbSeq)++;
1201 cmd[7] = protocol; /* bProtocolNum */
1202 cmd[8] = cmd[9] = 0; /* RFU */
1203
1204 /* check that the command is not too large */
1205 if (length > CMD_BUF_SIZE)
1206 return IFD_NOT_SUPPORTED;
1207
1208 memcpy(cmd+10, buffer, length);
1209
1210 if (WritePort(reader_index, 10+length, cmd) != STATUS_SUCCESS)
1211 return IFD_COMMUNICATION_ERROR;
1212
1213 length = sizeof(cmd);
1214 if (ReadPort(reader_index, &length, cmd) != STATUS_SUCCESS)
1215 return IFD_COMMUNICATION_ERROR;
1216
1217 if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED)
1218 {
1219 ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); /* bError */
1220 if (0x00 == cmd[ERROR_OFFSET]) /* command not supported */
1221 return IFD_NOT_SUPPORTED;
1222 else
1223 return IFD_COMMUNICATION_ERROR;
1224 }
1225
1226 return IFD_SUCCESS;
1227 } /* SetParameters */
1228
1229
1230 /*****************************************************************************
1231 *
1232 * isCharLevel
1233 *
1234 ****************************************************************************/
1235 int isCharLevel(int reader_index)
1236 {
1237 return CCID_CLASS_CHARACTER == (get_ccid_descriptor(reader_index)->dwFeatures & CCID_CLASS_EXCHANGE_MASK);
1238 } /* isCharLevel */
1239
1240
1241 /*****************************************************************************
1242 *
1243 * i2dw
1244 *
1245 ****************************************************************************/
1246 static void i2dw(int value, unsigned char buffer[])
1247 {
1248 buffer[0] = value & 0xFF;
1249 buffer[1] = (value >> 8) & 0xFF;
1250 buffer[2] = (value >> 16) & 0xFF;
1251 buffer[3] = (value >> 24) & 0xFF;
1252 } /* i2dw */
1253

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.5