jdk/src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp
author duke
Wed, 05 Jul 2017 21:06:18 +0200
changeset 34465 41a1258588da
parent 33653 c1ee09fe3274
child 36759 07dc1868fd1e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
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: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
//=--------------------------------------------------------------------------=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
// security.cpp    by Stanley Man-Kit Ho
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
//=--------------------------------------------------------------------------=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <jni.h>
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    32
#include "jni_util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdlib.h>
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    34
#include <string.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <BaseTsd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <wincrypt.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#define OID_EKU_ANY         "2.5.29.37.0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#define CERTIFICATE_PARSING_EXCEPTION \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                            "java/security/cert/CertificateParsingException"
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
    45
#define INVALID_KEY_EXCEPTION \
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
    46
                            "java/security/InvalidKeyException"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#define KEY_EXCEPTION       "java/security/KeyException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#define KEYSTORE_EXCEPTION  "java/security/KeyStoreException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define PROVIDER_EXCEPTION  "java/security/ProviderException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define SIGNATURE_EXCEPTION "java/security/SignatureException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
extern "C" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    55
 * Declare library specific JNI_Onload entry if static build
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    56
 */
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    57
DEF_STATIC_JNI_OnLoad
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    58
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    59
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Throws an arbitrary Java exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * The exception message is a Windows system error message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
void ThrowException(JNIEnv *env, char *exceptionName, DWORD dwError)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    char szMessage[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    szMessage[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    68
    DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    69
        NULL, szMessage, sizeof(szMessage), NULL);
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    70
    if (res == 0) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    71
        strcpy(szMessage, "Unknown error");
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    72
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    jclass exceptionClazz = env->FindClass(exceptionName);
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    75
    if (exceptionClazz != NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    76
        env->ThrowNew(exceptionClazz, szMessage);
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    77
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * Maps the name of a hash algorithm to an algorithm identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
ALG_ID MapHashAlgorithm(JNIEnv *env, jstring jHashAlgorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    const char* pszHashAlgorithm = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    ALG_ID algId = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
    89
    if ((pszHashAlgorithm = env->GetStringUTFChars(jHashAlgorithm, NULL))
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
    90
        == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
    91
        return algId;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
    92
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if ((strcmp("SHA", pszHashAlgorithm) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        (strcmp("SHA1", pszHashAlgorithm) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        (strcmp("SHA-1", pszHashAlgorithm) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        algId = CALG_SHA1;
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    99
    } else if (strcmp("SHA1+MD5", pszHashAlgorithm) == 0) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   100
        algId = CALG_SSL3_SHAMD5; // a 36-byte concatenation of SHA-1 and MD5
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    } else if (strcmp("SHA-256", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        algId = CALG_SHA_256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    } else if (strcmp("SHA-384", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        algId = CALG_SHA_384;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    } else if (strcmp("SHA-512", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        algId = CALG_SHA_512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    } else if (strcmp("MD5", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        algId = CALG_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    } else if (strcmp("MD2", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        algId = CALG_MD2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    if (pszHashAlgorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        env->ReleaseStringUTFChars(jHashAlgorithm, pszHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
   return algId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * Returns a certificate chain context given a certificate context and key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * usage identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
bool GetCertificateChain(LPSTR lpszKeyUsageIdentifier, PCCERT_CONTEXT pCertContext, PCCERT_CHAIN_CONTEXT* ppChainContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    CERT_ENHKEY_USAGE        EnhkeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    CERT_USAGE_MATCH         CertUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    CERT_CHAIN_PARA          ChainPara;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    DWORD                    dwFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    LPSTR                    szUsageIdentifierArray[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    szUsageIdentifierArray[0] = lpszKeyUsageIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    EnhkeyUsage.cUsageIdentifier = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    EnhkeyUsage.rgpszUsageIdentifier = szUsageIdentifierArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    CertUsage.dwType = USAGE_MATCH_TYPE_AND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    CertUsage.Usage  = EnhkeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    ChainPara.cbSize = sizeof(CERT_CHAIN_PARA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    ChainPara.RequestedUsage=CertUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // Build a chain using CertGetCertificateChain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // and the certificate retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    return (::CertGetCertificateChain(NULL,     // use the default chain engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                pCertContext,   // pointer to the end certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                NULL,           // use the default time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                NULL,           // search no additional stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                &ChainPara,     // use AND logic and enhanced key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                //  as indicated in the ChainPara
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                //  data structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                dwFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                NULL,           // currently reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                ppChainContext) == TRUE);       // return a pointer to the chain created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
/////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * Class:     sun_security_mscapi_PRNG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * Method:    generateSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * Signature: (I[B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_PRNG_generateSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
  (JNIEnv *env, jclass clazz, jint length, jbyteArray seed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    BYTE*      pbData = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    jbyte*     reseedBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    jbyte*     seedBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    jbyteArray result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        //  Acquire a CSP context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if(::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
           &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
           NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
           NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
           PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
           CRYPT_VERIFYCONTEXT) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            ThrowException(env, PROVIDER_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         * If length is negative then use the supplied seed to re-seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * generator and return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * If length is non-zero then generate a new seed according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * requested length and return the new seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         * If length is zero then overwrite the supplied seed with a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * seed of the same length and return the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            length = env->GetArrayLength(seed);
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   197
            if ((reseedBytes = env->GetByteArrayElements(seed, 0)) == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   198
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   199
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (::CryptGenRandom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                (BYTE *) reseedBytes) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                ThrowException(env, PROVIDER_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } else if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            pbData = new BYTE[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (::CryptGenRandom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                pbData) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                ThrowException(env, PROVIDER_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            result = env->NewByteArray(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            env->SetByteArrayRegion(result, 0, length, (jbyte*) pbData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else { // length == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            length = env->GetArrayLength(seed);
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   231
            if ((seedBytes = env->GetByteArrayElements(seed, 0)) == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   232
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   233
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (::CryptGenRandom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                (BYTE *) seedBytes) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                ThrowException(env, PROVIDER_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            result = seed; // seed will be updated when seedBytes gets released
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (reseedBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            env->ReleaseByteArrayElements(seed, reseedBytes, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (pbData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            delete [] pbData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (seedBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            env->ReleaseByteArrayElements(seed, seedBytes, 0); // update orig
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (hCryptProv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            ::CryptReleaseContext(hCryptProv, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * Method:    loadKeysOrCertificateChains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * Signature: (Ljava/lang/String;Ljava/util/Collection;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
JNIEXPORT void JNICALL Java_sun_security_mscapi_KeyStore_loadKeysOrCertificateChains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jobject jCollections)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Certificate in cert store has enhanced key usage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * property (or EKU property) that is not part of the certificate itself. To determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * if the certificate should be returned, both the enhanced key usage in certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * extension block and the extension property stored along with the certificate in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * certificate store should be examined. Otherwise, we won't be able to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * the proper key usage from the Java side because the information is not stored as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * part of the encoded certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    char* pszNameString = NULL; // certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    DWORD cchNameString = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // Open a system certificate store.
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   297
        if ((pszCertStoreName = env->GetStringUTFChars(jCertStoreName, NULL))
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   298
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   299
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   300
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // Determine clazz and method ID to generate certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        jclass clazzArrayList = env->FindClass("java/util/ArrayList");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   310
        if (clazzArrayList == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   311
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   312
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        jmethodID mNewArrayList = env->GetMethodID(clazzArrayList, "<init>", "()V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   315
        if (mNewArrayList == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   316
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   317
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   319
        jclass clazzOfThis = env->GetObjectClass(obj);
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   320
        if (clazzOfThis == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   321
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   322
        }
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   323
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   324
        jmethodID mGenCert = env->GetMethodID(clazzOfThis,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                              "generateCertificate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                              "([BLjava/util/Collection;)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   327
        if (mGenCert == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   328
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   329
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // Determine method ID to generate certificate chain
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   332
        jmethodID mGenCertChain = env->GetMethodID(clazzOfThis,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                                   "generateCertificateChain",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                                   "(Ljava/lang/String;Ljava/util/Collection;Ljava/util/Collection;)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   335
        if (mGenCertChain == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   336
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   337
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // Determine method ID to generate RSA certificate chain
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   340
        jmethodID mGenRSAKeyAndCertChain = env->GetMethodID(clazzOfThis,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                                   "generateRSAKeyAndCertificateChain",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                                   "(Ljava/lang/String;JJILjava/util/Collection;Ljava/util/Collection;)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   343
        if (mGenRSAKeyAndCertChain == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   344
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   345
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // Use CertEnumCertificatesInStore to get the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // from the open store. pCertContext must be reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // NULL to retrieve the first certificate in the store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        while (pCertContext = ::CertEnumCertificatesInStore(hCertStore, pCertContext))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // Check if private key available - client authentication certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            // must have private key available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            DWORD dwKeySpec = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            HCRYPTKEY hUserKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            BOOL bCallerFreeProv = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            BOOL bHasNoPrivateKey = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            DWORD dwPublicKeyLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (::CryptAcquireCertificatePrivateKey(pCertContext, NULL, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                                    &hCryptProv, &dwKeySpec, &bCallerFreeProv) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                bHasNoPrivateKey = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                // Private key is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            BOOL bGetUserKey = ::CryptGetUserKey(hCryptProv, dwKeySpec, &hUserKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // Skip certificate if cannot find private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (bGetUserKey == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if (bCallerFreeProv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    ::CryptReleaseContext(hCryptProv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // Set cipher mode to ECB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            DWORD dwCipherMode = CRYPT_MODE_ECB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            ::CryptSetKeyParam(hUserKey, KP_MODE, (BYTE*)&dwCipherMode, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            // If the private key is present in smart card, we may not be able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // determine the key length by using the private key handle. However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            // since public/private key pairs must have the same length, we could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            // determine the key length of the private key by using the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            // in the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            dwPublicKeyLength = ::CertGetPublicKeyLength(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                                               &(pCertContext->pCertInfo->SubjectPublicKeyInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            PCCERT_CHAIN_CONTEXT pCertChainContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // Build certificate chain by using system certificate store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // Add cert chain into collection for any key usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (GetCertificateChain(OID_EKU_ANY, pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                &pCertChainContext))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                for (unsigned int i=0; i < pCertChainContext->cChain; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    // Found cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    PCERT_SIMPLE_CHAIN rgpChain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        pCertChainContext->rgpChain[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    // Create ArrayList to store certs in each chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    jobject jArrayList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        env->NewObject(clazzArrayList, mNewArrayList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    for (unsigned int j=0; j < rgpChain->cElement; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        PCERT_CHAIN_ELEMENT rgpElement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                            rgpChain->rgpElement[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        PCCERT_CONTEXT pc = rgpElement->pCertContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        // Retrieve the friendly name of the first certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        // in the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        if (j == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                            // If the cert's name cannot be retrieved then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                            // pszNameString remains set to NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            // (An alias name will be generated automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                            // when storing this cert in the keystore.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                            // Get length of friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                            if ((cchNameString = CertGetNameString(pc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                NULL, 0)) > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                // Found friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                pszNameString = new char[cchNameString];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                CertGetNameString(pc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                                    CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                    pszNameString, cchNameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        BYTE* pbCertEncoded = pc->pbCertEncoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        DWORD cbCertEncoded = pc->cbCertEncoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        // Allocate and populate byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        jbyteArray byteArray = env->NewByteArray(cbCertEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        env->SetByteArrayRegion(byteArray, 0, cbCertEncoded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                            (jbyte*) pbCertEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        // Generate certificate from byte array and store into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        // cert collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        env->CallVoidMethod(obj, mGenCert, byteArray, jArrayList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    if (bHasNoPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        // Generate certificate chain and store into cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        // collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        env->CallVoidMethod(obj, mGenCertChain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            env->NewStringUTF(pszNameString),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                            jArrayList, jCollections);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    // Determine key type: RSA or DSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    DWORD dwData = CALG_RSA_KEYX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    DWORD dwSize = sizeof(DWORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    ::CryptGetKeyParam(hUserKey, KP_ALGID, (BYTE*)&dwData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        &dwSize, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    if ((dwData & ALG_TYPE_RSA) == ALG_TYPE_RSA)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        // Generate RSA certificate chain and store into cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        // chain collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        env->CallVoidMethod(obj, mGenRSAKeyAndCertChain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                            env->NewStringUTF(pszNameString),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                            (jlong) hCryptProv, (jlong) hUserKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                            dwPublicKeyLength, jArrayList, jCollections);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                // Free cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                if (pCertChainContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    ::CertFreeCertificateChain(pCertChainContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (pszNameString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
 * Class:     sun_security_mscapi_Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
 * Method:    cleanUp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
 * Signature: (JJ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
JNIEXPORT void JNICALL Java_sun_security_mscapi_Key_cleanUp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
  (JNIEnv *env, jclass clazz, jlong hCryptProv, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    if (hCryptKey != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        ::CryptDestroyKey((HCRYPTKEY) hCryptKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    if (hCryptProv != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        ::CryptReleaseContext((HCRYPTPROV) hCryptProv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
 * Class:     sun_security_mscapi_RSASignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
 * Method:    signHash
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   520
 * Signature: (Z[BILjava/lang/String;JJ)[B
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSASignature_signHash
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   523
  (JNIEnv *env, jclass clazz, jboolean noHashOID, jbyteArray jHash,
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   524
        jint jHashSize, jstring jHashAlgorithm, jlong hCryptProv,
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   525
        jlong hCryptKey)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    HCRYPTHASH hHash = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    jbyte* pHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    jbyte* pSignedHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    jbyteArray jSignedHash = NULL;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   531
    HCRYPTPROV hCryptProvAlt = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        // Map hash algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        ALG_ID algId = MapHashAlgorithm(env, jHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        // Acquire a hash object handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (::CryptCreateHash(HCRYPTPROV(hCryptProv), algId, 0, 0, &hHash) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   541
            // Failover to using the PROV_RSA_AES CSP
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   542
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   543
            DWORD cbData = 256;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   544
            BYTE pbData[256];
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   545
            pbData[0] = '\0';
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   546
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   547
            // Get name of the key container
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   548
            ::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_CONTAINER,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   549
                (BYTE *)pbData, &cbData, 0);
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   550
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   551
            // Acquire an alternative CSP handle
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   552
            if (::CryptAcquireContext(&hCryptProvAlt, LPCSTR(pbData), NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   553
                PROV_RSA_AES, 0) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   554
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   555
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   556
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   557
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   558
            }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   559
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   560
            // Acquire a hash object handle.
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   561
            if (::CryptCreateHash(HCRYPTPROV(hCryptProvAlt), algId, 0, 0,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   562
                &hHash) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   563
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   564
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   565
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   566
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        // Copy hash from Java to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        pHashBuffer = new jbyte[jHashSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        // Set hash value in the hash object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*)pHashBuffer, NULL) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // Determine key spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        DWORD dwKeySpec = AT_SIGNATURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        ALG_ID dwAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        DWORD dwAlgIdLen = sizeof(ALG_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (! ::CryptGetKeyParam((HCRYPTKEY) hCryptKey, KP_ALGID, (BYTE*)&dwAlgId, &dwAlgIdLen, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if (CALG_RSA_KEYX == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            dwKeySpec = AT_KEYEXCHANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        // Determine size of buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        DWORD dwBufLen = 0;
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   596
        DWORD dwFlags = 0;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   597
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   598
        if (noHashOID == JNI_TRUE) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   599
            dwFlags = CRYPT_NOHASHOID; // omit hash OID in NONEwithRSA signature
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   600
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   601
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   602
        if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, NULL, &dwBufLen) == FALSE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        pSignedHashBuffer = new jbyte[dwBufLen];
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   609
        if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, (BYTE*)pSignedHashBuffer, &dwBufLen) == FALSE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // Create new byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        jbyteArray temp = env->NewByteArray(dwBufLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // Copy data from native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        env->SetByteArrayRegion(temp, 0, dwBufLen, pSignedHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        jSignedHash = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if (pSignedHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            delete [] pSignedHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (pHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            delete [] pHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (hHash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            ::CryptDestroyHash(hHash);
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   633
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   634
        if (hCryptProvAlt)
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   635
            ::CryptReleaseContext(hCryptProvAlt, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    return jSignedHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
 * Class:     sun_security_mscapi_RSASignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
 * Method:    verifySignedHash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
 * Signature: ([BIL/java/lang/String;[BIJJ)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
JNIEXPORT jboolean JNICALL Java_sun_security_mscapi_RSASignature_verifySignedHash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
  (JNIEnv *env, jclass clazz, jbyteArray jHash, jint jHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        jstring jHashAlgorithm, jbyteArray jSignedHash, jint jSignedHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        jlong hCryptProv, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    HCRYPTHASH hHash = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    jbyte* pHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    jbyte* pSignedHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    DWORD dwSignedHashBufferLen = jSignedHashSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    jboolean result = JNI_FALSE;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   656
    HCRYPTPROV hCryptProvAlt = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        // Map hash algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        ALG_ID algId = MapHashAlgorithm(env, jHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        // Acquire a hash object handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (::CryptCreateHash(HCRYPTPROV(hCryptProv), algId, 0, 0, &hHash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   667
            // Failover to using the PROV_RSA_AES CSP
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   668
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   669
            DWORD cbData = 256;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   670
            BYTE pbData[256];
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   671
            pbData[0] = '\0';
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   672
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   673
            // Get name of the key container
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   674
            ::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_CONTAINER,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   675
                (BYTE *)pbData, &cbData, 0);
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   676
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   677
            // Acquire an alternative CSP handle
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   678
            if (::CryptAcquireContext(&hCryptProvAlt, LPCSTR(pbData), NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   679
                PROV_RSA_AES, 0) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   680
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   681
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   682
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   683
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   684
            }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   685
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   686
            // Acquire a hash object handle.
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   687
            if (::CryptCreateHash(HCRYPTPROV(hCryptProvAlt), algId, 0, 0,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   688
                &hHash) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   689
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   690
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   691
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   692
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        // Copy hash and signedHash from Java to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        pHashBuffer = new jbyte[jHashSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        pSignedHashBuffer = new jbyte[jSignedHashSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        env->GetByteArrayRegion(jSignedHash, 0, jSignedHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            pSignedHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        // Set hash value in the hash object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*) pHashBuffer, NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        // For RSA, the hash encryption algorithm is normally the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        // public key algorithm, so AT_SIGNATURE is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        // Verify the signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (::CryptVerifySignatureA(hHash, (BYTE *) pSignedHashBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            dwSignedHashBufferLen, (HCRYPTKEY) hCryptKey, NULL, 0) == TRUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            result = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (pSignedHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            delete [] pSignedHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (pHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            delete [] pHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (hHash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            ::CryptDestroyHash(hHash);
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   731
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   732
        if (hCryptProvAlt)
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   733
            ::CryptReleaseContext(hCryptProvAlt, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
 * Class:     sun_security_mscapi_RSAKeyPairGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
 * Method:    generateRSAKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
 * Signature: (ILjava/lang/String;)Lsun/security/mscapi/RSAKeyPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_RSAKeyPairGenerator_generateRSAKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
  (JNIEnv *env, jclass clazz, jint keySize, jstring keyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    HCRYPTKEY hKeyPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    DWORD dwFlags = (keySize << 16) | CRYPT_EXPORTABLE;
9674
5d8476e6e47a 6987652: VM crashed in sun.security.mscapi.RSAKeyPairGenerator.generateRSAKeyPair(...)
vinnie
parents: 9533
diff changeset
   750
    jobject keypair = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    const char* pszKeyContainerName = NULL; // UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   755
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   756
            env->GetStringUTFChars(keyContainerName, NULL)) == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   757
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   758
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        // Acquire a CSP context (create a new key container).
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   761
        // Prefer a PROV_RSA_AES CSP, when available, due to its support
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   762
        // for SHA-2-based signatures.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            NULL,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   767
            PROV_RSA_AES,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   770
            // Failover to using the default CSP (PROV_RSA_FULL)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   771
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   772
            if (::CryptAcquireContext(
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   773
                &hCryptProv,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   774
                pszKeyContainerName,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   775
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   776
                PROV_RSA_FULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   777
                CRYPT_NEWKEYSET) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   778
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   779
                ThrowException(env, KEY_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   780
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   781
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        // Generate an RSA keypair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if(::CryptGenKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
           hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
           AT_KEYEXCHANGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
           dwFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
           &hKeyPair) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        // Get the method ID for the RSAKeyPair constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        jclass clazzRSAKeyPair =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            env->FindClass("sun/security/mscapi/RSAKeyPair");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   798
        if (clazzRSAKeyPair == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   799
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   800
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        jmethodID mNewRSAKeyPair =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            env->GetMethodID(clazzRSAKeyPair, "<init>", "(JJI)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   804
        if (mNewRSAKeyPair == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   805
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   806
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        // Create a new RSA keypair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        keypair = env->NewObject(clazzRSAKeyPair, mNewRSAKeyPair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            (jlong) hCryptProv, (jlong) hKeyPair, keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    return keypair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
 * Class:     sun_security_mscapi_Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
 * Method:    getContainerName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
 * Signature: (J)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
JNIEXPORT jstring JNICALL Java_sun_security_mscapi_Key_getContainerName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
  (JNIEnv *env, jclass jclazz, jlong hCryptProv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    DWORD cbData = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    BYTE pbData[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    pbData[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        (HCRYPTPROV)hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        (BYTE *)pbData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        &cbData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    return env->NewStringUTF((const char*)pbData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
 * Class:     sun_security_mscapi_Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
 * Method:    getKeyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
 * Signature: (J)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
JNIEXPORT jstring JNICALL Java_sun_security_mscapi_Key_getKeyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
  (JNIEnv *env, jclass jclazz, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    ALG_ID dwAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    DWORD dwAlgIdLen = sizeof(ALG_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    if (::CryptGetKeyParam((HCRYPTKEY) hCryptKey, KP_ALGID, (BYTE*)&dwAlgId, &dwAlgIdLen, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (CALG_RSA_SIGN == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            return env->NewStringUTF("Signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        } else if (CALG_RSA_KEYX == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            return env->NewStringUTF("Exchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            char buffer[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            if (sprintf(buffer, "%lu", dwAlgId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                return env->NewStringUTF(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    return env->NewStringUTF("<Unknown>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
 * Method:    storeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
 * Signature: (Ljava/lang/String;Ljava/lang/String;[BIJJ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
JNIEXPORT void JNICALL Java_sun_security_mscapi_KeyStore_storeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        jbyteArray jCertEncoding, jint jCertEncodingSize, jlong hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    PWCHAR pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    jbyte* pbCertEncoding = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    const jchar* jCertAliasChars = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    const char* pszContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    const char* pszProviderName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    WCHAR * pwszContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    WCHAR * pwszProviderName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        // Open a system certificate store.
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   901
        if ((pszCertStoreName = env->GetStringUTFChars(jCertStoreName, NULL))
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   902
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   903
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   904
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        // Copy encoding from Java to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        pbCertEncoding = new jbyte[jCertEncodingSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        // Create a certificate context from the encoded cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            (BYTE*) pbCertEncoding, jCertEncodingSize))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            ThrowException(env, CERTIFICATE_PARSING_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        // Set the certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        int size = env->GetStringLength(jCertAliasName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        pszCertAliasName = new WCHAR[size + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        jCertAliasChars = env->GetStringChars(jCertAliasName, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        memcpy(pszCertAliasName, jCertAliasChars, size * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        pszCertAliasName[size] = 0; // append the string terminator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        CRYPT_DATA_BLOB friendlyName = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            sizeof(WCHAR) * (size + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            (BYTE *) pszCertAliasName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        env->ReleaseStringChars(jCertAliasName, jCertAliasChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        if (! ::CertSetCertificateContextProperty(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            CERT_FRIENDLY_NAME_PROP_ID, 0, &friendlyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        // Attach the certificate's private key (if supplied)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        if (hCryptProv != 0 && hCryptKey != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            CRYPT_KEY_PROV_INFO keyProviderInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            DWORD dwDataLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            // Get the name of the key container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            pszContainerName = new char[dwDataLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                (BYTE *) pszContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            // Convert to a wide char string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            pwszContainerName = new WCHAR[dwDataLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            if (mbstowcs(pwszContainerName, pszContainerName, dwDataLen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            // Set the name of the key container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            keyProviderInfo.pwszContainerName = pwszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            // Get the name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                PP_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            pszProviderName = new char[dwDataLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                PP_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                (BYTE *) pszProviderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            // Convert to a wide char string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            pwszProviderName = new WCHAR[dwDataLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            if (mbstowcs(pwszProviderName, pszProviderName, dwDataLen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            // Set the name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            keyProviderInfo.pwszProvName = pwszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            // Get and set the type of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                PP_PROVTYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                (LPBYTE) &keyProviderInfo.dwProvType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            // Set no provider flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            keyProviderInfo.dwFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            // Set no provider parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            keyProviderInfo.cProvParam = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            keyProviderInfo.rgProvParam = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            // Get the key's algorithm ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            if (! ::CryptGetKeyParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                (HCRYPTKEY) hCryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                KP_ALGID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                (LPBYTE) &keyProviderInfo.dwKeySpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            // Set the key spec (using the algorithm ID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            switch (keyProviderInfo.dwKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            case CALG_RSA_KEYX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            case CALG_DH_SF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                keyProviderInfo.dwKeySpec = AT_KEYEXCHANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            case CALG_RSA_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            case CALG_DSS_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                keyProviderInfo.dwKeySpec = AT_SIGNATURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_ALGID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            if (! ::CertSetCertificateContextProperty(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                CERT_KEY_PROV_INFO_PROP_ID, 0, &keyProviderInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        // Import encoded certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        if (!::CertAddCertificateContextToStore(hCertStore, pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            CERT_STORE_ADD_REPLACE_EXISTING, NULL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        if (pbCertEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            delete [] pbCertEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            delete [] pszCertAliasName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        if (pszContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            delete [] pszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (pwszContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            delete [] pwszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        if (pszProviderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            delete [] pszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        if (pwszProviderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            delete [] pwszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        if (pCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            ::CertFreeCertificateContext(pCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
 * Method:    removeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
 * Signature: (Ljava/lang/String;Ljava/lang/String;[BI)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
JNIEXPORT void JNICALL Java_sun_security_mscapi_KeyStore_removeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
  jbyteArray jCertEncoding, jint jCertEncodingSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    const char* pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    PCCERT_CONTEXT pTBDCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    jbyte* pbCertEncoding = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    DWORD cchNameString = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    char* pszNameString = NULL; // certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    BOOL bDeleteAttempted = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        // Open a system certificate store.
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1143
        if ((pszCertStoreName = env->GetStringUTFChars(jCertStoreName, NULL))
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1144
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1145
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1146
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        // Copy encoding from Java to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        pbCertEncoding = new jbyte[jCertEncodingSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        // Create a certificate context from the encoded cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            (BYTE*) pbCertEncoding, jCertEncodingSize))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            ThrowException(env, CERTIFICATE_PARSING_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        // Find the certificate to be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        if (!(pTBDCertContext = ::CertFindCertificateInStore(hCertStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            X509_ASN_ENCODING, 0, CERT_FIND_EXISTING, pCertContext, NULL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        // Check that its friendly name matches the supplied alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        if ((cchNameString = ::CertGetNameString(pTBDCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            pszNameString = new char[cchNameString];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            ::CertGetNameString(pTBDCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                cchNameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            // Compare the certificate's friendly name with supplied alias name
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1183
            if ((pszCertAliasName = env->GetStringUTFChars(jCertAliasName, NULL))
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1184
                == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1185
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1186
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            if (strcmp(pszCertAliasName, pszNameString) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                // Only delete the certificate if the alias names matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                if (! ::CertDeleteCertificateFromStore(pTBDCertContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    // pTBDCertContext is always freed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    //  CertDeleteCertificateFromStore method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    bDeleteAttempted = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            env->ReleaseStringUTFChars(jCertAliasName, pszCertAliasName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        if (pbCertEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            delete [] pbCertEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        if (pszNameString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        if (pCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            ::CertFreeCertificateContext(pCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        if (bDeleteAttempted && pTBDCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            ::CertFreeCertificateContext(pTBDCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
 * Method:    destroyKeyContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
 * Signature: (Ljava/lang/String;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
JNIEXPORT void JNICALL Java_sun_security_mscapi_KeyStore_destroyKeyContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
  (JNIEnv *env, jclass clazz, jstring keyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    const char* pszKeyContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1244
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1245
            env->GetStringUTFChars(keyContainerName, NULL)) == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1246
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1247
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        // Destroying the default key container is not permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        // (because it may contain more one keypair).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        if (pszKeyContainerName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_KEYSET_PARAM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        // Acquire a CSP context (to the key container).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            CRYPT_DELETEKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
 * Class:     sun_security_mscapi_RSACipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
 * Method:    findCertificateUsingAlias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
JNIEXPORT jlong JNICALL Java_sun_security_mscapi_RSACipher_findCertificateUsingAlias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    const char* pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    char* pszNameString = NULL; // certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    DWORD cchNameString = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1300
        if ((pszCertStoreName = env->GetStringUTFChars(jCertStoreName, NULL))
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1301
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1302
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1303
        }
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1304
        if ((pszCertAliasName = env->GetStringUTFChars(jCertAliasName, NULL))
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1305
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1306
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1307
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        // Open a system certificate store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        // Use CertEnumCertificatesInStore to get the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        // from the open store. pCertContext must be reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        // NULL to retrieve the first certificate in the store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        while (pCertContext = ::CertEnumCertificatesInStore(hCertStore, pCertContext))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            if ((cchNameString = ::CertGetNameString(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                continue; // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            pszNameString = new char[cchNameString];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            if (::CertGetNameString(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                cchNameString) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                continue; // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            // Compare the certificate's friendly name with supplied alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            if (strcmp(pszCertAliasName, pszNameString) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            env->ReleaseStringUTFChars(jCertAliasName, pszCertAliasName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    return (jlong) pCertContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
 * Class:     sun_security_mscapi_RSACipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
 * Method:    getKeyFromCert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
 * Signature: (JZ)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
JNIEXPORT jlong JNICALL Java_sun_security_mscapi_RSACipher_getKeyFromCert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
  (JNIEnv *env, jobject obj, jlong pCertContext, jboolean usePrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    DWORD dwKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        if (usePrivateKey == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            // Locate the key container for the certificate's private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            if (!(::CryptAcquireCertificatePrivateKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                (PCCERT_CONTEXT) pCertContext, 0, NULL, &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                &dwKeySpec, NULL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            // Get a handle to the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            if (!(::CryptGetUserKey(hCryptProv, dwKeySpec, &hKey))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        } else { // use public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            //  Acquire a CSP context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            if(::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
               &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
               "J2SE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
               NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
               PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
               0) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                // If CSP context hasn't been created, create one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    "J2SE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                    PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                    CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                    ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                    __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            // Import the certificate's public key into the key container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            if (!(::CryptImportPublicKeyInfo(hCryptProv, X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                &(((PCCERT_CONTEXT) pCertContext)->pCertInfo->SubjectPublicKeyInfo),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                &hKey))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        if (hCryptProv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            ::CryptReleaseContext(hCryptProv, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    return hKey;        // TODO - when finished with this key, call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                        //              CryptDestroyKey(hKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
 * Method:    getKeyLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
 * Signature: (J)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
JNIEXPORT jint JNICALL Java_sun_security_mscapi_KeyStore_getKeyLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
  (JNIEnv *env, jobject obj, jlong hKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    DWORD dwDataLen = sizeof(DWORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    BYTE pbData[sizeof(DWORD)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    DWORD length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        // Get key length (in bits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        //TODO - may need to use KP_BLOCKLEN instead?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        if (!(::CryptGetKeyParam((HCRYPTKEY) hKey, KP_KEYLEN, (BYTE *)pbData, &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        length = (DWORD) pbData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        // no cleanup required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    return (jint) length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
 * Class:     sun_security_mscapi_RSACipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
 * Method:    encryptDecrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
 * Signature: ([BIJZ)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSACipher_encryptDecrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
  (JNIEnv *env, jclass clazz, jbyteArray jData, jint jDataSize, jlong hKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
   jboolean doEncrypt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    jbyteArray result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    jbyte* pData = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    DWORD dwDataLen = jDataSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    DWORD dwBufLen = env->GetArrayLength(jData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    DWORD i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    BYTE tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        // Copy data from Java buffer to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        pData = new jbyte[dwBufLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        env->GetByteArrayRegion(jData, 0, dwBufLen, pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        if (doEncrypt == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            // encrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            if (! ::CryptEncrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                &dwDataLen, dwBufLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            dwBufLen = dwDataLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            // convert from little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            for (i = 0; i < dwBufLen / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                tmp = pData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                pData[i] = pData[dwBufLen - i -1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                pData[dwBufLen - i - 1] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            // convert to little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            for (i = 0; i < dwBufLen / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                tmp = pData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                pData[i] = pData[dwBufLen - i -1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                pData[dwBufLen - i - 1] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            // decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            if (! ::CryptDecrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                &dwBufLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        // Create new byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        result = env->NewByteArray(dwBufLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        // Copy data from native buffer to Java buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        env->SetByteArrayRegion(result, 0, dwBufLen, (jbyte*) pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        if (pData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            delete [] pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
 * Class:     sun_security_mscapi_RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
 * Method:    getPublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
 * Signature: (J)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getPublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    (JNIEnv *env, jclass clazz, jlong hCryptKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    jbyteArray blob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    DWORD dwBlobLen;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
  1549
    BYTE* pbKeyBlob = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        // Determine the size of the blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            &dwBlobLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        pbKeyBlob = new BYTE[dwBlobLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        // Generate key blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            pbKeyBlob, &dwBlobLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        // Create new byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        blob = env->NewByteArray(dwBlobLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        // Copy data from native buffer to Java buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        env->SetByteArrayRegion(blob, 0, dwBlobLen, (jbyte*) pbKeyBlob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            delete [] pbKeyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    return blob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
 * Class:     sun_security_mscapi_RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
 * Method:    getExponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
 * Signature: ([B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getExponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    (JNIEnv *env, jclass clazz, jbyteArray jKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    jbyteArray exponent = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    jbyte*     exponentBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    jbyte*     keyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        jsize length = env->GetArrayLength(jKeyBlob);
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1602
        if ((keyBlob = env->GetByteArrayElements(jKeyBlob, 0)) == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1603
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1604
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        // Check BLOB type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        RSAPUBKEY* pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        int len = sizeof(pRsaPubKey->pubexp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        exponentBytes = new jbyte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        // convert from little-endian while copying from blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        for (int i = 0, j = len - 1; i < len; i++, j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            exponentBytes[i] = ((BYTE*) &pRsaPubKey->pubexp)[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        exponent = env->NewByteArray(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        env->SetByteArrayRegion(exponent, 0, len, exponentBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        if (keyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        if (exponentBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            delete [] exponentBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
 * Class:     sun_security_mscapi_RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
 * Method:    getModulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
 * Signature: ([B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getModulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    (JNIEnv *env, jclass clazz, jbyteArray jKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
    jbyteArray modulus = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    jbyte*     modulusBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    jbyte*     keyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        jsize length = env->GetArrayLength(jKeyBlob);
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1654
        if ((keyBlob = env->GetByteArrayElements(jKeyBlob, 0)) == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1655
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1656
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        // Check BLOB type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        RSAPUBKEY* pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        int len = pRsaPubKey->bitlen / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        modulusBytes = new jbyte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        BYTE * pbModulus =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            (BYTE *) (keyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        // convert from little-endian while copying from blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        for (int i = 0, j = len - 1; i < len; i++, j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            modulusBytes[i] = pbModulus[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        modulus = env->NewByteArray(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        env->SetByteArrayRegion(modulus, 0, len, modulusBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        if (keyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        if (modulusBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            delete [] modulusBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    return modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
 * Convert an array in big-endian byte order into little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
int convertToLittleEndian(JNIEnv *env, jbyteArray source, jbyte* destination,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
    int destinationLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    int sourceLength = env->GetArrayLength(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    jbyte* sourceBytes = env->GetByteArrayElements(source, 0);
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1703
    if (sourceBytes == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1704
        return -1;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1705
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1707
    int copyLen = sourceLength;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1708
    if (sourceLength > destinationLength) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1709
        // source might include an extra sign byte
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1710
        if (sourceLength == destinationLength + 1 && sourceBytes[0] == 0) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1711
            copyLen--;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1712
        } else {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1713
            return -1;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1714
        }
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1715
    }
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1716
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    // Copy bytes from the end of the source array to the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
    // destination array (until the destination array is full).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    // This ensures that the sign byte from the source array will be excluded.
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1720
    for (int i = 0; i < copyLen; i++) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1721
        destination[i] = sourceBytes[sourceLength - 1 - i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    }
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1723
    if (copyLen < destinationLength) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1724
        memset(destination + copyLen, 0, destinationLength - copyLen);
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1725
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1727
    env->ReleaseByteArrayElements(source, sourceBytes, JNI_ABORT);
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1728
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1729
    return destinationLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
 * The Microsoft Base Cryptographic Provider supports public-key BLOBs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
 * that have the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
 *     PUBLICKEYSTRUC publickeystruc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
 *     RSAPUBKEY rsapubkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
 *     BYTE modulus[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
 * and private-key BLOBs that have the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
 *     PUBLICKEYSTRUC publickeystruc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
 *     RSAPUBKEY rsapubkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
 *     BYTE modulus[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
 *     BYTE prime1[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
 *     BYTE prime2[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
 *     BYTE exponent1[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
 *     BYTE exponent2[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
 *     BYTE coefficient[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
 *     BYTE privateExponent[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
 * This method generates such BLOBs from the key elements supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
jbyteArray generateKeyBlob(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        jbyteArray jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        jbyteArray jPrivateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        jbyteArray jPrimeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        jbyteArray jPrimeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        jbyteArray jExponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        jbyteArray jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        jbyteArray jCrtCoefficient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    jsize jKeyByteLength = jKeyBitLength / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    jsize jBlobLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    BOOL bGeneratePrivateKeyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    // Determine whether to generate a public-key or a private-key BLOB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    if (jPrivateExponent != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        jPrimeP != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        jPrimeQ != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        jExponentP != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        jExponentQ != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        jCrtCoefficient != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        bGeneratePrivateKeyBlob = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        jBlobLength = sizeof(BLOBHEADER) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                        sizeof(RSAPUBKEY) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                        ((jKeyBitLength / 8) * 4) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                        (jKeyBitLength / 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        bGeneratePrivateKeyBlob = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        jBlobLength = sizeof(BLOBHEADER) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                        sizeof(RSAPUBKEY) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                        (jKeyBitLength / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    jbyte* jBlobBytes = new jbyte[jBlobLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    jbyte* jBlobElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    jbyteArray jBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    jsize  jElementLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        BLOBHEADER *pBlobHeader = (BLOBHEADER *) jBlobBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            pBlobHeader->bType = PRIVATEKEYBLOB;  // 0x07
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            pBlobHeader->bType = PUBLICKEYBLOB;   // 0x06
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        pBlobHeader->bVersion = CUR_BLOB_VERSION; // 0x02
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        pBlobHeader->reserved = 0;                // 0x0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        pBlobHeader->aiKeyAlg = CALG_RSA_KEYX;    // 0x0000a400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        RSAPUBKEY *pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            (RSAPUBKEY *) (jBlobBytes + sizeof(PUBLICKEYSTRUC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            pRsaPubKey->magic = 0x32415352;       // "RSA2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            pRsaPubKey->magic = 0x31415352;       // "RSA1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        pRsaPubKey->bitlen = jKeyBitLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        pRsaPubKey->pubexp = 0; // init
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        // Sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        jsize jPublicExponentLength = env->GetArrayLength(jPublicExponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        if (jPublicExponentLength > sizeof(pRsaPubKey->pubexp)) {
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
  1821
            ThrowException(env, INVALID_KEY_EXCEPTION, NTE_BAD_TYPE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        // The length argument must be the smaller of jPublicExponentLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        // and sizeof(pRsaPubKey->pubkey)
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1826
        if ((jElementLength = convertToLittleEndian(env, jPublicExponent,
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1827
            (jbyte *) &(pRsaPubKey->pubexp), jPublicExponentLength)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1828
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1829
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        // Modulus n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        jBlobElement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            (jbyte *) (jBlobBytes + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY));
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1834
        if ((jElementLength = convertToLittleEndian(env, jModulus, jBlobElement,
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1835
            jKeyByteLength)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1836
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1837
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            // Prime p
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1842
            if ((jElementLength = convertToLittleEndian(env, jPrimeP,
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1843
                jBlobElement, jKeyByteLength / 2)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1844
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1845
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            // Prime q
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1849
            if ((jElementLength = convertToLittleEndian(env, jPrimeQ,
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1850
                jBlobElement, jKeyByteLength / 2)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1851
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1852
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            // Prime exponent p
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1856
            if ((jElementLength = convertToLittleEndian(env, jExponentP,
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1857
                jBlobElement, jKeyByteLength / 2)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1858
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1859
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            // Prime exponent q
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1863
            if ((jElementLength = convertToLittleEndian(env, jExponentQ,
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1864
                jBlobElement, jKeyByteLength / 2)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1865
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1866
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            // CRT coefficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1870
            if ((jElementLength = convertToLittleEndian(env, jCrtCoefficient,
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1871
                jBlobElement, jKeyByteLength / 2)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1872
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1873
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            // Private exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1877
            if ((jElementLength = convertToLittleEndian(env, jPrivateExponent,
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1878
                jBlobElement, jKeyByteLength)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1879
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1880
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        jBlob = env->NewByteArray(jBlobLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        env->SetByteArrayRegion(jBlob, 0, jBlobLength, jBlobBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        if (jBlobBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            delete [] jBlobBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
    return jBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
 * Method:    generatePrivateKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
 * Signature: (I[B[B[B[B[B[B[B[B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_KeyStore_generatePrivateKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    (JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        jbyteArray jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        jbyteArray jPrivateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        jbyteArray jPrimeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        jbyteArray jPrimeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        jbyteArray jExponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        jbyteArray jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        jbyteArray jCrtCoefficient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        jPrivateExponent, jPrimeP, jPrimeQ, jExponentP, jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        jCrtCoefficient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
 * Class:     sun_security_mscapi_RSASignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
 * Method:    generatePublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
 * Signature: (I[B[B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSASignature_generatePublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    (JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        jbyteArray jPublicExponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
    return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        NULL, NULL, NULL, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
 * Method:    storePrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
 * Signature: ([BLjava/lang/String;I)Lsun/security/mscapi/RSAPrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_KeyStore_storePrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    (JNIEnv *env, jclass clazz, jbyteArray keyBlob, jstring keyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     jint keySize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    DWORD dwBlobLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    BYTE * pbKeyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    const char* pszKeyContainerName = NULL; // UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    jobject privateKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1951
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1952
            env->GetStringUTFChars(keyContainerName, NULL)) == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1953
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1954
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        dwBlobLen = env->GetArrayLength(keyBlob);
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1956
        if ((pbKeyBlob = (BYTE *) env->GetByteArrayElements(keyBlob, 0))
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1957
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1958
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1959
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        // Acquire a CSP context (create a new key container).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        // Import the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        if (::CryptImportKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            dwBlobLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            CRYPT_EXPORTABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            &hKey) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        // Get the method ID for the RSAPrivateKey constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        jclass clazzRSAPrivateKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            env->FindClass("sun/security/mscapi/RSAPrivateKey");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1989
        if (clazzRSAPrivateKey == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1990
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1991
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        jmethodID mNewRSAPrivateKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            env->GetMethodID(clazzRSAPrivateKey, "<init>", "(JJI)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1995
        if (mNewRSAPrivateKey == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1996
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1997
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        // Create a new RSA private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        privateKey = env->NewObject(clazzRSAPrivateKey, mNewRSAPrivateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            (jlong) hCryptProv, (jlong) hKey, keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            env->ReleaseByteArrayElements(keyBlob, (jbyte *) pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
 * Class:     sun_security_mscapi_RSASignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
 * Method:    importPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
 * Signature: ([BI)Lsun/security/mscapi/RSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_RSASignature_importPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
    (JNIEnv *env, jclass clazz, jbyteArray keyBlob, jint keySize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    DWORD dwBlobLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    BYTE * pbKeyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
    jobject publicKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        dwBlobLen = env->GetArrayLength(keyBlob);
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2037
        if ((pbKeyBlob = (BYTE *) env->GetByteArrayElements(keyBlob, 0))
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2038
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2039
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2040
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        // Acquire a CSP context (create a new key container).
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2043
        // Prefer a PROV_RSA_AES CSP, when available, due to its support
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2044
        // for SHA-2-based signatures.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            NULL,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2049
            PROV_RSA_AES,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            CRYPT_VERIFYCONTEXT) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2052
            // Failover to using the default CSP (PROV_RSA_FULL)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2053
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2054
            if (::CryptAcquireContext(
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2055
                &hCryptProv,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2056
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2057
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2058
                PROV_RSA_FULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2059
                CRYPT_VERIFYCONTEXT) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2060
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2061
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2062
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2063
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        // Import the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        if (::CryptImportKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
            hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
            pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            dwBlobLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            CRYPT_EXPORTABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            &hKey) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        // Get the method ID for the RSAPublicKey constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        jclass clazzRSAPublicKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            env->FindClass("sun/security/mscapi/RSAPublicKey");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2082
        if (clazzRSAPublicKey == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2083
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2084
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        jmethodID mNewRSAPublicKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            env->GetMethodID(clazzRSAPublicKey, "<init>", "(JJI)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2088
        if (mNewRSAPublicKey == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2089
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2090
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        // Create a new RSA public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        publicKey = env->NewObject(clazzRSAPublicKey, mNewRSAPublicKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            (jlong) hCryptProv, (jlong) hKey, keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            env->ReleaseByteArrayElements(keyBlob, (jbyte *) pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
    return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
} /* extern "C" */