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