2002-12-17 11:48:14 +00:00
|
|
|
/*
|
|
|
|
* Convenience pkcs11 library that can be linked into an application,
|
|
|
|
* and will bind to a specific pkcs11 module.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 Olaf Kirch <okir@lst.de>
|
|
|
|
*/
|
|
|
|
|
2003-07-11 16:31:06 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2002-12-17 11:48:14 +00:00
|
|
|
#include "pkcs11.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2003-05-08 07:54:30 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#endif
|
2002-12-17 11:48:14 +00:00
|
|
|
|
|
|
|
#define MAGIC 0xd00bed00
|
|
|
|
|
2003-08-14 12:45:22 +00:00
|
|
|
#define DL_TYPE_DLFCN 0x01
|
|
|
|
#define DL_TYPE_WIN32 0x02
|
|
|
|
#define DL_TYPE_MAC 0x03
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#define DEFAULT_MODULE_NAME "opensc-pkcs11";
|
|
|
|
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
|
|
|
|
#define DEFAULT_MODULE_NAME "opensc-pkcs11.so";
|
|
|
|
#elif defined(__APPLE__)
|
2003-08-18 13:45:37 +00:00
|
|
|
#define DEFAULT_MODULE_NAME "opensc-pkcs11.bundle";
|
2003-08-14 12:45:22 +00:00
|
|
|
#else
|
|
|
|
#define DEFAULT_MODULE_NAME "opensc-pkcs11.so";
|
|
|
|
#endif
|
|
|
|
|
2002-12-17 11:48:14 +00:00
|
|
|
struct sc_pkcs11_module {
|
2003-08-06 08:45:57 +00:00
|
|
|
unsigned int _magic;
|
|
|
|
void *_dl_handle;
|
2003-08-14 12:45:22 +00:00
|
|
|
unsigned int _type;
|
2003-08-14 11:47:17 +00:00
|
|
|
#if defined(__APPLE__)
|
2003-08-06 12:01:40 +00:00
|
|
|
CFBundleRef bundleRef;
|
2003-04-18 11:55:16 +00:00
|
|
|
#endif
|
2002-12-17 11:48:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int sys_dlopen(sc_pkcs11_module_t *, const char *);
|
|
|
|
static int sys_dlclose(sc_pkcs11_module_t *);
|
|
|
|
static void * sys_dlsym(sc_pkcs11_module_t *, const char *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load a module - this will load the shared object, call
|
|
|
|
* C_Initialize, and get the list of function pointers
|
|
|
|
*/
|
|
|
|
sc_pkcs11_module_t *
|
|
|
|
C_LoadModule(const char *mspec, CK_FUNCTION_LIST_PTR_PTR funcs)
|
|
|
|
{
|
|
|
|
sc_pkcs11_module_t *mod;
|
|
|
|
CK_RV (*c_get_function_list)(CK_FUNCTION_LIST_PTR_PTR);
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
mod = (sc_pkcs11_module_t *) calloc(1, sizeof(*mod));
|
|
|
|
mod->_magic = MAGIC;
|
|
|
|
if (sys_dlopen(mod, mspec) < 0)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
/* Get the list of function pointers */
|
|
|
|
c_get_function_list = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR))
|
|
|
|
sys_dlsym(mod, "C_GetFunctionList");
|
|
|
|
if (!c_get_function_list)
|
|
|
|
goto failed;
|
|
|
|
rv = c_get_function_list(funcs);
|
|
|
|
if (rv == CKR_OK)
|
|
|
|
return mod;
|
|
|
|
|
|
|
|
failed: C_UnloadModule(mod);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unload a pkcs11 module.
|
|
|
|
* The calling application is responsible for cleaning up
|
|
|
|
* and calling C_Finalize
|
|
|
|
*/
|
|
|
|
CK_RV
|
|
|
|
C_UnloadModule(sc_pkcs11_module_t *mod)
|
|
|
|
{
|
|
|
|
if (!mod || mod->_magic != MAGIC)
|
|
|
|
return CKR_ARGUMENTS_BAD;
|
|
|
|
|
|
|
|
if (sys_dlclose(mod) < 0)
|
|
|
|
return CKR_FUNCTION_FAILED;
|
|
|
|
|
|
|
|
memset(mod, 0, sizeof(*mod));
|
|
|
|
free(mod);
|
|
|
|
return CKR_OK;
|
|
|
|
}
|
|
|
|
|
2003-08-14 12:45:22 +00:00
|
|
|
#if defined(HAVE_DLFCN_H)
|
2002-12-17 11:48:14 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Module loader for platforms that have dlopen
|
|
|
|
*
|
|
|
|
* This is intentionally primitive; we may want a more
|
|
|
|
* elaborate loader in libopensc one day
|
|
|
|
*/
|
|
|
|
int
|
2003-08-14 12:45:22 +00:00
|
|
|
dl_dlopen(struct sc_pkcs11_module *mod, const char *name)
|
2002-12-17 11:48:14 +00:00
|
|
|
{
|
|
|
|
const char **dir, *ldlist[64];
|
|
|
|
char pathbuf[4096], *ldenv;
|
|
|
|
unsigned int n = 0;
|
|
|
|
|
2003-08-06 12:01:40 +00:00
|
|
|
if ((ldenv = getenv("LD_LIBRARY_PATH"))
|
2002-12-17 11:48:14 +00:00
|
|
|
&& (ldenv = strdup(ldenv))) {
|
|
|
|
ldlist[n] = strtok(ldenv, ":");
|
|
|
|
while (ldlist[n] != NULL && ++n < 63)
|
|
|
|
ldlist[n] = strtok(NULL, ":");
|
|
|
|
}
|
|
|
|
ldlist[n] = NULL;
|
|
|
|
|
|
|
|
for (dir = ldlist; *dir; dir++) {
|
|
|
|
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", *dir, name);
|
|
|
|
mod->_dl_handle = dlopen(pathbuf, RTLD_NOW);
|
|
|
|
if (mod->_dl_handle != NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod->_dl_handle == NULL)
|
|
|
|
mod->_dl_handle = dlopen(name, RTLD_NOW);
|
|
|
|
|
|
|
|
if (ldenv)
|
|
|
|
free(ldenv);
|
|
|
|
|
2003-08-14 12:45:22 +00:00
|
|
|
mod->_type = DL_TYPE_DLFCN;
|
2002-12-17 11:48:14 +00:00
|
|
|
return (mod->_dl_handle? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-08-14 12:45:22 +00:00
|
|
|
dl_dlclose(struct sc_pkcs11_module *mod)
|
2002-12-17 11:48:14 +00:00
|
|
|
{
|
|
|
|
if (mod->_dl_handle)
|
|
|
|
dlclose(mod->_dl_handle);
|
|
|
|
mod->_dl_handle = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2003-08-14 12:45:22 +00:00
|
|
|
dl_dlsym(sc_pkcs11_module_t *mod, const char *name)
|
2002-12-17 11:48:14 +00:00
|
|
|
{
|
2003-08-06 08:45:57 +00:00
|
|
|
char sym_name[256];
|
|
|
|
void *address;
|
|
|
|
|
2002-12-17 11:48:14 +00:00
|
|
|
if (!mod->_dl_handle)
|
|
|
|
return NULL;
|
|
|
|
|
2003-08-06 08:45:57 +00:00
|
|
|
/* Some platforms might need a leading underscore for the symbol */
|
2003-08-06 12:01:40 +00:00
|
|
|
snprintf(sym_name, sizeof(sym_name), "_%s", name);
|
2003-08-06 08:45:57 +00:00
|
|
|
address = dlsym(mod->_dl_handle, sym_name);
|
2002-12-17 16:00:40 +00:00
|
|
|
|
2003-08-06 08:45:57 +00:00
|
|
|
/* Failed? Try again without the leading underscore */
|
|
|
|
if (address == NULL)
|
|
|
|
address = dlsym(mod->_dl_handle, name);
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2003-08-14 12:45:22 +00:00
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
2002-12-17 16:00:40 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Module loader for the Windows platform.
|
|
|
|
*/
|
|
|
|
int
|
2003-08-14 12:45:22 +00:00
|
|
|
win32_dlopen(struct sc_pkcs11_module *mod, const char *name)
|
2002-12-17 16:00:40 +00:00
|
|
|
{
|
|
|
|
mod->_dl_handle = LoadLibrary(name);
|
2003-08-14 12:45:22 +00:00
|
|
|
mod->_type = DL_TYPE_WIN32;
|
2002-12-17 16:00:40 +00:00
|
|
|
|
|
|
|
return (mod->_dl_handle? 0 : GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-08-14 12:45:22 +00:00
|
|
|
win32_dlclose(struct sc_pkcs11_module *mod)
|
2002-12-17 16:00:40 +00:00
|
|
|
{
|
|
|
|
if (mod->_dl_handle) {
|
|
|
|
if (FreeLibrary(mod->_dl_handle)) {
|
|
|
|
mod->_dl_handle = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2003-08-14 12:45:22 +00:00
|
|
|
win32_dlsym(sc_pkcs11_module_t *mod, const char *name)
|
2002-12-17 16:00:40 +00:00
|
|
|
{
|
|
|
|
if (!mod->_dl_handle)
|
|
|
|
return NULL;
|
|
|
|
return GetProcAddress(mod->_dl_handle, name);
|
|
|
|
}
|
|
|
|
|
2003-08-14 12:45:22 +00:00
|
|
|
#endif
|
|
|
|
#if defined(__APPLE__)
|
2003-04-18 11:55:16 +00:00
|
|
|
#include <mach-o/dyld.h>
|
2002-12-17 16:00:40 +00:00
|
|
|
|
2003-04-18 11:55:16 +00:00
|
|
|
/*
|
2003-07-11 16:31:06 +00:00
|
|
|
* Module loader for MacOS X
|
2003-04-18 11:55:16 +00:00
|
|
|
*/
|
|
|
|
int
|
2003-08-14 12:45:22 +00:00
|
|
|
mac_dlopen(struct sc_pkcs11_module *mod, const char *name)
|
2003-04-18 11:55:16 +00:00
|
|
|
{
|
2003-08-14 11:47:17 +00:00
|
|
|
if (strstr(name, ".bundle")) {
|
2003-05-08 07:54:30 +00:00
|
|
|
CFStringRef text = CFStringCreateWithFormat(
|
|
|
|
NULL, NULL, CFSTR("%s"), name);
|
|
|
|
CFURLRef urlRef = CFURLCreateWithFileSystemPath(
|
|
|
|
kCFAllocatorDefault, text, kCFURLPOSIXPathStyle, 1);
|
|
|
|
mod->bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
|
|
|
|
CFRelease(urlRef);
|
|
|
|
CFRelease(text);
|
|
|
|
mod->_dl_handle = NULL;
|
2003-08-14 11:47:17 +00:00
|
|
|
} else {
|
|
|
|
mod->_dl_handle = (struct mach_header *) NSAddImage(name,
|
|
|
|
NSADDIMAGE_OPTION_WITH_SEARCHING);
|
|
|
|
mod->bundleRef = NULL;
|
2003-05-08 07:54:30 +00:00
|
|
|
}
|
2003-08-14 12:45:22 +00:00
|
|
|
mod->_type = DL_TYPE_MAC;
|
2003-05-08 07:54:30 +00:00
|
|
|
|
|
|
|
return (mod->_dl_handle == NULL && mod->bundleRef == NULL ? -1 : 0);
|
|
|
|
}
|
2003-04-18 11:55:16 +00:00
|
|
|
|
|
|
|
int
|
2003-08-14 12:45:22 +00:00
|
|
|
mac_dlclose(struct sc_pkcs11_module *mod)
|
2003-04-18 11:55:16 +00:00
|
|
|
{
|
2003-05-08 07:54:30 +00:00
|
|
|
if (mod->bundleRef != NULL) {
|
|
|
|
CFBundleUnloadExecutable(mod->bundleRef);
|
|
|
|
CFRelease(mod->bundleRef);
|
|
|
|
}
|
|
|
|
|
2003-08-06 12:01:40 +00:00
|
|
|
return 0;
|
2003-04-18 11:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2003-08-14 12:45:22 +00:00
|
|
|
mac_dlsym(sc_pkcs11_module_t *mod, const char *name)
|
2003-04-18 11:55:16 +00:00
|
|
|
{
|
2003-05-08 07:54:30 +00:00
|
|
|
NSSymbol symbol = NULL;
|
2003-08-06 12:01:40 +00:00
|
|
|
|
2003-08-14 11:47:17 +00:00
|
|
|
if (mod->bundleRef != NULL) {
|
2003-05-08 07:54:30 +00:00
|
|
|
CFStringRef text = CFStringCreateWithFormat(
|
|
|
|
NULL, NULL, CFSTR("%s"), name);
|
|
|
|
symbol = CFBundleGetFunctionPointerForName(
|
|
|
|
mod->bundleRef, text);
|
|
|
|
CFRelease(text);
|
|
|
|
return symbol;
|
2003-08-14 11:47:17 +00:00
|
|
|
} else {
|
|
|
|
char sym_name[4096];
|
|
|
|
|
|
|
|
snprintf(sym_name, sizeof(sym_name), "_%s", name);
|
|
|
|
symbol = NSLookupSymbolInImage((const struct mach_header *)
|
|
|
|
mod->_dl_handle, sym_name,
|
|
|
|
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW);
|
|
|
|
if (symbol == NULL)
|
|
|
|
return NULL;
|
|
|
|
return NSAddressOfSymbol(symbol);
|
2003-05-08 07:54:30 +00:00
|
|
|
}
|
2003-04-18 11:55:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
2003-08-14 12:45:22 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
sys_dlopen(struct sc_pkcs11_module *mod, const char *name)
|
|
|
|
{
|
|
|
|
if (name == NULL)
|
|
|
|
name = DEFAULT_MODULE_NAME;
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return win32_dlopen(mod, name);
|
|
|
|
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
|
|
|
|
if (strstr(name, ".bundle") || strstr(name, ".dylib")) {
|
|
|
|
return mac_dlopen(mod, name);
|
|
|
|
} else {
|
|
|
|
return dl_dlopen(mod, name);
|
|
|
|
}
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
return mac_dlopen(mod, name);
|
|
|
|
#elif defined(HAVE_DLFCN_H)
|
|
|
|
return dl_dlopen(mod, name);
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sys_dlclose(struct sc_pkcs11_module *mod)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return win32_dlclose(mod);
|
|
|
|
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
|
|
|
|
switch(mod->_type) {
|
|
|
|
case DL_TYPE_MAC:
|
|
|
|
return mac_dlclose(mod);
|
|
|
|
break;
|
|
|
|
case DL_TYPE_DLFCN:
|
|
|
|
return dl_dlclose(mod);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
return mac_dlclose(mod);
|
|
|
|
#elif defined(HAVE_DLFCN_H)
|
|
|
|
return dl_dlclose(mod);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
sys_dlsym(sc_pkcs11_module_t *mod, const char *name)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return win32_dlsym(mod, name);
|
|
|
|
#elif defined(HAVE_DLFCN_H) && defined(__APPLE__)
|
|
|
|
switch(mod->_type) {
|
|
|
|
case DL_TYPE_MAC:
|
|
|
|
return mac_dlsym(mod, name);
|
|
|
|
break;
|
|
|
|
case DL_TYPE_DLFCN:
|
|
|
|
return dl_dlsym(mod, name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
return mac_dlsym(mod, name);
|
|
|
|
#elif defined(HAVE_DLFCN_H)
|
|
|
|
return dl_dlsym(mod, name);
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|