/* * Load a module - this will load the shared object, call * C_Initialize, and get the list of function pointers */ void * C_LoadModule(const char *mspec, CK_FUNCTION_LIST_PTR_PTR funcs) { sc_pkcs11_module_t *mod; CK_RV rv, (*c_get_function_list)(CK_FUNCTION_LIST_PTR_PTR); mod = calloc(1, sizeof(*mod)); mod->_magic = MAGIC; if (mspec == NULL) { free(mod); return NULL; } mod->handle = sc_dlopen(mspec); if (mod->handle == NULL) { fprintf(stderr, "sc_dlopen failed: %s\n", sc_dlerror()); goto failed; } /* Get the list of function pointers */ c_get_function_list = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR)) sc_dlsym(mod->handle, "C_GetFunctionList"); if (!c_get_function_list) goto failed; rv = c_get_function_list(funcs); if (rv == CKR_OK) return (void *) mod; else fprintf(stderr, "C_GetFunctionList failed %lx", rv); failed: C_UnloadModule((void *) mod); free(mod); return NULL; }
/* * Load a module - this will load the shared object, call * C_Initialize, and get the list of function pointers */ void * 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; lt_dlinit(); mod = (sc_pkcs11_module_t *) calloc(1, sizeof(*mod)); mod->_magic = MAGIC; if (mspec == NULL) mspec = PKCS11_DEFAULT_MODULE_NAME; mod->handle = lt_dlopen(mspec); if (mod->handle == NULL) { #if 0 fprintf(stderr, "lt_dlopen failed: %s\n", lt_dlerror()); #endif goto failed; } /* Get the list of function pointers */ c_get_function_list = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR)) lt_dlsym(mod->handle, "C_GetFunctionList"); if (!c_get_function_list) goto failed; rv = c_get_function_list(funcs); if (rv == CKR_OK) return (void *) mod; failed: C_UnloadModule((void *) mod); return NULL; }
Pkcs11* Pkcs11::open(const char_type *path) { // __CHECK_CONTEXT_RV(m_pContext, false); Dl* module; CK_RV rv; CK_RV (*c_get_function_list)(CK_FUNCTION_LIST_PTR_PTR); CK_FUNCTION_LIST_PTR api = NULL; module = new Dl(); JQ_ASSERT(module); do { if( !module->open(path) ) { jq_emitError(String().sprintf(E_SC_PKCS11_LoadModule)(path)()); break; } c_get_function_list = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR)) module->symbol("C_GetFunctionList"); if( !c_get_function_list ) { jq_emitError(E_SC_PKCS11_GetFuncList); break; } rv = c_get_function_list( &api ); if ( rv != CKR_OK ) { jq_emitError(E_SC_PKCS11_ApiLoad); break; } rv = ((CK_FUNCTION_LIST_PTR)api)->C_Initialize(NULL); if(rv != CKR_OK) { jq_emitError(E_SC_PKCS11_Initialize); break; } return new Pkcs11(module, api); } while(0); if( module ) { module->close(); delete module; } return NULL; }