0N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A */
0N/A
0N/A/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions are met:
0N/A *
0N/A * 1. Redistributions of source code must retain the above copyright notice,
0N/A * this list of conditions and the following disclaimer.
0N/A *
0N/A * 2. Redistributions in binary form must reproduce the above copyright notice,
0N/A * this list of conditions and the following disclaimer in the documentation
0N/A * and/or other materials provided with the distribution.
0N/A *
0N/A * 3. The end-user documentation included with the redistribution, if any, must
0N/A * include the following acknowledgment:
0N/A *
0N/A * "This product includes software developed by IAIK of Graz University of
0N/A * Technology."
0N/A *
0N/A * Alternately, this acknowledgment may appear in the software itself, if
0N/A * and wherever such third-party acknowledgments normally appear.
0N/A *
0N/A * 4. The names "Graz University of Technology" and "IAIK of Graz University of
0N/A * Technology" must not be used to endorse or promote products derived from
0N/A * this software without prior written permission.
0N/A *
0N/A * 5. Products derived from this software may not be called
0N/A * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
0N/A * written permission of Graz University of Technology.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
0N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0N/A * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
0N/A * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
0N/A * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
0N/A * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
0N/A * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0N/A * POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
0N/A/*
0N/A * pkcs11wrapper.c
0N/A * 18.05.2001
0N/A *
0N/A * This module contains the native functions of the Java to PKCS#11 interface
0N/A * which are platform dependent. This includes loading a dynamic link libary,
0N/A * retrieving the function list and unloading the dynamic link library.
0N/A *
0N/A * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
0N/A */
0N/A
0N/A#include "pkcs11wrapper.h"
0N/A
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include <assert.h>
0N/A
0N/A#include <dlfcn.h>
0N/A
0N/A#include <jni.h>
0N/A
0N/A#include "sun_security_pkcs11_wrapper_PKCS11.h"
0N/A
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: connect
0N/A * Signature: (Ljava/lang/String;)V
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect
0N/A (JNIEnv *env, jobject obj, jstring jPkcs11ModulePath, jstring jGetFunctionList)
0N/A{
0N/A void *hModule;
0N/A char *error;
0N/A CK_C_GetFunctionList C_GetFunctionList;
0N/A CK_RV rv;
0N/A ModuleData *moduleData;
0N/A jobject globalPKCS11ImplementationReference;
0N/A char *systemErrorMessage;
0N/A char *exceptionMessage;
0N/A const char *getFunctionListStr;
0N/A
0N/A const char *libraryNameStr = (*env)->GetStringUTFChars(env, jPkcs11ModulePath, 0);
0N/A TRACE1("DEBUG: connect to PKCS#11 module: %s ... ", libraryNameStr);
0N/A
0N/A
0N/A /*
0N/A * Load the PKCS #11 DLL
0N/A */
0N/A dlerror(); /* clear any old error message not fetched */
0N/A#ifdef DEBUG
0N/A hModule = dlopen(libraryNameStr, RTLD_NOW);
0N/A#else
0N/A hModule = dlopen(libraryNameStr, RTLD_LAZY);
0N/A#endif /* DEBUG */
0N/A
0N/A if (hModule == NULL) {
0N/A systemErrorMessage = dlerror();
0N/A exceptionMessage = (char *) malloc(sizeof(char) * (strlen(systemErrorMessage) + strlen(libraryNameStr) + 1));
0N/A strcpy(exceptionMessage, systemErrorMessage);
0N/A strcat(exceptionMessage, libraryNameStr);
0N/A throwIOException(env, exceptionMessage);
0N/A (*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);
0N/A free(exceptionMessage);
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * Get function pointer to C_GetFunctionList
0N/A */
0N/A dlerror(); /* clear any old error message not fetched */
0N/A // with the old JAR file jGetFunctionList is null, temporarily check for that
0N/A if (jGetFunctionList != NULL) {
0N/A getFunctionListStr = (*env)->GetStringUTFChars(env, jGetFunctionList, 0);
0N/A C_GetFunctionList = (CK_C_GetFunctionList) dlsym(hModule, getFunctionListStr);
0N/A (*env)->ReleaseStringUTFChars(env, jGetFunctionList, getFunctionListStr);
0N/A }
155N/A if (C_GetFunctionList == NULL) {
155N/A throwIOException(env, "ERROR: C_GetFunctionList == NULL");
155N/A return;
155N/A } else if ( (systemErrorMessage = dlerror()) != NULL ){
0N/A throwIOException(env, systemErrorMessage);
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * Get function pointers to all PKCS #11 functions
0N/A */
0N/A moduleData = (ModuleData *) malloc(sizeof(ModuleData));
0N/A moduleData->hModule = hModule;
0N/A moduleData->applicationMutexHandler = NULL;
0N/A rv = (C_GetFunctionList)(&(moduleData->ckFunctionListPtr));
0N/A globalPKCS11ImplementationReference = (*env)->NewGlobalRef(env, obj);
0N/A putModuleEntry(env, globalPKCS11ImplementationReference, moduleData);
0N/A
0N/A (*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);
0N/A TRACE0("FINISHED\n");
0N/A
0N/A if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A}
0N/A
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: disconnect
0N/A * Signature: ()V
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_disconnect
0N/A (JNIEnv *env, jobject obj)
0N/A{
0N/A ModuleData *moduleData;
0N/A TRACE0("DEBUG: disconnecting module...");
0N/A moduleData = removeModuleEntry(env, obj);
0N/A
0N/A if (moduleData != NULL) {
0N/A dlclose(moduleData->hModule);
0N/A }
0N/A
0N/A free(moduleData);
0N/A TRACE0("FINISHED\n");
0N/A
0N/A}