don't expect the card to specify the file length in generic tools

if sc_file_t.size == 0 we try to read 1024 bytes by default.
This commit is contained in:
Frank Morgner 2013-09-18 18:13:15 +02:00 committed by Viktor Tarasov
parent c023d20e14
commit 3d0064e983
5 changed files with 44 additions and 23 deletions

View File

@ -132,6 +132,7 @@ int sc_parse_ef_atr(struct sc_card *card)
struct sc_file *file;
int rv;
unsigned char *buf = NULL;
size_t size;
LOG_FUNC_CALLED(ctx);
@ -139,13 +140,18 @@ int sc_parse_ef_atr(struct sc_card *card)
rv = sc_select_file(card, &path, &file);
LOG_TEST_RET(ctx, rv, "Cannot select EF(ATR) file");
buf = malloc(file->size);
if (file->size) {
size = file->size;
} else {
size = 1024;
}
buf = malloc(size);
if (!buf)
LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Memory allocation error");
rv = sc_read_binary(card, 0, buf, file->size, 0);
rv = sc_read_binary(card, 0, buf, size, 0);
LOG_TEST_RET(ctx, rv, "Cannot read EF(ATR) file");
rv = sc_parse_ef_atr_content(card, buf, file->size);
rv = sc_parse_ef_atr_content(card, buf, rv);
LOG_TEST_RET(ctx, rv, "EF(ATR) parse error");
free(buf);

View File

@ -339,20 +339,16 @@ iso7816_process_fci(struct sc_card *card, struct sc_file *file,
}
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);
}
if (tag == NULL) {
tag = sc_asn1_find_tag(ctx, p, len, 0x81, &taglen);
if (tag != NULL && taglen >= 2) {
int bytes = (tag[0] << 8) + tag[1];
sc_log(ctx, " bytes in file: %d", bytes);
file->size = bytes;
}
} else {
file->size = 0;
}
tag = sc_asn1_find_tag(ctx, p, len, 0x82, &taglen);

View File

@ -545,6 +545,7 @@ sc_pkcs15_get_lastupdate(struct sc_pkcs15_card *p15card)
unsigned char *content, last_update[32];
size_t lupdate_len = sizeof(last_update) - 1;
int r, content_len;
size_t size;
if (p15card->tokeninfo->last_update.gtime)
goto done;
@ -556,11 +557,16 @@ sc_pkcs15_get_lastupdate(struct sc_pkcs15_card *p15card)
if (r < 0)
return NULL;
content = calloc(file->size, 1);
if (file->size) {
size = 1024;
} else {
size = file->size;
}
content = calloc(size, 1);
if (!content)
return NULL;
r = sc_read_binary(p15card->card, 0, content, file->size, 0);
r = sc_read_binary(p15card->card, 0, content, size, 0);
if (r < 0)
return NULL;
content_len = r;
@ -2212,7 +2218,11 @@ int sc_pkcs15_read_file(struct sc_pkcs15_card *p15card,
/* Handle the case where the ASN.1 Path object specified
* index and length values */
if (in_path->count < 0) {
len = file->size;
if (file->size) {
len = file->size;
} else {
len = 1024;
}
offset = 0;
}
else {

View File

@ -639,20 +639,22 @@ static int read_and_util_print_binary_file(sc_file_t *file)
{
unsigned char *buf = NULL;
int r;
size_t size;
buf = malloc(file->size);
if (file->size) {
size = file->size;
} else {
size = 1024;
}
buf = malloc(size);
if (!buf)
return -1;
r = sc_read_binary(card, 0, buf, file->size, 0);
r = sc_read_binary(card, 0, buf, size, 0);
if (r < 0) {
check_ret(r, SC_AC_OP_READ, "read failed", file);
return -1;
}
if ((r != file->size) && (card->type != SC_CARD_TYPE_BELPIC_EID)) {
printf("expecting %d, got only %d bytes.\n", file->size, r);
return -1;
}
if ((r == 0) && (card->type == SC_CARD_TYPE_BELPIC_EID))
return -1;
@ -1200,6 +1202,7 @@ static int do_get(int argc, char **argv)
}
count = file->size;
while (count) {
/* FIXME sc_read_binary does this kind of fetching in a loop already */
int c = count > sizeof(buf) ? sizeof(buf) : count;
r = sc_read_binary(card, idx, buf, c, 0);

View File

@ -1471,6 +1471,7 @@ static int read_and_cache_file(const sc_path_t *path)
const sc_acl_entry_t *e;
u8 *buf;
int r;
size_t size;
if (verbose) {
printf("Reading file ");
@ -1488,12 +1489,17 @@ static int read_and_cache_file(const sc_path_t *path)
printf("Skipping; ACL for read operation is not NONE.\n");
return -1;
}
buf = malloc(tfile->size);
if (tfile->size) {
size = 1024;
} else {
size = tfile->size;
}
buf = malloc(size);
if (!buf) {
printf("out of memory!");
return -1;
}
r = sc_read_binary(card, 0, buf, tfile->size, 0);
r = sc_read_binary(card, 0, buf, size, 0);
if (r < 0) {
fprintf(stderr, "sc_read_binary() failed: %s\n", sc_strerror(r));
free(buf);