Path: blob/master/src/jdk.crypto.cryptoki/unix/native/libj2pkcs11/p11_md.c
41149 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2*/34/* Copyright (c) 2002 Graz University of Technology. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* 1. Redistributions of source code must retain the above copyright notice,10* this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15*16* 3. The end-user documentation included with the redistribution, if any, must17* include the following acknowledgment:18*19* "This product includes software developed by IAIK of Graz University of20* Technology."21*22* Alternately, this acknowledgment may appear in the software itself, if23* and wherever such third-party acknowledgments normally appear.24*25* 4. The names "Graz University of Technology" and "IAIK of Graz University of26* Technology" must not be used to endorse or promote products derived from27* this software without prior written permission.28*29* 5. Products derived from this software may not be called30* "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior31* written permission of Graz University of Technology.32*33* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED34* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED35* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR36* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE37* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,38* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,39* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,40* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON41* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,42* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY43* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE44* POSSIBILITY OF SUCH DAMAGE.45*/4647/*48* pkcs11wrapper.c49* 18.05.200150*51* This module contains the native functions of the Java to PKCS#11 interface52* which are platform dependent. This includes loading a dynamic link libary,53* retrieving the function list and unloading the dynamic link library.54*55* @author Karl Scheibelhofer <[email protected]>56*/5758#include "pkcs11wrapper.h"5960#include <stdio.h>61#include <stdlib.h>62#include <string.h>63#include <assert.h>6465#include <dlfcn.h>6667#include <jni.h>6869#include "sun_security_pkcs11_wrapper_PKCS11.h"7071/*72* Class: sun_security_pkcs11_wrapper_PKCS1173* Method: connect74* Signature: (Ljava/lang/String;)V75*/76JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect77(JNIEnv *env, jobject obj, jstring jPkcs11ModulePath, jstring jGetFunctionList)78{79void *hModule;80char *error;81CK_C_GetFunctionList C_GetFunctionList=NULL;82CK_RV rv;83ModuleData *moduleData;84jobject globalPKCS11ImplementationReference;85char *systemErrorMessage;86char *exceptionMessage;87const char *getFunctionListStr;8889const char *libraryNameStr = (*env)->GetStringUTFChars(env, jPkcs11ModulePath, 0);90if (libraryNameStr == NULL) {91return;92}93TRACE1("DEBUG: connect to PKCS#11 module: %s ... ", libraryNameStr);9495/*96* Load the PKCS #11 DLL97*/98dlerror(); /* clear any old error message not fetched */99#ifdef DEBUG100hModule = dlopen(libraryNameStr, RTLD_NOW);101#else102hModule = dlopen(libraryNameStr, RTLD_LAZY);103#endif /* DEBUG */104105if (hModule == NULL) {106systemErrorMessage = dlerror();107exceptionMessage = (char *) malloc(sizeof(char) * (strlen(systemErrorMessage) + strlen(libraryNameStr) + 1));108if (exceptionMessage == NULL) {109throwOutOfMemoryError(env, 0);110(*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);111return;112}113strcpy(exceptionMessage, systemErrorMessage);114strcat(exceptionMessage, libraryNameStr);115throwIOException(env, exceptionMessage);116(*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);117free(exceptionMessage);118return;119}120(*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);121122/*123* Get function pointer to C_GetFunctionList124*/125dlerror(); /* clear any old error message not fetched */126// with the old JAR file jGetFunctionList is null, temporarily check for that127if (jGetFunctionList != NULL) {128getFunctionListStr = (*env)->GetStringUTFChars(env, jGetFunctionList, 0);129if (getFunctionListStr == NULL) {130return;131}132C_GetFunctionList = (CK_C_GetFunctionList) dlsym(hModule, getFunctionListStr);133(*env)->ReleaseStringUTFChars(env, jGetFunctionList, getFunctionListStr);134}135if (C_GetFunctionList == NULL) {136throwIOException(env, "ERROR: C_GetFunctionList == NULL");137return;138} else if ( (systemErrorMessage = dlerror()) != NULL ){139throwIOException(env, systemErrorMessage);140return;141}142143/*144* Get function pointers to all PKCS #11 functions145*/146moduleData = (ModuleData *) malloc(sizeof(ModuleData));147if (moduleData == NULL) {148dlclose(hModule);149throwOutOfMemoryError(env, 0);150return;151}152moduleData->hModule = hModule;153moduleData->applicationMutexHandler = NULL;154rv = (C_GetFunctionList)(&(moduleData->ckFunctionListPtr));155globalPKCS11ImplementationReference = (*env)->NewGlobalRef(env, obj);156putModuleEntry(env, globalPKCS11ImplementationReference, moduleData);157158TRACE0("FINISHED\n");159160if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }161}162163/*164* Class: sun_security_pkcs11_wrapper_PKCS11165* Method: disconnect166* Signature: ()V167*/168JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_disconnect169(JNIEnv *env, jobject obj)170{171ModuleData *moduleData;172TRACE0("DEBUG: disconnecting module...");173moduleData = removeModuleEntry(env, obj);174175if (moduleData != NULL) {176dlclose(moduleData->hModule);177}178179free(moduleData);180TRACE0("FINISHED\n");181182}183184185