diff --git a/src/libopensc/libopensc.exports b/src/libopensc/libopensc.exports index 0352006b..80a473a9 100644 --- a/src/libopensc/libopensc.exports +++ b/src/libopensc/libopensc.exports @@ -285,6 +285,7 @@ sc_pkcs15init_update_certificate sc_pkcs15init_update_file sc_pkcs15init_verify_secret sc_pkcs15init_sanity_check +sc_pkcs15init_finalize_profile sc_card_find_rsa_alg sc_print_cache sc_find_app diff --git a/src/pkcs15init/pkcs15-init.h b/src/pkcs15init/pkcs15-init.h index 702798d0..8daea46f 100644 --- a/src/pkcs15init/pkcs15-init.h +++ b/src/pkcs15init/pkcs15-init.h @@ -368,6 +368,9 @@ extern int sc_pkcs15init_get_pin_reference(struct sc_pkcs15_card *, extern int sc_pkcs15init_sanity_check(struct sc_pkcs15_card *, struct sc_profile *); +extern int sc_pkcs15init_finalize_profile(struct sc_card *card, struct sc_profile *profile, + struct sc_aid *aid); + extern struct sc_pkcs15init_operations *sc_pkcs15init_get_gpk_ops(void); extern struct sc_pkcs15init_operations *sc_pkcs15init_get_miocos_ops(void); extern struct sc_pkcs15init_operations *sc_pkcs15init_get_cryptoflex_ops(void); diff --git a/src/pkcs15init/pkcs15-lib.c b/src/pkcs15init/pkcs15-lib.c index 8ec68e50..756795f1 100644 --- a/src/pkcs15init/pkcs15-lib.c +++ b/src/pkcs15init/pkcs15-lib.c @@ -652,6 +652,36 @@ sc_pkcs15init_finalize_card(struct sc_card *card, struct sc_profile *profile) } +int +sc_pkcs15init_finalize_profile(struct sc_card *card, struct sc_profile *profile, + struct sc_aid *aid) +{ + struct sc_context *ctx = card->ctx; + const struct sc_app_info *app = NULL; + 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); + } + + sc_log(ctx, "Finalize profile with application '%s'", app->label); + 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 */ @@ -3174,7 +3204,7 @@ do_select_parent(struct sc_profile *profile, struct sc_pkcs15_card *p15card, path = file->path; if (path.len >= 2) path.len -= 2; - if (path.len == 0) + if (!path.len && !path.aid.len) sc_format_path("3F00", &path); /* Select the parent DF. */ diff --git a/src/tools/pkcs15-init.c b/src/tools/pkcs15-init.c index 72eab9fa..d899e65f 100644 --- a/src/tools/pkcs15-init.c +++ b/src/tools/pkcs15-init.c @@ -462,6 +462,13 @@ main(int argc, char **argv) fprintf(stderr, "Invalid AID value: '%s'\n", opt_bind_to_aid); return 1; } + + r = sc_pkcs15init_finalize_profile(card, profile, &aid); + if (r < 0) { + fprintf(stderr, "Finalize profile error %s\n", sc_strerror(r)); + break; + } + r = sc_pkcs15_bind(card, &aid, &p15card); } else {