On 1/28/2011 11:58 AM, Martin Paljak wrote:
Hello,
On Jan 28, 2011, at 12:36 AM, Douglas E. Engert wrote:
The changes are large, and still not ready. The 3 changes I discussed on 1/19 
are
still in this patch. Martin is working on a different version of the "don't
run sc_ctx_detect_reader" part of this patch.

Martin, any idea when that will be ready?
The parts that remove sc_ctx_detect_reader calling when creating a context 
should be OK with the patch I sent before. There was no feedback from Brian but 
I assume it would work for him too.
Feel free to apply it if you need this or point out any deficiencies with this 
approach.


OK, I have tried your patch, and it should work for Brian. But with the cardmod
it is only passing in the pcsc_context_handle and pcsc_card_handle in the
ctx_parms, but the ctx_parametrs are only used in the sc_context_create
routine, and there is no easy way to pass them into the cardmod pcsc code.
without some extra routine.

Attached is a version of your patch which adds two void parameters to the
sc_ctx_detect_readers routine. The cardmod_detect_readers will then save these.

Since you are requiring applications to call sc_ctx_detect_readers, adding
two NULL parameters should not be an issue.

The other drivers, do not have a *_detect_readers routine.

--

 Douglas E. Engert  <deeng...@anl.gov>
 Argonne National Laboratory
 9700 South Cass Avenue
 Argonne, Illinois  60439
 (630) 252-5444
Index: src/tools/piv-tool.c
===================================================================
--- src/tools/piv-tool.c	(revision 5125)
+++ src/tools/piv-tool.c	(working copy)
@@ -480,7 +480,8 @@
 	const char *object_id = NULL;
 	const char *key_info = NULL;
 	const char *admin_info = NULL;
-		
+	sc_context_param_t ctx_param;
+
 	setbuf(stderr, NULL);
 	setbuf(stdout, NULL);
 
@@ -566,8 +567,11 @@
 		BIO_set_fp(bp,stdout,BIO_NOCLOSE);
 	}
 
-	r = sc_establish_context(&ctx, app_name);
-	if (r) {
+	memset(&ctx_param, 0, sizeof(sc_context_param_t));
+	ctx_param.app_name = app_name;
+
+	r = sc_context_create(&ctx, &ctx_param);
+	if (r != SC_SUCCESS) {
 		fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
 		return 1;
 	}
Index: src/tools/netkey-tool.c
===================================================================
--- src/tools/netkey-tool.c	(revision 5125)
+++ src/tools/netkey-tool.c	(working copy)
@@ -556,6 +556,8 @@
 		exit(1);
 	}
 
+	/* FIXME: use util_connect_card */
+	sc_ctx_detect_readers(ctx, NULL, NULL);
 	printf("%d Readers detected\n", sc_ctx_get_reader_count(ctx));
 	if(reader < 0 || reader >= (int)sc_ctx_get_reader_count(ctx)){
 		fprintf(stderr,"Cannot open reader %d\n", reader);
Index: src/tools/util.c
===================================================================
--- src/tools/util.c	(revision 5125)
+++ src/tools/util.c	(working copy)
@@ -11,6 +11,7 @@
 #include <ctype.h>
 #include "util.h"
 
+/** Handles the common options of utilities and connects to the right card, specified with --reader */
 int util_connect_card(sc_context_t *ctx, sc_card_t **cardp,
 		 const char *reader_id, int do_wait, int verbose)
 {
@@ -18,6 +19,8 @@
 	sc_card_t *card;
 	int r, tmp_reader_num;
 
+	/* Detecting for readers is the responsibility of the application */
+	sc_ctx_detect_readers(ctx, NULL, NULL);
 	if (do_wait) {
 		unsigned int event;
 
@@ -28,7 +31,7 @@
 				fprintf(stderr, "Error while waiting for a reader: %s\n", sc_strerror(r));
 				return 3;
 			}
-			r = sc_ctx_detect_readers(ctx);
+			r = sc_ctx_detect_readers(ctx, NULL, NULL);
 			if (r < 0) {
 				fprintf(stderr, "Error while refreshing readers: %s\n", sc_strerror(r));
 				return 3;
Index: src/pkcs11/pkcs11-global.c
===================================================================
--- src/pkcs11/pkcs11-global.c	(revision 5125)
+++ src/pkcs11/pkcs11-global.c	(working copy)
@@ -228,6 +228,10 @@
 		goto out;
 	}
 
+	rc = sc_ctx_detect_readers(context, NULL, NULL);
+	if (rc != SC_SUCCESS)
+		sc_log(context, "Warning: reader detection failed: %d (%s)", rc, sc_strerror(rc));
+
 	/* Load configuration */
 	load_pkcs11_parameters(&sc_pkcs11_conf, context);
 
@@ -378,7 +382,7 @@
 		/* 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); 
+		sc_ctx_detect_readers(context, NULL, NULL); 
 	}
 
 	card_detect_all();
Index: src/libopensc/reader-pcsc.c
===================================================================
--- src/libopensc/reader-pcsc.c	(revision 5125)
+++ src/libopensc/reader-pcsc.c	(working copy)
@@ -843,7 +843,7 @@
 	}
 }
 
-static int pcsc_detect_readers(sc_context_t *ctx)
+static int pcsc_detect_readers(sc_context_t *ctx, void * notused1, void * notused2)
 {
 	struct pcsc_global_private_data *gpriv = (struct pcsc_global_private_data *) ctx->reader_drv_data;
 	DWORD active_proto, reader_buf_size;
@@ -1659,7 +1659,7 @@
 	return SC_SUCCESS;
 }
 
-static int cardmod_detect_readers(sc_context_t *ctx)
+static int cardmod_detect_readers(sc_context_t *ctx, void * pcsc_context_handle, void * pcsc_card_handle)
 {
 	SCARDHANDLE card_handle;
 	u8 feature_buf[256], rbuf[SC_MAX_APDU_BUFFER_SIZE];
@@ -1683,38 +1683,9 @@
 
 	sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "Probing pcsc readers");
 
-	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\OpenSC Project\\Opensc",\
-		NULL, KEY_READ, &key)==ERROR_SUCCESS)
-	{
-		CHAR val[1024]; 
-		DWORD type;
-		LONG size = sizeof(val);
+	gpriv->pcsc_ctx = *(SCARDCONTEXT *)pcsc_context_handle;
+	card_handle =  *(SCARDHANDLE *)pcsc_card_handle;
 
-		if(RegQueryValueEx(key,"pcsc_ctx", NULL, &type, 
-			val, &size) == ERROR_SUCCESS)
-		{
-			if(type == REG_DWORD)
-			{
-				gpriv->pcsc_ctx = *(DWORD*)val;
-			}
-		}
-
-		if(RegQueryValueEx(key,"pcsc_card", NULL, &type, 
-			val, &size) == ERROR_SUCCESS)
-		{
-			if(type == REG_DWORD)
-			{
-				card_handle = *(DWORD*)val;
-			}
-		}
-		
-		RegCloseKey(key);
-	}
-	else
-	{
-		sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "Unable to open registry key Opensc");
-	}
-
 	sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "gpriv->pcsc_ctx = %X, card_handle = %X", gpriv->pcsc_ctx, card_handle);
 	
 	if(gpriv->SCardGetAttrib(card_handle, SCARD_ATTR_DEVICE_SYSTEM_NAME_A, \
@@ -1736,7 +1707,7 @@
 			ret = SC_ERROR_OUT_OF_MEMORY;
 			goto err1;
 		}
-		if ((priv = malloc(sizeof(struct pcsc_private_data))) == NULL) {
+		if ((priv = calloc(1, sizeof(struct pcsc_private_data))) == NULL) {
 			ret = SC_ERROR_OUT_OF_MEMORY;
 			goto err1;
 		}
Index: src/libopensc/ctx.c
===================================================================
--- src/libopensc/ctx.c	(revision 5125)
+++ src/libopensc/ctx.c	(working copy)
@@ -544,7 +544,7 @@
 		load_parameters(ctx, ctx->conf_blocks[i], opts);
 }
 
-int sc_ctx_detect_readers(sc_context_t *ctx)
+int sc_ctx_detect_readers(sc_context_t *ctx, void * pcsc_context_handle, void * pcsc_card_handle)
 {
 	int r = 0;
 	const struct sc_reader_driver *drv = ctx->reader_driver;
@@ -552,7 +552,7 @@
 	sc_mutex_lock(ctx, ctx->mutex);
 
 	if (drv->ops->detect_readers != NULL)
-		r = drv->ops->detect_readers(ctx);
+		r = drv->ops->detect_readers(ctx, pcsc_context_handle, pcsc_card_handle);
 	
 	sc_mutex_unlock(ctx, ctx->mutex);
 
@@ -579,16 +579,6 @@
 	return list_size(&ctx->readers);
 }
 
-int sc_establish_context(sc_context_t **ctx_out, const char *app_name)
-{
-	sc_context_param_t ctx_param;
-
-	memset(&ctx_param, 0, sizeof(sc_context_param_t));
-	ctx_param.ver      = 0;
-	ctx_param.app_name = app_name;
-	return sc_context_create(ctx_out, &ctx_param);
-}
-
 int sc_context_create(sc_context_t **ctx_out, const sc_context_param_t *parm)
 {
 	sc_context_t		*ctx;
@@ -660,7 +650,6 @@
 		free(opts.forced_card_driver);
 	}
 	del_drvs(&opts);
-	sc_ctx_detect_readers(ctx);
 	*ctx_out = ctx;
 	return SC_SUCCESS;
 }
Index: src/libopensc/libopensc.exports
===================================================================
--- src/libopensc/libopensc.exports	(revision 5125)
+++ src/libopensc/libopensc.exports	(working copy)
@@ -81,7 +81,6 @@
 _sc_debug
 sc_enum_apps
 sc_parse_ef_atr
-sc_establish_context
 sc_file_add_acl_entry
 sc_file_clear_acl_entries
 sc_file_dup
Index: src/libopensc/opensc.h
===================================================================
--- src/libopensc/opensc.h	(revision 5125)
+++ src/libopensc/opensc.h	(working copy)
@@ -365,7 +365,7 @@
 	int (*finish)(struct sc_context *ctx);
 	/* Called when library wish to detect new readers
 	 * should add only new readers. */
-	int (*detect_readers)(struct sc_context *ctx);
+	int (*detect_readers)(struct sc_context *ctx, void * context_handle, void * card_handle);
 	int (*cancel)(struct sc_context *ctx);
 	/* Called when releasing a reader.  release() has to
 	 * deallocate the private data.  Other fields will be
@@ -668,18 +668,9 @@
 /********************************************************************/
 
 /**
- * Establishes an OpenSC context. Note: this function is deprecated,
- * please use sc_context_create() instead.
- * @param ctx A pointer to a pointer that will receive the allocated context
- * @param app_name A string that identifies the application, used primarily
- *	in finding application-specific configuration data. Can be NULL.
- */
-int sc_establish_context(sc_context_t **ctx, const char *app_name);
-
-/**
  * @struct sc_context_t initialization parameters
- * Structure to supply additional parameters, for example
- * mutex information, to the sc_context_t creation.
+ * Structure to supply parameters like app name, mutex
+ * information and PC/SC handles to the sc_context_t creation.
  */
 typedef struct {
 	/** version number of this structure (0 for this version) */
@@ -688,13 +679,17 @@
 	 *  dependend configuration data). If NULL the name "default"
 	 *  will be used. */
 	const char    *app_name;
-	/** flags, currently unused */
-	unsigned long flags;
 	/** mutex functions to use (optional) */
 	sc_thread_context_t *thread_ctx;
 } sc_context_param_t;
+
 /**
- * Creates a new sc_context_t object.
+ * Creates a new sc_context_t object. This initializes all available drivers,
+ * parses the configuration file and creates necessary in-memory structures.
+ * Probing for smart card readers with sc_ctx_detect_readers is needed before
+ * accessing smart cards is possible at which time existing PC/SC handles can
+ * be passed in to cardmod pcsc driver.
+ *
  * @param  ctx   pointer to a sc_context_t pointer for the newly
  *               created sc_context_t object.
  * @param  parm  parameters for the sc_context_t creation (see 
@@ -713,9 +708,11 @@
 /**
  * Detect new readers available on system.
  * @param  ctx  OpenSC context
+ * @param  context_handle NULL or used by windows cardmod minidriver to pass in handle.
+ * @param  card_handle NULL or used by windows cardmod minidriver to pass in handle.
  * @return SC_SUCCESS on success and an error code otherwise.
  */
-int sc_ctx_detect_readers(sc_context_t *ctx);
+int sc_ctx_detect_readers(sc_context_t *ctx, void * context_handle, void * card_handle);
 
 /**
  * Returns a pointer to the specified sc_reader_t object
_______________________________________________
opensc-devel mailing list
opensc-devel@lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc-devel

Reply via email to