2002-01-24 16:02:54 +00:00
|
|
|
/*
|
|
|
|
* pkcs15-cache.c: PKCS #15 file caching functions
|
|
|
|
*
|
2006-12-19 21:31:17 +00:00
|
|
|
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
|
2002-01-24 16:02:54 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2002-10-19 14:04:52 +00:00
|
|
|
#include <stdio.h>
|
2002-01-24 16:02:54 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-10-19 14:04:52 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2002-01-24 16:02:54 +00:00
|
|
|
#include <sys/stat.h>
|
2003-01-09 09:18:02 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
2002-01-24 16:02:54 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "internal.h"
|
|
|
|
#include "pkcs15.h"
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
static int generate_cache_filename(struct sc_pkcs15_card *p15card,
|
2005-03-08 20:59:35 +00:00
|
|
|
const sc_path_t *path,
|
2002-01-26 12:16:13 +00:00
|
|
|
char *buf, size_t bufsize)
|
2002-01-24 16:02:54 +00:00
|
|
|
{
|
2003-01-09 09:18:02 +00:00
|
|
|
char dir[PATH_MAX];
|
2002-01-24 16:02:54 +00:00
|
|
|
char pathname[SC_MAX_PATH_SIZE*2+1];
|
2003-05-30 08:54:42 +00:00
|
|
|
int r;
|
2002-01-24 16:02:54 +00:00
|
|
|
const u8 *pathptr;
|
2003-05-30 08:54:42 +00:00
|
|
|
size_t i, pathlen;
|
2002-01-24 16:02:54 +00:00
|
|
|
|
|
|
|
if (path->type != SC_PATH_TYPE_PATH)
|
|
|
|
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;
|
|
|
|
pathptr = path->value;
|
|
|
|
pathlen = path->len;
|
|
|
|
if (pathlen > 2 && memcmp(pathptr, "\x3F\x00", 2) == 0) {
|
|
|
|
pathptr += 2;
|
|
|
|
pathlen -= 2;
|
|
|
|
}
|
|
|
|
for (i = 0; i < pathlen; i++)
|
2005-08-05 16:24:35 +00:00
|
|
|
sprintf(pathname + 2*i, "%02X", pathptr[i]);
|
|
|
|
if (p15card->serial_number != NULL) {
|
|
|
|
if (p15card->last_update != NULL)
|
|
|
|
r = snprintf(buf, bufsize, "%s/%s_%s_%s", dir,
|
|
|
|
p15card->serial_number, p15card->last_update,
|
|
|
|
pathname);
|
|
|
|
else
|
|
|
|
r = snprintf(buf, bufsize, "%s/%s_DATE_%s", dir,
|
|
|
|
p15card->serial_number, pathname);
|
|
|
|
if (r < 0)
|
|
|
|
return SC_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
} else
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
return SC_SUCCESS;
|
2002-01-24 16:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sc_pkcs15_read_cached_file(struct sc_pkcs15_card *p15card,
|
2005-03-08 20:59:35 +00:00
|
|
|
const sc_path_t *path,
|
2002-01-24 16:02:54 +00:00
|
|
|
u8 **buf, size_t *bufsize)
|
|
|
|
{
|
2005-03-02 06:03:49 +00:00
|
|
|
char fname[PATH_MAX];
|
2003-05-30 08:54:42 +00:00
|
|
|
int r;
|
2002-01-24 16:02:54 +00:00
|
|
|
FILE *f;
|
2003-05-30 08:54:42 +00:00
|
|
|
size_t count, offset, got;
|
2002-01-24 16:02:54 +00:00
|
|
|
struct stat stbuf;
|
|
|
|
u8 *data = NULL;
|
|
|
|
|
|
|
|
r = generate_cache_filename(p15card, path, fname, sizeof(fname));
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
r = stat(fname, &stbuf);
|
|
|
|
if (r)
|
|
|
|
return SC_ERROR_FILE_NOT_FOUND;
|
2002-12-02 13:38:50 +00:00
|
|
|
if (path->count < 0) {
|
|
|
|
count = stbuf.st_size;
|
|
|
|
offset = 0;
|
|
|
|
} else {
|
|
|
|
count = path->count;
|
|
|
|
offset = path->index;
|
2005-03-02 06:03:49 +00:00
|
|
|
if (offset + count > (size_t)stbuf.st_size)
|
2002-12-02 13:38:50 +00:00
|
|
|
return SC_ERROR_FILE_NOT_FOUND; /* cache file bad? */
|
|
|
|
}
|
2002-01-24 16:02:54 +00:00
|
|
|
if (*buf == NULL) {
|
2004-10-13 19:07:57 +00:00
|
|
|
data = (u8 *) malloc((size_t)stbuf.st_size);
|
2002-01-24 16:02:54 +00:00
|
|
|
if (data == NULL)
|
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else
|
2002-12-02 13:38:50 +00:00
|
|
|
if (count > *bufsize)
|
2002-01-24 16:02:54 +00:00
|
|
|
return SC_ERROR_BUFFER_TOO_SMALL;
|
2003-01-09 08:46:11 +00:00
|
|
|
f = fopen(fname, "rb");
|
2002-01-24 16:02:54 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
if (data)
|
|
|
|
free(data);
|
|
|
|
return SC_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
2002-12-02 13:38:50 +00:00
|
|
|
if (offset)
|
2004-10-13 19:07:57 +00:00
|
|
|
fseek(f, (long)offset, SEEK_SET);
|
2002-01-24 16:02:54 +00:00
|
|
|
if (data)
|
|
|
|
*buf = data;
|
2002-12-02 13:38:50 +00:00
|
|
|
got = fread(*buf, 1, count, f);
|
2002-01-24 16:02:54 +00:00
|
|
|
fclose(f);
|
2002-12-02 13:38:50 +00:00
|
|
|
if (got != count) {
|
2002-01-24 16:02:54 +00:00
|
|
|
if (data)
|
|
|
|
free(data);
|
|
|
|
return SC_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2002-12-02 13:38:50 +00:00
|
|
|
*bufsize = count;
|
2002-01-24 16:02:54 +00:00
|
|
|
if (data)
|
|
|
|
*buf = data;
|
2006-01-23 17:29:59 +00:00
|
|
|
return 0;
|
2002-01-24 16:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sc_pkcs15_cache_file(struct sc_pkcs15_card *p15card,
|
2005-03-08 20:59:35 +00:00
|
|
|
const sc_path_t *path,
|
2002-01-24 16:02:54 +00:00
|
|
|
const u8 *buf, size_t bufsize)
|
|
|
|
{
|
2003-01-09 09:18:02 +00:00
|
|
|
char fname[PATH_MAX];
|
2002-01-24 16:02:54 +00:00
|
|
|
int r;
|
|
|
|
FILE *f;
|
|
|
|
size_t c;
|
|
|
|
|
|
|
|
r = generate_cache_filename(p15card, path, fname, sizeof(fname));
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
2003-01-09 09:18:02 +00:00
|
|
|
|
2003-01-09 08:46:11 +00:00
|
|
|
f = fopen(fname, "wb");
|
2003-01-09 09:18:02 +00:00
|
|
|
/* 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");
|
|
|
|
}
|
2002-01-24 16:02:54 +00:00
|
|
|
if (f == NULL)
|
|
|
|
return 0;
|
2003-01-09 09:18:02 +00:00
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
c = fwrite(buf, 1, bufsize, f);
|
|
|
|
fclose(f);
|
|
|
|
if (c != bufsize) {
|
2010-03-15 12:17:13 +00:00
|
|
|
sc_debug(p15card->card->ctx, SC_LOG_DEBUG_NORMAL, "fwrite() wrote only %d bytes", c);
|
2002-01-24 16:02:54 +00:00
|
|
|
unlink(fname);
|
|
|
|
return SC_ERROR_INTERNAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|