jdk/src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp
changeset 31470 93708c7917fc
parent 31264 896105040033
child 33653 c1ee09fe3274
equal deleted inserted replaced
31469:92cc72d2a11a 31470:93708c7917fc
    28 //=--------------------------------------------------------------------------=
    28 //=--------------------------------------------------------------------------=
    29 //
    29 //
    30 
    30 
    31 #include <jni.h>
    31 #include <jni.h>
    32 #include <stdlib.h>
    32 #include <stdlib.h>
       
    33 #include <string.h>
    33 #include <windows.h>
    34 #include <windows.h>
    34 #include <BaseTsd.h>
    35 #include <BaseTsd.h>
    35 #include <wincrypt.h>
    36 #include <wincrypt.h>
    36 #include <stdio.h>
    37 #include <stdio.h>
    37 
    38 
    56 void ThrowException(JNIEnv *env, char *exceptionName, DWORD dwError)
    57 void ThrowException(JNIEnv *env, char *exceptionName, DWORD dwError)
    57 {
    58 {
    58     char szMessage[1024];
    59     char szMessage[1024];
    59     szMessage[0] = '\0';
    60     szMessage[0] = '\0';
    60 
    61 
    61     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, NULL, szMessage,
    62     DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
    62         1024, NULL);
    63         NULL, szMessage, sizeof(szMessage), NULL);
       
    64     if (res == 0) {
       
    65         strcpy(szMessage, "Unknown error");
       
    66     }
    63 
    67 
    64     jclass exceptionClazz = env->FindClass(exceptionName);
    68     jclass exceptionClazz = env->FindClass(exceptionName);
    65     env->ThrowNew(exceptionClazz, szMessage);
    69     if (exceptionClazz != NULL) {
       
    70         env->ThrowNew(exceptionClazz, szMessage);
       
    71     }
    66 }
    72 }
    67 
    73 
    68 
    74 
    69 /*
    75 /*
    70  * Maps the name of a hash algorithm to an algorithm identifier.
    76  * Maps the name of a hash algorithm to an algorithm identifier.
   293             __leave;
   299             __leave;
   294         }
   300         }
   295 
   301 
   296         // Determine clazz and method ID to generate certificate
   302         // Determine clazz and method ID to generate certificate
   297         jclass clazzArrayList = env->FindClass("java/util/ArrayList");
   303         jclass clazzArrayList = env->FindClass("java/util/ArrayList");
       
   304         if (clazzArrayList == NULL) {
       
   305             __leave;
       
   306         }
   298 
   307 
   299         jmethodID mNewArrayList = env->GetMethodID(clazzArrayList, "<init>", "()V");
   308         jmethodID mNewArrayList = env->GetMethodID(clazzArrayList, "<init>", "()V");
   300 
   309         if (mNewArrayList == NULL) {
   301         jmethodID mGenCert = env->GetMethodID(env->GetObjectClass(obj),
   310             __leave;
       
   311         }
       
   312 
       
   313         jclass clazzOfThis = env->GetObjectClass(obj);
       
   314         if (clazzOfThis == NULL) {
       
   315             __leave;
       
   316         }
       
   317 
       
   318         jmethodID mGenCert = env->GetMethodID(clazzOfThis,
   302                                               "generateCertificate",
   319                                               "generateCertificate",
   303                                               "([BLjava/util/Collection;)V");
   320                                               "([BLjava/util/Collection;)V");
       
   321         if (mGenCert == NULL) {
       
   322             __leave;
       
   323         }
   304 
   324 
   305         // Determine method ID to generate certificate chain
   325         // Determine method ID to generate certificate chain
   306         jmethodID mGenCertChain = env->GetMethodID(env->GetObjectClass(obj),
   326         jmethodID mGenCertChain = env->GetMethodID(clazzOfThis,
   307                                                    "generateCertificateChain",
   327                                                    "generateCertificateChain",
   308                                                    "(Ljava/lang/String;Ljava/util/Collection;Ljava/util/Collection;)V");
   328                                                    "(Ljava/lang/String;Ljava/util/Collection;Ljava/util/Collection;)V");
       
   329         if (mGenCertChain == NULL) {
       
   330             __leave;
       
   331         }
   309 
   332 
   310         // Determine method ID to generate RSA certificate chain
   333         // Determine method ID to generate RSA certificate chain
   311         jmethodID mGenRSAKeyAndCertChain = env->GetMethodID(env->GetObjectClass(obj),
   334         jmethodID mGenRSAKeyAndCertChain = env->GetMethodID(clazzOfThis,
   312                                                    "generateRSAKeyAndCertificateChain",
   335                                                    "generateRSAKeyAndCertificateChain",
   313                                                    "(Ljava/lang/String;JJILjava/util/Collection;Ljava/util/Collection;)V");
   336                                                    "(Ljava/lang/String;JJILjava/util/Collection;Ljava/util/Collection;)V");
       
   337         if (mGenRSAKeyAndCertChain == NULL) {
       
   338             __leave;
       
   339         }
   314 
   340 
   315         // Use CertEnumCertificatesInStore to get the certificates
   341         // Use CertEnumCertificatesInStore to get the certificates
   316         // from the open store. pCertContext must be reset to
   342         // from the open store. pCertContext must be reset to
   317         // NULL to retrieve the first certificate in the store.
   343         // NULL to retrieve the first certificate in the store.
   318         while (pCertContext = ::CertEnumCertificatesInStore(hCertStore, pCertContext))
   344         while (pCertContext = ::CertEnumCertificatesInStore(hCertStore, pCertContext))
   588 
   614 
   589         jSignedHash = temp;
   615         jSignedHash = temp;
   590     }
   616     }
   591     __finally
   617     __finally
   592     {
   618     {
       
   619         if (pSignedHashBuffer)
       
   620             delete [] pSignedHashBuffer;
       
   621 
       
   622         if (pHashBuffer)
       
   623             delete [] pHashBuffer;
       
   624 
       
   625         if (hHash)
       
   626             ::CryptDestroyHash(hHash);
       
   627 
   593         if (hCryptProvAlt)
   628         if (hCryptProvAlt)
   594             ::CryptReleaseContext(hCryptProvAlt, 0);
   629             ::CryptReleaseContext(hCryptProvAlt, 0);
   595 
       
   596         if (pSignedHashBuffer)
       
   597             delete [] pSignedHashBuffer;
       
   598 
       
   599         if (pHashBuffer)
       
   600             delete [] pHashBuffer;
       
   601 
       
   602         if (hHash)
       
   603             ::CryptDestroyHash(hHash);
       
   604     }
   630     }
   605 
   631 
   606     return jSignedHash;
   632     return jSignedHash;
   607 }
   633 }
   608 
   634 
   686         }
   712         }
   687     }
   713     }
   688 
   714 
   689     __finally
   715     __finally
   690     {
   716     {
       
   717         if (pSignedHashBuffer)
       
   718             delete [] pSignedHashBuffer;
       
   719 
       
   720         if (pHashBuffer)
       
   721             delete [] pHashBuffer;
       
   722 
       
   723         if (hHash)
       
   724             ::CryptDestroyHash(hHash);
       
   725 
   691         if (hCryptProvAlt)
   726         if (hCryptProvAlt)
   692             ::CryptReleaseContext(hCryptProvAlt, 0);
   727             ::CryptReleaseContext(hCryptProvAlt, 0);
   693 
       
   694         if (pSignedHashBuffer)
       
   695             delete [] pSignedHashBuffer;
       
   696 
       
   697         if (pHashBuffer)
       
   698             delete [] pHashBuffer;
       
   699 
       
   700         if (hHash)
       
   701             ::CryptDestroyHash(hHash);
       
   702     }
   728     }
   703 
   729 
   704     return result;
   730     return result;
   705 }
   731 }
   706 
   732 
   761         }
   787         }
   762 
   788 
   763         // Get the method ID for the RSAKeyPair constructor
   789         // Get the method ID for the RSAKeyPair constructor
   764         jclass clazzRSAKeyPair =
   790         jclass clazzRSAKeyPair =
   765             env->FindClass("sun/security/mscapi/RSAKeyPair");
   791             env->FindClass("sun/security/mscapi/RSAKeyPair");
       
   792         if (clazzRSAKeyPair == NULL) {
       
   793             __leave;
       
   794         }
   766 
   795 
   767         jmethodID mNewRSAKeyPair =
   796         jmethodID mNewRSAKeyPair =
   768             env->GetMethodID(clazzRSAKeyPair, "<init>", "(JJI)V");
   797             env->GetMethodID(clazzRSAKeyPair, "<init>", "(JJI)V");
       
   798         if (mNewRSAKeyPair == NULL) {
       
   799             __leave;
       
   800         }
   769 
   801 
   770         // Create a new RSA keypair
   802         // Create a new RSA keypair
   771         keypair = env->NewObject(clazzRSAKeyPair, mNewRSAKeyPair,
   803         keypair = env->NewObject(clazzRSAKeyPair, mNewRSAKeyPair,
   772             (jlong) hCryptProv, (jlong) hKeyPair, keySize);
   804             (jlong) hCryptProv, (jlong) hKeyPair, keySize);
   773 
   805 
  1946         }
  1978         }
  1947 
  1979 
  1948         // Get the method ID for the RSAPrivateKey constructor
  1980         // Get the method ID for the RSAPrivateKey constructor
  1949         jclass clazzRSAPrivateKey =
  1981         jclass clazzRSAPrivateKey =
  1950             env->FindClass("sun/security/mscapi/RSAPrivateKey");
  1982             env->FindClass("sun/security/mscapi/RSAPrivateKey");
       
  1983         if (clazzRSAPrivateKey == NULL) {
       
  1984             __leave;
       
  1985         }
  1951 
  1986 
  1952         jmethodID mNewRSAPrivateKey =
  1987         jmethodID mNewRSAPrivateKey =
  1953             env->GetMethodID(clazzRSAPrivateKey, "<init>", "(JJI)V");
  1988             env->GetMethodID(clazzRSAPrivateKey, "<init>", "(JJI)V");
       
  1989         if (mNewRSAPrivateKey == NULL) {
       
  1990             __leave;
       
  1991         }
  1954 
  1992 
  1955         // Create a new RSA private key
  1993         // Create a new RSA private key
  1956         privateKey = env->NewObject(clazzRSAPrivateKey, mNewRSAPrivateKey,
  1994         privateKey = env->NewObject(clazzRSAPrivateKey, mNewRSAPrivateKey,
  1957             (jlong) hCryptProv, (jlong) hKey, keySize);
  1995             (jlong) hCryptProv, (jlong) hKey, keySize);
  1958 
  1996 
  2033         }
  2071         }
  2034 
  2072 
  2035         // Get the method ID for the RSAPublicKey constructor
  2073         // Get the method ID for the RSAPublicKey constructor
  2036         jclass clazzRSAPublicKey =
  2074         jclass clazzRSAPublicKey =
  2037             env->FindClass("sun/security/mscapi/RSAPublicKey");
  2075             env->FindClass("sun/security/mscapi/RSAPublicKey");
       
  2076         if (clazzRSAPublicKey == NULL) {
       
  2077             __leave;
       
  2078         }
  2038 
  2079 
  2039         jmethodID mNewRSAPublicKey =
  2080         jmethodID mNewRSAPublicKey =
  2040             env->GetMethodID(clazzRSAPublicKey, "<init>", "(JJI)V");
  2081             env->GetMethodID(clazzRSAPublicKey, "<init>", "(JJI)V");
       
  2082         if (mNewRSAPublicKey == NULL) {
       
  2083             __leave;
       
  2084         }
  2041 
  2085 
  2042         // Create a new RSA public key
  2086         // Create a new RSA public key
  2043         publicKey = env->NewObject(clazzRSAPublicKey, mNewRSAPublicKey,
  2087         publicKey = env->NewObject(clazzRSAPublicKey, mNewRSAPublicKey,
  2044             (jlong) hCryptProv, (jlong) hKey, keySize);
  2088             (jlong) hCryptProv, (jlong) hKey, keySize);
  2045 
  2089