From b80890d49ce5b8a687a6c064544121dfd6cd3cd2 Mon Sep 17 00:00:00 2001 From: aet Date: Sun, 20 Feb 2005 08:26:27 +0000 Subject: [PATCH] - Increase SC_MAX_READER_DRIVERS / SC_MAX_CARD_DRIVERS - Some cleanups before future commits git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@2191 c6295689-39f2-0310-b995-f0e70906c6a9 --- etc/opensc.conf.example | 49 +++++++++++----------- src/libopensc/ctx.c | 85 ++++++++++++++++++-------------------- src/libopensc/internal.h | 12 +++--- src/libopensc/opensc.h | 59 ++++++++++++++------------ src/libopensc/pkcs15-syn.c | 18 ++++---- 5 files changed, 113 insertions(+), 110 deletions(-) diff --git a/etc/opensc.conf.example b/etc/opensc.conf.example index 671ba989..f02e615d 100644 --- a/etc/opensc.conf.example +++ b/etc/opensc.conf.example @@ -35,7 +35,7 @@ app default { # internal) driver is supplied, a separate configuration # configuration block has to be written for the driver. # Default: internal - # NOTE: if "internal" keyword is used, must be the + # NOTE: if "internal" keyword is used, must be the # last entry in reader_drivers list # # reader_drivers = openct, pcsc, ctapi; @@ -86,7 +86,7 @@ app default { # # EXPERIMENTAL: Enable CCID pinpad support # implemented (at least) in the libccid driver. - #use_ccid_pin_cmd = true; + # use_ccid_pin_cmd = true; } # What card drivers to load at start-up @@ -96,15 +96,15 @@ app default { # internal) driver is supplied, a separate configuration # configuration block has to be written for the driver. # Default: internal - # NOTE: When "internal" keyword is used, must be last entry + # NOTE: When "internal" keyword is used, must be last entry # # card_drivers = customcos, internal; - # Card driver configuration blocks. + # Card driver configuration blocks. # For all drivers, you can specify ATRs of cards that # should be handled by this driver (in addition to the - # list of compiled-in ATRs). + # list of compiled-in ATRs). # # The supported internal card driver names are # flex Cryptoflex/Multiflex @@ -122,7 +122,7 @@ app default { # atr = 00:11:22; } - # For card drivers loaded from an external shared library/DLL, + # For card drivers loaded from an external shared library/DLL, # you need to specify the path name of the module # # card_driver customcos { @@ -139,7 +139,7 @@ app default { # # Default: autodetect # - # force_card_driver = miocos; + # force_card_driver = customcos; # Below are the framework specific configuration blocks. @@ -148,42 +148,43 @@ app default { # Whether to use the cache files in the user's # home directory. # - # At the moment you have to 'teach' the card to the - # system by: - # pkcs15-tool -L + # At the moment you have to 'teach' the card + # to the system by running command: pkcs15-tool -L # # WARNING: Caching shouldn't be used in setuid root # applications. # Default: false # use_caching = true; - # Enable pkcs15 emulation + # Enable pkcs15 emulation. # Default: yes enable_pkcs15_emulation = yes; - # Try pkcs15 emulation code first (before the normal - # pkcs15 processing). + # Prefer pkcs15 emulation code before + # the normal pkcs15 processing. # Default: no try_emulation_first = no; - # Enable builtin emulators + # Enable builtin emulators. # Default: yes enable_builtin_emulation = yes; # list of the builtin pkcs15 emulators to test - # possible values: esteid, openpgp, netkey, netkey, - # starcert, infocamere, postecert builtin_emulators = esteid, openpgp, netkey, netkey, starcert, infocamere, postecert; - # additional pkcs15 emulators (dynamic or builtin with - # a different atr etc.) - # emulate foo { + # additional settings per driver + # + # For pkcs15 emulators loaded from an external shared library/DLL, + # you need to specify the path name of the module + # + # emulate custom { + # The location of the driver library # module = builtin; # atr = 11:22:33:44; - #} + # } } - + # Estonian ID card and Micardo driver currently play together with T=0 only. # In theory only the 'cold' ATR should be specified, as T=0 will be the preferred # protocol once you boot it up with T=0, but be paranoid. - + # Generic format: card_atr # Only parameter currently understood is force_protocol card_atr 3b:6e:00:ff:45:73:74:45:49:44:20:76:65:72:20:31:2e:30 { @@ -269,7 +270,7 @@ app pkcs11-spy { # variable is used. And if that one isn't defined: stderr # is used. # - #output = /tmp/pkcs11-spy.log; + # output = /tmp/pkcs11-spy.log; # Which PKCS11 module to load. # @@ -277,6 +278,6 @@ app pkcs11-spy { # variable is used. And if that one isn't defined, # opensc-pkcs11.so is used. # - #module = opensc-pkcs11.so; + # module = opensc-pkcs11.so; } } diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c index 138e7b21..993badaf 100644 --- a/src/libopensc/ctx.c +++ b/src/libopensc/ctx.c @@ -26,7 +26,6 @@ #include #include #include - #include /* Default value for apdu_masquerade option */ @@ -45,62 +44,60 @@ int _sc_add_reader(struct sc_context *ctx, struct sc_reader *reader) ctx->reader[ctx->reader_count] = reader; ctx->reader_count++; - return 0; + return SC_SUCCESS; } struct _sc_driver_entry { char *name; void *func; - char *libpath; }; static const struct _sc_driver_entry internal_card_drivers[] = { - { "etoken", (void *) sc_get_etoken_driver, NULL }, - { "flex", (void *) sc_get_cryptoflex_driver, NULL }, - { "cyberflex", (void *) sc_get_cyberflex_driver, NULL }, + { "etoken", (void *) sc_get_etoken_driver }, + { "flex", (void *) sc_get_cryptoflex_driver }, + { "cyberflex", (void *) sc_get_cyberflex_driver }, #ifdef HAVE_OPENSSL - { "gpk", (void *) sc_get_gpk_driver, NULL }, + { "gpk", (void *) sc_get_gpk_driver }, #endif - { "miocos", (void *) sc_get_miocos_driver, NULL }, - { "mcrd", (void *) sc_get_mcrd_driver, NULL }, - { "setcos", (void *) sc_get_setcos_driver, NULL }, - { "starcos", (void *) sc_get_starcos_driver, NULL }, - { "tcos", (void *) sc_get_tcos_driver, NULL }, - { "opengpg", (void *) sc_get_openpgp_driver, NULL }, - { "jcop", (void *) sc_get_jcop_driver, NULL }, + { "miocos", (void *) sc_get_miocos_driver }, + { "mcrd", (void *) sc_get_mcrd_driver }, + { "setcos", (void *) sc_get_setcos_driver }, + { "starcos", (void *) sc_get_starcos_driver }, + { "tcos", (void *) sc_get_tcos_driver }, + { "opengpg", (void *) sc_get_openpgp_driver }, + { "jcop", (void *) sc_get_jcop_driver }, #ifdef HAVE_OPENSSL - { "oberthur", (void *) sc_get_oberthur_driver, NULL }, + { "oberthur", (void *) sc_get_oberthur_driver }, #endif - { "belpic", (void *) sc_get_belpic_driver, NULL }, - { "emv", (void *) sc_get_emv_driver, NULL }, + { "belpic", (void *) sc_get_belpic_driver }, + { "emv", (void *) sc_get_emv_driver }, /* The default driver should be last, as it handles all the * unrecognized cards. */ - { "default", (void *) sc_get_default_driver, NULL }, - { NULL, NULL, NULL } + { "default", (void *) sc_get_default_driver }, + { NULL, NULL } }; static const struct _sc_driver_entry internal_reader_drivers[] = { #if defined(HAVE_PCSC) - { "pcsc", (void *) sc_get_pcsc_driver, NULL }, + { "pcsc", (void *) sc_get_pcsc_driver }, #endif - { "ctapi", (void *) sc_get_ctapi_driver, NULL }, + { "ctapi", (void *) sc_get_ctapi_driver }, #ifndef _WIN32 #ifdef HAVE_OPENCT - { "openct", (void *) sc_get_openct_driver, NULL }, + { "openct", (void *) sc_get_openct_driver }, #endif #endif - { NULL, NULL, NULL } + { NULL, NULL } }; struct _sc_ctx_options { - struct _sc_driver_entry rdrv[16]; + struct _sc_driver_entry rdrv[SC_MAX_READER_DRIVERS]; int rcount; - struct _sc_driver_entry cdrv[16]; + struct _sc_driver_entry cdrv[SC_MAX_CARD_DRIVERS]; int ccount; char *forced_card_driver; }; - static void del_drvs(struct _sc_ctx_options *opts, int type) { struct _sc_driver_entry *lst; @@ -115,8 +112,6 @@ static void del_drvs(struct _sc_ctx_options *opts, int type) } for (i = 0; i < *cp; i++) { free(lst[i].name); - if (lst[i].libpath) - free(lst[i].libpath); } *cp = 0; } @@ -124,16 +119,18 @@ static void del_drvs(struct _sc_ctx_options *opts, int type) static void add_drv(struct _sc_ctx_options *opts, int type, const char *name) { struct _sc_driver_entry *lst; - int *cp, i; + int *cp, max, i; if (type == 0) { lst = opts->rdrv; cp = &opts->rcount; + max = SC_MAX_READER_DRIVERS; } else { lst = opts->cdrv; cp = &opts->ccount; + max = SC_MAX_CARD_DRIVERS; } - if (*cp == 16) /* No space for more drivers... */ + if (*cp == max) /* No space for more drivers... */ return; for (i = 0; i < *cp; i++) if (strcmp(name, lst[i].name) == 0) @@ -179,8 +176,7 @@ static int load_parameters(struct sc_context *ctx, scconf_block *block, { int err = 0; const scconf_list *list; - const char *val; - const char *s_internal = "internal"; + const char *val, *s_internal = "internal"; ctx->debug = scconf_get_int(block, "debug", ctx->debug); val = scconf_get_str(block, "debug_file", NULL); @@ -328,7 +324,9 @@ static const char *find_library(struct sc_context *ctx, const char *name, int ty * that returns a pointer to the function _sc_get_xxxx_driver() * used to initialize static modules * Also, an exported "char *sc_module_version" variable should exist in module - * type=1 -> carddriver Type=0 -> readerdriver + * + * type == 0 -> reader driver + * type == 1 -> card driver */ static void *load_dynamic_driver(struct sc_context *ctx, void **dll, const char *name, int type) @@ -407,9 +405,9 @@ static int load_reader_drivers(struct sc_context *ctx, driver->ops->init(ctx, &ctx->reader_drv_data[i]); ctx->reader_drivers[drv_count] = driver; - drv_count++; + drv_count++; } - return 0; + return SC_SUCCESS; } static int load_card_driver_options(struct sc_context *ctx, @@ -439,8 +437,7 @@ static int load_card_driver_options(struct sc_context *ctx, list = list->next; } } - - return 0; + return SC_SUCCESS; } static int load_card_drivers(struct sc_context *ctx, @@ -479,9 +476,9 @@ static int load_card_drivers(struct sc_context *ctx, ctx->card_drivers[drv_count]->natrs = 0; load_card_driver_options(ctx, ctx->card_drivers[drv_count]); - drv_count++; + drv_count++; } - return 0; + return SC_SUCCESS; } static void process_config_file(struct sc_context *ctx, struct _sc_ctx_options *opts) @@ -572,7 +569,7 @@ int sc_establish_context(struct sc_context **ctx_out, const char *app_name) return SC_ERROR_NO_READERS_FOUND; } *ctx_out = ctx; - return 0; + return SC_SUCCESS; } int sc_release_context(struct sc_context *ctx) @@ -616,7 +613,7 @@ int sc_release_context(struct sc_context *ctx) free(ctx->app_name); memset(ctx, 0, sizeof(*ctx)); free(ctx); - return 0; + return SC_SUCCESS; } int sc_set_card_driver(struct sc_context *ctx, const char *short_name) @@ -640,7 +637,7 @@ int sc_set_card_driver(struct sc_context *ctx, const char *short_name) sc_mutex_unlock(ctx->mutex); if (match == 0) return SC_ERROR_OBJECT_NOT_FOUND; /* FIXME: invent error */ - return 0; + return SC_SUCCESS; } int sc_get_cache_dir(struct sc_context *ctx, char *buf, size_t bufsize) @@ -668,7 +665,7 @@ int sc_get_cache_dir(struct sc_context *ctx, char *buf, size_t bufsize) return SC_ERROR_INTERNAL; if (snprintf(buf, bufsize, "%s/%s", homedir, cache_dir) < 0) return SC_ERROR_BUFFER_TOO_SMALL; - return 0; + return SC_SUCCESS; } int sc_make_cache_dir(struct sc_context *ctx) @@ -701,7 +698,7 @@ int sc_make_cache_dir(struct sc_context *ctx) if (mkdir(dirname, 0700) < 0) goto failed; } - return 0; + return SC_SUCCESS; /* for lack of a better return code */ failed: sc_error(ctx, "failed to create cache directory\n"); diff --git a/src/libopensc/internal.h b/src/libopensc/internal.h index 4221de55..6c33e09e 100644 --- a/src/libopensc/internal.h +++ b/src/libopensc/internal.h @@ -40,11 +40,13 @@ extern "C" { #define SC_CTX_MAGIC 0x0A550335 struct sc_atr_table { - char *atr; /* The atr fields are required to - be in aa:bb:cc hex format. */ - char *atrmask; /* The atrmask is logically AND'd with an - card atr prior to comparison with the - atr reference value above. */ + /* The atr fields are required to + * be in aa:bb:cc hex format. */ + char *atr; + /* The atrmask is logically AND'd with an + * card atr prior to comparison with the + * atr reference value above. */ + char *atrmask; char *name; int type; unsigned long flags; diff --git a/src/libopensc/opensc.h b/src/libopensc/opensc.h index 6afea7a5..b71b1204 100644 --- a/src/libopensc/opensc.h +++ b/src/libopensc/opensc.h @@ -18,11 +18,11 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/** +/** * @file opensc.h * @brief OpenSC library core header file */ - + #ifndef _OPENSC_H #define _OPENSC_H @@ -92,7 +92,7 @@ extern "C" { #define SC_AC_OP_CRYPTO 7 /* If you add more OPs here, make sure you increase * SC_MAX_AC_OPS in types.h */ - + /* Operations relating to access control (in case of EF) */ #define SC_AC_OP_READ 0 #define SC_AC_OP_UPDATE 1 @@ -107,10 +107,10 @@ extern "C" { #define SC_RECORD_CURRENT 0 /* various maximum values */ -#define SC_MAX_CARD_DRIVERS 16 -#define SC_MAX_READER_DRIVERS 4 -#define SC_MAX_CARD_DRIVER_SNAME_SIZE 16 +#define SC_MAX_READER_DRIVERS 6 #define SC_MAX_READERS 16 +#define SC_MAX_CARD_DRIVERS 32 +#define SC_MAX_CARD_DRIVER_SNAME_SIZE 16 #define SC_MAX_SLOTS 4 #define SC_MAX_CARD_APPS 8 #define SC_MAX_APDU_BUFFER_SIZE 258 @@ -159,7 +159,7 @@ extern "C" { #define SC_ALGORITHM_SPECIFIC_FLAGS 0x0000FFFF #define SC_ALGORITHM_RSA_RAW 0x00000001 -/* If the card is willing to produce a cryptogram padded with the following +/* If the card is willing to produce a cryptogram padded with the following * methods, set these flags accordingly. */ #define SC_ALGORITHM_RSA_PADS 0x0000000E #define SC_ALGORITHM_RSA_PAD_NONE 0x00000000 @@ -167,7 +167,7 @@ extern "C" { #define SC_ALGORITHM_RSA_PAD_ANSI 0x00000004 #define SC_ALGORITHM_RSA_PAD_ISO9796 0x00000008 -/* If the card is willing to produce a cryptogram with the following +/* If the card is willing to produce a cryptogram with the following * hash values, set these flags accordingly. */ #define SC_ALGORITHM_RSA_HASH_NONE 0x00000010 #define SC_ALGORITHM_RSA_HASHES 0x000001E0 @@ -241,7 +241,7 @@ struct sc_app_info { struct sc_path path; u8 *ddo; size_t ddo_len; - + const char *desc; /* App description, if known */ int rec_nr; /* -1, if EF(DIR) is transparent */ }; @@ -278,7 +278,7 @@ struct sc_reader_driver { #define SC_SLOT_CAP_PIN_PAD 0x00000002 struct sc_slot_info { - int id; + int id; unsigned long flags, capabilities; unsigned int supported_protocols, active_protocol; u8 atr[SC_MAX_ATR_SIZE]; @@ -399,7 +399,7 @@ struct sc_reader_operations { struct sc_pin_cmd_data *); /* Wait for an event */ - int (*wait_for_event)(struct sc_reader **readers, + int (*wait_for_event)(struct sc_reader **readers, struct sc_slot_info **slots, size_t nslots, unsigned int event_mask, @@ -425,18 +425,23 @@ void sc_mutex_free(struct sc_mutex *p); /* none yet */ /* - * Card capabilities + * Card capabilities */ -/* SC_CARD_APDU_EXT: Card can handle large (> 256 bytes) buffers in - * calls to read_binary, write_binary and update_binary; if not, - * several successive calls to the corresponding function is made. */ + +/* Card can handle large (> 256 bytes) buffers in calls to + * read_binary, write_binary and update_binary; if not, + * several successive calls to the corresponding function + * is made. */ #define SC_CARD_CAP_APDU_EXT 0x00000001 -/* SC_CARD_CAP_EMV: Card can handle operations specified in the + +/* Card can handle operations specified in the * EMV 4.0 standard. */ #define SC_CARD_CAP_EMV 0x00000002 -/* SC_CARD_CAP_RNG: Card has on-board random number source */ + +/* Card has on-board random number source. */ #define SC_CARD_CAP_RNG 0x00000004 -/* The card doesn't return any File Control Info */ + +/* Card doesn't return any File Control Info. */ #define SC_CARD_CAP_NO_FCI 0x00000008 struct sc_card { @@ -456,10 +461,10 @@ struct sc_card { struct sc_app_info *app[SC_MAX_CARD_APPS]; int app_count; struct sc_file *ef_dir; - + struct sc_algorithm_info *algorithms; int algorithm_count; - + int lock_count; struct sc_card_driver *driver; @@ -552,7 +557,7 @@ struct sc_card_operations { * restore_security_env. */ int (*decipher)(struct sc_card *card, const u8 * crgram, size_t crgram_len, u8 * out, size_t outlen); - + /* compute_signature: Generates a digital signature on the card. Similiar * to the function decipher. */ int (*compute_signature)(struct sc_card *card, const u8 * data, @@ -575,7 +580,7 @@ struct sc_card_operations { * writes the corresponding file identifiers to . Returns * the number of bytes stored. */ int (*list_files)(struct sc_card *card, u8 *buf, size_t buflen); - + int (*check_sw)(struct sc_card *card, int sw1, int sw2); int (*card_ctl)(struct sc_card *card, unsigned long request, void *data); @@ -615,13 +620,13 @@ struct sc_context { FILE *debug_file, *error_file; char *preferred_language; - const struct sc_reader_driver *reader_drivers[SC_MAX_READER_DRIVERS+1]; + const struct sc_reader_driver *reader_drivers[SC_MAX_READER_DRIVERS]; void *reader_drv_data[SC_MAX_READER_DRIVERS]; - + struct sc_reader *reader[SC_MAX_READERS]; int reader_count; - - struct sc_card_driver *card_drivers[SC_MAX_CARD_DRIVERS+1]; + + struct sc_card_driver *card_drivers[SC_MAX_CARD_DRIVERS]; struct sc_card_driver *forced_driver; sc_mutex_t *mutex; @@ -727,7 +732,7 @@ int sc_wait_for_event(struct sc_reader **readers, int *slots, size_t nslots, * @retval SC_SUCCESS on success */ int sc_lock(struct sc_card *card); -/** +/** * Unlocks a previously locked card. After the lock count drops to zero, * the card is again placed in shared mode, where other processes * may access or lock it. diff --git a/src/libopensc/pkcs15-syn.c b/src/libopensc/pkcs15-syn.c index 96521c50..b23ed765 100644 --- a/src/libopensc/pkcs15-syn.c +++ b/src/libopensc/pkcs15-syn.c @@ -26,7 +26,6 @@ #include #include #include - #include extern int sc_pkcs15emu_openpgp_init_ex(sc_pkcs15_card_t *, @@ -71,10 +70,9 @@ sc_pkcs15_bind_synthetic(sc_pkcs15_card_t *p15card) int i, r = SC_ERROR_WRONG_CARD; SC_FUNC_CALLED(ctx, 1); - memset(&opts, 0, sizeof(opts)); - conf_block = NULL; + for (i = 0; ctx->conf_blocks[i] != NULL; i++) { blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i], "framework", "pkcs15"); @@ -96,6 +94,7 @@ sc_pkcs15_bind_synthetic(sc_pkcs15_card_t *p15card) } else { /* we have a conf file => let's use it */ const scconf_list *list, *item; + /* find out if the internal drivers should be used */ i = scconf_get_bool(conf_block, "enable_builtin_emulation", 1); if (i) { @@ -115,10 +114,10 @@ sc_pkcs15_bind_synthetic(sc_pkcs15_card_t *p15card) } } } + /* search for 'emulate foo { ... }' entries in the conf file */ sc_debug(ctx, "searching for 'emulate foo { ... }' blocks\n"); blocks = scconf_find_blocks(ctx->conf, conf_block, "emulate", NULL); - for (i = 0; (blk = blocks[i]) != NULL; i++) { const char *name = blk->name->data; sc_debug(ctx, "trying %s\n", name); @@ -187,28 +186,27 @@ static int parse_emu_block(sc_pkcs15_card_t *p15card, scconf_block *conf) int (*init_func)(sc_pkcs15_card_t *); int (*init_func_ex)(sc_pkcs15_card_t *, sc_pkcs15emu_opt_t *); int r; - const char *module_name; + const char *driver, *module_name; + driver = conf->name->data; r = emu_detect_card(card, conf); if (!r) return SC_ERROR_WRONG_CARD; init_func = NULL; init_func_ex = NULL; + + memset(&opts, 0, sizeof(opts)); opts.blk = conf; opts.flags = SC_PKCS15EMU_FLAGS_NO_CHECK; module_name = scconf_get_str(conf, "module", builtin_name); - if (!strcmp(module_name, "builtin")) { int i; /* This function is built into libopensc itself. * Look it up in the table of emulators */ - if (!conf->name) - return SC_ERROR_INTERNAL; - - module_name = conf->name->data; + module_name = driver; for (i = 0; builtin_emulators[i].name; i++) { if (!strcmp(builtin_emulators[i].name, module_name)) { init_func_ex = builtin_emulators[i].handler;