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

@ -350,6 +350,7 @@ iso7816_process_fci(struct sc_card *card, struct sc_file *file,
{
struct sc_context *ctx = card->ctx;
size_t taglen, len = buflen;
int i;
const unsigned char *tag = NULL, *p = buf;
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]);
}
tag = sc_asn1_find_tag(ctx, p, len, 0x80, &taglen);
if (tag == NULL) {
tag = sc_asn1_find_tag(ctx, p, len, 0x81, &taglen);
}
if (tag != NULL && taglen > 0 && taglen < 3) {
file->size = tag[0];
if (taglen == 2)
file->size = (file->size << 8) + tag[1];
sc_log(ctx, " bytes in file: %d", file->size);
} else {
/* determine the file size */
/* try the tag 0x80 then the tag 0x81 */
file->size = 0;
for (i = 0x80; i <= 0x81; i++) {
int size = 0;
len = buflen;
tag = sc_asn1_find_tag(ctx, p, len, i, &taglen);
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);
break;
}
tag = sc_asn1_find_tag(ctx, p, len, 0x82, &taglen);