jazz_led: Add missing break in switch case
[qemu/kevin.git] / libcacard / card_7816.c
blobbca8c4adf814e204ecfa845ad733fcc06dfac525
1 /*
2 * Implement the 7816 portion of the card spec
4 * This code is licensed under the GNU LGPL, version 2.1 or later.
5 * See the COPYING.LIB file in the top-level directory.
6 */
8 #include "qemu-common.h"
10 #include "vcard.h"
11 #include "vcard_emul.h"
12 #include "card_7816.h"
15 * set the status bytes based on the status word
17 static void
18 vcard_response_set_status(VCardResponse *response, vcard_7816_status_t status)
20 unsigned char sw1, sw2;
21 response->b_status = status; /* make sure the status and swX representations
22 * are consistent */
23 sw1 = (status >> 8) & 0xff;
24 sw2 = status & 0xff;
25 response->b_sw1 = sw1;
26 response->b_sw2 = sw2;
27 response->b_data[response->b_len] = sw1;
28 response->b_data[response->b_len+1] = sw2;
32 * set the status bytes in a response buffer
34 static void
35 vcard_response_set_status_bytes(VCardResponse *response,
36 unsigned char sw1, unsigned char sw2)
38 response->b_status = sw1 << 8 | sw2;
39 response->b_sw1 = sw1;
40 response->b_sw2 = sw2;
41 response->b_data[response->b_len] = sw1;
42 response->b_data[response->b_len+1] = sw2;
46 * allocate a VCardResponse structure, plus space for the data buffer, and
47 * set up everything but the resonse bytes.
49 VCardResponse *
50 vcard_response_new_data(unsigned char *buf, int len)
52 VCardResponse *new_response;
54 new_response = g_new(VCardResponse, 1);
55 new_response->b_data = g_malloc(len + 2);
56 memcpy(new_response->b_data, buf, len);
57 new_response->b_total_len = len+2;
58 new_response->b_len = len;
59 new_response->b_type = VCARD_MALLOC;
60 return new_response;
63 static VCardResponse *
64 vcard_init_buffer_response(VCard *card, unsigned char *buf, int len)
66 VCardResponse *response;
67 VCardBufferResponse *buffer_response;
69 buffer_response = vcard_get_buffer_response(card);
70 if (buffer_response) {
71 vcard_set_buffer_response(card, NULL);
72 vcard_buffer_response_delete(buffer_response);
74 buffer_response = vcard_buffer_response_new(buf, len);
75 if (buffer_response == NULL) {
76 return NULL;
78 response = vcard_response_new_status_bytes(VCARD7816_SW1_RESPONSE_BYTES,
79 len > 255 ? 0 : len);
80 if (response == NULL) {
81 return NULL;
83 vcard_set_buffer_response(card, buffer_response);
84 return response;
88 * general buffer to hold results from APDU calls
90 VCardResponse *
91 vcard_response_new(VCard *card, unsigned char *buf,
92 int len, int Le, vcard_7816_status_t status)
94 VCardResponse *new_response;
96 if (len > Le) {
97 return vcard_init_buffer_response(card, buf, len);
99 new_response = vcard_response_new_data(buf, len);
100 if (new_response == NULL) {
101 return NULL;
103 vcard_response_set_status(new_response, status);
104 return new_response;
108 * general buffer to hold results from APDU calls
110 VCardResponse *
111 vcard_response_new_bytes(VCard *card, unsigned char *buf, int len, int Le,
112 unsigned char sw1, unsigned char sw2)
114 VCardResponse *new_response;
116 if (len > Le) {
117 return vcard_init_buffer_response(card, buf, len);
119 new_response = vcard_response_new_data(buf, len);
120 if (new_response == NULL) {
121 return NULL;
123 vcard_response_set_status_bytes(new_response, sw1, sw2);
124 return new_response;
128 * get a new Response buffer that only has a status.
130 static VCardResponse *
131 vcard_response_new_status(vcard_7816_status_t status)
133 VCardResponse *new_response;
135 new_response = g_new(VCardResponse, 1);
136 new_response->b_data = &new_response->b_sw1;
137 new_response->b_len = 0;
138 new_response->b_total_len = 2;
139 new_response->b_type = VCARD_MALLOC_STRUCT;
140 vcard_response_set_status(new_response, status);
141 return new_response;
145 * same as above, but specify the status as separate bytes
147 VCardResponse *
148 vcard_response_new_status_bytes(unsigned char sw1, unsigned char sw2)
150 VCardResponse *new_response;
152 new_response = g_new(VCardResponse, 1);
153 new_response->b_data = &new_response->b_sw1;
154 new_response->b_len = 0;
155 new_response->b_total_len = 2;
156 new_response->b_type = VCARD_MALLOC_STRUCT;
157 vcard_response_set_status_bytes(new_response, sw1, sw2);
158 return new_response;
163 * free the response buffer. The Buffer has a type to handle the buffer
164 * allocated in other ways than through malloc.
166 void
167 vcard_response_delete(VCardResponse *response)
169 if (response == NULL) {
170 return;
172 switch (response->b_type) {
173 case VCARD_MALLOC:
174 /* everything was malloc'ed */
175 if (response->b_data) {
176 g_free(response->b_data);
178 g_free(response);
179 break;
180 case VCARD_MALLOC_DATA:
181 /* only the data buffer was malloc'ed */
182 if (response->b_data) {
183 g_free(response->b_data);
185 break;
186 case VCARD_MALLOC_STRUCT:
187 /* only the structure was malloc'ed */
188 g_free(response);
189 break;
190 case VCARD_STATIC:
191 break;
196 * decode the class bit and set our generic type field, channel, and
197 * secure messaging values.
199 static vcard_7816_status_t
200 vcard_apdu_set_class(VCardAPDU *apdu) {
201 apdu->a_channel = 0;
202 apdu->a_secure_messaging = 0;
203 apdu->a_type = apdu->a_cla & 0xf0;
204 apdu->a_gen_type = VCARD_7816_ISO;
206 /* parse the class tables 8 & 9 of the 7816-4 Part 4 spec */
207 switch (apdu->a_type) {
208 /* we only support the basic types */
209 case 0x00:
210 case 0x80:
211 case 0x90:
212 case 0xa0:
213 apdu->a_channel = apdu->a_cla & 3;
214 apdu->a_secure_messaging = apdu->a_cla & 0xe;
215 break;
216 case 0xb0:
217 case 0xc0:
218 break;
220 case 0x10:
221 case 0x20:
222 case 0x30:
223 case 0x40:
224 case 0x50:
225 case 0x60:
226 case 0x70:
227 /* Reserved for future use */
228 apdu->a_gen_type = VCARD_7816_RFU;
229 break;
230 case 0xd0:
231 case 0xe0:
232 case 0xf0:
233 default:
234 apdu->a_gen_type =
235 (apdu->a_cla == 0xff) ? VCARD_7816_PTS : VCARD_7816_PROPRIETARY;
236 break;
238 return VCARD7816_STATUS_SUCCESS;
242 * set the Le and Lc fields according to table 5 of the
243 * 7816-4 part 4 spec
245 static vcard_7816_status_t
246 vcard_apdu_set_length(VCardAPDU *apdu)
248 int L, Le;
250 /* process according to table 5 of the 7816-4 Part 4 spec.
251 * variable names match the variables in the spec */
252 L = apdu->a_len-4; /* fixed APDU header */
253 apdu->a_Lc = 0;
254 apdu->a_Le = 0;
255 apdu->a_body = NULL;
256 switch (L) {
257 case 0:
258 /* 1 minimal apdu */
259 return VCARD7816_STATUS_SUCCESS;
260 case 1:
261 /* 2S only return values apdu */
262 /* zero maps to 256 here */
263 apdu->a_Le = apdu->a_header->ah_Le ?
264 apdu->a_header->ah_Le : 256;
265 return VCARD7816_STATUS_SUCCESS;
266 default:
267 /* if the ah_Le byte is zero and we have more than
268 * 1 byte in the header, then we must be using extended Le and Lc.
269 * process the extended now. */
270 if (apdu->a_header->ah_Le == 0) {
271 if (L < 3) {
272 /* coding error, need at least 3 bytes */
273 return VCARD7816_STATUS_ERROR_WRONG_LENGTH;
275 /* calculate the first extended value. Could be either Le or Lc */
276 Le = (apdu->a_header->ah_body[0] << 8)
277 || apdu->a_header->ah_body[1];
278 if (L == 3) {
279 /* 2E extended, return data only */
280 /* zero maps to 65536 */
281 apdu->a_Le = Le ? Le : 65536;
282 return VCARD7816_STATUS_SUCCESS;
284 if (Le == 0) {
285 /* reserved for future use, probably for next time we need
286 * to extend the lengths */
287 return VCARD7816_STATUS_ERROR_WRONG_LENGTH;
289 /* we know that the first extended value is Lc now */
290 apdu->a_Lc = Le;
291 apdu->a_body = &apdu->a_header->ah_body[2];
292 if (L == Le+3) {
293 /* 3E extended, only body parameters */
294 return VCARD7816_STATUS_SUCCESS;
296 if (L == Le+5) {
297 /* 4E extended, parameters and return data */
298 Le = (apdu->a_data[apdu->a_len-2] << 8)
299 || apdu->a_data[apdu->a_len-1];
300 apdu->a_Le = Le ? Le : 65536;
301 return VCARD7816_STATUS_SUCCESS;
303 return VCARD7816_STATUS_ERROR_WRONG_LENGTH;
305 /* not extended */
306 apdu->a_Lc = apdu->a_header->ah_Le;
307 apdu->a_body = &apdu->a_header->ah_body[0];
308 if (L == apdu->a_Lc + 1) {
309 /* 3S only body parameters */
310 return VCARD7816_STATUS_SUCCESS;
312 if (L == apdu->a_Lc + 2) {
313 /* 4S parameters and return data */
314 Le = apdu->a_data[apdu->a_len-1];
315 apdu->a_Le = Le ? Le : 256;
316 return VCARD7816_STATUS_SUCCESS;
318 break;
320 return VCARD7816_STATUS_ERROR_WRONG_LENGTH;
324 * create a new APDU from a raw set of bytes. This will decode all the
325 * above fields. users of VCARDAPDU's can then depend on the already decoded
326 * values.
328 VCardAPDU *
329 vcard_apdu_new(unsigned char *raw_apdu, int len, vcard_7816_status_t *status)
331 VCardAPDU *new_apdu;
333 *status = VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE;
334 if (len < 4) {
335 *status = VCARD7816_STATUS_ERROR_WRONG_LENGTH;
336 return NULL;
339 new_apdu = g_new(VCardAPDU, 1);
340 new_apdu->a_data = g_memdup(raw_apdu, len);
341 new_apdu->a_len = len;
342 *status = vcard_apdu_set_class(new_apdu);
343 if (*status != VCARD7816_STATUS_SUCCESS) {
344 g_free(new_apdu);
345 return NULL;
347 *status = vcard_apdu_set_length(new_apdu);
348 if (*status != VCARD7816_STATUS_SUCCESS) {
349 g_free(new_apdu);
350 new_apdu = NULL;
352 return new_apdu;
355 void
356 vcard_apdu_delete(VCardAPDU *apdu)
358 if (apdu == NULL) {
359 return;
361 if (apdu->a_data) {
362 g_free(apdu->a_data);
364 g_free(apdu);
369 * declare response buffers for all the 7816 defined error codes
371 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_SUCCESS)
372 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_WARNING)
373 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_WARNING_RET_CORUPT)
374 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_WARNING_BUF_END_BEFORE_LE)
375 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_WARNING_INVALID_FILE_SELECTED)
376 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_WARNING_FCI_FORMAT_INVALID)
377 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_WARNING_CHANGE)
378 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_WARNING_FILE_FILLED)
379 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_EXC_ERROR)
380 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_EXC_ERROR_CHANGE)
381 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE)
382 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_WRONG_LENGTH)
383 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_CLA_NOT_SUPPORTED)
384 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_CHANNEL_NOT_SUPPORTED)
385 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_SECURE_NOT_SUPPORTED)
386 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED)
387 VCARD_RESPONSE_NEW_STATIC_STATUS(
388 VCARD7816_STATUS_ERROR_COMMAND_INCOMPATIBLE_WITH_FILE)
389 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_SECURITY_NOT_SATISFIED)
390 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_AUTHENTICATION_BLOCKED)
391 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_DATA_INVALID)
392 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED)
393 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_DATA_NO_EF)
394 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_SM_OBJECT_MISSING)
395 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_SM_OBJECT_INCORRECT)
396 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_WRONG_PARAMETERS)
397 VCARD_RESPONSE_NEW_STATIC_STATUS(
398 VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_IN_DATA)
399 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_FUNCTION_NOT_SUPPORTED)
400 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_FILE_NOT_FOUND)
401 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_RECORD_NOT_FOUND)
402 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_NO_SPACE_FOR_FILE)
403 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_LC_TLV_INCONSISTENT)
404 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_P1_P2_INCORRECT)
405 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_LC_P1_P2_INCONSISTENT)
406 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_DATA_NOT_FOUND)
407 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_2)
408 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_INS_CODE_INVALID)
409 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_CLA_INVALID)
410 VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_GENERAL)
413 * return a single response code. This function cannot fail. It will always
414 * return a response.
416 VCardResponse *
417 vcard_make_response(vcard_7816_status_t status)
419 VCardResponse *response = NULL;
421 switch (status) {
422 /* known 7816 response codes */
423 case VCARD7816_STATUS_SUCCESS:
424 return VCARD_RESPONSE_GET_STATIC(
425 VCARD7816_STATUS_SUCCESS);
426 case VCARD7816_STATUS_WARNING:
427 return VCARD_RESPONSE_GET_STATIC(
428 VCARD7816_STATUS_WARNING);
429 case VCARD7816_STATUS_WARNING_RET_CORUPT:
430 return VCARD_RESPONSE_GET_STATIC(
431 VCARD7816_STATUS_WARNING_RET_CORUPT);
432 case VCARD7816_STATUS_WARNING_BUF_END_BEFORE_LE:
433 return VCARD_RESPONSE_GET_STATIC(
434 VCARD7816_STATUS_WARNING_BUF_END_BEFORE_LE);
435 case VCARD7816_STATUS_WARNING_INVALID_FILE_SELECTED:
436 return VCARD_RESPONSE_GET_STATIC(
437 VCARD7816_STATUS_WARNING_INVALID_FILE_SELECTED);
438 case VCARD7816_STATUS_WARNING_FCI_FORMAT_INVALID:
439 return VCARD_RESPONSE_GET_STATIC(
440 VCARD7816_STATUS_WARNING_FCI_FORMAT_INVALID);
441 case VCARD7816_STATUS_WARNING_CHANGE:
442 return VCARD_RESPONSE_GET_STATIC(
443 VCARD7816_STATUS_WARNING_CHANGE);
444 case VCARD7816_STATUS_WARNING_FILE_FILLED:
445 return VCARD_RESPONSE_GET_STATIC(
446 VCARD7816_STATUS_WARNING_FILE_FILLED);
447 case VCARD7816_STATUS_EXC_ERROR:
448 return VCARD_RESPONSE_GET_STATIC(
449 VCARD7816_STATUS_EXC_ERROR);
450 case VCARD7816_STATUS_EXC_ERROR_CHANGE:
451 return VCARD_RESPONSE_GET_STATIC(
452 VCARD7816_STATUS_EXC_ERROR_CHANGE);
453 case VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE:
454 return VCARD_RESPONSE_GET_STATIC(
455 VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
456 case VCARD7816_STATUS_ERROR_WRONG_LENGTH:
457 return VCARD_RESPONSE_GET_STATIC(
458 VCARD7816_STATUS_ERROR_WRONG_LENGTH);
459 case VCARD7816_STATUS_ERROR_CLA_NOT_SUPPORTED:
460 return VCARD_RESPONSE_GET_STATIC(
461 VCARD7816_STATUS_ERROR_CLA_NOT_SUPPORTED);
462 case VCARD7816_STATUS_ERROR_CHANNEL_NOT_SUPPORTED:
463 return VCARD_RESPONSE_GET_STATIC(
464 VCARD7816_STATUS_ERROR_CHANNEL_NOT_SUPPORTED);
465 case VCARD7816_STATUS_ERROR_SECURE_NOT_SUPPORTED:
466 return VCARD_RESPONSE_GET_STATIC(
467 VCARD7816_STATUS_ERROR_SECURE_NOT_SUPPORTED);
468 case VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED:
469 return VCARD_RESPONSE_GET_STATIC(
470 VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
471 case VCARD7816_STATUS_ERROR_COMMAND_INCOMPATIBLE_WITH_FILE:
472 return VCARD_RESPONSE_GET_STATIC(
473 VCARD7816_STATUS_ERROR_COMMAND_INCOMPATIBLE_WITH_FILE);
474 case VCARD7816_STATUS_ERROR_SECURITY_NOT_SATISFIED:
475 return VCARD_RESPONSE_GET_STATIC(
476 VCARD7816_STATUS_ERROR_SECURITY_NOT_SATISFIED);
477 case VCARD7816_STATUS_ERROR_AUTHENTICATION_BLOCKED:
478 return VCARD_RESPONSE_GET_STATIC(
479 VCARD7816_STATUS_ERROR_AUTHENTICATION_BLOCKED);
480 case VCARD7816_STATUS_ERROR_DATA_INVALID:
481 return VCARD_RESPONSE_GET_STATIC(
482 VCARD7816_STATUS_ERROR_DATA_INVALID);
483 case VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED:
484 return VCARD_RESPONSE_GET_STATIC(
485 VCARD7816_STATUS_ERROR_CONDITION_NOT_SATISFIED);
486 case VCARD7816_STATUS_ERROR_DATA_NO_EF:
487 return VCARD_RESPONSE_GET_STATIC(
488 VCARD7816_STATUS_ERROR_DATA_NO_EF);
489 case VCARD7816_STATUS_ERROR_SM_OBJECT_MISSING:
490 return VCARD_RESPONSE_GET_STATIC(
491 VCARD7816_STATUS_ERROR_SM_OBJECT_MISSING);
492 case VCARD7816_STATUS_ERROR_SM_OBJECT_INCORRECT:
493 return VCARD_RESPONSE_GET_STATIC(
494 VCARD7816_STATUS_ERROR_SM_OBJECT_INCORRECT);
495 case VCARD7816_STATUS_ERROR_WRONG_PARAMETERS:
496 return VCARD_RESPONSE_GET_STATIC(
497 VCARD7816_STATUS_ERROR_WRONG_PARAMETERS);
498 case VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_IN_DATA:
499 return VCARD_RESPONSE_GET_STATIC(
500 VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_IN_DATA);
501 case VCARD7816_STATUS_ERROR_FUNCTION_NOT_SUPPORTED:
502 return VCARD_RESPONSE_GET_STATIC(
503 VCARD7816_STATUS_ERROR_FUNCTION_NOT_SUPPORTED);
504 case VCARD7816_STATUS_ERROR_FILE_NOT_FOUND:
505 return VCARD_RESPONSE_GET_STATIC(
506 VCARD7816_STATUS_ERROR_FILE_NOT_FOUND);
507 case VCARD7816_STATUS_ERROR_RECORD_NOT_FOUND:
508 return VCARD_RESPONSE_GET_STATIC(
509 VCARD7816_STATUS_ERROR_RECORD_NOT_FOUND);
510 case VCARD7816_STATUS_ERROR_NO_SPACE_FOR_FILE:
511 return VCARD_RESPONSE_GET_STATIC(
512 VCARD7816_STATUS_ERROR_NO_SPACE_FOR_FILE);
513 case VCARD7816_STATUS_ERROR_LC_TLV_INCONSISTENT:
514 return VCARD_RESPONSE_GET_STATIC(
515 VCARD7816_STATUS_ERROR_LC_TLV_INCONSISTENT);
516 case VCARD7816_STATUS_ERROR_P1_P2_INCORRECT:
517 return VCARD_RESPONSE_GET_STATIC(
518 VCARD7816_STATUS_ERROR_P1_P2_INCORRECT);
519 case VCARD7816_STATUS_ERROR_LC_P1_P2_INCONSISTENT:
520 return VCARD_RESPONSE_GET_STATIC(
521 VCARD7816_STATUS_ERROR_LC_P1_P2_INCONSISTENT);
522 case VCARD7816_STATUS_ERROR_DATA_NOT_FOUND:
523 return VCARD_RESPONSE_GET_STATIC(
524 VCARD7816_STATUS_ERROR_DATA_NOT_FOUND);
525 case VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_2:
526 return VCARD_RESPONSE_GET_STATIC(
527 VCARD7816_STATUS_ERROR_WRONG_PARAMETERS_2);
528 case VCARD7816_STATUS_ERROR_INS_CODE_INVALID:
529 return VCARD_RESPONSE_GET_STATIC(
530 VCARD7816_STATUS_ERROR_INS_CODE_INVALID);
531 case VCARD7816_STATUS_ERROR_CLA_INVALID:
532 return VCARD_RESPONSE_GET_STATIC(
533 VCARD7816_STATUS_ERROR_CLA_INVALID);
534 case VCARD7816_STATUS_ERROR_GENERAL:
535 return VCARD_RESPONSE_GET_STATIC(
536 VCARD7816_STATUS_ERROR_GENERAL);
537 default:
538 /* we don't know this status code, create a response buffer to
539 * hold it */
540 response = vcard_response_new_status(status);
541 if (response == NULL) {
542 /* couldn't allocate the buffer, return memmory error */
543 return VCARD_RESPONSE_GET_STATIC(
544 VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
547 assert(response);
548 return response;
552 * Add File card support here if you need it.
554 static VCardStatus
555 vcard7816_file_system_process_apdu(VCard *card, VCardAPDU *apdu,
556 VCardResponse **response)
558 /* TODO: if we want to support a virtual file system card, we do it here.
559 * It would probably be a pkcs #15 card type */
560 *response = vcard_make_response(
561 VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
562 return VCARD_DONE;
566 * VM card (including java cards)
568 static VCardStatus
569 vcard7816_vm_process_apdu(VCard *card, VCardAPDU *apdu,
570 VCardResponse **response)
572 int bytes_to_copy, next_byte_count, count;
573 VCardApplet *current_applet;
574 VCardBufferResponse *buffer_response;
575 vcard_7816_status_t status;
577 /* parse the class first */
578 if (apdu->a_gen_type != VCARD_7816_ISO) {
579 *response = vcard_make_response(
580 VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
581 return VCARD_DONE;
584 /* use a switch so that if we need to support secure channel stuff later,
585 * we know where to put it */
586 switch (apdu->a_secure_messaging) {
587 case 0x0: /* no SM */
588 break;
589 case 0x4: /* proprietary SM */
590 case 0x8: /* header not authenticated */
591 case 0xc: /* header authenticated */
592 default:
593 /* for now, don't try to support secure channel stuff in the
594 * virtual card. */
595 *response = vcard_make_response(
596 VCARD7816_STATUS_ERROR_SECURE_NOT_SUPPORTED);
597 return VCARD_DONE;
600 /* now parse the instruction */
601 switch (apdu->a_ins) {
602 case VCARD7816_INS_MANAGE_CHANNEL: /* secure channel op */
603 case VCARD7816_INS_EXTERNAL_AUTHENTICATE: /* secure channel op */
604 case VCARD7816_INS_GET_CHALLENGE: /* secure channel op */
605 case VCARD7816_INS_INTERNAL_AUTHENTICATE: /* secure channel op */
606 case VCARD7816_INS_ERASE_BINARY: /* applet control op */
607 case VCARD7816_INS_READ_BINARY: /* applet control op */
608 case VCARD7816_INS_WRITE_BINARY: /* applet control op */
609 case VCARD7816_INS_UPDATE_BINARY: /* applet control op */
610 case VCARD7816_INS_READ_RECORD: /* file op */
611 case VCARD7816_INS_WRITE_RECORD: /* file op */
612 case VCARD7816_INS_UPDATE_RECORD: /* file op */
613 case VCARD7816_INS_APPEND_RECORD: /* file op */
614 case VCARD7816_INS_ENVELOPE:
615 case VCARD7816_INS_PUT_DATA:
616 *response = vcard_make_response(
617 VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
618 break;
620 case VCARD7816_INS_SELECT_FILE:
621 if (apdu->a_p1 != 0x04) {
622 *response = vcard_make_response(
623 VCARD7816_STATUS_ERROR_FUNCTION_NOT_SUPPORTED);
624 break;
627 /* side effect, deselect the current applet if no applet has been found
628 * */
629 current_applet = vcard_find_applet(card, apdu->a_body, apdu->a_Lc);
630 vcard_select_applet(card, apdu->a_channel, current_applet);
631 if (current_applet) {
632 unsigned char *aid;
633 int aid_len;
634 aid = vcard_applet_get_aid(current_applet, &aid_len);
635 *response = vcard_response_new(card, aid, aid_len, apdu->a_Le,
636 VCARD7816_STATUS_SUCCESS);
637 } else {
638 *response = vcard_make_response(
639 VCARD7816_STATUS_ERROR_FILE_NOT_FOUND);
641 break;
643 case VCARD7816_INS_VERIFY:
644 if ((apdu->a_p1 != 0x00) || (apdu->a_p2 != 0x00)) {
645 *response = vcard_make_response(
646 VCARD7816_STATUS_ERROR_WRONG_PARAMETERS);
647 } else {
648 if (apdu->a_Lc == 0) {
649 /* handle pin count if possible */
650 count = vcard_emul_get_login_count(card);
651 if (count < 0) {
652 *response = vcard_make_response(
653 VCARD7816_STATUS_ERROR_DATA_NOT_FOUND);
654 } else {
655 if (count > 0xf) {
656 count = 0xf;
658 *response = vcard_response_new_status_bytes(
659 VCARD7816_SW1_WARNING_CHANGE,
660 0xc0 | count);
661 if (*response == NULL) {
662 *response = vcard_make_response(
663 VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
666 } else {
667 status = vcard_emul_login(card, apdu->a_body, apdu->a_Lc);
668 *response = vcard_make_response(status);
671 break;
673 case VCARD7816_INS_GET_RESPONSE:
674 buffer_response = vcard_get_buffer_response(card);
675 if (!buffer_response) {
676 *response = vcard_make_response(
677 VCARD7816_STATUS_ERROR_DATA_NOT_FOUND);
678 /* handle error */
679 break;
681 bytes_to_copy = MIN(buffer_response->len, apdu->a_Le);
682 next_byte_count = MIN(256, buffer_response->len - bytes_to_copy);
683 *response = vcard_response_new_bytes(
684 card, buffer_response->current, bytes_to_copy,
685 apdu->a_Le,
686 next_byte_count ?
687 VCARD7816_SW1_RESPONSE_BYTES : VCARD7816_SW1_SUCCESS,
688 next_byte_count);
689 buffer_response->current += bytes_to_copy;
690 buffer_response->len -= bytes_to_copy;
691 if (*response == NULL || (next_byte_count == 0)) {
692 vcard_set_buffer_response(card, NULL);
693 vcard_buffer_response_delete(buffer_response);
695 if (*response == NULL) {
696 *response =
697 vcard_make_response(VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE);
699 break;
701 case VCARD7816_INS_GET_DATA:
702 *response =
703 vcard_make_response(VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
704 break;
706 default:
707 *response =
708 vcard_make_response(VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
709 break;
712 /* response should have been set somewhere */
713 assert(*response != NULL);
714 return VCARD_DONE;
719 * APDU processing starts here. This routes the card processing stuff to the
720 * right location.
722 VCardStatus
723 vcard_process_apdu(VCard *card, VCardAPDU *apdu, VCardResponse **response)
725 VCardStatus status;
726 VCardBufferResponse *buffer_response;
728 /* first handle any PTS commands, which aren't really APDU's */
729 if (apdu->a_type == VCARD_7816_PTS) {
730 /* the PTS responses aren't really responses either */
731 *response = vcard_response_new_data(apdu->a_data, apdu->a_len);
732 /* PTS responses have no status bytes */
733 (*response)->b_total_len = (*response)->b_len;
734 return VCARD_DONE;
736 buffer_response = vcard_get_buffer_response(card);
737 if (buffer_response && apdu->a_ins != VCARD7816_INS_GET_RESPONSE) {
738 /* clear out buffer_response, return an error */
739 vcard_set_buffer_response(card, NULL);
740 vcard_buffer_response_delete(buffer_response);
741 *response = vcard_make_response(VCARD7816_STATUS_EXC_ERROR);
742 return VCARD_DONE;
745 status = vcard_process_applet_apdu(card, apdu, response);
746 if (status != VCARD_NEXT) {
747 return status;
749 switch (vcard_get_type(card)) {
750 case VCARD_FILE_SYSTEM:
751 return vcard7816_file_system_process_apdu(card, apdu, response);
752 case VCARD_VM:
753 return vcard7816_vm_process_apdu(card, apdu, response);
754 case VCARD_DIRECT:
755 /* if we are type direct, then the applet should handle everything */
756 assert(!"VCARD_DIRECT: applet failure");
757 break;
759 *response =
760 vcard_make_response(VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED);
761 return VCARD_DONE;