Skip to content

8358451: SunJCE PBEKey impl should throw IllegalStateException when getEncoded() is called #25632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

valeriepeng
Copy link
Contributor

@valeriepeng valeriepeng commented Jun 4, 2025

Update the PBEKey class of the SunJCE provider which override the javax.security.auth.Destroyable interface to

  1. throw IllegalStateException if getEncoded() is called after key is destroyed
  2. serialization of such destroyed PBEKey object will lead to exception.

Also update the PBEKeyFactory class of the SunJCE provider to check for destroyed keys and throw exceptions per the method javadoc.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8358451: SunJCE PBEKey impl should throw IllegalStateException when getEncoded() is called (Bug - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25632/head:pull/25632
$ git checkout pull/25632

Update a local copy of the PR:
$ git checkout pull/25632
$ git pull https://git.openjdk.org/jdk.git pull/25632/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25632

View PR using the GUI difftool:
$ git pr show -t 25632

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25632.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 4, 2025

👋 Welcome back valeriep! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jun 4, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 4, 2025
@openjdk
Copy link

openjdk bot commented Jun 4, 2025

@valeriepeng The following label will be automatically applied to this pull request:

  • security

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@mlbridge
Copy link

mlbridge bot commented Jun 4, 2025

Webrevs

Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we also throw ISE when getFormat and getAlgorithm are called? Calling these methods after the key is destroyed looks suspicious and may reveal a coding error.

Copy link
Contributor Author

@valeriepeng valeriepeng Jun 6, 2025

Choose a reason for hiding this comment

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

Well, I see that throw IllegalStateException for almost all methods seems to be the style for the Destroyable impl classes under the javax.security.auth.kerberos package. But I am not sure if this would be too "noisy" for Key objects. At a minimum, we should throw IllegalStateException for method which trying to use the sensitive info which has been cleared out. However, if we throw IllegalStateException for all methods such as getAlgorithm(), then it may lead to even more IllegalStateException being thrown unexpectedly and may make troubleshooting harder.

@@ -80,6 +81,9 @@ final class PBEKey implements SecretKey {

public byte[] getEncoded() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand this is not a public API class so there is no need to provide @throws in the spec. But, on the other hand, do we need to provide one in its super class java.security.key? I have no opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I am not sure. java.securityKey is the super interface for all keys, including private, public, and secret keys. The Exception is due to implementing the javax.security.auth.Destroyable interface, so the @throws spec should goes to the class which implements both Destroyable and Key interfaces. PBEKey is not a public API class, so perhaps we can documenting this in the SunJCE provider section as this is implementation-specific?

Copy link
Member

Choose a reason for hiding this comment

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

Also, PublicKey does not implement (or implement by inheritance) Destroyable.

@@ -202,6 +205,9 @@ private void readObject(java.io.ObjectInputStream s)
@java.io.Serial
private Object writeReplace() throws java.io.ObjectStreamException {
try {
if (isDestroyed()) {
throw new NotSerializableException("key is destroyed");
Copy link
Member

Choose a reason for hiding this comment

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

nit: for uniformity, you may want to capitalize key since it is capitalized elsewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, will fix.

@@ -80,6 +81,9 @@ final class PBEKey implements SecretKey {

public byte[] getEncoded() {
try {
if (isDestroyed()) {
throw new IllegalStateException("key is destroyed");
Copy link
Member

Choose a reason for hiding this comment

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

nit: for uniformity, you may want to capitalize key since it is capitalized elsewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, will fix.

JCA SecretKeyFactory class.
Changed to throw ISE whenever destroyed key is encountered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rfr Pull request is ready for review security [email protected]
Development

Successfully merging this pull request may close these issues.

3 participants