jdk/src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp
author avstepan
Thu, 28 Apr 2016 19:35:09 +0300
changeset 38386 d7e7d592d396
parent 36759 07dc1868fd1e
child 40414 1f4a9da41c1f
permissions -rw-r--r--
8155021: [TEST] create one more inheritance test for @BeanProperty Reviewed-by: serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
     2
 * Copyright (c) 2005, 2016, 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
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   275
  (JNIEnv *env, jobject obj, jstring jCertStoreName)
2
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",
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   334
                                                   "(Ljava/lang/String;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",
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   342
                                                   "(Ljava/lang/String;JJILjava/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
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   369
                BOOL bGetUserKey = ::CryptGetUserKey(hCryptProv, dwKeySpec, &hUserKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   371
                // Skip certificate if cannot find private key
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   372
                if (bGetUserKey == FALSE)
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   373
                {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   374
                    if (bCallerFreeProv)
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   375
                        ::CryptReleaseContext(hCryptProv, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   377
                    continue;
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   378
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   380
                // Set cipher mode to ECB
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   381
                DWORD dwCipherMode = CRYPT_MODE_ECB;
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   382
                ::CryptSetKeyParam(hUserKey, KP_MODE, (BYTE*)&dwCipherMode, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   385
                // If the private key is present in smart card, we may not be able to
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   386
                // determine the key length by using the private key handle. However,
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   387
                // since public/private key pairs must have the same length, we could
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   388
                // determine the key length of the private key by using the public key
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   389
                // in the certificate.
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   390
                dwPublicKeyLength = ::CertGetPublicKeyLength(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   391
                        &(pCertContext->pCertInfo->SubjectPublicKeyInfo));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   393
            }
2
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
            //
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   399
            if (GetCertificateChain(OID_EKU_ANY, pCertContext, &pCertChainContext))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                for (unsigned int i=0; i < pCertChainContext->cChain; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    // Found cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    PCERT_SIMPLE_CHAIN rgpChain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        pCertChainContext->rgpChain[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    // Create ArrayList to store certs in each chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    jobject jArrayList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        env->NewObject(clazzArrayList, mNewArrayList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    for (unsigned int j=0; j < rgpChain->cElement; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        PCERT_CHAIN_ELEMENT rgpElement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                            rgpChain->rgpElement[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        PCCERT_CONTEXT pc = rgpElement->pCertContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        // Retrieve the friendly name of the first certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        // in the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        if (j == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                            // If the cert's name cannot be retrieved then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                            // pszNameString remains set to NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                            // (An alias name will be generated automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            // when storing this cert in the keystore.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            // Get length of friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                            if ((cchNameString = CertGetNameString(pc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                                NULL, 0)) > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                // Found friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                pszNameString = new char[cchNameString];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                CertGetNameString(pc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                    CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                                    pszNameString, cchNameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        BYTE* pbCertEncoded = pc->pbCertEncoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        DWORD cbCertEncoded = pc->cbCertEncoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        // Allocate and populate byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        jbyteArray byteArray = env->NewByteArray(cbCertEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        env->SetByteArrayRegion(byteArray, 0, cbCertEncoded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                            (jbyte*) pbCertEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        // Generate certificate from byte array and store into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        // cert collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        env->CallVoidMethod(obj, mGenCert, byteArray, jArrayList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    if (bHasNoPrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        // Generate certificate chain and store into cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        // collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        env->CallVoidMethod(obj, mGenCertChain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            env->NewStringUTF(pszNameString),
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   458
                            jArrayList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    {
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   462
                        // Determine key type: RSA or DSA
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   463
                        DWORD dwData = CALG_RSA_KEYX;
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   464
                        DWORD dwSize = sizeof(DWORD);
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   465
                        ::CryptGetKeyParam(hUserKey, KP_ALGID, (BYTE*)&dwData,
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   466
                                &dwSize, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   468
                        if ((dwData & ALG_TYPE_RSA) == ALG_TYPE_RSA)
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   469
                        {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   470
                            // Generate RSA certificate chain and store into cert
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   471
                            // chain collection
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   472
                            env->CallVoidMethod(obj, mGenRSAKeyAndCertChain,
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   473
                                    env->NewStringUTF(pszNameString),
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   474
                                    (jlong) hCryptProv, (jlong) hUserKey,
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   475
                                    dwPublicKeyLength, jArrayList);
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   476
                        }
2
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
                // Free cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                if (pCertChainContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    ::CertFreeCertificateChain(pCertChainContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (pszNameString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
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
 * Class:     sun_security_mscapi_Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
 * Method:    cleanUp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
 * Signature: (JJ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
JNIEXPORT void JNICALL Java_sun_security_mscapi_Key_cleanUp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
  (JNIEnv *env, jclass clazz, jlong hCryptProv, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    if (hCryptKey != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        ::CryptDestroyKey((HCRYPTKEY) hCryptKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    if (hCryptProv != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        ::CryptReleaseContext((HCRYPTPROV) hCryptProv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
}
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
 * Class:     sun_security_mscapi_RSASignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
 * Method:    signHash
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   519
 * Signature: (Z[BILjava/lang/String;JJ)[B
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
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
   522
  (JNIEnv *env, jclass clazz, jboolean noHashOID, jbyteArray jHash,
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   523
        jint jHashSize, jstring jHashAlgorithm, jlong hCryptProv,
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   524
        jlong hCryptKey)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    HCRYPTHASH hHash = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    jbyte* pHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    jbyte* pSignedHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    jbyteArray jSignedHash = NULL;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   530
    HCRYPTPROV hCryptProvAlt = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        // Map hash algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        ALG_ID algId = MapHashAlgorithm(env, jHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        // Acquire a hash object handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (::CryptCreateHash(HCRYPTPROV(hCryptProv), algId, 0, 0, &hHash) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   540
            // Failover to using the PROV_RSA_AES CSP
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   541
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   542
            DWORD cbData = 256;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   543
            BYTE pbData[256];
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   544
            pbData[0] = '\0';
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   545
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   546
            // Get name of the key container
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   547
            ::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_CONTAINER,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   548
                (BYTE *)pbData, &cbData, 0);
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   549
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   550
            // Acquire an alternative CSP handle
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   551
            if (::CryptAcquireContext(&hCryptProvAlt, LPCSTR(pbData), NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   552
                PROV_RSA_AES, 0) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   553
            {
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
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   556
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   557
            }
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
            // Acquire a hash object handle.
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   560
            if (::CryptCreateHash(HCRYPTPROV(hCryptProvAlt), algId, 0, 0,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   561
                &hHash) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   562
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   563
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   564
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   565
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // Copy hash from Java to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        pHashBuffer = new jbyte[jHashSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        // Set hash value in the hash object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*)pHashBuffer, NULL) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // Determine key spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        DWORD dwKeySpec = AT_SIGNATURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        ALG_ID dwAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        DWORD dwAlgIdLen = sizeof(ALG_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (! ::CryptGetKeyParam((HCRYPTKEY) hCryptKey, KP_ALGID, (BYTE*)&dwAlgId, &dwAlgIdLen, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (CALG_RSA_KEYX == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            dwKeySpec = AT_KEYEXCHANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // Determine size of buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        DWORD dwBufLen = 0;
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   595
        DWORD dwFlags = 0;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   596
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   597
        if (noHashOID == JNI_TRUE) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   598
            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
   599
        }
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
        if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, NULL, &dwBufLen) == FALSE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        pSignedHashBuffer = new jbyte[dwBufLen];
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   608
        if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, (BYTE*)pSignedHashBuffer, &dwBufLen) == FALSE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        // Create new byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        jbyteArray temp = env->NewByteArray(dwBufLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // Copy data from native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        env->SetByteArrayRegion(temp, 0, dwBufLen, pSignedHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        jSignedHash = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (pSignedHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            delete [] pSignedHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        if (pHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            delete [] pHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (hHash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            ::CryptDestroyHash(hHash);
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   632
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   633
        if (hCryptProvAlt)
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   634
            ::CryptReleaseContext(hCryptProvAlt, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    return jSignedHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
 * Class:     sun_security_mscapi_RSASignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
 * Method:    verifySignedHash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
 * Signature: ([BIL/java/lang/String;[BIJJ)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
JNIEXPORT jboolean JNICALL Java_sun_security_mscapi_RSASignature_verifySignedHash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
  (JNIEnv *env, jclass clazz, jbyteArray jHash, jint jHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        jstring jHashAlgorithm, jbyteArray jSignedHash, jint jSignedHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        jlong hCryptProv, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    HCRYPTHASH hHash = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    jbyte* pHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    jbyte* pSignedHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    DWORD dwSignedHashBufferLen = jSignedHashSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    jboolean result = JNI_FALSE;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   655
    HCRYPTPROV hCryptProvAlt = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        // Map hash algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        ALG_ID algId = MapHashAlgorithm(env, jHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        // Acquire a hash object handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (::CryptCreateHash(HCRYPTPROV(hCryptProv), algId, 0, 0, &hHash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   666
            // Failover to using the PROV_RSA_AES CSP
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   667
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   668
            DWORD cbData = 256;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   669
            BYTE pbData[256];
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   670
            pbData[0] = '\0';
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   671
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   672
            // Get name of the key container
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   673
            ::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_CONTAINER,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   674
                (BYTE *)pbData, &cbData, 0);
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   675
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   676
            // Acquire an alternative CSP handle
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   677
            if (::CryptAcquireContext(&hCryptProvAlt, LPCSTR(pbData), NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   678
                PROV_RSA_AES, 0) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   679
            {
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
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   682
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   683
            }
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
            // Acquire a hash object handle.
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   686
            if (::CryptCreateHash(HCRYPTPROV(hCryptProvAlt), algId, 0, 0,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   687
                &hHash) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   688
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   689
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   690
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   691
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        // Copy hash and signedHash from Java to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        pHashBuffer = new jbyte[jHashSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        pSignedHashBuffer = new jbyte[jSignedHashSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        env->GetByteArrayRegion(jSignedHash, 0, jSignedHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            pSignedHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        // Set hash value in the hash object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*) pHashBuffer, NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // For RSA, the hash encryption algorithm is normally the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        // public key algorithm, so AT_SIGNATURE is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        // Verify the signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (::CryptVerifySignatureA(hHash, (BYTE *) pSignedHashBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            dwSignedHashBufferLen, (HCRYPTKEY) hCryptKey, NULL, 0) == TRUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            result = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (pSignedHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            delete [] pSignedHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (pHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            delete [] pHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (hHash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            ::CryptDestroyHash(hHash);
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   730
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   731
        if (hCryptProvAlt)
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   732
            ::CryptReleaseContext(hCryptProvAlt, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
 * Class:     sun_security_mscapi_RSAKeyPairGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
 * Method:    generateRSAKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
 * Signature: (ILjava/lang/String;)Lsun/security/mscapi/RSAKeyPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_RSAKeyPairGenerator_generateRSAKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
  (JNIEnv *env, jclass clazz, jint keySize, jstring keyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    HCRYPTKEY hKeyPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    DWORD dwFlags = (keySize << 16) | CRYPT_EXPORTABLE;
9674
5d8476e6e47a 6987652: VM crashed in sun.security.mscapi.RSAKeyPairGenerator.generateRSAKeyPair(...)
vinnie
parents: 9533
diff changeset
   749
    jobject keypair = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    const char* pszKeyContainerName = NULL; // UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   754
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   755
            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
   756
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   757
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        // Acquire a CSP context (create a new key container).
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   760
        // 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
   761
        // for SHA-2-based signatures.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            NULL,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   766
            PROV_RSA_AES,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   769
            // Failover to using the default CSP (PROV_RSA_FULL)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   770
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   771
            if (::CryptAcquireContext(
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   772
                &hCryptProv,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   773
                pszKeyContainerName,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   774
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   775
                PROV_RSA_FULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   776
                CRYPT_NEWKEYSET) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   777
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   778
                ThrowException(env, KEY_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   779
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   780
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        // Generate an RSA keypair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        if(::CryptGenKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
           hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
           AT_KEYEXCHANGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
           dwFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
           &hKeyPair) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        // Get the method ID for the RSAKeyPair constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        jclass clazzRSAKeyPair =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            env->FindClass("sun/security/mscapi/RSAKeyPair");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   797
        if (clazzRSAKeyPair == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   798
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   799
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        jmethodID mNewRSAKeyPair =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            env->GetMethodID(clazzRSAKeyPair, "<init>", "(JJI)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   803
        if (mNewRSAKeyPair == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   804
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   805
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        // Create a new RSA keypair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        keypair = env->NewObject(clazzRSAKeyPair, mNewRSAKeyPair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            (jlong) hCryptProv, (jlong) hKeyPair, keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    return keypair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
 * Class:     sun_security_mscapi_Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
 * Method:    getContainerName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
 * Signature: (J)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
JNIEXPORT jstring JNICALL Java_sun_security_mscapi_Key_getContainerName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
  (JNIEnv *env, jclass jclazz, jlong hCryptProv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    DWORD cbData = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    BYTE pbData[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    pbData[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        (HCRYPTPROV)hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        (BYTE *)pbData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        &cbData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    return env->NewStringUTF((const char*)pbData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
 * Class:     sun_security_mscapi_Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
 * Method:    getKeyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
 * Signature: (J)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
JNIEXPORT jstring JNICALL Java_sun_security_mscapi_Key_getKeyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
  (JNIEnv *env, jclass jclazz, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    ALG_ID dwAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    DWORD dwAlgIdLen = sizeof(ALG_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    if (::CryptGetKeyParam((HCRYPTKEY) hCryptKey, KP_ALGID, (BYTE*)&dwAlgId, &dwAlgIdLen, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        if (CALG_RSA_SIGN == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            return env->NewStringUTF("Signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        } else if (CALG_RSA_KEYX == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            return env->NewStringUTF("Exchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            char buffer[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            if (sprintf(buffer, "%lu", dwAlgId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                return env->NewStringUTF(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
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
    return env->NewStringUTF("<Unknown>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
 * Method:    storeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
 * Signature: (Ljava/lang/String;Ljava/lang/String;[BIJJ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
JNIEXPORT void JNICALL Java_sun_security_mscapi_KeyStore_storeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        jbyteArray jCertEncoding, jint jCertEncodingSize, jlong hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    PWCHAR pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    jbyte* pbCertEncoding = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    const jchar* jCertAliasChars = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    const char* pszContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    const char* pszProviderName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    WCHAR * pwszContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    WCHAR * pwszProviderName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        // 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
   900
        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
   901
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   902
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   903
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        // Copy encoding from Java to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        pbCertEncoding = new jbyte[jCertEncodingSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        // Create a certificate context from the encoded cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            (BYTE*) pbCertEncoding, jCertEncodingSize))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            ThrowException(env, CERTIFICATE_PARSING_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        // Set the certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        int size = env->GetStringLength(jCertAliasName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        pszCertAliasName = new WCHAR[size + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        jCertAliasChars = env->GetStringChars(jCertAliasName, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        memcpy(pszCertAliasName, jCertAliasChars, size * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        pszCertAliasName[size] = 0; // append the string terminator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        CRYPT_DATA_BLOB friendlyName = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            sizeof(WCHAR) * (size + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            (BYTE *) pszCertAliasName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        env->ReleaseStringChars(jCertAliasName, jCertAliasChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        if (! ::CertSetCertificateContextProperty(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            CERT_FRIENDLY_NAME_PROP_ID, 0, &friendlyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        // Attach the certificate's private key (if supplied)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        if (hCryptProv != 0 && hCryptKey != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            CRYPT_KEY_PROV_INFO keyProviderInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            DWORD dwDataLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            // Get the name of the key container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            pszContainerName = new char[dwDataLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                (BYTE *) pszContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            // Convert to a wide char string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            pwszContainerName = new WCHAR[dwDataLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if (mbstowcs(pwszContainerName, pszContainerName, dwDataLen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            // Set the name of the key container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            keyProviderInfo.pwszContainerName = pwszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            // Get the name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                PP_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            pszProviderName = new char[dwDataLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                PP_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                (BYTE *) pszProviderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            // Convert to a wide char string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            pwszProviderName = new WCHAR[dwDataLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (mbstowcs(pwszProviderName, pszProviderName, dwDataLen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            // Set the name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            keyProviderInfo.pwszProvName = pwszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            // Get and set the type of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            if (! ::CryptGetProvParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                PP_PROVTYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                (LPBYTE) &keyProviderInfo.dwProvType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            // Set no provider flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            keyProviderInfo.dwFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            // Set no provider parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            keyProviderInfo.cProvParam = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            keyProviderInfo.rgProvParam = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            // Get the key's algorithm ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            if (! ::CryptGetKeyParam(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                (HCRYPTKEY) hCryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                KP_ALGID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                (LPBYTE) &keyProviderInfo.dwKeySpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            // Set the key spec (using the algorithm ID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            switch (keyProviderInfo.dwKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            case CALG_RSA_KEYX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            case CALG_DH_SF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                keyProviderInfo.dwKeySpec = AT_KEYEXCHANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            case CALG_RSA_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            case CALG_DSS_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                keyProviderInfo.dwKeySpec = AT_SIGNATURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_ALGID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            if (! ::CertSetCertificateContextProperty(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                CERT_KEY_PROV_INFO_PROP_ID, 0, &keyProviderInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        // Import encoded certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        if (!::CertAddCertificateContextToStore(hCertStore, pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            CERT_STORE_ADD_REPLACE_EXISTING, NULL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        if (pbCertEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            delete [] pbCertEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            delete [] pszCertAliasName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        if (pszContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            delete [] pszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if (pwszContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            delete [] pwszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        if (pszProviderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            delete [] pszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        if (pwszProviderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            delete [] pwszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (pCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            ::CertFreeCertificateContext(pCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    }
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
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
 * Method:    removeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
 * Signature: (Ljava/lang/String;Ljava/lang/String;[BI)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
JNIEXPORT void JNICALL Java_sun_security_mscapi_KeyStore_removeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
  jbyteArray jCertEncoding, jint jCertEncodingSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    const char* pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    PCCERT_CONTEXT pTBDCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    jbyte* pbCertEncoding = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    DWORD cchNameString = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    char* pszNameString = NULL; // certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    BOOL bDeleteAttempted = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        // 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
  1142
        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
  1143
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1144
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1145
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        // Copy encoding from Java to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        pbCertEncoding = new jbyte[jCertEncodingSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        // Create a certificate context from the encoded cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            (BYTE*) pbCertEncoding, jCertEncodingSize))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            ThrowException(env, CERTIFICATE_PARSING_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        // Find the certificate to be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        if (!(pTBDCertContext = ::CertFindCertificateInStore(hCertStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            X509_ASN_ENCODING, 0, CERT_FIND_EXISTING, pCertContext, NULL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        // Check that its friendly name matches the supplied alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        if ((cchNameString = ::CertGetNameString(pTBDCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            pszNameString = new char[cchNameString];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            ::CertGetNameString(pTBDCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                cchNameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            // 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
  1182
            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
  1183
                == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1184
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1185
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            if (strcmp(pszCertAliasName, pszNameString) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                // Only delete the certificate if the alias names matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                if (! ::CertDeleteCertificateFromStore(pTBDCertContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                    // pTBDCertContext is always freed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    //  CertDeleteCertificateFromStore method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    bDeleteAttempted = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                }
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
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            env->ReleaseStringUTFChars(jCertAliasName, pszCertAliasName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        if (pbCertEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            delete [] pbCertEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        if (pszNameString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        if (pCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            ::CertFreeCertificateContext(pCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        if (bDeleteAttempted && pTBDCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            ::CertFreeCertificateContext(pTBDCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    }
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
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
 * Method:    destroyKeyContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
 * Signature: (Ljava/lang/String;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
JNIEXPORT void JNICALL Java_sun_security_mscapi_KeyStore_destroyKeyContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
  (JNIEnv *env, jclass clazz, jstring keyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    const char* pszKeyContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1243
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1244
            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
  1245
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1246
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        // Destroying the default key container is not permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        // (because it may contain more one keypair).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        if (pszKeyContainerName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_KEYSET_PARAM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        // Acquire a CSP context (to the key container).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            CRYPT_DELETEKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    }
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
 * Class:     sun_security_mscapi_RSACipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
 * Method:    findCertificateUsingAlias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
JNIEXPORT jlong JNICALL Java_sun_security_mscapi_RSACipher_findCertificateUsingAlias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    const char* pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    char* pszNameString = NULL; // certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    DWORD cchNameString = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1299
        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
  1300
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1301
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1302
        }
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1303
        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
  1304
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1305
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1306
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        // Open a system certificate store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        // Use CertEnumCertificatesInStore to get the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        // from the open store. pCertContext must be reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        // NULL to retrieve the first certificate in the store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        while (pCertContext = ::CertEnumCertificatesInStore(hCertStore, pCertContext))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            if ((cchNameString = ::CertGetNameString(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                continue; // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            pszNameString = new char[cchNameString];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            if (::CertGetNameString(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                cchNameString) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                continue; // not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            // Compare the certificate's friendly name with supplied alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            if (strcmp(pszCertAliasName, pszNameString) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            env->ReleaseStringUTFChars(jCertAliasName, pszCertAliasName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    return (jlong) pCertContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
 * Class:     sun_security_mscapi_RSACipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
 * Method:    getKeyFromCert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
 * Signature: (JZ)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
JNIEXPORT jlong JNICALL Java_sun_security_mscapi_RSACipher_getKeyFromCert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
  (JNIEnv *env, jobject obj, jlong pCertContext, jboolean usePrivateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    DWORD dwKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        if (usePrivateKey == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            // Locate the key container for the certificate's private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            if (!(::CryptAcquireCertificatePrivateKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                (PCCERT_CONTEXT) pCertContext, 0, NULL, &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                &dwKeySpec, NULL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            // Get a handle to the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            if (!(::CryptGetUserKey(hCryptProv, dwKeySpec, &hKey))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        } else { // use public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            //  Acquire a CSP context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            if(::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
               &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
               "J2SE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
               NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
               PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
               0) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                // If CSP context hasn't been created, create one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                    &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    "J2SE",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                    CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                    ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                    __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            // Import the certificate's public key into the key container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            if (!(::CryptImportPublicKeyInfo(hCryptProv, X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                &(((PCCERT_CONTEXT) pCertContext)->pCertInfo->SubjectPublicKeyInfo),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                &hKey))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        if (hCryptProv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            ::CryptReleaseContext(hCryptProv, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    return hKey;        // TODO - when finished with this key, call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                        //              CryptDestroyKey(hKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
 * Method:    getKeyLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
 * Signature: (J)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
JNIEXPORT jint JNICALL Java_sun_security_mscapi_KeyStore_getKeyLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
  (JNIEnv *env, jobject obj, jlong hKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    DWORD dwDataLen = sizeof(DWORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    BYTE pbData[sizeof(DWORD)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    DWORD length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        // Get key length (in bits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        //TODO - may need to use KP_BLOCKLEN instead?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        if (!(::CryptGetKeyParam((HCRYPTKEY) hKey, KP_KEYLEN, (BYTE *)pbData, &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        length = (DWORD) pbData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        // no cleanup required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    return (jint) length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
 * Class:     sun_security_mscapi_RSACipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
 * Method:    encryptDecrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
 * Signature: ([BIJZ)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSACipher_encryptDecrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
  (JNIEnv *env, jclass clazz, jbyteArray jData, jint jDataSize, jlong hKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
   jboolean doEncrypt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    jbyteArray result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    jbyte* pData = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    DWORD dwDataLen = jDataSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    DWORD dwBufLen = env->GetArrayLength(jData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    DWORD i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    BYTE tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        // Copy data from Java buffer to native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        pData = new jbyte[dwBufLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        env->GetByteArrayRegion(jData, 0, dwBufLen, pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        if (doEncrypt == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            // encrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            if (! ::CryptEncrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                &dwDataLen, dwBufLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            dwBufLen = dwDataLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            // convert from little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            for (i = 0; i < dwBufLen / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                tmp = pData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                pData[i] = pData[dwBufLen - i -1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                pData[dwBufLen - i - 1] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            // convert to little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            for (i = 0; i < dwBufLen / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                tmp = pData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                pData[i] = pData[dwBufLen - i -1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                pData[dwBufLen - i - 1] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            // decrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            if (! ::CryptDecrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                &dwBufLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        // Create new byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        result = env->NewByteArray(dwBufLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        // Copy data from native buffer to Java buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        env->SetByteArrayRegion(result, 0, dwBufLen, (jbyte*) pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        if (pData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            delete [] pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
 * Class:     sun_security_mscapi_RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
 * Method:    getPublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
 * Signature: (J)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getPublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    (JNIEnv *env, jclass clazz, jlong hCryptKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    jbyteArray blob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    DWORD dwBlobLen;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
  1548
    BYTE* pbKeyBlob = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        // Determine the size of the blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            &dwBlobLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        pbKeyBlob = new BYTE[dwBlobLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        // Generate key blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            pbKeyBlob, &dwBlobLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        // Create new byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        blob = env->NewByteArray(dwBlobLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        // Copy data from native buffer to Java buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        env->SetByteArrayRegion(blob, 0, dwBlobLen, (jbyte*) pbKeyBlob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            delete [] pbKeyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    return blob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
 * Class:     sun_security_mscapi_RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
 * Method:    getExponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
 * Signature: ([B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getExponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    (JNIEnv *env, jclass clazz, jbyteArray jKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    jbyteArray exponent = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    jbyte*     exponentBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    jbyte*     keyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        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
  1601
        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
  1602
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1603
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        // Check BLOB type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        RSAPUBKEY* pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        int len = sizeof(pRsaPubKey->pubexp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        exponentBytes = new jbyte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        // convert from little-endian while copying from blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        for (int i = 0, j = len - 1; i < len; i++, j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            exponentBytes[i] = ((BYTE*) &pRsaPubKey->pubexp)[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        exponent = env->NewByteArray(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        env->SetByteArrayRegion(exponent, 0, len, exponentBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        if (keyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        if (exponentBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            delete [] exponentBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
 * Class:     sun_security_mscapi_RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
 * Method:    getModulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
 * Signature: ([B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSAPublicKey_getModulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    (JNIEnv *env, jclass clazz, jbyteArray jKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
    jbyteArray modulus = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
    jbyte*     modulusBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    jbyte*     keyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        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
  1653
        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
  1654
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1655
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        // Check BLOB type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        RSAPUBKEY* pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        int len = pRsaPubKey->bitlen / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        modulusBytes = new jbyte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        BYTE * pbModulus =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            (BYTE *) (keyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        // convert from little-endian while copying from blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        for (int i = 0, j = len - 1; i < len; i++, j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            modulusBytes[i] = pbModulus[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        modulus = env->NewByteArray(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        env->SetByteArrayRegion(modulus, 0, len, modulusBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        if (keyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        if (modulusBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            delete [] modulusBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    return modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
 * Convert an array in big-endian byte order into little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
int convertToLittleEndian(JNIEnv *env, jbyteArray source, jbyte* destination,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    int destinationLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    int sourceLength = env->GetArrayLength(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    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
  1702
    if (sourceBytes == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1703
        return -1;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1704
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1706
    int copyLen = sourceLength;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1707
    if (sourceLength > destinationLength) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1708
        // source might include an extra sign byte
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1709
        if (sourceLength == destinationLength + 1 && sourceBytes[0] == 0) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1710
            copyLen--;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1711
        } else {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1712
            return -1;
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1713
        }
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
    // Copy bytes from the end of the source array to the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    // destination array (until the destination array is full).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
    // 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
  1719
    for (int i = 0; i < copyLen; i++) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1720
        destination[i] = sourceBytes[sourceLength - 1 - i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    }
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1722
    if (copyLen < destinationLength) {
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1723
        memset(destination + copyLen, 0, destinationLength - copyLen);
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1724
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1726
    env->ReleaseByteArrayElements(source, sourceBytes, JNI_ABORT);
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1727
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  1728
    return destinationLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
 * The Microsoft Base Cryptographic Provider supports public-key BLOBs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
 * that have the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
 *     PUBLICKEYSTRUC publickeystruc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
 *     RSAPUBKEY rsapubkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
 *     BYTE modulus[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
 * and private-key BLOBs that have the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
 *     PUBLICKEYSTRUC publickeystruc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
 *     RSAPUBKEY rsapubkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
 *     BYTE modulus[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
 *     BYTE prime1[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
 *     BYTE prime2[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
 *     BYTE exponent1[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
 *     BYTE exponent2[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
 *     BYTE coefficient[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
 *     BYTE privateExponent[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
 * This method generates such BLOBs from the key elements supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
jbyteArray generateKeyBlob(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        jbyteArray jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        jbyteArray jPrivateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        jbyteArray jPrimeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        jbyteArray jPrimeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        jbyteArray jExponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        jbyteArray jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        jbyteArray jCrtCoefficient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    jsize jKeyByteLength = jKeyBitLength / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    jsize jBlobLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    BOOL bGeneratePrivateKeyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    // Determine whether to generate a public-key or a private-key BLOB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    if (jPrivateExponent != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        jPrimeP != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        jPrimeQ != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        jExponentP != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        jExponentQ != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        jCrtCoefficient != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        bGeneratePrivateKeyBlob = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        jBlobLength = sizeof(BLOBHEADER) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                        sizeof(RSAPUBKEY) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                        ((jKeyBitLength / 8) * 4) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                        (jKeyBitLength / 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        bGeneratePrivateKeyBlob = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        jBlobLength = sizeof(BLOBHEADER) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                        sizeof(RSAPUBKEY) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                        (jKeyBitLength / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    jbyte* jBlobBytes = new jbyte[jBlobLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    jbyte* jBlobElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    jbyteArray jBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    jsize  jElementLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        BLOBHEADER *pBlobHeader = (BLOBHEADER *) jBlobBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            pBlobHeader->bType = PRIVATEKEYBLOB;  // 0x07
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            pBlobHeader->bType = PUBLICKEYBLOB;   // 0x06
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        pBlobHeader->bVersion = CUR_BLOB_VERSION; // 0x02
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        pBlobHeader->reserved = 0;                // 0x0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        pBlobHeader->aiKeyAlg = CALG_RSA_KEYX;    // 0x0000a400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        RSAPUBKEY *pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            (RSAPUBKEY *) (jBlobBytes + sizeof(PUBLICKEYSTRUC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            pRsaPubKey->magic = 0x32415352;       // "RSA2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            pRsaPubKey->magic = 0x31415352;       // "RSA1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        pRsaPubKey->bitlen = jKeyBitLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        pRsaPubKey->pubexp = 0; // init
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        // Sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        jsize jPublicExponentLength = env->GetArrayLength(jPublicExponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        if (jPublicExponentLength > sizeof(pRsaPubKey->pubexp)) {
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
  1820
            ThrowException(env, INVALID_KEY_EXCEPTION, NTE_BAD_TYPE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        // The length argument must be the smaller of jPublicExponentLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        // 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
  1825
        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
  1826
            (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
  1827
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1828
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        // Modulus n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        jBlobElement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            (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
  1833
        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
  1834
            jKeyByteLength)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1835
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1836
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            // Prime p
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1841
            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
  1842
                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
  1843
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1844
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            // Prime q
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1848
            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
  1849
                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
  1850
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1851
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            // Prime exponent p
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1855
            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
  1856
                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
  1857
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1858
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            // Prime exponent q
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1862
            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
  1863
                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
  1864
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1865
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            // CRT coefficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1869
            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
  1870
                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
  1871
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1872
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            // Private exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1876
            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
  1877
                jBlobElement, jKeyByteLength)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1878
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1879
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        jBlob = env->NewByteArray(jBlobLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        env->SetByteArrayRegion(jBlob, 0, jBlobLength, jBlobBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        if (jBlobBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            delete [] jBlobBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    return jBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
 * Method:    generatePrivateKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
 * Signature: (I[B[B[B[B[B[B[B[B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_KeyStore_generatePrivateKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    (JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        jbyteArray jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        jbyteArray jPrivateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        jbyteArray jPrimeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        jbyteArray jPrimeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        jbyteArray jExponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        jbyteArray jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        jbyteArray jCrtCoefficient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        jPrivateExponent, jPrimeP, jPrimeQ, jExponentP, jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        jCrtCoefficient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
 * Class:     sun_security_mscapi_RSASignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
 * Method:    generatePublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
 * Signature: (I[B[B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSASignature_generatePublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    (JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        jbyteArray jPublicExponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        NULL, NULL, NULL, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
 * Class:     sun_security_mscapi_KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
 * Method:    storePrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
 * Signature: ([BLjava/lang/String;I)Lsun/security/mscapi/RSAPrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_KeyStore_storePrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
    (JNIEnv *env, jclass clazz, jbyteArray keyBlob, jstring keyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     jint keySize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    DWORD dwBlobLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    BYTE * pbKeyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    const char* pszKeyContainerName = NULL; // UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    jobject privateKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1950
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1951
            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
  1952
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1953
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        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
  1955
        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
  1956
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1957
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1958
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        // Acquire a CSP context (create a new key container).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        // Import the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        if (::CryptImportKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            dwBlobLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            CRYPT_EXPORTABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            &hKey) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        // Get the method ID for the RSAPrivateKey constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        jclass clazzRSAPrivateKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            env->FindClass("sun/security/mscapi/RSAPrivateKey");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1988
        if (clazzRSAPrivateKey == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1989
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1990
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        jmethodID mNewRSAPrivateKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            env->GetMethodID(clazzRSAPrivateKey, "<init>", "(JJI)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1994
        if (mNewRSAPrivateKey == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1995
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1996
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        // Create a new RSA private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        privateKey = env->NewObject(clazzRSAPrivateKey, mNewRSAPrivateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            (jlong) hCryptProv, (jlong) hKey, keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
            env->ReleaseByteArrayElements(keyBlob, (jbyte *) pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
    return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
 * Class:     sun_security_mscapi_RSASignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
 * Method:    importPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
 * Signature: ([BI)Lsun/security/mscapi/RSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_RSASignature_importPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    (JNIEnv *env, jclass clazz, jbyteArray keyBlob, jint keySize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    DWORD dwBlobLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    BYTE * pbKeyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    jobject publicKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        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
  2036
        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
  2037
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2038
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2039
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        // Acquire a CSP context (create a new key container).
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2042
        // 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
  2043
        // for SHA-2-based signatures.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        if (::CryptAcquireContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            NULL,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2048
            PROV_RSA_AES,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            CRYPT_VERIFYCONTEXT) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2051
            // Failover to using the default CSP (PROV_RSA_FULL)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2052
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2053
            if (::CryptAcquireContext(
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2054
                &hCryptProv,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2055
                NULL,
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
                PROV_RSA_FULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2058
                CRYPT_VERIFYCONTEXT) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2059
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2060
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2061
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2062
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        // Import the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        if (::CryptImportKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
            hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
            pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
            dwBlobLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            CRYPT_EXPORTABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            &hKey) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        // Get the method ID for the RSAPublicKey constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        jclass clazzRSAPublicKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            env->FindClass("sun/security/mscapi/RSAPublicKey");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2081
        if (clazzRSAPublicKey == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2082
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2083
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        jmethodID mNewRSAPublicKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            env->GetMethodID(clazzRSAPublicKey, "<init>", "(JJI)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2087
        if (mNewRSAPublicKey == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2088
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2089
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        // Create a new RSA public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        publicKey = env->NewObject(clazzRSAPublicKey, mNewRSAPublicKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            (jlong) hCryptProv, (jlong) hKey, keySize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
            env->ReleaseByteArrayElements(keyBlob, (jbyte *) pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
    return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
} /* extern "C" */