sc_pkcs15_read{,_cached}_file now honor the index/length parameters from Path
git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@732 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
parent
fcfbeb1845
commit
d3c6bfd10f
|
@ -67,9 +67,9 @@ int sc_pkcs15_read_cached_file(struct sc_pkcs15_card *p15card,
|
||||||
u8 **buf, size_t *bufsize)
|
u8 **buf, size_t *bufsize)
|
||||||
{
|
{
|
||||||
char fname[160];
|
char fname[160];
|
||||||
int r;
|
int r, got;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
size_t c;
|
size_t count, offset;
|
||||||
struct stat stbuf;
|
struct stat stbuf;
|
||||||
u8 *data = NULL;
|
u8 *data = NULL;
|
||||||
|
|
||||||
|
@ -79,13 +79,22 @@ int sc_pkcs15_read_cached_file(struct sc_pkcs15_card *p15card,
|
||||||
r = stat(fname, &stbuf);
|
r = stat(fname, &stbuf);
|
||||||
if (r)
|
if (r)
|
||||||
return SC_ERROR_FILE_NOT_FOUND;
|
return SC_ERROR_FILE_NOT_FOUND;
|
||||||
c = stbuf.st_size;
|
if (path->count < 0) {
|
||||||
|
count = stbuf.st_size;
|
||||||
|
offset = 0;
|
||||||
|
} else {
|
||||||
|
count = path->count;
|
||||||
|
offset = path->index;
|
||||||
|
if (offset >= stbuf.st_size
|
||||||
|
|| offset + count >= stbuf.st_size)
|
||||||
|
return SC_ERROR_FILE_NOT_FOUND; /* cache file bad? */
|
||||||
|
}
|
||||||
if (*buf == NULL) {
|
if (*buf == NULL) {
|
||||||
data = (u8 *) malloc(stbuf.st_size);
|
data = (u8 *) malloc(stbuf.st_size);
|
||||||
if (data == NULL)
|
if (data == NULL)
|
||||||
return SC_ERROR_OUT_OF_MEMORY;
|
return SC_ERROR_OUT_OF_MEMORY;
|
||||||
} else
|
} else
|
||||||
if (c > *bufsize)
|
if (count > *bufsize)
|
||||||
return SC_ERROR_BUFFER_TOO_SMALL;
|
return SC_ERROR_BUFFER_TOO_SMALL;
|
||||||
f = fopen(fname, "r");
|
f = fopen(fname, "r");
|
||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
|
@ -93,16 +102,18 @@ int sc_pkcs15_read_cached_file(struct sc_pkcs15_card *p15card,
|
||||||
free(data);
|
free(data);
|
||||||
return SC_ERROR_FILE_NOT_FOUND;
|
return SC_ERROR_FILE_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
if (offset)
|
||||||
|
fseek(f, offset, SEEK_SET);
|
||||||
if (data)
|
if (data)
|
||||||
*buf = data;
|
*buf = data;
|
||||||
c = fread(*buf, 1, c, f);
|
got = fread(*buf, 1, count, f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
if (c != stbuf.st_size) {
|
if (got != count) {
|
||||||
if (data)
|
if (data)
|
||||||
free(data);
|
free(data);
|
||||||
return SC_ERROR_BUFFER_TOO_SMALL;
|
return SC_ERROR_BUFFER_TOO_SMALL;
|
||||||
}
|
}
|
||||||
*bufsize = c;
|
*bufsize = count;
|
||||||
if (data)
|
if (data)
|
||||||
*buf = data;
|
*buf = data;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -948,11 +948,12 @@ int sc_pkcs15_read_file(struct sc_pkcs15_card *p15card,
|
||||||
{
|
{
|
||||||
struct sc_file *file;
|
struct sc_file *file;
|
||||||
u8 *data = NULL;
|
u8 *data = NULL;
|
||||||
size_t len = 0;
|
size_t len = 0, offset = 0;
|
||||||
int r = -1;
|
int r = -1;
|
||||||
|
|
||||||
assert(p15card != NULL && path != NULL && buf != NULL);
|
assert(p15card != NULL && path != NULL && buf != NULL);
|
||||||
SC_FUNC_CALLED(p15card->card->ctx, 1);
|
SC_FUNC_CALLED(p15card->card->ctx, 1);
|
||||||
|
|
||||||
if (p15card->opts.use_cache) {
|
if (p15card->opts.use_cache) {
|
||||||
r = sc_pkcs15_read_cached_file(p15card, path, &data, &len);
|
r = sc_pkcs15_read_cached_file(p15card, path, &data, &len);
|
||||||
}
|
}
|
||||||
|
@ -964,7 +965,21 @@ int sc_pkcs15_read_file(struct sc_pkcs15_card *p15card,
|
||||||
sc_unlock(p15card->card);
|
sc_unlock(p15card->card);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
len = file->size;
|
/* Handle the case where the ASN.1 Path object specified
|
||||||
|
* index and length values */
|
||||||
|
if (path->count < 0) {
|
||||||
|
len = file->size;
|
||||||
|
offset = 0;
|
||||||
|
} else {
|
||||||
|
offset = path->index;
|
||||||
|
len = path->count;
|
||||||
|
/* Make sure we're within proper bounds */
|
||||||
|
if (offset >= file->size
|
||||||
|
|| offset + len >= file->size) {
|
||||||
|
sc_unlock(p15card->card);
|
||||||
|
return SC_ERROR_INVALID_ASN1_OBJECT;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (file_out != NULL)
|
if (file_out != NULL)
|
||||||
*file_out = file;
|
*file_out = file;
|
||||||
else
|
else
|
||||||
|
@ -974,7 +989,7 @@ int sc_pkcs15_read_file(struct sc_pkcs15_card *p15card,
|
||||||
sc_unlock(p15card->card);
|
sc_unlock(p15card->card);
|
||||||
return SC_ERROR_OUT_OF_MEMORY;
|
return SC_ERROR_OUT_OF_MEMORY;
|
||||||
}
|
}
|
||||||
r = sc_read_binary(p15card->card, 0, data, len, 0);
|
r = sc_read_binary(p15card->card, offset, data, len, 0);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
sc_unlock(p15card->card);
|
sc_unlock(p15card->card);
|
||||||
free(data);
|
free(data);
|
||||||
|
|
Loading…
Reference in New Issue