src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43248 jdk/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c@5e15de85a1a0
child 51800 bccd9966f1ed
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39142
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2016, 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
#include "sun_security_pkcs11_wrapper_PKCS11.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#ifdef P11_ENABLE_C_GENERATEKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Method:    C_GenerateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @param   jobjectArray jTemplate      CK_ATTRIBUTE_PTR pTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *                                      CK_ULONG ulCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @return  jlong jKeyHandle            CK_OBJECT_HANDLE_PTR phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jobjectArray jTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    CK_ULONG ckAttributesLength;
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
    76
    CK_OBJECT_HANDLE ckKeyHandle = 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
    77
    jlong jKeyHandle = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    if (ckpFunctions == NULL) { return 0L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
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
    85
    if ((*env)->ExceptionCheck(env)) { return 0L ; }
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
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
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
    88
    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
    89
        if (ckMechanism.pParameter != NULL_PTR) {
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
    90
            free(ckMechanism.pParameter);
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
    91
        }
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
    92
        return 0L;
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
    93
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    rv = (*ckpFunctions->C_GenerateKey)(ckSessionHandle, &ckMechanism, ckpAttributes, ckAttributesLength, &ckKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
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
    97
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
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
    98
        jKeyHandle = ckULongToJLong(ckKeyHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
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
   100
        /* cheack, if we must give a initialization vector back to Java */
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
   101
        switch (ckMechanism.mechanism) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        case CKM_PBE_MD2_DES_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        case CKM_PBE_MD5_DES_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        case CKM_PBE_MD5_CAST_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        case CKM_PBE_MD5_CAST3_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        case CKM_PBE_MD5_CAST128_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        /* case CKM_PBE_MD5_CAST5_CBC:  the same as CKM_PBE_MD5_CAST128_CBC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        case CKM_PBE_SHA1_CAST128_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        /* case CKM_PBE_SHA1_CAST5_CBC: the same as CKM_PBE_SHA1_CAST128_CBC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            /* we must copy back the initialization vector to the jMechanism object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            copyBackPBEInitializationVector(env, &ckMechanism, jMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            break;
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
   113
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
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
   116
    if (ckMechanism.pParameter != NULL_PTR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
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
   119
    freeCKAttributeArray(ckpAttributes, ckAttributesLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    return jKeyHandle ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
#ifdef P11_ENABLE_C_GENERATEKEYPAIR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Method:    C_GenerateKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)[J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * Parametermapping:                          *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @param   jlong jSessionHandle              CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @param   jobject jMechanism                CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @param   jobjectArray jPublicKeyTemplate   CK_ATTRIBUTE_PTR pPublicKeyTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *                                            CK_ULONG ulPublicKeyAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * @param   jobjectArray jPrivateKeyTemplate  CK_ATTRIBUTE_PTR pPrivateKeyTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *                                            CK_ULONG ulPrivateKeyAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * @return  jlongArray jKeyHandles            CK_OBJECT_HANDLE_PTR phPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *                                            CK_OBJECT_HANDLE_PTR phPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     jobjectArray jPublicKeyTemplate, jobjectArray jPrivateKeyTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    CK_ATTRIBUTE_PTR ckpPublicKeyAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    CK_ATTRIBUTE_PTR ckpPrivateKeyAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    CK_ULONG ckPublicKeyAttributesLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    CK_ULONG ckPrivateKeyAttributesLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    CK_OBJECT_HANDLE_PTR ckpPublicKeyHandle;  /* pointer to Public Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    CK_OBJECT_HANDLE_PTR ckpPrivateKeyHandle; /* pointer to Private Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    CK_OBJECT_HANDLE_PTR ckpKeyHandles;     /* pointer to array with Public and Private Key */
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   153
    jlongArray jKeyHandles = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    CK_RV rv;
39142
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   155
    int attempts;
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   156
    const int MAX_ATTEMPTS = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    if (ckpFunctions == NULL) { return NULL; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
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
   163
    if ((*env)->ExceptionCheck(env)) { 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
   164
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    ckpKeyHandles = (CK_OBJECT_HANDLE_PTR) malloc(2 * sizeof(CK_OBJECT_HANDLE));
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
   166
    if (ckpKeyHandles == 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
   167
        if (ckMechanism.pParameter != NULL_PTR) {
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
   168
            free(ckMechanism.pParameter);
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
   169
        }
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   170
        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
   171
        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
   172
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    ckpPublicKeyHandle = ckpKeyHandles;   /* first element of array is Public Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    ckpPrivateKeyHandle = (ckpKeyHandles + 1);  /* second element of array is Private Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
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
   176
    jAttributeArrayToCKAttributeArray(env, jPublicKeyTemplate, &ckpPublicKeyAttributes, &ckPublicKeyAttributesLength);
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
   177
    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
   178
        if (ckMechanism.pParameter != NULL_PTR) {
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
   179
            free(ckMechanism.pParameter);
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
   180
        }
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
   181
        free(ckpKeyHandles);
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
   182
        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
   183
    }
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
   184
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
   185
    jAttributeArrayToCKAttributeArray(env, jPrivateKeyTemplate, &ckpPrivateKeyAttributes, &ckPrivateKeyAttributesLength);
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
   186
    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
   187
        if (ckMechanism.pParameter != NULL_PTR) {
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
   188
            free(ckMechanism.pParameter);
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
   189
        }
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
   190
        free(ckpKeyHandles);
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
   191
        freeCKAttributeArray(ckpPublicKeyAttributes, ckPublicKeyAttributesLength);
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
   192
        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
   193
    }
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
   194
39142
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   195
    /*
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   196
     * Workaround for NSS bug 1012786:
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   197
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   198
     * Key generation may fail with CKR_FUNCTION_FAILED error
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   199
     * if there is insufficient entropy to generate a random key.
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   200
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   201
     * PKCS11 spec says the following about CKR_FUNCTION_FAILED error
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   202
     * (see section 11.1.1):
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   203
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   204
     *      ... In any event, although the function call failed, the situation
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   205
     *      is not necessarily totally hopeless, as it is likely to be
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   206
     *      when CKR_GENERAL_ERROR is returned. Depending on what the root cause of
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   207
     *      the error actually was, it is possible that an attempt
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   208
     *      to make the exact same function call again would succeed.
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   209
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   210
     * Call C_GenerateKeyPair() several times if CKR_FUNCTION_FAILED occurs.
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   211
     */
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   212
    for (attempts = 0; attempts < MAX_ATTEMPTS; attempts++) {
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   213
        rv = (*ckpFunctions->C_GenerateKeyPair)(ckSessionHandle, &ckMechanism,
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   214
                        ckpPublicKeyAttributes, ckPublicKeyAttributesLength,
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   215
                        ckpPrivateKeyAttributes, ckPrivateKeyAttributesLength,
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   216
                        ckpPublicKeyHandle, ckpPrivateKeyHandle);
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   217
        if (rv == CKR_FUNCTION_FAILED) {
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   218
            printDebug("C_1GenerateKeyPair(): C_GenerateKeyPair() failed \
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   219
                    with CKR_FUNCTION_FAILED error, try again\n");
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   220
        } else {
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   221
            break;
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   222
        }
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   223
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
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
   225
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
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
   226
        jKeyHandles = ckULongArrayToJLongArray(env, ckpKeyHandles, 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    if(ckMechanism.pParameter != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    free(ckpKeyHandles);
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
   233
    freeCKAttributeArray(ckpPublicKeyAttributes, ckPublicKeyAttributesLength);
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
   234
    freeCKAttributeArray(ckpPrivateKeyAttributes, ckPrivateKeyAttributesLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    return jKeyHandles ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
#ifdef P11_ENABLE_C_WRAPKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * Method:    C_WrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;JJ)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * @param   jlong jWrappingKeyHandle    CK_OBJECT_HANDLE hWrappingKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * @param   jlong jKeyHandle            CK_OBJECT_HANDLE hKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * @return  jbyteArray jWrappedKey      CK_BYTE_PTR pWrappedKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *                                      CK_ULONG_PTR pulWrappedKeyLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1WrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jWrappingKeyHandle, jlong jKeyHandle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    CK_OBJECT_HANDLE ckWrappingKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    CK_OBJECT_HANDLE ckKeyHandle;
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
   260
    jbyteArray jWrappedKey = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    CK_BYTE BUF[MAX_STACK_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    CK_BYTE_PTR ckpWrappedKey = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    CK_ULONG ckWrappedKeyLength = MAX_STACK_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    if (ckpFunctions == NULL) { return NULL; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
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
   271
    if ((*env)->ExceptionCheck(env)) { 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
   272
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    ckWrappingKeyHandle = jLongToCKULong(jWrappingKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    ckKeyHandle = jLongToCKULong(jKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    if (rv == CKR_BUFFER_TOO_SMALL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        ckpWrappedKey = (CK_BYTE_PTR) malloc(ckWrappedKeyLength);
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
   279
        if (ckpWrappedKey == 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
   280
            if (ckMechanism.pParameter != NULL_PTR) {
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
   281
                free(ckMechanism.pParameter);
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
   282
            }
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   283
            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
   284
            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
   285
        }
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
   286
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        jWrappedKey = ckByteArrayToJByteArray(env, ckpWrappedKey, ckWrappedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
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
   293
    if (ckpWrappedKey != BUF) { free(ckpWrappedKey); }
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
   294
    if (ckMechanism.pParameter != NULL_PTR) {
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
   295
        free(ckMechanism.pParameter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    return jWrappedKey ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
#ifdef P11_ENABLE_C_UNWRAPKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * Method:    C_UnwrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J[B[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 * @param   jlong jUnwrappingKeyHandle  CK_OBJECT_HANDLE hUnwrappingKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * @param   jbyteArray jWrappedKey      CK_BYTE_PTR pWrappedKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *                                      CK_ULONG_PTR pulWrappedKeyLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * @param   jobjectArray jTemplate      CK_ATTRIBUTE_PTR pTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 *                                      CK_ULONG ulCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 * @return  jlong jKeyHandle            CK_OBJECT_HANDLE_PTR phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1UnwrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jUnwrappingKeyHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     jbyteArray jWrappedKey, jobjectArray jTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    CK_OBJECT_HANDLE ckUnwrappingKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    CK_BYTE_PTR ckpWrappedKey = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    CK_ULONG ckWrappedKeyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    CK_ULONG ckAttributesLength;
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   327
    CK_OBJECT_HANDLE ckKeyHandle = 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
   328
    jlong jKeyHandle = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    if (ckpFunctions == NULL) { return 0L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
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
   336
    if ((*env)->ExceptionCheck(env)) { return 0L; }
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
   337
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    ckUnwrappingKeyHandle = jLongToCKULong(jUnwrappingKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    jByteArrayToCKByteArray(env, jWrappedKey, &ckpWrappedKey, &ckWrappedKeyLength);
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
   340
    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
   341
        if (ckMechanism.pParameter != NULL_PTR) {
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
   342
            free(ckMechanism.pParameter);
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
   343
        }
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
   344
        return 0L;
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
   345
    }
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
   346
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
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
   348
    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
   349
        if (ckMechanism.pParameter != NULL_PTR) {
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
   350
            free(ckMechanism.pParameter);
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
   351
        }
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
   352
        free(ckpWrappedKey);
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
   353
        return 0L;
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
   354
    }
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
   355
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    rv = (*ckpFunctions->C_UnwrapKey)(ckSessionHandle, &ckMechanism, ckUnwrappingKeyHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                 ckpWrappedKey, ckWrappedKeyLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                 ckpAttributes, ckAttributesLength, &ckKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
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
   361
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
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
   362
        jKeyHandle = ckLongToJLong(ckKeyHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
#if 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
   365
        /* cheack, if we must give a initialization vector back to Java */
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
   366
        if (ckMechanism.mechanism == CKM_KEY_WRAP_SET_OAEP) {
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
   367
            /* we must copy back the unwrapped key info to the jMechanism object */
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
   368
            copyBackSetUnwrappedKey(env, &ckMechanism, jMechanism);
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
   369
        }
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
   370
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
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
   373
    if (ckMechanism.pParameter != NULL_PTR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
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
   376
    freeCKAttributeArray(ckpAttributes, ckAttributesLength);
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
   377
    free(ckpWrappedKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    return jKeyHandle ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
#ifdef P11_ENABLE_C_DERIVEKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
void freeMasterKeyDeriveParams(CK_MECHANISM_PTR ckMechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    CK_SSL3_MASTER_KEY_DERIVE_PARAMS *params = (CK_SSL3_MASTER_KEY_DERIVE_PARAMS *) ckMechanism->pParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    if (params == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    if (params->RandomInfo.pClientRandom != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        free(params->RandomInfo.pClientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    if (params->RandomInfo.pServerRandom != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        free(params->RandomInfo.pServerRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    if (params->pVersion != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        free(params->pVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
void freeEcdh1DeriveParams(CK_MECHANISM_PTR ckMechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    CK_ECDH1_DERIVE_PARAMS *params = (CK_ECDH1_DERIVE_PARAMS *) ckMechanism->pParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    if (params == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    if (params->pSharedData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        free(params->pSharedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    if (params->pPublicData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        free(params->pPublicData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
 * Copy back the PRF output to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
void copyBackTLSPrfParams(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
{
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
   421
    jclass jMechanismClass, jTLSPrfParamsClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    CK_TLS_PRF_PARAMS *ckTLSPrfParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    jobject jTLSPrfParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    jfieldID fieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    CK_MECHANISM_TYPE ckMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    jlong jMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    CK_BYTE_PTR output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    jobject jOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    jint jLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    jbyte* jBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /* get mechanism */
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
   434
    jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);
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
   435
    if (jMechanismClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "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
   437
    if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    ckMechanismType = jLongToCKULong(jMechanismType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    if (ckMechanismType != ckMechanism->mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        /* we do not have maching types, this should not occur */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /* get the native CK_TLS_PRF_PARAMS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    ckTLSPrfParams = (CK_TLS_PRF_PARAMS *) ckMechanism->pParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    if (ckTLSPrfParams != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        /* get the Java CK_TLS_PRF_PARAMS object (pParameter) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter", "Ljava/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
   450
        if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        jTLSPrfParams = (*env)->GetObjectField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        /* copy back the client IV */
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
   454
        jTLSPrfParamsClass = (*env)->FindClass(env, CLASS_TLS_PRF_PARAMS);
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
   455
        if (jTLSPrfParamsClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        fieldID = (*env)->GetFieldID(env, jTLSPrfParamsClass, "pOutput", "[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
   457
        if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        jOutput = (*env)->GetObjectField(env, jTLSPrfParams, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        output = ckTLSPrfParams->pOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // Note: we assume that the token returned exactly as many bytes as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // requested. Anything else would not make sense.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (jOutput != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            jLength = (*env)->GetArrayLength(env, jOutput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            jBytes = (*env)->GetByteArrayElements(env, jOutput, 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
   466
            if (jBytes == 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
   467
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            /* copy the bytes to the Java buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            for (i=0; i < jLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                jBytes[i] = ckByteToJByte(output[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            /* copy back the Java buffer to the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            (*env)->ReleaseByteArrayElements(env, jOutput, jBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // free malloc'd data
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
   477
        free(ckTLSPrfParams->pSeed);
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
   478
        free(ckTLSPrfParams->pLabel);
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
        free(ckTLSPrfParams->pulOutputLen);
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
        free(ckTLSPrfParams->pOutput);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
 * Method:    C_DeriveKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
 * @param   jlong jBaseKeyHandle        CK_OBJECT_HANDLE hBaseKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
 * @param   jobjectArray jTemplate      CK_ATTRIBUTE_PTR pTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
 *                                      CK_ULONG ulCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
 * @return  jlong jKeyHandle            CK_OBJECT_HANDLE_PTR phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DeriveKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jBaseKeyHandle, jobjectArray jTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    CK_OBJECT_HANDLE ckBaseKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    CK_ULONG ckAttributesLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    CK_OBJECT_HANDLE ckKeyHandle = 0;
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   505
    jlong jKeyHandle = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    CK_OBJECT_HANDLE_PTR phKey = &ckKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    if (ckpFunctions == NULL) { return 0L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
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
   514
    if ((*env)->ExceptionCheck(env)) { return 0L; }
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
    ckBaseKeyHandle = jLongToCKULong(jBaseKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
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
   518
    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
   519
        if (ckMechanism.pParameter != NULL_PTR) {
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
   520
            free(ckMechanism.pParameter);
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
   521
        }
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
        return 0L;
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
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    switch (ckMechanism.mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    case CKM_SSL3_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    case CKM_TLS_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    case CKM_TLS_PRF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        // these mechanism do not return a key handle via phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        // set to NULL in case pedantic implementations check for it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        phKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    rv = (*ckpFunctions->C_DeriveKey)(ckSessionHandle, &ckMechanism, ckBaseKeyHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                 ckpAttributes, ckAttributesLength, phKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    jKeyHandle = ckLongToJLong(ckKeyHandle);
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
   542
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
   543
    freeCKAttributeArray(ckpAttributes, ckAttributesLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    switch (ckMechanism.mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    case CKM_SSL3_MASTER_KEY_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    case CKM_TLS_MASTER_KEY_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        /* we must copy back the client version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        copyBackClientVersion(env, &ckMechanism, jMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        freeMasterKeyDeriveParams(&ckMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    case CKM_SSL3_MASTER_KEY_DERIVE_DH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    case CKM_TLS_MASTER_KEY_DERIVE_DH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        freeMasterKeyDeriveParams(&ckMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    case CKM_SSL3_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    case CKM_TLS_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        /* we must copy back the unwrapped key info to the jMechanism object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        copyBackSSLKeyMatParams(env, &ckMechanism, jMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    case CKM_TLS_PRF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        copyBackTLSPrfParams(env, &ckMechanism, jMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    case CKM_ECDH1_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        freeEcdh1DeriveParams(&ckMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
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
   572
    if (ckMechanism.pParameter != NULL_PTR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
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
   575
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    return jKeyHandle ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
 * Copy back the client version information from the native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
 * structure to the Java object. This is only used for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
 * CKM_SSL3_MASTER_KEY_DERIVE mechanism when used for deriving a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
void copyBackClientVersion(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
{
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
   588
  jclass jMechanismClass, jSSL3MasterKeyDeriveParamsClass, jVersionClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
  CK_SSL3_MASTER_KEY_DERIVE_PARAMS *ckSSL3MasterKeyDeriveParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
  CK_VERSION *ckVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
  jfieldID fieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
  CK_MECHANISM_TYPE ckMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
  jlong jMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
  jobject jSSL3MasterKeyDeriveParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
  jobject jVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
  /* get mechanism */
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
  jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);
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
  if (jMechanismClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
  fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "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
   601
  if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
  jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
  ckMechanismType = jLongToCKULong(jMechanismType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
  if (ckMechanismType != ckMechanism->mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /* we do not have maching types, this should not occur */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
  /* get the native CK_SSL3_MASTER_KEY_DERIVE_PARAMS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
  ckSSL3MasterKeyDeriveParams = (CK_SSL3_MASTER_KEY_DERIVE_PARAMS *) ckMechanism->pParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
  if (ckSSL3MasterKeyDeriveParams != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /* get the native CK_VERSION */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    ckVersion = ckSSL3MasterKeyDeriveParams->pVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    if (ckVersion != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
      /* get the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS (pParameter) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
      fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter", "Ljava/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
   617
      if (fieldID == 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
   618
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
      jSSL3MasterKeyDeriveParams = (*env)->GetObjectField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
      /* get the Java CK_VERSION */
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
   622
      jSSL3MasterKeyDeriveParamsClass = (*env)->FindClass(env, CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);
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
   623
      if (jSSL3MasterKeyDeriveParamsClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
      fieldID = (*env)->GetFieldID(env, jSSL3MasterKeyDeriveParamsClass, "pVersion", "L"CLASS_VERSION";");
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
   625
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
      jVersion = (*env)->GetObjectField(env, jSSL3MasterKeyDeriveParams, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
      /* now copy back the version from the native structure to the Java structure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
      /* copy back the major version */
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
   631
      jVersionClass = (*env)->FindClass(env, CLASS_VERSION);
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
   632
      if (jVersionClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
      fieldID = (*env)->GetFieldID(env, jVersionClass, "major", "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
   634
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
      (*env)->SetByteField(env, jVersion, fieldID, ckByteToJByte(ckVersion->major));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
      /* copy back the minor version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
      fieldID = (*env)->GetFieldID(env, jVersionClass, "minor", "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
   639
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
      (*env)->SetByteField(env, jVersion, fieldID, ckByteToJByte(ckVersion->minor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
 * Copy back the derived keys and initialization vectors from the native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
 * structure to the Java object. This is only used for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
 * CKM_SSL3_KEY_AND_MAC_DERIVE mechanism when used for deriving a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
void copyBackSSLKeyMatParams(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
{
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
   654
  jclass jMechanismClass, jSSL3KeyMatParamsClass, jSSL3KeyMatOutClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
  CK_SSL3_KEY_MAT_PARAMS *ckSSL3KeyMatParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
  CK_SSL3_KEY_MAT_OUT *ckSSL3KeyMatOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
  jfieldID fieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
  CK_MECHANISM_TYPE ckMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
  jlong jMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
  CK_BYTE_PTR iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
  jobject jSSL3KeyMatParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
  jobject jSSL3KeyMatOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
  jobject jIV;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
  jint jLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
  jbyte* jBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
  /* get mechanism */
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
   669
  jMechanismClass= (*env)->FindClass(env, CLASS_MECHANISM);
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
   670
  if (jMechanismClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
  fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "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
   672
  if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
  jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
  ckMechanismType = jLongToCKULong(jMechanismType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
  if (ckMechanismType != ckMechanism->mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /* we do not have maching types, this should not occur */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
  /* get the native CK_SSL3_KEY_MAT_PARAMS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
  ckSSL3KeyMatParam = (CK_SSL3_KEY_MAT_PARAMS *) ckMechanism->pParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
  if (ckSSL3KeyMatParam != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    // free malloc'd data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    if (ckSSL3KeyMatParam->RandomInfo.pClientRandom != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        free(ckSSL3KeyMatParam->RandomInfo.pClientRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    if (ckSSL3KeyMatParam->RandomInfo.pServerRandom != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        free(ckSSL3KeyMatParam->RandomInfo.pServerRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /* get the native CK_SSL3_KEY_MAT_OUT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    ckSSL3KeyMatOut = ckSSL3KeyMatParam->pReturnedKeyMaterial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    if (ckSSL3KeyMatOut != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
      /* get the Java CK_SSL3_KEY_MAT_PARAMS (pParameter) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
      fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter", "Ljava/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
   696
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
      jSSL3KeyMatParam = (*env)->GetObjectField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
      /* get the Java CK_SSL3_KEY_MAT_OUT */
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
   700
      jSSL3KeyMatParamsClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_PARAMS);
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
   701
      if (jSSL3KeyMatParamsClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatParamsClass, "pReturnedKeyMaterial", "L"CLASS_SSL3_KEY_MAT_OUT";");
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
   703
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
      jSSL3KeyMatOut = (*env)->GetObjectField(env, jSSL3KeyMatParam, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
      /* now copy back all the key handles and the initialization vectors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
      /* copy back client MAC secret handle */
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
   708
      jSSL3KeyMatOutClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_OUT);
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
   709
      if (jSSL3KeyMatOutClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "hClientMacSecret", "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
   711
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID, ckULongToJLong(ckSSL3KeyMatOut->hClientMacSecret));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
      /* copy back server MAC secret handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "hServerMacSecret", "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
   716
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID, ckULongToJLong(ckSSL3KeyMatOut->hServerMacSecret));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
      /* copy back client secret key handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "hClientKey", "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
   721
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID, ckULongToJLong(ckSSL3KeyMatOut->hClientKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
      /* copy back server secret key handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "hServerKey", "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
   726
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID, ckULongToJLong(ckSSL3KeyMatOut->hServerKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
      /* copy back the client IV */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "pIVClient", "[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
   731
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
      jIV = (*env)->GetObjectField(env, jSSL3KeyMatOut, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
      iv = ckSSL3KeyMatOut->pIVClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
      if (jIV != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        jLength = (*env)->GetArrayLength(env, jIV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        jBytes = (*env)->GetByteArrayElements(env, jIV, 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
   738
        if (jBytes == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        /* copy the bytes to the Java buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        for (i=0; i < jLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
          jBytes[i] = ckByteToJByte(iv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        /* copy back the Java buffer to the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        (*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
      }
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
   746
      // free malloc'd data
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
      free(ckSSL3KeyMatOut->pIVClient);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
      /* copy back the server IV */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "pIVServer", "[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
   751
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
      jIV = (*env)->GetObjectField(env, jSSL3KeyMatOut, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
      iv = ckSSL3KeyMatOut->pIVServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
      if (jIV != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        jLength = (*env)->GetArrayLength(env, jIV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        jBytes = (*env)->GetByteArrayElements(env, jIV, 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
   758
        if (jBytes == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        /* copy the bytes to the Java buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        for (i=0; i < jLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
          jBytes[i] = ckByteToJByte(iv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        /* copy back the Java buffer to the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        (*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
      // free malloc'd data
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
   767
      free(ckSSL3KeyMatOut->pIVServer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
      free(ckSSL3KeyMatOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
#endif