- implement thread-safe path printing function sc_path_print() and use

it src/libopensc/ and src/pkcs15init/
- use size_t for the certlen parameter of sc_pkcs15init_update_certificate()


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@2811 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
nils 2006-01-11 23:41:17 +00:00
parent 363d9be861
commit de4b5c280f
15 changed files with 249 additions and 70 deletions

View File

@ -369,10 +369,15 @@ static int atrust_acos_select_file(struct sc_card *card,
size_t i, pathlen;
if (card->ctx->debug >= 4) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), &card->cache.current_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "current path (%s, %s): %s (len: %u)\n",
(card->cache.current_path.type==SC_PATH_TYPE_DF_NAME?"aid":"path"),
(card->cache_valid?"valid":"invalid"),
sc_print_path(&card->cache.current_path),
(card->cache_valid?"valid":"invalid"), pbuf,
card->cache.current_path.len);
}

View File

@ -578,8 +578,15 @@ static int flex_select_file(sc_card_t *card, const sc_path_t *path,
int locked = 0, magic_done;
u8 p1 = 0;
if (card->ctx->debug >= 2)
sc_debug(card->ctx, "called, cached path=%s\n", sc_print_path(&card->cache.current_path));
if (card->ctx->debug >= 2) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), &card->cache.current_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "called, cached path=%s\n", pbuf);
}
switch (path->type) {
case SC_PATH_TYPE_PATH:

View File

@ -737,10 +737,22 @@ auth_select_file(sc_card_t *card, const sc_path_t *path,
int locked = 0, magic_done;
u8 p1 = 0;
sc_debug(card->ctx, "path; type=%d, path=%s\n",
path->type, sc_print_path(path));
sc_debug(card->ctx, "cache; type=%d, path=%s\n",
card->cache.current_path.type, sc_print_path(&card->cache.current_path));
if (card->ctx->debug >= 1) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
rv = sc_path_print(pbuf, sizeof(pbuf), path);
if (rv != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "path; type=%d, path=%s\n", path->type, pbuf);
rv = sc_path_print(pbuf, sizeof(pbuf), &card->cache.current_path);
if (rv != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "cache; type=%d, path=%s\n",
card->cache.current_path.type, pbuf);
}
switch (path->type) {
case SC_PATH_TYPE_PATH:
@ -832,7 +844,16 @@ auth_delete_file(sc_card_t *card, const sc_path_t *path)
u8 sbuf[2];
sc_apdu_t apdu;
sc_debug(card->ctx, "path; type=%d, path=%s\n", path->type, sc_print_path(path));
if (card->ctx->debug >= 1) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
rv = sc_path_print(pbuf, sizeof(pbuf), path);
if (rv != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "path; type=%d, path=%s\n", path->type, pbuf);
}
SC_FUNC_CALLED(card->ctx, 1);
if (path->len < 2) {
sc_error(card->ctx, "Invalid path length\n");
@ -1135,16 +1156,28 @@ auth_create_file(sc_card_t *card, sc_file_t *file)
int rv, rec_nr;
sc_apdu_t apdu;
sc_path_t path;
char pbuf[SC_MAX_PATH_STRING_SIZE];
struct auth_private_data *prv = (struct auth_private_data *) card->drv_data;
sc_debug(card->ctx, " create path=%s\n", sc_print_path(&file->path));
sc_debug(card->ctx,"id %04X; size %i; type %i; ef %i\n",
if (card->ctx->debug >= 1) {
rv = sc_path_print(pbuf, sizeof(pbuf), &file->path);
if (rv != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, " create path=%s\n", pbuf);
sc_debug(card->ctx,"id %04X; size %i; type %i; ef %i\n",
file->id, file->size, file->type, file->ef_structure);
}
if (file->id==0x0000 || file->id==0xFFFF || file->id==0x3FFF)
return SC_ERROR_INVALID_ARGUMENTS;
sc_debug(card->ctx, " cache path=%s\n",
sc_print_path(&card->cache.current_path));
if (card->ctx->debug >= 1) {
rv = sc_path_print(pbuf, sizeof(pbuf), &card->cache.current_path);
if (rv != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, " cache path=%s\n", pbuf);
}
if (file->path.len) {
memcpy(&path, &file->path, sizeof(path));

View File

@ -366,10 +366,15 @@ static int starcos_select_file(sc_card_t *card,
SC_FUNC_CALLED(card->ctx, 1);
if (card->ctx->debug >= 4) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), &card->cache.current_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "current path (%s, %s): %s (len: %u)\n",
(card->cache.current_path.type==SC_PATH_TYPE_DF_NAME?"aid":"path"),
(card->cache_valid?"valid":"invalid"),
sc_print_path(&card->cache.current_path),
(card->cache_valid?"valid":"invalid"), pbuf,
card->cache.current_path.len);
}

View File

@ -297,11 +297,15 @@ int sc_create_file(sc_card_t *card, sc_file_t *file)
assert(card != NULL);
if (card->ctx->debug >= 1) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
const sc_path_t *in_path = &file->path;
r = sc_path_print(pbuf, sizeof(pbuf), in_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "called; type=%d, path=%s, size=%u\n",
in_path->type,
sc_print_path(in_path), file->size);
in_path->type, pbuf, file->size);
}
if (card->ops->create_file == NULL)
SC_FUNC_RETURN(card->ctx, 1, SC_ERROR_NOT_SUPPORTED);
@ -315,9 +319,14 @@ int sc_delete_file(sc_card_t *card, const sc_path_t *path)
assert(card != NULL);
if (card->ctx->debug >= 1) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "called; type=%d, path=%s\n",
path->type,
sc_print_path(path));
path->type, pbuf);
}
if (card->ops->delete_file == NULL)
SC_FUNC_RETURN(card->ctx, 1, SC_ERROR_NOT_SUPPORTED);
@ -459,9 +468,14 @@ int sc_select_file(sc_card_t *card,
assert(card != NULL && in_path != NULL);
if (card->ctx->debug >= 1) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), in_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "called; type=%d, path=%s\n",
in_path->type,
sc_print_path(in_path));
in_path->type, pbuf);
}
if (in_path->len > SC_MAX_PATH_SIZE)
SC_FUNC_RETURN(card->ctx, 2, SC_ERROR_INVALID_ARGUMENTS);

View File

@ -915,12 +915,21 @@ int sc_file_set_type_attr(sc_file_t *file, const u8 *type_attr,
void sc_format_path(const char *path_in, sc_path_t *path_out);
/**
* Return string representation of the given sc_path_t object
* Warning: as static memory is used this function is not thread-safe.
* Warning: as static memory is used for the return value
* this function is not thread-safe !!!
* @param path sc_path_t object of the path to be printed
* @return pointer to a const buffer with the string representation
* of the path
*/
const char *sc_print_path(const sc_path_t *path);
/**
* Prints the sc_path_t object to a character buffer
* @param buf pointer to the buffer
* @param buflen size of the buffer
* @param path sc_path_t object to be printed
* @return SC_SUCCESS on success and an error code otherwise
*/
int sc_path_print(char *buf, size_t buflen, const sc_path_t *path);
/**
* Compares two sc_path_t objects
* @param patha sc_path_t object of the first path

View File

@ -552,7 +552,13 @@ static int sc_pkcs15_bind_internal(sc_pkcs15_card_t *p15card)
err = sc_select_file(card, &tmppath, &p15card->file_odf);
}
if (err != SC_SUCCESS) {
sc_debug(ctx, "EF(ODF) not found in '%s'\n", sc_print_path(&tmppath));
char pbuf[SC_MAX_PATH_STRING_SIZE];
int r = sc_path_print(pbuf, sizeof(pbuf), &tmppath);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(ctx, "EF(ODF) not found in '%s'\n", pbuf);
goto end;
}
@ -579,10 +585,15 @@ static int sc_pkcs15_bind_internal(sc_pkcs15_card_t *p15card)
sc_debug(card->ctx, "The following DFs were found:\n");
for (df = p15card->df_list; df; df = df->next) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
int r = sc_path_print(pbuf, sizeof(pbuf), &df->path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx,
" DF type %u, path %s, index %u, count %d\n",
df->type, sc_print_path(&df->path),
df->path.index, df->path.count);
df->type, pbuf, df->path.index, df->path.count);
}
}
@ -1336,8 +1347,13 @@ int sc_pkcs15_add_unusedspace(struct sc_pkcs15_card *p15card,
sc_pkcs15_unusedspace_t *p = p15card->unusedspace_list, *new_unusedspace;
if (path->count == -1) {
sc_error(p15card->card->ctx, "No offset and length present in path %s\n",
sc_print_path(path));
char pbuf[SC_MAX_PATH_STRING_SIZE];
int r = sc_path_print(pbuf, sizeof(pbuf), path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_error(p15card->card->ctx, "No offset and length present in path %s\n", pbuf);
return SC_ERROR_INVALID_ARGUMENTS;
}
@ -1506,8 +1522,17 @@ int sc_pkcs15_read_file(struct sc_pkcs15_card *p15card,
int r = -1;
assert(p15card != NULL && in_path != NULL && buf != NULL);
sc_debug(p15card->card->ctx, "called, path=%s, index=%u, count=%d\n",
sc_print_path(in_path), in_path->index, in_path->count);
if (p15card->card->ctx->debug >= 1) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), in_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(p15card->card->ctx, "called, path=%s, index=%u, count=%d\n",
pbuf, in_path->index, in_path->count);
}
if (in_path->type == SC_PATH_TYPE_FILE_ID) {
/* in case of a FID prepend the application DF */

View File

@ -265,18 +265,31 @@ int sc_concatenate_path(sc_path_t *d, const sc_path_t *p1, const sc_path_t *p2)
const char *sc_print_path(const sc_path_t *path)
{
static char buffer[64];
size_t n, len;
static char buffer[SC_MAX_PATH_STRING_SIZE];
buffer[0] = '\0';
if ((len = path->len) >= sizeof(buffer)/2)
len = sizeof(buffer)/2;
for (n = 0; n < len; n++)
sprintf(buffer + 2*n, "%02x", path->value[n]);
if (sc_path_print(buffer, sizeof(buffer), path) != SC_SUCCESS)
buffer[0] = '\0';
return buffer;
}
int sc_path_print(char *buf, size_t buflen, const sc_path_t *path)
{
size_t i;
if (buf == NULL || path == NULL)
return SC_ERROR_INVALID_ARGUMENTS;
if (buflen < path->len * 2 + 1)
return SC_ERROR_BUFFER_TOO_SMALL;
buf[0] = '\0';
for (i = 0; i < path->len; i++)
snprintf(buf + 2 * i, buflen - 2 * i, "%02x", path->value[i]);
return SC_SUCCESS;
}
int sc_compare_path(const sc_path_t *path1, const sc_path_t *path2)
{
return path1->len == path2->len

View File

@ -40,6 +40,7 @@ struct sc_object_id {
EnveleopedData objects */
#define SC_MAX_PATH_SIZE 16
#define SC_MAX_PATH_STRING_SIZE (SC_MAX_PATH_SIZE * 2 + 1)
typedef struct sc_path {
u8 value[SC_MAX_PATH_SIZE];

View File

@ -612,9 +612,14 @@ cflex_get_keyfiles(sc_profile_t *profile, sc_card_t *card,
/* Get the private key file */
r = sc_profile_get_file_by_path(profile, &path, prkf);
if (r < 0) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), &path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_error(card->ctx, "Cannot find private key file info "
"in profile (path=%s).",
sc_print_path(&path));
"in profile (path=%s).", pbuf);
return r;
}

View File

@ -497,9 +497,17 @@ gpk_generate_key(sc_profile_t *profile, sc_card_t *card,
sc_file_t *keyfile;
int r, n;
sc_debug(card->ctx, "path=%s, %d bits\n",
sc_print_path(&key_info->path),
if (card->ctx->debug >= 1) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), &key_info->path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "path=%s, %d bits\n", pbuf,
key_info->modulus_length);
}
if (obj->type != SC_PKCS15_TYPE_PRKEY_RSA) {
sc_error(card->ctx, "GPK supports generating only RSA keys.");
return SC_ERROR_NOT_SUPPORTED;

View File

@ -341,7 +341,7 @@ extern int sc_pkcs15init_update_certificate(sc_pkcs15_card_t *p15card,
sc_profile_t *profile,
sc_pkcs15_object_t *obj,
const unsigned char *rawcert,
int certlen);
size_t certlen);
extern int sc_pkcs15init_create_file(struct sc_profile *,
struct sc_card *, struct sc_file *);

View File

@ -509,9 +509,13 @@ sc_pkcs15init_rmdir(sc_card_t *card, struct sc_profile *profile,
struct sc_path path;
struct sc_file *file, *parent;
int r = 0, nfids;
char pbuf[SC_MAX_PATH_STRING_SIZE];
sc_debug(card->ctx, "sc_pkcs15init_rmdir(%s)\n",
sc_print_path(&df->path));
r = sc_path_print(pbuf, sizeof(pbuf), &df->path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "sc_pkcs15init_rmdir(%s)\n", pbuf);
if (df == NULL)
return SC_ERROR_INTERNAL;
@ -2250,6 +2254,7 @@ select_object_path(sc_pkcs15_card_t *p15card, sc_profile_t *profile,
sc_file_t *file;
const char *name;
int r;
char pbuf[SC_MAX_PATH_STRING_SIZE];
/* For cards with a pin-domain profile, we need
* to put the key below the DF of the specified PIN */
@ -2282,9 +2287,13 @@ select_object_path(sc_pkcs15_card_t *p15card, sc_profile_t *profile,
return 0;
}
r = sc_path_print(pbuf, sizeof(pbuf), path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(p15card->card->ctx,
"key-domain.%s @%s (auth_id.len=%d)\n",
name, sc_print_path(path), obj->auth_id.len);
"key-domain.%s @%s (auth_id.len=%d)\n",
name, pbuf, obj->auth_id.len);
r = sc_profile_instantiate_template(profile,
"key-domain", path,
name, obj_id, &file);
@ -2790,7 +2799,7 @@ int
sc_pkcs15init_update_certificate(sc_pkcs15_card_t *p15card,
sc_profile_t *profile,
sc_pkcs15_object_t *obj,
const unsigned char *rawcert, int certlen)
const unsigned char *rawcert, size_t certlen)
{
sc_file_t *file = NULL, *parent = NULL;
sc_path_t *path = &((sc_pkcs15_cert_info_t *)obj->data)->path;
@ -3138,10 +3147,14 @@ sc_pkcs15init_authenticate(struct sc_profile *pro, sc_card_t *card,
{
const sc_acl_entry_t *acl;
sc_file_t *file_tmp = NULL;
int r = 0;
int r = 0;
char pbuf[SC_MAX_PATH_STRING_SIZE];
sc_debug(card->ctx, "path=%s, op=%u\n",
sc_print_path(&file->path), op);
r = sc_path_print(pbuf, sizeof(pbuf), &file->path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "path=%s, op=%u\n", pbuf, op);
if (card->caps & SC_CARD_CAP_USE_FCI_AC) {
if ((r = sc_select_file(card, &file->path, &file_tmp)) < 0)
@ -3194,8 +3207,14 @@ do_select_parent(struct sc_profile *pro, sc_card_t *card,
if (r == SC_ERROR_FILE_NOT_FOUND && path.len != 2) {
r = sc_profile_get_file_by_path(pro, &path, parent);
if (r < 0) {
sc_error(card->ctx, "profile doesn't define a DF %s",
sc_print_path(&path));
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), &path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_error(card->ctx,
"profile doesn't define a DF %s", pbuf);
return r;
}
if (!(r = sc_pkcs15init_create_file(pro, card, *parent)))
@ -3206,8 +3225,14 @@ do_select_parent(struct sc_profile *pro, sc_card_t *card,
sc_file_free(*parent);
r = sc_profile_get_file_by_path(pro, &path, parent);
if (r < 0) {
sc_error(card->ctx, "profile doesn't define a DF %s",
sc_print_path(&path));
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), &path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_error(card->ctx,
"profile doesn't define a DF %s", pbuf);
return r;
}
}
@ -3250,9 +3275,12 @@ sc_pkcs15init_update_file(struct sc_profile *profile, sc_card_t *card,
struct sc_file *info = NULL;
void *copy = NULL;
int r, need_to_zap = 0;
char pbuf[SC_MAX_PATH_STRING_SIZE];
sc_debug(card->ctx, "called, path=%s, %u bytes\n",
sc_print_path(&file->path), datalen);
r = sc_path_print(pbuf, sizeof(pbuf), &file->path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "called, path=%s, %u bytes\n", pbuf, datalen);
sc_ctx_suppress_errors_on(card->ctx);
if ((r = sc_select_file(card, &file->path, &info)) < 0) {
@ -3270,11 +3298,16 @@ sc_pkcs15init_update_file(struct sc_profile *profile, sc_card_t *card,
}
if (info->size < datalen) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
r = sc_path_print(pbuf, sizeof(pbuf), &file->path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_error(card->ctx,
"File %s too small (require %u, have %u) - "
"please increase size in profile",
sc_print_path(&file->path),
datalen, info->size);
"File %s too small (require %u, have %u) - "
"please increase size in profile", pbuf,
datalen, info->size);
sc_file_free(info);
return SC_ERROR_TOO_MANY_OBJECTS;
} else if (info->size > datalen && need_to_zap) {

View File

@ -625,6 +625,7 @@ cosm_new_key(struct sc_profile *profile, sc_card_t *card,
struct sc_pkcs15_bignum bn[6];
u8 *buff;
int rv, ii;
char pbuf[SC_MAX_PATH_STRING_SIZE];
sc_debug(card->ctx, " index %i\n", idx);
if (key->algorithm != SC_ALGORITHM_RSA) {
@ -638,8 +639,10 @@ cosm_new_key(struct sc_profile *profile, sc_card_t *card,
if (rv < 0 || !prvfile)
return SC_ERROR_SYNTAX_ERROR;
sc_debug(card->ctx, " prvfile->id %i; path=%s\n",
prvfile->id, sc_print_path(&prvfile->path));
rv = sc_path_print(pbuf, sizeof(pbuf), &prvfile->path);
if (rv != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, " prvfile->id %i; path=%s\n", prvfile->id, pbuf);
rsa = &key->u.rsa;

View File

@ -568,15 +568,25 @@ sc_profile_instantiate_template(sc_profile_t *profile,
}
if (profile->card->ctx->debug >= 2) {
char pbuf[SC_MAX_PATH_STRING_SIZE];
int r = sc_path_print(pbuf, sizeof(pbuf), base_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(profile->card->ctx,
"Instantiating template %s at %s",
template_name, sc_print_path(base_path));
"Instantiating template %s at %s", template_name, pbuf);
}
base_file = sc_profile_find_file_by_path(profile, base_path);
if (base_file == NULL) {
sc_error(card->ctx, "Directory %s not defined in profile",
sc_print_path(base_path));
char pbuf[SC_MAX_PATH_STRING_SIZE];
int r = sc_path_print(pbuf, sizeof(pbuf), base_path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_error(card->ctx, "Directory %s not defined in profile", pbuf);
return SC_ERROR_OBJECT_NOT_FOUND;
}
@ -649,11 +659,19 @@ sc_profile_instantiate_file(sc_profile_t *profile, file_info *ft,
ft->instance = fi;
if (card->ctx->debug >= 2) {
sc_debug(card->ctx, "Instantiated %s at %s",
ft->ident, sc_print_path(&fi->file->path));
sc_debug(card->ctx, " parent=%s@%s",
parent->ident,
sc_print_path(&parent->file->path));
char pbuf[SC_MAX_PATH_STRING_SIZE];
int r = sc_path_print(pbuf, sizeof(pbuf), &fi->file->path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, "Instantiated %s at %s", ft->ident, pbuf);
r = sc_path_print(pbuf, sizeof(pbuf), &parent->file->path);
if (r != SC_SUCCESS)
pbuf[0] = '\0';
sc_debug(card->ctx, " parent=%s@%s", parent->ident, pbuf);
}
return fi;