< prev index next >

src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp

Print this page
rev 15549 : [mq]: 8165463-Native-implementation-of-sunmscapi-should-use-operator-new-nothrow-for-allocations

*** 34,43 **** --- 34,44 ---- #include <string.h> #include <windows.h> #include <BaseTsd.h> #include <wincrypt.h> #include <stdio.h> + #include <memory> #define OID_EKU_ANY "2.5.29.37.0" #define CERTIFICATE_PARSING_EXCEPTION \
*** 46,84 **** "java/security/InvalidKeyException" #define KEY_EXCEPTION "java/security/KeyException" #define KEYSTORE_EXCEPTION "java/security/KeyStoreException" #define PROVIDER_EXCEPTION "java/security/ProviderException" #define SIGNATURE_EXCEPTION "java/security/SignatureException" extern "C" { /* * Declare library specific JNI_Onload entry if static build */ DEF_STATIC_JNI_OnLoad /* * Throws an arbitrary Java exception. * The exception message is a Windows system error message. */ ! void ThrowException(JNIEnv *env, char *exceptionName, DWORD dwError) { char szMessage[1024]; szMessage[0] = '\0'; DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, NULL, szMessage, sizeof(szMessage), NULL); if (res == 0) { strcpy(szMessage, "Unknown error"); } ! jclass exceptionClazz = env->FindClass(exceptionName); ! if (exceptionClazz != NULL) { ! env->ThrowNew(exceptionClazz, szMessage); ! } } /* * Maps the name of a hash algorithm to an algorithm identifier. */ ALG_ID MapHashAlgorithm(JNIEnv *env, jstring jHashAlgorithm) { --- 47,108 ---- "java/security/InvalidKeyException" #define KEY_EXCEPTION "java/security/KeyException" #define KEYSTORE_EXCEPTION "java/security/KeyStoreException" #define PROVIDER_EXCEPTION "java/security/ProviderException" #define SIGNATURE_EXCEPTION "java/security/SignatureException" + #define OUT_OF_MEMORY_ERROR "java/lang/OutOfMemoryError" extern "C" { /* * Declare library specific JNI_Onload entry if static build */ DEF_STATIC_JNI_OnLoad /* + * Throws an arbitrary Java exception with the given message. + */ + void ThrowExceptionWithMessage(JNIEnv *env, const char *exceptionName, + const char *szMessage) + { + jclass exceptionClazz = env->FindClass(exceptionName); + if (exceptionClazz != NULL) { + env->ThrowNew(exceptionClazz, szMessage); + } + } + + /* * Throws an arbitrary Java exception. * The exception message is a Windows system error message. */ ! void ThrowException(JNIEnv *env, const char *exceptionName, DWORD dwError) { char szMessage[1024]; szMessage[0] = '\0'; DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, NULL, szMessage, sizeof(szMessage), NULL); if (res == 0) { strcpy(szMessage, "Unknown error"); } ! ThrowExceptionWithMessage(env, exceptionName, szMessage); } + /* + * Overloaded 'operator new[]' variant, which will raise Java's + * OutOfMemoryError in a case of the failure. + */ + static void* operator new[](std::size_t size, JNIEnv *env) + { + void* buf = ::operator new[](size, std::nothrow); + if (buf == NULL) { + ThrowExceptionWithMessage(env, OUT_OF_MEMORY_ERROR, + "native memory allocation failed"); + } + return buf; + } /* * Maps the name of a hash algorithm to an algorithm identifier. */ ALG_ID MapHashAlgorithm(JNIEnv *env, jstring jHashAlgorithm) {
*** 209,219 **** result = NULL; } else if (length > 0) { ! pbData = new BYTE[length]; if (::CryptGenRandom( hCryptProv, length, pbData) == FALSE) { --- 233,246 ---- result = NULL; } else if (length > 0) { ! pbData = new (env) BYTE[length]; ! if (pbData == NULL) { ! __leave; ! } if (::CryptGenRandom( hCryptProv, length, pbData) == FALSE) {
*** 439,449 **** if ((cchNameString = CertGetNameString(pc, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) > 1) { // Found friendly name ! pszNameString = new char[cchNameString]; CertGetNameString(pc, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString, cchNameString); } } --- 466,480 ---- if ((cchNameString = CertGetNameString(pc, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) > 1) { // Found friendly name ! pszNameString = new (env) char[cchNameString]; ! if (pszNameString == NULL) { ! __leave; ! } ! CertGetNameString(pc, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString, cchNameString); } }
*** 576,586 **** __leave; } } // Copy hash from Java to native buffer ! pHashBuffer = new jbyte[jHashSize]; env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer); // Set hash value in the hash object if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*)pHashBuffer, NULL) == FALSE) { --- 607,620 ---- __leave; } } // Copy hash from Java to native buffer ! pHashBuffer = new (env) jbyte[jHashSize]; ! if (pHashBuffer == NULL) { ! __leave; ! } env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer); // Set hash value in the hash object if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*)pHashBuffer, NULL) == FALSE) {
*** 614,624 **** { ThrowException(env, SIGNATURE_EXCEPTION, GetLastError()); __leave; } ! pSignedHashBuffer = new jbyte[dwBufLen]; if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, (BYTE*)pSignedHashBuffer, &dwBufLen) == FALSE) { ThrowException(env, SIGNATURE_EXCEPTION, GetLastError()); __leave; } --- 648,661 ---- { ThrowException(env, SIGNATURE_EXCEPTION, GetLastError()); __leave; } ! pSignedHashBuffer = new (env) jbyte[dwBufLen]; ! if (pSignedHashBuffer == NULL) { ! __leave; ! } if (::CryptSignHash(hHash, dwKeySpec, NULL, dwFlags, (BYTE*)pSignedHashBuffer, &dwBufLen) == FALSE) { ThrowException(env, SIGNATURE_EXCEPTION, GetLastError()); __leave; }
*** 702,714 **** __leave; } } // Copy hash and signedHash from Java to native buffer ! pHashBuffer = new jbyte[jHashSize]; env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer); ! pSignedHashBuffer = new jbyte[jSignedHashSize]; env->GetByteArrayRegion(jSignedHash, 0, jSignedHashSize, pSignedHashBuffer); // Set hash value in the hash object if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*) pHashBuffer, NULL) --- 739,758 ---- __leave; } } // Copy hash and signedHash from Java to native buffer ! pHashBuffer = new (env) jbyte[jHashSize]; ! if (pHashBuffer == NULL) { ! __leave; ! } env->GetByteArrayRegion(jHash, 0, jHashSize, pHashBuffer); ! ! pSignedHashBuffer = new (env) jbyte[jSignedHashSize]; ! if (pSignedHashBuffer == NULL) { ! __leave; ! } env->GetByteArrayRegion(jSignedHash, 0, jSignedHashSize, pSignedHashBuffer); // Set hash value in the hash object if (::CryptSetHashParam(hHash, HP_HASHVAL, (BYTE*) pHashBuffer, NULL)
*** 917,927 **** ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } // Copy encoding from Java to native buffer ! pbCertEncoding = new jbyte[jCertEncodingSize]; env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding); // Create a certificate context from the encoded cert if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING, (BYTE*) pbCertEncoding, jCertEncodingSize))) { --- 961,974 ---- ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } // Copy encoding from Java to native buffer ! pbCertEncoding = new (env) jbyte[jCertEncodingSize]; ! if (pbCertEncoding == NULL) { ! __leave; ! } env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding); // Create a certificate context from the encoded cert if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING, (BYTE*) pbCertEncoding, jCertEncodingSize))) {
*** 930,940 **** __leave; } // Set the certificate's friendly name int size = env->GetStringLength(jCertAliasName); ! pszCertAliasName = new WCHAR[size + 1]; jCertAliasChars = env->GetStringChars(jCertAliasName, NULL); memcpy(pszCertAliasName, jCertAliasChars, size * sizeof(WCHAR)); pszCertAliasName[size] = 0; // append the string terminator --- 977,990 ---- __leave; } // Set the certificate's friendly name int size = env->GetStringLength(jCertAliasName); ! pszCertAliasName = new (env) WCHAR[size + 1]; ! if (pszCertAliasName == NULL) { ! __leave; ! } jCertAliasChars = env->GetStringChars(jCertAliasName, NULL); memcpy(pszCertAliasName, jCertAliasChars, size * sizeof(WCHAR)); pszCertAliasName[size] = 0; // append the string terminator
*** 968,978 **** ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } ! pszContainerName = new char[dwDataLen]; if (! ::CryptGetProvParam( (HCRYPTPROV) hCryptProv, PP_CONTAINER, (BYTE *) pszContainerName, --- 1018,1031 ---- ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } ! pszContainerName = new (env) char[dwDataLen]; ! if (pszContainerName == NULL) { ! __leave; ! } if (! ::CryptGetProvParam( (HCRYPTPROV) hCryptProv, PP_CONTAINER, (BYTE *) pszContainerName,
*** 982,992 **** ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } // Convert to a wide char string ! pwszContainerName = new WCHAR[dwDataLen]; if (mbstowcs(pwszContainerName, pszContainerName, dwDataLen) == 0) { ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } --- 1035,1048 ---- ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } // Convert to a wide char string ! pwszContainerName = new (env) WCHAR[dwDataLen]; ! if (pwszContainerName == NULL) { ! __leave; ! } if (mbstowcs(pwszContainerName, pszContainerName, dwDataLen) == 0) { ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; }
*** 1005,1015 **** ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } ! pszProviderName = new char[dwDataLen]; if (! ::CryptGetProvParam( (HCRYPTPROV) hCryptProv, PP_NAME, (BYTE *) pszProviderName, --- 1061,1074 ---- ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } ! pszProviderName = new (env) char[dwDataLen]; ! if (pszProviderName == NULL) { ! __leave; ! } if (! ::CryptGetProvParam( (HCRYPTPROV) hCryptProv, PP_NAME, (BYTE *) pszProviderName,
*** 1019,1029 **** ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } // Convert to a wide char string ! pwszProviderName = new WCHAR[dwDataLen]; if (mbstowcs(pwszProviderName, pszProviderName, dwDataLen) == 0) { ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } --- 1078,1091 ---- ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } // Convert to a wide char string ! pwszProviderName = new (env) WCHAR[dwDataLen]; ! if (pwszProviderName == NULL) { ! __leave; ! } if (mbstowcs(pwszProviderName, pszProviderName, dwDataLen) == 0) { ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; }
*** 1159,1169 **** ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } // Copy encoding from Java to native buffer ! pbCertEncoding = new jbyte[jCertEncodingSize]; env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding); // Create a certificate context from the encoded cert if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING, (BYTE*) pbCertEncoding, jCertEncodingSize))) { --- 1221,1234 ---- ThrowException(env, KEYSTORE_EXCEPTION, GetLastError()); __leave; } // Copy encoding from Java to native buffer ! pbCertEncoding = new (env) jbyte[jCertEncodingSize]; ! if (pbCertEncoding == NULL) { ! __leave; ! } env->GetByteArrayRegion(jCertEncoding, 0, jCertEncodingSize, pbCertEncoding); // Create a certificate context from the encoded cert if (!(pCertContext = ::CertCreateCertificateContext(X509_ASN_ENCODING, (BYTE*) pbCertEncoding, jCertEncodingSize))) {
*** 1182,1192 **** // Check that its friendly name matches the supplied alias if ((cchNameString = ::CertGetNameString(pTBDCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) > 1) { ! pszNameString = new char[cchNameString]; ::CertGetNameString(pTBDCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString, cchNameString); --- 1247,1260 ---- // Check that its friendly name matches the supplied alias if ((cchNameString = ::CertGetNameString(pTBDCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) > 1) { ! pszNameString = new (env) char[cchNameString]; ! if (pszNameString == NULL) { ! __leave; ! } ::CertGetNameString(pTBDCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString, cchNameString);
*** 1332,1342 **** CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) == 1) { continue; // not found } ! pszNameString = new char[cchNameString]; if (::CertGetNameString(pCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString, cchNameString) == 1) { --- 1400,1413 ---- CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, NULL, 0)) == 1) { continue; // not found } ! pszNameString = new (env) char[cchNameString]; ! if (pszNameString == NULL) { ! __leave; ! } if (::CertGetNameString(pCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString, cchNameString) == 1) {
*** 1508,1518 **** BYTE tmp; __try { // Copy data from Java buffer to native buffer ! pData = new jbyte[dwBufLen]; env->GetByteArrayRegion(jData, 0, dwBufLen, pData); if (doEncrypt == JNI_TRUE) { // encrypt if (! ::CryptEncrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData, --- 1579,1592 ---- BYTE tmp; __try { // Copy data from Java buffer to native buffer ! pData = new (env) jbyte[dwBufLen]; ! if (pData == NULL) { ! __leave; ! } env->GetByteArrayRegion(jData, 0, dwBufLen, pData); if (doEncrypt == JNI_TRUE) { // encrypt if (! ::CryptEncrypt((HCRYPTKEY) hKey, 0, TRUE, 0, (BYTE *)pData,
*** 1582,1592 **** ThrowException(env, KEY_EXCEPTION, GetLastError()); __leave; } ! pbKeyBlob = new BYTE[dwBlobLen]; // Generate key blob if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, pbKeyBlob, &dwBlobLen)) { --- 1656,1669 ---- ThrowException(env, KEY_EXCEPTION, GetLastError()); __leave; } ! pbKeyBlob = new (env) BYTE[dwBlobLen]; ! if (pbKeyBlob == NULL) { ! __leave; ! } // Generate key blob if (! ::CryptExportKey((HCRYPTKEY) hCryptKey, 0, PUBLICKEYBLOB, 0, pbKeyBlob, &dwBlobLen)) {
*** 1636,1647 **** __leave; } RSAPUBKEY* pRsaPubKey = (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC)); int len = sizeof(pRsaPubKey->pubexp); ! exponentBytes = new jbyte[len]; // convert from little-endian while copying from blob for (int i = 0, j = len - 1; i < len; i++, j--) { exponentBytes[i] = ((BYTE*) &pRsaPubKey->pubexp)[j]; } --- 1713,1728 ---- __leave; } RSAPUBKEY* pRsaPubKey = (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC)); + int len = sizeof(pRsaPubKey->pubexp); ! exponentBytes = new (env) jbyte[len]; ! if (exponentBytes == NULL) { ! __leave; ! } // convert from little-endian while copying from blob for (int i = 0, j = len - 1; i < len; i++, j--) { exponentBytes[i] = ((BYTE*) &pRsaPubKey->pubexp)[j]; }
*** 1688,1700 **** __leave; } RSAPUBKEY* pRsaPubKey = (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC)); - int len = pRsaPubKey->bitlen / 8; ! modulusBytes = new jbyte[len]; BYTE * pbModulus = (BYTE *) (keyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY)); // convert from little-endian while copying from blob for (int i = 0, j = len - 1; i < len; i++, j--) { --- 1769,1784 ---- __leave; } RSAPUBKEY* pRsaPubKey = (RSAPUBKEY *) (keyBlob + sizeof(PUBLICKEYSTRUC)); ! int len = pRsaPubKey->bitlen / 8; ! modulusBytes = new (env) jbyte[len]; ! if (modulusBytes == NULL) { ! __leave; ! } BYTE * pbModulus = (BYTE *) (keyBlob + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY)); // convert from little-endian while copying from blob for (int i = 0, j = len - 1; i < len; i++, j--) {
*** 1811,1826 **** jBlobLength = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (jKeyBitLength / 8); } ! jbyte* jBlobBytes = new jbyte[jBlobLength]; jbyte* jBlobElement; jbyteArray jBlob = NULL; jsize jElementLength; __try { BLOBHEADER *pBlobHeader = (BLOBHEADER *) jBlobBytes; if (bGeneratePrivateKeyBlob) { pBlobHeader->bType = PRIVATEKEYBLOB; // 0x07 } else { --- 1895,1914 ---- jBlobLength = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (jKeyBitLength / 8); } ! jbyte* jBlobBytes; jbyte* jBlobElement; jbyteArray jBlob = NULL; jsize jElementLength; __try { + jBlobBytes = new (env) jbyte[jBlobLength]; + if (jBlobBytes == NULL) { + __leave; + } BLOBHEADER *pBlobHeader = (BLOBHEADER *) jBlobBytes; if (bGeneratePrivateKeyBlob) { pBlobHeader->bType = PRIVATEKEYBLOB; // 0x07 } else {
< prev index next >