Changeset 1563
- Timestamp:
- 02/14/10 20:35:52 (14 years ago)
- Location:
- trunk/csctapi
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/csctapi/apdu.c
r1559 r1563 43 43 int APDU_Cmd_Case (unsigned char * command, unsigned long command_len) 44 44 { 45 APDU_Cmd c, * apdu;46 c.command=command;47 c.length=command_len;48 apdu = &c;49 50 45 BYTE B1; 51 46 ushort B2B3; … … 54 49 55 50 /* Calculate length of body */ 56 L = MAX( apdu->length- 4, 0);51 L = MAX(command_len - 4, 0); 57 52 58 53 /* Case 1 */ … … 64 59 { 65 60 /* Get first byte of body */ 66 B1 = apdu->command[4];61 B1 = command[4]; 67 62 68 63 if ((B1 != 0) && (L == (ulong)B1 + 1)) … … 75 70 { 76 71 /* Get second and third byte of body */ 77 B2B3 = (((ushort)( apdu->command[5]) << 8) | apdu->command[6]);72 B2B3 = (((ushort)(command[5]) << 8) | command[6]); 78 73 79 74 if ((B2B3 != 0) && (L == (ulong)B2B3 + 3)) … … 91 86 } 92 87 } 93 94 return res;95 }96 97 BYTE APDU_Cmd_Ins (APDU_Cmd * apdu)98 {99 return apdu->command[1];100 }101 102 unsigned long APDU_Cmd_Lc (APDU_Cmd * apdu)103 {104 int c;105 unsigned long res;106 107 c = APDU_Cmd_Case (apdu->command, apdu->length);108 109 if ((c == APDU_CASE_1) || (c == APDU_CASE_3S) || (c == APDU_CASE_3E))110 res = 0;111 else if ((c == APDU_CASE_2S) || (c == APDU_CASE_4S))112 res = apdu->command[4];113 else if ((c == APDU_CASE_2E) || (c == APDU_CASE_4E))114 res = (((unsigned long)(apdu->command[5]) << 8) | apdu->command[6]);115 else116 res = 0;117 118 return res;119 }120 121 unsigned long APDU_Cmd_Le (APDU_Cmd * apdu)122 {123 int c;124 unsigned long res;125 126 c = APDU_Cmd_Case (apdu->command, apdu->length);127 128 if ((c == APDU_CASE_1) || (c == APDU_CASE_2S) || (c == APDU_CASE_2E))129 res = 0;130 else if (c == APDU_CASE_3S)131 // res = ((apdu->command[4] == 0) ? 256: apdu->command[4]);132 res = apdu->command[4];133 else if (c == APDU_CASE_4S)134 res = ((apdu->command[apdu->length - 1] == 0) ? 256: apdu->command[apdu->length - 1]);135 else if (c == APDU_CASE_3E)136 res = ((((unsigned long)(apdu->command[5]) << 8) | apdu->command[6]) == 0 ? 65536 : (((unsigned long)(apdu->command[5]) << 8) | apdu->command[6]));137 else if (c == APDU_CASE_4E)138 res = ((((unsigned long)(apdu->command[apdu->length - 2]) << 8) | apdu->command[apdu->length - 1]) == 0 ? 65536 : (((unsigned long)(apdu->command[apdu->length - 2]) << 8) | apdu->command[apdu->length - 1]));139 else140 res = 0;141 142 return res;143 }144 145 BYTE * APDU_Cmd_Header (APDU_Cmd * apdu)146 {147 return apdu->command;148 }149 150 151 BYTE * APDU_Cmd_Data (APDU_Cmd * apdu)152 {153 int c;154 BYTE * res;155 156 c = APDU_Cmd_Case (apdu->command, apdu->length);157 158 if ((c == APDU_CASE_1) || (c == APDU_CASE_3S) || (c == APDU_CASE_3E))159 res = NULL;160 else if ((c == APDU_CASE_2S) || (c == APDU_CASE_4S))161 res = apdu->command + 5;162 else if ((c == APDU_CASE_2E) || (c == APDU_CASE_4E))163 res = apdu->command + 7;164 else165 res = NULL;166 88 167 89 return res; -
trunk/csctapi/apdu.h
r1559 r1563 58 58 */ 59 59 60 /* Command APDU */61 typedef struct62 {63 BYTE *command;64 unsigned long length;65 }66 APDU_Cmd;67 68 60 /* Response APDU */ 69 61 typedef struct … … 80 72 /* Return the case of command */ 81 73 int APDU_Cmd_Case (unsigned char * command, unsigned long command_len); 82 83 /* Return command instruction */84 BYTE APDU_Cmd_Ins (APDU_Cmd * apdu);85 86 /* Return length of data sent */87 unsigned long APDU_Cmd_Lc (APDU_Cmd * apdu);88 89 /* Return length of data expected */90 unsigned long APDU_Cmd_Le (APDU_Cmd * apdu);91 92 /* Return a pointer to the header of the command */93 BYTE *APDU_Cmd_Header (APDU_Cmd * apdu);94 95 /* Return a pointer to the data of the comamnd */96 BYTE *APDU_Cmd_Data (APDU_Cmd * apdu);97 74 98 75 /* Create a APDU_Rsp */ -
trunk/csctapi/protocol_t0.c
r1559 r1563 72 72 return ERROR; 73 73 74 APDU_Cmd c, * cmd; 75 c.command=command; 76 c.length=command_len; 77 cmd = &c; 78 79 int cmd_case; 80 81 cmd_case = APDU_Cmd_Case (command, command_len); 74 int cmd_case = APDU_Cmd_Case (command, command_len); 82 75 switch (cmd_case) { 83 76 case APDU_CASE_2E: … … 105 98 static int Protocol_T0_Case2E (unsigned char * command, unsigned long command_len, APDU_Rsp ** rsp) 106 99 { 107 int ret = OK;108 100 BYTE buffer[PROTOCOL_T0_MAX_SHORT_COMMAND]; 109 101 APDU_Rsp *tpdu_rsp; … … 131 123 buffer[4] = MIN (255, command_len - i); 132 124 memcpy (buffer + 5, command + i, buffer[4]); 133 ret = Protocol_T0_ExchangeTPDU(buffer, buffer[4] + 5, (&tpdu_rsp)); 134 if (ret == OK) 135 { 125 call (Protocol_T0_ExchangeTPDU(buffer, buffer[4] + 5, (&tpdu_rsp))); 136 126 /* Card does support envelope command */ 137 127 if (APDU_Rsp_SW1 (tpdu_rsp) == 0x90) … … 139 129 /* This is not the last segment */ 140 130 if (buffer[4] + i < command_len) 141 { 142 /* Delete response TPDU */ 143 APDU_Rsp_Delete (tpdu_rsp); 144 } 131 APDU_Rsp_Delete (tpdu_rsp); //delete response TPDU 145 132 else 146 { 147 /* Map response TPDU onto APDU */ 148 (*rsp) = tpdu_rsp; 149 } 133 (*rsp) = tpdu_rsp;// Map response TPDU onto APDU 150 134 } 151 135 else /* Card does not support envelope command or error */ … … 155 139 break; 156 140 } 157 } 158 else 159 { 160 break; 161 } 162 } 163 164 return ret; 141 } 142 143 return OK; 165 144 } 166 145 … … 301 280 static int Protocol_T0_ExchangeTPDU (unsigned char * command, unsigned long command_len, APDU_Rsp ** rsp) 302 281 { 303 APDU_Cmd c, * cmd;304 c.command=command;305 c.length=command_len;306 cmd = &c;307 282 BYTE buffer[PROTOCOL_T0_MAX_SHORT_RESPONSE]; 308 283 BYTE *data; … … 311 286 (*rsp) = NULL;//in case of error this will be returned 312 287 313 /* Parse APDU */314 Lc = APDU_Cmd_Lc (cmd);315 Le = APDU_Cmd_Le (cmd);316 data = APDU_Cmd_Data (cmd);317 288 cmd_case = APDU_Cmd_Case (command, command_len); 318 319 /* Check case of command */ 320 if ((cmd_case != APDU_CASE_2S) && (cmd_case != APDU_CASE_3S)) 321 return ERROR; 322 323 call (ICC_Async_Transmit (5, APDU_Cmd_Header (cmd))); //Send header bytes 289 switch (cmd_case) { 290 case APDU_CASE_2S: 291 Lc = command[4]; 292 Le = 0; 293 data = command + 5; 294 break; 295 case APDU_CASE_3S: 296 Lc = 0; 297 Le = command[4]; 298 data = NULL; 299 break; 300 default: 301 return ERROR; 302 } 303 call (ICC_Async_Transmit (5, command)); //Send header bytes 324 304 325 305 /* Initialise counters */ … … 355 335 break; 356 336 } 357 else if ((buffer[recv] & 0x0E) == ( APDU_Cmd_Ins (cmd)& 0x0E)) /* ACK byte received */337 else if ((buffer[recv] & 0x0E) == (command[1] & 0x0E)) /* ACK byte received */ 358 338 {//printf("ack\n"); 359 339 /* Reset null's counter */ … … 384 364 } 385 365 } 386 else if ((buffer[recv] & 0x0E) == ((~ APDU_Cmd_Ins (cmd)) & 0x0E)) /* ~ACK byte received */366 else if ((buffer[recv] & 0x0E) == ((~command[1]) & 0x0E)) /* ~ACK byte received */ 387 367 {//printf("~ack\n"); 388 368 nulls = 0; //Reset null's counter … … 414 394 int Protocol_T14_ExchangeTPDU (unsigned char * cmd_raw, unsigned long command_len, APDU_Rsp ** rsp) 415 395 { 416 APDU_Cmd c, * cmd;417 c.command=cmd_raw;418 c.length=command_len;419 cmd = &c;420 421 396 BYTE buffer[PROTOCOL_T14_MAX_SHORT_RESPONSE]; 422 long recv , cmd_len;397 long recv; 423 398 int cmd_case; 424 399 BYTE ixor = 0x3E; … … 426 401 BYTE b1 = 0x01; 427 402 int i; 403 long cmd_len = (long) command_len; 428 404 (*rsp) = NULL;//in case of error this is returned 429 405 430 406 /* Parse APDU */ 431 cmd_len = APDU_Cmd_Lc (cmd) + 5; 432 cmd_case = APDU_Cmd_Case (cmd_raw, command_len); 407 cmd_case = APDU_Cmd_Case (cmd_raw, cmd_len); 433 408 for(i=0; i<cmd_len; i++) 434 409 ixor^=cmd_raw[i]; 435 410 436 411 /* Check case of command */ 437 412 if ((cmd_case != APDU_CASE_2S) && (cmd_case != APDU_CASE_3S))
Note:
See TracChangeset
for help on using the changeset viewer.