src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp
author jvernee
Thu, 14 Nov 2019 10:55:46 +0100
changeset 59075 355f4f42dda5
parent 54827 01fa7f06f806
permissions -rw-r--r--
8233920: MethodHandles::tryFinally generates illegal bytecode for long/double return type Reviewed-by: redestad, vlivanov, jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
     2
 * Copyright (c) 2005, 2019, 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) {
54287
c02b8d6384ab 8221407: Windows 32bit build error in libsunmscapi/security.cpp
stuefe
parents: 53333
diff changeset
   505
                        PP("CNG %I64d", (__int64)hCryptProv);
53007
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
54287
c02b8d6384ab 8221407: Windows 32bit build error in libsunmscapi/security.cpp
stuefe
parents: 53333
diff changeset
   520
                        PP("CAPI %I64d %I64d", (__int64)hCryptProv, (__int64)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) {
54827
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   652
                                    jstring name = env->NewStringUTF(pszNameString);
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   653
                                    if (name == NULL) {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   654
                                        __leave;
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   655
                                    }
53333
henryjen
parents: 53330 53007
diff changeset
   656
                                    if (buffer[0] == 'E' && buffer[2] == 'C'
henryjen
parents: 53330 53007
diff changeset
   657
                                            && (dwPublicKeyLength == 256
henryjen
parents: 53330 53007
diff changeset
   658
                                                    || dwPublicKeyLength == 384
henryjen
parents: 53330 53007
diff changeset
   659
                                                    || dwPublicKeyLength == 521)) {
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);
54827
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   665
                                    } else if (buffer[0] == 'R' && buffer[2] == 'S'
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   666
                                            && buffer[4] == 'A') {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   667
                                        env->CallVoidMethod(obj, mGenKeyAndCertChain,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   668
                                            1,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   669
                                            name,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   670
                                            (jlong) hCryptProv, 0,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   671
                                            dwPublicKeyLength, jArrayList);
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   672
                                    } else {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   673
                                        dump("Unknown NCRYPT_ALGORITHM_PROPERTY", buffer, len);
53333
henryjen
parents: 53330 53007
diff changeset
   674
                                    }
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   675
                                }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   676
                            }
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 33653
diff changeset
   677
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                // Free cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                if (pCertChainContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    ::CertFreeCertificateChain(pCertChainContext);
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   684
            } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   685
                PP("GetCertificateChain failed %d", GetLastError());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (pszNameString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
/*
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   704
 * Class:     sun_security_mscapi_CKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
 * Method:    cleanUp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
 * Signature: (JJ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
 */
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   708
JNIEXPORT void JNICALL Java_sun_security_mscapi_CKey_cleanUp
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
  (JNIEnv *env, jclass clazz, jlong hCryptProv, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
{
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   711
    if (hCryptKey == NULL && hCryptProv != NULL) {
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   712
        NCryptFreeObject((NCRYPT_HANDLE)hCryptProv);
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   713
    } else {
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   714
        if (hCryptKey != NULL)
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   715
            ::CryptDestroyKey((HCRYPTKEY) hCryptKey); // deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
54639
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   717
        if (hCryptProv != NULL)
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   718
            ::CryptReleaseContext((HCRYPTPROV) hCryptProv, NULL); // deprecated
2cac7d48db4c 8223003: SunMSCAPI keys are not cleaned up
weijun
parents: 54287
diff changeset
   719
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   723
 * Class:     sun_security_mscapi_CSignature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
 * Method:    signHash
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   725
 * Signature: (Z[BILjava/lang/String;JJ)[B
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   727
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
   728
  (JNIEnv *env, jclass clazz, jboolean noHashOID, jbyteArray jHash,
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   729
        jint jHashSize, jstring jHashAlgorithm, jlong hCryptProv,
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   730
        jlong hCryptKey)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    HCRYPTHASH hHash = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    jbyte* pHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    jbyte* pSignedHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    jbyteArray jSignedHash = NULL;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   736
    HCRYPTPROV hCryptProvAlt = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        // Map hash algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        ALG_ID algId = MapHashAlgorithm(env, jHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        // Acquire a hash object handle.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   744
        if (::CryptCreateHash(HCRYPTPROV(hCryptProv), algId, 0, 0, &hHash) == FALSE) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   746
            // Failover to using the PROV_RSA_AES CSP
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
            DWORD cbData = 256;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   749
            BYTE pbData[256];
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   750
            pbData[0] = '\0';
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
            // Get name of the key container
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   753
            ::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_CONTAINER, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   754
                (BYTE *)pbData, &cbData, 0);
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
            // Acquire an alternative CSP handle
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   757
            if (::CryptAcquireContext(&hCryptProvAlt, LPCSTR(pbData), NULL, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   758
                PROV_RSA_AES, 0) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   759
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   760
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   761
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   762
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   763
            }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   764
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   765
            // Acquire a hash object handle.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   766
            if (::CryptCreateHash(HCRYPTPROV(hCryptProvAlt), algId, 0, 0, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   767
                &hHash) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   768
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   769
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   770
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   771
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // 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
   775
        pHashBuffer = new (env) jbyte[jHashSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   776
        if (pHashBuffer == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   777
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   778
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        // Set hash value in the hash object
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   782
        if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*)pHashBuffer, NULL) == FALSE) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        // Determine key spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        DWORD dwKeySpec = AT_SIGNATURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        ALG_ID dwAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        DWORD dwAlgIdLen = sizeof(ALG_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   793
        if (! ::CryptGetKeyParam((HCRYPTKEY) hCryptKey, KP_ALGID, (BYTE*)&dwAlgId, &dwAlgIdLen, 0)) { //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        if (CALG_RSA_KEYX == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            dwKeySpec = AT_KEYEXCHANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        // Determine size of buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        DWORD dwBufLen = 0;
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   804
        DWORD dwFlags = 0;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   805
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   806
        if (noHashOID == JNI_TRUE) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   807
            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
   808
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   809
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   810
        if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, NULL, &dwBufLen) == FALSE) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   816
        pSignedHashBuffer = new (env) jbyte[dwBufLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   817
        if (pSignedHashBuffer == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   818
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
   819
        }
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   820
        if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, (BYTE*)pSignedHashBuffer, &dwBufLen) == FALSE) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        // Create new byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        jbyteArray temp = env->NewByteArray(dwBufLen);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   828
        if (temp == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   829
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   830
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        // Copy data from native buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        env->SetByteArrayRegion(temp, 0, dwBufLen, pSignedHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        jSignedHash = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        if (pSignedHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            delete [] pSignedHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (pHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            delete [] pHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        if (hHash)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   846
            ::CryptDestroyHash(hHash); //deprecated
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   847
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
   848
        if (hCryptProvAlt)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   849
            ::CryptReleaseContext(hCryptProvAlt, 0); // deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    return jSignedHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   856
 * Class:     sun_security_mscapi_CSignature
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   857
 * Method:    signCngHash
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   858
 * Signature: (I[BIILjava/lang/String;JJ)[B
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   859
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   860
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CSignature_signCngHash
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   861
  (JNIEnv *env, jclass clazz, jint type, jbyteArray jHash,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   862
        jint jHashSize, jint saltLen, jstring jHashAlgorithm, jlong hCryptProv,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   863
        jlong hCryptKey)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   864
{
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   865
    jbyteArray jSignedHash = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   866
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   867
    jbyte* pHashBuffer = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   868
    jbyte* pSignedHashBuffer = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   869
    NCRYPT_KEY_HANDLE hk = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   870
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   871
    __try
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   872
    {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   873
        if (hCryptKey == 0) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   874
            hk = (NCRYPT_KEY_HANDLE)hCryptProv;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   875
        } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   876
            SS_CHECK(::NCryptTranslateHandle(
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   877
                NULL,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   878
                &hk,
54287
c02b8d6384ab 8221407: Windows 32bit build error in libsunmscapi/security.cpp
stuefe
parents: 53333
diff changeset
   879
                (HCRYPTPROV)hCryptProv,
c02b8d6384ab 8221407: Windows 32bit build error in libsunmscapi/security.cpp
stuefe
parents: 53333
diff changeset
   880
                (HCRYPTKEY)hCryptKey,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   881
                NULL,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   882
                0));
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   883
        }
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   884
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   885
        // Copy hash from Java to native buffer
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   886
        pHashBuffer = new (env) jbyte[jHashSize];
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   887
        if (pHashBuffer == NULL) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   888
            __leave;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   889
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   890
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   891
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   892
        VOID* param;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   893
        DWORD dwFlags;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   894
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   895
        switch (type) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   896
        case 0:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   897
            param = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   898
            dwFlags = 0;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   899
            break;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   900
        case 1:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   901
            BCRYPT_PKCS1_PADDING_INFO pkcs1Info;
54827
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   902
            if (jHashAlgorithm) {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   903
                pkcs1Info.pszAlgId = MapHashIdentifier(env, jHashAlgorithm);
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   904
                if (pkcs1Info.pszAlgId == NULL) {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   905
                    ThrowExceptionWithMessage(env, SIGNATURE_EXCEPTION,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   906
                            "Unrecognised hash algorithm");
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   907
                    __leave;
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   908
                }
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   909
            } else {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
   910
                pkcs1Info.pszAlgId = NULL;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   911
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   912
            param = &pkcs1Info;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   913
            dwFlags = BCRYPT_PAD_PKCS1;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   914
            break;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   915
        case 2:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   916
            BCRYPT_PSS_PADDING_INFO pssInfo;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   917
            pssInfo.pszAlgId = MapHashIdentifier(env, jHashAlgorithm);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   918
            pssInfo.cbSalt = saltLen;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   919
            if (pssInfo.pszAlgId == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   920
                ThrowExceptionWithMessage(env, SIGNATURE_EXCEPTION,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   921
                        "Unrecognised hash algorithm");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   922
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   923
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   924
            param = &pssInfo;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   925
            dwFlags = BCRYPT_PAD_PSS;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   926
            break;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   927
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   928
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   929
        DWORD jSignedHashSize = 0;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   930
        SS_CHECK(::NCryptSignHash(
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   931
                hk,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   932
                param,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   933
                (BYTE*)pHashBuffer, jHashSize,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   934
                NULL, 0, &jSignedHashSize,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   935
                dwFlags
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   936
                ));
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   937
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   938
        pSignedHashBuffer = new (env) jbyte[jSignedHashSize];
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   939
        if (pSignedHashBuffer == NULL) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   940
            __leave;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   941
        }
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
        SS_CHECK(::NCryptSignHash(
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   944
                hk,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   945
                param,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   946
                (BYTE*)pHashBuffer, jHashSize,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   947
                (BYTE*)pSignedHashBuffer, jSignedHashSize, &jSignedHashSize,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   948
                dwFlags
50715
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
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   951
        // Create new byte array
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   952
        jbyteArray temp = env->NewByteArray(jSignedHashSize);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   953
        if (temp == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   954
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
   955
        }
50715
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
        // Copy data from native buffer
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   958
        env->SetByteArrayRegion(temp, 0, jSignedHashSize, pSignedHashBuffer);
50715
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
        jSignedHash = temp;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   961
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   962
    __finally
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   963
    {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   964
        if (pSignedHashBuffer)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   965
            delete [] pSignedHashBuffer;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   966
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   967
        if (pHashBuffer)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   968
            delete [] pHashBuffer;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   969
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   970
        if (hCryptKey != 0 && hk != NULL)
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   971
            ::NCryptFreeObject(hk);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   972
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   973
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   974
    return jSignedHash;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   975
}
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   976
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
   977
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   978
 * Class:     sun_security_mscapi_CSignature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
 * Method:    verifySignedHash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
 * Signature: ([BIL/java/lang/String;[BIJJ)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
   982
JNIEXPORT jboolean JNICALL Java_sun_security_mscapi_CSignature_verifySignedHash
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
  (JNIEnv *env, jclass clazz, jbyteArray jHash, jint jHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        jstring jHashAlgorithm, jbyteArray jSignedHash, jint jSignedHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        jlong hCryptProv, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    HCRYPTHASH hHash = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    jbyte* pHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    jbyte* pSignedHashBuffer = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    DWORD dwSignedHashBufferLen = jSignedHashSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    jboolean result = JNI_FALSE;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   992
    HCRYPTPROV hCryptProvAlt = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        // Map hash algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        ALG_ID algId = MapHashAlgorithm(env, jHashAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        // Acquire a hash object handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        if (::CryptCreateHash(HCRYPTPROV(hCryptProv), algId, 0, 0, &hHash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1003
            // Failover to using the PROV_RSA_AES CSP
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
            DWORD cbData = 256;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1006
            BYTE pbData[256];
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1007
            pbData[0] = '\0';
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
            // Get name of the key container
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1010
            ::CryptGetProvParam((HCRYPTPROV)hCryptProv, PP_CONTAINER, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1011
                (BYTE *)pbData, &cbData, 0);
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1012
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1013
            // Acquire an alternative CSP handle
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1014
            if (::CryptAcquireContext(&hCryptProvAlt, LPCSTR(pbData), NULL, //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1015
                PROV_RSA_AES, 0) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1016
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1017
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1018
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1019
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1020
            }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1021
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1022
            // Acquire a hash object handle.
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1023
            if (::CryptCreateHash(HCRYPTPROV(hCryptProvAlt), algId, 0, 0,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1024
                &hHash) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1025
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1026
                ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1027
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1028
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        // 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
  1032
        pHashBuffer = new (env) jbyte[jHashSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1033
        if (pHashBuffer == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1034
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1035
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        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
  1037
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1038
        pSignedHashBuffer = new (env) jbyte[jSignedHashSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1039
        if (pSignedHashBuffer == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1040
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1041
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        env->GetByteArrayRegion(jSignedHash, 0, jSignedHashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            pSignedHashBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        // Set hash value in the hash object
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1046
        if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*) pHashBuffer, NULL) //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            ThrowException(env, SIGNATURE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        // For RSA, the hash encryption algorithm is normally the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        // public key algorithm, so AT_SIGNATURE is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        // Verify the signature
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1057
        if (::CryptVerifySignatureA(hHash, (BYTE *) pSignedHashBuffer, //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            dwSignedHashBufferLen, (HCRYPTKEY) hCryptKey, NULL, 0) == TRUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            result = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        if (pSignedHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            delete [] pSignedHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (pHashBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            delete [] pHashBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        if (hHash)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1073
            ::CryptDestroyHash(hHash); //deprecated
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1074
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1075
        if (hCryptProvAlt)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1076
            ::CryptReleaseContext(hCryptProvAlt, 0); // deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1083
 * Class:     sun_security_mscapi_CSignature
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1084
 * Method:    verifyCngSignedHash
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1085
 * Signature: (I[BI[BIILjava/lang/String;JJ)Z
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1086
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1087
JNIEXPORT jboolean JNICALL Java_sun_security_mscapi_CSignature_verifyCngSignedHash
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1088
  (JNIEnv *env, jclass clazz, jint type,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1089
        jbyteArray jHash, jint jHashSize,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1090
        jbyteArray jSignedHash, jint jSignedHashSize,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1091
        jint saltLen, jstring jHashAlgorithm,
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1092
        jlong hCryptProv, jlong hCryptKey)
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1093
{
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1094
    jbyte* pHashBuffer = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1095
    jbyte* pSignedHashBuffer = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1096
    jboolean result = JNI_FALSE;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1097
    NCRYPT_KEY_HANDLE hk = NULL;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1098
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1099
    __try
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1100
    {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1101
        if (hCryptKey == 0) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1102
            hk = (NCRYPT_KEY_HANDLE)hCryptProv;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1103
        } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1104
            SS_CHECK(::NCryptTranslateHandle(
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1105
                NULL,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1106
                &hk,
54287
c02b8d6384ab 8221407: Windows 32bit build error in libsunmscapi/security.cpp
stuefe
parents: 53333
diff changeset
  1107
                (HCRYPTPROV)hCryptProv,
c02b8d6384ab 8221407: Windows 32bit build error in libsunmscapi/security.cpp
stuefe
parents: 53333
diff changeset
  1108
                (HCRYPTKEY)hCryptKey,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1109
                NULL,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1110
                0));
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1111
        }
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1112
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1113
        // Copy hash and signedHash from Java to native buffer
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1114
        pHashBuffer = new (env) jbyte[jHashSize];
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1115
        if (pHashBuffer == NULL) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1116
            __leave;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1117
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1118
        env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1119
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1120
        pSignedHashBuffer = new (env) jbyte[jSignedHashSize];
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1121
        if (pSignedHashBuffer == NULL) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1122
            __leave;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1123
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1124
        env->GetByteArrayRegion(jSignedHash, 0, jSignedHashSize,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1125
            pSignedHashBuffer);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1126
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1127
        VOID* param;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1128
        DWORD dwFlags;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1129
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1130
        switch (type) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1131
        case 0:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1132
            param = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1133
            dwFlags = 0;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1134
            break;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1135
        case 1:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1136
            BCRYPT_PKCS1_PADDING_INFO pkcs1Info;
54827
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
  1137
            if (jHashAlgorithm) {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
  1138
                pkcs1Info.pszAlgId = MapHashIdentifier(env, jHashAlgorithm);
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
  1139
                if (pkcs1Info.pszAlgId == NULL) {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
  1140
                    ThrowExceptionWithMessage(env, SIGNATURE_EXCEPTION,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
  1141
                            "Unrecognised hash algorithm");
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
  1142
                    __leave;
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
  1143
                }
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
  1144
            } else {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 54639
diff changeset
  1145
                pkcs1Info.pszAlgId = NULL;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1146
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1147
            param = &pkcs1Info;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1148
            dwFlags = NCRYPT_PAD_PKCS1_FLAG;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1149
            break;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1150
        case 2:
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1151
            BCRYPT_PSS_PADDING_INFO pssInfo;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1152
            pssInfo.pszAlgId = MapHashIdentifier(env, jHashAlgorithm);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1153
            pssInfo.cbSalt = saltLen;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1154
            if (pssInfo.pszAlgId == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1155
                ThrowExceptionWithMessage(env, SIGNATURE_EXCEPTION,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1156
                        "Unrecognised hash algorithm");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1157
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1158
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1159
            param = &pssInfo;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1160
            dwFlags = NCRYPT_PAD_PSS_FLAG;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1161
            break;
50715
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
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1164
        if (::NCryptVerifySignature(hk, param,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1165
                (BYTE *) pHashBuffer, jHashSize,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1166
                (BYTE *) pSignedHashBuffer, jSignedHashSize,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1167
                dwFlags) == ERROR_SUCCESS)
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1168
        {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1169
            result = JNI_TRUE;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1170
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1171
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1172
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1173
    __finally
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1174
    {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1175
        if (pSignedHashBuffer)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1176
            delete [] pSignedHashBuffer;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1177
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1178
        if (pHashBuffer)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1179
            delete [] pHashBuffer;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1180
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1181
        if (hCryptKey != 0 && hk != NULL)
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1182
            ::NCryptFreeObject(hk);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1183
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1184
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1185
    return result;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1186
}
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1187
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1188
#define DUMP_PROP(p) \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1189
    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
  1190
        sprintf(header, "%s %ls", #p, p); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1191
        dump(header, buffer, len); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1192
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1193
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1194
#define EXPORT_BLOB(p) \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1195
    desc.cBuffers = 0; \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1196
    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
  1197
        sprintf(header, "%s %ls (%ld)", #p, p, desc.cBuffers); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1198
        dump(header, buffer, len); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1199
        for (int i = 0; i < (int)desc.cBuffers; i++) { \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1200
            sprintf(header, "desc %ld", desc.pBuffers[i].BufferType); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1201
            dump(header, (PBYTE)desc.pBuffers[i].pvBuffer, desc.pBuffers[i].cbBuffer); \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1202
        } \
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1203
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1204
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1205
void showProperty(NCRYPT_HANDLE hKey) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1206
    char header[100];
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1207
    BYTE buffer[8192];
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1208
    DWORD len = 9;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1209
    NCryptBufferDesc desc;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1210
    DUMP_PROP(NCRYPT_ALGORITHM_GROUP_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1211
    DUMP_PROP(NCRYPT_ALGORITHM_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1212
    DUMP_PROP(NCRYPT_ASSOCIATED_ECDH_KEY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1213
    DUMP_PROP(NCRYPT_BLOCK_LENGTH_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1214
    DUMP_PROP(NCRYPT_CERTIFICATE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1215
    DUMP_PROP(NCRYPT_DH_PARAMETERS_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1216
    DUMP_PROP(NCRYPT_EXPORT_POLICY_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1217
    DUMP_PROP(NCRYPT_IMPL_TYPE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1218
    DUMP_PROP(NCRYPT_KEY_TYPE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1219
    DUMP_PROP(NCRYPT_KEY_USAGE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1220
    DUMP_PROP(NCRYPT_LAST_MODIFIED_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1221
    DUMP_PROP(NCRYPT_LENGTH_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1222
    DUMP_PROP(NCRYPT_LENGTHS_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1223
    DUMP_PROP(NCRYPT_MAX_NAME_LENGTH_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1224
    DUMP_PROP(NCRYPT_NAME_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1225
    DUMP_PROP(NCRYPT_PIN_PROMPT_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1226
    DUMP_PROP(NCRYPT_PIN_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1227
    DUMP_PROP(NCRYPT_PROVIDER_HANDLE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1228
    DUMP_PROP(NCRYPT_READER_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1229
    DUMP_PROP(NCRYPT_ROOT_CERTSTORE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1230
    DUMP_PROP(NCRYPT_SCARD_PIN_ID);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1231
    DUMP_PROP(NCRYPT_SCARD_PIN_INFO);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1232
    DUMP_PROP(NCRYPT_SECURE_PIN_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1233
    DUMP_PROP(NCRYPT_SECURITY_DESCR_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1234
    DUMP_PROP(NCRYPT_SECURITY_DESCR_SUPPORT_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1235
    DUMP_PROP(NCRYPT_SMARTCARD_GUID_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1236
    DUMP_PROP(NCRYPT_UI_POLICY_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1237
    DUMP_PROP(NCRYPT_UNIQUE_NAME_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1238
    DUMP_PROP(NCRYPT_USE_CONTEXT_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1239
    DUMP_PROP(NCRYPT_USE_COUNT_ENABLED_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1240
    DUMP_PROP(NCRYPT_USE_COUNT_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1241
    DUMP_PROP(NCRYPT_USER_CERTSTORE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1242
    DUMP_PROP(NCRYPT_VERSION_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1243
    DUMP_PROP(NCRYPT_WINDOW_HANDLE_PROPERTY);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1244
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1245
    EXPORT_BLOB(BCRYPT_DH_PRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1246
    EXPORT_BLOB(BCRYPT_DH_PUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1247
    EXPORT_BLOB(BCRYPT_DSA_PRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1248
    EXPORT_BLOB(BCRYPT_DSA_PUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1249
    EXPORT_BLOB(BCRYPT_ECCPRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1250
    EXPORT_BLOB(BCRYPT_ECCPUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1251
    EXPORT_BLOB(BCRYPT_PUBLIC_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1252
    EXPORT_BLOB(BCRYPT_PRIVATE_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1253
    EXPORT_BLOB(BCRYPT_RSAFULLPRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1254
    EXPORT_BLOB(BCRYPT_RSAPRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1255
    EXPORT_BLOB(BCRYPT_RSAPUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1256
    EXPORT_BLOB(LEGACY_DH_PRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1257
    EXPORT_BLOB(LEGACY_DH_PUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1258
    EXPORT_BLOB(LEGACY_DSA_PRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1259
    EXPORT_BLOB(LEGACY_DSA_PUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1260
    EXPORT_BLOB(LEGACY_RSAPRIVATE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1261
    EXPORT_BLOB(LEGACY_RSAPUBLIC_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1262
    EXPORT_BLOB(NCRYPT_CIPHER_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1263
    EXPORT_BLOB(NCRYPT_OPAQUETRANSPORT_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1264
    EXPORT_BLOB(NCRYPT_PKCS7_ENVELOPE_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1265
    //EXPORT_BLOB(NCRYPTBUFFER_CERT_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1266
    //EXPORT_BLOB(NCRYPT_PKCS8_PRIVATE_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1267
    BCryptBuffer bb;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1268
    bb.BufferType = NCRYPTBUFFER_PKCS_SECRET;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1269
    bb.cbBuffer = 18;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1270
    bb.pvBuffer = L"changeit";
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1271
    BCryptBufferDesc bbd;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1272
    bbd.ulVersion = 0;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1273
    bbd.cBuffers = 1;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1274
    bbd.pBuffers = &bb;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1275
    if(::NCryptExportKey(hKey, NULL, NCRYPT_PKCS8_PRIVATE_KEY_BLOB, NULL,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1276
            (PBYTE)buffer, 8192, &len, NCRYPT_SILENT_FLAG) == ERROR_SUCCESS) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1277
        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
  1278
        dump(header, buffer, len);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1279
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1280
    EXPORT_BLOB(NCRYPT_PROTECTED_KEY_BLOB);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1281
}
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1282
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50471
diff changeset
  1283
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1284
 * Class:     sun_security_mscapi_CKeyPairGenerator_RSA
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1285
 * Method:    generateCKeyPair
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1286
 * Signature: (Ljava/lang/String;ILjava/lang/String;)Lsun/security/mscapi/CKeyPair;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1288
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_CKeyPairGenerator_00024RSA_generateCKeyPair
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1289
  (JNIEnv *env, jclass clazz, jstring alg, jint keySize, jstring keyContainerName)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    HCRYPTKEY hKeyPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    DWORD dwFlags = (keySize << 16) | CRYPT_EXPORTABLE;
9674
5d8476e6e47a 6987652: VM crashed in sun.security.mscapi.RSAKeyPairGenerator.generateRSAKeyPair(...)
vinnie
parents: 9533
diff changeset
  1294
    jobject keypair = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    const char* pszKeyContainerName = NULL; // UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1299
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1300
            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
  1301
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1302
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        // Acquire a CSP context (create a new key container).
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1305
        // 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
  1306
        // for SHA-2-based signatures.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1307
        if (::CryptAcquireContext( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            NULL,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1311
            PROV_RSA_AES,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1314
            // Failover to using the default CSP (PROV_RSA_FULL)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1315
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1316
            if (::CryptAcquireContext( //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1317
                &hCryptProv,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1318
                pszKeyContainerName,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1319
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1320
                PROV_RSA_FULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1321
                CRYPT_NEWKEYSET) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1322
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1323
                ThrowException(env, KEY_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1324
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  1325
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1328
        // Generate an keypair
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1329
        if(::CryptGenKey( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
           hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
           AT_KEYEXCHANGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
           dwFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
           &hKeyPair) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1339
        // Get the method ID for the CKeyPair constructor
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1340
        jclass clazzCKeyPair =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1341
            env->FindClass("sun/security/mscapi/CKeyPair");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1342
        if (clazzCKeyPair == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1343
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1344
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1346
        jmethodID mNewCKeyPair =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1347
            env->GetMethodID(clazzCKeyPair, "<init>", "(Ljava/lang/String;JJI)V");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1348
        if (mNewCKeyPair == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1349
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  1350
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1352
        // Create a new keypair
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1353
        keypair = env->NewObject(clazzCKeyPair, mNewCKeyPair,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1354
            alg, (jlong) hCryptProv, (jlong) hKeyPair, keySize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    return keypair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1370
 * Class:     sun_security_mscapi_CKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
 * Method:    getContainerName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
 * Signature: (J)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1374
JNIEXPORT jstring JNICALL Java_sun_security_mscapi_CKey_getContainerName
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
  (JNIEnv *env, jclass jclazz, jlong hCryptProv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    DWORD cbData = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    BYTE pbData[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    pbData[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1381
    ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        (HCRYPTPROV)hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        (BYTE *)pbData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        &cbData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    return env->NewStringUTF((const char*)pbData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1392
 * Class:     sun_security_mscapi_CKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
 * Method:    getKeyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
 * Signature: (J)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1396
JNIEXPORT jstring JNICALL Java_sun_security_mscapi_CKey_getKeyType
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
  (JNIEnv *env, jclass jclazz, jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    ALG_ID dwAlgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    DWORD dwAlgIdLen = sizeof(ALG_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1402
    if (::CryptGetKeyParam((HCRYPTKEY) hCryptKey, KP_ALGID, (BYTE*)&dwAlgId, &dwAlgIdLen, 0)) { //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        if (CALG_RSA_SIGN == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            return env->NewStringUTF("Signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        } else if (CALG_RSA_KEYX == dwAlgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            return env->NewStringUTF("Exchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            char buffer[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            if (sprintf(buffer, "%lu", dwAlgId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                return env->NewStringUTF(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    return env->NewStringUTF("<Unknown>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1422
 * Class:     sun_security_mscapi_CKeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
 * Method:    storeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
 * Signature: (Ljava/lang/String;Ljava/lang/String;[BIJJ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1426
JNIEXPORT void JNICALL Java_sun_security_mscapi_CKeyStore_storeCertificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        jbyteArray jCertEncoding, jint jCertEncodingSize, jlong hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        jlong hCryptKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    PWCHAR pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    jbyte* pbCertEncoding = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    const jchar* jCertAliasChars = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    const char* pszContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    const char* pszProviderName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    WCHAR * pwszContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    WCHAR * pwszProviderName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        // 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
  1445
        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
  1446
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1447
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1448
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        // 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
  1455
        pbCertEncoding = new (env) jbyte[jCertEncodingSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1456
        if (pbCertEncoding == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1457
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1458
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        // Create a certificate context from the encoded cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            (BYTE*) pbCertEncoding, jCertEncodingSize))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            ThrowException(env, CERTIFICATE_PARSING_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        // Set the certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        int size = env->GetStringLength(jCertAliasName);
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1471
        pszCertAliasName = new (env) WCHAR[size + 1];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1472
        if (pszCertAliasName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1473
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1474
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        jCertAliasChars = env->GetStringChars(jCertAliasName, NULL);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1477
        if (jCertAliasChars == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1478
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1479
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        memcpy(pszCertAliasName, jCertAliasChars, size * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        pszCertAliasName[size] = 0; // append the string terminator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        CRYPT_DATA_BLOB friendlyName = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            sizeof(WCHAR) * (size + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            (BYTE *) pszCertAliasName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        env->ReleaseStringChars(jCertAliasName, jCertAliasChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        if (! ::CertSetCertificateContextProperty(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            CERT_FRIENDLY_NAME_PROP_ID, 0, &friendlyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        // Attach the certificate's private key (if supplied)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        if (hCryptProv != 0 && hCryptKey != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            CRYPT_KEY_PROV_INFO keyProviderInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            DWORD dwDataLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            // Get the name of the key container
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1504
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1515
            pszContainerName = new (env) char[dwDataLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1516
            if (pszContainerName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1517
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1518
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1520
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                PP_CONTAINER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                (BYTE *) pszContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            // 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
  1532
            pwszContainerName = new (env) WCHAR[dwDataLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1533
            if (pwszContainerName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1534
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1535
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            if (mbstowcs(pwszContainerName, pszContainerName, dwDataLen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            // Set the name of the key container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            keyProviderInfo.pwszContainerName = pwszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            // Get the name of the provider
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1547
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                PP_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1558
            pszProviderName = new (env) char[dwDataLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1559
            if (pszProviderName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1560
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1561
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1563
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                PP_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                (BYTE *) pszProviderName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            // 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
  1575
            pwszProviderName = new (env) WCHAR[dwDataLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1576
            if (pwszProviderName == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1577
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1578
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            if (mbstowcs(pwszProviderName, pszProviderName, dwDataLen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            // Set the name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            keyProviderInfo.pwszProvName = pwszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            // Get and set the type of the provider
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1589
            if (! ::CryptGetProvParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                (HCRYPTPROV) hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                PP_PROVTYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                (LPBYTE) &keyProviderInfo.dwProvType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            // Set no provider flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            keyProviderInfo.dwFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            // Set no provider parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            keyProviderInfo.cProvParam = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            keyProviderInfo.rgProvParam = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            // Get the key's algorithm ID
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1608
            if (! ::CryptGetKeyParam( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                (HCRYPTKEY) hCryptKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                KP_ALGID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                (LPBYTE) &keyProviderInfo.dwKeySpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                &dwDataLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            // Set the key spec (using the algorithm ID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            switch (keyProviderInfo.dwKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            case CALG_RSA_KEYX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            case CALG_DH_SF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                keyProviderInfo.dwKeySpec = AT_KEYEXCHANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            case CALG_RSA_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            case CALG_DSS_SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                keyProviderInfo.dwKeySpec = AT_SIGNATURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_ALGID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            if (! ::CertSetCertificateContextProperty(pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                CERT_KEY_PROV_INFO_PROP_ID, 0, &keyProviderInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        // Import encoded certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        if (!::CertAddCertificateContextToStore(hCertStore, pCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            CERT_STORE_ADD_REPLACE_EXISTING, NULL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        if (pbCertEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            delete [] pbCertEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            delete [] pszCertAliasName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        if (pszContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            delete [] pszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        if (pwszContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            delete [] pwszContainerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        if (pszProviderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            delete [] pszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        if (pwszProviderName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            delete [] pwszProviderName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        if (pCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            ::CertFreeCertificateContext(pCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1687
 * Class:     sun_security_mscapi_CKeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
 * Method:    removeCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
 * Signature: (Ljava/lang/String;Ljava/lang/String;[BI)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1691
JNIEXPORT void JNICALL Java_sun_security_mscapi_CKeyStore_removeCertificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
  (JNIEnv *env, jobject obj, jstring jCertStoreName, jstring jCertAliasName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
  jbyteArray jCertEncoding, jint jCertEncodingSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    const char* pszCertStoreName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
    const char* pszCertAliasName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    HCERTSTORE hCertStore = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
    PCCERT_CONTEXT pCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    PCCERT_CONTEXT pTBDCertContext = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    jbyte* pbCertEncoding = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    DWORD cchNameString = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    char* pszNameString = NULL; // certificate's friendly name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    BOOL bDeleteAttempted = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        // 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
  1708
        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
  1709
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1710
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1711
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        if ((hCertStore = ::CertOpenSystemStore(NULL, pszCertStoreName)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        // 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
  1718
        pbCertEncoding = new (env) jbyte[jCertEncodingSize];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1719
        if (pbCertEncoding == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1720
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1721
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        // Create a certificate context from the encoded cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            (BYTE*) pbCertEncoding, jCertEncodingSize))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            ThrowException(env, CERTIFICATE_PARSING_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        // Find the certificate to be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        if (!(pTBDCertContext = ::CertFindCertificateInStore(hCertStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            X509_ASN_ENCODING, 0, CERT_FIND_EXISTING, pCertContext, NULL))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        // Check that its friendly name matches the supplied alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        if ((cchNameString = ::CertGetNameString(pTBDCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1744
            pszNameString = new (env) char[cchNameString];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1745
            if (pszNameString == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1746
                __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1747
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            ::CertGetNameString(pTBDCertContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                cchNameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            // 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
  1754
            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
  1755
                == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1756
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1757
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            if (strcmp(pszCertAliasName, pszNameString) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                // Only delete the certificate if the alias names matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                if (! ::CertDeleteCertificateFromStore(pTBDCertContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                    // pTBDCertContext is always freed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                    //  CertDeleteCertificateFromStore method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                    bDeleteAttempted = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                    ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                    __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        if (hCertStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            ::CertCloseStore(hCertStore, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        if (pszCertStoreName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            env->ReleaseStringUTFChars(jCertStoreName, pszCertStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        if (pszCertAliasName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            env->ReleaseStringUTFChars(jCertAliasName, pszCertAliasName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        if (pbCertEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
            delete [] pbCertEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        if (pszNameString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            delete [] pszNameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        if (pCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            ::CertFreeCertificateContext(pCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        if (bDeleteAttempted && pTBDCertContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            ::CertFreeCertificateContext(pTBDCertContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1803
 * Class:     sun_security_mscapi_CKeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
 * Method:    destroyKeyContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
 * Signature: (Ljava/lang/String;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1807
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
  1808
  (JNIEnv *env, jobject clazz, jstring keyContainerName)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    const char* pszKeyContainerName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1815
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1816
            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
  1817
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  1818
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        // Destroying the default key container is not permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        // (because it may contain more one keypair).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        if (pszKeyContainerName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_KEYSET_PARAM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        // Acquire a CSP context (to the key container).
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1829
        if (::CryptAcquireContext( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            CRYPT_DELETEKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1852
 * Class:     sun_security_mscapi_CRSACipher
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
 * Method:    encryptDecrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
 * Signature: ([BIJZ)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1856
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CRSACipher_encryptDecrypt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
  (JNIEnv *env, jclass clazz, jbyteArray jData, jint jDataSize, jlong hKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
   jboolean doEncrypt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
    jbyteArray result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    jbyte* pData = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
    DWORD dwDataLen = jDataSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
    DWORD dwBufLen = env->GetArrayLength(jData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    DWORD i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    BYTE tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        // 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
  1870
        pData = new (env) jbyte[dwBufLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1871
        if (pData == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1872
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1873
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        env->GetByteArrayRegion(jData, 0, dwBufLen, pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        if (doEncrypt == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            // encrypt
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1878
            if (! ::CryptEncrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData, //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                &dwDataLen, dwBufLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            dwBufLen = dwDataLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            // convert from little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            for (i = 0; i < dwBufLen / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                tmp = pData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                pData[i] = pData[dwBufLen - i -1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                pData[dwBufLen - i - 1] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            // convert to little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            for (i = 0; i < dwBufLen / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                tmp = pData[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                pData[i] = pData[dwBufLen - i -1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                pData[dwBufLen - i - 1] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            // decrypt
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1901
            if (! ::CryptDecrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData, //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                &dwBufLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                ThrowException(env, KEY_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        // Create new byte array
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1910
        if ((result = env->NewByteArray(dwBufLen)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1911
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1912
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        // Copy data from native buffer to Java buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        env->SetByteArrayRegion(result, 0, dwBufLen, (jbyte*) pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        if (pData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            delete [] pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1927
 * Class:     sun_security_mscapi_CPublicKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
 * Method:    getPublicKeyBlob
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1929
 * Signature: (JJ)[B
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1931
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CPublicKey_getPublicKeyBlob
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1932
    (JNIEnv *env, jobject clazz, jlong hCryptProv, jlong hCryptKey) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    jbyteArray blob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
    DWORD dwBlobLen;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
  1936
    BYTE* pbKeyBlob = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        // Determine the size of the blob
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1942
        if (hCryptKey == 0) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1943
            SS_CHECK(::NCryptExportKey(
54287
c02b8d6384ab 8221407: Windows 32bit build error in libsunmscapi/security.cpp
stuefe
parents: 53333
diff changeset
  1944
                (NCRYPT_KEY_HANDLE)hCryptProv, NULL, BCRYPT_ECCPUBLIC_BLOB,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1945
                NULL, NULL, 0, &dwBlobLen, NCRYPT_SILENT_FLAG));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1946
        } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1947
            if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, NULL, //deprecated
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1948
                &dwBlobLen)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1950
                ThrowException(env, KEY_EXCEPTION, GetLastError());
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1951
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1952
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1955
        pbKeyBlob = new (env) BYTE[dwBlobLen];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1956
        if (pbKeyBlob == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1957
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  1958
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        // Generate key blob
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1961
        if (hCryptKey == 0) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1962
            SS_CHECK(::NCryptExportKey(
54287
c02b8d6384ab 8221407: Windows 32bit build error in libsunmscapi/security.cpp
stuefe
parents: 53333
diff changeset
  1963
                (NCRYPT_KEY_HANDLE)hCryptProv, NULL, BCRYPT_ECCPUBLIC_BLOB,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1964
                NULL, pbKeyBlob, dwBlobLen, &dwBlobLen, NCRYPT_SILENT_FLAG));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1965
        } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1966
            if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, //deprecated
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1967
                pbKeyBlob, &dwBlobLen)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1969
                ThrowException(env, KEY_EXCEPTION, GetLastError());
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1970
                __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  1971
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        // Create new byte array
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1975
        if ((blob = env->NewByteArray(dwBlobLen)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1976
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  1977
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        // Copy data from native buffer to Java buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        env->SetByteArrayRegion(blob, 0, dwBlobLen, (jbyte*) pbKeyBlob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
            delete [] pbKeyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    return blob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1992
 * Class:     sun_security_mscapi_CPublicKey_CRSAPublicKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
 * Method:    getExponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
 * Signature: ([B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  1996
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
  1997
    (JNIEnv *env, jobject clazz, jbyteArray jKeyBlob) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    jbyteArray exponent = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    jbyte*     exponentBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
    jbyte*     keyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        jsize length = env->GetArrayLength(jKeyBlob);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2006
        jsize headerLength = sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY);
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2007
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2008
        if (length < headerLength) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2009
            ThrowExceptionWithMessage(env, KEY_EXCEPTION, "Invalid BLOB");
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2010
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2011
        }
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2012
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2013
        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
  2014
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2015
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        // Check BLOB type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
        if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
            ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        RSAPUBKEY* pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC));
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2027
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        int len = sizeof(pRsaPubKey->pubexp);
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2029
        exponentBytes = new (env) jbyte[len];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2030
        if (exponentBytes == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2031
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2032
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        // convert from little-endian while copying from blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        for (int i = 0, j = len - 1; i < len; i++, j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            exponentBytes[i] = ((BYTE*) &pRsaPubKey->pubexp)[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2039
        if ((exponent = env->NewByteArray(len)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2040
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2041
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        env->SetByteArrayRegion(exponent, 0, len, exponentBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        if (keyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        if (exponentBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            delete [] exponentBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2057
 * Class:     sun_security_mscapi_CPublicKey_CRSAPublicKey
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
 * Method:    getModulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
 * Signature: ([B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2061
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
  2062
    (JNIEnv *env, jobject clazz, jbyteArray jKeyBlob) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
    jbyteArray modulus = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    jbyte*     modulusBytes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
    jbyte*     keyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    __try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        jsize length = env->GetArrayLength(jKeyBlob);
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2071
        jsize headerLength = sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY);
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2072
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2073
        if (length < headerLength) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2074
            ThrowExceptionWithMessage(env, KEY_EXCEPTION, "Invalid BLOB");
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
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2078
        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
  2079
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2080
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
        PUBLICKEYSTRUC* pPublicKeyStruc = (PUBLICKEYSTRUC *) keyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        // Check BLOB type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        if (pPublicKeyStruc->bType != PUBLICKEYBLOB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            ThrowException(env, KEY_EXCEPTION, NTE_BAD_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        RSAPUBKEY* pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC));
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2092
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        int len = pRsaPubKey->bitlen / 8;
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2094
        if (len < 0 || len > length - headerLength) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2095
            ThrowExceptionWithMessage(env, KEY_EXCEPTION, "Invalid key length");
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2096
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2097
        }
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2098
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2099
        modulusBytes = new (env) jbyte[len];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2100
        if (modulusBytes == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2101
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2102
        }
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2103
        BYTE * pbModulus = (BYTE *) (keyBlob + headerLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        // convert from little-endian while copying from blob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        for (int i = 0, j = len - 1; i < len; i++, j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            modulusBytes[i] = pbModulus[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2110
        if ((modulus = env->NewByteArray(len)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2111
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2112
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        env->SetByteArrayRegion(modulus, 0, len, modulusBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        if (keyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            env->ReleaseByteArrayElements(jKeyBlob, keyBlob, JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        if (modulusBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
            delete [] modulusBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    return modulus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
 * Convert an array in big-endian byte order into little-endian byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
int convertToLittleEndian(JNIEnv *env, jbyteArray source, jbyte* destination,
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2131
        int destinationLength) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2132
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2133
    int result = -1;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2134
    jbyte* sourceBytes = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2136
    __try {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2137
        int sourceLength = env->GetArrayLength(source);
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
        sourceBytes = env->GetByteArrayElements(source, 0);
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2140
        if (sourceBytes == NULL) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2141
            __leave;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2142
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2144
        int copyLen = sourceLength;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2145
        if (sourceLength > destinationLength) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2146
            // source might include an extra sign byte
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2147
            if (sourceLength == destinationLength + 1 && sourceBytes[0] == 0) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2148
                copyLen--;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2149
            } else {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2150
                __leave;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2151
            }
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2152
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2154
        // 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
  2155
        // destination array (until the destination array is full).
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2156
        // 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
  2157
        for (int i = 0; i < copyLen; i++) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2158
            destination[i] = sourceBytes[sourceLength - 1 - i];
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2159
        }
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2160
        if (copyLen < destinationLength) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2161
            memset(destination + copyLen, 0, destinationLength - copyLen);
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2162
        }
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2163
        result = destinationLength;
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2164
    } __finally {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2165
        // Clean up.
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2166
        if (sourceBytes) {
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2167
            env->ReleaseByteArrayElements(source, sourceBytes, JNI_ABORT);
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  2168
        }
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  2169
    }
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
  2170
49151
a7d2f0dd9c1f 8193262: JNI array not released in libsunmscapi convertToLittleEndian
weijun
parents: 49149
diff changeset
  2171
    return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
 * The Microsoft Base Cryptographic Provider supports public-key BLOBs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
 * that have the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
 *     PUBLICKEYSTRUC publickeystruc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
 *     RSAPUBKEY rsapubkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
 *     BYTE modulus[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
 * and private-key BLOBs that have the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
 *     PUBLICKEYSTRUC publickeystruc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
 *     RSAPUBKEY rsapubkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
 *     BYTE modulus[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
 *     BYTE prime1[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
 *     BYTE prime2[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
 *     BYTE exponent1[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
 *     BYTE exponent2[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
 *     BYTE coefficient[rsapubkey.bitlen/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
 *     BYTE privateExponent[rsapubkey.bitlen/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
 * This method generates such BLOBs from the key elements supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
jbyteArray generateKeyBlob(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
        jbyteArray jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
        jbyteArray jPrivateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        jbyteArray jPrimeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        jbyteArray jPrimeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        jbyteArray jExponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        jbyteArray jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
        jbyteArray jCrtCoefficient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
    jsize jKeyByteLength = jKeyBitLength / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
    jsize jBlobLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
    BOOL bGeneratePrivateKeyBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
    // Determine whether to generate a public-key or a private-key BLOB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    if (jPrivateExponent != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        jPrimeP != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        jPrimeQ != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        jExponentP != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        jExponentQ != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
        jCrtCoefficient != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
        bGeneratePrivateKeyBlob = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        jBlobLength = sizeof(BLOBHEADER) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                        sizeof(RSAPUBKEY) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                        ((jKeyBitLength / 8) * 4) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                        (jKeyBitLength / 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        bGeneratePrivateKeyBlob = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        jBlobLength = sizeof(BLOBHEADER) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                        sizeof(RSAPUBKEY) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                        (jKeyBitLength / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2233
    jbyte* jBlobBytes = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    jbyte* jBlobElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    jbyteArray jBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
    jsize  jElementLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
    __try {
41605
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2239
        jBlobBytes = new (env) jbyte[jBlobLength];
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2240
        if (jBlobBytes == NULL) {
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2241
            __leave;
290923d3b566 8165463: Native implementation of sunmscapi should use operator new (nothrow) for allocations
igerasim
parents: 40414
diff changeset
  2242
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
        BLOBHEADER *pBlobHeader = (BLOBHEADER *) jBlobBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            pBlobHeader->bType = PRIVATEKEYBLOB;  // 0x07
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            pBlobHeader->bType = PUBLICKEYBLOB;   // 0x06
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        pBlobHeader->bVersion = CUR_BLOB_VERSION; // 0x02
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        pBlobHeader->reserved = 0;                // 0x0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        pBlobHeader->aiKeyAlg = CALG_RSA_KEYX;    // 0x0000a400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        RSAPUBKEY *pRsaPubKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            (RSAPUBKEY *) (jBlobBytes + sizeof(PUBLICKEYSTRUC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            pRsaPubKey->magic = 0x32415352;       // "RSA2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            pRsaPubKey->magic = 0x31415352;       // "RSA1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        pRsaPubKey->bitlen = jKeyBitLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        pRsaPubKey->pubexp = 0; // init
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        // Sanity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        jsize jPublicExponentLength = env->GetArrayLength(jPublicExponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        if (jPublicExponentLength > sizeof(pRsaPubKey->pubexp)) {
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
  2267
            ThrowException(env, INVALID_KEY_EXCEPTION, NTE_BAD_TYPE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
        // The length argument must be the smaller of jPublicExponentLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        // 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
  2272
        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
  2273
            (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
  2274
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2275
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
        // Modulus n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
        jBlobElement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            (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
  2280
        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
  2281
            jKeyByteLength)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2282
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2283
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        if (bGeneratePrivateKeyBlob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            // Prime p
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, jPrimeP,
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
            // Prime q
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, jPrimeQ,
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
            // Prime exponent p
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, jExponentP,
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2303
                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
  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
            // Prime exponent q
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2309
            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
  2310
                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
  2311
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2312
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            // CRT coefficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2316
            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
  2317
                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
  2318
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2319
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            // Private exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
            jBlobElement += jElementLength;
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2323
            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
  2324
                jBlobElement, jKeyByteLength)) < 0) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2325
                __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2326
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2329
        if ((jBlob = env->NewByteArray(jBlobLength)) == NULL) {
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2330
            __leave;
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 52801
diff changeset
  2331
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
        env->SetByteArrayRegion(jBlob, 0, jBlobLength, jBlobBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        if (jBlobBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            delete [] jBlobBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
    return jBlob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2345
 * Class:     sun_security_mscapi_CKeyStore
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2346
 * Method:    generateRSAPrivateKeyBlob
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
 * Signature: (I[B[B[B[B[B[B[B[B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2349
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
  2350
    (JNIEnv *env, jobject clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        jbyteArray jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        jbyteArray jPrivateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        jbyteArray jPrimeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        jbyteArray jPrimeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        jbyteArray jExponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        jbyteArray jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        jbyteArray jCrtCoefficient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
    return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        jPrivateExponent, jPrimeP, jPrimeQ, jExponentP, jExponentQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
        jCrtCoefficient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2367
 * Class:     sun_security_mscapi_CSignature_RSA
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
 * Method:    generatePublicKeyBlob
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
 * Signature: (I[B[B)[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2371
JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_CSignature_00024RSA_generatePublicKeyBlob
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    (JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        jint jKeyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        jbyteArray jModulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        jbyteArray jPublicExponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
    return generateKeyBlob(env, jKeyBitLength, jModulus, jPublicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        NULL, NULL, NULL, NULL, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2382
 * Class:     sun_security_mscapi_CKeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
 * Method:    storePrivateKey
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2384
 * Signature: (Ljava/lang/String;[BLjava/lang/String;I)Lsun/security/mscapi/CPrivateKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2386
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_CKeyStore_storePrivateKey
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2387
    (JNIEnv *env, jobject clazz, jstring alg, jbyteArray keyBlob,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2388
     jstring keyContainerName, jint keySize)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
    DWORD dwBlobLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
    BYTE * pbKeyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
    const char* pszKeyContainerName = NULL; // UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
    jobject privateKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    {
25812
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2399
        if ((pszKeyContainerName =
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2400
            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
  2401
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2402
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        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
  2404
        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
  2405
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2406
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2407
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        // Acquire a CSP context (create a new key container).
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2410
        if (::CryptAcquireContext( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            pszKeyContainerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            PROV_RSA_FULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            CRYPT_NEWKEYSET) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        // Import the private key
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2422
        if (::CryptImportKey( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
            pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
            dwBlobLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            CRYPT_EXPORTABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
            &hKey) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2434
        // Get the method ID for the CPrivateKey constructor
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2435
        jclass clazzCPrivateKey =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2436
            env->FindClass("sun/security/mscapi/CPrivateKey");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2437
        if (clazzCPrivateKey == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2438
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2439
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2441
        jmethodID mNewCPrivateKey =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2442
            env->GetStaticMethodID(clazzCPrivateKey, "of",
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2443
            "(Ljava/lang/String;JJI)Lsun/security/mscapi/CPrivateKey;");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2444
        if (mNewCPrivateKey == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2445
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2446
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2448
        // Create a new private key
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2449
        privateKey = env->CallStaticObjectMethod(clazzCPrivateKey, mNewCPrivateKey,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2450
            alg, (jlong) hCryptProv, (jlong) hKey, keySize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        if (pszKeyContainerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
            env->ReleaseStringUTFChars(keyContainerName, pszKeyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
            env->ReleaseByteArrayElements(keyBlob, (jbyte *) pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
    return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2470
 * Class:     sun_security_mscapi_CSignature
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2471
 * Method:    importECPublicKey
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2472
 * Signature: (Ljava/lang/String;[BI)Lsun/security/mscapi/CPublicKey;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2473
 */
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2474
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_CSignature_importECPublicKey
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2475
    (JNIEnv *env, jclass clazz, jstring alg, jbyteArray keyBlob, jint keySize)
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2476
{
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2477
    BCRYPT_ALG_HANDLE hSignAlg = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2478
    NCRYPT_KEY_HANDLE       hTmpKey         = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2479
    DWORD dwBlobLen;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2480
    BYTE * pbKeyBlob = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2481
    jobject publicKey = NULL;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2482
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2483
    __try
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2484
    {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2485
        dwBlobLen = env->GetArrayLength(keyBlob);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2486
        if ((pbKeyBlob = (BYTE *) env->GetByteArrayElements(keyBlob, 0))
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2487
            == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2488
            __leave;
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
        dump("NCryptImportKey", pbKeyBlob, dwBlobLen);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2491
        NCRYPT_PROV_HANDLE hProv;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2492
        SS_CHECK(NCryptOpenStorageProvider(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2493
                &hProv, L"Microsoft Software Key Storage Provider", 0 ));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2494
        SS_CHECK(NCryptImportKey(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2495
                                                    hProv,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2496
                                                    NULL,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2497
                                                    BCRYPT_ECCPUBLIC_BLOB,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2498
                                                    NULL,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2499
                                                    &hTmpKey,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2500
                                                    pbKeyBlob,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2501
                                                    dwBlobLen,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2502
                                                    0));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2503
        NCryptFreeObject( hProv );
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2504
        // Get the method ID for the CPublicKey constructor
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2505
        jclass clazzCPublicKey =
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2506
            env->FindClass("sun/security/mscapi/CPublicKey");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2507
        if (clazzCPublicKey == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2508
            __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2509
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2510
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2511
        jmethodID mNewCPublicKey =
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2512
            env->GetStaticMethodID(clazzCPublicKey, "of",
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2513
            "(Ljava/lang/String;JJI)Lsun/security/mscapi/CPublicKey;");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2514
        if (mNewCPublicKey == NULL) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2515
            __leave;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2516
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2517
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2518
        // Create a new public key
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2519
        publicKey = env->CallStaticObjectMethod(clazzCPublicKey, mNewCPublicKey,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2520
            alg, (jlong) hTmpKey, (jlong) 0, keySize);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2521
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2522
    __finally
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2523
    {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2524
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2525
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2526
    return publicKey;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2527
}
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2528
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2529
/*
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
  2530
 * Class:     sun_security_mscapi_CSignature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
 * Method:    importPublicKey
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2532
 * Signature: (Ljava/lang/String;[BI)Lsun/security/mscapi/CPublicKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2534
JNIEXPORT jobject JNICALL Java_sun_security_mscapi_CSignature_importPublicKey
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2535
    (JNIEnv *env, jclass clazz, jstring alg, jbyteArray keyBlob, jint keySize)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
    HCRYPTPROV hCryptProv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
    HCRYPTKEY hKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
    DWORD dwBlobLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
    BYTE * pbKeyBlob = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
    jobject publicKey = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
    __try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        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
  2546
        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
  2547
            == NULL) {
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2548
            __leave;
5412629bed70 8036612: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/mscapi/security.cpp
vinnie
parents: 9674
diff changeset
  2549
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
        // Acquire a CSP context (create a new key container).
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2552
        // 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
  2553
        // for SHA-2-based signatures.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2554
        if (::CryptAcquireContext( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
            &hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
            NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
            NULL,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2558
            PROV_RSA_AES,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            CRYPT_VERIFYCONTEXT) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
        {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2561
            // Failover to using the default CSP (PROV_RSA_FULL)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2562
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2563
            if (::CryptAcquireContext( //deprecated
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2564
                &hCryptProv,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2565
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2566
                NULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2567
                PROV_RSA_FULL,
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2568
                CRYPT_VERIFYCONTEXT) == FALSE)
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2569
            {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2570
                ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2571
                __leave;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
  2572
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        // Import the public key
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2576
        if (::CryptImportKey( //deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
            hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
            pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
            dwBlobLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
            0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
            CRYPT_EXPORTABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            &hKey) == FALSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
            ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
            __leave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2588
        // Get the method ID for the CPublicKey constructor
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2589
        jclass clazzCPublicKey =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2590
            env->FindClass("sun/security/mscapi/CPublicKey");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2591
        if (clazzCPublicKey == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2592
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2593
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2595
        jmethodID mNewCPublicKey =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2596
            env->GetStaticMethodID(clazzCPublicKey, "of",
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2597
            "(Ljava/lang/String;JJI)Lsun/security/mscapi/CPublicKey;");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2598
        if (mNewCPublicKey == NULL) {
31470
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2599
            __leave;
93708c7917fc 8098854: Do cleanup in a proper order in sunmscapi code
igerasim
parents: 31264
diff changeset
  2600
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2602
        // Create a new public key
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2603
        publicKey = env->CallStaticObjectMethod(clazzCPublicKey, mNewCPublicKey,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 52801
diff changeset
  2604
            alg, (jlong) hCryptProv, (jlong) hKey, keySize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    __finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
        //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
        // Clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
        if (pbKeyBlob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            env->ReleaseByteArrayElements(keyBlob, (jbyte *) pbKeyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                JNI_ABORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
    return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
} /* extern "C" */