remove dependence on libopensc, instead use scconf directly.
Note: this code is still experimental ! git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@2552 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
parent
9f26a4ae35
commit
7a2fec9b86
@ -31,7 +31,7 @@ libpkcs11_la_LIBADD =
|
|||||||
|
|
||||||
pkcs11_spy_la_SOURCES = pkcs11-spy.c pkcs11-display.c pkcs11-display.h
|
pkcs11_spy_la_SOURCES = pkcs11-spy.c pkcs11-display.c pkcs11-display.h
|
||||||
pkcs11_spy_la_LDFLAGS = -module -avoid-version
|
pkcs11_spy_la_LDFLAGS = -module -avoid-version
|
||||||
pkcs11_spy_la_LIBADD = @LIBOPENSC@ @OPENSSL_LIBS@ libpkcs11.la @LIBLTDL@
|
pkcs11_spy_la_LIBADD = @LIBSCCONF@ @OPENSSL_LIBS@ libpkcs11.la @LIBLTDL@
|
||||||
|
|
||||||
include_HEADERS = pkcs11.h
|
include_HEADERS = pkcs11.h
|
||||||
|
|
||||||
|
@ -21,9 +21,13 @@
|
|||||||
#endif
|
#endif
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <opensc/opensc.h>
|
#include <opensc/scconf.h>
|
||||||
#include "pkcs11-display.h"
|
#include "pkcs11-display.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <winreg.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define __PASTE(x,y) x##y
|
#define __PASTE(x,y) x##y
|
||||||
|
|
||||||
/* Declare all spy_* Cryptoki function */
|
/* Declare all spy_* Cryptoki function */
|
||||||
@ -47,13 +51,42 @@ FILE *spy_output = NULL;
|
|||||||
#define CK_PKCS11_FUNCTION_INFO(name) \
|
#define CK_PKCS11_FUNCTION_INFO(name) \
|
||||||
pkcs11_spy->name = name;
|
pkcs11_spy->name = name;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
const char *get_reg_config(const char *spath)
|
||||||
|
{
|
||||||
|
static char path[PATH_MAX];
|
||||||
|
char *ptr = NULL;
|
||||||
|
int plen = sizeof(path);
|
||||||
|
long rc;
|
||||||
|
HKEY hkey;
|
||||||
|
|
||||||
|
rc = RegOpenKeyEx(HKEY_CURRENT_USER, spath, 0, KEY_QUERY, &hkey);
|
||||||
|
if (rc == ERROR_SUCCESS) {
|
||||||
|
rc = RegQueryValueEx(hkey, "ConfigFile", NULL, NULL, (LPBYTE)path, &plen);
|
||||||
|
if ((rc == ERROR_SUCCESS) && (plen < PATH_MAX))
|
||||||
|
ptr = path;
|
||||||
|
RegCloseKey(hkey);
|
||||||
|
}
|
||||||
|
if (ptr == NULL) {
|
||||||
|
rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, spath, 0, KEY_QUERY, &hkey);
|
||||||
|
if (rc == ERROR_SUCCESS) {
|
||||||
|
rc = RegQueryValueEx(hkey, "ConfigFile", NULL, NULL, (LPBYTE)path, &plen);
|
||||||
|
if ((rc == ERROR_SUCCESS) && (plen < PATH_MAX))
|
||||||
|
ptr = path;
|
||||||
|
RegCloseKey(hkey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Inits the spy. If successfull, po != NULL */
|
/* Inits the spy. If successfull, po != NULL */
|
||||||
static CK_RV init_spy(void)
|
static CK_RV init_spy(void)
|
||||||
{
|
{
|
||||||
const char *mspec = NULL, *file = NULL, *env = NULL;
|
const char *mspec = NULL, *file = NULL, *env = NULL, *conf_path = NULL;
|
||||||
|
scconf_context *conf_ctx = NULL;
|
||||||
scconf_block *conf_block = NULL, **blocks;
|
scconf_block *conf_block = NULL, **blocks;
|
||||||
sc_context_t *ctx = NULL;
|
int rv = CKR_OK, r;
|
||||||
int rv = CKR_OK, r, i;
|
|
||||||
|
|
||||||
/* Allocates and initializes the pkcs11_spy structure */
|
/* Allocates and initializes the pkcs11_spy structure */
|
||||||
pkcs11_spy =
|
pkcs11_spy =
|
||||||
@ -64,19 +97,49 @@ static CK_RV init_spy(void)
|
|||||||
return CKR_HOST_MEMORY;
|
return CKR_HOST_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = sc_establish_context(&ctx, "pkcs11-spy");
|
#ifdef _WIN32
|
||||||
if (r != 0) {
|
conf_path = get_reg_config("Software\\pkcs15-spy");
|
||||||
free(pkcs11_spy);
|
if (conf_path == NULL)
|
||||||
return CKR_HOST_MEMORY;
|
conf_path = get_reg_config("Software\\OpenSC");
|
||||||
|
#else
|
||||||
|
conf_path = getenv("PKCS11SPY_CONF");
|
||||||
|
if (conf_path == NULL) {
|
||||||
|
conf_path = getenv("OPENSC_CONF");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
for (i = 0; ctx->conf_blocks[i] != NULL; i++) {
|
if (conf_path == NULL) {
|
||||||
blocks = scconf_find_blocks(ctx->conf, ctx->conf_blocks[i],
|
fprintf(stderr, "Error: no config file found.\n");
|
||||||
"spy", NULL);
|
fprintf(stderr, "Please set the path to the config in the PKCS11SPY_CONF environment variable.\n");
|
||||||
conf_block = blocks[0];
|
free(pkcs11_spy);
|
||||||
free(blocks);
|
return CKR_DEVICE_ERROR;
|
||||||
if (conf_block != NULL)
|
}
|
||||||
break;
|
conf_ctx = scconf_new(conf_path);
|
||||||
|
if (conf_ctx == NULL) {
|
||||||
|
fprintf(spy_output, "Error: unable to parse config file\n");
|
||||||
|
free(pkcs11_spy);
|
||||||
|
return CKR_DEVICE_ERROR;
|
||||||
|
}
|
||||||
|
r = scconf_parse(conf_ctx);
|
||||||
|
if (r < 0) {
|
||||||
|
fprintf(stderr, "Error: scconf_parse failed\n");
|
||||||
|
free(pkcs11_spy);
|
||||||
|
return CKR_DEVICE_ERROR;
|
||||||
|
}
|
||||||
|
blocks = scconf_find_blocks(conf_ctx, NULL, "app", "pkcs11-spy");
|
||||||
|
conf_block = blocks[0];
|
||||||
|
free(blocks);
|
||||||
|
if (conf_block == NULL) {
|
||||||
|
fprintf(stderr, "Error: scconf_find_blocks failed for 'pkcs11-spy'\n");
|
||||||
|
free(pkcs11_spy);
|
||||||
|
return CKR_DEVICE_ERROR;
|
||||||
|
}
|
||||||
|
blocks = scconf_find_blocks(conf_ctx, conf_block, "spy", NULL);
|
||||||
|
conf_block = blocks[0];
|
||||||
|
free(blocks);
|
||||||
|
if (conf_block == NULL) {
|
||||||
|
fprintf(stderr, "Error: scconf_find_blocks failed for 'spy'\n");
|
||||||
|
free(pkcs11_spy);
|
||||||
|
return CKR_DEVICE_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If conf_block is NULL, just return the default value
|
/* If conf_block is NULL, just return the default value
|
||||||
@ -97,15 +160,20 @@ static CK_RV init_spy(void)
|
|||||||
|
|
||||||
env = getenv("PKCS11SPY");
|
env = getenv("PKCS11SPY");
|
||||||
mspec = env ? env : scconf_get_str(conf_block, "module", NULL);
|
mspec = env ? env : scconf_get_str(conf_block, "module", NULL);
|
||||||
|
if (mspec == NULL) {
|
||||||
|
fprintf(spy_output, "Error: no module specified\n");
|
||||||
|
free(pkcs11_spy);
|
||||||
|
return CKR_DEVICE_ERROR;
|
||||||
|
}
|
||||||
modhandle = C_LoadModule(mspec, &po);
|
modhandle = C_LoadModule(mspec, &po);
|
||||||
if (modhandle && po) {
|
if (modhandle && po) {
|
||||||
fprintf(spy_output, "Loaded: \"%s\"\n", mspec == NULL ? "default module" : mspec);
|
fprintf(spy_output, "Loaded: \"%s\"\n", mspec);
|
||||||
} else {
|
} else {
|
||||||
po = NULL;
|
po = NULL;
|
||||||
free(pkcs11_spy);
|
free(pkcs11_spy);
|
||||||
rv = CKR_GENERAL_ERROR;
|
rv = CKR_GENERAL_ERROR;
|
||||||
}
|
}
|
||||||
sc_release_context(ctx);
|
scconf_free(conf_ctx);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user