Ejemplo n.º 1
0
int sc_context_create(sc_context_t **ctx_out, const sc_context_param_t *parm)
{
	sc_context_t		*ctx;
	struct _sc_ctx_options	opts;
	int			r;

	if (ctx_out == NULL || parm == NULL)
		return SC_ERROR_INVALID_ARGUMENTS;

	ctx = calloc(1, sizeof(sc_context_t));
	if (ctx == NULL)
		return SC_ERROR_OUT_OF_MEMORY;
	memset(&opts, 0, sizeof(opts));

	/* set the application name if set in the parameter options */
	if (parm->app_name != NULL)
		ctx->app_name = strdup(parm->app_name);
	else
		ctx->app_name = strdup("default");
	if (ctx->app_name == NULL) {
		sc_release_context(ctx);
		return SC_ERROR_OUT_OF_MEMORY;
	}
	
	set_defaults(ctx, &opts);
	list_init(&ctx->readers);
	list_attributes_seeker(&ctx->readers, reader_list_seeker);
	/* set thread context and create mutex object (if specified) */
	if (parm->thread_ctx != NULL)
		ctx->thread_ctx = parm->thread_ctx;
	r = sc_mutex_create(ctx, &ctx->mutex);
	if (r != SC_SUCCESS) {
		sc_release_context(ctx);
		return r;
	}

	process_config_file(ctx, &opts);
	sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "==================================="); /* first thing in the log */
	sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "opensc version: %s", sc_get_version());

#ifdef HAVE_LTDL_H
	/* initialize ltdl, if available. See scdl.c for more information */
	if (lt_dlinit() != 0) {
		sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "lt_dlinit() failed");
		sc_release_context(ctx);
		return SC_ERROR_INTERNAL;
	}
#endif

#ifdef ENABLE_PCSC
	ctx->reader_driver = sc_get_pcsc_driver();
/* XXX: remove cardmod pseudoreader driver */
#ifdef ENABLE_MINIDRIVER
	if(strcmp(ctx->app_name, "cardmod") == 0) {
		ctx->reader_driver = sc_get_cardmod_driver();
	}
#endif
#elif ENABLE_CTAPI
	ctx->reader_driver = sc_get_ctapi_driver();
#elif ENABLE_OPENCT
	ctx->reader_driver = sc_get_openct_driver();
#endif

	load_reader_driver_options(ctx);
	ctx->reader_driver->ops->init(ctx);
	
	load_card_drivers(ctx, &opts);
	load_card_atrs(ctx);
	if (opts.forced_card_driver) {
		/* FIXME: check return value? */
		sc_set_card_driver(ctx, opts.forced_card_driver);
		free(opts.forced_card_driver);
	}
	del_drvs(&opts);
	sc_ctx_detect_readers(ctx);
	*ctx_out = ctx;
	return SC_SUCCESS;
}
Ejemplo n.º 2
0
CK_RV C_GetSlotList(CK_BBOOL       tokenPresent,  /* only slots with token present */
		    CK_SLOT_ID_PTR pSlotList,     /* receives the array of slot IDs */
		    CK_ULONG_PTR   pulCount)      /* receives the number of slots */
{
	CK_SLOT_ID_PTR found = NULL;
	unsigned int i;
	CK_ULONG numMatches;
	sc_pkcs11_slot_t *slot;
	sc_reader_t *prev_reader = NULL;
	CK_RV rv;

	if (pulCount == NULL_PTR)
		return CKR_ARGUMENTS_BAD;

	rv = sc_pkcs11_lock();
	if (rv != CKR_OK)
		return rv;

	sc_log(context, "C_GetSlotList(token=%d, %s)", tokenPresent,
		 (pSlotList==NULL_PTR && sc_pkcs11_conf.plug_and_play)? "plug-n-play":"refresh");

	/* Slot list can only change in v2.20 */
	if (pSlotList == NULL_PTR && sc_pkcs11_conf.plug_and_play) {
		/* Trick NSS into updating the slot list by changing the hotplug slot ID */
		sc_pkcs11_slot_t *hotplug_slot = list_get_at(&virtual_slots, 0);
		hotplug_slot->id--;
		sc_ctx_detect_readers(context);
	}

	card_detect_all();

	found = calloc(list_size(&virtual_slots), sizeof(CK_SLOT_ID));

	if (found == NULL) {
		rv = CKR_HOST_MEMORY;
		goto out;
	}

	prev_reader = NULL;
	numMatches = 0;
	for (i=0; i<list_size(&virtual_slots); i++) {
	        slot = (sc_pkcs11_slot_t *) list_get_at(&virtual_slots, i);
		/* the list of available slots contains:
		 * - if present, virtual hotplug slot;
		 * - any slot with token;
		 * - without token(s), one empty slot per reader;
		 */
	        if ((!tokenPresent && !slot->reader)
				|| (!tokenPresent && slot->reader != prev_reader)
				|| (slot->slot_info.flags & CKF_TOKEN_PRESENT))
			found[numMatches++] = slot->id;
		prev_reader = slot->reader;
	}

	if (pSlotList == NULL_PTR) {
		sc_log(context, "was only a size inquiry (%d)\n", numMatches);
		*pulCount = numMatches;
		rv = CKR_OK;
		goto out;
	}

	if (*pulCount < numMatches) {
		sc_log(context, "buffer was too small (needed %d)\n", numMatches);
		*pulCount = numMatches;
		rv = CKR_BUFFER_TOO_SMALL;
		goto out;
	}

	memcpy(pSlotList, found, numMatches * sizeof(CK_SLOT_ID));
	*pulCount = numMatches;
	rv = CKR_OK;

	sc_log(context, "returned %d slots\n", numMatches);

out:
	if (found != NULL) {
		free (found);
		found = NULL;
	}
	sc_pkcs11_unlock();
	return rv;
}
Ejemplo n.º 3
0
Archivo: ctx.c Proyecto: AktivCo/OpenSC
int sc_context_create(sc_context_t **ctx_out, const sc_context_param_t *parm)
{
	sc_context_t		*ctx;
	struct _sc_ctx_options	opts;
	int			r;
	char			*driver;

	if (ctx_out == NULL || parm == NULL)
		return SC_ERROR_INVALID_ARGUMENTS;

	ctx = calloc(1, sizeof(sc_context_t));
	if (ctx == NULL)
		return SC_ERROR_OUT_OF_MEMORY;
	memset(&opts, 0, sizeof(opts));

	/* set the application name if set in the parameter options */
	if (parm->app_name != NULL)
		ctx->app_name = strdup(parm->app_name);
	else
		ctx->app_name = strdup("default");
	if (ctx->app_name == NULL) {
		sc_release_context(ctx);
		return SC_ERROR_OUT_OF_MEMORY;
	}

	ctx->flags = parm->flags;
	set_defaults(ctx, &opts);

	if (0 != list_init(&ctx->readers)) {
		sc_release_context(ctx);
		return SC_ERROR_OUT_OF_MEMORY;
	}
	list_attributes_seeker(&ctx->readers, reader_list_seeker);
	/* set thread context and create mutex object (if specified) */
	if (parm->thread_ctx != NULL)
		ctx->thread_ctx = parm->thread_ctx;
	r = sc_mutex_create(ctx, &ctx->mutex);
	if (r != SC_SUCCESS) {
		sc_release_context(ctx);
		return r;
	}

#if defined(ENABLE_OPENSSL) && defined(OPENSSL_SECURE_MALLOC_SIZE)
	if (!CRYPTO_secure_malloc_initialized()) {
		CRYPTO_secure_malloc_init(OPENSSL_SECURE_MALLOC_SIZE, OPENSSL_SECURE_MALLOC_SIZE/8);
	}
#endif

	process_config_file(ctx, &opts);
	sc_log(ctx, "==================================="); /* first thing in the log */
	sc_log(ctx, "opensc version: %s", sc_get_version());

#ifdef ENABLE_PCSC
	ctx->reader_driver = sc_get_pcsc_driver();
#elif defined(ENABLE_CRYPTOTOKENKIT)
	ctx->reader_driver = sc_get_cryptotokenkit_driver();
#elif defined(ENABLE_CTAPI)
	ctx->reader_driver = sc_get_ctapi_driver();
#elif defined(ENABLE_OPENCT)
	ctx->reader_driver = sc_get_openct_driver();
#endif

	r = ctx->reader_driver->ops->init(ctx);
	if (r != SC_SUCCESS)   {
		sc_release_context(ctx);
		return r;
	}

	driver = getenv("OPENSC_DRIVER");
	if (driver) {
		scconf_list *list = NULL;
		scconf_list_add(&list, driver);
		set_drivers(&opts, list);
		scconf_list_destroy(list);
	}

	load_card_drivers(ctx, &opts);
	load_card_atrs(ctx);

	del_drvs(&opts);
	sc_ctx_detect_readers(ctx);
	*ctx_out = ctx;

	return SC_SUCCESS;
}