opensc/src/libopensc/pkcs15-cache.c

224 lines
5.4 KiB
C
Raw Normal View History

/*
* pkcs15-cache.c: PKCS #15 file caching functions
*
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2015-04-22 21:55:33 +00:00
#if HAVE_CONFIG_H
#include "config.h"
2015-04-22 21:55:33 +00:00
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/stat.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include "internal.h"
#include "pkcs15.h"
2019-05-28 15:46:44 +00:00
#include "common/compat_strlcpy.h"
#define RANDOM_UID_INDICATOR 0x08
static int generate_cache_filename(struct sc_pkcs15_card *p15card,
const sc_path_t *path,
char *buf, size_t bufsize)
{
char dir[PATH_MAX];
char *last_update = NULL;
int r;
unsigned u;
if (p15card->tokeninfo->serial_number == NULL
&& (p15card->card->uid.len == 0
|| p15card->card->uid.value[0] == RANDOM_UID_INDICATOR))
return SC_ERROR_INVALID_ARGUMENTS;
assert(path->len <= SC_MAX_PATH_SIZE);
r = sc_get_cache_dir(p15card->card->ctx, dir, sizeof(dir));
if (r)
return r;
snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "/");
last_update = sc_pkcs15_get_lastupdate(p15card);
if (!last_update)
last_update = "NODATE";
if (p15card->tokeninfo->serial_number) {
snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir),
"%s_%s", p15card->tokeninfo->serial_number,
last_update);
} else {
snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir),
"uid-%s_%s", sc_dump_hex(
p15card->card->uid.value,
p15card->card->uid.len), last_update);
}
if (path->aid.len &&
(path->type == SC_PATH_TYPE_FILE_ID || path->type == SC_PATH_TYPE_PATH)) {
snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "_");
for (u = 0; u < path->aid.len; u++)
snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir),
"%02X", path->aid.value[u]);
}
else if (path->type != SC_PATH_TYPE_PATH) {
return SC_ERROR_INVALID_ARGUMENTS;
}
if (path->len) {
size_t offs = 0;
if (path->len > 2 && memcmp(path->value, "\x3F\x00", 2) == 0)
offs = 2;
snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir), "_");
for (u = 0; u < path->len - offs; u++)
snprintf(dir + strlen(dir), sizeof(dir) - strlen(dir),
"%02X", path->value[u + offs]);
}
2019-05-28 15:46:44 +00:00
if (!buf)
return SC_ERROR_BUFFER_TOO_SMALL;
2019-05-28 15:46:44 +00:00
strlcpy(buf, dir, bufsize);
return SC_SUCCESS;
}
int sc_pkcs15_read_cached_file(struct sc_pkcs15_card *p15card,
const sc_path_t *path,
u8 **buf, size_t *bufsize)
{
char fname[PATH_MAX];
int rv;
FILE *f;
size_t count;
struct stat stbuf;
u8 *data = NULL;
if (path->len < 2)
return SC_ERROR_INVALID_ARGUMENTS;
/* Accept full path or FILE-ID path with AID */
if ((path->type != SC_PATH_TYPE_PATH) && (path->type != SC_PATH_TYPE_FILE_ID || path->aid.len == 0))
return SC_ERROR_INVALID_ARGUMENTS;
sc_log(p15card->card->ctx, "try to read cache for %s", sc_print_path(path));
rv = generate_cache_filename(p15card, path, fname, sizeof(fname));
if (rv != SC_SUCCESS)
return rv;
sc_log(p15card->card->ctx, "read cached file %s", fname);
f = fopen(fname, "rb");
if (!f)
return SC_ERROR_FILE_NOT_FOUND;
if (fstat(fileno(f), &stbuf)) {
fclose(f);
return SC_ERROR_FILE_NOT_FOUND;
}
if (path->count < 0) {
count = stbuf.st_size;
}
else {
count = path->count;
if (path->index + count > (size_t)stbuf.st_size) {
rv = SC_ERROR_FILE_NOT_FOUND; /* cache file bad? */
goto err;
}
if (0 != fseek(f, (long)path->index, SEEK_SET)) {
rv = SC_ERROR_FILE_NOT_FOUND;
goto err;
}
}
if (*buf == NULL) {
Do not cast the return value of malloc(3) and calloc(3) From http://en.wikipedia.org/wiki/Malloc#Casting_and_type_safety " Casting and type safety malloc returns a void pointer (void *), which indicates that it is a pointer to a region of unknown data type. One may "cast" (see type conversion) this pointer to a specific type, as in int *ptr = (int*)malloc(10 * sizeof (int)); When using C, this is considered bad practice; it is redundant under the C standard. Moreover, putting in a cast may mask failure to include the header stdlib.h, in which the prototype for malloc is found. In the absence of a prototype for malloc, the C compiler will assume that malloc returns an int, and will issue a warning in a context such as the above, provided the error is not masked by a cast. On certain architectures and data models (such as LP64 on 64 bit systems, where long and pointers are 64 bit and int is 32 bit), this error can actually result in undefined behavior, as the implicitly declared malloc returns a 32 bit value whereas the actually defined function returns a 64 bit value. Depending on calling conventions and memory layout, this may result in stack smashing. The returned pointer need not be explicitly cast to a more specific pointer type, since ANSI C defines an implicit conversion between the void pointer type and other pointers to objects. An explicit cast of malloc's return value is sometimes performed because malloc originally returned a char *, but this cast is unnecessary in standard C code.[4][5] Omitting the cast, however, creates an incompatibility with C++, which does require it. The lack of a specific pointer type returned from malloc is type-unsafe behaviour: malloc allocates based on byte count but not on type. This distinguishes it from the C++ new operator that returns a pointer whose type relies on the operand. (see C Type Safety). " See also http://www.opensc-project.org/pipermail/opensc-devel/2010-August/014586.html git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4636 c6295689-39f2-0310-b995-f0e70906c6a9
2010-08-18 15:08:51 +00:00
data = malloc((size_t)stbuf.st_size);
if (data == NULL) {
rv = SC_ERROR_OUT_OF_MEMORY;
goto err;
2015-04-29 21:22:27 +00:00
}
2015-01-28 03:45:08 +00:00
}
else {
if (count > *bufsize) {
rv = SC_ERROR_BUFFER_TOO_SMALL;
goto err;
}
data = *buf;
}
if (count != fread(data, 1, count, f)) {
rv = SC_ERROR_BUFFER_TOO_SMALL;
goto err;
}
*buf = data;
*bufsize = count;
rv = SC_SUCCESS;
err:
if (rv != SC_SUCCESS) {
if (data != *buf) {
free(data);
}
}
fclose(f);
return rv;
}
int sc_pkcs15_cache_file(struct sc_pkcs15_card *p15card,
const sc_path_t *path,
const u8 *buf, size_t bufsize)
{
char fname[PATH_MAX];
int r;
FILE *f;
size_t c;
r = generate_cache_filename(p15card, path, fname, sizeof(fname));
if (r != 0)
return r;
f = fopen(fname, "wb");
/* If the open failed because the cache directory does
* not exist, create it and a re-try the fopen() call.
*/
if (f == NULL && errno == ENOENT) {
if ((r = sc_make_cache_dir(p15card->card->ctx)) < 0)
return r;
f = fopen(fname, "wb");
}
if (f == NULL)
return 0;
c = fwrite(buf, 1, bufsize, f);
fclose(f);
if (c != bufsize) {
2018-11-22 08:31:29 +00:00
sc_log(p15card->card->ctx,
"fwrite() wrote only %"SC_FORMAT_LEN_SIZE_T"u bytes",
c);
unlink(fname);
return SC_ERROR_INTERNAL;
}
return 0;
}