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