iso7816.c: allow file length stored in more than 2 bytes

as indicated in iso7816-4 chapter 7.4.3 table 10

Fixes #459
This commit is contained in:
vletoux 2015-05-10 22:54:56 +02:00 committed by Viktor Tarasov
parent 8b62221abc
commit 492ffe0fd7
1 changed files with 21 additions and 12 deletions

View File

@ -31,7 +31,7 @@
static void fixup_transceive_length(const struct sc_card *card, static void fixup_transceive_length(const struct sc_card *card,
struct sc_apdu *apdu) struct sc_apdu *apdu)
{ {
size_t max_send_size; size_t max_send_size;
size_t max_recv_size; size_t max_recv_size;
@ -105,7 +105,7 @@ static const struct sc_card_error iso7816_errors[] = {
{ 0x6A87, SC_ERROR_INCORRECT_PARAMETERS,"Lc inconsistent with P1-P2" }, { 0x6A87, SC_ERROR_INCORRECT_PARAMETERS,"Lc inconsistent with P1-P2" },
{ 0x6A88, SC_ERROR_DATA_OBJECT_NOT_FOUND,"Referenced data not found" }, { 0x6A88, SC_ERROR_DATA_OBJECT_NOT_FOUND,"Referenced data not found" },
{ 0x6A89, SC_ERROR_FILE_ALREADY_EXISTS, "File already exists"}, { 0x6A89, SC_ERROR_FILE_ALREADY_EXISTS, "File already exists"},
{ 0x6A8A, SC_ERROR_FILE_ALREADY_EXISTS, "DF name already exists"}, { 0x6A8A, SC_ERROR_FILE_ALREADY_EXISTS, "DF name already exists"},
{ 0x6B00, SC_ERROR_INCORRECT_PARAMETERS,"Wrong parameter(s) P1-P2" }, { 0x6B00, SC_ERROR_INCORRECT_PARAMETERS,"Wrong parameter(s) P1-P2" },
{ 0x6D00, SC_ERROR_INS_NOT_SUPPORTED, "Instruction code not supported or invalid" }, { 0x6D00, SC_ERROR_INS_NOT_SUPPORTED, "Instruction code not supported or invalid" },
@ -350,6 +350,7 @@ iso7816_process_fci(struct sc_card *card, struct sc_file *file,
{ {
struct sc_context *ctx = card->ctx; struct sc_context *ctx = card->ctx;
size_t taglen, len = buflen; size_t taglen, len = buflen;
int i;
const unsigned char *tag = NULL, *p = buf; const unsigned char *tag = NULL, *p = buf;
sc_log(ctx, "processing FCI bytes"); sc_log(ctx, "processing FCI bytes");
@ -359,17 +360,25 @@ iso7816_process_fci(struct sc_card *card, struct sc_file *file,
sc_log(ctx, " file identifier: 0x%02X%02X", tag[0], tag[1]); sc_log(ctx, " file identifier: 0x%02X%02X", tag[0], tag[1]);
} }
tag = sc_asn1_find_tag(ctx, p, len, 0x80, &taglen); /* determine the file size */
if (tag == NULL) { /* try the tag 0x80 then the tag 0x81 */
tag = sc_asn1_find_tag(ctx, p, len, 0x81, &taglen); file->size = 0;
} for (i = 0x80; i <= 0x81; i++) {
if (tag != NULL && taglen > 0 && taglen < 3) { int size = 0;
file->size = tag[0]; len = buflen;
if (taglen == 2) tag = sc_asn1_find_tag(ctx, p, len, i, &taglen);
file->size = (file->size << 8) + tag[1]; if (tag == NULL)
continue;
if (taglen == 0)
continue;
if (sc_asn1_decode_integer(tag, taglen, &size) < 0)
continue;
if (size <0)
continue;
file->size = size;
sc_log(ctx, " bytes in file: %d", file->size); sc_log(ctx, " bytes in file: %d", file->size);
} else { break;
file->size = 0;
} }
tag = sc_asn1_find_tag(ctx, p, len, 0x82, &taglen); tag = sc_asn1_find_tag(ctx, p, len, 0x82, &taglen);