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.
8 #include "qemu-common.h"
11 #include "vcard_emul.h"
12 #include "card_7816.h"
15 * set the status bytes based on the status word
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
23 sw1
= (status
>> 8) & 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
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.
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
;
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
) {
78 response
= vcard_response_new_status_bytes(VCARD7816_SW1_RESPONSE_BYTES
,
80 if (response
== NULL
) {
83 vcard_set_buffer_response(card
, buffer_response
);
88 * general buffer to hold results from APDU calls
91 vcard_response_new(VCard
*card
, unsigned char *buf
,
92 int len
, int Le
, vcard_7816_status_t status
)
94 VCardResponse
*new_response
;
97 return vcard_init_buffer_response(card
, buf
, len
);
99 new_response
= vcard_response_new_data(buf
, len
);
100 if (new_response
== NULL
) {
103 vcard_response_set_status(new_response
, status
);
108 * general buffer to hold results from APDU calls
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
;
117 return vcard_init_buffer_response(card
, buf
, len
);
119 new_response
= vcard_response_new_data(buf
, len
);
120 if (new_response
== NULL
) {
123 vcard_response_set_status_bytes(new_response
, sw1
, sw2
);
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
);
145 * same as above, but specify the status as separate bytes
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
);
163 * free the response buffer. The Buffer has a type to handle the buffer
164 * allocated in other ways than through malloc.
167 vcard_response_delete(VCardResponse
*response
)
169 if (response
== NULL
) {
172 switch (response
->b_type
) {
174 /* everything was malloc'ed */
175 if (response
->b_data
) {
176 g_free(response
->b_data
);
180 case VCARD_MALLOC_DATA
:
181 /* only the data buffer was malloc'ed */
182 if (response
->b_data
) {
183 g_free(response
->b_data
);
186 case VCARD_MALLOC_STRUCT
:
187 /* only the structure was malloc'ed */
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
) {
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 */
213 apdu
->a_channel
= apdu
->a_cla
& 3;
214 apdu
->a_secure_messaging
= apdu
->a_cla
& 0xe;
227 /* Reserved for future use */
228 apdu
->a_gen_type
= VCARD_7816_RFU
;
235 (apdu
->a_cla
== 0xff) ? VCARD_7816_PTS
: VCARD_7816_PROPRIETARY
;
238 return VCARD7816_STATUS_SUCCESS
;
242 * set the Le and Lc fields according to table 5 of the
245 static vcard_7816_status_t
246 vcard_apdu_set_length(VCardAPDU
*apdu
)
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 */
259 return VCARD7816_STATUS_SUCCESS
;
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
;
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) {
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];
279 /* 2E extended, return data only */
280 /* zero maps to 65536 */
281 apdu
->a_Le
= Le
? Le
: 65536;
282 return VCARD7816_STATUS_SUCCESS
;
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 */
291 apdu
->a_body
= &apdu
->a_header
->ah_body
[2];
293 /* 3E extended, only body parameters */
294 return VCARD7816_STATUS_SUCCESS
;
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
;
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
;
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
329 vcard_apdu_new(unsigned char *raw_apdu
, int len
, vcard_7816_status_t
*status
)
333 *status
= VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE
;
335 *status
= VCARD7816_STATUS_ERROR_WRONG_LENGTH
;
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
) {
347 *status
= vcard_apdu_set_length(new_apdu
);
348 if (*status
!= VCARD7816_STATUS_SUCCESS
) {
356 vcard_apdu_delete(VCardAPDU
*apdu
)
362 g_free(apdu
->a_data
);
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
417 vcard_make_response(vcard_7816_status_t status
)
419 VCardResponse
*response
= NULL
;
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
);
538 /* we don't know this status code, create a response buffer to
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
);
552 * Add File card support here if you need it.
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
);
566 * VM card (including java cards)
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
);
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 */
589 case 0x4: /* proprietary SM */
590 case 0x8: /* header not authenticated */
591 case 0xc: /* header authenticated */
593 /* for now, don't try to support secure channel stuff in the
595 *response
= vcard_make_response(
596 VCARD7816_STATUS_ERROR_SECURE_NOT_SUPPORTED
);
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
);
620 case VCARD7816_INS_SELECT_FILE
:
621 if (apdu
->a_p1
!= 0x04) {
622 *response
= vcard_make_response(
623 VCARD7816_STATUS_ERROR_FUNCTION_NOT_SUPPORTED
);
627 /* side effect, deselect the current applet if no applet has been found
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
) {
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
);
638 *response
= vcard_make_response(
639 VCARD7816_STATUS_ERROR_FILE_NOT_FOUND
);
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
);
648 if (apdu
->a_Lc
== 0) {
649 /* handle pin count if possible */
650 count
= vcard_emul_get_login_count(card
);
652 *response
= vcard_make_response(
653 VCARD7816_STATUS_ERROR_DATA_NOT_FOUND
);
658 *response
= vcard_response_new_status_bytes(
659 VCARD7816_SW1_WARNING_CHANGE
,
661 if (*response
== NULL
) {
662 *response
= vcard_make_response(
663 VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE
);
667 status
= vcard_emul_login(card
, apdu
->a_body
, apdu
->a_Lc
);
668 *response
= vcard_make_response(status
);
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
);
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
,
687 VCARD7816_SW1_RESPONSE_BYTES
: VCARD7816_SW1_SUCCESS
,
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
) {
697 vcard_make_response(VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE
);
701 case VCARD7816_INS_GET_DATA
:
703 vcard_make_response(VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED
);
708 vcard_make_response(VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED
);
712 /* response should have been set somewhere */
713 assert(*response
!= NULL
);
719 * APDU processing starts here. This routes the card processing stuff to the
723 vcard_process_apdu(VCard
*card
, VCardAPDU
*apdu
, VCardResponse
**response
)
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
;
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
);
745 status
= vcard_process_applet_apdu(card
, apdu
, response
);
746 if (status
!= VCARD_NEXT
) {
749 switch (vcard_get_type(card
)) {
750 case VCARD_FILE_SYSTEM
:
751 return vcard7816_file_system_process_apdu(card
, apdu
, response
);
753 return vcard7816_vm_process_apdu(card
, apdu
, response
);
755 /* if we are type direct, then the applet should handle everything */
756 assert(!"VCARD_DIRECT: applet failure");
760 vcard_make_response(VCARD7816_STATUS_ERROR_COMMAND_NOT_SUPPORTED
);