Path: blob/master/src/jdk.crypto.cryptoki/windows/native/libj2pkcs11/p11_md.c
41152 views
/*1* Copyright (c) 2003, 2018, 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 <windows.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,78jstring jGetFunctionList)79{80HINSTANCE hModule;81CK_C_GetFunctionList C_GetFunctionList;82CK_RV rv = CK_ASSERT_OK;83ModuleData *moduleData;84jobject globalPKCS11ImplementationReference;85LPVOID lpMsgBuf = NULL;86char *exceptionMessage = NULL;87const char *getFunctionListStr;8889const char *libraryNameStr = (*env)->GetStringUTFChars(env,90jPkcs11ModulePath, 0);91TRACE1("DEBUG: connect to PKCS#11 module: %s ... ", libraryNameStr);929394/*95* Load the PKCS #11 DLL96*/97hModule = LoadLibrary(libraryNameStr);98if (hModule == NULL) {99FormatMessage(100FORMAT_MESSAGE_ALLOCATE_BUFFER |101FORMAT_MESSAGE_FROM_SYSTEM |102FORMAT_MESSAGE_IGNORE_INSERTS,103NULL,104GetLastError(),1050, /* Default language */106(LPTSTR) &lpMsgBuf,1070,108NULL109);110exceptionMessage = (char *) malloc(sizeof(char) *111(strlen((LPTSTR) lpMsgBuf) + strlen(libraryNameStr) + 1));112if (exceptionMessage == NULL) {113throwOutOfMemoryError(env, 0);114goto cleanup;115}116strcpy(exceptionMessage, (LPTSTR) lpMsgBuf);117strcat(exceptionMessage, libraryNameStr);118throwIOException(env, (LPTSTR) exceptionMessage);119goto cleanup;120}121122/*123* Get function pointer to C_GetFunctionList124*/125getFunctionListStr = (*env)->GetStringUTFChars(env, jGetFunctionList, 0);126C_GetFunctionList = (CK_C_GetFunctionList) GetProcAddress(hModule,127getFunctionListStr);128(*env)->ReleaseStringUTFChars(env, jGetFunctionList, getFunctionListStr);129if (C_GetFunctionList == NULL) {130FormatMessage(131FORMAT_MESSAGE_ALLOCATE_BUFFER |132FORMAT_MESSAGE_FROM_SYSTEM |133FORMAT_MESSAGE_IGNORE_INSERTS,134NULL,135GetLastError(),1360, /* Default language */137(LPTSTR) &lpMsgBuf,1380,139NULL140);141throwIOException(env, (LPTSTR) lpMsgBuf);142goto cleanup;143}144145/*146* Get function pointers to all PKCS #11 functions147*/148moduleData = (ModuleData *) malloc(sizeof(ModuleData));149if (moduleData == NULL) {150throwOutOfMemoryError(env, 0);151goto cleanup;152}153moduleData->hModule = hModule;154moduleData->applicationMutexHandler = NULL;155rv = (C_GetFunctionList)(&(moduleData->ckFunctionListPtr));156globalPKCS11ImplementationReference = (*env)->NewGlobalRef(env, obj);157putModuleEntry(env, globalPKCS11ImplementationReference, moduleData);158159TRACE0("FINISHED\n");160161cleanup:162/* Free up allocated buffers we no longer need */163if (lpMsgBuf != NULL) {164LocalFree( lpMsgBuf );165}166if (libraryNameStr != NULL) {167(*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);168}169if (exceptionMessage != NULL) {170free(exceptionMessage);171}172173if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }174}175176/*177* Class: sun_security_pkcs11_wrapper_PKCS11178* Method: disconnect179* Signature: ()V180*/181JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_disconnect182(JNIEnv *env, jobject obj)183{184ModuleData *moduleData;185TRACE0("DEBUG: disconnecting module...");186moduleData = removeModuleEntry(env, obj);187188if (moduleData != NULL) {189FreeLibrary(moduleData->hModule);190}191192free(moduleData);193TRACE0("FINISHED\n");194}195196197