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

Reviewed-by: weijun
  • Loading branch information
MBaesken committed Sep 19, 2022
1 parent cbd0688 commit 36c9034
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 @@ -1845,8 +1866,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 @@ -1858,7 +1878,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

3 comments on commit 36c9034

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@MBaesken
Copy link
Member Author

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 36c9034 Sep 22, 2022

Choose a reason for hiding this comment

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

@MBaesken the backport was successfully created on the branch MBaesken-backport-36c9034f in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 36c9034f from the openjdk/jdk repository.

The commit being backported was authored by Matthias Baesken on 19 Sep 2022 and was reviewed by Weijun Wang.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev MBaesken-backport-36c9034f:MBaesken-backport-36c9034f
$ git checkout MBaesken-backport-36c9034f
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev MBaesken-backport-36c9034f

Please sign in to comment.