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