From 936ad78b02911ce9886c5769dec80c042f751372 Mon Sep 17 00:00:00 2001 From: aet Date: Mon, 13 Oct 2003 20:41:00 +0000 Subject: [PATCH] - Some build/portability fixes for the pkcs15init rewrite - Add sc_pkcs15_get__ops(), yet untested git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@1512 c6295689-39f2-0310-b995-f0e70906c6a9 --- src/pkcs15init/keycache.c | 5 +---- src/pkcs15init/pkcs15-cflex.c | 34 ++++++++++++++++------------- src/pkcs15init/pkcs15-etoken.c | 33 ++++++++++++++-------------- src/pkcs15init/pkcs15-gpk.c | 23 ++++++++++--------- src/pkcs15init/pkcs15-init.h | 5 +++++ src/pkcs15init/pkcs15-lib.c | 40 +++++++++++++++++++--------------- src/pkcs15init/pkcs15-miocos.c | 18 +++++++++------ 7 files changed, 88 insertions(+), 70 deletions(-) diff --git a/src/pkcs15init/keycache.c b/src/pkcs15init/keycache.c index 25f670a9..10ce50b8 100644 --- a/src/pkcs15init/keycache.c +++ b/src/pkcs15init/keycache.c @@ -30,12 +30,9 @@ #endif #include #include +#include #include "profile.h" #include "pkcs15-init.h" -#include -#ifdef _WIN32 -#include -#endif #undef KEYCACHE_DEBUG #define MAX_SECRET 32 /* sufficient for 128bit symmetric keys */ diff --git a/src/pkcs15init/pkcs15-cflex.c b/src/pkcs15init/pkcs15-cflex.c index a50d8b97..6025103d 100644 --- a/src/pkcs15init/pkcs15-cflex.c +++ b/src/pkcs15init/pkcs15-cflex.c @@ -707,11 +707,11 @@ cflex_encode_private_key(struct sc_pkcs15_prkey_rsa *rsa, *key++ = (5 * base + 3) & 0xFF; *key++ = key_num; - if ((r < bn2cf(&rsa->p, key + 0 * base, base)) < 0 - || (r < bn2cf(&rsa->q, key + 1 * base, base)) < 0 - || (r < bn2cf(&rsa->iqmp, key + 2 * base, base)) < 0 - || (r < bn2cf(&rsa->dmp1, key + 3 * base, base)) < 0 - || (r < bn2cf(&rsa->dmq1, key + 4 * base, base)) < 0) + if ((r = bn2cf(&rsa->p, key + 0 * base, base)) < 0 + || (r = bn2cf(&rsa->q, key + 1 * base, base)) < 0 + || (r = bn2cf(&rsa->iqmp, key + 2 * base, base)) < 0 + || (r = bn2cf(&rsa->dmp1, key + 3 * base, base)) < 0 + || (r = bn2cf(&rsa->dmq1, key + 4 * base, base)) < 0) return r; key += 5 * base; @@ -765,14 +765,18 @@ cflex_encode_public_key(struct sc_pkcs15_prkey_rsa *rsa, return 0; } -struct sc_pkcs15init_operations sc_pkcs15init_cflex_operations = { - .erase_card = cflex_erase_card, - .create_dir = cflex_create_dir, - .create_domain = cflex_create_domain, - .select_pin_reference = cflex_select_pin_reference, - .create_pin = cflex_create_pin, - .create_key = cflex_create_key, - .generate_key = cflex_generate_key, - .store_key = cflex_store_key, +static struct sc_pkcs15init_operations sc_pkcs15init_cflex_operations; -}; +struct sc_pkcs15init_operations *sc_pkcs15init_get_cflex_ops(void) +{ + sc_pkcs15init_cflex_operations.erase_card = cflex_erase_card; + sc_pkcs15init_cflex_operations.create_dir = cflex_create_dir; + sc_pkcs15init_cflex_operations.create_domain = cflex_create_domain; + sc_pkcs15init_cflex_operations.select_pin_reference = cflex_select_pin_reference; + sc_pkcs15init_cflex_operations.create_pin = cflex_create_pin; + sc_pkcs15init_cflex_operations.create_key = cflex_create_key; + sc_pkcs15init_cflex_operations.generate_key = cflex_generate_key; + sc_pkcs15init_cflex_operations.store_key = cflex_store_key; + + return &sc_pkcs15init_cflex_operations; +} diff --git a/src/pkcs15init/pkcs15-etoken.c b/src/pkcs15init/pkcs15-etoken.c index 3a6b2e6c..18980edb 100644 --- a/src/pkcs15init/pkcs15-etoken.c +++ b/src/pkcs15init/pkcs15-etoken.c @@ -59,9 +59,6 @@ static int etoken_store_pin(sc_profile_t *profile, sc_card_t *card, const u8 *pin, size_t pin_len); static int etoken_create_sec_env(sc_profile_t *, sc_card_t *, unsigned int, unsigned int); -static int etoken_new_file(struct sc_profile *, struct sc_card *, - unsigned int, unsigned int, - struct sc_file **); static int etoken_put_key(struct sc_profile *, struct sc_card *, int, unsigned int, struct sc_pkcs15_prkey_rsa *); static int etoken_key_algorithm(unsigned int, int *); @@ -660,6 +657,7 @@ etoken_put_key(struct sc_profile *profile, struct sc_card *card, return r; } +#if 0 /* * Allocate a file */ @@ -737,6 +735,7 @@ etoken_new_file(struct sc_profile *profile, struct sc_card *card, *out = file; return 0; } +#endif /* * Extract a key component from the public key file populated by @@ -775,16 +774,18 @@ error(struct sc_profile *profile, const char *fmt, ...) profile->cbs->error("%s", buffer); } -struct sc_pkcs15init_operations sc_pkcs15init_etoken_operations = { - .erase_card = etoken_erase, - .create_dir = etoken_create_dir, - .select_pin_reference = etoken_select_pin_reference, - .create_pin = etoken_create_pin, - .select_key_reference = etoken_select_key_reference, - .create_key = etoken_create_key, - .store_key = etoken_store_key, - //.new_pin = etoken_new_pin, - //.new_key = etoken_new_key, - .generate_key = etoken_generate_key - //.new_file = etoken_new_file, -}; +static struct sc_pkcs15init_operations sc_pkcs15init_etoken_operations; + +struct sc_pkcs15init_operations *sc_pkcs15init_get_etoken_ops(void) +{ + sc_pkcs15init_etoken_operations.erase_card = etoken_erase; + sc_pkcs15init_etoken_operations.create_dir = etoken_create_dir; + sc_pkcs15init_etoken_operations.select_pin_reference = etoken_select_pin_reference; + sc_pkcs15init_etoken_operations.create_pin = etoken_create_pin; + sc_pkcs15init_etoken_operations.select_key_reference = etoken_select_key_reference; + sc_pkcs15init_etoken_operations.create_key = etoken_create_key; + sc_pkcs15init_etoken_operations.store_key = etoken_store_key; + sc_pkcs15init_etoken_operations.generate_key = etoken_generate_key; + + return &sc_pkcs15init_etoken_operations; +} diff --git a/src/pkcs15init/pkcs15-gpk.c b/src/pkcs15init/pkcs15-gpk.c index a1fd827d..3d7503d0 100644 --- a/src/pkcs15init/pkcs15-gpk.c +++ b/src/pkcs15init/pkcs15-gpk.c @@ -1117,13 +1117,16 @@ debug(struct sc_profile *profile, const char *fmt, ...) printf("%s", buffer); /* XXX */ } -struct sc_pkcs15init_operations sc_pkcs15init_gpk_operations = { - .erase_card = gpk_erase_card, - .create_dir = gpk_create_dir, - .select_pin_reference = gpk_select_pin_reference, - .create_pin = gpk_create_pin, - .create_key = gpk_create_key, - .store_key = gpk_store_key, -// .new_key = gpk_new_key, -// .new_file = gpk_new_file, -}; +static struct sc_pkcs15init_operations sc_pkcs15init_gpk_operations; + +struct sc_pkcs15init_operations *sc_pkcs15init_get_gpk_ops(void) +{ + sc_pkcs15init_gpk_operations.erase_card = gpk_erase_card; + sc_pkcs15init_gpk_operations.create_dir = gpk_create_dir; + sc_pkcs15init_gpk_operations.select_pin_reference = gpk_select_pin_reference; + sc_pkcs15init_gpk_operations.create_pin = gpk_create_pin; + sc_pkcs15init_gpk_operations.create_key = gpk_create_key; + sc_pkcs15init_gpk_operations.store_key = gpk_store_key; + + return &sc_pkcs15init_gpk_operations; +} diff --git a/src/pkcs15init/pkcs15-init.h b/src/pkcs15init/pkcs15-init.h index 27cf2e3e..817a97b0 100644 --- a/src/pkcs15init/pkcs15-init.h +++ b/src/pkcs15init/pkcs15-init.h @@ -321,6 +321,11 @@ extern int sc_pkcs15init_requires_restrictive_usage( extern int sc_pkcs15_create_pin_domain(sc_profile_t *, sc_card_t *, const sc_pkcs15_id_t *, sc_file_t **); +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_cflex_ops(void); +extern struct sc_pkcs15init_operations *sc_pkcs15init_get_etoken_ops(void); + #ifdef __cplusplus } #endif diff --git a/src/pkcs15init/pkcs15-lib.c b/src/pkcs15init/pkcs15-lib.c index 6e794b48..9fc60f24 100644 --- a/src/pkcs15init/pkcs15-lib.c +++ b/src/pkcs15init/pkcs15-lib.c @@ -113,23 +113,27 @@ static struct sc_pkcs15_df * find_df_by_type(struct sc_pkcs15_card *, int); static void default_error_handler(const char *fmt, ...); static void default_debug_handler(int, const char *fmt, ...); -/* Card specific functions */ -extern struct sc_pkcs15init_operations sc_pkcs15init_gpk_operations; -extern struct sc_pkcs15init_operations sc_pkcs15init_miocos_operations; -extern struct sc_pkcs15init_operations sc_pkcs15init_cflex_operations; -extern struct sc_pkcs15init_operations sc_pkcs15init_etoken_operations; +static struct profile_operations { + char *name; + void *func; +} profile_operations[] = { + { "gpk", (void *) sc_pkcs15init_get_gpk_ops }, + { "miocos", (void *) sc_pkcs15init_get_miocos_ops }, + { "flex", (void *) sc_pkcs15init_get_cflex_ops }, + { "etoken", (void *) sc_pkcs15init_get_etoken_ops }, + { NULL, NULL }, +}; static struct sc_pkcs15init_callbacks callbacks = { default_error_handler, default_debug_handler, NULL, - NULL + NULL, }; #define p15init_error callbacks.error #define p15init_debug callbacks.debug - /* * Set the application callbacks */ @@ -182,11 +186,12 @@ sc_pkcs15init_bind(struct sc_card *card, const char *name, struct sc_profile **result) { struct sc_profile *profile; + struct sc_pkcs15init_operations * (* func)(void) = NULL; const char *driver = card->driver->short_name; char main_profile[128], card_profile[PATH_MAX], *option = "default"; - int r; + int r, i; /* Put the card into administrative mode */ r = sc_pkcs15init_set_lifecycle(card, SC_CARDCTRL_LIFECYCLE_ADMIN); @@ -194,17 +199,16 @@ sc_pkcs15init_bind(struct sc_card *card, const char *name, return r; profile = sc_profile_new(); - profile->cbs = &callbacks; - if (!strcasecmp(driver, "GPK")) - profile->ops = &sc_pkcs15init_gpk_operations; - else if (!strcasecmp(driver, "MioCOS")) - profile->ops = &sc_pkcs15init_miocos_operations; - else if (!strcasecmp(driver, "flex")) - profile->ops = &sc_pkcs15init_cflex_operations; - else if (!strcasecmp(driver, "eToken")) - profile->ops = &sc_pkcs15init_etoken_operations; - else { + for (i = 0; profile_operations[i].name; i++) { + if (!strcasecmp(driver, profile_operations[i].name)) { + func = (struct sc_pkcs15init_operations * (*)(void)) profile_operations[i].func; + break; + } + } + if (func) { + profile->ops = func(); + } else { p15init_error("Unsupported card driver %s", driver); sc_profile_free(profile); return SC_ERROR_NOT_SUPPORTED; diff --git a/src/pkcs15init/pkcs15-miocos.c b/src/pkcs15init/pkcs15-miocos.c index 5348460e..8ac6e94f 100644 --- a/src/pkcs15init/pkcs15-miocos.c +++ b/src/pkcs15init/pkcs15-miocos.c @@ -199,10 +199,14 @@ miocos_new_key(struct sc_profile *profile, struct sc_card *card, return r; } -struct sc_pkcs15init_operations sc_pkcs15init_miocos_operations = { - NULL, - .init_app = miocos_init_app, - .new_pin = miocos_new_pin, - .new_key = miocos_new_key, - .new_file = miocos_new_file, -}; +static struct sc_pkcs15init_operations sc_pkcs15init_miocos_operations; + +struct sc_pkcs15init_operations *sc_pkcs15init_get_miocos_ops(void) +{ + sc_pkcs15init_miocos_operations.init_app = miocos_init_app; + sc_pkcs15init_miocos_operations.new_pin = miocos_new_pin; + sc_pkcs15init_miocos_operations.new_key = miocos_new_key; + sc_pkcs15init_miocos_operations.new_file = miocos_new_file; + + return &sc_pkcs15init_miocos_operations; +}