示例#1
0
/*
 * Class:     sun_security_pkcs11_wrapper_PKCS11
 * Method:    connect
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect
    (JNIEnv *env, jobject obj, jstring jPkcs11ModulePath, jstring jGetFunctionList)
{
    void *hModule;
    char *error;
    CK_C_GetFunctionList C_GetFunctionList;
    CK_RV rv;
    ModuleData *moduleData;
    jobject globalPKCS11ImplementationReference;
    char *systemErrorMessage;
    char *exceptionMessage;
    const char *getFunctionListStr;

    const char *libraryNameStr = (*env)->GetStringUTFChars(env, jPkcs11ModulePath, 0);
    TRACE1("DEBUG: connect to PKCS#11 module: %s ... ", libraryNameStr);


    /*
     * Load the PKCS #11 DLL
     */
    dlerror(); /* clear any old error message not fetched */
#ifdef DEBUG
    hModule = dlopen(libraryNameStr, RTLD_NOW);
#else
    hModule = dlopen(libraryNameStr, RTLD_LAZY);
#endif /* DEBUG */

    if (hModule == NULL) {
        systemErrorMessage = dlerror();
        exceptionMessage = (char *) malloc(sizeof(char) * (strlen(systemErrorMessage) + strlen(libraryNameStr) + 1));
        strcpy(exceptionMessage, systemErrorMessage);
        strcat(exceptionMessage, libraryNameStr);
        throwIOException(env, exceptionMessage);
        (*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);
        free(exceptionMessage);
        return;
    }

    /*
     * Get function pointer to C_GetFunctionList
     */
    dlerror(); /* clear any old error message not fetched */
    // with the old JAR file jGetFunctionList is null, temporarily check for that
    if (jGetFunctionList != NULL) {
        getFunctionListStr = (*env)->GetStringUTFChars(env, jGetFunctionList, 0);
        C_GetFunctionList = (CK_C_GetFunctionList) dlsym(hModule, getFunctionListStr);
        (*env)->ReleaseStringUTFChars(env, jGetFunctionList, getFunctionListStr);
    }
    if (C_GetFunctionList == NULL) {
        throwIOException(env, "ERROR: C_GetFunctionList == NULL");
        return;
    } else if ( (systemErrorMessage = dlerror()) != NULL ){
        throwIOException(env, systemErrorMessage);
        return;
    }

    /*
     * Get function pointers to all PKCS #11 functions
     */
    moduleData = (ModuleData *) malloc(sizeof(ModuleData));
    moduleData->hModule = hModule;
    moduleData->applicationMutexHandler = NULL;
    rv = (C_GetFunctionList)(&(moduleData->ckFunctionListPtr));
    globalPKCS11ImplementationReference = (*env)->NewGlobalRef(env, obj);
    putModuleEntry(env, globalPKCS11ImplementationReference, moduleData);

    (*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);
    TRACE0("FINISHED\n");

    if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
}
示例#2
0
/*
 * Class:     iaik_pkcs_pkcs11_wrapper_PKCS11Implementation
 * Method:    connect
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_iaik_pkcs_pkcs11_wrapper_PKCS11Implementation_connect
	(JNIEnv *env, jobject obj, jstring jPkcs11ModulePath)
{
  HINSTANCE hModule;
  CK_C_GetFunctionList C_GetFunctionList;
  CK_RV rv;
  ModuleData *moduleData;
  jobject globalPKCS11ImplementationReference;
  LPVOID lpMsgBuf;
  char *exceptionMessage;

  const char *libraryNameStr = (*env)->GetStringUTFChars(env, jPkcs11ModulePath, 0);
  TRACE0(tag_call, __FUNCTION__, "entering");
  TRACE1(tag_info, __FUNCTION__, "connect to PKCS#11 module: %s ... ", libraryNameStr);


  /*
   * Load the PKCS #11 DLL
   */
  hModule = LoadLibrary(libraryNameStr);
  if (hModule == NULL) {
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        GetLastError(),
        0, /* Default language */
        (LPTSTR) &lpMsgBuf,
        0,
        NULL
    );
    exceptionMessage = (char *) malloc(sizeof(char) * (strlen((LPTSTR) lpMsgBuf) + strlen(libraryNameStr) + 1));
    strcpy(exceptionMessage, (LPTSTR) lpMsgBuf);
    strcat(exceptionMessage, libraryNameStr);
    throwIOException(env, (LPTSTR) exceptionMessage);
    /* Free the buffer. */
    free(exceptionMessage);
    LocalFree(lpMsgBuf);
    return;
  }

  /*
   * Get function pointer to C_GetFunctionList
   */
  C_GetFunctionList = (CK_C_GetFunctionList) GetProcAddress(hModule, "C_GetFunctionList");
  if (C_GetFunctionList == NULL) {
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        GetLastError(),
        0, /* Default language */
        (LPTSTR) &lpMsgBuf,
        0,
        NULL
    );
    throwIOException(env, (LPTSTR) lpMsgBuf);
    /* Free the buffer. */
    LocalFree( lpMsgBuf );
    return;
  }

  /*
   * Get function pointers to all PKCS #11 functions
   */
  moduleData = (ModuleData *) malloc(sizeof(ModuleData));
  moduleData->hModule = hModule;
  moduleData->applicationMutexHandler = NULL;
  rv = (C_GetFunctionList)(&(moduleData->ckFunctionListPtr));
  ckAssertReturnValueOK(env, rv, __FUNCTION__);

  globalPKCS11ImplementationReference = (*env)->NewGlobalRef(env, obj);
  putModuleEntry(env, globalPKCS11ImplementationReference, moduleData);

  (*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);

  TRACE0(tag_call, __FUNCTION__, "exiting ");
}