src/jdk.crypto.ec/share/native/libsunec/ECC_JNI.cpp
author egahlin
Fri, 10 May 2019 18:53:35 +0200
branchJEP-349-branch
changeset 57356 2cf72293e1db
parent 50471 f0aeede1b855
permissions -rw-r--r--
Initial changes for JEP 349
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     1
/*
50471
f0aeede1b855 8204572: SetupJdkLibrary should setup SRC and -I flags automatically
ihse
parents: 47216
diff changeset
     2
 * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     4
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    10
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    15
 * accompanied this code).
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    16
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    23
 * questions.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    24
 */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    25
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    26
#include <jni.h>
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26878
diff changeset
    27
#include "jni_util.h"
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents: 3492
diff changeset
    28
#include "impl/ecc_impl.h"
50471
f0aeede1b855 8204572: SetupJdkLibrary should setup SRC and -I flags automatically
ihse
parents: 47216
diff changeset
    29
#include "sun_security_ec_ECDHKeyAgreement.h"
f0aeede1b855 8204572: SetupJdkLibrary should setup SRC and -I flags automatically
ihse
parents: 47216
diff changeset
    30
#include "sun_security_ec_ECKeyPairGenerator.h"
f0aeede1b855 8204572: SetupJdkLibrary should setup SRC and -I flags automatically
ihse
parents: 47216
diff changeset
    31
#include "sun_security_ec_ECDSASignature.h"
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    32
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    33
#define ILLEGAL_STATE_EXCEPTION "java/lang/IllegalStateException"
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    34
#define INVALID_ALGORITHM_PARAMETER_EXCEPTION \
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    35
        "java/security/InvalidAlgorithmParameterException"
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    36
#define INVALID_PARAMETER_EXCEPTION \
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    37
        "java/security/InvalidParameterException"
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    38
#define KEY_EXCEPTION   "java/security/KeyException"
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    39
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    40
extern "C" {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    41
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    42
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26878
diff changeset
    43
 * Declare library specific JNI_Onload entry if static build
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26878
diff changeset
    44
 */
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26878
diff changeset
    45
DEF_STATIC_JNI_OnLoad
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26878
diff changeset
    46
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26878
diff changeset
    47
/*
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    48
 * Throws an arbitrary Java exception.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    49
 */
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 7668
diff changeset
    50
void ThrowException(JNIEnv *env, const char *exceptionName)
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    51
{
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    52
    jclass exceptionClazz = env->FindClass(exceptionName);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
    53
    if (exceptionClazz != NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
    54
        env->ThrowNew(exceptionClazz, NULL);
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
    55
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    56
}
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    57
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    58
/*
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    59
 * Deep free of the ECParams struct
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    60
 */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    61
void FreeECParams(ECParams *ecparams, jboolean freeStruct)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    62
{
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    63
    // Use B_FALSE to free the SECItem->data element, but not the SECItem itself
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    64
    // Use B_TRUE to free both
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    65
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    66
    SECITEM_FreeItem(&ecparams->fieldID.u.prime, B_FALSE);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    67
    SECITEM_FreeItem(&ecparams->curve.a, B_FALSE);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    68
    SECITEM_FreeItem(&ecparams->curve.b, B_FALSE);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    69
    SECITEM_FreeItem(&ecparams->curve.seed, B_FALSE);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    70
    SECITEM_FreeItem(&ecparams->base, B_FALSE);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    71
    SECITEM_FreeItem(&ecparams->order, B_FALSE);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    72
    SECITEM_FreeItem(&ecparams->DEREncoding, B_FALSE);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    73
    SECITEM_FreeItem(&ecparams->curveOID, B_FALSE);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    74
    if (freeStruct)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    75
        free(ecparams);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    76
}
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    77
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    78
jbyteArray getEncodedBytes(JNIEnv *env, SECItem *hSECItem)
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    79
{
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    80
    SECItem *s = (SECItem *)hSECItem;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    81
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    82
    jbyteArray jEncodedBytes = env->NewByteArray(s->len);
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    83
    if (jEncodedBytes == NULL) {
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    84
        return NULL;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    85
    }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    86
    // Copy bytes from a native SECItem buffer to Java byte array
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    87
    env->SetByteArrayRegion(jEncodedBytes, 0, s->len, (jbyte *)s->data);
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    88
    if (env->ExceptionCheck()) { // should never happen
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    89
        return NULL;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    90
    }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    91
    return jEncodedBytes;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    92
}
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
    93
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    94
/*
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    95
 * Class:     sun_security_ec_ECKeyPairGenerator
45883
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
    96
 * Method:    isCurveSupported
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
    97
 * Signature: ([B)Z
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
    98
 */
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
    99
JNIEXPORT jboolean
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   100
JNICALL Java_sun_security_ec_ECKeyPairGenerator_isCurveSupported
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   101
  (JNIEnv *env, jclass clazz, jbyteArray encodedParams)
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   102
{
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   103
    SECKEYECParams params_item;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   104
    ECParams *ecparams = NULL;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   105
    jboolean result = JNI_FALSE;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   106
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   107
    // The curve is supported if we can get parameters for it
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   108
    params_item.len = env->GetArrayLength(encodedParams);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   109
    params_item.data =
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   110
        (unsigned char *) env->GetByteArrayElements(encodedParams, 0);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   111
    if (params_item.data == NULL) {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   112
        goto cleanup;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   113
    }
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   114
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   115
    // Fill a new ECParams using the supplied OID
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   116
    if (EC_DecodeParams(&params_item, &ecparams, 0) != SECSuccess) {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   117
        /* bad curve OID */
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   118
        goto cleanup;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   119
    }
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   120
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   121
    // If we make it to here, then the curve is supported
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   122
    result = JNI_TRUE;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   123
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   124
cleanup:
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   125
    {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   126
        if (params_item.data) {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   127
            env->ReleaseByteArrayElements(encodedParams,
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   128
                (jbyte *) params_item.data, JNI_ABORT);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   129
        }
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   130
        if (ecparams) {
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   131
            FreeECParams(ecparams, true);
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   132
        }
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   133
    }
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   134
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   135
    return result;
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   136
}
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   137
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   138
/*
89df6f9465a3 8182999: SunEC throws ProviderException on invalid curves
apetcher
parents: 33653
diff changeset
   139
 * Class:     sun_security_ec_ECKeyPairGenerator
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   140
 * Method:    generateECKeyPair
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   141
 * Signature: (I[B[B)[[B
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   142
 */
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   143
JNIEXPORT jobjectArray
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   144
JNICALL Java_sun_security_ec_ECKeyPairGenerator_generateECKeyPair
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   145
  (JNIEnv *env, jclass clazz, jint keySize, jbyteArray encodedParams, jbyteArray seed)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   146
{
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   147
    ECPrivateKey *privKey = NULL; // contains both public and private values
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   148
    ECParams *ecparams = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   149
    SECKEYECParams params_item;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   150
    jint jSeedLength;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   151
    jbyte* pSeedBuffer = NULL;
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   152
    jobjectArray result = NULL;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   153
    jclass baCls = NULL;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   154
    jbyteArray jba;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   155
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   156
    // Initialize the ECParams struct
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   157
    params_item.len = env->GetArrayLength(encodedParams);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   158
    params_item.data =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   159
        (unsigned char *) env->GetByteArrayElements(encodedParams, 0);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   160
    if (params_item.data == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   161
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   162
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   163
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   164
    // Fill a new ECParams using the supplied OID
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   165
    if (EC_DecodeParams(&params_item, &ecparams, 0) != SECSuccess) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   166
        /* bad curve OID */
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 7668
diff changeset
   167
        ThrowException(env, INVALID_ALGORITHM_PARAMETER_EXCEPTION);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   168
        goto cleanup;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   169
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   170
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   171
    // Copy seed from Java to native buffer
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   172
    jSeedLength = env->GetArrayLength(seed);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   173
    pSeedBuffer = new jbyte[jSeedLength];
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   174
    env->GetByteArrayRegion(seed, 0, jSeedLength, pSeedBuffer);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   175
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   176
    // Generate the new keypair (using the supplied seed)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   177
    if (EC_NewKey(ecparams, &privKey, (unsigned char *) pSeedBuffer,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   178
        jSeedLength, 0) != SECSuccess) {
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 7668
diff changeset
   179
        ThrowException(env, KEY_EXCEPTION);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   180
        goto cleanup;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   181
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   182
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   183
    jboolean isCopy;
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   184
    baCls = env->FindClass("[B");
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   185
    if (baCls == NULL) {
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   186
        goto cleanup;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   187
    }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   188
    result = env->NewObjectArray(2, baCls, NULL);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   189
    if (result == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   190
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   191
    }
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   192
    jba = getEncodedBytes(env, &(privKey->privateValue));
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   193
    if (jba == NULL) {
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   194
        result = NULL;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   195
        goto cleanup;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   196
    }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   197
    env->SetObjectArrayElement(result, 0, jba); // big integer
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   198
    if (env->ExceptionCheck()) { // should never happen
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   199
        result = NULL;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   200
        goto cleanup;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   201
    }
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   202
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   203
    jba = getEncodedBytes(env, &(privKey->publicValue));
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   204
    if (jba == NULL) {
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   205
        result = NULL;
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   206
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   207
    }
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   208
    env->SetObjectArrayElement(result, 1, jba); // encoded ec point
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   209
    if (env->ExceptionCheck()) { // should never happen
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   210
        result = NULL;
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   211
        goto cleanup;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   212
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   213
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   214
cleanup:
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   215
    {
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   216
        if (params_item.data) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   217
            env->ReleaseByteArrayElements(encodedParams,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   218
                (jbyte *) params_item.data, JNI_ABORT);
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   219
        }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   220
        if (ecparams) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   221
            FreeECParams(ecparams, true);
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   222
        }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   223
        if (privKey) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   224
            FreeECParams(&privKey->ecParams, false);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   225
            SECITEM_FreeItem(&privKey->version, B_FALSE);
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   226
            SECITEM_FreeItem(&privKey->privateValue, B_FALSE);
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   227
            SECITEM_FreeItem(&privKey->publicValue, B_FALSE);
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   228
            free(privKey);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   229
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   230
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   231
        if (pSeedBuffer) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   232
            delete [] pSeedBuffer;
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   233
        }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   234
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   235
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   236
    return result;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   237
}
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   238
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   239
/*
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   240
 * Class:     sun_security_ec_ECDSASignature
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   241
 * Method:    signDigest
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   242
 * Signature: ([B[B[B[B)[B
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   243
 */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   244
JNIEXPORT jbyteArray
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   245
JNICALL Java_sun_security_ec_ECDSASignature_signDigest
45993
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 33653
diff changeset
   246
  (JNIEnv *env, jclass clazz, jbyteArray digest, jbyteArray privateKey, jbyteArray encodedParams, jbyteArray seed, jint timing)
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   247
{
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   248
    jbyte* pDigestBuffer = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   249
    jint jDigestLength = env->GetArrayLength(digest);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   250
    jbyteArray jSignedDigest = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   251
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   252
    SECItem signature_item;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   253
    jbyte* pSignedDigestBuffer = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   254
    jbyteArray temp;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   255
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   256
    jint jSeedLength = env->GetArrayLength(seed);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   257
    jbyte* pSeedBuffer = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   258
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   259
    // Copy digest from Java to native buffer
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   260
    pDigestBuffer = new jbyte[jDigestLength];
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   261
    env->GetByteArrayRegion(digest, 0, jDigestLength, pDigestBuffer);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   262
    SECItem digest_item;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   263
    digest_item.data = (unsigned char *) pDigestBuffer;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   264
    digest_item.len = jDigestLength;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   265
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   266
    ECPrivateKey privKey;
26878
57077013aeb6 8048512: Uninitialised memory in jdk/src/share/native/sun/security/ec/ECC_JNI.cpp
vinnie
parents: 25859
diff changeset
   267
    privKey.privateValue.data = NULL;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   268
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   269
    // Initialize the ECParams struct
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   270
    ECParams *ecparams = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   271
    SECKEYECParams params_item;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   272
    params_item.len = env->GetArrayLength(encodedParams);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   273
    params_item.data =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   274
        (unsigned char *) env->GetByteArrayElements(encodedParams, 0);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   275
    if (params_item.data == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   276
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   277
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   278
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   279
    // Fill a new ECParams using the supplied OID
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   280
    if (EC_DecodeParams(&params_item, &ecparams, 0) != SECSuccess) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   281
        /* bad curve OID */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   282
        ThrowException(env, INVALID_ALGORITHM_PARAMETER_EXCEPTION);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   283
        goto cleanup;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   284
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   285
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   286
    // Extract private key data
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   287
    privKey.ecParams = *ecparams; // struct assignment
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   288
    privKey.privateValue.len = env->GetArrayLength(privateKey);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   289
    privKey.privateValue.data =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   290
        (unsigned char *) env->GetByteArrayElements(privateKey, 0);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   291
    if (privKey.privateValue.data == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   292
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   293
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   294
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   295
    // Prepare a buffer for the signature (twice the key length)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   296
    pSignedDigestBuffer = new jbyte[ecparams->order.len * 2];
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   297
    signature_item.data = (unsigned char *) pSignedDigestBuffer;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   298
    signature_item.len = ecparams->order.len * 2;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   299
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   300
    // Copy seed from Java to native buffer
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   301
    pSeedBuffer = new jbyte[jSeedLength];
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   302
    env->GetByteArrayRegion(seed, 0, jSeedLength, pSeedBuffer);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   303
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   304
    // Sign the digest (using the supplied seed)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   305
    if (ECDSA_SignDigest(&privKey, &signature_item, &digest_item,
45993
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 33653
diff changeset
   306
        (unsigned char *) pSeedBuffer, jSeedLength, 0, timing) != SECSuccess) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   307
        ThrowException(env, KEY_EXCEPTION);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   308
        goto cleanup;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   309
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   310
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   311
    // Create new byte array
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   312
    temp = env->NewByteArray(signature_item.len);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   313
    if (temp == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   314
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   315
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   316
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   317
    // Copy data from native buffer
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   318
    env->SetByteArrayRegion(temp, 0, signature_item.len, pSignedDigestBuffer);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   319
    jSignedDigest = temp;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   320
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   321
cleanup:
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   322
    {
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   323
        if (params_item.data) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   324
            env->ReleaseByteArrayElements(encodedParams,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   325
                (jbyte *) params_item.data, JNI_ABORT);
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   326
        }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   327
        if (privKey.privateValue.data) {
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   328
            env->ReleaseByteArrayElements(privateKey,
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   329
                (jbyte *) privKey.privateValue.data, JNI_ABORT);
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   330
        }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   331
        if (pDigestBuffer) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   332
            delete [] pDigestBuffer;
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   333
        }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   334
        if (pSignedDigestBuffer) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   335
            delete [] pSignedDigestBuffer;
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   336
        }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   337
        if (pSeedBuffer) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   338
            delete [] pSeedBuffer;
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   339
        }
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   340
        if (ecparams) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   341
            FreeECParams(ecparams, true);
25530
eff82298a934 8031340: Better TLS/EC management
valeriep
parents: 22645
diff changeset
   342
        }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   343
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   344
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   345
    return jSignedDigest;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   346
}
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   347
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   348
/*
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   349
 * Class:     sun_security_ec_ECDSASignature
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   350
 * Method:    verifySignedDigest
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   351
 * Signature: ([B[B[B[B)Z
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   352
 */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   353
JNIEXPORT jboolean
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   354
JNICALL Java_sun_security_ec_ECDSASignature_verifySignedDigest
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   355
  (JNIEnv *env, jclass clazz, jbyteArray signedDigest, jbyteArray digest, jbyteArray publicKey, jbyteArray encodedParams)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   356
{
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   357
    jboolean isValid = false;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   358
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   359
    // Copy signedDigest from Java to native buffer
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   360
    jbyte* pSignedDigestBuffer = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   361
    jint jSignedDigestLength = env->GetArrayLength(signedDigest);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   362
    pSignedDigestBuffer = new jbyte[jSignedDigestLength];
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   363
    env->GetByteArrayRegion(signedDigest, 0, jSignedDigestLength,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   364
        pSignedDigestBuffer);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   365
    SECItem signature_item;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   366
    signature_item.data = (unsigned char *) pSignedDigestBuffer;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   367
    signature_item.len = jSignedDigestLength;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   368
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   369
    // Copy digest from Java to native buffer
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   370
    jbyte* pDigestBuffer = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   371
    jint jDigestLength = env->GetArrayLength(digest);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   372
    pDigestBuffer = new jbyte[jDigestLength];
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   373
    env->GetByteArrayRegion(digest, 0, jDigestLength, pDigestBuffer);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   374
    SECItem digest_item;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   375
    digest_item.data = (unsigned char *) pDigestBuffer;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   376
    digest_item.len = jDigestLength;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   377
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   378
    // Extract public key data
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   379
    ECPublicKey pubKey;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   380
    pubKey.publicValue.data = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   381
    ECParams *ecparams = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   382
    SECKEYECParams params_item;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   383
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   384
    // Initialize the ECParams struct
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   385
    params_item.len = env->GetArrayLength(encodedParams);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   386
    params_item.data =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   387
        (unsigned char *) env->GetByteArrayElements(encodedParams, 0);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   388
    if (params_item.data == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   389
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   390
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   391
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   392
    // Fill a new ECParams using the supplied OID
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   393
    if (EC_DecodeParams(&params_item, &ecparams, 0) != SECSuccess) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   394
        /* bad curve OID */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   395
        ThrowException(env, INVALID_ALGORITHM_PARAMETER_EXCEPTION);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   396
        goto cleanup;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   397
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   398
    pubKey.ecParams = *ecparams; // struct assignment
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   399
    pubKey.publicValue.len = env->GetArrayLength(publicKey);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   400
    pubKey.publicValue.data =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   401
        (unsigned char *) env->GetByteArrayElements(publicKey, 0);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   402
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   403
    if (ECDSA_VerifyDigest(&pubKey, &signature_item, &digest_item, 0)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   404
            != SECSuccess) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   405
        goto cleanup;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   406
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   407
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   408
    isValid = true;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   409
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   410
cleanup:
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   411
    {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   412
        if (params_item.data)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   413
            env->ReleaseByteArrayElements(encodedParams,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   414
                (jbyte *) params_item.data, JNI_ABORT);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   415
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   416
        if (pubKey.publicValue.data)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   417
            env->ReleaseByteArrayElements(publicKey,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   418
                (jbyte *) pubKey.publicValue.data, JNI_ABORT);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   419
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   420
        if (ecparams)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   421
            FreeECParams(ecparams, true);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   422
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   423
        if (pSignedDigestBuffer)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   424
            delete [] pSignedDigestBuffer;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   425
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   426
        if (pDigestBuffer)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   427
            delete [] pDigestBuffer;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   428
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   429
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   430
    return isValid;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   431
}
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   432
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   433
/*
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   434
 * Class:     sun_security_ec_ECDHKeyAgreement
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   435
 * Method:    deriveKey
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   436
 * Signature: ([B[B[B)[B
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   437
 */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   438
JNIEXPORT jbyteArray
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   439
JNICALL Java_sun_security_ec_ECDHKeyAgreement_deriveKey
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   440
  (JNIEnv *env, jclass clazz, jbyteArray privateKey, jbyteArray publicKey, jbyteArray encodedParams)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   441
{
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   442
    jbyteArray jSecret = NULL;
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   443
    ECParams *ecparams = NULL;
26878
57077013aeb6 8048512: Uninitialised memory in jdk/src/share/native/sun/security/ec/ECC_JNI.cpp
vinnie
parents: 25859
diff changeset
   444
    SECItem privateValue_item;
57077013aeb6 8048512: Uninitialised memory in jdk/src/share/native/sun/security/ec/ECC_JNI.cpp
vinnie
parents: 25859
diff changeset
   445
    privateValue_item.data = NULL;
57077013aeb6 8048512: Uninitialised memory in jdk/src/share/native/sun/security/ec/ECC_JNI.cpp
vinnie
parents: 25859
diff changeset
   446
    SECItem publicValue_item;
57077013aeb6 8048512: Uninitialised memory in jdk/src/share/native/sun/security/ec/ECC_JNI.cpp
vinnie
parents: 25859
diff changeset
   447
    publicValue_item.data = NULL;
57077013aeb6 8048512: Uninitialised memory in jdk/src/share/native/sun/security/ec/ECC_JNI.cpp
vinnie
parents: 25859
diff changeset
   448
    SECKEYECParams params_item;
57077013aeb6 8048512: Uninitialised memory in jdk/src/share/native/sun/security/ec/ECC_JNI.cpp
vinnie
parents: 25859
diff changeset
   449
    params_item.data = NULL;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   450
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   451
    // Extract private key value
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   452
    privateValue_item.len = env->GetArrayLength(privateKey);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   453
    privateValue_item.data =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   454
            (unsigned char *) env->GetByteArrayElements(privateKey, 0);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   455
    if (privateValue_item.data == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   456
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   457
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   458
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   459
    // Extract public key value
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   460
    publicValue_item.len = env->GetArrayLength(publicKey);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   461
    publicValue_item.data =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   462
        (unsigned char *) env->GetByteArrayElements(publicKey, 0);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   463
    if (publicValue_item.data == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   464
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   465
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   466
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   467
    // Initialize the ECParams struct
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   468
    params_item.len = env->GetArrayLength(encodedParams);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   469
    params_item.data =
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   470
        (unsigned char *) env->GetByteArrayElements(encodedParams, 0);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   471
    if (params_item.data == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   472
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   473
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   474
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   475
    // Fill a new ECParams using the supplied OID
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   476
    if (EC_DecodeParams(&params_item, &ecparams, 0) != SECSuccess) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   477
        /* bad curve OID */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   478
        ThrowException(env, INVALID_ALGORITHM_PARAMETER_EXCEPTION);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   479
        goto cleanup;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   480
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   481
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   482
    // Prepare a buffer for the secret
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   483
    SECItem secret_item;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   484
    secret_item.data = NULL;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   485
    secret_item.len = ecparams->order.len * 2;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   486
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   487
    if (ECDH_Derive(&publicValue_item, ecparams, &privateValue_item, B_FALSE,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   488
        &secret_item, 0) != SECSuccess) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   489
        ThrowException(env, ILLEGAL_STATE_EXCEPTION);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   490
        goto cleanup;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   491
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   493
    // Create new byte array
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   494
    jSecret = env->NewByteArray(secret_item.len);
22645
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   495
    if (jSecret == NULL) {
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   496
        goto cleanup;
636153f07b26 8028727: [parfait] warnings from b116 for jdk.src.share.native.sun.security.ec: JNI pending exceptions
vinnie
parents: 9035
diff changeset
   497
    }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   498
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   499
    // Copy bytes from the SECItem buffer to a Java byte array
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   500
    env->SetByteArrayRegion(jSecret, 0, secret_item.len,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   501
        (jbyte *)secret_item.data);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   502
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   503
    // Free the SECItem data buffer
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   504
    SECITEM_FreeItem(&secret_item, B_FALSE);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   505
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   506
cleanup:
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   507
    {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   508
        if (privateValue_item.data)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   509
            env->ReleaseByteArrayElements(privateKey,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   510
                (jbyte *) privateValue_item.data, JNI_ABORT);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   511
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   512
        if (publicValue_item.data)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   513
            env->ReleaseByteArrayElements(publicKey,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   514
                (jbyte *) publicValue_item.data, JNI_ABORT);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   515
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   516
        if (params_item.data)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   517
            env->ReleaseByteArrayElements(encodedParams,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   518
                (jbyte *) params_item.data, JNI_ABORT);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   519
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   520
        if (ecparams)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   521
            FreeECParams(ecparams, true);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   522
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   523
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   524
    return jSecret;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   525
}
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   526
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   527
} /* extern "C" */