75 * The exception message is a Windows system error message.
76 */
77 void ThrowException(JNIEnv *env, const char *exceptionName, DWORD dwError)
78 {
79 char szMessage[1024];
80 szMessage[0] = '\0';
81
82 DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
83 NULL, szMessage, sizeof(szMessage), NULL);
84 if (res == 0) {
85 strcpy(szMessage, "Unknown error");
86 }
87
88 ThrowExceptionWithMessage(env, exceptionName, szMessage);
89 }
90
91 /*
92 * Overloaded 'operator new[]' variant, which will raise Java's
93 * OutOfMemoryError in the case of a failure.
94 */
95 static void* operator new[](std::size_t size, JNIEnv *env)
96 {
97 void* buf = ::operator new[](size, std::nothrow);
98 if (buf == NULL) {
99 ThrowExceptionWithMessage(env, OUT_OF_MEMORY_ERROR,
100 "Native memory allocation failed");
101 }
102 return buf;
103 }
104
105 /*
106 * Maps the name of a hash algorithm to an algorithm identifier.
107 */
108 ALG_ID MapHashAlgorithm(JNIEnv *env, jstring jHashAlgorithm) {
109
110 const char* pszHashAlgorithm = NULL;
111 ALG_ID algId = 0;
112
113 if ((pszHashAlgorithm = env->GetStringUTFChars(jHashAlgorithm, NULL))
114 == NULL) {
115 return algId;
1522 //--------------------------------------------------------------------
1523 // Clean up.
1524
1525 if (bCallerFreeProv == TRUE && hCryptProv != NULL)
1526 ::CryptReleaseContext(hCryptProv, 0);
1527 }
1528
1529 return hKey; // TODO - when finished with this key, call
1530 // CryptDestroyKey(hKey)
1531 }
1532
1533 /*
1534 * Class: sun_security_mscapi_KeyStore
1535 * Method: getKeyLength
1536 * Signature: (J)I
1537 */
1538 JNIEXPORT jint JNICALL Java_sun_security_mscapi_KeyStore_getKeyLength
1539 (JNIEnv *env, jobject obj, jlong hKey)
1540 {
1541 DWORD dwDataLen = sizeof(DWORD);
1542 BYTE pbData[sizeof(DWORD)];
1543 DWORD length = 0;
1544
1545 __try
1546 {
1547 // Get key length (in bits)
1548 //TODO - may need to use KP_BLOCKLEN instead?
1549 if (!(::CryptGetKeyParam((HCRYPTKEY) hKey, KP_KEYLEN, (BYTE *)pbData, &dwDataLen,
1550 0))) {
1551
1552 ThrowException(env, KEY_EXCEPTION, GetLastError());
1553 __leave;
1554 }
1555 length = (DWORD) pbData;
1556 }
1557 __finally
1558 {
1559 // no cleanup required
1560 }
1561
1562 return (jint) length;
1563 }
1564
1565 /*
1566 * Class: sun_security_mscapi_RSACipher
1567 * Method: encryptDecrypt
1568 * Signature: ([BIJZ)[B
1569 */
1570 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSACipher_encryptDecrypt
1571 (JNIEnv *env, jclass clazz, jbyteArray jData, jint jDataSize, jlong hKey,
1572 jboolean doEncrypt)
1573 {
1574 jbyteArray result = NULL;
1575 jbyte* pData = NULL;
|
75 * The exception message is a Windows system error message.
76 */
77 void ThrowException(JNIEnv *env, const char *exceptionName, DWORD dwError)
78 {
79 char szMessage[1024];
80 szMessage[0] = '\0';
81
82 DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
83 NULL, szMessage, sizeof(szMessage), NULL);
84 if (res == 0) {
85 strcpy(szMessage, "Unknown error");
86 }
87
88 ThrowExceptionWithMessage(env, exceptionName, szMessage);
89 }
90
91 /*
92 * Overloaded 'operator new[]' variant, which will raise Java's
93 * OutOfMemoryError in the case of a failure.
94 */
95 void* operator new[](std::size_t size, JNIEnv *env)
96 {
97 void* buf = ::operator new[](size, std::nothrow);
98 if (buf == NULL) {
99 ThrowExceptionWithMessage(env, OUT_OF_MEMORY_ERROR,
100 "Native memory allocation failed");
101 }
102 return buf;
103 }
104
105 /*
106 * Maps the name of a hash algorithm to an algorithm identifier.
107 */
108 ALG_ID MapHashAlgorithm(JNIEnv *env, jstring jHashAlgorithm) {
109
110 const char* pszHashAlgorithm = NULL;
111 ALG_ID algId = 0;
112
113 if ((pszHashAlgorithm = env->GetStringUTFChars(jHashAlgorithm, NULL))
114 == NULL) {
115 return algId;
1522 //--------------------------------------------------------------------
1523 // Clean up.
1524
1525 if (bCallerFreeProv == TRUE && hCryptProv != NULL)
1526 ::CryptReleaseContext(hCryptProv, 0);
1527 }
1528
1529 return hKey; // TODO - when finished with this key, call
1530 // CryptDestroyKey(hKey)
1531 }
1532
1533 /*
1534 * Class: sun_security_mscapi_KeyStore
1535 * Method: getKeyLength
1536 * Signature: (J)I
1537 */
1538 JNIEXPORT jint JNICALL Java_sun_security_mscapi_KeyStore_getKeyLength
1539 (JNIEnv *env, jobject obj, jlong hKey)
1540 {
1541 DWORD dwDataLen = sizeof(DWORD);
1542 DWORD length = 0;
1543
1544 __try
1545 {
1546 // Get key length (in bits)
1547 //TODO - may need to use KP_BLOCKLEN instead?
1548 if (!(::CryptGetKeyParam((HCRYPTKEY) hKey, KP_KEYLEN, (PBYTE)&length, &dwDataLen,
1549 0))) {
1550
1551 ThrowException(env, KEY_EXCEPTION, GetLastError());
1552 __leave;
1553 }
1554 }
1555 __finally
1556 {
1557 // no cleanup required
1558 }
1559
1560 return (jint) length;
1561 }
1562
1563 /*
1564 * Class: sun_security_mscapi_RSACipher
1565 * Method: encryptDecrypt
1566 * Signature: ([BIJZ)[B
1567 */
1568 JNIEXPORT jbyteArray JNICALL Java_sun_security_mscapi_RSACipher_encryptDecrypt
1569 (JNIEnv *env, jclass clazz, jbyteArray jData, jint jDataSize, jlong hKey,
1570 jboolean doEncrypt)
1571 {
1572 jbyteArray result = NULL;
1573 jbyte* pData = NULL;
|