src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c
author mbalao
Mon, 15 Apr 2019 15:52:38 -0300
changeset 54578 895a6a380484
parent 53856 15ae25f7eefd
child 55332 f492567244ab
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8220513: Wrapper Key may get deleted when closing sessions in SunPKCS11 crypto provider Summary: Do not close the session holding the Wrapper Key while in use. Delete the Wrapper Key when no longer needed. Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/* Copyright  (c) 2002 Graz University of Technology. All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * Redistribution and use in  source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * modification, are permitted  provided that the following conditions are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * 1. Redistributions of  source code must retain the above copyright notice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *    this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * 2. Redistributions in  binary form must reproduce the above copyright notice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *    this list of conditions and the following disclaimer in the documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *    and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 3. The end-user documentation included with the redistribution, if any, must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *    include the following acknowledgment:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *    "This product includes software developed by IAIK of Graz University of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 *     Technology."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 *    Alternately, this acknowledgment may appear in the software itself, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 *    and wherever such third-party acknowledgments normally appear.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * 4. The names "Graz University of Technology" and "IAIK of Graz University of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *    Technology" must not be used to endorse or promote products derived from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *    this software without prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * 5. Products derived from this software may not be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *    "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *    written permission of Graz University of Technology.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  POSSIBILITY  OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include "pkcs11wrapper.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#include "sun_security_pkcs11_wrapper_PKCS11.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    57
#ifdef P11_ENABLE_GETNATIVEKEYINFO
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    58
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    59
#define CK_ATTRIBUTES_TEMPLATE_LENGTH (CK_ULONG)61U
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    60
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    61
static CK_ATTRIBUTE ckpAttributesTemplate[CK_ATTRIBUTES_TEMPLATE_LENGTH] = {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    62
        {CKA_CLASS, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    63
        {CKA_TOKEN, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    64
        {CKA_PRIVATE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    65
        {CKA_LABEL, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    66
        {CKA_APPLICATION, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    67
        {CKA_VALUE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    68
        {CKA_OBJECT_ID, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    69
        {CKA_CERTIFICATE_TYPE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    70
        {CKA_ISSUER, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    71
        {CKA_SERIAL_NUMBER, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    72
        {CKA_AC_ISSUER, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    73
        {CKA_OWNER, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    74
        {CKA_ATTR_TYPES, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    75
        {CKA_TRUSTED, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    76
        {CKA_KEY_TYPE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    77
        {CKA_SUBJECT, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    78
        {CKA_ID, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    79
        {CKA_SENSITIVE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    80
        {CKA_ENCRYPT, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    81
        {CKA_DECRYPT, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    82
        {CKA_WRAP, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    83
        {CKA_UNWRAP, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    84
        {CKA_SIGN, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    85
        {CKA_SIGN_RECOVER, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    86
        {CKA_VERIFY, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    87
        {CKA_VERIFY_RECOVER, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    88
        {CKA_DERIVE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    89
        {CKA_START_DATE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    90
        {CKA_END_DATE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    91
        {CKA_MODULUS, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    92
        {CKA_MODULUS_BITS, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    93
        {CKA_PUBLIC_EXPONENT, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    94
        {CKA_PRIVATE_EXPONENT, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    95
        {CKA_PRIME_1, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    96
        {CKA_PRIME_2, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    97
        {CKA_EXPONENT_1, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    98
        {CKA_EXPONENT_2, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
    99
        {CKA_COEFFICIENT, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   100
        {CKA_PRIME, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   101
        {CKA_SUBPRIME, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   102
        {CKA_BASE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   103
        {CKA_PRIME_BITS, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   104
        {CKA_SUB_PRIME_BITS, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   105
        {CKA_VALUE_BITS, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   106
        {CKA_VALUE_LEN, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   107
        {CKA_EXTRACTABLE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   108
        {CKA_LOCAL, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   109
        {CKA_NEVER_EXTRACTABLE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   110
        {CKA_ALWAYS_SENSITIVE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   111
        {CKA_KEY_GEN_MECHANISM, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   112
        {CKA_MODIFIABLE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   113
        {CKA_ECDSA_PARAMS, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   114
        {CKA_EC_PARAMS, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   115
        {CKA_EC_POINT, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   116
        {CKA_SECONDARY_AUTH, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   117
        {CKA_AUTH_PIN_FLAGS, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   118
        {CKA_HW_FEATURE_TYPE, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   119
        {CKA_RESET_ON_INIT, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   120
        {CKA_HAS_RESET, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   121
        {CKA_VENDOR_DEFINED, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   122
        {CKA_NETSCAPE_DB, 0, 0},
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   123
};
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   124
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   125
/*
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   126
 * Class:     sun_security_pkcs11_wrapper_PKCS11
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   127
 * Method:    getNativeKeyInfo
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   128
 * Signature: (JJJLsun/security/pkcs11/wrapper/CK_MECHANISM;)[B
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   129
 * Parametermapping:                         *PKCS11*
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   130
 * @param   jlong         jSessionHandle     CK_SESSION_HANDLE hSession
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   131
 * @param   jlong         jKeyHandle         CK_OBJECT_HANDLE hObject
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   132
 * @param   jlong         jWrappingKeyHandle CK_OBJECT_HANDLE hObject
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   133
 * @param   jobject       jWrappingMech      CK_MECHANISM_PTR pMechanism
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   134
 * @return  jbyteArray    jNativeKeyInfo     -
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   135
 */
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   136
JNIEXPORT jbyteArray JNICALL
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   137
Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   138
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jKeyHandle,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   139
    jlong jWrappingKeyHandle, jobject jWrappingMech)
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   140
{
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   141
    jbyteArray returnValue = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   142
    CK_SESSION_HANDLE ckSessionHandle = jLongToCKULong(jSessionHandle);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   143
    CK_OBJECT_HANDLE ckObjectHandle = jLongToCKULong(jKeyHandle);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   144
    CK_ATTRIBUTE_PTR ckpAttributes = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   145
    CK_RV rv;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   146
    jbyteArray nativeKeyInfoArray = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   147
    jbyteArray nativeKeyInfoWrappedKeyArray = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   148
    jbyte* nativeKeyInfoArrayRaw = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   149
    jbyte* nativeKeyInfoWrappedKeyArrayRaw = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   150
    unsigned int sensitiveAttributePosition = (unsigned int)-1;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   151
    unsigned int i = 0U;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   152
    unsigned long totalDataSize = 0UL, attributesCount = 0UL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   153
    unsigned long totalCkAttributesSize = 0UL, totalNativeKeyInfoArraySize = 0UL;
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   154
    jbyte* wrappedKeySizePtr = NULL;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   155
    jbyte* nativeKeyInfoArrayRawCkAttributes = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   156
    jbyte* nativeKeyInfoArrayRawCkAttributesPtr = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   157
    jbyte* nativeKeyInfoArrayRawDataPtr = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   158
    CK_MECHANISM ckMechanism;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   159
    char iv[16] = {0x0};
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   160
    CK_ULONG ckWrappedKeyLength = 0U;
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   161
    jbyte* wrappedKeySizeWrappedKeyArrayPtr = NULL;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   162
    CK_BYTE_PTR wrappedKeyBufferPtr = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   163
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   164
    CK_OBJECT_CLASS class;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   165
    CK_KEY_TYPE keyType;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   166
    CK_BBOOL sensitive;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   167
    CK_BBOOL netscapeAttributeValueNeeded = CK_FALSE;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   168
    CK_ATTRIBUTE ckNetscapeAttributesTemplate[4];
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   169
    ckNetscapeAttributesTemplate[0].type = CKA_CLASS;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   170
    ckNetscapeAttributesTemplate[1].type = CKA_KEY_TYPE;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   171
    ckNetscapeAttributesTemplate[2].type = CKA_SENSITIVE;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   172
    ckNetscapeAttributesTemplate[3].type = CKA_NETSCAPE_DB;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   173
    ckNetscapeAttributesTemplate[0].pValue = &class;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   174
    ckNetscapeAttributesTemplate[1].pValue = &keyType;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   175
    ckNetscapeAttributesTemplate[2].pValue = &sensitive;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   176
    ckNetscapeAttributesTemplate[3].pValue = 0;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   177
    ckNetscapeAttributesTemplate[0].ulValueLen = sizeof(class);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   178
    ckNetscapeAttributesTemplate[1].ulValueLen = sizeof(keyType);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   179
    ckNetscapeAttributesTemplate[2].ulValueLen = sizeof(sensitive);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   180
    ckNetscapeAttributesTemplate[3].ulValueLen = 0;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   181
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   182
    if (ckpFunctions == NULL) { goto cleanup; }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   183
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   184
    // If key is private and of DSA or EC type, NSS may require CKA_NETSCAPE_DB
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   185
    // attribute to unwrap it.
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   186
    rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   187
            ckNetscapeAttributesTemplate,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   188
            sizeof(ckNetscapeAttributesTemplate)/sizeof(CK_ATTRIBUTE));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   189
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   190
    if (rv == CKR_OK && class == CKO_PRIVATE_KEY &&
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   191
            (keyType == CKK_EC || keyType == CKK_DSA) &&
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   192
            sensitive == CK_TRUE &&
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   193
            ckNetscapeAttributesTemplate[3].ulValueLen == CK_UNAVAILABLE_INFORMATION) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   194
        // We cannot set the attribute through C_SetAttributeValue here
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   195
        // because it might be read-only. However, we can add it to
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   196
        // the extracted buffer.
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   197
        netscapeAttributeValueNeeded = CK_TRUE;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   198
        TRACE0("DEBUG: override CKA_NETSCAPE_DB attr value to TRUE\n");
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   199
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   200
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   201
    ckpAttributes = (CK_ATTRIBUTE_PTR)malloc(
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   202
            CK_ATTRIBUTES_TEMPLATE_LENGTH * sizeof(CK_ATTRIBUTE));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   203
    if (ckpAttributes == NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   204
        throwOutOfMemoryError(env, 0);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   205
        goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   206
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   207
    memcpy(ckpAttributes, ckpAttributesTemplate,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   208
            CK_ATTRIBUTES_TEMPLATE_LENGTH * sizeof(CK_ATTRIBUTE));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   209
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   210
    // Get sizes for value buffers
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   211
    // NOTE: may return an error code but length values are filled anyways
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   212
    (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   213
            ckpAttributes, CK_ATTRIBUTES_TEMPLATE_LENGTH);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   214
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   215
    for (i = 0; i < CK_ATTRIBUTES_TEMPLATE_LENGTH; i++) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   216
        if ((ckpAttributes+i)->ulValueLen != CK_UNAVAILABLE_INFORMATION) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   217
            totalDataSize += (ckpAttributes+i)->ulValueLen;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   218
            if ((ckpAttributes+i)->type == CKA_SENSITIVE) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   219
                 sensitiveAttributePosition = attributesCount;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   220
                 TRACE0("DEBUG: GetNativeKeyInfo key is sensitive");
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   221
            }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   222
            attributesCount++;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   223
        }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   224
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   225
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   226
    if (netscapeAttributeValueNeeded) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   227
        attributesCount++;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   228
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   229
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   230
    // Allocate a single buffer to hold valid attributes and attribute's values
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   231
    // Buffer structure: [ attributes-size, [ ... attributes ... ],
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   232
    //                   values-size, [ ... values ... ], wrapped-key-size,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   233
    //                   [ ... wrapped-key ... ] ]
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   234
    //     * sizes are expressed in bytes and data type is unsigned long
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   235
    totalCkAttributesSize = attributesCount * sizeof(CK_ATTRIBUTE);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   236
    TRACE1("DEBUG: GetNativeKeyInfo attributesCount = %lu\n", attributesCount);
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   237
    TRACE1("DEBUG: GetNativeKeyInfo sizeof CK_ATTRIBUTE = %zu\n", sizeof(CK_ATTRIBUTE));
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   238
    TRACE1("DEBUG: GetNativeKeyInfo totalCkAttributesSize = %lu\n", totalCkAttributesSize);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   239
    TRACE1("DEBUG: GetNativeKeyInfo totalDataSize = %lu\n", totalDataSize);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   240
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   241
    totalNativeKeyInfoArraySize =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   242
            totalCkAttributesSize + sizeof(unsigned long) * 3 + totalDataSize;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   243
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   244
    TRACE1("DEBUG: GetNativeKeyInfo totalNativeKeyInfoArraySize = %lu\n", totalNativeKeyInfoArraySize);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   245
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   246
    nativeKeyInfoArray = (*env)->NewByteArray(env, totalNativeKeyInfoArraySize);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   247
    if (nativeKeyInfoArray == NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   248
        goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   249
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   250
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   251
    nativeKeyInfoArrayRaw = (*env)->GetByteArrayElements(env, nativeKeyInfoArray,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   252
            NULL);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   253
    if (nativeKeyInfoArrayRaw == NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   254
        goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   255
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   256
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   257
    wrappedKeySizePtr = nativeKeyInfoArrayRaw +
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   258
            sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   259
    memcpy(nativeKeyInfoArrayRaw, &totalCkAttributesSize, sizeof(unsigned long));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   260
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   261
    memcpy(nativeKeyInfoArrayRaw + sizeof(unsigned long) + totalCkAttributesSize,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   262
        &totalDataSize, sizeof(unsigned long));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   263
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   264
    memset(wrappedKeySizePtr, 0, sizeof(unsigned long));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   265
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   266
    nativeKeyInfoArrayRawCkAttributes = nativeKeyInfoArrayRaw +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   267
            sizeof(unsigned long);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   268
    nativeKeyInfoArrayRawCkAttributesPtr = nativeKeyInfoArrayRawCkAttributes;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   269
    nativeKeyInfoArrayRawDataPtr = nativeKeyInfoArrayRaw +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   270
            totalCkAttributesSize + sizeof(unsigned long) * 2;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   271
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   272
    for (i = 0; i < CK_ATTRIBUTES_TEMPLATE_LENGTH; i++) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   273
        if ((ckpAttributes+i)->ulValueLen != CK_UNAVAILABLE_INFORMATION) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   274
            (*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).type =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   275
                    (ckpAttributes+i)->type;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   276
            (*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).ulValueLen =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   277
                    (ckpAttributes+i)->ulValueLen;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   278
            if ((ckpAttributes+i)->ulValueLen != 0) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   279
                (*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).pValue =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   280
                        nativeKeyInfoArrayRawDataPtr;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   281
            } else {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   282
                (*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).pValue = 0;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   283
            }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   284
            nativeKeyInfoArrayRawDataPtr +=
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   285
                    (*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).ulValueLen;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   286
            nativeKeyInfoArrayRawCkAttributesPtr += sizeof(CK_ATTRIBUTE);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   287
        }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   288
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   289
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   290
    TRACE0("DEBUG: GetNativeKeyInfo finished prepping nativeKeyInfoArray\n");
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   291
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   292
    // Get attribute's values
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   293
    rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   294
            (CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   295
            attributesCount);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   296
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   297
        goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   298
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   299
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   300
    TRACE0("DEBUG: GetNativeKeyInfo 1st C_GetAttributeValue call passed\n");
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   301
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   302
    if (netscapeAttributeValueNeeded) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   303
        (*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).type = CKA_NETSCAPE_DB;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   304
        // Value is not needed, public key is not used
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   305
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   306
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   307
    if ((sensitiveAttributePosition != (unsigned int)-1) &&
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   308
        *(CK_BBOOL*)(((CK_ATTRIBUTE_PTR)(((CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes)
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   309
                +sensitiveAttributePosition))->pValue) == CK_TRUE) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   310
        // Key is sensitive. Need to extract it wrapped.
54578
895a6a380484 8220513: Wrapper Key may get deleted when closing sessions in SunPKCS11 crypto provider
mbalao
parents: 53856
diff changeset
   311
        if (jWrappingKeyHandle != 0) {
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   312
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   313
            jMechanismToCKMechanism(env, jWrappingMech, &ckMechanism);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   314
            rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   315
                    jLongToCKULong(jWrappingKeyHandle), ckObjectHandle,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   316
                    NULL_PTR, &ckWrappedKeyLength);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   317
            if (ckWrappedKeyLength != 0) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   318
                // Allocate space for getting the wrapped key
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   319
                nativeKeyInfoWrappedKeyArray = (*env)->NewByteArray(env,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   320
                        totalNativeKeyInfoArraySize + ckWrappedKeyLength);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   321
                if (nativeKeyInfoWrappedKeyArray == NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   322
                    goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   323
                }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   324
                nativeKeyInfoWrappedKeyArrayRaw =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   325
                        (*env)->GetByteArrayElements(env,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   326
                                nativeKeyInfoWrappedKeyArray, NULL);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   327
                if (nativeKeyInfoWrappedKeyArrayRaw == NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   328
                    goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   329
                }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   330
                memcpy(nativeKeyInfoWrappedKeyArrayRaw, nativeKeyInfoArrayRaw,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   331
                        totalNativeKeyInfoArraySize);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   332
                wrappedKeySizeWrappedKeyArrayPtr =
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   333
                        nativeKeyInfoWrappedKeyArrayRaw +
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   334
                        sizeof(unsigned long)*2 + totalCkAttributesSize +
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   335
                        totalDataSize;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   336
                memcpy(wrappedKeySizeWrappedKeyArrayPtr, &ckWrappedKeyLength, sizeof(unsigned long));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   337
                TRACE1("DEBUG: GetNativeKeyInfo 1st C_WrapKey wrappedKeyLength = %lu\n", ckWrappedKeyLength);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   338
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   339
                wrappedKeyBufferPtr =
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   340
                        (CK_BYTE_PTR) (wrappedKeySizeWrappedKeyArrayPtr +
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   341
                        sizeof(unsigned long));
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   342
                rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   343
                        jLongToCKULong(jWrappingKeyHandle),ckObjectHandle,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   344
                        wrappedKeyBufferPtr, &ckWrappedKeyLength);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   345
                if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   346
                    goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   347
                }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   348
                memcpy(wrappedKeySizeWrappedKeyArrayPtr, &ckWrappedKeyLength, sizeof(unsigned long));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   349
                TRACE1("DEBUG: GetNativeKeyInfo 2nd C_WrapKey wrappedKeyLength = %lu\n", ckWrappedKeyLength);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   350
            } else {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   351
                goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   352
            }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   353
        } else {
54578
895a6a380484 8220513: Wrapper Key may get deleted when closing sessions in SunPKCS11 crypto provider
mbalao
parents: 53856
diff changeset
   354
            ckAssertReturnValueOK(env, CKR_KEY_HANDLE_INVALID);
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   355
            goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   356
        }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   357
        returnValue = nativeKeyInfoWrappedKeyArray;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   358
    } else {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   359
        returnValue = nativeKeyInfoArray;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   360
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   361
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   362
cleanup:
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   363
    if (ckpAttributes != NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   364
        free(ckpAttributes);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   365
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   366
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   367
    if (nativeKeyInfoArrayRaw != NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   368
        (*env)->ReleaseByteArrayElements(env, nativeKeyInfoArray,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   369
                nativeKeyInfoArrayRaw, 0);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   370
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   371
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   372
    if (nativeKeyInfoWrappedKeyArrayRaw != NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   373
        (*env)->ReleaseByteArrayElements(env, nativeKeyInfoWrappedKeyArray,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   374
                nativeKeyInfoWrappedKeyArrayRaw, 0);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   375
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   376
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   377
    if (nativeKeyInfoArray != NULL && returnValue != nativeKeyInfoArray) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   378
        (*env)->DeleteLocalRef(env, nativeKeyInfoArray);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   379
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   380
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   381
    if (nativeKeyInfoWrappedKeyArray != NULL
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   382
            && returnValue != nativeKeyInfoWrappedKeyArray) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   383
        (*env)->DeleteLocalRef(env, nativeKeyInfoWrappedKeyArray);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   384
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   385
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   386
    return returnValue;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   387
}
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   388
#endif
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   389
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   390
#ifdef P11_ENABLE_CREATENATIVEKEY
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   391
/*
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   392
 * Class:     sun_security_pkcs11_wrapper_PKCS11
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   393
 * Method:    createNativeKey
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   394
 * Signature: (J[BJLsun/security/pkcs11/wrapper/CK_MECHANISM;)J
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   395
 * Parametermapping:                          *PKCS11*
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   396
 * @param   jlong         jSessionHandle      CK_SESSION_HANDLE hSession
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   397
 * @param   jbyteArray    jNativeKeyInfo      -
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   398
 * @param   jlong         jWrappingKeyHandle  CK_OBJECT_HANDLE hObject
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   399
 * @param   jobject       jWrappingMech       CK_MECHANISM_PTR pMechanism
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   400
 * @return  jlong         jKeyHandle          CK_OBJECT_HANDLE hObject
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   401
 */
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   402
JNIEXPORT jlong JNICALL
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   403
Java_sun_security_pkcs11_wrapper_PKCS11_createNativeKey
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   404
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jNativeKeyInfo,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   405
    jlong jWrappingKeyHandle, jobject jWrappingMech)
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   406
{
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   407
    CK_OBJECT_HANDLE ckObjectHandle;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   408
    CK_RV rv;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   409
    CK_SESSION_HANDLE ckSessionHandle = jLongToCKULong(jSessionHandle);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   410
    jbyte* nativeKeyInfoArrayRaw = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   411
    jlong jObjectHandle = 0L;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   412
    unsigned long totalCkAttributesSize = 0UL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   413
    unsigned long nativeKeyInfoCkAttributesCount = 0UL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   414
    jbyte* nativeKeyInfoArrayRawCkAttributes = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   415
    jbyte* nativeKeyInfoArrayRawCkAttributesPtr = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   416
    jbyte* nativeKeyInfoArrayRawDataPtr = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   417
    unsigned long totalDataSize = 0UL;
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   418
    jbyte* wrappedKeySizePtr = NULL;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   419
    unsigned int i = 0U;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   420
    CK_MECHANISM ckMechanism;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   421
    char iv[16] = {0x0};
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   422
    CK_ULONG ckWrappedKeyLength = 0UL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   423
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   424
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   425
    if (ckpFunctions == NULL) { goto cleanup; }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   426
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   427
    nativeKeyInfoArrayRaw =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   428
            (*env)->GetByteArrayElements(env, jNativeKeyInfo, NULL);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   429
    if (nativeKeyInfoArrayRaw == NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   430
        goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   431
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   432
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   433
    memcpy(&totalCkAttributesSize, nativeKeyInfoArrayRaw, sizeof(unsigned long));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   434
    TRACE1("DEBUG: createNativeKey totalCkAttributesSize = %lu\n", totalCkAttributesSize);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   435
    nativeKeyInfoCkAttributesCount = totalCkAttributesSize/sizeof(CK_ATTRIBUTE);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   436
    TRACE1("DEBUG: createNativeKey nativeKeyInfoCkAttributesCount = %lu\n", nativeKeyInfoCkAttributesCount);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   437
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   438
    nativeKeyInfoArrayRawCkAttributes = nativeKeyInfoArrayRaw +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   439
            sizeof(unsigned long);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   440
    nativeKeyInfoArrayRawCkAttributesPtr = nativeKeyInfoArrayRawCkAttributes;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   441
    nativeKeyInfoArrayRawDataPtr = nativeKeyInfoArrayRaw +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   442
            totalCkAttributesSize + sizeof(unsigned long) * 2;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   443
    memcpy(&totalDataSize, (nativeKeyInfoArrayRaw + totalCkAttributesSize + sizeof(unsigned long)),
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   444
            sizeof(unsigned long));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   445
    TRACE1("DEBUG: createNativeKey totalDataSize = %lu\n", totalDataSize);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   446
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   447
    wrappedKeySizePtr = nativeKeyInfoArrayRaw +
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   448
            sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   449
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   450
    memcpy(&ckWrappedKeyLength, wrappedKeySizePtr, sizeof(unsigned long));
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   451
    TRACE1("DEBUG: createNativeKey wrappedKeyLength = %lu\n", ckWrappedKeyLength);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   452
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   453
    for (i = 0; i < nativeKeyInfoCkAttributesCount; i++) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   454
        if ((*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).ulValueLen
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   455
                > 0) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   456
            (*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).pValue =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   457
                    nativeKeyInfoArrayRawDataPtr;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   458
        }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   459
        nativeKeyInfoArrayRawDataPtr +=
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   460
                (*(CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributesPtr).ulValueLen;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   461
        nativeKeyInfoArrayRawCkAttributesPtr += sizeof(CK_ATTRIBUTE);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   462
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   463
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   464
    if (ckWrappedKeyLength == 0) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   465
        // Not a wrapped key
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   466
        rv = (*ckpFunctions->C_CreateObject)(ckSessionHandle,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   467
                (CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   468
                jLongToCKULong(nativeKeyInfoCkAttributesCount), &ckObjectHandle);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   469
    } else {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   470
        // Wrapped key
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   471
        jMechanismToCKMechanism(env, jWrappingMech, &ckMechanism);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   472
        rv = (*ckpFunctions->C_UnwrapKey)(ckSessionHandle, &ckMechanism,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   473
                jLongToCKULong(jWrappingKeyHandle),
53856
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   474
                (CK_BYTE_PTR)(wrappedKeySizePtr + sizeof(unsigned long)),
15ae25f7eefd 8216597: SIGBUS in Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo after JDK-6913047
valeriep
parents: 53257
diff changeset
   475
                ckWrappedKeyLength,
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   476
                (CK_ATTRIBUTE_PTR)nativeKeyInfoArrayRawCkAttributes,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   477
                jLongToCKULong(nativeKeyInfoCkAttributesCount),
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   478
                &ckObjectHandle);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   479
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   480
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   481
        goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   482
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   483
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   484
    jObjectHandle = ckULongToJLong(ckObjectHandle);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   485
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   486
cleanup:
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   487
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   488
    if (nativeKeyInfoArrayRaw != NULL) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   489
        (*env)->ReleaseByteArrayElements(env, jNativeKeyInfo,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   490
                nativeKeyInfoArrayRaw, JNI_ABORT);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   491
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   492
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   493
    return jObjectHandle;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   494
}
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   495
#endif
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   496
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
#ifdef P11_ENABLE_C_GENERATEKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
 * Method:    C_GenerateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
 * @param   jobjectArray jTemplate      CK_ATTRIBUTE_PTR pTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
 *                                      CK_ULONG ulCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
 * @return  jlong jKeyHandle            CK_OBJECT_HANDLE_PTR phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jobjectArray jTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    CK_ULONG ckAttributesLength;
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   516
    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
   517
    jlong jKeyHandle = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    if (ckpFunctions == NULL) { return 0L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    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
   525
    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
   526
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    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
   528
    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
   529
        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
   530
            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
   531
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   532
        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
   533
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    rv = (*ckpFunctions->C_GenerateKey)(ckSessionHandle, &ckMechanism, ckpAttributes, ckAttributesLength, &ckKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
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
   537
    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
   538
        jKeyHandle = ckULongToJLong(ckKeyHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
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
   540
        /* 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
   541
        switch (ckMechanism.mechanism) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        case CKM_PBE_MD2_DES_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        case CKM_PBE_MD5_DES_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        case CKM_PBE_MD5_CAST_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        case CKM_PBE_MD5_CAST3_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        case CKM_PBE_MD5_CAST128_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        /* case CKM_PBE_MD5_CAST5_CBC:  the same as CKM_PBE_MD5_CAST128_CBC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        case CKM_PBE_SHA1_CAST128_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        /* case CKM_PBE_SHA1_CAST5_CBC: the same as CKM_PBE_SHA1_CAST128_CBC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            /* we must copy back the initialization vector to the jMechanism object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            copyBackPBEInitializationVector(env, &ckMechanism, jMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            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
   553
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   556
    if (ckMechanism.pParameter != NULL_PTR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   559
    freeCKAttributeArray(ckpAttributes, ckAttributesLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    return jKeyHandle ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
#ifdef P11_ENABLE_C_GENERATEKEYPAIR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
 * Method:    C_GenerateKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
 * 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
   570
 * Parametermapping:                          *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
 * @param   jlong jSessionHandle              CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
 * @param   jobject jMechanism                CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
 * @param   jobjectArray jPublicKeyTemplate   CK_ATTRIBUTE_PTR pPublicKeyTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
 *                                            CK_ULONG ulPublicKeyAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
 * @param   jobjectArray jPrivateKeyTemplate  CK_ATTRIBUTE_PTR pPrivateKeyTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
 *                                            CK_ULONG ulPrivateKeyAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
 * @return  jlongArray jKeyHandles            CK_OBJECT_HANDLE_PTR phPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
 *                                            CK_OBJECT_HANDLE_PTR phPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     jobjectArray jPublicKeyTemplate, jobjectArray jPrivateKeyTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    CK_ATTRIBUTE_PTR ckpPublicKeyAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    CK_ATTRIBUTE_PTR ckpPrivateKeyAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    CK_ULONG ckPublicKeyAttributesLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    CK_ULONG ckPrivateKeyAttributesLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    CK_OBJECT_HANDLE_PTR ckpPublicKeyHandle;  /* pointer to Public Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    CK_OBJECT_HANDLE_PTR ckpPrivateKeyHandle; /* pointer to Private Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    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
   593
    jlongArray jKeyHandles = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    CK_RV rv;
39142
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   595
    int attempts;
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   596
    const int MAX_ATTEMPTS = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    if (ckpFunctions == NULL) { return NULL; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    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
   603
    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
   604
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    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
   606
    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
   607
        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
   608
            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
   609
        }
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   610
        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
   611
        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
   612
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    ckpPublicKeyHandle = ckpKeyHandles;   /* first element of array is Public Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    ckpPrivateKeyHandle = (ckpKeyHandles + 1);  /* second element of array is Private Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
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
   616
    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
   617
    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
   618
        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
   619
            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
   620
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   621
        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
   622
        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
   623
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   624
9994f4f08a59 6812738: SSL 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
    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
   626
    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
   627
        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
   628
            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
   629
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   630
        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
   631
        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
   632
        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
   633
    }
9994f4f08a59 6812738: SSL 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
39142
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   635
    /*
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   636
     * 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
   637
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   638
     * 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
   639
     * 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
   640
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   641
     * 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
   642
     * (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
   643
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   644
     *      ... 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
   645
     *      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
   646
     *      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
   647
     *      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
   648
     *      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
   649
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   650
     * 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
   651
     */
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   652
    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
   653
        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
   654
                        ckpPublicKeyAttributes, ckPublicKeyAttributesLength,
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   655
                        ckpPrivateKeyAttributes, ckPrivateKeyAttributesLength,
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   656
                        ckpPublicKeyHandle, ckpPrivateKeyHandle);
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   657
        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
   658
            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
   659
                    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
   660
        } else {
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   661
            break;
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   662
        }
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   663
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
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
   665
    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
   666
        jKeyHandles = ckULongArrayToJLongArray(env, ckpKeyHandles, 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    if(ckMechanism.pParameter != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    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
   673
    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
   674
    freeCKAttributeArray(ckpPrivateKeyAttributes, ckPrivateKeyAttributesLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    return jKeyHandles ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
#ifdef P11_ENABLE_C_WRAPKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
 * Method:    C_WrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;JJ)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
 * @param   jlong jWrappingKeyHandle    CK_OBJECT_HANDLE hWrappingKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
 * @param   jlong jKeyHandle            CK_OBJECT_HANDLE hKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
 * @return  jbyteArray jWrappedKey      CK_BYTE_PTR pWrappedKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
 *                                      CK_ULONG_PTR pulWrappedKeyLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1WrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jWrappingKeyHandle, jlong jKeyHandle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    CK_OBJECT_HANDLE ckWrappingKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    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
   700
    jbyteArray jWrappedKey = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    CK_BYTE BUF[MAX_STACK_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    CK_BYTE_PTR ckpWrappedKey = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    CK_ULONG ckWrappedKeyLength = MAX_STACK_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    if (ckpFunctions == NULL) { return NULL; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    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
   711
    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
   712
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    ckWrappingKeyHandle = jLongToCKULong(jWrappingKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    ckKeyHandle = jLongToCKULong(jKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    if (rv == CKR_BUFFER_TOO_SMALL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        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
   719
        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
   720
            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
   721
                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
   722
            }
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   723
            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
   724
            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
   725
        }
9994f4f08a59 6812738: SSL 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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        jWrappedKey = ckByteArrayToJByteArray(env, ckpWrappedKey, ckWrappedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   733
    if (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
   734
    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
   735
        free(ckMechanism.pParameter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    return jWrappedKey ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
#ifdef P11_ENABLE_C_UNWRAPKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
 * Method:    C_UnwrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J[B[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
 * @param   jlong jUnwrappingKeyHandle  CK_OBJECT_HANDLE hUnwrappingKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
 * @param   jbyteArray jWrappedKey      CK_BYTE_PTR pWrappedKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
 *                                      CK_ULONG_PTR pulWrappedKeyLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
 * @param   jobjectArray jTemplate      CK_ATTRIBUTE_PTR pTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
 *                                      CK_ULONG ulCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
 * @return  jlong jKeyHandle            CK_OBJECT_HANDLE_PTR phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1UnwrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jUnwrappingKeyHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     jbyteArray jWrappedKey, jobjectArray jTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    CK_OBJECT_HANDLE ckUnwrappingKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    CK_BYTE_PTR ckpWrappedKey = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    CK_ULONG ckWrappedKeyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    CK_ULONG ckAttributesLength;
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   767
    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
   768
    jlong jKeyHandle = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    if (ckpFunctions == NULL) { return 0L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    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
   776
    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
   777
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    ckUnwrappingKeyHandle = jLongToCKULong(jUnwrappingKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    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
   780
    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
   781
        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
   782
            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
   783
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   784
        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
   785
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   786
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    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
   788
    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
   789
        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
   790
            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
   791
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   792
        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
   793
        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
   794
    }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   795
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    rv = (*ckpFunctions->C_UnwrapKey)(ckSessionHandle, &ckMechanism, ckUnwrappingKeyHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                 ckpWrappedKey, ckWrappedKeyLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                 ckpAttributes, ckAttributesLength, &ckKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
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
   801
    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
   802
        jKeyHandle = ckLongToJLong(ckKeyHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
#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
   805
        /* 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
   806
        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
   807
            /* 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
   808
            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
   809
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   810
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
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
   813
    if (ckMechanism.pParameter != NULL_PTR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
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
   816
    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
   817
    free(ckpWrappedKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    return jKeyHandle ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
#ifdef P11_ENABLE_C_DERIVEKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   825
static void freeMasterKeyDeriveParams(CK_SSL3_RANDOM_DATA *RandomInfo, CK_VERSION_PTR pVersion) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   826
    if (RandomInfo->pClientRandom != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   827
        free(RandomInfo->pClientRandom);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   828
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   829
    if (RandomInfo->pServerRandom != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   830
        free(RandomInfo->pServerRandom);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   831
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   832
    if (pVersion != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   833
        free(pVersion);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   834
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   835
}
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   836
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   837
void ssl3FreeMasterKeyDeriveParams(CK_MECHANISM_PTR ckMechanism) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    CK_SSL3_MASTER_KEY_DERIVE_PARAMS *params = (CK_SSL3_MASTER_KEY_DERIVE_PARAMS *) ckMechanism->pParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    if (params == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   842
    freeMasterKeyDeriveParams(&(params->RandomInfo), params->pVersion);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   843
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   845
void tls12FreeMasterKeyDeriveParams(CK_MECHANISM_PTR ckMechanism) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   846
    CK_TLS12_MASTER_KEY_DERIVE_PARAMS *params =
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   847
            (CK_TLS12_MASTER_KEY_DERIVE_PARAMS *)ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   848
    if (params == NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   849
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   851
    freeMasterKeyDeriveParams(&(params->RandomInfo), params->pVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
void freeEcdh1DeriveParams(CK_MECHANISM_PTR ckMechanism) {
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   855
    CK_ECDH1_DERIVE_PARAMS *params =
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   856
            (CK_ECDH1_DERIVE_PARAMS *)ckMechanism->pParameter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    if (params == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    if (params->pSharedData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        free(params->pSharedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    if (params->pPublicData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        free(params->pPublicData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
 * Copy back the PRF output to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
void copyBackTLSPrfParams(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
{
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
   874
    jclass jMechanismClass, jTLSPrfParamsClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    CK_TLS_PRF_PARAMS *ckTLSPrfParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    jobject jTLSPrfParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    jfieldID fieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    CK_MECHANISM_TYPE ckMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    jlong jMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    CK_BYTE_PTR output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    jobject jOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    jint jLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    jbyte* jBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    /* 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
   887
    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
   888
    if (jMechanismClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    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
   890
    if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    ckMechanismType = jLongToCKULong(jMechanismType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    if (ckMechanismType != ckMechanism->mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        /* we do not have maching types, this should not occur */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /* get the native CK_TLS_PRF_PARAMS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    ckTLSPrfParams = (CK_TLS_PRF_PARAMS *) ckMechanism->pParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    if (ckTLSPrfParams != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        /* get the Java CK_TLS_PRF_PARAMS object (pParameter) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        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
   903
        if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        jTLSPrfParams = (*env)->GetObjectField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        /* 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
   907
        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
   908
        if (jTLSPrfParamsClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        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
   910
        if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        jOutput = (*env)->GetObjectField(env, jTLSPrfParams, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        output = ckTLSPrfParams->pOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        // Note: we assume that the token returned exactly as many bytes as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        // requested. Anything else would not make sense.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (jOutput != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            jLength = (*env)->GetArrayLength(env, jOutput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            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
   919
            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
   920
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            /* copy the bytes to the Java buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            for (i=0; i < jLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                jBytes[i] = ckByteToJByte(output[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            /* copy back the Java buffer to the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            (*env)->ReleaseByteArrayElements(env, jOutput, jBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        // 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
   930
        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
   931
        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
   932
        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
   933
        free(ckTLSPrfParams->pOutput);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
 * Method:    C_DeriveKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
 * @param   jlong jBaseKeyHandle        CK_OBJECT_HANDLE hBaseKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
 * @param   jobjectArray jTemplate      CK_ATTRIBUTE_PTR pTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
 *                                      CK_ULONG ulCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
 * @return  jlong jKeyHandle            CK_OBJECT_HANDLE_PTR phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DeriveKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jBaseKeyHandle, jobjectArray jTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    CK_OBJECT_HANDLE ckBaseKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    CK_ULONG ckAttributesLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    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
   958
    jlong jKeyHandle = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    CK_OBJECT_HANDLE_PTR phKey = &ckKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    if (ckpFunctions == NULL) { return 0L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    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
   967
    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
   968
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    ckBaseKeyHandle = jLongToCKULong(jBaseKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    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
   971
    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
   972
        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
   973
            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
   974
        }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   975
        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
   976
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    switch (ckMechanism.mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    case CKM_SSL3_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    case CKM_TLS_KEY_AND_MAC_DERIVE:
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   981
    case CKM_TLS12_KEY_AND_MAC_DERIVE:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    case CKM_TLS_PRF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        // these mechanism do not return a key handle via phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        // set to NULL in case pedantic implementations check for it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        phKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    rv = (*ckpFunctions->C_DeriveKey)(ckSessionHandle, &ckMechanism, ckBaseKeyHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                 ckpAttributes, ckAttributesLength, phKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    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
   996
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   997
    freeCKAttributeArray(ckpAttributes, ckAttributesLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    switch (ckMechanism.mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    case CKM_SSL3_MASTER_KEY_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    case CKM_TLS_MASTER_KEY_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        /* we must copy back the client version */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1003
        ssl3CopyBackClientVersion(env, &ckMechanism, jMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1004
        ssl3FreeMasterKeyDeriveParams(&ckMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1005
        break;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1006
    case CKM_TLS12_MASTER_KEY_DERIVE:
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1007
        tls12CopyBackClientVersion(env, &ckMechanism, jMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1008
        tls12FreeMasterKeyDeriveParams(&ckMechanism);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    case CKM_SSL3_MASTER_KEY_DERIVE_DH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    case CKM_TLS_MASTER_KEY_DERIVE_DH:
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1012
        ssl3FreeMasterKeyDeriveParams(&ckMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1013
        break;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1014
    case CKM_TLS12_MASTER_KEY_DERIVE_DH:
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1015
        tls12FreeMasterKeyDeriveParams(&ckMechanism);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    case CKM_SSL3_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    case CKM_TLS_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        /* we must copy back the unwrapped key info to the jMechanism object */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1020
        ssl3CopyBackKeyMatParams(env, &ckMechanism, jMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1021
        break;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1022
    case CKM_TLS12_KEY_AND_MAC_DERIVE:
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1023
        /* we must copy back the unwrapped key info to the jMechanism object */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1024
        tls12CopyBackKeyMatParams(env, &ckMechanism, jMechanism);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    case CKM_TLS_PRF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        copyBackTLSPrfParams(env, &ckMechanism, jMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    case CKM_ECDH1_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        freeEcdh1DeriveParams(&ckMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
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
  1037
    if (ckMechanism.pParameter != NULL_PTR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
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
  1040
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    return jKeyHandle ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1045
static void copyBackClientVersion(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1046
        CK_VERSION *ckVersion, const char *class_master_key_derive_params)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
{
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1048
    jclass jMasterKeyDeriveParamsClass, jMechanismClass, jVersionClass;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1049
    jobject jMasterKeyDeriveParams;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1050
    jfieldID fieldID;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1051
    CK_MECHANISM_TYPE ckMechanismType;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1052
    jlong jMechanismType;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1053
    jobject jVersion;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1055
    /* get mechanism */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1056
    jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1057
    if (jMechanismClass == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1058
    fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1059
    if (fieldID == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1060
    jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1061
    ckMechanismType = jLongToCKULong(jMechanismType);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1062
    if (ckMechanismType != ckMechanism->mechanism) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1063
        /* we do not have maching types, this should not occur */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1064
        return;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1065
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    if (ckVersion != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
      /* get the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS (pParameter) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
      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
  1070
      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
  1071
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1072
      jMasterKeyDeriveParams = (*env)->GetObjectField(env, jMechanism, fieldID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
      /* get the Java CK_VERSION */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1075
      jMasterKeyDeriveParamsClass = (*env)->FindClass(env, class_master_key_derive_params);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1076
      if (jMasterKeyDeriveParamsClass == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1077
      fieldID = (*env)->GetFieldID(env, jMasterKeyDeriveParamsClass,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1078
              "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
  1079
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1080
      jVersion = (*env)->GetObjectField(env, jMasterKeyDeriveParams, fieldID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
      /* now copy back the version from the native structure to the Java structure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
      /* 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
  1085
      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
  1086
      if (jVersionClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
      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
  1088
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
      (*env)->SetByteField(env, jVersion, fieldID, ckByteToJByte(ckVersion->major));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
      /* copy back the minor version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
      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
  1093
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
      (*env)->SetByteField(env, jVersion, fieldID, ckByteToJByte(ckVersion->minor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1098
/*
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1099
 * Copy back the client version information from the native
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1100
 * structure to the Java object. This is only used for
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1101
 * CKM_SSL3_MASTER_KEY_DERIVE and CKM_TLS_MASTER_KEY_DERIVE
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1102
 * mechanisms when used for deriving a key.
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1103
 *
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1104
 */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1105
void ssl3CopyBackClientVersion(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1106
        jobject jMechanism)
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1107
{
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1108
    CK_SSL3_MASTER_KEY_DERIVE_PARAMS *ckSSL3MasterKeyDeriveParams;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1109
    ckSSL3MasterKeyDeriveParams =
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1110
            (CK_SSL3_MASTER_KEY_DERIVE_PARAMS *)ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1111
    if (ckSSL3MasterKeyDeriveParams != NULL_PTR) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1112
        copyBackClientVersion(env, ckMechanism, jMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1113
                ckSSL3MasterKeyDeriveParams->pVersion,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1114
                CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1115
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1116
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
/*
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1119
 * Copy back the client version information from the native
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1120
 * structure to the Java object. This is only used for
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1121
 * CKM_TLS12_MASTER_KEY_DERIVE mechanism when used for deriving a key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
 */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1124
void tls12CopyBackClientVersion(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1125
        jobject jMechanism)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
{
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1127
    CK_TLS12_MASTER_KEY_DERIVE_PARAMS *ckTLS12MasterKeyDeriveParams;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1128
    ckTLS12MasterKeyDeriveParams =
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1129
            (CK_TLS12_MASTER_KEY_DERIVE_PARAMS *)ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1130
    if (ckTLS12MasterKeyDeriveParams != NULL_PTR) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1131
        copyBackClientVersion(env, ckMechanism, jMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1132
                ckTLS12MasterKeyDeriveParams->pVersion,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1133
                CLASS_TLS12_MASTER_KEY_DERIVE_PARAMS);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1134
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1135
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1137
static void copyBackKeyMatParams(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1138
        jobject jMechanism, CK_SSL3_RANDOM_DATA *RandomInfo,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1139
        CK_SSL3_KEY_MAT_OUT_PTR ckSSL3KeyMatOut, const char *class_key_mat_params)
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1140
{
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1141
    jclass jMechanismClass, jKeyMatParamsClass, jSSL3KeyMatOutClass;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1142
    jfieldID fieldID;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1143
    CK_MECHANISM_TYPE ckMechanismType;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1144
    jlong jMechanismType;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1145
    CK_BYTE_PTR iv;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1146
    jobject jKeyMatParam;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1147
    jobject jSSL3KeyMatOut;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1148
    jobject jIV;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1149
    jint jLength;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1150
    jbyte* jBytes;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1151
    int i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1153
    /* get mechanism */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1154
    jMechanismClass= (*env)->FindClass(env, CLASS_MECHANISM);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1155
    if (jMechanismClass == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1156
    fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1157
    if (fieldID == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1158
    jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1159
    ckMechanismType = jLongToCKULong(jMechanismType);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1160
    if (ckMechanismType != ckMechanism->mechanism) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1161
        /* we do not have maching types, this should not occur */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1162
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1165
    // free malloc'd data
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1166
    if (RandomInfo->pClientRandom != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1167
        free(RandomInfo->pClientRandom);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1168
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1169
    if (RandomInfo->pServerRandom != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1170
        free(RandomInfo->pServerRandom);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1171
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1172
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    if (ckSSL3KeyMatOut != NULL_PTR) {
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1174
      /* get the Java params object (pParameter) */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1175
      fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter",
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1176
              "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
  1177
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1178
      jKeyMatParam = (*env)->GetObjectField(env, jMechanism, fieldID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
      /* get the Java CK_SSL3_KEY_MAT_OUT */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1181
      jKeyMatParamsClass = (*env)->FindClass(env, class_key_mat_params);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1182
      if (jKeyMatParamsClass == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1183
      fieldID = (*env)->GetFieldID(env, jKeyMatParamsClass,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1184
              "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
  1185
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1186
      jSSL3KeyMatOut = (*env)->GetObjectField(env, jKeyMatParam, fieldID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
      /* now copy back all the key handles and the initialization vectors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
      /* 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
  1190
      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
  1191
      if (jSSL3KeyMatOutClass == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1192
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1193
              "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
  1194
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1195
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1196
              ckULongToJLong(ckSSL3KeyMatOut->hClientMacSecret));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
      /* copy back server MAC secret handle */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1199
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1200
              "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
  1201
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1202
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1203
              ckULongToJLong(ckSSL3KeyMatOut->hServerMacSecret));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
      /* copy back client secret key handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
      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
  1207
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1208
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1209
              ckULongToJLong(ckSSL3KeyMatOut->hClientKey));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
      /* copy back server secret key handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
      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
  1213
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1214
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1215
              ckULongToJLong(ckSSL3KeyMatOut->hServerKey));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
      /* copy back the client IV */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
      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
  1219
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
      jIV = (*env)->GetObjectField(env, jSSL3KeyMatOut, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
      iv = ckSSL3KeyMatOut->pIVClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
      if (jIV != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        jLength = (*env)->GetArrayLength(env, jIV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        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
  1226
        if (jBytes == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        /* copy the bytes to the Java buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        for (i=0; i < jLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
          jBytes[i] = ckByteToJByte(iv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        /* copy back the Java buffer to the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        (*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
      }
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
  1234
      // 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
  1235
      free(ckSSL3KeyMatOut->pIVClient);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
      /* copy back the server IV */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
      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
  1239
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
      jIV = (*env)->GetObjectField(env, jSSL3KeyMatOut, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
      iv = ckSSL3KeyMatOut->pIVServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
      if (jIV != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        jLength = (*env)->GetArrayLength(env, jIV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        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
  1246
        if (jBytes == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        /* copy the bytes to the Java buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        for (i=0; i < jLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
          jBytes[i] = ckByteToJByte(iv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        /* copy back the Java buffer to the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        (*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
      // 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
  1255
      free(ckSSL3KeyMatOut->pIVServer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
      free(ckSSL3KeyMatOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1258
}
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1259
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1260
/*
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1261
 * Copy back the derived keys and initialization vectors from the native
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1262
 * structure to the Java object. This is only used for
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1263
 * CKM_SSL3_KEY_AND_MAC_DERIVE and CKM_TLS_KEY_AND_MAC_DERIVE mechanisms
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1264
 * when used for deriving a key.
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1265
 *
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1266
 */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1267
void ssl3CopyBackKeyMatParams(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1268
        jobject jMechanism)
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1269
{
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1270
    CK_SSL3_KEY_MAT_PARAMS *ckSSL3KeyMatParam;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1271
    ckSSL3KeyMatParam = (CK_SSL3_KEY_MAT_PARAMS *)ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1272
    if (ckSSL3KeyMatParam != NULL_PTR) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1273
        copyBackKeyMatParams(env, ckMechanism, jMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1274
                &(ckSSL3KeyMatParam->RandomInfo),
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1275
                ckSSL3KeyMatParam->pReturnedKeyMaterial,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1276
                CLASS_SSL3_KEY_MAT_PARAMS);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1277
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1278
}
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1279
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1280
/*
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1281
 * Copy back the derived keys and initialization vectors from the native
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1282
 * structure to the Java object. This is only used for
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1283
 * CKM_TLS12_KEY_AND_MAC_DERIVE mechanism when used for deriving a key.
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1284
 *
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1285
 */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1286
void tls12CopyBackKeyMatParams(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1287
        jobject jMechanism)
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1288
{
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1289
    CK_TLS12_KEY_MAT_PARAMS *ckTLS12KeyMatParam;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1290
    ckTLS12KeyMatParam = (CK_TLS12_KEY_MAT_PARAMS *) ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1291
    if (ckTLS12KeyMatParam != NULL_PTR) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1292
        copyBackKeyMatParams(env, ckMechanism, jMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1293
                &(ckTLS12KeyMatParam->RandomInfo),
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1294
                ckTLS12KeyMatParam->pReturnedKeyMaterial,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1295
                CLASS_TLS12_KEY_MAT_PARAMS);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1296
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
#endif