Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8293808: mscapi destroyKeyContainer enhance KeyStoreException: Access…
… is denied exception

Backport-of: 36c9034ff1274f37969550a3f9239f1bb16a0b25
  • Loading branch information
MBaesken committed Sep 28, 2022
1 parent fa8feb4 commit 7425564
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/jdk.crypto.mscapi/windows/native/libsunmscapi/security.cpp
Expand Up @@ -128,22 +128,43 @@ void ThrowExceptionWithMessage(JNIEnv *env, const char *exceptionName,
}
}

void ThrowExceptionWithMessageAndErrcode(JNIEnv *env, const char *exceptionName,
const char *msg, DWORD dwError) {
char szMessage[500];
szMessage[0] = '\0';
char szMessage2[1024];
szMessage2[0] = '\0';

DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
NULL, szMessage, sizeof(szMessage), NULL);
if (res == 0) {
strcpy(szMessage, "Unknown error");
}
snprintf(szMessage2, sizeof(szMessage2), "%s: error %lu, %s", msg, dwError, szMessage);

ThrowExceptionWithMessage(env, exceptionName, szMessage2);
}


/*
* 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];
char szMessage[500];
szMessage[0] = '\0';
char szMessage2[1024];
szMessage2[0] = '\0';

DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
NULL, szMessage, sizeof(szMessage), NULL);
if (res == 0) {
strcpy(szMessage, "Unknown error");
}
snprintf(szMessage2, sizeof(szMessage2), "error %lu, %s", dwError, szMessage);

ThrowExceptionWithMessage(env, exceptionName, szMessage);
ThrowExceptionWithMessage(env, exceptionName, szMessage2);
}

/*
Expand Down Expand Up @@ -1834,8 +1855,7 @@ JNIEXPORT void JNICALL Java_sun_security_mscapi_CKeyStore_destroyKeyContainer
// Destroying the default key container is not permitted
// (because it may contain more one keypair).
if (pszKeyContainerName == NULL) {

ThrowException(env, KEYSTORE_EXCEPTION, NTE_BAD_KEYSET_PARAM);
ThrowExceptionWithMessage(env, KEYSTORE_EXCEPTION, "key container name was NULL, NTE_BAD_KEYSET_PARAM");
__leave;
}

Expand All @@ -1847,7 +1867,7 @@ JNIEXPORT void JNICALL Java_sun_security_mscapi_CKeyStore_destroyKeyContainer
PROV_RSA_FULL,
CRYPT_DELETEKEYSET) == FALSE)
{
ThrowException(env, KEYSTORE_EXCEPTION, GetLastError());
ThrowExceptionWithMessageAndErrcode(env, KEYSTORE_EXCEPTION, "CryptAcquireContext failure", GetLastError());
__leave;
}

Expand Down

1 comment on commit 7425564

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.