Skip to content

Conversation

@valeriepeng
Copy link
Contributor

@valeriepeng valeriepeng commented Jul 25, 2025

This PR is for clarifying the NoSuchAlgorithmException and NoSuchPaddingException for the Cipher.getInstance(String transformation, Provider provider) and Cipher.getInstance(String transformation, String provider) methods.

As stated in javax.crypto.CipherSpi class, provider has the flexibility to register their implementations through various sub-transformations. As a result, depending on how the providers register the implementation, it may lead to NoSuchAlgorithmException or NoSuchPaddingException. For example, the provider A registers to support "AES/CBC/PKCS5Padding" vs provider B registers to support "AES" (but would only accept "CBC" and "PKCS5Padding" as the valid input for setting mode and padding). Calling Cipher.getInstance(...) with "AES/CBC/NoPadding" against provider A and B would lead to NoSuchAlgorithmException and NoSuchPaddingException. This javadoc update hope to make it clear.

Thanks in advance for the review~
Valerie


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
  • Change requires CSR request JDK-8364134 to be approved

Issues

  • JDK-8360463: Ambiguity in Cipher.getInstance() specification between NoSuchAlgorithmException and NoSuchPaddingException (Bug - P3)
  • JDK-8364134: Ambiguity in Cipher.getInstance() specification between NoSuchAlgorithmException and NoSuchPaddingException (CSR)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26489

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

Using diff file

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

Using Webrev

Link to Webrev Comment

…chAlgorithmException and NoSuchPaddingException
@bridgekeeper
Copy link

bridgekeeper bot commented Jul 25, 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 Jul 25, 2025

@valeriepeng This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8360463: Ambiguity in Cipher.getInstance() specification between NoSuchAlgorithmException and NoSuchPaddingException

Reviewed-by: mullan

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 794 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

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

openjdk bot commented Jul 25, 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.

@openjdk openjdk bot added the security security-dev@openjdk.org label Jul 25, 2025
@valeriepeng
Copy link
Contributor Author

/csr

@openjdk openjdk bot added the csr Pull request needs approved CSR before integration label Jul 25, 2025
@openjdk
Copy link

openjdk bot commented Jul 25, 2025

@valeriepeng has indicated that a compatibility and specification (CSR) request is needed for this pull request.

@valeriepeng please create a CSR request for issue JDK-8360463 with the correct fix version. This pull request cannot be integrated until the CSR request is approved.

@mlbridge
Copy link

mlbridge bot commented Jul 25, 2025

@haimaychao
Copy link
Contributor

Changes look good with minor suggestions.

*
* @throws NoSuchPaddingException if {@code transformation}
* contains a padding scheme that is not available
* @throws NoSuchPaddingException if a {@code CipherSpi} object
Copy link
Contributor

Choose a reason for hiding this comment

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

suggest to s/{@code CipherSpi} object/{@code CipherSpi} implementation, consistent with other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will change~

* @throws NoSuchPaddingException if {@code transformation}
* contains a padding scheme that is not available
* @throws NoSuchPaddingException if a {@code CipherSpi} object
* from the {@code provider} is found using the algorithm
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest to s/from the/from the specified, and to make the same changes in this method at lines #567 and #568, s/the specified provider/the specified {@code provider}.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will change~

*
* @throws NoSuchPaddingException if {@code transformation}
* contains a padding scheme that is not available
* @throws NoSuchPaddingException if the {@code CipherSpi} object
Copy link
Contributor

Choose a reason for hiding this comment

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

same suggestion as above.

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

* @throws NoSuchPaddingException if {@code transformation}
* contains a padding scheme that is not available
* @throws NoSuchPaddingException if the {@code CipherSpi} object
* from the {@code provider} is found using the algorithm
Copy link
Contributor

Choose a reason for hiding this comment

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

same suggestion as above.

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

@wangweij
Copy link
Contributor

Is it worth demonstrating the different behaviors with a test?

@valeriepeng
Copy link
Contributor Author

Is it worth demonstrating the different behaviors with a test?

This is covered by existing TCK test and thus the request for clarification. This is provider-specific, i.e. depending on how provider registers its implementation, it may leads to different exceptions. Given that the difference is only the type of exception, e.g. NoSuchAlgorithmException vs NoSuchPaddingException, I don't feel it's that critical to add a regression test in addition to the existing TCK test.

@wangweij
Copy link
Contributor

I wonder if we should list NSPE before NSAE, since NSAE refers to it.

Or, can we just say NSAE is thrown when the specified algorithm and mode are not supported?

@wangweij
Copy link
Contributor

Also, is this consistent among other security providers?

@valeriepeng
Copy link
Contributor Author

valeriepeng commented Aug 20, 2025

I wonder if we should list NSPE before NSAE, since NSAE refers to it.

Or, can we just say NSAE is thrown when the specified algorithm and mode are not supported?

I see your point to list NSPE before NSAE. However, it seems that the exceptions are currently ordered alphabetically... Given the NSAE under getInstance(String) does not refer to NSPE, I am inclined to keep the current ordering, otherwise it looks inconsistent?

As for the NSAE vs NSPE, it's somewhat complicated due to the various provider registrations documented in the javax.crypto.CipherSpi class. There could be situations which NSAE is thrown besides the unsupported algorithm and/or mode. E.g. provider supports the algorithm and mode and a different padding scheme and registers the support through full transformation instead of only the algorithm and mode. The former case leads to NSAE vs the other one leads to NSPE.

@valeriepeng
Copy link
Contributor Author

Also, is this consistent among other security providers?

Whether it's NSAE or NSPE depends on how security provider registers the support. The updated wording in the PR reflects the current JCA behavior, i.e. Cipher lookup, and should be consistent across providers.

* contains a padding scheme that is not available
* @throws NoSuchPaddingException if a {@code CipherSpi} implementation
* from the specified {@code provider} is found but it does not
* support the padding scheme
Copy link
Contributor

Choose a reason for hiding this comment

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

The wording is correct now. Small nit: the NSAE one uses "but does not" and the NSPE one uses "but it does not". You might want to make them the same.

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 do.

* or if a {@code CipherSpi} implementation for the
* specified algorithm is not available from the specified
* provider
* or if a {@code CipherSpi} implementation from the specified

Choose a reason for hiding this comment

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

IMO,
NoSuchAlgorithmException - if transformation is null, empty, in an invalid format, or if a CipherSpi implementation for the specified transformation is not available from the specified provider

The transformation can be linked to the class level mention

A transformation is of the form:
"algorithm/mode/padding" or
"algorithm"

Copy link
Contributor

Choose a reason for hiding this comment

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

It's important to point out that when a matched (either full or partial) transform is found, not able to set mode throws an NSAE, while not able to set padding throws an NSPE. So NSAE is related on mode availability and NSPE to padding availability. This is why the current PR need to mention mode here.

Choose a reason for hiding this comment

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

Thanks for clarifying this.

System.out.println("Expected transformation: " + transformation);
}
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be good to enhance this test to distinguish between the expected exceptions for each transformation.

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, which exception is thrown depends on which provider is used and how it registers its implementations. Pinpointing the exact Exception would require running the test against a specific provider which we know how the implementations are registered. Without tying the test to a specific provider, existing transformations in this test may lead to either NSAE or NSPE, thus I just add NSPE to the catch clause. I can add more transformations which would lead to only NSAE assuming this is what you want to see?

Copy link
Member

Choose a reason for hiding this comment

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

I think it is ok to assume the JDK provider configuration has not been changed.

Wouldn't "ChaCha20/None/PKCS5Padding" and ""ChaCha20-Poly1305/None/PKCS5Padding"" always throw NoSuchPaddingException, assuming the current JDK configuration is not changed? Maybe instead of passing a boolean expected parameter, you could pass the expected Exception, or null if none expected.

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, I have adopted your suggestion.

("Cannot find any provider supporting " + transformation, failure);
if (failure instanceof NoSuchPaddingException nspe) {
throw nspe;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Small nit, you don't need the else block here.

checkTransformation("ChaCha20-Poly1305/ECB/NoPadding", false);
checkTransformation("ChaCha20-Poly1305/None/PKCS5Padding", false);
checkTransformation("ChaCha20/ECB/NoPadding", Expected.NSAE_OR_NSPE);
checkTransformation("ChaCha20/None/PKCS5Padding", Expected.NSAE_OR_NSPE);
Copy link
Member

@seanjmullan seanjmullan Sep 4, 2025

Choose a reason for hiding this comment

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

Isn't this always going to throw NSPE given the current provider configuration? Also, same comment on line 88.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can explicit specify SunJCE provider in the getInstance(...) call and then match the specific exception based on its impl.

@openjdk openjdk bot removed the csr Pull request needs approved CSR before integration label Sep 15, 2025
@valeriepeng
Copy link
Contributor Author

Release Note is filed @ https://bugs.openjdk.org/browse/JDK-8367577
Thanks in advance for review~

@seanjmullan
Copy link
Member

Release Note is filed @ https://bugs.openjdk.org/browse/JDK-8367577 Thanks in advance for review~

Made a couple of edits but otherwise looks fine. Also, you can use markdown instead of html, if you like.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 18, 2025
@wangweij
Copy link
Contributor

Release Note is filed @ https://bugs.openjdk.org/browse/JDK-8367577 Thanks in advance for review~

Made a couple of edits but otherwise looks fine. Also, you can use markdown instead of html, if you like.

Is it common to use italics instead of fixed width for exception types?

* transformation
*
* @throws NoSuchAlgorithmException if {@code transformation}
* is {@code null}, empty, in an invalid format,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to this change, but do we need an "or" before "in an valid format"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, how about this?

@throws NoSuchAlgorithmException if {@code transformation}
        is {@code null}, empty or in an invalid format,
        or if a {@code CipherSpi} implementation is not found or
        is found but does not support the mode

Copy link
Member

@seanjmullan seanjmullan Sep 18, 2025

Choose a reason for hiding this comment

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

A semi-colon (instead of a comma) after "format" might also help separate the 2 main error conditions:

@throws NoSuchAlgorithmException if {@code transformation}
        is {@code null}, empty or in an invalid format;
        or if a {@code CipherSpi} implementation is not found or
        is found but does not support the mode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will use this. Thanks for the suggestions @wangweij @seanjmullan

Copy link
Contributor

Choose a reason for hiding this comment

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

Now that we are using semi-colon, do we dare add back the commas?

Copy link
Member

Choose a reason for hiding this comment

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

Don't think it makes a significant difference.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Sep 22, 2025
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 23, 2025
@valeriepeng
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Sep 25, 2025

Going to push as commit 5c596e2.
Since your change was applied there have been 847 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Sep 25, 2025
@openjdk openjdk bot closed this Sep 25, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Sep 25, 2025
@openjdk
Copy link

openjdk bot commented Sep 25, 2025

@valeriepeng Pushed as commit 5c596e2.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@valeriepeng valeriepeng deleted the JDK-8360463 branch September 25, 2025 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integrated Pull request has been integrated security security-dev@openjdk.org

Development

Successfully merging this pull request may close these issues.

5 participants