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)) |
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 |