Attached is a patch to the cardmod code to do the following:
(1) Fix an uninitialized stricture by by using calloc in stead of malloc. With out this it was having problems with checking the status on the reader. (2) Implement a change that this should work for Brian Thomas that allows one to not detect the reader when creating the sc_context. This uses the SC_CONTEXT_PARAM_DONT_DETECT_READERS flag in the ctx_params (The more I think about this, this should be the default to not detect the readers during the sc_create_ctx, as a flag on opensc.conf could control this instead.) (2) Eliminate the storing of the SCARDCONTEXT and SCARDHANDLE in the registry. Instead, cardmod.c will store them in its VENDOR_SCPECIFIC structure, call sc_context_create with the SC_CONTEXT_PARAM_DONT_DETECT_READERS flag set in the ctx_params. It will then modify the ctx, and then call sc_ctx_detect_readers that will call the cardmod code in reader-pcsc.c that will use the SCARDCONTEXT and SCARDHANDLE. I can get this to work in Vista with certutil -SCinfo, but there is some issue as is says it can not open the key. I think this is an issue with using a 39 character key container name and a constant for a serial number. It does not work with login or runas. This may be the same issue with after reading the certificate, it is not registering the container so it can be used later. The patch is against 0.12.0. and was built on Ubuntu. I would hope Brian and François could look this over to see if it should be committed. -- Douglas E. Engert <deeng...@anl.gov> Argonne National Laboratory 9700 South Cass Avenue Argonne, Illinois 60439 (630) 252-5444
--- ./src/cardmod/,cardmod.c Wed Dec 22 11:14:36 2010 +++ ./src/cardmod/cardmod.c Wed Jan 19 17:19:20 2011 @@ -77,6 +77,8 @@ CARD_CACHE_FILE_FORMAT file_cardcf; BYTE file_cardid[16]; }cardFiles; + SCARDCONTEXT hSCardCtx; + SCARDHANDLE hScard; }VENDOR_SPECIFIC; @@ -1381,6 +1383,9 @@ "hScard=0x%08X, hSCardCtx=0x%08X\n", pCardData->dwVersion, \ NULLWSTR(pCardData->pwszCardName),pCardData->hScard, \ pCardData->hSCardCtx); + + vs->hScard = pCardData->hScard; + vs->hSCardCtx = pCardData->hSCardCtx; /* The lowest supported version is 4. */ if (pCardData->dwVersion < MINIMUM_VERSION_SUPPORTED) @@ -1413,29 +1418,10 @@ memset(&ctx_param, 0, sizeof(ctx_param)); ctx_param.ver = 1; ctx_param.app_name = "cardmod"; + /* we need to modify the context before detecting readers */ + ctx_param.flags = SC_CONTEXT_PARAM_DONT_DETECT_READERS; + - if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\OpenSC Project\\Opensc", 0, NULL, \ - REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) - { - if(RegSetValueEx(key, "pcsc_ctx", NULL, REG_DWORD, &(pCardData->hSCardCtx), \ - sizeof(pCardData->hSCardCtx)) != ERROR_SUCCESS) - { - print_werror(pCardData, "RegSetValueEx pcsc_ctx"); - return SCARD_F_UNKNOWN_ERROR; - } - if(RegSetValueEx(key, "pcsc_card", NULL, REG_DWORD, &(pCardData->hScard), \ - sizeof(pCardData->hScard)) != ERROR_SUCCESS) - { - print_werror(pCardData, "RegSetValueEx pcsc_card"); - return SCARD_F_UNKNOWN_ERROR; - } - RegCloseKey(key); - } - else - { - print_werror(pCardData, "RegCreateKeyEx"); - return SCARD_F_UNKNOWN_ERROR; - } r = sc_context_create(&(vs->ctx), &ctx_param); logprintf(pCardData, 3, "sc_context_create passed r = %d\n", r); @@ -1449,6 +1435,16 @@ { int i; + /* set the addresses of the reader and card handles + * Our cardmod pcsc code will use these during the detect_readers + * We use the address of the handles as stored in the vs + */ + vs->ctx->phSCardCtx = &vs->hSCardCtx; + vs->ctx->phScard = &vs->hScard; + + logprintf(pCardData, 5, "sc_ctx_detect_readers(ctx): %d\n", \ + sc_ctx_detect_readers(vs->ctx)); + logprintf(pCardData, 5, "sc_ctx_get_reader_count(ctx): %d\n", \ sc_ctx_get_reader_count(vs->ctx)); @@ -1663,7 +1659,11 @@ p = name + strlen(name) - 1; while (isalnum(*p) || ('.' == *p) || ('_' == *p)) p--; p++; - + +/* the following doies not look correct, as it is trying to limit + * what applications can use cardmod. + */ +#if 0 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\OpenSC Project\\Opensc",\ NULL, KEY_READ, &key)==ERROR_SUCCESS) { @@ -1682,6 +1682,7 @@ RegCloseKey(key); } +#endif if (*p == '\0') return FALSE; if(!winlogon) --- ./src/libopensc/,reader-pcsc.c Wed Dec 22 11:14:47 2010 +++ ./src/libopensc/reader-pcsc.c Wed Jan 19 16:57:54 2011 @@ -1674,37 +1674,8 @@ 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); - - 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"); - } + gpriv->pcsc_ctx = *(SCARDCONTEXT *)ctx->phSCardCtx; + card_handle = *(SCARDHANDLE *)ctx->phScard; sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "gpriv->pcsc_ctx = %X, card_handle = %X", gpriv->pcsc_ctx, card_handle); @@ -1727,7 +1698,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; } --- ./src/libopensc/,ctx.c Wed Dec 22 11:14:47 2010 +++ ./src/libopensc/ctx.c Wed Jan 19 10:46:57 2011 @@ -659,7 +659,8 @@ free(opts.forced_card_driver); } del_drvs(&opts); - sc_ctx_detect_readers(ctx); + if (parm == NULL || (parm->flags & SC_CONTEXT_PARAM_DONT_DETECT_READERS) == 0) + sc_ctx_detect_readers(ctx); *ctx_out = ctx; return SC_SUCCESS; } --- ./src/libopensc/,opensc.h Wed Dec 22 11:14:47 2010 +++ ./src/libopensc/opensc.h Wed Jan 19 10:40:03 2011 @@ -609,6 +609,10 @@ void *mutex; unsigned int magic; + + /* Used by cardmod as BaseCSP will pass in the handles to use */ + void * phSCardCtx; + void * phScard; } sc_context_t; /* APDU handling functions */ @@ -637,6 +641,8 @@ * in finding application-specific configuration data. Can be NULL. */ int sc_establish_context(sc_context_t **ctx, const char *app_name); + +#define SC_CONTEXT_PARAM_DONT_DETECT_READERS 0x00000001 /** * @struct sc_context_t initialization parameters
_______________________________________________ opensc-devel mailing list opensc-devel@lists.opensc-project.org http://www.opensc-project.org/mailman/listinfo/opensc-devel