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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5