pkcs15init: change sc_pkcs15init_bind() prototype

Add new argument 'application-info',
that will allow to select the on-card application to by binded with.

pkcs11: use sc_pkcs15init_bind with 'AID' argument

Prototype of sc_pkcs15init_bind() has been changed to add argument with
AID of the on-card application to be binded with.
This commit is contained in:
Viktor Tarasov 2012-05-25 09:56:15 +02:00
parent bf752f3c61
commit 10e1ad001d
5 changed files with 717 additions and 426 deletions

File diff suppressed because it is too large Load Diff

View File

@ -36,7 +36,7 @@ static CK_RV pkcs15init_bind(struct sc_pkcs11_card *p11card, struct sc_app_info
struct sc_profile *profile;
int rc;
rc = sc_pkcs15init_bind(card, "pkcs15", NULL, &profile);
rc = sc_pkcs15init_bind(card, "pkcs15", NULL, NULL, &profile);
if (rc == 0)
p11card->fws_data[0] = profile;
return sc_to_cryptoki_error(rc, NULL);

View File

@ -201,6 +201,7 @@ struct sc_pkcs15init_prkeyargs {
struct sc_pkcs15_id id;
struct sc_pkcs15_id auth_id;
const char * label;
const char * guid;
unsigned long usage;
unsigned long x509_usage;
unsigned int flags;
@ -261,7 +262,7 @@ extern struct sc_pkcs15_object *sc_pkcs15init_new_object(int, const char *,
struct sc_pkcs15_id *, void *);
extern void sc_pkcs15init_set_callbacks(struct sc_pkcs15init_callbacks *);
extern int sc_pkcs15init_bind(struct sc_card *, const char *, const char *,
struct sc_profile **);
struct sc_app_info *app_info, struct sc_profile **);
extern void sc_pkcs15init_unbind(struct sc_profile *);
extern void sc_pkcs15init_set_p15card(struct sc_profile *,
struct sc_pkcs15_card *);

View File

@ -59,6 +59,7 @@
#include "common/libscdl.h"
#include "libopensc/pkcs15.h"
#include "libopensc/cardctl.h"
#include "libopensc/asn1.h"
#include "libopensc/log.h"
#include "profile.h"
#include "pkcs15-init.h"
@ -282,9 +283,8 @@ load_dynamic_driver(struct sc_context *ctx, void **dll,
* Set up profile
*/
int
sc_pkcs15init_bind(struct sc_card *card, const char *name,
const char *profile_option,
struct sc_profile **result)
sc_pkcs15init_bind(struct sc_card *card, const char *name, const char *profile_option,
struct sc_app_info *app_info, struct sc_profile **result)
{
struct sc_context *ctx = card->ctx;
struct sc_profile *profile;
@ -335,21 +335,20 @@ sc_pkcs15init_bind(struct sc_card *card, const char *name,
profile->options[i++] = strdup(s);
}
}
#if 0
r = sc_pkcs15init_read_info(card, profile);
if (r < 0) {
sc_profile_free(profile);
LOG_TEST_RET(ctx, r, "Read info error");
}
#endif
/* Check the config file for a profile name.
* If none is defined, use the default profile name.
*/
if (!get_profile_from_config(card, card_profile, sizeof(card_profile)))
strcpy(card_profile, driver);
if (profile_option != NULL) {
if (profile_option != NULL)
strlcpy(card_profile, profile_option, sizeof(card_profile));
}
do {
r = sc_profile_load(profile, profile->name);
@ -364,7 +363,7 @@ sc_pkcs15init_bind(struct sc_card *card, const char *name,
break;
}
r = sc_profile_finish(profile, NULL);
r = sc_profile_finish(profile, NULL);
if (r < 0)
sc_log(ctx, "Failed to finalize profile: %s", sc_strerror(r));
} while (0);
@ -374,6 +373,28 @@ sc_pkcs15init_bind(struct sc_card *card, const char *name,
LOG_TEST_RET(ctx, r, "Load profile error");
}
if (app_info && app_info->aid.len) {
struct sc_path path;
if (card->ef_atr->aid.len) {
sc_log(ctx, "sc_pkcs15init_bind() select MF");
memset(&path, 0, sizeof(struct sc_path));
path.type = SC_PATH_TYPE_DF_NAME;
path.aid = card->ef_atr->aid;
r = sc_select_file(card, &path, NULL);
sc_log(ctx, "rv %i", r);
if (r)
return r;
}
sc_log(ctx, "sc_pkcs15init_bind() select application DF");
memset(&path, 0, sizeof(struct sc_path));
path.type = SC_PATH_TYPE_DF_NAME;
path.aid = app_info->aid;
r = sc_select_file(card, &path, NULL);
sc_log(ctx, "sc_pkcs15init_bind() select application DF returned %i", r);
}
*result = profile;
LOG_FUNC_RETURN(ctx, r);
}
@ -666,27 +687,31 @@ sc_pkcs15init_finalize_profile(struct sc_card *card, struct sc_profile *profile,
int rv;
LOG_FUNC_CALLED(ctx);
if (!aid || !aid->len)
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
if (card->app_count < 0)
sc_enum_apps(card);
sc_log(ctx, "finalize profile for AID %s", sc_dump_hex(aid->value, aid->len));
app = sc_find_app(card, aid);
if (!app) {
sc_log(ctx, "Cannot find oncard application");
LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
if (aid) {
sc_log(ctx, "finalize profile for AID %s", sc_dump_hex(aid->value, aid->len));
app = sc_find_app(card, aid);
if (!app) {
sc_log(ctx, "Cannot find oncard application");
LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
}
}
else if (card->app_count == 1) {
app = card->app[0];
}
else if (card->app_count > 1) {
LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "Need AID defined in this context");
}
sc_log(ctx, "Finalize profile with application '%s'", app->label);
sc_log(ctx, "Finalize profile with application '%s'", app ? app->label : "default");
rv = sc_profile_finish(profile, app);
sc_log(ctx, "sc_pkcs15init_finalize_profile() returns %i", rv);
LOG_FUNC_RETURN(ctx, rv);
}
/*
* Initialize the PKCS#15 application
*/
@ -840,8 +865,8 @@ sc_pkcs15init_add_app(struct sc_card *card, struct sc_profile *profile,
if (r >= 0)
r = sc_pkcs15init_update_tokeninfo(p15card, profile);
/* FIXME: what to do if sc_pkcs15init_update_dir failed? */
} else {
}
else {
free(app); /* unused */
}
@ -1178,8 +1203,10 @@ sc_pkcs15init_init_prkdf(struct sc_pkcs15_card *p15card,
LOG_TEST_RET(ctx, r, "Failed to select card specific key reference");
r = sc_pkcs15_find_prkey_by_reference(p15card, &key_info->path, key_info->key_reference, NULL);
if (r == SC_ERROR_OBJECT_NOT_FOUND)
if (r == SC_ERROR_OBJECT_NOT_FOUND) {
sc_log(ctx, "Will use key reference %i", key_info->key_reference);
break;
}
if (r != 0)
/* Other error trying to retrieve pin obj */

View File

@ -437,8 +437,7 @@ main(int argc, char **argv)
sc_pkcs15init_set_callbacks(&callbacks);
/* Bind the card-specific operations and load the profile */
if ((r = sc_pkcs15init_bind(card, opt_profile,
opt_card_profile, &profile)) < 0) {
if ((r = sc_pkcs15init_bind(card, opt_profile, opt_card_profile, NULL, &profile)) < 0) {
printf("Couldn't bind to the card: %s\n", sc_strerror(r));
return 1;
}