src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp
author henryjen
Thu, 13 Dec 2018 11:47:35 -0800
changeset 53333 fd6de53a0d6e
parent 53330 e8bae92beee3
parent 53007 e2798bf6318a
child 54287 c02b8d6384ab
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
//=--------------------------------------------------------------------------=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
// security.cpp    by Stanley Man-Kit Ho
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
//=--------------------------------------------------------------------------=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <jni.h>
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    32
#include "jni_util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdlib.h>
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
    34
#include <string.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <BaseTsd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <wincrypt.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <stdio.h>
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
    39
#include <memory>
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
    40
#include "sun_security_mscapi_CKey.h"
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
    41
#include "sun_security_mscapi_CKeyStore.h"
50471
f0aeede1b855 8204572: SetupJdkLibrary should setup SRC and -I flags automatically
ihse
parents: 49151
diff changeset
    42
#include "sun_security_mscapi_PRNG.h"
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
    43
#include "sun_security_mscapi_CRSACipher.h"
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
    44
#include "sun_security_mscapi_CKeyPairGenerator_RSA.h"
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
    45
#include "sun_security_mscapi_CPublicKey.h"
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
    46
#include "sun_security_mscapi_CPublicKey_CRSAPublicKey.h"
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
    47
#include "sun_security_mscapi_CSignature.h"
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
    48
#include "sun_security_mscapi_CSignature_RSA.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define OID_EKU_ANY         "2.5.29.37.0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define CERTIFICATE_PARSING_EXCEPTION \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                            "java/security/cert/CertificateParsingException"
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
    54
#define INVALID_KEY_EXCEPTION \
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
    55
                            "java/security/InvalidKeyException"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#define KEY_EXCEPTION       "java/security/KeyException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#define KEYSTORE_EXCEPTION  "java/security/KeyStoreException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#define PROVIDER_EXCEPTION  "java/security/ProviderException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#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
    60
#define OUT_OF_MEMORY_ERROR "java/lang/OutOfMemoryError"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
    62
#define SS_CHECK(Status) \
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
    63
        if (Status != ERROR_SUCCESS) { \
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
    64
            ThrowException(env, SIGNATURE_EXCEPTION, Status); \
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
    65
            __leave; \
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
    66
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
    67
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    68
#define PP(fmt, ...) \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    69
        if (trace) { \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    70
            fprintf(stdout, "MSCAPI (%ld): ", __LINE__); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    71
            fprintf(stdout, fmt, ##__VA_ARGS__); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    72
            fprintf(stdout, "\n"); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    73
            fflush(stdout); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    74
        }
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
    75
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
extern "C" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    78
char* trace = getenv("CAPI_TRACE");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    79
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    81
 * 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
    82
 */
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    83
DEF_STATIC_JNI_OnLoad
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
    84
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    85
void showProperty(NCRYPT_HANDLE hKey);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    86
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    87
void dump(LPSTR title, PBYTE data, DWORD len)
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    88
{
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    89
    if (trace) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    90
        printf("==== %s ====\n", title);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    91
        for (DWORD i = 0; i < len; i+=16) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    92
            printf("%04x: ", i);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    93
            for (int j = 0; j < 16; j++) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    94
                if (j == 8) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    95
                    printf("  ");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    96
                }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    97
                if (i + j < len) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    98
                    printf("%02X ", *(data + i + j) & 0xff);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    99
                } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   100
                    printf("   ");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   101
                }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   102
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   103
            for (int j = 0; j < 16; j++) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   104
                if (i + j < len) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   105
                    int k = *(data + i + j) & 0xff;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   106
                    if (k < 32 || k > 127) printf(".");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   107
                    else printf("%c", (char)k);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   108
                }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   109
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   110
            printf("\n");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   111
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   112
        fflush(stdout);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   113
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   114
}
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   115
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 31470
diff changeset
   116
/*
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   117
 * 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
   118
 */
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   119
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
   120
                               const char *szMessage)
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   121
{
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   122
    jclass exceptionClazz = env->FindClass(exceptionName);
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   123
    if (exceptionClazz != NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   124
        env->ThrowNew(exceptionClazz, szMessage);
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   125
    }
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   126
}
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   127
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   128
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * Throws an arbitrary Java exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * The exception message is a Windows system error message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 */
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   132
void ThrowException(JNIEnv *env, const char *exceptionName, DWORD dwError)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    char szMessage[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    szMessage[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   137
    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
   138
        NULL, szMessage, sizeof(szMessage), NULL);
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   139
    if (res == 0) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   140
        strcpy(szMessage, "Unknown error");
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   141
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   143
    ThrowExceptionWithMessage(env, exceptionName, szMessage);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   146
/*
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   147
 * 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
   148
 * 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
   149
 */
49149
217780dff1bf 8198898: Compilation errors in jdk.crypto.mscapi with VS 2017
weijun
parents: 49148
diff changeset
   150
void* operator new[](std::size_t size, JNIEnv *env)
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   151
{
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   152
    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
   153
    if (buf == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   154
        ThrowExceptionWithMessage(env, OUT_OF_MEMORY_ERROR,
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   155
                "Native memory allocation failed");
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   156
    }
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   157
    return buf;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   158
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * Maps the name of a hash algorithm to an algorithm identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
ALG_ID MapHashAlgorithm(JNIEnv *env, jstring jHashAlgorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    const char* pszHashAlgorithm = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    ALG_ID algId = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   168
    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
   169
        == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   170
        return algId;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   171
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    if ((strcmp("SHA", pszHashAlgorithm) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        (strcmp("SHA1", pszHashAlgorithm) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        (strcmp("SHA-1", pszHashAlgorithm) == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        algId = CALG_SHA1;
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   178
    } else if (strcmp("SHA1+MD5", pszHashAlgorithm) == 0) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   179
        algId = CALG_SSL3_SHAMD5; // a 36-byte concatenation of SHA-1 and MD5
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    } else if (strcmp("SHA-256", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        algId = CALG_SHA_256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    } else if (strcmp("SHA-384", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        algId = CALG_SHA_384;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    } else if (strcmp("SHA-512", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        algId = CALG_SHA_512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    } else if (strcmp("MD5", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        algId = CALG_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    } else if (strcmp("MD2", pszHashAlgorithm) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        algId = CALG_MD2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    if (pszHashAlgorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        env->ReleaseStringUTFChars(jHashAlgorithm, pszHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
   return algId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   198
/*
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   199
 * Maps the name of a hash algorithm to a CNG Algorithm Identifier.
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   200
 */
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   201
LPCWSTR MapHashIdentifier(JNIEnv *env, jstring jHashAlgorithm) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   202
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   203
    const char* pszHashAlgorithm = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   204
    LPCWSTR id = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   205
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   206
    if ((pszHashAlgorithm = env->GetStringUTFChars(jHashAlgorithm, NULL))
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   207
            == NULL) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   208
        return id;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   209
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   210
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   211
    if ((strcmp("SHA", pszHashAlgorithm) == 0) ||
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   212
        (strcmp("SHA1", pszHashAlgorithm) == 0) ||
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   213
        (strcmp("SHA-1", pszHashAlgorithm) == 0)) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   214
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   215
        id = BCRYPT_SHA1_ALGORITHM;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   216
    } else if (strcmp("SHA-256", pszHashAlgorithm) == 0) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   217
        id = BCRYPT_SHA256_ALGORITHM;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   218
    } else if (strcmp("SHA-384", pszHashAlgorithm) == 0) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   219
        id = BCRYPT_SHA384_ALGORITHM;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   220
    } else if (strcmp("SHA-512", pszHashAlgorithm) == 0) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   221
        id = BCRYPT_SHA512_ALGORITHM;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   222
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   223
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   224
    if (pszHashAlgorithm)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   225
        env->ReleaseStringUTFChars(jHashAlgorithm, pszHashAlgorithm);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   226
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   227
    return id;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   228
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * Returns a certificate chain context given a certificate context and key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * usage identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
bool GetCertificateChain(LPSTR lpszKeyUsageIdentifier, PCCERT_CONTEXT pCertContext, PCCERT_CHAIN_CONTEXT* ppChainContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    CERT_ENHKEY_USAGE        EnhkeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    CERT_USAGE_MATCH         CertUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    CERT_CHAIN_PARA          ChainPara;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    DWORD                    dwFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    LPSTR                    szUsageIdentifierArray[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    szUsageIdentifierArray[0] = lpszKeyUsageIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    EnhkeyUsage.cUsageIdentifier = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    EnhkeyUsage.rgpszUsageIdentifier = szUsageIdentifierArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    CertUsage.dwType = USAGE_MATCH_TYPE_AND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    CertUsage.Usage  = EnhkeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    ChainPara.cbSize = sizeof(CERT_CHAIN_PARA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    ChainPara.RequestedUsage=CertUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    // Build a chain using CertGetCertificateChain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    // and the certificate retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    return (::CertGetCertificateChain(NULL,     // use the default chain engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                pCertContext,   // pointer to the end certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                NULL,           // use the default time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                NULL,           // search no additional stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                &ChainPara,     // use AND logic and enhanced key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                //  as indicated in the ChainPara
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                //  data structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                dwFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                NULL,           // currently reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                ppChainContext) == TRUE);       // return a pointer to the chain created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
/////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * Class:     sun_security_mscapi_PRNG
52801
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   270
 * Method:    getContext
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   271
 * Signature: ()J
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   272
 */
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   273
JNIEXPORT jlong JNICALL Java_sun_security_mscapi_PRNG_getContext
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   274
        (JNIEnv *env, jclass clazz) {
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   275
    HCRYPTPROV hCryptProv = NULL;
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   276
    if(::CryptAcquireContext( //deprecated
52801
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   277
       &hCryptProv,
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   278
       NULL,
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   279
       NULL,
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   280
       PROV_RSA_FULL,
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   281
       CRYPT_VERIFYCONTEXT) == FALSE)
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   282
    {
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   283
        ThrowException(env, PROVIDER_EXCEPTION, GetLastError());
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   284
    }
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   285
    return hCryptProv;
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   286
}
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   287
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   288
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   289
/*
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   290
 * Class:     sun_security_mscapi_PRNG
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   291
 * Method:    releaseContext
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   292
 * Signature: (J)V
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   293
 */
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   294
JNIEXPORT void JNICALL Java_sun_security_mscapi_PRNG_releaseContext
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   295
        (JNIEnv *env, jclass clazz, jlong ctxt) {
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   296
    if (ctxt) {
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   297
        ::CryptReleaseContext((HCRYPTPROV)ctxt, 0); //deprecated
52801
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   298
    }
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   299
}
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   300
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   301
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   302
/*
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   303
 * Class:     sun_security_mscapi_PRNG
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * Method:    generateSeed
52801
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   305
 * Signature: (JI[B)[B
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_PRNG_generateSeed
52801
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   308
  (JNIEnv *env, jclass clazz, jlong ctxt, jint length, jbyteArray seed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
52801
0baf34792a27 8210476: sun/security/mscapi/PrngSlow.java fails with Still too slow
weijun
parents: 52193
diff changeset
   311
    HCRYPTPROV hCryptProv = (HCRYPTPROV)ctxt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    jbyte*     reseedBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    jbyte*     seedBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    jbyteArray result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         * If length is negative then use the supplied seed to re-seed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * generator and return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * If length is non-zero then generate a new seed according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * requested length and return the new seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * If length is zero then overwrite the supplied seed with a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * seed of the same length and return the seed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            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
   328
            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
   329
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   330
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   332
            if (::CryptGenRandom( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                (BYTE *) reseedBytes) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                ThrowException(env, PROVIDER_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
52193
a3cab5d26ef8 8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed
igerasim
parents: 52155
diff changeset
   343
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
52193
a3cab5d26ef8 8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed
igerasim
parents: 52155
diff changeset
   345
            if (length > 0) {
a3cab5d26ef8 8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed
igerasim
parents: 52155
diff changeset
   346
                seed = env->NewByteArray(length);
a3cab5d26ef8 8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed
igerasim
parents: 52155
diff changeset
   347
                if (seed == NULL) {
a3cab5d26ef8 8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed
igerasim
parents: 52155
diff changeset
   348
                    __leave;
a3cab5d26ef8 8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed
igerasim
parents: 52155
diff changeset
   349
                }
a3cab5d26ef8 8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed
igerasim
parents: 52155
diff changeset
   350
            } else {
a3cab5d26ef8 8201355: Avoid native memory allocation in sun.security.mscapi.PRNG.generateSeed
igerasim
parents: 52155
diff changeset
   351
                length = env->GetArrayLength(seed);
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   352
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   354
            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
   355
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   356
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   358
            if (::CryptGenRandom( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                (BYTE *) seedBytes) == FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                ThrowException(env, PROVIDER_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            result = seed; // seed will be updated when seedBytes gets released
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (reseedBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            env->ReleaseByteArrayElements(seed, reseedBytes, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (seedBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            env->ReleaseByteArrayElements(seed, seedBytes, 0); // update orig
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   387
 * Class:     sun_security_mscapi_CKeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
 * Method:    loadKeysOrCertificateChains
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   389
 * Signature: (Ljava/lang/String;)V
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   391
JNIEXPORT void JNICALL Java_sun_security_mscapi_CKeyStore_loadKeysOrCertificateChains
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   392
  (JNIEnv *env, jobject obj, jstring jCertStoreName)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Certificate in cert store has enhanced key usage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * property (or EKU property) that is not part of the certificate itself. To determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * if the certificate should be returned, both the enhanced key usage in certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * extension block and the extension property stored along with the certificate in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * certificate store should be examined. Otherwise, we won't be able to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * the proper key usage from the Java side because the information is not stored as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * part of the encoded certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    char* pszNameString = NULL; // certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    DWORD cchNameString = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // 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
   414
        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
   415
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   416
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
   417
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // Determine clazz and method ID to generate certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        jclass clazzArrayList = env->FindClass("java/util/ArrayList");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   427
        if (clazzArrayList == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   428
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   429
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        jmethodID mNewArrayList = env->GetMethodID(clazzArrayList, "<init>", "()V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   432
        if (mNewArrayList == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   433
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   434
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   436
        jclass clazzOfThis = env->GetObjectClass(obj);
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   437
        if (clazzOfThis == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   438
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   439
        }
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   440
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   441
        jmethodID mGenCert = env->GetMethodID(clazzOfThis,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                              "generateCertificate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                              "([BLjava/util/Collection;)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   444
        if (mGenCert == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   445
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   446
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // Determine method ID to generate certificate chain
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   449
        jmethodID mGenCertChain = env->GetMethodID(clazzOfThis,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                                                   "generateCertificateChain",
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   451
                                                   "(Ljava/lang/String;Ljava/util/Collection;)V");
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   452
        if (mGenCertChain == NULL) {
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   453
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   454
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // Determine method ID to generate RSA certificate chain
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   457
        jmethodID mGenKeyAndCertChain = env->GetMethodID(clazzOfThis,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   458
                                                   "generateKeyAndCertificateChain",
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   459
                                                   "(ZLjava/lang/String;JJILjava/util/Collection;)V");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   460
        if (mGenKeyAndCertChain == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   461
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   462
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        // Use CertEnumCertificatesInStore to get the certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // from the open store. pCertContext must be reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // NULL to retrieve the first certificate in the store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        while (pCertContext = ::CertEnumCertificatesInStore(hCertStore, pCertContext))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   469
            PP("--------------------------");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            // Check if private key available - client authentication certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            // must have private key available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            DWORD dwKeySpec = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            HCRYPTKEY hUserKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            BOOL bCallerFreeProv = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            BOOL bHasNoPrivateKey = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            DWORD dwPublicKeyLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   479
            // First, probe it silently
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   480
            if (::CryptAcquireCertificatePrivateKey(pCertContext,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   481
                    CRYPT_ACQUIRE_ALLOW_NCRYPT_KEY_FLAG | CRYPT_ACQUIRE_SILENT_FLAG, NULL,
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   482
                    &hCryptProv, &dwKeySpec, &bCallerFreeProv) == FALSE
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   483
                && GetLastError() != NTE_SILENT_CONTEXT)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   485
                PP("bHasNoPrivateKey = TRUE!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                bHasNoPrivateKey = TRUE;
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   487
            }
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   488
            else
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   489
            {
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   490
                if (bCallerFreeProv == TRUE) {
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   491
                    ::CryptReleaseContext(hCryptProv, NULL); // deprecated
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   492
                    bCallerFreeProv = FALSE;
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   493
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   495
                // Second, acquire the key normally (not silently)
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   496
                if (::CryptAcquireCertificatePrivateKey(pCertContext, CRYPT_ACQUIRE_ALLOW_NCRYPT_KEY_FLAG, NULL,
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   497
                        &hCryptProv, &dwKeySpec, &bCallerFreeProv) == FALSE)
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   498
                {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   499
                    PP("bHasNoPrivateKey = TRUE!!");
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   500
                    bHasNoPrivateKey = TRUE;
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   501
                }
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   502
                else
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   503
                {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   504
                    if ((dwKeySpec & CERT_NCRYPT_KEY_SPEC) == CERT_NCRYPT_KEY_SPEC) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   505
                        PP("CNG %I64d", hCryptProv);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   506
                    } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   507
                        // Private key is available
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   508
                        BOOL bGetUserKey = ::CryptGetUserKey(hCryptProv, dwKeySpec, &hUserKey); //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   510
                        // Skip certificate if cannot find private key
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   511
                        if (bGetUserKey == FALSE) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   512
                            if (bCallerFreeProv)
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   513
                                ::CryptReleaseContext(hCryptProv, NULL); // deprecated
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   514
                            continue;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   515
                        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   516
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   517
                        // Set cipher mode to ECB
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   518
                        DWORD dwCipherMode = CRYPT_MODE_ECB;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   519
                        ::CryptSetKeyParam(hUserKey, KP_MODE, (BYTE*)&dwCipherMode, NULL); //deprecated
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   520
                        PP("CAPI %I64d %I64d", hCryptProv, hUserKey);
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   521
                    }
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   522
                    // 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
   523
                    // 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
   524
                    // 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
   525
                    // 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
   526
                    // in the certificate.
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   527
                    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
   528
                            &(pCertContext->pCertInfo->SubjectPublicKeyInfo));
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   529
                }
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   530
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            PCCERT_CHAIN_CONTEXT pCertChainContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            // Build certificate chain by using system certificate store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            // Add cert chain into collection for any key usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            //
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   536
            if (GetCertificateChain(OID_EKU_ANY, pCertContext, &pCertChainContext))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            {
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   538
                for (DWORD i = 0; i < pCertChainContext->cChain; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    // Found cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    PCERT_SIMPLE_CHAIN rgpChain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                        pCertChainContext->rgpChain[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    // Create ArrayList to store certs in each chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    jobject jArrayList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        env->NewObject(clazzArrayList, mNewArrayList);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   547
                    if (jArrayList == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   548
                        __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   549
                    }
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   550
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   551
                    // Cleanup the previous allocated name
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   552
                    if (pszNameString) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   553
                        delete [] pszNameString;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   554
                        pszNameString = NULL;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   555
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    for (unsigned int j=0; j < rgpChain->cElement; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        PCERT_CHAIN_ELEMENT rgpElement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                            rgpChain->rgpElement[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        PCCERT_CONTEXT pc = rgpElement->pCertContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        // Retrieve the friendly name of the first certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        // in the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        if (j == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                            // If the cert's name cannot be retrieved then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                            // pszNameString remains set to NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                            // (An alias name will be generated automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                            // when storing this cert in the keystore.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                            // Get length of friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                            if ((cchNameString = CertGetNameString(pc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                                NULL, 0)) > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                                // Found friendly name
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   578
                                pszNameString = new (env) char[cchNameString];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   579
                                if (pszNameString == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   580
                                    __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   581
                                }
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   582
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                CertGetNameString(pc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                    CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                                    pszNameString, cchNameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        BYTE* pbCertEncoded = pc->pbCertEncoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        DWORD cbCertEncoded = pc->cbCertEncoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        // Allocate and populate byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        jbyteArray byteArray = env->NewByteArray(cbCertEncoded);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   594
                        if (byteArray == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   595
                            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   596
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                        env->SetByteArrayRegion(byteArray, 0, cbCertEncoded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            (jbyte*) pbCertEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        // Generate certificate from byte array and store into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        // cert collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                        env->CallVoidMethod(obj, mGenCert, byteArray, jArrayList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    }
40414
1f4a9da41c1f 8153438: Avoid repeated "Please insert a smart card" popup windows
igerasim
parents: 36759
diff changeset
   604
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   605
                    // Usually pszNameString should be non-NULL. It's either
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   606
                    // the friendly name or an element from the subject name
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   607
                    // or SAN.
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   608
                    if (pszNameString)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    {
53333
henryjen
parents: 53330 53007
diff changeset
   610
                        PP("%s: %s", pszNameString, pCertContext->pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   611
                        if (bHasNoPrivateKey)
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   612
                        {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   613
                            // Generate certificate chain and store into cert chain
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   614
                            // collection
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   615
                            jstring name = env->NewStringUTF(pszNameString);
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   616
                            if (name == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   617
                                __leave;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   618
                            }
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   619
                            env->CallVoidMethod(obj, mGenCertChain,
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   620
                                name,
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   621
                                jArrayList);
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   622
                        }
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   623
                        else
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   624
                        {
53333
henryjen
parents: 53330 53007
diff changeset
   625
                            if (hUserKey) {
henryjen
parents: 53330 53007
diff changeset
   626
                                // Only accept RSA for CAPI
henryjen
parents: 53330 53007
diff changeset
   627
                                DWORD dwData = CALG_RSA_KEYX;
henryjen
parents: 53330 53007
diff changeset
   628
                                DWORD dwSize = sizeof(DWORD);
henryjen
parents: 53330 53007
diff changeset
   629
                                ::CryptGetKeyParam(hUserKey, KP_ALGID, (BYTE*)&dwData, //deprecated
henryjen
parents: 53330 53007
diff changeset
   630
                                        &dwSize, NULL);
henryjen
parents: 53330 53007
diff changeset
   631
                                if ((dwData & ALG_TYPE_RSA) == ALG_TYPE_RSA)
henryjen
parents: 53330 53007
diff changeset
   632
                                {
henryjen
parents: 53330 53007
diff changeset
   633
                                    // Generate RSA certificate chain and store into cert
henryjen
parents: 53330 53007
diff changeset
   634
                                    // chain collection
henryjen
parents: 53330 53007
diff changeset
   635
                                    jstring name = env->NewStringUTF(pszNameString);
henryjen
parents: 53330 53007
diff changeset
   636
                                    if (name == NULL) {
henryjen
parents: 53330 53007
diff changeset
   637
                                        __leave;
henryjen
parents: 53330 53007
diff changeset
   638
                                    }
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   639
                                    env->CallVoidMethod(obj, mGenKeyAndCertChain,
53333
henryjen
parents: 53330 53007
diff changeset
   640
                                            1,
henryjen
parents: 53330 53007
diff changeset
   641
                                            name,
henryjen
parents: 53330 53007
diff changeset
   642
                                            (jlong) hCryptProv, (jlong) hUserKey,
henryjen
parents: 53330 53007
diff changeset
   643
                                            dwPublicKeyLength, jArrayList);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   644
                                }
53333
henryjen
parents: 53330 53007
diff changeset
   645
                            } else {
henryjen
parents: 53330 53007
diff changeset
   646
                                // Only accept EC for CNG
henryjen
parents: 53330 53007
diff changeset
   647
                                BYTE buffer[32];
henryjen
parents: 53330 53007
diff changeset
   648
                                DWORD len = 0;
henryjen
parents: 53330 53007
diff changeset
   649
                                if (::NCryptGetProperty(
henryjen
parents: 53330 53007
diff changeset
   650
                                        hCryptProv, NCRYPT_ALGORITHM_PROPERTY,
henryjen
parents: 53330 53007
diff changeset
   651
                                        (PBYTE)buffer, 32, &len, NCRYPT_SILENT_FLAG) == ERROR_SUCCESS) {
henryjen
parents: 53330 53007
diff changeset
   652
                                    if (buffer[0] == 'E' && buffer[2] == 'C'
henryjen
parents: 53330 53007
diff changeset
   653
                                            && (dwPublicKeyLength == 256
henryjen
parents: 53330 53007
diff changeset
   654
                                                    || dwPublicKeyLength == 384
henryjen
parents: 53330 53007
diff changeset
   655
                                                    || dwPublicKeyLength == 521)) {
henryjen
parents: 53330 53007
diff changeset
   656
                                        jstring name = env->NewStringUTF(pszNameString);
henryjen
parents: 53330 53007
diff changeset
   657
                                        if (name == NULL) {
henryjen
parents: 53330 53007
diff changeset
   658
                                            __leave;
henryjen
parents: 53330 53007
diff changeset
   659
                                        }
henryjen
parents: 53330 53007
diff changeset
   660
                                        env->CallVoidMethod(obj, mGenKeyAndCertChain,
henryjen
parents: 53330 53007
diff changeset
   661
                                            0,
henryjen
parents: 53330 53007
diff changeset
   662
                                            name,
henryjen
parents: 53330 53007
diff changeset
   663
                                            (jlong) hCryptProv, 0,
henryjen
parents: 53330 53007
diff changeset
   664
                                            dwPublicKeyLength, jArrayList);
henryjen
parents: 53330 53007
diff changeset
   665
                                    }
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   666
                                }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   667
                            }
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   668
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                // Free cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                if (pCertChainContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    ::CertFreeCertificateChain(pCertChainContext);
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   675
            } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   676
                PP("GetCertificateChain failed %d", GetLastError());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (pszNameString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
 * Class:     sun_security_mscapi_Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
 * Method:    cleanUp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
 * Signature: (JJ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
JNIEXPORT void JNICALL Java_sun_security_mscapi_Key_cleanUp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
  (JNIEnv *env, jclass clazz, jlong hCryptProv, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    if (hCryptKey != NULL)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   703
        ::CryptDestroyKey((HCRYPTKEY) hCryptKey); // deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    if (hCryptProv != NULL)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   706
        ::CryptReleaseContext((HCRYPTPROV) hCryptProv, NULL); // deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   710
 * Class:     sun_security_mscapi_CSignature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
 * Method:    signHash
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   712
 * Signature: (Z[BILjava/lang/String;JJ)[B
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   714
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CSignature_signHash
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   715
  (JNIEnv *env, jclass clazz, jboolean noHashOID, jbyteArray jHash,
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   716
        jint jHashSize, jstring jHashAlgorithm, jlong hCryptProv,
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   717
        jlong hCryptKey)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    HCRYPTHASH hHash = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    jbyte* pHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    jbyte* pSignedHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    jbyteArray jSignedHash = NULL;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   723
    HCRYPTPROV hCryptProvAlt = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        // Map hash algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        ALG_ID algId = MapHashAlgorithm(env, jHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        // Acquire a hash object handle.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   731
        if (::CryptCreateHash(HCRYPTPROV(hCryptProv), algId, 0, 0, &hHash) == FALSE) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   733
            // Failover to using the PROV_RSA_AES CSP
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   734
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   735
            DWORD cbData = 256;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   736
            BYTE pbData[256];
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   737
            pbData[0] = '\0';
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   738
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   739
            // Get name of the key container
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   740
            ::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_CONTAINER, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   741
                (BYTE *)pbData, &cbData, 0);
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   742
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   743
            // Acquire an alternative CSP handle
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   744
            if (::CryptAcquireContext(&hCryptProvAlt, LPCSTR(pbData), NULL, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   745
                PROV_RSA_AES, 0) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   746
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   747
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   748
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   749
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   750
            }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   751
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   752
            // Acquire a hash object handle.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   753
            if (::CryptCreateHash(HCRYPTPROV(hCryptProvAlt), algId, 0, 0, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   754
                &hHash) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   755
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   756
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   757
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   758
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        // 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
   762
        pHashBuffer = new (env) jbyte[jHashSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   763
        if (pHashBuffer == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   764
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   765
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        // Set hash value in the hash object
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   769
        if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*)pHashBuffer, NULL) == FALSE) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        // Determine key spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        DWORD dwKeySpec = AT_SIGNATURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        ALG_ID dwAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        DWORD dwAlgIdLen = sizeof(ALG_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   780
        if (! ::CryptGetKeyParam((HCRYPTKEY) hCryptKey, KP_ALGID, (BYTE*)&dwAlgId, &dwAlgIdLen, 0)) { //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if (CALG_RSA_KEYX == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            dwKeySpec = AT_KEYEXCHANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        // Determine size of buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        DWORD dwBufLen = 0;
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   791
        DWORD dwFlags = 0;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   792
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   793
        if (noHashOID == JNI_TRUE) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   794
            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
   795
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   796
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   797
        if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, NULL, &dwBufLen) == FALSE) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   803
        pSignedHashBuffer = new (env) jbyte[dwBufLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   804
        if (pSignedHashBuffer == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   805
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   806
        }
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   807
        if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, (BYTE*)pSignedHashBuffer, &dwBufLen) == FALSE) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        // Create new byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        jbyteArray temp = env->NewByteArray(dwBufLen);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   815
        if (temp == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   816
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   817
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        // Copy data from native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        env->SetByteArrayRegion(temp, 0, dwBufLen, pSignedHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        jSignedHash = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (pSignedHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            delete [] pSignedHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (pHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            delete [] pHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (hHash)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   833
            ::CryptDestroyHash(hHash); //deprecated
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   834
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   835
        if (hCryptProvAlt)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   836
            ::CryptReleaseContext(hCryptProvAlt, 0); // deprecated
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
    return jSignedHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   843
 * Class:     sun_security_mscapi_CSignature
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   844
 * Method:    signCngHash
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   845
 * Signature: (I[BIILjava/lang/String;JJ)[B
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   846
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   847
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CSignature_signCngHash
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   848
  (JNIEnv *env, jclass clazz, jint type, jbyteArray jHash,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   849
        jint jHashSize, jint saltLen, jstring jHashAlgorithm, jlong hCryptProv,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   850
        jlong hCryptKey)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   851
{
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   852
    jbyteArray jSignedHash = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   853
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   854
    jbyte* pHashBuffer = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   855
    jbyte* pSignedHashBuffer = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   856
    NCRYPT_KEY_HANDLE hk = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   857
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   858
    __try
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   859
    {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   860
        if (hCryptKey == 0) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   861
            hk = (NCRYPT_KEY_HANDLE)hCryptProv;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   862
        } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   863
            SS_CHECK(::NCryptTranslateHandle(
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   864
                NULL,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   865
                &hk,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   866
                hCryptProv,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   867
                hCryptKey,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   868
                NULL,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   869
                0));
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   870
        }
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   871
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   872
        // Copy hash from Java to native buffer
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   873
        pHashBuffer = new (env) jbyte[jHashSize];
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   874
        if (pHashBuffer == NULL) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   875
            __leave;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   876
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   877
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   878
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   879
        VOID* param;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   880
        DWORD dwFlags;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   881
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   882
        switch (type) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   883
        case 0:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   884
            param = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   885
            dwFlags = 0;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   886
            break;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   887
        case 1:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   888
            BCRYPT_PKCS1_PADDING_INFO pkcs1Info;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   889
            pkcs1Info.pszAlgId = MapHashIdentifier(env, jHashAlgorithm);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   890
            if (pkcs1Info.pszAlgId == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   891
                ThrowExceptionWithMessage(env, SIGNATURE_EXCEPTION,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   892
                        "Unrecognised hash algorithm");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   893
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   894
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   895
            param = &pkcs1Info;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   896
            dwFlags = BCRYPT_PAD_PKCS1;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   897
            break;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   898
        case 2:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   899
            BCRYPT_PSS_PADDING_INFO pssInfo;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   900
            pssInfo.pszAlgId = MapHashIdentifier(env, jHashAlgorithm);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   901
            pssInfo.cbSalt = saltLen;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   902
            if (pssInfo.pszAlgId == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   903
                ThrowExceptionWithMessage(env, SIGNATURE_EXCEPTION,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   904
                        "Unrecognised hash algorithm");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   905
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   906
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   907
            param = &pssInfo;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   908
            dwFlags = BCRYPT_PAD_PSS;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   909
            break;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   910
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   911
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   912
        DWORD jSignedHashSize = 0;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   913
        SS_CHECK(::NCryptSignHash(
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   914
                hk,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   915
                param,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   916
                (BYTE*)pHashBuffer, jHashSize,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   917
                NULL, 0, &jSignedHashSize,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   918
                dwFlags
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   919
                ));
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   920
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   921
        pSignedHashBuffer = new (env) jbyte[jSignedHashSize];
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   922
        if (pSignedHashBuffer == NULL) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   923
            __leave;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   924
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   925
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   926
        SS_CHECK(::NCryptSignHash(
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   927
                hk,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   928
                param,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   929
                (BYTE*)pHashBuffer, jHashSize,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   930
                (BYTE*)pSignedHashBuffer, jSignedHashSize, &jSignedHashSize,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   931
                dwFlags
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   932
                ));
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   933
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   934
        // Create new byte array
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   935
        jbyteArray temp = env->NewByteArray(jSignedHashSize);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   936
        if (temp == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   937
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   938
        }
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   939
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   940
        // Copy data from native buffer
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   941
        env->SetByteArrayRegion(temp, 0, jSignedHashSize, pSignedHashBuffer);
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   942
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   943
        jSignedHash = temp;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   944
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   945
    __finally
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   946
    {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   947
        if (pSignedHashBuffer)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   948
            delete [] pSignedHashBuffer;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   949
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   950
        if (pHashBuffer)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   951
            delete [] pHashBuffer;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   952
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   953
        if (hCryptKey != 0 && hk != NULL)
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   954
            ::NCryptFreeObject(hk);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   955
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   956
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   957
    return jSignedHash;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   958
}
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   959
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   960
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   961
 * Class:     sun_security_mscapi_CSignature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
 * Method:    verifySignedHash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
 * Signature: ([BIL/java/lang/String;[BIJJ)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   965
JNIEXPORT jboolean JNICALL Java_sun_security_mscapi_CSignature_verifySignedHash
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
  (JNIEnv *env, jclass clazz, jbyteArray jHash, jint jHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        jstring jHashAlgorithm, jbyteArray jSignedHash, jint jSignedHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        jlong hCryptProv, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    HCRYPTHASH hHash = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    jbyte* pHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    jbyte* pSignedHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    DWORD dwSignedHashBufferLen = jSignedHashSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    jboolean result = JNI_FALSE;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   975
    HCRYPTPROV hCryptProvAlt = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        // Map hash algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        ALG_ID algId = MapHashAlgorithm(env, jHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        // Acquire a hash object handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if (::CryptCreateHash(HCRYPTPROV(hCryptProv), algId, 0, 0, &hHash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   986
            // Failover to using the PROV_RSA_AES CSP
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   987
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   988
            DWORD cbData = 256;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   989
            BYTE pbData[256];
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   990
            pbData[0] = '\0';
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   991
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   992
            // Get name of the key container
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   993
            ::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_CONTAINER, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   994
                (BYTE *)pbData, &cbData, 0);
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   995
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   996
            // Acquire an alternative CSP handle
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   997
            if (::CryptAcquireContext(&hCryptProvAlt, LPCSTR(pbData), NULL, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   998
                PROV_RSA_AES, 0) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   999
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1000
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1001
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1002
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1003
            }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1004
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1005
            // Acquire a hash object handle.
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1006
            if (::CryptCreateHash(HCRYPTPROV(hCryptProvAlt), algId, 0, 0,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1007
                &hHash) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1008
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1009
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1010
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1011
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        // 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
  1015
        pHashBuffer = new (env) jbyte[jHashSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1016
        if (pHashBuffer == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1017
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1018
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        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
  1020
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1021
        pSignedHashBuffer = new (env) jbyte[jSignedHashSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1022
        if (pSignedHashBuffer == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1023
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1024
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        env->GetByteArrayRegion(jSignedHash, 0, jSignedHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            pSignedHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        // Set hash value in the hash object
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1029
        if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*) pHashBuffer, NULL) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        // For RSA, the hash encryption algorithm is normally the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        // public key algorithm, so AT_SIGNATURE is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        // Verify the signature
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1040
        if (::CryptVerifySignatureA(hHash, (BYTE *) pSignedHashBuffer, //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            dwSignedHashBufferLen, (HCRYPTKEY) hCryptKey, NULL, 0) == TRUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            result = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        if (pSignedHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            delete [] pSignedHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        if (pHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            delete [] pHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        if (hHash)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1056
            ::CryptDestroyHash(hHash); //deprecated
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1057
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1058
        if (hCryptProvAlt)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1059
            ::CryptReleaseContext(hCryptProvAlt, 0); // deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1066
 * Class:     sun_security_mscapi_CSignature
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1067
 * Method:    verifyCngSignedHash
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1068
 * Signature: (I[BI[BIILjava/lang/String;JJ)Z
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1069
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1070
JNIEXPORT jboolean JNICALL Java_sun_security_mscapi_CSignature_verifyCngSignedHash
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1071
  (JNIEnv *env, jclass clazz, jint type,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1072
        jbyteArray jHash, jint jHashSize,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1073
        jbyteArray jSignedHash, jint jSignedHashSize,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1074
        jint saltLen, jstring jHashAlgorithm,
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1075
        jlong hCryptProv, jlong hCryptKey)
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1076
{
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1077
    jbyte* pHashBuffer = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1078
    jbyte* pSignedHashBuffer = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1079
    jboolean result = JNI_FALSE;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1080
    NCRYPT_KEY_HANDLE hk = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1081
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1082
    __try
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1083
    {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1084
        if (hCryptKey == 0) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1085
            hk = (NCRYPT_KEY_HANDLE)hCryptProv;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1086
        } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1087
            SS_CHECK(::NCryptTranslateHandle(
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1088
                NULL,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1089
                &hk,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1090
                hCryptProv,
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1091
                hCryptKey,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1092
                NULL,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1093
                0));
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1094
        }
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1095
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1096
        // Copy hash and signedHash from Java to native buffer
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1097
        pHashBuffer = new (env) jbyte[jHashSize];
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1098
        if (pHashBuffer == NULL) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1099
            __leave;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1100
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1101
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1102
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1103
        pSignedHashBuffer = new (env) jbyte[jSignedHashSize];
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1104
        if (pSignedHashBuffer == NULL) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1105
            __leave;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1106
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1107
        env->GetByteArrayRegion(jSignedHash, 0, jSignedHashSize,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1108
            pSignedHashBuffer);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1109
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1110
        VOID* param;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1111
        DWORD dwFlags;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1112
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1113
        switch (type) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1114
        case 0:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1115
            param = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1116
            dwFlags = 0;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1117
            break;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1118
        case 1:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1119
            BCRYPT_PKCS1_PADDING_INFO pkcs1Info;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1120
            pkcs1Info.pszAlgId = MapHashIdentifier(env, jHashAlgorithm);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1121
            if (pkcs1Info.pszAlgId == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1122
                ThrowExceptionWithMessage(env, SIGNATURE_EXCEPTION,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1123
                        "Unrecognised hash algorithm");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1124
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1125
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1126
            param = &pkcs1Info;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1127
            dwFlags = NCRYPT_PAD_PKCS1_FLAG;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1128
            break;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1129
        case 2:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1130
            BCRYPT_PSS_PADDING_INFO pssInfo;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1131
            pssInfo.pszAlgId = MapHashIdentifier(env, jHashAlgorithm);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1132
            pssInfo.cbSalt = saltLen;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1133
            if (pssInfo.pszAlgId == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1134
                ThrowExceptionWithMessage(env, SIGNATURE_EXCEPTION,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1135
                        "Unrecognised hash algorithm");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1136
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1137
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1138
            param = &pssInfo;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1139
            dwFlags = NCRYPT_PAD_PSS_FLAG;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1140
            break;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1141
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1142
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1143
        if (::NCryptVerifySignature(hk, param,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1144
                (BYTE *) pHashBuffer, jHashSize,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1145
                (BYTE *) pSignedHashBuffer, jSignedHashSize,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1146
                dwFlags) == ERROR_SUCCESS)
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1147
        {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1148
            result = JNI_TRUE;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1149
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1150
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1151
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1152
    __finally
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1153
    {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1154
        if (pSignedHashBuffer)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1155
            delete [] pSignedHashBuffer;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1156
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1157
        if (pHashBuffer)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1158
            delete [] pHashBuffer;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1159
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1160
        if (hCryptKey != 0 && hk != NULL)
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1161
            ::NCryptFreeObject(hk);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1162
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1163
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1164
    return result;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1165
}
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1166
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1167
#define DUMP_PROP(p) \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1168
    if (::NCryptGetProperty(hKey, p, (PBYTE)buffer, 8192, &len, NCRYPT_SILENT_FLAG) == ERROR_SUCCESS) { \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1169
        sprintf(header, "%s %ls", #p, p); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1170
        dump(header, buffer, len); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1171
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1172
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1173
#define EXPORT_BLOB(p) \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1174
    desc.cBuffers = 0; \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1175
    if (::NCryptExportKey(hKey, NULL, p, &desc, (PBYTE)buffer, 8192, &len, NCRYPT_SILENT_FLAG) == ERROR_SUCCESS) { \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1176
        sprintf(header, "%s %ls (%ld)", #p, p, desc.cBuffers); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1177
        dump(header, buffer, len); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1178
        for (int i = 0; i < (int)desc.cBuffers; i++) { \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1179
            sprintf(header, "desc %ld", desc.pBuffers[i].BufferType); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1180
            dump(header, (PBYTE)desc.pBuffers[i].pvBuffer, desc.pBuffers[i].cbBuffer); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1181
        } \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1182
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1183
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1184
void showProperty(NCRYPT_HANDLE hKey) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1185
    char header[100];
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1186
    BYTE buffer[8192];
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1187
    DWORD len = 9;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1188
    NCryptBufferDesc desc;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1189
    DUMP_PROP(NCRYPT_ALGORITHM_GROUP_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1190
    DUMP_PROP(NCRYPT_ALGORITHM_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1191
    DUMP_PROP(NCRYPT_ASSOCIATED_ECDH_KEY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1192
    DUMP_PROP(NCRYPT_BLOCK_LENGTH_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1193
    DUMP_PROP(NCRYPT_CERTIFICATE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1194
    DUMP_PROP(NCRYPT_DH_PARAMETERS_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1195
    DUMP_PROP(NCRYPT_EXPORT_POLICY_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1196
    DUMP_PROP(NCRYPT_IMPL_TYPE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1197
    DUMP_PROP(NCRYPT_KEY_TYPE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1198
    DUMP_PROP(NCRYPT_KEY_USAGE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1199
    DUMP_PROP(NCRYPT_LAST_MODIFIED_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1200
    DUMP_PROP(NCRYPT_LENGTH_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1201
    DUMP_PROP(NCRYPT_LENGTHS_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1202
    DUMP_PROP(NCRYPT_MAX_NAME_LENGTH_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1203
    DUMP_PROP(NCRYPT_NAME_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1204
    DUMP_PROP(NCRYPT_PIN_PROMPT_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1205
    DUMP_PROP(NCRYPT_PIN_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1206
    DUMP_PROP(NCRYPT_PROVIDER_HANDLE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1207
    DUMP_PROP(NCRYPT_READER_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1208
    DUMP_PROP(NCRYPT_ROOT_CERTSTORE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1209
    DUMP_PROP(NCRYPT_SCARD_PIN_ID);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1210
    DUMP_PROP(NCRYPT_SCARD_PIN_INFO);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1211
    DUMP_PROP(NCRYPT_SECURE_PIN_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1212
    DUMP_PROP(NCRYPT_SECURITY_DESCR_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1213
    DUMP_PROP(NCRYPT_SECURITY_DESCR_SUPPORT_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1214
    DUMP_PROP(NCRYPT_SMARTCARD_GUID_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1215
    DUMP_PROP(NCRYPT_UI_POLICY_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1216
    DUMP_PROP(NCRYPT_UNIQUE_NAME_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1217
    DUMP_PROP(NCRYPT_USE_CONTEXT_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1218
    DUMP_PROP(NCRYPT_USE_COUNT_ENABLED_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1219
    DUMP_PROP(NCRYPT_USE_COUNT_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1220
    DUMP_PROP(NCRYPT_USER_CERTSTORE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1221
    DUMP_PROP(NCRYPT_VERSION_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1222
    DUMP_PROP(NCRYPT_WINDOW_HANDLE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1223
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1224
    EXPORT_BLOB(BCRYPT_DH_PRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1225
    EXPORT_BLOB(BCRYPT_DH_PUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1226
    EXPORT_BLOB(BCRYPT_DSA_PRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1227
    EXPORT_BLOB(BCRYPT_DSA_PUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1228
    EXPORT_BLOB(BCRYPT_ECCPRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1229
    EXPORT_BLOB(BCRYPT_ECCPUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1230
    EXPORT_BLOB(BCRYPT_PUBLIC_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1231
    EXPORT_BLOB(BCRYPT_PRIVATE_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1232
    EXPORT_BLOB(BCRYPT_RSAFULLPRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1233
    EXPORT_BLOB(BCRYPT_RSAPRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1234
    EXPORT_BLOB(BCRYPT_RSAPUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1235
    EXPORT_BLOB(LEGACY_DH_PRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1236
    EXPORT_BLOB(LEGACY_DH_PUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1237
    EXPORT_BLOB(LEGACY_DSA_PRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1238
    EXPORT_BLOB(LEGACY_DSA_PUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1239
    EXPORT_BLOB(LEGACY_RSAPRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1240
    EXPORT_BLOB(LEGACY_RSAPUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1241
    EXPORT_BLOB(NCRYPT_CIPHER_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1242
    EXPORT_BLOB(NCRYPT_OPAQUETRANSPORT_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1243
    EXPORT_BLOB(NCRYPT_PKCS7_ENVELOPE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1244
    //EXPORT_BLOB(NCRYPTBUFFER_CERT_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1245
    //EXPORT_BLOB(NCRYPT_PKCS8_PRIVATE_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1246
    BCryptBuffer bb;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1247
    bb.BufferType = NCRYPTBUFFER_PKCS_SECRET;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1248
    bb.cbBuffer = 18;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1249
    bb.pvBuffer = L"changeit";
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1250
    BCryptBufferDesc bbd;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1251
    bbd.ulVersion = 0;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1252
    bbd.cBuffers = 1;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1253
    bbd.pBuffers = &bb;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1254
    if(::NCryptExportKey(hKey, NULL, NCRYPT_PKCS8_PRIVATE_KEY_BLOB, NULL,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1255
            (PBYTE)buffer, 8192, &len, NCRYPT_SILENT_FLAG) == ERROR_SUCCESS) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1256
        sprintf(header, "NCRYPT_PKCS8_PRIVATE_KEY_BLOB %ls", NCRYPT_PKCS8_PRIVATE_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1257
        dump(header, buffer, len);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1258
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1259
    EXPORT_BLOB(NCRYPT_PROTECTED_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1260
}
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1261
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1262
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1263
 * Class:     sun_security_mscapi_CKeyPairGenerator_RSA
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1264
 * Method:    generateCKeyPair
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1265
 * Signature: (Ljava/lang/String;ILjava/lang/String;)Lsun/security/mscapi/CKeyPair;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1267
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_CKeyPairGenerator_00024RSA_generateCKeyPair
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1268
  (JNIEnv *env, jclass clazz, jstring alg, jint keySize, jstring keyContainerName)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    HCRYPTKEY hKeyPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    DWORD dwFlags = (keySize << 16) | CRYPT_EXPORTABLE;
9674
5d8476e6e47a 6987652: VM crashed in sun.security.mscapi.RSAKeyPairGenerator.generateRSAKeyPair(...)
vinnie
parents: 9533
diff changeset
  1273
    jobject keypair = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    const char* pszKeyContainerName = NULL; // UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1278
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1279
            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
  1280
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1281
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        // Acquire a CSP context (create a new key container).
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1284
        // 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
  1285
        // for SHA-2-based signatures.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1286
        if (::CryptAcquireContext( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            NULL,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1290
            PROV_RSA_AES,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1293
            // Failover to using the default CSP (PROV_RSA_FULL)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1294
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1295
            if (::CryptAcquireContext( //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1296
                &hCryptProv,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1297
                pszKeyContainerName,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1298
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1299
                PROV_RSA_FULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1300
                CRYPT_NEWKEYSET) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1301
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1302
                ThrowException(env, KEY_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1303
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1304
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1307
        // Generate an keypair
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1308
        if(::CryptGenKey( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
           hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
           AT_KEYEXCHANGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
           dwFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
           &hKeyPair) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1318
        // Get the method ID for the CKeyPair constructor
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1319
        jclass clazzCKeyPair =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1320
            env->FindClass("sun/security/mscapi/CKeyPair");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1321
        if (clazzCKeyPair == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1322
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1323
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1325
        jmethodID mNewCKeyPair =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1326
            env->GetMethodID(clazzCKeyPair, "<init>", "(Ljava/lang/String;JJI)V");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1327
        if (mNewCKeyPair == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1328
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1329
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1331
        // Create a new keypair
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1332
        keypair = env->NewObject(clazzCKeyPair, mNewCKeyPair,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1333
            alg, (jlong) hCryptProv, (jlong) hKeyPair, keySize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    return keypair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1349
 * Class:     sun_security_mscapi_CKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
 * Method:    getContainerName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
 * Signature: (J)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1353
JNIEXPORT jstring JNICALL Java_sun_security_mscapi_CKey_getContainerName
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
  (JNIEnv *env, jclass jclazz, jlong hCryptProv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    DWORD cbData = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    BYTE pbData[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    pbData[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1360
    ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        (HCRYPTPROV)hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        (BYTE *)pbData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        &cbData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    return env->NewStringUTF((const char*)pbData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1371
 * Class:     sun_security_mscapi_CKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
 * Method:    getKeyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
 * Signature: (J)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1375
JNIEXPORT jstring JNICALL Java_sun_security_mscapi_CKey_getKeyType
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
  (JNIEnv *env, jclass jclazz, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    ALG_ID dwAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    DWORD dwAlgIdLen = sizeof(ALG_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1381
    if (::CryptGetKeyParam((HCRYPTKEY) hCryptKey, KP_ALGID, (BYTE*)&dwAlgId, &dwAlgIdLen, 0)) { //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        if (CALG_RSA_SIGN == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            return env->NewStringUTF("Signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        } else if (CALG_RSA_KEYX == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            return env->NewStringUTF("Exchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            char buffer[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            if (sprintf(buffer, "%lu", dwAlgId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                return env->NewStringUTF(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    return env->NewStringUTF("<Unknown>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1401
 * Class:     sun_security_mscapi_CKeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
 * Method:    storeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
 * Signature: (Ljava/lang/String;Ljava/lang/String;[BIJJ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1405
JNIEXPORT void JNICALL Java_sun_security_mscapi_CKeyStore_storeCertificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        jbyteArray jCertEncoding, jint jCertEncodingSize, jlong hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    PWCHAR pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    jbyte* pbCertEncoding = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    const jchar* jCertAliasChars = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    const char* pszContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    const char* pszProviderName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    WCHAR * pwszContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    WCHAR * pwszProviderName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        // 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
  1424
        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
  1425
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1426
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1427
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        // 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
  1434
        pbCertEncoding = new (env) jbyte[jCertEncodingSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1435
        if (pbCertEncoding == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1436
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1437
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        // Create a certificate context from the encoded cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            (BYTE*) pbCertEncoding, jCertEncodingSize))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            ThrowException(env, CERTIFICATE_PARSING_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        // Set the certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        int size = env->GetStringLength(jCertAliasName);
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1450
        pszCertAliasName = new (env) WCHAR[size + 1];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1451
        if (pszCertAliasName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1452
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1453
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        jCertAliasChars = env->GetStringChars(jCertAliasName, NULL);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1456
        if (jCertAliasChars == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1457
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1458
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        memcpy(pszCertAliasName, jCertAliasChars, size * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        pszCertAliasName[size] = 0; // append the string terminator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        CRYPT_DATA_BLOB friendlyName = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            sizeof(WCHAR) * (size + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            (BYTE *) pszCertAliasName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        env->ReleaseStringChars(jCertAliasName, jCertAliasChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        if (! ::CertSetCertificateContextProperty(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            CERT_FRIENDLY_NAME_PROP_ID, 0, &friendlyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        // Attach the certificate's private key (if supplied)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        if (hCryptProv != 0 && hCryptKey != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            CRYPT_KEY_PROV_INFO keyProviderInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            DWORD dwDataLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            // Get the name of the key container
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1483
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1494
            pszContainerName = new (env) char[dwDataLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1495
            if (pszContainerName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1496
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1497
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1499
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                (BYTE *) pszContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
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
            // 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
  1511
            pwszContainerName = new (env) WCHAR[dwDataLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1512
            if (pwszContainerName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1513
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1514
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            if (mbstowcs(pwszContainerName, pszContainerName, dwDataLen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            // Set the name of the key container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            keyProviderInfo.pwszContainerName = pwszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            // Get the name of the provider
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1526
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                PP_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1537
            pszProviderName = new (env) char[dwDataLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1538
            if (pszProviderName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1539
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1540
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1542
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                PP_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                (BYTE *) pszProviderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            // 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
  1554
            pwszProviderName = new (env) WCHAR[dwDataLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1555
            if (pwszProviderName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1556
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1557
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            if (mbstowcs(pwszProviderName, pszProviderName, dwDataLen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            // Set the name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            keyProviderInfo.pwszProvName = pwszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            // Get and set the type of the provider
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1568
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                PP_PROVTYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                (LPBYTE) &keyProviderInfo.dwProvType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            // Set no provider flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            keyProviderInfo.dwFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            // Set no provider parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            keyProviderInfo.cProvParam = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            keyProviderInfo.rgProvParam = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            // Get the key's algorithm ID
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1587
            if (! ::CryptGetKeyParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                (HCRYPTKEY) hCryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                KP_ALGID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                (LPBYTE) &keyProviderInfo.dwKeySpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            // Set the key spec (using the algorithm ID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            switch (keyProviderInfo.dwKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            case CALG_RSA_KEYX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            case CALG_DH_SF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                keyProviderInfo.dwKeySpec = AT_KEYEXCHANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            case CALG_RSA_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            case CALG_DSS_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                keyProviderInfo.dwKeySpec = AT_SIGNATURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_ALGID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            if (! ::CertSetCertificateContextProperty(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                CERT_KEY_PROV_INFO_PROP_ID, 0, &keyProviderInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        // Import encoded certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        if (!::CertAddCertificateContextToStore(hCertStore, pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            CERT_STORE_ADD_REPLACE_EXISTING, NULL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        if (pbCertEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            delete [] pbCertEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            delete [] pszCertAliasName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        if (pszContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            delete [] pszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        if (pwszContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            delete [] pwszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        if (pszProviderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            delete [] pszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        if (pwszProviderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            delete [] pwszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        if (pCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            ::CertFreeCertificateContext(pCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1666
 * Class:     sun_security_mscapi_CKeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
 * Method:    removeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
 * Signature: (Ljava/lang/String;Ljava/lang/String;[BI)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1670
JNIEXPORT void JNICALL Java_sun_security_mscapi_CKeyStore_removeCertificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
  jbyteArray jCertEncoding, jint jCertEncodingSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    const char* pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    PCCERT_CONTEXT pTBDCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
    jbyte* pbCertEncoding = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    DWORD cchNameString = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    char* pszNameString = NULL; // certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    BOOL bDeleteAttempted = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        // 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
  1687
        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
  1688
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1689
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1690
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        // 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
  1697
        pbCertEncoding = new (env) jbyte[jCertEncodingSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1698
        if (pbCertEncoding == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1699
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1700
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        // Create a certificate context from the encoded cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            (BYTE*) pbCertEncoding, jCertEncodingSize))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            ThrowException(env, CERTIFICATE_PARSING_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        // Find the certificate to be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        if (!(pTBDCertContext = ::CertFindCertificateInStore(hCertStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            X509_ASN_ENCODING, 0, CERT_FIND_EXISTING, pCertContext, NULL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        // Check that its friendly name matches the supplied alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        if ((cchNameString = ::CertGetNameString(pTBDCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1723
            pszNameString = new (env) char[cchNameString];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1724
            if (pszNameString == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1725
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1726
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            ::CertGetNameString(pTBDCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                cchNameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            // 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
  1733
            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
  1734
                == NULL) {
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
            if (strcmp(pszCertAliasName, pszNameString) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                // Only delete the certificate if the alias names matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                if (! ::CertDeleteCertificateFromStore(pTBDCertContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                    // pTBDCertContext is always freed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                    //  CertDeleteCertificateFromStore method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                    bDeleteAttempted = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                    ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                    __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            env->ReleaseStringUTFChars(jCertAliasName, pszCertAliasName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        if (pbCertEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            delete [] pbCertEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        if (pszNameString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        if (pCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            ::CertFreeCertificateContext(pCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        if (bDeleteAttempted && pTBDCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            ::CertFreeCertificateContext(pTBDCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1782
 * Class:     sun_security_mscapi_CKeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
 * Method:    destroyKeyContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
 * Signature: (Ljava/lang/String;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1786
JNIEXPORT void JNICALL Java_sun_security_mscapi_CKeyStore_destroyKeyContainer
50471
f0aeede1b855 8204572: SetupJdkLibrary should setup SRC and -I flags automatically
ihse
parents: 49151
diff changeset
  1787
  (JNIEnv *env, jobject clazz, jstring keyContainerName)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    const char* pszKeyContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1794
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1795
            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
  1796
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1797
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        // Destroying the default key container is not permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        // (because it may contain more one keypair).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        if (pszKeyContainerName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_KEYSET_PARAM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        // Acquire a CSP context (to the key container).
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1808
        if (::CryptAcquireContext( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            CRYPT_DELETEKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1831
 * Class:     sun_security_mscapi_CRSACipher
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
 * Method:    encryptDecrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
 * Signature: ([BIJZ)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1835
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CRSACipher_encryptDecrypt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
  (JNIEnv *env, jclass clazz, jbyteArray jData, jint jDataSize, jlong hKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
   jboolean doEncrypt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
    jbyteArray result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
    jbyte* pData = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    DWORD dwDataLen = jDataSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    DWORD dwBufLen = env->GetArrayLength(jData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    DWORD i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    BYTE tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        // 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
  1849
        pData = new (env) jbyte[dwBufLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1850
        if (pData == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1851
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1852
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        env->GetByteArrayRegion(jData, 0, dwBufLen, pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        if (doEncrypt == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            // encrypt
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1857
            if (! ::CryptEncrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData, //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                &dwDataLen, dwBufLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            dwBufLen = dwDataLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            // convert from little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            for (i = 0; i < dwBufLen / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                tmp = pData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                pData[i] = pData[dwBufLen - i -1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                pData[dwBufLen - i - 1] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            // convert to little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            for (i = 0; i < dwBufLen / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                tmp = pData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                pData[i] = pData[dwBufLen - i -1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                pData[dwBufLen - i - 1] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            // decrypt
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1880
            if (! ::CryptDecrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData, //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                &dwBufLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        // Create new byte array
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1889
        if ((result = env->NewByteArray(dwBufLen)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1890
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1891
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        // Copy data from native buffer to Java buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        env->SetByteArrayRegion(result, 0, dwBufLen, (jbyte*) pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        if (pData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            delete [] pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1906
 * Class:     sun_security_mscapi_CPublicKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
 * Method:    getPublicKeyBlob
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1908
 * Signature: (JJ)[B
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1910
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CPublicKey_getPublicKeyBlob
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1911
    (JNIEnv *env, jobject clazz, jlong hCryptProv, jlong hCryptKey) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    jbyteArray blob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    DWORD dwBlobLen;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
  1915
    BYTE* pbKeyBlob = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        // Determine the size of the blob
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1921
        if (hCryptKey == 0) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1922
            SS_CHECK(::NCryptExportKey(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1923
                hCryptProv, NULL, BCRYPT_ECCPUBLIC_BLOB,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1924
                NULL, NULL, 0, &dwBlobLen, NCRYPT_SILENT_FLAG));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1925
        } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1926
            if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, NULL, //deprecated
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1927
                &dwBlobLen)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1929
                ThrowException(env, KEY_EXCEPTION, GetLastError());
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1930
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1931
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1934
        pbKeyBlob = new (env) BYTE[dwBlobLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1935
        if (pbKeyBlob == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1936
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1937
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        // Generate key blob
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1940
        if (hCryptKey == 0) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1941
            SS_CHECK(::NCryptExportKey(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1942
                hCryptProv, NULL, BCRYPT_ECCPUBLIC_BLOB,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1943
                NULL, pbKeyBlob, dwBlobLen, &dwBlobLen, NCRYPT_SILENT_FLAG));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1944
        } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1945
            if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, //deprecated
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1946
                pbKeyBlob, &dwBlobLen)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1948
                ThrowException(env, KEY_EXCEPTION, GetLastError());
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1949
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1950
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        // Create new byte array
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1954
        if ((blob = env->NewByteArray(dwBlobLen)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1955
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1956
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        // Copy data from native buffer to Java buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        env->SetByteArrayRegion(blob, 0, dwBlobLen, (jbyte*) pbKeyBlob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            delete [] pbKeyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
    return blob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1971
 * Class:     sun_security_mscapi_CPublicKey_CRSAPublicKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
 * Method:    getExponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
 * Signature: ([B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1975
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CPublicKey_00024CRSAPublicKey_getExponent
50471
f0aeede1b855 8204572: SetupJdkLibrary should setup SRC and -I flags automatically
ihse
parents: 49151
diff changeset
  1976
    (JNIEnv *env, jobject clazz, jbyteArray jKeyBlob) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    jbyteArray exponent = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    jbyte*     exponentBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    jbyte*     keyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        jsize length = env->GetArrayLength(jKeyBlob);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1985
        jsize headerLength = sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY);
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1986
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1987
        if (length < headerLength) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1988
            ThrowExceptionWithMessage(env, KEY_EXCEPTION, "Invalid BLOB");
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1989
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1990
        }
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1991
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1992
        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
  1993
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1994
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        // Check BLOB type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        RSAPUBKEY* pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC));
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2006
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        int len = sizeof(pRsaPubKey->pubexp);
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2008
        exponentBytes = new (env) jbyte[len];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2009
        if (exponentBytes == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2010
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2011
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        // convert from little-endian while copying from blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        for (int i = 0, j = len - 1; i < len; i++, j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            exponentBytes[i] = ((BYTE*) &pRsaPubKey->pubexp)[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2018
        if ((exponent = env->NewByteArray(len)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2019
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2020
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        env->SetByteArrayRegion(exponent, 0, len, exponentBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        if (keyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        if (exponentBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            delete [] exponentBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
    return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2036
 * Class:     sun_security_mscapi_CPublicKey_CRSAPublicKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
 * Method:    getModulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
 * Signature: ([B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2040
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CPublicKey_00024CRSAPublicKey_getModulus
50471
f0aeede1b855 8204572: SetupJdkLibrary should setup SRC and -I flags automatically
ihse
parents: 49151
diff changeset
  2041
    (JNIEnv *env, jobject clazz, jbyteArray jKeyBlob) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
    jbyteArray modulus = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    jbyte*     modulusBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
    jbyte*     keyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        jsize length = env->GetArrayLength(jKeyBlob);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2050
        jsize headerLength = sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY);
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2051
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2052
        if (length < headerLength) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2053
            ThrowExceptionWithMessage(env, KEY_EXCEPTION, "Invalid BLOB");
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2054
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2055
        }
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2056
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2057
        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
  2058
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2059
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        // Check BLOB type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        RSAPUBKEY* pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC));
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2071
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        int len = pRsaPubKey->bitlen / 8;
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2073
        if (len < 0 || len > length - headerLength) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2074
            ThrowExceptionWithMessage(env, KEY_EXCEPTION, "Invalid key length");
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2075
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2076
        }
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2077
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2078
        modulusBytes = new (env) jbyte[len];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2079
        if (modulusBytes == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2080
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2081
        }
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2082
        BYTE * pbModulus = (BYTE *) (keyBlob + headerLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        // convert from little-endian while copying from blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        for (int i = 0, j = len - 1; i < len; i++, j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            modulusBytes[i] = pbModulus[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2089
        if ((modulus = env->NewByteArray(len)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2090
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2091
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        env->SetByteArrayRegion(modulus, 0, len, modulusBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        if (keyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
            env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        if (modulusBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
            delete [] modulusBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
    return modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
 * Convert an array in big-endian byte order into little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
int convertToLittleEndian(JNIEnv *env, jbyteArray source, jbyte* destination,
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2110
        int destinationLength) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2111
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2112
    int result = -1;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2113
    jbyte* sourceBytes = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2115
    __try {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2116
        int sourceLength = env->GetArrayLength(source);
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2117
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2118
        sourceBytes = env->GetByteArrayElements(source, 0);
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2119
        if (sourceBytes == NULL) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2120
            __leave;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2121
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2123
        int copyLen = sourceLength;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2124
        if (sourceLength > destinationLength) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2125
            // source might include an extra sign byte
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2126
            if (sourceLength == destinationLength + 1 && sourceBytes[0] == 0) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2127
                copyLen--;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2128
            } else {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2129
                __leave;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2130
            }
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2131
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2133
        // Copy bytes from the end of the source array to the beginning of the
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2134
        // destination array (until the destination array is full).
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2135
        // This ensures that the sign byte from the source array will be excluded.
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2136
        for (int i = 0; i < copyLen; i++) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2137
            destination[i] = sourceBytes[sourceLength - 1 - i];
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2138
        }
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2139
        if (copyLen < destinationLength) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2140
            memset(destination + copyLen, 0, destinationLength - copyLen);
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2141
        }
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2142
        result = destinationLength;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2143
    } __finally {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2144
        // Clean up.
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2145
        if (sourceBytes) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2146
            env->ReleaseByteArrayElements(source, sourceBytes, JNI_ABORT);
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  2147
        }
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  2148
    }
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  2149
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2150
    return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
 * The Microsoft Base Cryptographic Provider supports public-key BLOBs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
 * that have the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
 *     PUBLICKEYSTRUC publickeystruc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
 *     RSAPUBKEY rsapubkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
 *     BYTE modulus[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
 * and private-key BLOBs that have the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
 *     PUBLICKEYSTRUC publickeystruc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
 *     RSAPUBKEY rsapubkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
 *     BYTE modulus[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
 *     BYTE prime1[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
 *     BYTE prime2[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
 *     BYTE exponent1[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
 *     BYTE exponent2[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
 *     BYTE coefficient[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
 *     BYTE privateExponent[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
 * This method generates such BLOBs from the key elements supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
jbyteArray generateKeyBlob(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        jbyteArray jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        jbyteArray jPrivateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        jbyteArray jPrimeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        jbyteArray jPrimeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        jbyteArray jExponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        jbyteArray jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        jbyteArray jCrtCoefficient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    jsize jKeyByteLength = jKeyBitLength / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    jsize jBlobLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
    BOOL bGeneratePrivateKeyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
    // Determine whether to generate a public-key or a private-key BLOB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
    if (jPrivateExponent != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        jPrimeP != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        jPrimeQ != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        jExponentP != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        jExponentQ != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        jCrtCoefficient != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        bGeneratePrivateKeyBlob = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        jBlobLength = sizeof(BLOBHEADER) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                        sizeof(RSAPUBKEY) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                        ((jKeyBitLength / 8) * 4) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                        (jKeyBitLength / 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
        bGeneratePrivateKeyBlob = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        jBlobLength = sizeof(BLOBHEADER) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                        sizeof(RSAPUBKEY) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                        (jKeyBitLength / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2212
    jbyte* jBlobBytes = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    jbyte* jBlobElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
    jbyteArray jBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
    jsize  jElementLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    __try {
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2218
        jBlobBytes = new (env) jbyte[jBlobLength];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2219
        if (jBlobBytes == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2220
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2221
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        BLOBHEADER *pBlobHeader = (BLOBHEADER *) jBlobBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
            pBlobHeader->bType = PRIVATEKEYBLOB;  // 0x07
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            pBlobHeader->bType = PUBLICKEYBLOB;   // 0x06
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
        pBlobHeader->bVersion = CUR_BLOB_VERSION; // 0x02
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        pBlobHeader->reserved = 0;                // 0x0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
        pBlobHeader->aiKeyAlg = CALG_RSA_KEYX;    // 0x0000a400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        RSAPUBKEY *pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
            (RSAPUBKEY *) (jBlobBytes + sizeof(PUBLICKEYSTRUC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            pRsaPubKey->magic = 0x32415352;       // "RSA2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            pRsaPubKey->magic = 0x31415352;       // "RSA1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        pRsaPubKey->bitlen = jKeyBitLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        pRsaPubKey->pubexp = 0; // init
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
        // Sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
        jsize jPublicExponentLength = env->GetArrayLength(jPublicExponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        if (jPublicExponentLength > sizeof(pRsaPubKey->pubexp)) {
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
  2246
            ThrowException(env, INVALID_KEY_EXCEPTION, NTE_BAD_TYPE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        // The length argument must be the smaller of jPublicExponentLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        // 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
  2251
        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
  2252
            (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
  2253
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2254
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        // Modulus n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        jBlobElement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            (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
  2259
        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
  2260
            jKeyByteLength)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2261
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2262
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            // Prime p
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2267
            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
  2268
                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
  2269
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2270
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            // Prime q
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2274
            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
  2275
                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
  2276
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2277
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            // Prime exponent p
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2281
            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
  2282
                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
  2283
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2284
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            // Prime exponent q
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2288
            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
  2289
                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
  2290
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2291
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
            // CRT coefficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2295
            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
  2296
                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
  2297
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2298
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            // Private exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2302
            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
  2303
                jBlobElement, jKeyByteLength)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2304
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2305
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2308
        if ((jBlob = env->NewByteArray(jBlobLength)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2309
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2310
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
        env->SetByteArrayRegion(jBlob, 0, jBlobLength, jBlobBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        if (jBlobBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            delete [] jBlobBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    return jBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2324
 * Class:     sun_security_mscapi_CKeyStore
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2325
 * Method:    generateRSAPrivateKeyBlob
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
 * Signature: (I[B[B[B[B[B[B[B[B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2328
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CKeyStore_generateRSAPrivateKeyBlob
50471
f0aeede1b855 8204572: SetupJdkLibrary should setup SRC and -I flags automatically
ihse
parents: 49151
diff changeset
  2329
    (JNIEnv *env, jobject clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
        jbyteArray jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        jbyteArray jPrivateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        jbyteArray jPrimeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        jbyteArray jPrimeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
        jbyteArray jExponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        jbyteArray jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        jbyteArray jCrtCoefficient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
    return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        jPrivateExponent, jPrimeP, jPrimeQ, jExponentP, jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        jCrtCoefficient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2346
 * Class:     sun_security_mscapi_CSignature_RSA
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
 * Method:    generatePublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
 * Signature: (I[B[B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2350
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CSignature_00024RSA_generatePublicKeyBlob
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
    (JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        jbyteArray jPublicExponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
    return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        NULL, NULL, NULL, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2361
 * Class:     sun_security_mscapi_CKeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
 * Method:    storePrivateKey
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2363
 * Signature: (Ljava/lang/String;[BLjava/lang/String;I)Lsun/security/mscapi/CPrivateKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2365
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_CKeyStore_storePrivateKey
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2366
    (JNIEnv *env, jobject clazz, jstring alg, jbyteArray keyBlob,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2367
     jstring keyContainerName, jint keySize)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    DWORD dwBlobLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    BYTE * pbKeyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    const char* pszKeyContainerName = NULL; // UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
    jobject privateKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2378
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2379
            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
  2380
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2381
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        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
  2383
        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
  2384
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2385
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2386
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        // Acquire a CSP context (create a new key container).
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2389
        if (::CryptAcquireContext( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
            PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
            CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        // Import the private key
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2401
        if (::CryptImportKey( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
            pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            dwBlobLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            CRYPT_EXPORTABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            &hKey) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2413
        // Get the method ID for the CPrivateKey constructor
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2414
        jclass clazzCPrivateKey =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2415
            env->FindClass("sun/security/mscapi/CPrivateKey");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2416
        if (clazzCPrivateKey == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2417
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2418
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2420
        jmethodID mNewCPrivateKey =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2421
            env->GetStaticMethodID(clazzCPrivateKey, "of",
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2422
            "(Ljava/lang/String;JJI)Lsun/security/mscapi/CPrivateKey;");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2423
        if (mNewCPrivateKey == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2424
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2425
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2427
        // Create a new private key
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2428
        privateKey = env->CallStaticObjectMethod(clazzCPrivateKey, mNewCPrivateKey,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2429
            alg, (jlong) hCryptProv, (jlong) hKey, keySize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            env->ReleaseByteArrayElements(keyBlob, (jbyte *) pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2449
 * Class:     sun_security_mscapi_CSignature
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2450
 * Method:    importECPublicKey
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2451
 * Signature: (Ljava/lang/String;[BI)Lsun/security/mscapi/CPublicKey;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2452
 */
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2453
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_CSignature_importECPublicKey
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2454
    (JNIEnv *env, jclass clazz, jstring alg, jbyteArray keyBlob, jint keySize)
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2455
{
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2456
    BCRYPT_ALG_HANDLE hSignAlg = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2457
    NCRYPT_KEY_HANDLE       hTmpKey         = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2458
    DWORD dwBlobLen;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2459
    BYTE * pbKeyBlob = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2460
    jobject publicKey = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2461
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2462
    __try
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2463
    {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2464
        dwBlobLen = env->GetArrayLength(keyBlob);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2465
        if ((pbKeyBlob = (BYTE *) env->GetByteArrayElements(keyBlob, 0))
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2466
            == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2467
            __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2468
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2469
        dump("NCryptImportKey", pbKeyBlob, dwBlobLen);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2470
        NCRYPT_PROV_HANDLE hProv;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2471
        SS_CHECK(NCryptOpenStorageProvider(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2472
                &hProv, L"Microsoft Software Key Storage Provider", 0 ));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2473
        SS_CHECK(NCryptImportKey(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2474
                                                    hProv,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2475
                                                    NULL,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2476
                                                    BCRYPT_ECCPUBLIC_BLOB,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2477
                                                    NULL,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2478
                                                    &hTmpKey,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2479
                                                    pbKeyBlob,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2480
                                                    dwBlobLen,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2481
                                                    0));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2482
        NCryptFreeObject( hProv );
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2483
        // Get the method ID for the CPublicKey constructor
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2484
        jclass clazzCPublicKey =
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2485
            env->FindClass("sun/security/mscapi/CPublicKey");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2486
        if (clazzCPublicKey == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2487
            __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2488
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2489
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2490
        jmethodID mNewCPublicKey =
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2491
            env->GetStaticMethodID(clazzCPublicKey, "of",
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2492
            "(Ljava/lang/String;JJI)Lsun/security/mscapi/CPublicKey;");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2493
        if (mNewCPublicKey == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2494
            __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2495
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2496
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2497
        // Create a new public key
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2498
        publicKey = env->CallStaticObjectMethod(clazzCPublicKey, mNewCPublicKey,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2499
            alg, (jlong) hTmpKey, (jlong) 0, keySize);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2500
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2501
    __finally
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2502
    {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2503
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2504
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2505
    return publicKey;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2506
}
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2507
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2508
/*
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2509
 * Class:     sun_security_mscapi_CSignature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
 * Method:    importPublicKey
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2511
 * Signature: (Ljava/lang/String;[BI)Lsun/security/mscapi/CPublicKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2513
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_CSignature_importPublicKey
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2514
    (JNIEnv *env, jclass clazz, jstring alg, jbyteArray keyBlob, jint keySize)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    DWORD dwBlobLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    BYTE * pbKeyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
    jobject publicKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
        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
  2525
        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
  2526
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2527
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2528
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
        // Acquire a CSP context (create a new key container).
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2531
        // 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
  2532
        // for SHA-2-based signatures.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2533
        if (::CryptAcquireContext( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
            NULL,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2537
            PROV_RSA_AES,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
            CRYPT_VERIFYCONTEXT) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2540
            // Failover to using the default CSP (PROV_RSA_FULL)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2541
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2542
            if (::CryptAcquireContext( //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2543
                &hCryptProv,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2544
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2545
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2546
                PROV_RSA_FULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2547
                CRYPT_VERIFYCONTEXT) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2548
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2549
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2550
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2551
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        // Import the public key
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2555
        if (::CryptImportKey( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
            hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
            pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
            dwBlobLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            CRYPT_EXPORTABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
            &hKey) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2567
        // Get the method ID for the CPublicKey constructor
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2568
        jclass clazzCPublicKey =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2569
            env->FindClass("sun/security/mscapi/CPublicKey");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2570
        if (clazzCPublicKey == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2571
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2572
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2574
        jmethodID mNewCPublicKey =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2575
            env->GetStaticMethodID(clazzCPublicKey, "of",
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2576
            "(Ljava/lang/String;JJI)Lsun/security/mscapi/CPublicKey;");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2577
        if (mNewCPublicKey == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2578
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2579
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2581
        // Create a new public key
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2582
        publicKey = env->CallStaticObjectMethod(clazzCPublicKey, mNewCPublicKey,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2583
            alg, (jlong) hCryptProv, (jlong) hKey, keySize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            env->ReleaseByteArrayElements(keyBlob, (jbyte *) pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
    return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
} /* extern "C" */