src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_util.c
author valeriep
Wed, 14 Aug 2019 01:40:29 +0000
changeset 57737 6bbb4af131e3
parent 55332 f492567244ab
child 58281 49836127542b
permissions -rw-r--r--
8228835: Memory leak in PKCS11 provider when using AES GCM Summary: updated freeCKMechanismPtr to free mechanism-specific memories Reviewed-by: jnimeh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2019, 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
#include "pkcs11wrapper.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/* declare file private functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
ModuleData * getModuleEntry(JNIEnv *env, jobject pkcs11Implementation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
int isModulePresent(JNIEnv *env, jobject pkcs11Implementation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
void removeAllModuleEntries(JNIEnv *env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/* ************************************************************************** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/* Functions for keeping track of currently active and loaded modules         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
/* ************************************************************************** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Create a new object for locking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
jobject createLockObject(JNIEnv *env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    jclass jObjectClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    jobject jLockObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    jmethodID jConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    jObjectClass = (*env)->FindClass(env, "java/lang/Object");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
    76
    if (jObjectClass == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    jConstructor = (*env)->GetMethodID(env, jObjectClass, "<init>", "()V");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
    78
    if (jConstructor == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    jLockObject = (*env)->NewObject(env, jObjectClass, jConstructor);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
    80
    if (jLockObject == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    jLockObject = (*env)->NewGlobalRef(env, jLockObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    return jLockObject ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * Create a new object for locking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
void destroyLockObject(JNIEnv *env, jobject jLockObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    if (jLockObject != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        (*env)->DeleteGlobalRef(env, jLockObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Add the given pkcs11Implementation object to the list of present modules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Attach the given data to the entry. If the given pkcs11Implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * already in the lsit, just override its old module data with the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * None of the arguments can be NULL. If one of the arguments is NULL, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * function does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
void putModuleEntry(JNIEnv *env, jobject pkcs11Implementation, ModuleData *moduleData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if (pkcs11Implementation == NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    if (moduleData == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
14414
f338be3ef659 8001579: Cleanup warnings in security native code
jzavgren
parents: 14342
diff changeset
   109
    (*env)->SetLongField(env, pkcs11Implementation, pNativeDataID, ptr_to_jlong(moduleData));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Get the module data of the entry for the given pkcs11Implementation. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * NULL, if the pkcs11Implementation is not in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
ModuleData * getModuleEntry(JNIEnv *env, jobject pkcs11Implementation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    jlong jData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    if (pkcs11Implementation == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    jData = (*env)->GetLongField(env, pkcs11Implementation, pNativeDataID);
14414
f338be3ef659 8001579: Cleanup warnings in security native code
jzavgren
parents: 14342
diff changeset
   123
    return (ModuleData*)jlong_to_ptr(jData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
CK_FUNCTION_LIST_PTR getFunctionList(JNIEnv *env, jobject pkcs11Implementation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    ModuleData *moduleData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    CK_FUNCTION_LIST_PTR ckpFunctions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    moduleData = getModuleEntry(env, pkcs11Implementation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if (moduleData == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        throwDisconnectedRuntimeException(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    ckpFunctions = moduleData->ckFunctionListPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    return ckpFunctions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * Returns 1, if the given pkcs11Implementation is in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * 0, otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
int isModulePresent(JNIEnv *env, jobject pkcs11Implementation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    int present;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    ModuleData *moduleData = getModuleEntry(env, pkcs11Implementation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    present = (moduleData != NULL) ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    return present ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * Removes the entry for the given pkcs11Implementation from the list. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * the module's data, after the node was removed. If this function returns NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * the pkcs11Implementation was not in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
ModuleData * removeModuleEntry(JNIEnv *env, jobject pkcs11Implementation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    ModuleData *moduleData = getModuleEntry(env, pkcs11Implementation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    if (moduleData == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    (*env)->SetLongField(env, pkcs11Implementation, pNativeDataID, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    return moduleData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * Removes all present entries from the list of modules and frees all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * associated resources. This function is used for clean-up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
void removeAllModuleEntries(JNIEnv *env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /* XXX empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
/* ************************************************************************** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
/* Below there follow the helper functions to support conversions between     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
/* Java and Cryptoki types                                                    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
/* ************************************************************************** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * function to convert a PKCS#11 return value into a PKCS#11Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * This function generates a PKCS#11Exception with the returnValue as the errorcode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * if the returnValue is not CKR_OK. The functin returns 0, if the returnValue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * CKR_OK. Otherwise, it returns the returnValue as a jLong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * @param env - used to call JNI funktions and to get the Exception class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * @param returnValue - of the PKCS#11 function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
jlong ckAssertReturnValueOK(JNIEnv *env, CK_RV returnValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    jclass jPKCS11ExceptionClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    jmethodID jConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    jthrowable jPKCS11Exception;
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   197
    jlong jErrorCode = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   199
    if (returnValue != CKR_OK) {
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   200
        jErrorCode = ckULongToJLong(returnValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        jPKCS11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   202
        if (jPKCS11ExceptionClass != NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   203
            jConstructor = (*env)->GetMethodID(env, jPKCS11ExceptionClass, "<init>", "(J)V");
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   204
            if (jConstructor != NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   205
                jPKCS11Exception = (jthrowable) (*env)->NewObject(env, jPKCS11ExceptionClass, jConstructor, jErrorCode);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   206
                if (jPKCS11Exception != NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   207
                    (*env)->Throw(env, jPKCS11Exception);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   208
                }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   209
            }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   210
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   211
        (*env)->DeleteLocalRef(env, jPKCS11ExceptionClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   213
    return jErrorCode ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   216
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
/*
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   218
 * Throws a Java Exception by name
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   219
 */
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   220
void throwByName(JNIEnv *env, const char *name, const char *msg)
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   221
{
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   222
    jclass cls = (*env)->FindClass(env, name);
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   223
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   224
    if (cls != 0) /* Otherwise an exception has already been thrown */
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   225
        (*env)->ThrowNew(env, cls, msg);
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   226
}
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   227
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   228
/*
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   229
 * Throws java.lang.OutOfMemoryError
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 */
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   231
void throwOutOfMemoryError(JNIEnv *env, const char *msg)
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   232
{
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   233
    throwByName(env, "java/lang/OutOfMemoryError", msg);
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   234
}
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   235
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   236
/*
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   237
 * Throws java.lang.NullPointerException
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   238
 */
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   239
void throwNullPointerException(JNIEnv *env, const char *msg)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
{
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   241
    throwByName(env, "java/lang/NullPointerException", msg);
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   242
}
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   243
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   244
/*
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   245
 * Throws java.io.IOException
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   246
 */
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   247
void throwIOException(JNIEnv *env, const char *msg)
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   248
{
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   249
    throwByName(env, "java/io/IOException", msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * This function simply throws a PKCS#11RuntimeException with the given
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   254
 * string as its message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * @param env Used to call JNI funktions and to get the Exception class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * @param jmessage The message string of the Exception object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 */
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   259
void throwPKCS11RuntimeException(JNIEnv *env, const char *message)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
{
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   261
    throwByName(env, CLASS_PKCS11RUNTIMEEXCEPTION, message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * This function simply throws a PKCS#11RuntimeException. The message says that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * the object is not connected to the module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * @param env Used to call JNI funktions and to get the Exception class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
void throwDisconnectedRuntimeException(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
{
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   272
    throwPKCS11RuntimeException(env, "This object is not connected to a module.");
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   273
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   275
/* This function frees the specified CK_ATTRIBUTE array.
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   276
 *
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   277
 * @param attrPtr pointer to the to-be-freed CK_ATTRIBUTE array.
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   278
 * @param len the length of the array
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   279
 */
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   280
void freeCKAttributeArray(CK_ATTRIBUTE_PTR attrPtr, int len) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   281
    if (attrPtr != NULL) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   282
        int i;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   283
        for (i=0; i<len; i++) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   284
            if (attrPtr[i].pValue != NULL_PTR) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   285
                free(attrPtr[i].pValue);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   286
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   287
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   288
        free(attrPtr);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   289
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   290
}
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   291
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   292
/* This function frees the specified CK_MECHANISM_PTR pointer and its
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   293
 * pParameter including mechanism-specific memory allocations.
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   294
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   295
 * @param mechPtr pointer to the to-be-freed CK_MECHANISM structure.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   296
 */
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   297
void freeCKMechanismPtr(CK_MECHANISM_PTR mechPtr) {
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   298
     void *tmp;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   299
     CK_SSL3_MASTER_KEY_DERIVE_PARAMS *sslMkdTmp;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   300
     CK_SSL3_KEY_MAT_PARAMS* sslKmTmp;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   301
     CK_TLS12_MASTER_KEY_DERIVE_PARAMS *tlsMkdTmp;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   302
     CK_TLS12_KEY_MAT_PARAMS* tlsKmTmp;
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   303
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   304
     if (mechPtr != NULL) {
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   305
         TRACE2("DEBUG: free mech %lX (mech id = 0x%lX)\n",
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   306
                 ptr_to_jlong(mechPtr),  mechPtr->mechanism);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   307
         if (mechPtr->pParameter != NULL) {
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   308
             switch (mechPtr->mechanism) {
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   309
                 case CKM_AES_GCM:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   310
                     tmp = mechPtr->pParameter;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   311
                     TRACE1("\t=> free GCM_PARAMS %lX\n",
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   312
                             ptr_to_jlong(tmp));
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   313
                     free(((CK_GCM_PARAMS*)tmp)->pIv);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   314
                     free(((CK_GCM_PARAMS*)tmp)->pAAD);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   315
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   316
                 case CKM_AES_CCM:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   317
                     tmp = mechPtr->pParameter;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   318
                     TRACE1("\t=> free CK_CCM_PARAMS %lX\n",
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   319
                             ptr_to_jlong(tmp));
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   320
                     free(((CK_CCM_PARAMS*)tmp)->pNonce);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   321
                     free(((CK_CCM_PARAMS*)tmp)->pAAD);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   322
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   323
                 case CKM_TLS_PRF:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   324
                 case CKM_NSS_TLS_PRF_GENERAL:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   325
                     tmp = mechPtr->pParameter;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   326
                     TRACE1("\t=> free CK_TLS_PRF_PARAMS %lX\n",
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   327
                         ptr_to_jlong(tmp));
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   328
                     free(((CK_TLS_PRF_PARAMS*)tmp)->pSeed);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   329
                     free(((CK_TLS_PRF_PARAMS*)tmp)->pLabel);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   330
                     free(((CK_TLS_PRF_PARAMS*)tmp)->pulOutputLen);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   331
                     free(((CK_TLS_PRF_PARAMS*)tmp)->pOutput);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   332
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   333
                 case CKM_SSL3_MASTER_KEY_DERIVE:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   334
                 case CKM_TLS_MASTER_KEY_DERIVE:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   335
                 case CKM_SSL3_MASTER_KEY_DERIVE_DH:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   336
                 case CKM_TLS_MASTER_KEY_DERIVE_DH:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   337
                     sslMkdTmp = mechPtr->pParameter;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   338
                     TRACE1("\t=> free CK_SSL3_MASTER_KEY_DERIVE_PARAMS %lX\n",
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   339
                         ptr_to_jlong(sslMkdTmp));
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   340
                     free(sslMkdTmp->RandomInfo.pClientRandom);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   341
                     free(sslMkdTmp->RandomInfo.pServerRandom);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   342
                     free(sslMkdTmp->pVersion);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   343
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   344
                 case CKM_SSL3_KEY_AND_MAC_DERIVE:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   345
                 case CKM_TLS_KEY_AND_MAC_DERIVE:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   346
                     sslKmTmp = mechPtr->pParameter;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   347
                     TRACE1("\t=> free CK_SSL3_KEY_MAT_PARAMS %lX\n",
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   348
                         ptr_to_jlong(sslKmTmp));
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   349
                     free(sslKmTmp->RandomInfo.pClientRandom);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   350
                     free(sslKmTmp->RandomInfo.pServerRandom);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   351
                     if (sslKmTmp->pReturnedKeyMaterial != NULL) {
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   352
                         free(sslKmTmp->pReturnedKeyMaterial->pIVClient);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   353
                         free(sslKmTmp->pReturnedKeyMaterial->pIVServer);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   354
                         free(sslKmTmp->pReturnedKeyMaterial);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   355
                     }
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   356
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   357
                 case CKM_TLS12_MASTER_KEY_DERIVE:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   358
                 case CKM_TLS12_MASTER_KEY_DERIVE_DH:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   359
                     tlsMkdTmp = mechPtr->pParameter;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   360
                     TRACE1("\t=> CK_TLS12_MASTER_KEY_DERIVE_PARAMS %lX\n",
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   361
                         ptr_to_jlong(tlsMkdTmp));
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   362
                     free(tlsMkdTmp->RandomInfo.pClientRandom);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   363
                     free(tlsMkdTmp->RandomInfo.pServerRandom);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   364
                     free(tlsMkdTmp->pVersion);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   365
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   366
                 case CKM_TLS12_KEY_AND_MAC_DERIVE:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   367
                     tlsKmTmp = mechPtr->pParameter;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   368
                     TRACE1("\t=> free CK_TLS12_KEY_MAT_PARAMS %lX\n",
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   369
                         ptr_to_jlong(tlsKmTmp));
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   370
                     free(tlsKmTmp->RandomInfo.pClientRandom);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   371
                     free(tlsKmTmp->RandomInfo.pServerRandom);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   372
                     if (tlsKmTmp->pReturnedKeyMaterial != NULL) {
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   373
                         free(tlsKmTmp->pReturnedKeyMaterial->pIVClient);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   374
                         free(tlsKmTmp->pReturnedKeyMaterial->pIVServer);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   375
                         free(tlsKmTmp->pReturnedKeyMaterial);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   376
                     }
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   377
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   378
                 case CKM_ECDH1_DERIVE:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   379
                 case CKM_ECDH1_COFACTOR_DERIVE:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   380
                     tmp = mechPtr->pParameter;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   381
                     TRACE1("\t=> free CK_ECDH1_DERIVE_PARAMS %lX\n",
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   382
                         ptr_to_jlong(tmp));
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   383
                     free(((CK_ECDH1_DERIVE_PARAMS *)tmp)->pSharedData);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   384
                     free(((CK_ECDH1_DERIVE_PARAMS *)tmp)->pPublicData);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   385
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   386
                 case CKM_TLS_MAC:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   387
                 case CKM_AES_CTR:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   388
                 case CKM_RSA_PKCS_PSS:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   389
                 case CKM_CAMELLIA_CTR:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   390
                     TRACE0("\t=> NO OP\n");
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   391
                     // params do not contain pointers
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   392
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   393
                 default:
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   394
                     // currently unsupported mechs by SunPKCS11 provider
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   395
                     // CKM_RSA_PKCS_OAEP, CKM_ECMQV_DERIVE,
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   396
                     // CKM_X9_42_*, CKM_KEA_DERIVE, CKM_RC2_*, CKM_RC5_*,
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   397
                     // CKM_SKIPJACK_*, CKM_KEY_WRAP_SET_OAEP, CKM_PKCS5_PBKD2,
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   398
                     // PBE mechs, WTLS mechs, CMS mechs,
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   399
                     // CKM_EXTRACT_KEY_FROM_KEY, CKM_OTP, CKM_KIP,
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   400
                     // CKM_DSA_PARAMETER_GEN?, CKM_GOSTR3410_*
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   401
                     // CK_any_CBC_ENCRYPT_DATA?
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   402
                     TRACE0("\t=> ERROR UNSUPPORTED CK PARAMS\n");
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   403
                     break;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   404
             }
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   405
             free(mechPtr->pParameter);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   406
         } else {
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   407
             TRACE0("DEBUG => Parameter NULL\n");
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   408
         }
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   409
         free(mechPtr);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
   410
     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
 * the following functions convert Java arrays to PKCS#11 array pointers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
 * their array length and vice versa
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
 * void j<Type>ArrayToCK<Type>Array(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
 *                                  const j<Type>Array jArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
 *                                  CK_<Type>_PTR *ckpArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
 *                                  CK_ULONG_PTR ckLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
 * j<Type>Array ck<Type>ArrayToJ<Type>Array(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
 *                                          const CK_<Type>_PTR ckpArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
 *                                          CK_ULONG ckLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
 * PKCS#11 arrays consist always of a pointer to the beginning of the array and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
 * the array length whereas Java arrays carry their array length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 * The Functions to convert a Java array to a PKCS#11 array are void functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 * Their arguments are the Java array object to convert, the reference to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 * array pointer, where the new PKCS#11 array should be stored and the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * to the array length where the PKCS#11 array length should be stored. These two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 * references must not be NULL_PTR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
 * The functions first obtain the array length of the Java array and then allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
 * the memory for the PKCS#11 array and set the array length. Then each element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
 * gets converted depending on their type. After use the allocated memory of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
 * PKCS#11 array has to be explicitly freed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
 * The Functions to convert a PKCS#11 array to a Java array get the PKCS#11 array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 * pointer and the array length and they return the new Java array object. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 * Java array does not need to get freed after use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 * converts a jbooleanArray to a CK_BBOOL array. The allocated memory has to be freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
 * @param env - used to call JNI funktions to get the array informtaion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
 * @param jArray - the Java array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
 * @param ckpArray - the reference, where the pointer to the new CK_BBOOL array will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 * @param ckpLength - the reference, where the array length will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
void jBooleanArrayToCKBBoolArray(JNIEnv *env, const jbooleanArray jArray, CK_BBOOL **ckpArray, CK_ULONG_PTR ckpLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    jboolean* jpTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    if(jArray == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        *ckpArray = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        *ckpLength = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    *ckpLength = (*env)->GetArrayLength(env, jArray);
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   464
    jpTemp = (jboolean*) calloc(*ckpLength, sizeof(jboolean));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   465
    if (jpTemp == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   466
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   467
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   468
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    (*env)->GetBooleanArrayRegion(env, jArray, 0, *ckpLength, jpTemp);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   470
    if ((*env)->ExceptionCheck(env)) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   471
        free(jpTemp);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   472
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   473
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   474
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   475
    *ckpArray = (CK_BBOOL*) calloc (*ckpLength, sizeof(CK_BBOOL));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   476
    if (*ckpArray == NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   477
        free(jpTemp);
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   478
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   479
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   480
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    for (i=0; i<(*ckpLength); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        (*ckpArray)[i] = jBooleanToCKBBool(jpTemp[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    free(jpTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
 * converts a jbyteArray to a CK_BYTE array. The allocated memory has to be freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
 * @param env - used to call JNI funktions to get the array informtaion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
 * @param jArray - the Java array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
 * @param ckpArray - the reference, where the pointer to the new CK_BYTE array will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
 * @param ckpLength - the reference, where the array length will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
void jByteArrayToCKByteArray(JNIEnv *env, const jbyteArray jArray, CK_BYTE_PTR *ckpArray, CK_ULONG_PTR ckpLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    jbyte* jpTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    if(jArray == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        *ckpArray = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        *ckpLength = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    *ckpLength = (*env)->GetArrayLength(env, jArray);
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   506
    jpTemp = (jbyte*) calloc(*ckpLength, sizeof(jbyte));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   507
    if (jpTemp == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   508
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   509
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   510
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    (*env)->GetByteArrayRegion(env, jArray, 0, *ckpLength, jpTemp);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   512
    if ((*env)->ExceptionCheck(env)) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   513
        free(jpTemp);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   514
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   515
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /* if CK_BYTE is the same size as jbyte, we save an additional copy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    if (sizeof(CK_BYTE) == sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        *ckpArray = (CK_BYTE_PTR) jpTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    } else {
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   521
        *ckpArray = (CK_BYTE_PTR) calloc (*ckpLength, sizeof(CK_BYTE));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   522
        if (*ckpArray == NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   523
            free(jpTemp);
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   524
            throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   525
            return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   526
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        for (i=0; i<(*ckpLength); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            (*ckpArray)[i] = jByteToCKByte(jpTemp[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        free(jpTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
 * converts a jlongArray to a CK_ULONG array. The allocated memory has to be freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
 * @param env - used to call JNI funktions to get the array informtaion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
 * @param jArray - the Java array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
 * @param ckpArray - the reference, where the pointer to the new CK_ULONG array will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
 * @param ckpLength - the reference, where the array length will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
void jLongArrayToCKULongArray(JNIEnv *env, const jlongArray jArray, CK_ULONG_PTR *ckpArray, CK_ULONG_PTR ckpLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    jlong* jTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    if(jArray == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        *ckpArray = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        *ckpLength = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    *ckpLength = (*env)->GetArrayLength(env, jArray);
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   553
    jTemp = (jlong*) calloc(*ckpLength, sizeof(jlong));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   554
    if (jTemp == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   555
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   556
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   557
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    (*env)->GetLongArrayRegion(env, jArray, 0, *ckpLength, jTemp);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   559
    if ((*env)->ExceptionCheck(env)) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   560
        free(jTemp);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   561
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   562
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   563
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   564
    *ckpArray = (CK_ULONG_PTR) calloc(*ckpLength, sizeof(CK_ULONG));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   565
    if (*ckpArray == NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   566
        free(jTemp);
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   567
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   568
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   569
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    for (i=0; i<(*ckpLength); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        (*ckpArray)[i] = jLongToCKULong(jTemp[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    free(jTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
 * converts a jcharArray to a CK_CHAR array. The allocated memory has to be freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
 * @param env - used to call JNI funktions to get the array informtaion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
 * @param jArray - the Java array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
 * @param ckpArray - the reference, where the pointer to the new CK_CHAR array will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
 * @param ckpLength - the reference, where the array length will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
void jCharArrayToCKCharArray(JNIEnv *env, const jcharArray jArray, CK_CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    jchar* jpTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    if(jArray == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        *ckpArray = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        *ckpLength = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    *ckpLength = (*env)->GetArrayLength(env, jArray);
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   595
    jpTemp = (jchar*) calloc(*ckpLength, sizeof(jchar));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   596
    if (jpTemp == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   597
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   598
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   599
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    (*env)->GetCharArrayRegion(env, jArray, 0, *ckpLength, jpTemp);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   601
    if ((*env)->ExceptionCheck(env)) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   602
        free(jpTemp);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   603
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   604
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   605
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   606
    *ckpArray = (CK_CHAR_PTR) calloc (*ckpLength, sizeof(CK_CHAR));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   607
    if (*ckpArray == NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   608
        free(jpTemp);
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   609
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   610
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   611
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    for (i=0; i<(*ckpLength); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        (*ckpArray)[i] = jCharToCKChar(jpTemp[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    free(jpTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
 * converts a jcharArray to a CK_UTF8CHAR array. The allocated memory has to be freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
 * @param env - used to call JNI funktions to get the array informtaion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
 * @param jArray - the Java array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
 * @param ckpArray - the reference, where the pointer to the new CK_UTF8CHAR array will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
 * @param ckpLength - the reference, where the array length will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
void jCharArrayToCKUTF8CharArray(JNIEnv *env, const jcharArray jArray, CK_UTF8CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    jchar* jTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    if(jArray == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        *ckpArray = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        *ckpLength = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    *ckpLength = (*env)->GetArrayLength(env, jArray);
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   637
    jTemp = (jchar*) calloc(*ckpLength, sizeof(jchar));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   638
    if (jTemp == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   639
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   640
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   641
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    (*env)->GetCharArrayRegion(env, jArray, 0, *ckpLength, jTemp);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   643
    if ((*env)->ExceptionCheck(env)) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   644
        free(jTemp);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   645
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   646
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   647
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   648
    *ckpArray = (CK_UTF8CHAR_PTR) calloc(*ckpLength, sizeof(CK_UTF8CHAR));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   649
    if (*ckpArray == NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   650
        free(jTemp);
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   651
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   652
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   653
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    for (i=0; i<(*ckpLength); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        (*ckpArray)[i] = jCharToCKUTF8Char(jTemp[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    free(jTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
 * converts a jstring to a CK_CHAR array. The allocated memory has to be freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
 * @param env - used to call JNI funktions to get the array informtaion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
 * @param jArray - the Java array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
 * @param ckpArray - the reference, where the pointer to the new CK_CHAR array will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
 * @param ckpLength - the reference, where the array length will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
void jStringToCKUTF8CharArray(JNIEnv *env, const jstring jArray, CK_UTF8CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    const char* pCharArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    if(jArray == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        *ckpArray = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        *ckpLength = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    pCharArray = (*env)->GetStringUTFChars(env, jArray, &isCopy);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   680
    if (pCharArray == NULL) { return; }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   681
40563
4bff68330f39 8074838: Resolve disabled warnings for libj2pkcs11
ascarpino
parents: 39142
diff changeset
   682
    *ckpLength = (CK_ULONG) strlen(pCharArray);
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   683
    *ckpArray = (CK_UTF8CHAR_PTR) calloc(*ckpLength + 1, sizeof(CK_UTF8CHAR));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   684
    if (*ckpArray == NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   685
        (*env)->ReleaseStringUTFChars(env, (jstring) jArray, pCharArray);
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   686
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   687
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   688
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    strcpy((char*)*ckpArray, pCharArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    (*env)->ReleaseStringUTFChars(env, (jstring) jArray, pCharArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
 * converts a jobjectArray with Java Attributes to a CK_ATTRIBUTE array. The allocated memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
 * has to be freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
 * @param env - used to call JNI funktions to get the array informtaion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
 * @param jArray - the Java Attribute array (template) to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
 * @param ckpArray - the reference, where the pointer to the new CK_ATTRIBUTE array will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
 *                   stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
 * @param ckpLength - the reference, where the array length will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
void jAttributeArrayToCKAttributeArray(JNIEnv *env, jobjectArray jArray, CK_ATTRIBUTE_PTR *ckpArray, CK_ULONG_PTR ckpLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    jlong jLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    jobject jAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    TRACE0("\nDEBUG: jAttributeArrayToCKAttributeArray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    if (jArray == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        *ckpArray = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        *ckpLength = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    jLength = (*env)->GetArrayLength(env, jArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    *ckpLength = jLongToCKULong(jLength);
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   717
    *ckpArray = (CK_ATTRIBUTE_PTR) calloc(*ckpLength, sizeof(CK_ATTRIBUTE));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   718
    if (*ckpArray == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   719
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   720
        return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   721
    }
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 14414
diff changeset
   722
    TRACE1(", converting %d attributes", jLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    for (i=0; i<(*ckpLength); i++) {
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 14414
diff changeset
   724
        TRACE1(", getting %d. attribute", i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        jAttribute = (*env)->GetObjectArrayElement(env, jArray, i);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   726
        if ((*env)->ExceptionCheck(env)) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   727
            freeCKAttributeArray(*ckpArray, i);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   728
            return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   729
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        TRACE1(", jAttribute = %d", jAttribute);
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 14414
diff changeset
   731
        TRACE1(", converting %d. attribute", i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        (*ckpArray)[i] = jAttributeToCKAttribute(env, jAttribute);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   733
        if ((*env)->ExceptionCheck(env)) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   734
            freeCKAttributeArray(*ckpArray, i);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   735
            return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   736
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    TRACE0("FINISHED\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
 * converts a CK_BYTE array and its length to a jbyteArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
 * @param env - used to call JNI funktions to create the new Java array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
 * @param ckpArray - the pointer to the CK_BYTE array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
 * @param ckpLength - the length of the array to convert
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   747
 * @return - the new Java byte array or NULL if error occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
jbyteArray ckByteArrayToJByteArray(JNIEnv *env, const CK_BYTE_PTR ckpArray, CK_ULONG ckLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    jbyte* jpTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    jbyteArray jArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /* if CK_BYTE is the same size as jbyte, we save an additional copy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    if (sizeof(CK_BYTE) == sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        jpTemp = (jbyte*) ckpArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    } else {
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   759
        jpTemp = (jbyte*) calloc(ckLength, sizeof(jbyte));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   760
        if (jpTemp == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   761
            throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   762
            return NULL;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   763
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        for (i=0; i<ckLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            jpTemp[i] = ckByteToJByte(ckpArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    jArray = (*env)->NewByteArray(env, ckULongToJSize(ckLength));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   770
    if (jArray != NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   771
        (*env)->SetByteArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   772
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   774
    if (sizeof(CK_BYTE) != sizeof(jbyte)) { free(jpTemp); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    return jArray ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
 * converts a CK_ULONG array and its length to a jlongArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
 * @param env - used to call JNI funktions to create the new Java array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
 * @param ckpArray - the pointer to the CK_ULONG array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
 * @param ckpLength - the length of the array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
 * @return - the new Java long array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
jlongArray ckULongArrayToJLongArray(JNIEnv *env, const CK_ULONG_PTR ckpArray, CK_ULONG ckLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    jlong* jpTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    jlongArray jArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   793
    jpTemp = (jlong*) calloc(ckLength, sizeof(jlong));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   794
    if (jpTemp == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   795
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   796
        return NULL;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   797
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    for (i=0; i<ckLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        jpTemp[i] = ckLongToJLong(ckpArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    jArray = (*env)->NewLongArray(env, ckULongToJSize(ckLength));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   802
    if (jArray != NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   803
        (*env)->SetLongArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   804
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    free(jpTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    return jArray ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
 * converts a CK_CHAR array and its length to a jcharArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
 * @param env - used to call JNI funktions to create the new Java array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
 * @param ckpArray - the pointer to the CK_CHAR array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
 * @param ckpLength - the length of the array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
 * @return - the new Java char array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
jcharArray ckCharArrayToJCharArray(JNIEnv *env, const CK_CHAR_PTR ckpArray, CK_ULONG ckLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    jchar* jpTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    jcharArray jArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   824
    jpTemp = (jchar*) calloc(ckLength, sizeof(jchar));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   825
    if (jpTemp == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   826
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   827
        return NULL;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   828
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    for (i=0; i<ckLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        jpTemp[i] = ckCharToJChar(ckpArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    jArray = (*env)->NewCharArray(env, ckULongToJSize(ckLength));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   833
    if (jArray != NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   834
        (*env)->SetCharArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   835
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    free(jpTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    return jArray ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
 * converts a CK_UTF8CHAR array and its length to a jcharArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
 * @param env - used to call JNI funktions to create the new Java array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
 * @param ckpArray - the pointer to the CK_UTF8CHAR array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
 * @param ckpLength - the length of the array to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
 * @return - the new Java char array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
jcharArray ckUTF8CharArrayToJCharArray(JNIEnv *env, const CK_UTF8CHAR_PTR ckpArray, CK_ULONG ckLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    CK_ULONG i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    jchar* jpTemp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    jcharArray jArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
   855
    jpTemp = (jchar*) calloc(ckLength, sizeof(jchar));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   856
    if (jpTemp == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   857
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   858
        return NULL;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   859
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    for (i=0; i<ckLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        jpTemp[i] = ckUTF8CharToJChar(ckpArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    jArray = (*env)->NewCharArray(env, ckULongToJSize(ckLength));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   864
    if (jArray != NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   865
        (*env)->SetCharArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   866
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    free(jpTemp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    return jArray ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
 * the following functions convert Java objects to PKCS#11 pointers and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
 * length in bytes and vice versa
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
 * CK_<Type>_PTR j<Object>ToCK<Type>Ptr(JNIEnv *env, jobject jObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
 * jobject ck<Type>PtrToJ<Object>(JNIEnv *env, const CK_<Type>_PTR ckpValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
 * The functions that convert a Java object to a PKCS#11 pointer first allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
 * the memory for the PKCS#11 pointer. Then they set each element corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
 * to the fields in the Java object to convert. After use the allocated memory of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
 * the PKCS#11 pointer has to be explicitly freed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
 * The functions to convert a PKCS#11 pointer to a Java object create a new Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
 * object first and than they set all fields in the object depending on the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
 * of the type or structure where the PKCS#11 pointer points to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
 * converts a CK_BBOOL pointer to a Java boolean Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
 * @param env - used to call JNI funktions to create the new Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
 * @param ckpValue - the pointer to the CK_BBOOL value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
 * @return - the new Java boolean object with the boolean value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
jobject ckBBoolPtrToJBooleanObject(JNIEnv *env, const CK_BBOOL *ckpValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    jclass jValueObjectClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    jmethodID jConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    jobject jValueObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    jboolean jValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    jValueObjectClass = (*env)->FindClass(env, "java/lang/Boolean");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   905
    if (jValueObjectClass == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    jConstructor = (*env)->GetMethodID(env, jValueObjectClass, "<init>", "(Z)V");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   907
    if (jConstructor == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    jValue = ckBBoolToJBoolean(*ckpValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    jValueObject = (*env)->NewObject(env, jValueObjectClass, jConstructor, jValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    return jValueObject ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
 * converts a CK_ULONG pointer to a Java long Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
 * @param env - used to call JNI funktions to create the new Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
 * @param ckpValue - the pointer to the CK_ULONG value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
 * @return - the new Java long object with the long value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
jobject ckULongPtrToJLongObject(JNIEnv *env, const CK_ULONG_PTR ckpValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    jclass jValueObjectClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    jmethodID jConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    jobject jValueObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    jlong jValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    jValueObjectClass = (*env)->FindClass(env, "java/lang/Long");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   929
    if (jValueObjectClass == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    jConstructor = (*env)->GetMethodID(env, jValueObjectClass, "<init>", "(J)V");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   931
    if (jConstructor == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    jValue = ckULongToJLong(*ckpValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    jValueObject = (*env)->NewObject(env, jValueObjectClass, jConstructor, jValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    return jValueObject ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
 * converts a Java boolean object into a pointer to a CK_BBOOL value. The memory has to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
 * freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
 * @param env - used to call JNI funktions to get the value out of the Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
 * @param jObject - the "java/lang/Boolean" object to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
 * @return - the pointer to the new CK_BBOOL value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
CK_BBOOL* jBooleanObjectToCKBBoolPtr(JNIEnv *env, jobject jObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    jclass jObjectClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    jmethodID jValueMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    jboolean jValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    CK_BBOOL *ckpValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    jObjectClass = (*env)->FindClass(env, "java/lang/Boolean");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   954
    if (jObjectClass == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    jValueMethod = (*env)->GetMethodID(env, jObjectClass, "booleanValue", "()Z");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   956
    if (jValueMethod == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    jValue = (*env)->CallBooleanMethod(env, jObject, jValueMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    ckpValue = (CK_BBOOL *) malloc(sizeof(CK_BBOOL));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   959
    if (ckpValue == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   960
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   961
        return NULL;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   962
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    *ckpValue = jBooleanToCKBBool(jValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    return ckpValue ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
 * converts a Java byte object into a pointer to a CK_BYTE value. The memory has to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
 * freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
 * @param env - used to call JNI funktions to get the value out of the Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
 * @param jObject - the "java/lang/Byte" object to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
 * @return - the pointer to the new CK_BYTE value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
CK_BYTE_PTR jByteObjectToCKBytePtr(JNIEnv *env, jobject jObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    jclass jObjectClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    jmethodID jValueMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    jbyte jValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    CK_BYTE_PTR ckpValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    jObjectClass = (*env)->FindClass(env, "java/lang/Byte");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   984
    if (jObjectClass == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    jValueMethod = (*env)->GetMethodID(env, jObjectClass, "byteValue", "()B");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   986
    if (jValueMethod == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    jValue = (*env)->CallByteMethod(env, jObject, jValueMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    ckpValue = (CK_BYTE_PTR) malloc(sizeof(CK_BYTE));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   989
    if (ckpValue == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   990
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   991
        return NULL;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   992
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    *ckpValue = jByteToCKByte(jValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    return ckpValue ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
 * converts a Java integer object into a pointer to a CK_ULONG value. The memory has to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
 * freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
 * @param env - used to call JNI funktions to get the value out of the Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
 * @param jObject - the "java/lang/Integer" object to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
 * @return - the pointer to the new CK_ULONG value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
CK_ULONG* jIntegerObjectToCKULongPtr(JNIEnv *env, jobject jObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    jclass jObjectClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    jmethodID jValueMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    jint jValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    CK_ULONG *ckpValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    jObjectClass = (*env)->FindClass(env, "java/lang/Integer");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1013
    if (jObjectClass == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    jValueMethod = (*env)->GetMethodID(env, jObjectClass, "intValue", "()I");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1015
    if (jValueMethod == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    jValue = (*env)->CallIntMethod(env, jObject, jValueMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    ckpValue = (CK_ULONG *) malloc(sizeof(CK_ULONG));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1018
    if (ckpValue == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
  1019
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1020
        return NULL;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1021
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    *ckpValue = jLongToCKLong(jValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    return ckpValue ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
 * converts a Java long object into a pointer to a CK_ULONG value. The memory has to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
 * freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
 * @param env - used to call JNI funktions to get the value out of the Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
 * @param jObject - the "java/lang/Long" object to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
 * @return - the pointer to the new CK_ULONG value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
CK_ULONG* jLongObjectToCKULongPtr(JNIEnv *env, jobject jObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    jclass jObjectClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    jmethodID jValueMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    jlong jValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    CK_ULONG *ckpValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    jObjectClass = (*env)->FindClass(env, "java/lang/Long");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1042
    if (jObjectClass == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    jValueMethod = (*env)->GetMethodID(env, jObjectClass, "longValue", "()J");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1044
    if (jValueMethod == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    jValue = (*env)->CallLongMethod(env, jObject, jValueMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    ckpValue = (CK_ULONG *) malloc(sizeof(CK_ULONG));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1047
    if (ckpValue == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
  1048
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1049
        return NULL;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1050
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    *ckpValue = jLongToCKULong(jValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    return ckpValue ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
 * converts a Java char object into a pointer to a CK_CHAR value. The memory has to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
 * freed after use!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
 * @param env - used to call JNI funktions to get the value out of the Java object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
 * @param jObject - the "java/lang/Char" object to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
 * @return - the pointer to the new CK_CHAR value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
CK_CHAR_PTR jCharObjectToCKCharPtr(JNIEnv *env, jobject jObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    jclass jObjectClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    jmethodID jValueMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    jchar jValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    CK_CHAR_PTR ckpValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    jObjectClass = (*env)->FindClass(env, "java/lang/Char");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1072
    if (jObjectClass == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    jValueMethod = (*env)->GetMethodID(env, jObjectClass, "charValue", "()C");
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1074
    if (jValueMethod == NULL) { return NULL; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    jValue = (*env)->CallCharMethod(env, jObject, jValueMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    ckpValue = (CK_CHAR_PTR) malloc(sizeof(CK_CHAR));
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1077
    if (ckpValue == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
  1078
        throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1079
        return NULL;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1080
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    *ckpValue = jCharToCKChar(jValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    return ckpValue ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
 * converts a Java object into a pointer to CK-type or a CK-structure with the length in Bytes.
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1088
 * The memory of the returned pointer MUST BE FREED BY CALLER!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
 * @param env - used to call JNI funktions to get the Java classes and objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
 * @param jObject - the Java object to convert
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1092
 * @param ckpLength - pointer to the length (bytes) of the newly-allocated CK-value or CK-structure
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1093
 * @return ckpObject - pointer to the newly-allocated CK-value or CK-structure
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
 */
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1095
CK_VOID_PTR jObjectToPrimitiveCKObjectPtr(JNIEnv *env, jobject jObject, CK_ULONG *ckpLength)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
{
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1097
    jclass jLongClass, jBooleanClass, jByteArrayClass, jCharArrayClass;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1098
    jclass jByteClass, jDateClass, jCharacterClass, jIntegerClass;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1099
    jclass jBooleanArrayClass, jIntArrayClass, jLongArrayClass;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1100
    jclass jStringClass;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1101
    jclass jObjectClass, jClassClass;
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1102
    CK_VOID_PTR ckpObject;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    jmethodID jMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    jobject jClassObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    jstring jClassNameString;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1106
    char *classNameString, *exceptionMsgPrefix, *exceptionMsg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1108
    TRACE0("\nDEBUG: jObjectToPrimitiveCKObjectPtr");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    if (jObject == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        *ckpLength = 0;
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1111
        return NULL;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1112
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1113
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1114
    jLongClass = (*env)->FindClass(env, "java/lang/Long");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1115
    if (jLongClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1116
    if ((*env)->IsInstanceOf(env, jObject, jLongClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1117
        ckpObject = jLongObjectToCKULongPtr(env, jObject);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        *ckpLength = sizeof(CK_ULONG);
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1119
        TRACE1("<converted long value %X>", *((CK_ULONG *) ckpObject));
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1120
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1121
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1122
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1123
    jBooleanClass = (*env)->FindClass(env, "java/lang/Boolean");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1124
    if (jBooleanClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1125
    if ((*env)->IsInstanceOf(env, jObject, jBooleanClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1126
        ckpObject = jBooleanObjectToCKBBoolPtr(env, jObject);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        *ckpLength = sizeof(CK_BBOOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        TRACE0(" <converted boolean value ");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1129
        TRACE0((*((CK_BBOOL *) ckpObjectPtr) == TRUE) ? "TRUE>" : "FALSE>");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1130
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1131
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1132
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1133
    jByteArrayClass = (*env)->FindClass(env, "[B");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1134
    if (jByteArrayClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1135
    if ((*env)->IsInstanceOf(env, jObject, jByteArrayClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1136
        jByteArrayToCKByteArray(env, jObject, (CK_BYTE_PTR*) &ckpObject, ckpLength);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1137
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1138
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1139
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1140
    jCharArrayClass = (*env)->FindClass(env, "[C");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1141
    if (jCharArrayClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1142
    if ((*env)->IsInstanceOf(env, jObject, jCharArrayClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1143
        jCharArrayToCKUTF8CharArray(env, jObject, (CK_UTF8CHAR_PTR*) &ckpObject, ckpLength);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1144
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1145
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1146
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1147
    jByteClass = (*env)->FindClass(env, "java/lang/Byte");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1148
    if (jByteClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1149
    if ((*env)->IsInstanceOf(env, jObject, jByteClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1150
        ckpObject = jByteObjectToCKBytePtr(env, jObject);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        *ckpLength = sizeof(CK_BYTE);
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1152
        TRACE1("<converted byte value %X>", *((CK_BYTE *) ckpObject));
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1153
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1154
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1155
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1156
    jDateClass = (*env)->FindClass(env, CLASS_DATE);
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1157
    if (jDateClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1158
    if ((*env)->IsInstanceOf(env, jObject, jDateClass)) {
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1159
        ckpObject = jDateObjectToCKDatePtr(env, jObject);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        *ckpLength = sizeof(CK_DATE);
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1161
        TRACE3("<converted date value %.4s-%.2s-%.2s>", ((CK_DATE *) ckpObject)->year,
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1162
                ((CK_DATE *) ckpObject)->month, ((CK_DATE *) ckpObject)->day);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1163
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1164
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1165
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1166
    jCharacterClass = (*env)->FindClass(env, "java/lang/Character");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1167
    if (jCharacterClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1168
    if ((*env)->IsInstanceOf(env, jObject, jCharacterClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1169
        ckpObject = jCharObjectToCKCharPtr(env, jObject);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        *ckpLength = sizeof(CK_UTF8CHAR);
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1171
        TRACE1("<converted char value %c>", *((CK_CHAR *) ckpObject));
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1172
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1173
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1174
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1175
    jIntegerClass = (*env)->FindClass(env, "java/lang/Integer");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1176
    if (jIntegerClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1177
    if ((*env)->IsInstanceOf(env, jObject, jIntegerClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1178
        ckpObject = jIntegerObjectToCKULongPtr(env, jObject);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        *ckpLength = sizeof(CK_ULONG);
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1180
        TRACE1("<converted integer value %X>", *((CK_ULONG *) ckpObject));
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1181
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1182
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1183
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1184
    jBooleanArrayClass = (*env)->FindClass(env, "[Z");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1185
    if (jBooleanArrayClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1186
    if ((*env)->IsInstanceOf(env, jObject, jBooleanArrayClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1187
        jBooleanArrayToCKBBoolArray(env, jObject, (CK_BBOOL**) &ckpObject, ckpLength);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1188
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1189
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1190
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1191
    jIntArrayClass = (*env)->FindClass(env, "[I");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1192
    if (jIntArrayClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1193
    if ((*env)->IsInstanceOf(env, jObject, jIntArrayClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1194
        jLongArrayToCKULongArray(env, jObject, (CK_ULONG_PTR*) &ckpObject, ckpLength);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1195
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1196
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1197
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1198
    jLongArrayClass = (*env)->FindClass(env, "[J");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1199
    if (jLongArrayClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1200
    if ((*env)->IsInstanceOf(env, jObject, jLongArrayClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1201
        jLongArrayToCKULongArray(env, jObject, (CK_ULONG_PTR*) &ckpObject, ckpLength);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1202
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1203
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1204
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1205
    jStringClass = (*env)->FindClass(env, "java/lang/String");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1206
    if (jStringClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1207
    if ((*env)->IsInstanceOf(env, jObject, jStringClass)) {
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1208
        jStringToCKUTF8CharArray(env, jObject, (CK_UTF8CHAR_PTR*) &ckpObject, ckpLength);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1209
        return ckpObject;
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1210
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1212
    /* type of jObject unknown, throw PKCS11RuntimeException */
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1213
    jObjectClass = (*env)->FindClass(env, "java/lang/Object");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1214
    if (jObjectClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1215
    jMethod = (*env)->GetMethodID(env, jObjectClass, "getClass", "()Ljava/lang/Class;");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1216
    if (jMethod == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1217
    jClassObject = (*env)->CallObjectMethod(env, jObject, jMethod);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1218
    assert(jClassObject != 0);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1219
    jClassClass = (*env)->FindClass(env, "java/lang/Class");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1220
    if (jClassClass == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1221
    jMethod = (*env)->GetMethodID(env, jClassClass, "getName", "()Ljava/lang/String;");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1222
    if (jMethod == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1223
    jClassNameString = (jstring)
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1224
        (*env)->CallObjectMethod(env, jClassObject, jMethod);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1225
    assert(jClassNameString != 0);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1226
    classNameString = (char*)
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1227
        (*env)->GetStringUTFChars(env, jClassNameString, NULL);
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1228
    if (classNameString == NULL) { return NULL; }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1229
    exceptionMsgPrefix = "Java object of this class cannot be converted to native PKCS#11 type: ";
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1230
    exceptionMsg = (char *)
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1231
        malloc(strlen(exceptionMsgPrefix) + strlen(classNameString) + 1);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1232
    if (exceptionMsg == NULL) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1233
        (*env)->ReleaseStringUTFChars(env, jClassNameString, classNameString);
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
  1234
        throwOutOfMemoryError(env, 0);
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1235
        return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1237
    strcpy(exceptionMsg, exceptionMsgPrefix);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1238
    strcat(exceptionMsg, classNameString);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1239
    (*env)->ReleaseStringUTFChars(env, jClassNameString, classNameString);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1240
    throwPKCS11RuntimeException(env, exceptionMsg);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1241
    free(exceptionMsg);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1242
    *ckpLength = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    TRACE0("FINISHED\n");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents: 47216
diff changeset
  1245
    return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
}
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1247
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1248
#ifdef P11_MEMORYDEBUG
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1249
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1250
#undef malloc
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1251
#undef calloc
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1252
#undef free
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1253
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1254
void *p11malloc(size_t c, char *file, int line) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1255
    void *p = malloc(c);
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1256
    fprintf(stdout, "malloc\t%08lX\t%lX\t%s:%d\n", ptr_to_jlong(p), c, file, line);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1257
    fflush(stdout);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1258
    return p;
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1259
}
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1260
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1261
void *p11calloc(size_t c, size_t s, char *file, int line) {
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1262
    void *p = calloc(c, s);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1263
    fprintf(stdout, "calloc\t%08lX\t%lX\t%lX\t%s:%d\n", ptr_to_jlong(p), c, s, file, line);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1264
    fflush(stdout);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1265
    return p;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1266
}
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1267
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1268
void p11free(void *p, char *file, int line) {
57737
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1269
    fprintf(stdout, "free\t%08lX\t\t%s:%d\n", ptr_to_jlong(p), file, line);
6bbb4af131e3 8228835: Memory leak in PKCS11 provider when using AES GCM
valeriep
parents: 55332
diff changeset
  1270
    fflush(stdout);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1271
    free(p);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1272
}
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1273
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1274
#endif
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1275
39142
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1276
// prints a message to stdout if debug output is enabled
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1277
void printDebug(const char *format, ...) {
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1278
    if (debug == JNI_TRUE) {
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1279
        va_list args;
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1280
        fprintf(stdout, "sunpkcs11: ");
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1281
        va_start(args, format);
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1282
        vfprintf(stdout, format, args);
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1283
        va_end(args);
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1284
        fflush(stdout);
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1285
    }
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1286
}
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
  1287