CK_RV C_Finalize(CK_VOID_PTR pReserved) { CK_RV ret = CKR_OK; log_trace(WHERE, "I: enter"); if (p11_get_init() != BEIDP11_INITIALIZED) { log_trace(WHERE, "I: leave, CKR_CRYPTOKI_NOT_INITIALIZED"); return (CKR_CRYPTOKI_NOT_INITIALIZED); } if (pReserved != NULL) { log_trace(WHERE, "I: leave, CKR_ARGUMENTS_BAD"); return (CKR_ARGUMENTS_BAD); } ret = p11_lock(); if (ret != CKR_OK) { log_trace(WHERE, "I: leave, p11_lock failed with %i",ret); return ret; } //g_final = 0; /* Belpic */ p11_set_init(BEIDP11_DEINITIALIZING); //ret = cal_close(); if (g_aseP11Handle == NULL) { log_trace(WHERE, "E: leave, CKR_CRYPTOKI_NOT_INITIALIZED - g_aseP11Handle is NULL"); ret = CKR_ARGUMENTS_BAD; } else { ret = (pFunctions->C_Finalize) (&pReserved); if (g_aseP11Handle != NULL) { dlclose(g_aseP11Handle); } } /* Release and destroy the mutex */ // mutex might still be in use by C_waitforslotlist p11_free_lock(); p11_set_init(BEIDP11_NOT_INITIALIZED); // util_clean_lock(&logmutex); log_trace(WHERE, "I: p11_free_lock()"); log_trace(WHERE, "I: leave, ret = %i",ret); return ret; }
CK_RV C_Initialize(CK_VOID_PTR pReserved) { int ret = CKR_OK; CK_C_INITIALIZE_ARGS_PTR p_args; unsigned char initial_state = p11_get_init(); #if _DEBUG log_init(DEFAULT_LOG_FILE, LOG_LEVEL_PKCS11_INFO); #else log_init(DEFAULT_LOG_FILE, LOG_LEVEL_PKCS11_NONE); #endif log_trace(WHERE, "I: enter pReserved = %p",pReserved); if (p11_get_init() != BEIDP11_NOT_INITIALIZED) { ret = CKR_CRYPTOKI_ALREADY_INITIALIZED; log_trace(WHERE, "I: Module is allready initialized"); } else { //g_init = BEIDP11_INITIALIZED; p11_set_init(BEIDP11_INITIALIZING); if (pReserved != NULL) { p_args = (CK_C_INITIALIZE_ARGS *)pReserved; if(p_args->pReserved != NULL) { ret = CKR_ARGUMENTS_BAD; goto cleanup; } if( (p_args->CreateMutex == NULL) || (p_args->DestroyMutex == NULL) || \ (p_args->LockMutex == NULL) || (p_args->UnlockMutex == NULL) ) { log_trace(WHERE, "S: use supplied locking mechanism"); //If some, but not all, of the supplied function pointers to C_Initialize are non-NULL_PTR, //then C_Initialize should return with the value CKR_ARGUMENTS_BAD. if(!((p_args->CreateMutex == NULL) && (p_args->DestroyMutex == NULL) && \ (p_args->LockMutex == NULL) && (p_args->UnlockMutex == NULL))) { ret = CKR_ARGUMENTS_BAD; goto cleanup; } } log_trace(WHERE, "S: p11_init_lock"); p11_init_lock(p_args); } cal_init(); p11_set_init(BEIDP11_INITIALIZED); log_trace(WHERE, "S: Initialize this PKCS11 Module"); log_trace(WHERE, "S: ============================="); /*#ifdef PKCS11_FF cal_init_pcsc(); #endif*/ } cleanup: log_trace(WHERE, "I: leave, ret = %i",ret); if (ret != CKR_OK) { p11_set_init(initial_state); } return ret; }
CK_RV C_Initialize(CK_VOID_PTR pReserved) { int ret = CKR_OK; CK_RV retVal = CKR_OK; CK_C_INITIALIZE_ARGS_PTR p_args; #if _DEBUG log_init(DEFAULT_LOG_FILE, LOG_LEVEL_PKCS11_INFO); #else log_init(DEFAULT_LOG_FILE, LOG_LEVEL_PKCS11_NONE); #endif log_trace(WHERE, "I: enter pReserved = %p",pReserved); if (p11_get_init() != BEIDP11_NOT_INITIALIZED) { ret = CKR_CRYPTOKI_ALREADY_INITIALIZED; log_trace(WHERE, "I: Module is allready initialized"); } else { //g_init = BEIDP11_INITIALIZED; p11_set_init(BEIDP11_INITIALIZING); if (pReserved != NULL) { p_args = (CK_C_INITIALIZE_ARGS *)pReserved; if(p_args->pReserved != NULL) { ret = CKR_ARGUMENTS_BAD; goto cleanup; } if( (p_args->CreateMutex == NULL) || (p_args->DestroyMutex == NULL) || \ (p_args->LockMutex == NULL) || (p_args->UnlockMutex == NULL) ) { log_trace(WHERE, "S: use supplied locking mechanism"); //If some, but not all, of the supplied function pointers to C_Initialize are non-NULL_PTR, //then C_Initialize should return with the value CKR_ARGUMENTS_BAD. if(!((p_args->CreateMutex == NULL) && (p_args->DestroyMutex == NULL) && \ (p_args->LockMutex == NULL) && (p_args->UnlockMutex == NULL))) { ret = CKR_ARGUMENTS_BAD; goto cleanup; } } log_trace(WHERE, "S: p11_init_lock"); p11_init_lock(p_args); } //cal_init(); g_aseP11Handle = dlopen(ASEP11_LIB, RTLD_LAZY); // RTLD_NOW is slower log_trace(WHERE, "S: dlopen(ASEP11_LIB"); if (g_aseP11Handle != NULL) { // get function pointer to C_GetFunctionList pC_GetFunctionList = (CK_C_GetFunctionList)dlsym(g_aseP11Handle, "C_GetFunctionList"); if (pC_GetFunctionList != NULL) { // invoke C_GetFunctionList to get the list of pkcs11 function pointers retVal = (*pC_GetFunctionList) (&pFunctions); if (retVal == CKR_OK) { // initialize Cryptoki retVal = (pFunctions->C_Initialize) (NULL); } } } if (retVal == CKR_OK) { p11_set_init(BEIDP11_INITIALIZED); log_trace(WHERE, "S: Initialize this PKCS11 Module"); log_trace(WHERE, "S: ============================="); } else { log_trace(WHERE, "E: Not Initialized this PKCS11 Module"); ret = CKR_ARGUMENTS_BAD; goto cleanup; } } cleanup: log_trace(WHERE, "I: leave, ret = %i",ret); return ret; }