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