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
This commit is contained in:
nils 2005-09-16 10:18:55 +00:00
parent aec4a81304
commit 5123be2b85
4 changed files with 42 additions and 7 deletions

View File

@ -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";

View File

@ -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

View File

@ -84,7 +84,7 @@ CK_RV C_Finalize(CK_VOID_PTR pReserved)
}
sc_debug(context, "Shutting down Cryptoki\n");
for (i=0; i<context->reader_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;

View File

@ -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;