From 5123be2b85db185c5764c2bbcdf07b6beb739e34 Mon Sep 17 00:00:00 2001 From: nils Date: Fri, 16 Sep 2005 10:18:55 +0000 Subject: [PATCH] add two new functions sc_reader_t *sc_ctx_get_reader(sc_context_t *ctx, unsigned int i); unsigned int sc_ctx_get_reader_count(sc_context_t *ctx); to access the reader_count and the sc_reader objects (to avoid accessing the sc_context members directly). Use these functions in src/pkcs11 + error checking to avoid accessing invalid sc_reader objects. git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@2595 c6295689-39f2-0310-b995-f0e70906c6a9 --- src/libopensc/ctx.c | 12 ++++++++++++ src/libopensc/opensc.h | 17 +++++++++++++++++ src/pkcs11/pkcs11-global.c | 10 +++++++--- src/pkcs11/slot.c | 10 ++++++---- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c index 6b8641f3..b2ca0aa6 100644 --- a/src/libopensc/ctx.c +++ b/src/libopensc/ctx.c @@ -643,6 +643,18 @@ static void process_config_file(sc_context_t *ctx, struct _sc_ctx_options *opts) load_parameters(ctx, ctx->conf_blocks[i], opts); } +sc_reader_t *sc_ctx_get_reader(sc_context_t *ctx, unsigned int i) +{ + if (i >= (unsigned int)ctx->reader_count || i >= SC_MAX_READERS) + return NULL; + return ctx->reader[i]; +} + +unsigned int sc_ctx_get_reader_count(sc_context_t *ctx) +{ + return (unsigned int)ctx->reader_count; +} + int sc_establish_context(sc_context_t **ctx_out, const char *app_name) { const char *default_app = "default"; diff --git a/src/libopensc/opensc.h b/src/libopensc/opensc.h index b965ef05..063c3f31 100644 --- a/src/libopensc/opensc.h +++ b/src/libopensc/opensc.h @@ -678,6 +678,23 @@ int sc_establish_context(sc_context_t **ctx, const char *app_name); * @param ctx A pointer to the context structure to be released */ int sc_release_context(sc_context_t *ctx); + +/** + * Returns a pointer to the specified sc_reader_t object + * @param ctx OpenSC context + * @param i number of the reader structure to return (starting with 0) + * @return the requested sc_reader object or NULL if the index is + * not available + */ +sc_reader_t *sc_ctx_get_reader(sc_context_t *ctx, unsigned int i); + +/** + * Returns the number a available sc_reader objects + * @param ctx OpenSC context + * @return the number of available reader objects + */ +unsigned int sc_ctx_get_reader_count(sc_context_t *ctx); + /** * Forces the use of a specified card driver * @param ctx OpenSC context diff --git a/src/pkcs11/pkcs11-global.c b/src/pkcs11/pkcs11-global.c index eccc3831..6fbea19f 100644 --- a/src/pkcs11/pkcs11-global.c +++ b/src/pkcs11/pkcs11-global.c @@ -84,7 +84,7 @@ CK_RV C_Finalize(CK_VOID_PTR pReserved) } sc_debug(context, "Shutting down Cryptoki\n"); - for (i=0; ireader_count; i++) + for (i=0; i < (int)sc_ctx_get_reader_count(context); i++) card_removed(i); sc_release_context(context); @@ -361,8 +361,12 @@ CK_RV C_WaitForSlotEvent(CK_FLAGS flags, /* blocking/nonblocking flag */ || (flags & CKF_DONT_BLOCK)) goto out; - for (i = k = 0; i < context->reader_count; i++) { - reader = context->reader[i]; + for (i = k = 0; i < (int)sc_ctx_get_reader_count(context); i++) { + reader = sc_ctx_get_reader(context, i); + if (reader == NULL) { + rv = CKR_GENERAL_ERROR; + goto out; + } for (j = 0; j < reader->slot_count; j++, k++) { readers[k] = reader; slots[k] = j; diff --git a/src/pkcs11/slot.c b/src/pkcs11/slot.c index ab374d1f..5a90d376 100644 --- a/src/pkcs11/slot.c +++ b/src/pkcs11/slot.c @@ -83,10 +83,12 @@ CK_RV card_detect(int reader) sc_debug(context, "%d: Detecting smart card\n", reader); for (i = card->max_slots; i--; ) { struct sc_pkcs11_slot *slot; + sc_reader_t *rdr = sc_ctx_get_reader(context, (unsigned int)reader); + if (rdr == NULL) + return CKR_GENERAL_ERROR; slot = virtual_slots + card->first_slot + i; - strcpy_bp(slot->slot_info.slotDescription, - context->reader[reader]->name, 64); + strcpy_bp(slot->slot_info.slotDescription, rdr->name, 64); slot->reader = reader; } @@ -119,7 +121,7 @@ again: rc = sc_detect_card_presence(context->reader[reader], 0); /* Detect the card if it's not known already */ if (card->card == NULL) { sc_debug(context, "%d: Connecting to smart card\n", reader); - rc = sc_connect_card(context->reader[reader], 0, &card->card); + rc = sc_connect_card(sc_ctx_get_reader(context, reader), 0, &card->card); if (rc != SC_SUCCESS) return sc_to_cryptoki_error(rc, reader); } @@ -158,7 +160,7 @@ CK_RV __card_detect_all(int report_events) if (context == NULL_PTR) return CKR_CRYPTOKI_NOT_INITIALIZED; - for (i = 0; i < context->reader_count; i++) + for (i = 0; i < (int)sc_ctx_get_reader_count(context); i++) card_detect(i); if (!report_events) { CK_SLOT_ID id;