annotate src/solaris/native/sun/security/pkcs11/wrapper/p11_md.c @ 155:e1bf7407c933

6469580: 1.5.0_08 JVM crashes in SignatureHandlerLibrary::add on Fujitsu Primepower platform Reviewed-by: andreas, valeriep, wetmore Contributed-by: chris.phillips@sun.com
author wetmore
date Mon, 31 Mar 2008 13:27:10 -0700
parents 37a05a11f281
children b6d6877c1155
rev   line source
duke@0 1 /*
duke@0 2 * Portions Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 */
duke@0 4
duke@0 5 /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
duke@0 6 *
duke@0 7 * Redistribution and use in source and binary forms, with or without
duke@0 8 * modification, are permitted provided that the following conditions are met:
duke@0 9 *
duke@0 10 * 1. Redistributions of source code must retain the above copyright notice,
duke@0 11 * this list of conditions and the following disclaimer.
duke@0 12 *
duke@0 13 * 2. Redistributions in binary form must reproduce the above copyright notice,
duke@0 14 * this list of conditions and the following disclaimer in the documentation
duke@0 15 * and/or other materials provided with the distribution.
duke@0 16 *
duke@0 17 * 3. The end-user documentation included with the redistribution, if any, must
duke@0 18 * include the following acknowledgment:
duke@0 19 *
duke@0 20 * "This product includes software developed by IAIK of Graz University of
duke@0 21 * Technology."
duke@0 22 *
duke@0 23 * Alternately, this acknowledgment may appear in the software itself, if
duke@0 24 * and wherever such third-party acknowledgments normally appear.
duke@0 25 *
duke@0 26 * 4. The names "Graz University of Technology" and "IAIK of Graz University of
duke@0 27 * Technology" must not be used to endorse or promote products derived from
duke@0 28 * this software without prior written permission.
duke@0 29 *
duke@0 30 * 5. Products derived from this software may not be called
duke@0 31 * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
duke@0 32 * written permission of Graz University of Technology.
duke@0 33 *
duke@0 34 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
duke@0 35 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
duke@0 36 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
duke@0 37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
duke@0 38 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
duke@0 39 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
duke@0 40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
duke@0 41 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
duke@0 42 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
duke@0 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
duke@0 44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
duke@0 45 * POSSIBILITY OF SUCH DAMAGE.
duke@0 46 */
duke@0 47
duke@0 48 /*
duke@0 49 * pkcs11wrapper.c
duke@0 50 * 18.05.2001
duke@0 51 *
duke@0 52 * This module contains the native functions of the Java to PKCS#11 interface
duke@0 53 * which are platform dependent. This includes loading a dynamic link libary,
duke@0 54 * retrieving the function list and unloading the dynamic link library.
duke@0 55 *
duke@0 56 * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
duke@0 57 */
duke@0 58
duke@0 59 #include "pkcs11wrapper.h"
duke@0 60
duke@0 61 #include <stdio.h>
duke@0 62 #include <stdlib.h>
duke@0 63 #include <string.h>
duke@0 64 #include <assert.h>
duke@0 65
duke@0 66 #include <dlfcn.h>
duke@0 67 #include <link.h>
duke@0 68
duke@0 69 #include <jni.h>
duke@0 70
duke@0 71 #include "sun_security_pkcs11_wrapper_PKCS11.h"
duke@0 72
duke@0 73 /*
duke@0 74 * Class: sun_security_pkcs11_wrapper_PKCS11
duke@0 75 * Method: connect
duke@0 76 * Signature: (Ljava/lang/String;)V
duke@0 77 */
duke@0 78 JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_connect
duke@0 79 (JNIEnv *env, jobject obj, jstring jPkcs11ModulePath, jstring jGetFunctionList)
duke@0 80 {
duke@0 81 void *hModule;
duke@0 82 char *error;
duke@0 83 CK_C_GetFunctionList C_GetFunctionList;
duke@0 84 CK_RV rv;
duke@0 85 ModuleData *moduleData;
duke@0 86 jobject globalPKCS11ImplementationReference;
duke@0 87 char *systemErrorMessage;
duke@0 88 char *exceptionMessage;
duke@0 89 const char *getFunctionListStr;
duke@0 90
duke@0 91 const char *libraryNameStr = (*env)->GetStringUTFChars(env, jPkcs11ModulePath, 0);
duke@0 92 TRACE1("DEBUG: connect to PKCS#11 module: %s ... ", libraryNameStr);
duke@0 93
duke@0 94
duke@0 95 /*
duke@0 96 * Load the PKCS #11 DLL
duke@0 97 */
duke@0 98 dlerror(); /* clear any old error message not fetched */
duke@0 99 #ifdef DEBUG
duke@0 100 hModule = dlopen(libraryNameStr, RTLD_NOW);
duke@0 101 #else
duke@0 102 hModule = dlopen(libraryNameStr, RTLD_LAZY);
duke@0 103 #endif /* DEBUG */
duke@0 104
duke@0 105 if (hModule == NULL) {
duke@0 106 systemErrorMessage = dlerror();
duke@0 107 exceptionMessage = (char *) malloc(sizeof(char) * (strlen(systemErrorMessage) + strlen(libraryNameStr) + 1));
duke@0 108 strcpy(exceptionMessage, systemErrorMessage);
duke@0 109 strcat(exceptionMessage, libraryNameStr);
duke@0 110 throwIOException(env, exceptionMessage);
duke@0 111 (*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);
duke@0 112 free(exceptionMessage);
duke@0 113 return;
duke@0 114 }
duke@0 115
duke@0 116 /*
duke@0 117 * Get function pointer to C_GetFunctionList
duke@0 118 */
duke@0 119 dlerror(); /* clear any old error message not fetched */
duke@0 120 // with the old JAR file jGetFunctionList is null, temporarily check for that
duke@0 121 if (jGetFunctionList != NULL) {
duke@0 122 getFunctionListStr = (*env)->GetStringUTFChars(env, jGetFunctionList, 0);
duke@0 123 C_GetFunctionList = (CK_C_GetFunctionList) dlsym(hModule, getFunctionListStr);
duke@0 124 (*env)->ReleaseStringUTFChars(env, jGetFunctionList, getFunctionListStr);
duke@0 125 }
wetmore@155 126 if (C_GetFunctionList == NULL) {
wetmore@155 127 throwIOException(env, "ERROR: C_GetFunctionList == NULL");
wetmore@155 128 return;
wetmore@155 129 } else if ( (systemErrorMessage = dlerror()) != NULL ){
duke@0 130 throwIOException(env, systemErrorMessage);
duke@0 131 return;
duke@0 132 }
duke@0 133
duke@0 134 /*
duke@0 135 * Get function pointers to all PKCS #11 functions
duke@0 136 */
duke@0 137 moduleData = (ModuleData *) malloc(sizeof(ModuleData));
duke@0 138 moduleData->hModule = hModule;
duke@0 139 moduleData->applicationMutexHandler = NULL;
duke@0 140 rv = (C_GetFunctionList)(&(moduleData->ckFunctionListPtr));
duke@0 141 globalPKCS11ImplementationReference = (*env)->NewGlobalRef(env, obj);
duke@0 142 putModuleEntry(env, globalPKCS11ImplementationReference, moduleData);
duke@0 143
duke@0 144 (*env)->ReleaseStringUTFChars(env, jPkcs11ModulePath, libraryNameStr);
duke@0 145 TRACE0("FINISHED\n");
duke@0 146
duke@0 147 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
duke@0 148 }
duke@0 149
duke@0 150 /*
duke@0 151 * Class: sun_security_pkcs11_wrapper_PKCS11
duke@0 152 * Method: disconnect
duke@0 153 * Signature: ()V
duke@0 154 */
duke@0 155 JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_disconnect
duke@0 156 (JNIEnv *env, jobject obj)
duke@0 157 {
duke@0 158 ModuleData *moduleData;
duke@0 159 TRACE0("DEBUG: disconnecting module...");
duke@0 160 moduleData = removeModuleEntry(env, obj);
duke@0 161
duke@0 162 if (moduleData != NULL) {
duke@0 163 dlclose(moduleData->hModule);
duke@0 164 }
duke@0 165
duke@0 166 free(moduleData);
duke@0 167 TRACE0("FINISHED\n");
duke@0 168
duke@0 169 }