src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_keymgmt.c
author mbalao
Thu, 29 Nov 2018 13:36:23 -0300
changeset 53257 5170dc2bcf64
parent 51800 bccd9966f1ed
child 53856 15ae25f7eefd
permissions -rw-r--r--
6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space Summary: Extract cryptographic keys within NSS PKCS11 software tokens for memory management purposes. Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   154
    unsigned long* wrappedKeySizePtr = NULL;
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;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   161
    unsigned long* wrappedKeySizeWrappedKeyArrayPtr = NULL;
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);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   237
    TRACE1("DEBUG: GetNativeKeyInfo sizeof CK_ATTRIBUTE = %lu\n", 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
   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
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   257
    wrappedKeySizePtr = (unsigned long*)(nativeKeyInfoArrayRaw +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   258
            sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize);
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.
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   311
        if (jWrappingKeyHandle != -1) {
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 =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   333
                        (unsigned long*)(nativeKeyInfoWrappedKeyArrayRaw +
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 +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   335
                        totalDataSize);
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 =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   340
                        (unsigned char*)wrappedKeySizeWrappedKeyArrayPtr +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   341
                        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
   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 {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   354
            goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   355
        }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   356
        returnValue = nativeKeyInfoWrappedKeyArray;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   357
    } else {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   358
        returnValue = nativeKeyInfoArray;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   359
    }
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
cleanup:
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   362
    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
   363
        free(ckpAttributes);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   364
    }
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
    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
   367
        (*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
   368
                nativeKeyInfoArrayRaw, 0);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   369
    }
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
    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
   372
        (*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
   373
                nativeKeyInfoWrappedKeyArrayRaw, 0);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   374
    }
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
    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
   377
        (*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
   378
    }
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
    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
   381
            && returnValue != nativeKeyInfoWrappedKeyArray) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   382
        (*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
   383
    }
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
    return returnValue;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   386
}
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   387
#endif
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   388
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   389
#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
   390
/*
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   391
 * 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
   392
 * Method:    createNativeKey
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   393
 * 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
   394
 * Parametermapping:                          *PKCS11*
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   395
 * @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
   396
 * @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
   397
 * @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
   398
 * @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
   399
 * @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
   400
 */
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   401
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
   402
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
   403
    (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
   404
    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
   405
{
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   406
    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
   407
    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
   408
    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
   409
    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
   410
    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
   411
    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
   412
    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
   413
    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
   414
    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
   415
    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
   416
    unsigned long totalDataSize = 0UL;
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* wrappedKeySizePtr = NULL;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   418
    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
   419
    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
   420
    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
   421
    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
   422
    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
   423
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   424
    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
   425
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   426
    nativeKeyInfoArrayRaw =
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   427
            (*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
   428
    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
   429
        goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   430
    }
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
    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
   433
    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
   434
    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
   435
    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
   436
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   437
    nativeKeyInfoArrayRawCkAttributes = nativeKeyInfoArrayRaw +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   438
            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
   439
    nativeKeyInfoArrayRawCkAttributesPtr = nativeKeyInfoArrayRawCkAttributes;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   440
    nativeKeyInfoArrayRawDataPtr = nativeKeyInfoArrayRaw +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   441
            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
   442
    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
   443
            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
    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
   445
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   446
    wrappedKeySizePtr = (unsigned long*)(nativeKeyInfoArrayRaw +
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   447
            sizeof(unsigned long)*2 + totalCkAttributesSize + totalDataSize);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   448
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   449
    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
   450
    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
   451
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   452
    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
   453
        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
   454
                > 0) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   455
            (*(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
   456
                    nativeKeyInfoArrayRawDataPtr;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   457
        }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   458
        nativeKeyInfoArrayRawDataPtr +=
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   459
                (*(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
   460
        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
   461
    }
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
    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
   464
        // 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
   465
        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
   466
                (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
   467
                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
   468
    } else {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   469
        // Wrapped key
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   470
        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
   471
        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
   472
                jLongToCKULong(jWrappingKeyHandle),
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   473
                (CK_BYTE_PTR)(wrappedKeySizePtr + 1), ckWrappedKeyLength,
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   474
                (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
   475
                jLongToCKULong(nativeKeyInfoCkAttributesCount),
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   476
                &ckObjectHandle);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   477
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   478
    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
   479
        goto cleanup;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   480
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   481
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   482
    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
   483
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   484
cleanup:
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
    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
   487
        (*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
   488
                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
   489
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   490
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   491
    return jObjectHandle;
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
#endif
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 51800
diff changeset
   494
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
#ifdef P11_ENABLE_C_GENERATEKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
 * Method:    C_GenerateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
 * @param   jobjectArray jTemplate      CK_ATTRIBUTE_PTR pTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
 *                                      CK_ULONG ulCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
 * @return  jlong jKeyHandle            CK_OBJECT_HANDLE_PTR phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jobjectArray jTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    CK_ULONG ckAttributesLength;
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   514
    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
   515
    jlong jKeyHandle = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    if (ckpFunctions == NULL) { return 0L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    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
   523
    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
   524
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    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
   526
    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
   527
        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
   528
            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
   529
        }
9994f4f08a59 6812738: SSL 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
        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
   531
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    rv = (*ckpFunctions->C_GenerateKey)(ckSessionHandle, &ckMechanism, ckpAttributes, ckAttributesLength, &ckKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
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
   535
    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
   536
        jKeyHandle = ckULongToJLong(ckKeyHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
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
   538
        /* 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
   539
        switch (ckMechanism.mechanism) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        case CKM_PBE_MD2_DES_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        case CKM_PBE_MD5_DES_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        case CKM_PBE_MD5_CAST_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        case CKM_PBE_MD5_CAST3_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        case CKM_PBE_MD5_CAST128_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        /* case CKM_PBE_MD5_CAST5_CBC:  the same as CKM_PBE_MD5_CAST128_CBC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        case CKM_PBE_SHA1_CAST128_CBC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        /* case CKM_PBE_SHA1_CAST5_CBC: the same as CKM_PBE_SHA1_CAST128_CBC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            /* we must copy back the initialization vector to the jMechanism object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            copyBackPBEInitializationVector(env, &ckMechanism, jMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            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
   551
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   554
    if (ckMechanism.pParameter != NULL_PTR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
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
   557
    freeCKAttributeArray(ckpAttributes, ckAttributesLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    return jKeyHandle ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
#ifdef P11_ENABLE_C_GENERATEKEYPAIR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
 * Method:    C_GenerateKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
 * 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
   568
 * Parametermapping:                          *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
 * @param   jlong jSessionHandle              CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
 * @param   jobject jMechanism                CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
 * @param   jobjectArray jPublicKeyTemplate   CK_ATTRIBUTE_PTR pPublicKeyTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
 *                                            CK_ULONG ulPublicKeyAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
 * @param   jobjectArray jPrivateKeyTemplate  CK_ATTRIBUTE_PTR pPrivateKeyTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
 *                                            CK_ULONG ulPrivateKeyAttributeCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
 * @return  jlongArray jKeyHandles            CK_OBJECT_HANDLE_PTR phPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
 *                                            CK_OBJECT_HANDLE_PTR phPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     jobjectArray jPublicKeyTemplate, jobjectArray jPrivateKeyTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    CK_ATTRIBUTE_PTR ckpPublicKeyAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    CK_ATTRIBUTE_PTR ckpPrivateKeyAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    CK_ULONG ckPublicKeyAttributesLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    CK_ULONG ckPrivateKeyAttributesLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    CK_OBJECT_HANDLE_PTR ckpPublicKeyHandle;  /* pointer to Public Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    CK_OBJECT_HANDLE_PTR ckpPrivateKeyHandle; /* pointer to Private Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    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
   591
    jlongArray jKeyHandles = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    CK_RV rv;
39142
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   593
    int attempts;
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   594
    const int MAX_ATTEMPTS = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    if (ckpFunctions == NULL) { return NULL; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    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
   601
    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
   602
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    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
   604
    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
   605
        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
   606
            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
   607
        }
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   608
        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
   609
        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
   610
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    ckpPublicKeyHandle = ckpKeyHandles;   /* first element of array is Public Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    ckpPrivateKeyHandle = (ckpKeyHandles + 1);  /* second element of array is Private Key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
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
   614
    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
   615
    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
   616
        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
   617
            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
   618
        }
9994f4f08a59 6812738: SSL 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(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
   620
        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
   621
    }
9994f4f08a59 6812738: SSL 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
9994f4f08a59 6812738: SSL 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
    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
   624
    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
   625
        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
   626
            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
   627
        }
9994f4f08a59 6812738: SSL 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(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
   629
        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
   630
        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
   631
    }
9994f4f08a59 6812738: SSL 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
39142
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   633
    /*
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   634
     * 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
   635
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   636
     * 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
   637
     * 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
   638
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   639
     * 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
   640
     * (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
   641
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   642
     *      ... 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
   643
     *      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
   644
     *      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
   645
     *      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
   646
     *      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
   647
     *
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   648
     * 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
   649
     */
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   650
    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
   651
        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
   652
                        ckpPublicKeyAttributes, ckPublicKeyAttributesLength,
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   653
                        ckpPrivateKeyAttributes, ckPrivateKeyAttributesLength,
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   654
                        ckpPublicKeyHandle, ckpPrivateKeyHandle);
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   655
        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
   656
            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
   657
                    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
   658
        } else {
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   659
            break;
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   660
        }
bf48a9f13cf2 8074580: sun/security/pkcs11/rsa/TestKeyPairGenerator.java fails due to PKCS11Exception: CKR_FUNCTION_FAILED
asmotrak
parents: 25859
diff changeset
   661
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
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
   663
    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
   664
        jKeyHandles = ckULongArrayToJLongArray(env, ckpKeyHandles, 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    if(ckMechanism.pParameter != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    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
   671
    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
   672
    freeCKAttributeArray(ckpPrivateKeyAttributes, ckPrivateKeyAttributesLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    return jKeyHandles ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
#ifdef P11_ENABLE_C_WRAPKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
 * Method:    C_WrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;JJ)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
 * @param   jlong jWrappingKeyHandle    CK_OBJECT_HANDLE hWrappingKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
 * @param   jlong jKeyHandle            CK_OBJECT_HANDLE hKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
 * @return  jbyteArray jWrappedKey      CK_BYTE_PTR pWrappedKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
 *                                      CK_ULONG_PTR pulWrappedKeyLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1WrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jWrappingKeyHandle, jlong jKeyHandle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    CK_OBJECT_HANDLE ckWrappingKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    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
   698
    jbyteArray jWrappedKey = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    CK_BYTE BUF[MAX_STACK_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    CK_BYTE_PTR ckpWrappedKey = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    CK_ULONG ckWrappedKeyLength = MAX_STACK_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    if (ckpFunctions == NULL) { return NULL; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    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
   709
    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
   710
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    ckWrappingKeyHandle = jLongToCKULong(jWrappingKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    ckKeyHandle = jLongToCKULong(jKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    if (rv == CKR_BUFFER_TOO_SMALL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        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
   717
        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
   718
            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
   719
                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
   720
            }
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   721
            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
   722
            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
   723
        }
9994f4f08a59 6812738: SSL 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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        jWrappedKey = ckByteArrayToJByteArray(env, ckpWrappedKey, ckWrappedKeyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   731
    if (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
   732
    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
   733
        free(ckMechanism.pParameter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    return jWrappedKey ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
#ifdef P11_ENABLE_C_UNWRAPKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
 * Method:    C_UnwrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J[B[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
 * @param   jlong jUnwrappingKeyHandle  CK_OBJECT_HANDLE hUnwrappingKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
 * @param   jbyteArray jWrappedKey      CK_BYTE_PTR pWrappedKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
 *                                      CK_ULONG_PTR pulWrappedKeyLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
 * @param   jobjectArray jTemplate      CK_ATTRIBUTE_PTR pTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
 *                                      CK_ULONG ulCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
 * @return  jlong jKeyHandle            CK_OBJECT_HANDLE_PTR phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1UnwrapKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jUnwrappingKeyHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     jbyteArray jWrappedKey, jobjectArray jTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    CK_OBJECT_HANDLE ckUnwrappingKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    CK_BYTE_PTR ckpWrappedKey = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    CK_ULONG ckWrappedKeyLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    CK_ULONG ckAttributesLength;
3321
fed33393bc52 6823905: crash in sun.security.pkcs11.wrapper.PKCS11.C_Sign during stress-test
valeriep
parents: 2180
diff changeset
   765
    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
   766
    jlong jKeyHandle = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    if (ckpFunctions == NULL) { return 0L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    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
   774
    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
   775
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    ckUnwrappingKeyHandle = jLongToCKULong(jUnwrappingKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    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
   778
    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
   779
        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
   780
            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
   781
        }
9994f4f08a59 6812738: SSL 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
        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
   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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    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
   786
    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
   787
        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
   788
            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
   789
        }
9994f4f08a59 6812738: SSL 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(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
   791
        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
   792
    }
9994f4f08a59 6812738: SSL 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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    rv = (*ckpFunctions->C_UnwrapKey)(ckSessionHandle, &ckMechanism, ckUnwrappingKeyHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                 ckpWrappedKey, ckWrappedKeyLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                 ckpAttributes, ckAttributesLength, &ckKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
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
   799
    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
   800
        jKeyHandle = ckLongToJLong(ckKeyHandle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
#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
   803
        /* 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
   804
        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
   805
            /* 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
   806
            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
   807
        }
9994f4f08a59 6812738: SSL 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
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
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
   811
    if (ckMechanism.pParameter != NULL_PTR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
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
   814
    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
   815
    free(ckpWrappedKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    return jKeyHandle ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
#ifdef P11_ENABLE_C_DERIVEKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   823
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
   824
    if (RandomInfo->pClientRandom != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   825
        free(RandomInfo->pClientRandom);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   826
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   827
    if (RandomInfo->pServerRandom != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   828
        free(RandomInfo->pServerRandom);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   829
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   830
    if (pVersion != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   831
        free(pVersion);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   832
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   833
}
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
void ssl3FreeMasterKeyDeriveParams(CK_MECHANISM_PTR ckMechanism) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    CK_SSL3_MASTER_KEY_DERIVE_PARAMS *params = (CK_SSL3_MASTER_KEY_DERIVE_PARAMS *) ckMechanism->pParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    if (params == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   840
    freeMasterKeyDeriveParams(&(params->RandomInfo), params->pVersion);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   841
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   843
void tls12FreeMasterKeyDeriveParams(CK_MECHANISM_PTR ckMechanism) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   844
    CK_TLS12_MASTER_KEY_DERIVE_PARAMS *params =
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   845
            (CK_TLS12_MASTER_KEY_DERIVE_PARAMS *)ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   846
    if (params == NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   847
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   849
    freeMasterKeyDeriveParams(&(params->RandomInfo), params->pVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
void freeEcdh1DeriveParams(CK_MECHANISM_PTR ckMechanism) {
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   853
    CK_ECDH1_DERIVE_PARAMS *params =
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   854
            (CK_ECDH1_DERIVE_PARAMS *)ckMechanism->pParameter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    if (params == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    if (params->pSharedData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        free(params->pSharedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    if (params->pPublicData != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        free(params->pPublicData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
 * Copy back the PRF output to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
void copyBackTLSPrfParams(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
{
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
   872
    jclass jMechanismClass, jTLSPrfParamsClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    CK_TLS_PRF_PARAMS *ckTLSPrfParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    jobject jTLSPrfParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    jfieldID fieldID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    CK_MECHANISM_TYPE ckMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    jlong jMechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    CK_BYTE_PTR output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    jobject jOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    jint jLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    jbyte* jBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /* 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
   885
    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
   886
    if (jMechanismClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    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
   888
    if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    ckMechanismType = jLongToCKULong(jMechanismType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    if (ckMechanismType != ckMechanism->mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        /* we do not have maching types, this should not occur */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /* get the native CK_TLS_PRF_PARAMS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    ckTLSPrfParams = (CK_TLS_PRF_PARAMS *) ckMechanism->pParameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    if (ckTLSPrfParams != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        /* get the Java CK_TLS_PRF_PARAMS object (pParameter) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        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
   901
        if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        jTLSPrfParams = (*env)->GetObjectField(env, jMechanism, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        /* 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
   905
        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
   906
        if (jTLSPrfParamsClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        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
   908
        if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        jOutput = (*env)->GetObjectField(env, jTLSPrfParams, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        output = ckTLSPrfParams->pOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        // Note: we assume that the token returned exactly as many bytes as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        // requested. Anything else would not make sense.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (jOutput != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            jLength = (*env)->GetArrayLength(env, jOutput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            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
   917
            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
   918
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            /* copy the bytes to the Java buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            for (i=0; i < jLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                jBytes[i] = ckByteToJByte(output[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            /* copy back the Java buffer to the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            (*env)->ReleaseByteArrayElements(env, jOutput, jBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        // 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
   928
        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
   929
        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
   930
        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
   931
        free(ckTLSPrfParams->pOutput);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
 * Method:    C_DeriveKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
 * @param   jlong jBaseKeyHandle        CK_OBJECT_HANDLE hBaseKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
 * @param   jobjectArray jTemplate      CK_ATTRIBUTE_PTR pTemplate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
 *                                      CK_ULONG ulCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
 * @return  jlong jKeyHandle            CK_OBJECT_HANDLE_PTR phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DeriveKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jBaseKeyHandle, jobjectArray jTemplate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    CK_OBJECT_HANDLE ckBaseKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    CK_ULONG ckAttributesLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    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
   956
    jlong jKeyHandle = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    CK_OBJECT_HANDLE_PTR phKey = &ckKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    if (ckpFunctions == NULL) { return 0L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    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
   965
    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
   966
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    ckBaseKeyHandle = jLongToCKULong(jBaseKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    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
   969
    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
   970
        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
   971
            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
   972
        }
9994f4f08a59 6812738: SSL 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
        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
   974
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    switch (ckMechanism.mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    case CKM_SSL3_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    case CKM_TLS_KEY_AND_MAC_DERIVE:
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
   979
    case CKM_TLS12_KEY_AND_MAC_DERIVE:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    case CKM_TLS_PRF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        // these mechanism do not return a key handle via phKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        // set to NULL in case pedantic implementations check for it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        phKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    rv = (*ckpFunctions->C_DeriveKey)(ckSessionHandle, &ckMechanism, ckBaseKeyHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                 ckpAttributes, ckAttributesLength, phKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    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
   994
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   995
    freeCKAttributeArray(ckpAttributes, ckAttributesLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    switch (ckMechanism.mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    case CKM_SSL3_MASTER_KEY_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    case CKM_TLS_MASTER_KEY_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        /* we must copy back the client version */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1001
        ssl3CopyBackClientVersion(env, &ckMechanism, jMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1002
        ssl3FreeMasterKeyDeriveParams(&ckMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1003
        break;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1004
    case CKM_TLS12_MASTER_KEY_DERIVE:
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1005
        tls12CopyBackClientVersion(env, &ckMechanism, jMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1006
        tls12FreeMasterKeyDeriveParams(&ckMechanism);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    case CKM_SSL3_MASTER_KEY_DERIVE_DH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    case CKM_TLS_MASTER_KEY_DERIVE_DH:
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1010
        ssl3FreeMasterKeyDeriveParams(&ckMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1011
        break;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1012
    case CKM_TLS12_MASTER_KEY_DERIVE_DH:
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1013
        tls12FreeMasterKeyDeriveParams(&ckMechanism);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    case CKM_SSL3_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    case CKM_TLS_KEY_AND_MAC_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        /* 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
  1018
        ssl3CopyBackKeyMatParams(env, &ckMechanism, jMechanism);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1019
        break;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1020
    case CKM_TLS12_KEY_AND_MAC_DERIVE:
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1021
        /* 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
  1022
        tls12CopyBackKeyMatParams(env, &ckMechanism, jMechanism);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    case CKM_TLS_PRF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        copyBackTLSPrfParams(env, &ckMechanism, jMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    case CKM_ECDH1_DERIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        freeEcdh1DeriveParams(&ckMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
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
  1035
    if (ckMechanism.pParameter != NULL_PTR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
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
  1038
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    return jKeyHandle ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1043
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
  1044
        CK_VERSION *ckVersion, const char *class_master_key_derive_params)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
{
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1046
    jclass jMasterKeyDeriveParamsClass, jMechanismClass, jVersionClass;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1047
    jobject jMasterKeyDeriveParams;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1048
    jfieldID fieldID;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1049
    CK_MECHANISM_TYPE ckMechanismType;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1050
    jlong jMechanismType;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1051
    jobject jVersion;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1053
    /* get mechanism */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1054
    jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1055
    if (jMechanismClass == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1056
    fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1057
    if (fieldID == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1058
    jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1059
    ckMechanismType = jLongToCKULong(jMechanismType);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1060
    if (ckMechanismType != ckMechanism->mechanism) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1061
        /* 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
  1062
        return;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1063
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    if (ckVersion != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
      /* get the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS (pParameter) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
      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
  1068
      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
  1069
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1070
      jMasterKeyDeriveParams = (*env)->GetObjectField(env, jMechanism, fieldID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
      /* get the Java CK_VERSION */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1073
      jMasterKeyDeriveParamsClass = (*env)->FindClass(env, class_master_key_derive_params);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1074
      if (jMasterKeyDeriveParamsClass == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1075
      fieldID = (*env)->GetFieldID(env, jMasterKeyDeriveParamsClass,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1076
              "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
  1077
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1078
      jVersion = (*env)->GetObjectField(env, jMasterKeyDeriveParams, fieldID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
      /* now copy back the version from the native structure to the Java structure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
      /* 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
  1083
      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
  1084
      if (jVersionClass == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
      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
  1086
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
      (*env)->SetByteField(env, jVersion, fieldID, ckByteToJByte(ckVersion->major));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
      /* copy back the minor version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
      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
  1091
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
      (*env)->SetByteField(env, jVersion, fieldID, ckByteToJByte(ckVersion->minor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1096
/*
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1097
 * Copy back the client version information from the native
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1098
 * 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
  1099
 * 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
  1100
 * mechanisms when used for deriving a key.
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1101
 *
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1102
 */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1103
void ssl3CopyBackClientVersion(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1104
        jobject jMechanism)
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1105
{
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1106
    CK_SSL3_MASTER_KEY_DERIVE_PARAMS *ckSSL3MasterKeyDeriveParams;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1107
    ckSSL3MasterKeyDeriveParams =
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1108
            (CK_SSL3_MASTER_KEY_DERIVE_PARAMS *)ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1109
    if (ckSSL3MasterKeyDeriveParams != NULL_PTR) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1110
        copyBackClientVersion(env, ckMechanism, jMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1111
                ckSSL3MasterKeyDeriveParams->pVersion,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1112
                CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1113
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1114
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
/*
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1117
 * Copy back the client version information from the native
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1118
 * 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
  1119
 * CKM_TLS12_MASTER_KEY_DERIVE mechanism when used for deriving a key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
 */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1122
void tls12CopyBackClientVersion(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1123
        jobject jMechanism)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
{
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1125
    CK_TLS12_MASTER_KEY_DERIVE_PARAMS *ckTLS12MasterKeyDeriveParams;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1126
    ckTLS12MasterKeyDeriveParams =
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1127
            (CK_TLS12_MASTER_KEY_DERIVE_PARAMS *)ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1128
    if (ckTLS12MasterKeyDeriveParams != NULL_PTR) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1129
        copyBackClientVersion(env, ckMechanism, jMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1130
                ckTLS12MasterKeyDeriveParams->pVersion,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1131
                CLASS_TLS12_MASTER_KEY_DERIVE_PARAMS);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1132
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1133
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1135
static void copyBackKeyMatParams(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1136
        jobject jMechanism, CK_SSL3_RANDOM_DATA *RandomInfo,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1137
        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
  1138
{
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1139
    jclass jMechanismClass, jKeyMatParamsClass, jSSL3KeyMatOutClass;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1140
    jfieldID fieldID;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1141
    CK_MECHANISM_TYPE ckMechanismType;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1142
    jlong jMechanismType;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1143
    CK_BYTE_PTR iv;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1144
    jobject jKeyMatParam;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1145
    jobject jSSL3KeyMatOut;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1146
    jobject jIV;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1147
    jint jLength;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1148
    jbyte* jBytes;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1149
    int i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1151
    /* get mechanism */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1152
    jMechanismClass= (*env)->FindClass(env, CLASS_MECHANISM);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1153
    if (jMechanismClass == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1154
    fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1155
    if (fieldID == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1156
    jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1157
    ckMechanismType = jLongToCKULong(jMechanismType);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1158
    if (ckMechanismType != ckMechanism->mechanism) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1159
        /* 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
  1160
        return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1163
    // free malloc'd data
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1164
    if (RandomInfo->pClientRandom != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1165
        free(RandomInfo->pClientRandom);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1166
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1167
    if (RandomInfo->pServerRandom != NULL) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1168
        free(RandomInfo->pServerRandom);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1169
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1170
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    if (ckSSL3KeyMatOut != NULL_PTR) {
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1172
      /* get the Java params object (pParameter) */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1173
      fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter",
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1174
              "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
  1175
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1176
      jKeyMatParam = (*env)->GetObjectField(env, jMechanism, fieldID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
      /* get the Java CK_SSL3_KEY_MAT_OUT */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1179
      jKeyMatParamsClass = (*env)->FindClass(env, class_key_mat_params);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1180
      if (jKeyMatParamsClass == NULL) { return; }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1181
      fieldID = (*env)->GetFieldID(env, jKeyMatParamsClass,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1182
              "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
  1183
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1184
      jSSL3KeyMatOut = (*env)->GetObjectField(env, jKeyMatParam, fieldID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
      /* now copy back all the key handles and the initialization vectors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
      /* 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
  1188
      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
  1189
      if (jSSL3KeyMatOutClass == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1190
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1191
              "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
  1192
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1193
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1194
              ckULongToJLong(ckSSL3KeyMatOut->hClientMacSecret));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
      /* copy back server MAC secret handle */
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1197
      fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1198
              "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
  1199
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1200
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1201
              ckULongToJLong(ckSSL3KeyMatOut->hServerMacSecret));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
      /* copy back client secret key handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
      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
  1205
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1206
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1207
              ckULongToJLong(ckSSL3KeyMatOut->hClientKey));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
      /* copy back server secret key handle */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
      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
  1211
      if (fieldID == NULL) { return; }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1212
      (*env)->SetLongField(env, jSSL3KeyMatOut, fieldID,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1213
              ckULongToJLong(ckSSL3KeyMatOut->hServerKey));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
      /* copy back the client IV */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
      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
  1217
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
      jIV = (*env)->GetObjectField(env, jSSL3KeyMatOut, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
      iv = ckSSL3KeyMatOut->pIVClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
      if (jIV != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        jLength = (*env)->GetArrayLength(env, jIV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        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
  1224
        if (jBytes == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        /* copy the bytes to the Java buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        for (i=0; i < jLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
          jBytes[i] = ckByteToJByte(iv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        /* copy back the Java buffer to the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        (*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
      }
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
  1232
      // 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
  1233
      free(ckSSL3KeyMatOut->pIVClient);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
      /* copy back the server IV */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
      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
  1237
      if (fieldID == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
      jIV = (*env)->GetObjectField(env, jSSL3KeyMatOut, fieldID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
      iv = ckSSL3KeyMatOut->pIVServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
      if (jIV != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        jLength = (*env)->GetArrayLength(env, jIV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        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
  1244
        if (jBytes == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        /* copy the bytes to the Java buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        for (i=0; i < jLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
          jBytes[i] = ckByteToJByte(iv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        /* copy back the Java buffer to the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        (*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
      // 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
  1253
      free(ckSSL3KeyMatOut->pIVServer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
      free(ckSSL3KeyMatOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    }
51800
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1256
}
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1257
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
 * 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
  1260
 * 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
  1261
 * 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
  1262
 * when used for deriving a key.
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1263
 *
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1264
 */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1265
void ssl3CopyBackKeyMatParams(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1266
        jobject jMechanism)
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1267
{
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1268
    CK_SSL3_KEY_MAT_PARAMS *ckSSL3KeyMatParam;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1269
    ckSSL3KeyMatParam = (CK_SSL3_KEY_MAT_PARAMS *)ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1270
    if (ckSSL3KeyMatParam != NULL_PTR) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1271
        copyBackKeyMatParams(env, ckMechanism, jMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1272
                &(ckSSL3KeyMatParam->RandomInfo),
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1273
                ckSSL3KeyMatParam->pReturnedKeyMaterial,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1274
                CLASS_SSL3_KEY_MAT_PARAMS);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1275
    }
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1276
}
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
 * 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
  1280
 * 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
  1281
 * 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
  1282
 *
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1283
 */
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1284
void tls12CopyBackKeyMatParams(JNIEnv *env, CK_MECHANISM *ckMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1285
        jobject jMechanism)
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1286
{
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1287
    CK_TLS12_KEY_MAT_PARAMS *ckTLS12KeyMatParam;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1288
    ckTLS12KeyMatParam = (CK_TLS12_KEY_MAT_PARAMS *) ckMechanism->pParameter;
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1289
    if (ckTLS12KeyMatParam != NULL_PTR) {
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1290
        copyBackKeyMatParams(env, ckMechanism, jMechanism,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1291
                &(ckTLS12KeyMatParam->RandomInfo),
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1292
                ckTLS12KeyMatParam->pReturnedKeyMaterial,
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1293
                CLASS_TLS12_KEY_MAT_PARAMS);
bccd9966f1ed 8029661: Support TLS v1.2 algorithm in SunPKCS11 provider
mbalao
parents: 47216
diff changeset
  1294
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
#endif