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-09-01 08:43:38 +00:00
|
|
|
#include <opensc/scdl.h>
|
2002-12-17 11:48:14 +00:00
|
|
|
|
|
|
|
#define MAGIC 0xd00bed00
|
|
|
|
|
|
|
|
struct sc_pkcs11_module {
|
2003-08-06 08:45:57 +00:00
|
|
|
unsigned int _magic;
|
2003-09-06 16:18:15 +00:00
|
|
|
void *handle;
|
2002-12-17 11:48:14 +00:00
|
|
|
};
|
2003-09-06 16:18:15 +00:00
|
|
|
typedef struct sc_pkcs11_module sc_pkcs11_module_t;
|
2002-12-17 11:48:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load a module - this will load the shared object, call
|
|
|
|
* C_Initialize, and get the list of function pointers
|
|
|
|
*/
|
2003-09-06 16:18:15 +00:00
|
|
|
void *
|
2002-12-17 11:48:14 +00:00
|
|
|
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;
|
2003-08-29 16:29:52 +00:00
|
|
|
|
|
|
|
if (mspec == NULL)
|
2003-09-03 17:07:15 +00:00
|
|
|
mspec = PKCS11_DEFAULT_MODULE_NAME;
|
2003-08-29 16:29:52 +00:00
|
|
|
mod->handle = scdl_open(mspec);
|
|
|
|
if (mod->handle == NULL)
|
2002-12-17 11:48:14 +00:00
|
|
|
goto failed;
|
|
|
|
|
|
|
|
/* Get the list of function pointers */
|
|
|
|
c_get_function_list = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR))
|
2003-08-29 16:29:52 +00:00
|
|
|
scdl_get_address(mod->handle, "C_GetFunctionList");
|
2002-12-17 11:48:14 +00:00
|
|
|
if (!c_get_function_list)
|
|
|
|
goto failed;
|
|
|
|
rv = c_get_function_list(funcs);
|
|
|
|
if (rv == CKR_OK)
|
2003-09-06 16:18:15 +00:00
|
|
|
return (void *) mod;
|
2002-12-17 11:48:14 +00:00
|
|
|
|
2003-09-06 16:18:15 +00:00
|
|
|
failed: C_UnloadModule((void *) mod);
|
2002-12-17 11:48:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unload a pkcs11 module.
|
|
|
|
* The calling application is responsible for cleaning up
|
|
|
|
* and calling C_Finalize
|
|
|
|
*/
|
|
|
|
CK_RV
|
2003-09-06 16:18:15 +00:00
|
|
|
C_UnloadModule(void *module)
|
2002-12-17 11:48:14 +00:00
|
|
|
{
|
2003-09-06 16:18:15 +00:00
|
|
|
sc_pkcs11_module_t *mod = (sc_pkcs11_module_t *) module;
|
|
|
|
|
2002-12-17 11:48:14 +00:00
|
|
|
if (!mod || mod->_magic != MAGIC)
|
|
|
|
return CKR_ARGUMENTS_BAD;
|
|
|
|
|
2003-08-29 16:29:52 +00:00
|
|
|
if (scdl_close(mod->handle) < 0)
|
2002-12-17 11:48:14 +00:00
|
|
|
return CKR_FUNCTION_FAILED;
|
|
|
|
|
|
|
|
memset(mod, 0, sizeof(*mod));
|
|
|
|
free(mod);
|
|
|
|
return CKR_OK;
|
|
|
|
}
|