Skip to content

Conversation

@ascarpino
Copy link
Contributor

@ascarpino ascarpino commented Sep 8, 2025

Hi

Please review the Second Preview for the PEM API. The most significant changes from JEP 470 are:

  • Renamed the name of PEMRecord class to PEM.
  • Revised the new encryptKey methods of the EncryptedPrivateKeyInfo class to accept DEREncodable objects rather than just PrivateKey objects so that cryptographic objects with public keys, i.e., KeyPair and PKCS8EncodedKeySpec, can also be encrypted.
  • Enhanced the PEMEncoder and PEMDecoder classes to support the encryption and decryption of KeyPair and PKCS8EncodedKeySpec objects.

thanks

Tony


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change requires CSR request JDK-8367431 to be approved
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issues

  • JDK-8360564: Implement JEP 524: PEM Encodings of Cryptographic Objects (Second Preview) (Enhancement - P4)
  • JDK-8367431: Implement JEP 524: PEM Encodings of Cryptographic Objects (Second Preview) (CSR)

Reviewers

Reviewers without OpenJDK IDs

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27147

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 8, 2025

👋 Welcome back ascarpino! 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 Sep 8, 2025

@ascarpino 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:

8360564: Implement JEP 524: PEM Encodings of Cryptographic Objects (Second Preview)

Reviewed-by: weijun, 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 98 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
Copy link

openjdk bot commented Sep 8, 2025

@ascarpino 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.

@ascarpino ascarpino changed the title Enhancement - null JDK-8360564 Implementation of PEM Encodings of Cryptographic Objects (Second Preview) JDK-8360564 Implementation of PEM Encodings of Cryptographic Objects (Second Preview) Sep 10, 2025
@ascarpino ascarpino changed the title JDK-8360564 Implementation of PEM Encodings of Cryptographic Objects (Second Preview) 8360564: Implementation of PEM Encodings of Cryptographic Objects (Second Preview) Sep 10, 2025
@ascarpino ascarpino changed the title 8360564: Implementation of PEM Encodings of Cryptographic Objects (Second Preview) 8360564: Implement JEP XXX: PEM Encodings of Cryptographic Objects (Second Preview) Sep 10, 2025
@ascarpino
Copy link
Contributor Author

/csr

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

openjdk bot commented Sep 11, 2025

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

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

@ascarpino ascarpino changed the title 8360564: Implement JEP XXX: PEM Encodings of Cryptographic Objects (Second Preview) 8360564: Implement JEP 524: PEM Encodings of Cryptographic Objects (Second Preview) Sep 11, 2025
@ascarpino ascarpino marked this pull request as ready for review September 11, 2025 16:52
@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 11, 2025
@mlbridge
Copy link

mlbridge bot commented Sep 11, 2025

Copy link
Contributor

@wangweij wangweij left a comment

Choose a reason for hiding this comment

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

Some comments.

String p = pem.content().replaceAll("(.{64})", "$1\r\n");
return pemEncoded(pem.type(), p);
}

Copy link
Contributor

@wangweij wangweij Sep 12, 2025

Choose a reason for hiding this comment

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

Is it correct to put the 2 new methods here? Seems not closely related to PEM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are needed by PEM and EKPI, so this seems like the right place.

* @param pair set to true for returning a KeyPair, if possible. Otherwise,
* return a PrivateKey
* @param provider KeyFactory provider
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

This method is very similar to PKCS8Key::parseKey. Maybe we should combine them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before the code review, I intentionally split these apart. I didn't like how the code looked and that returning a DEREncodable complicated parseKey(). It made PKCS8Key much more complicated with typecasting and passing in pair.

If I redo PKCS8Key.java, this would be on the list to find a more elegant way to merge them.

// Store key material for subclasses to parse
privKeyMaterial = val.data.getOctetString();

// Special check and parsing for ECDSA's SEC1v2 format
Copy link
Contributor

@wangweij wangweij Sep 12, 2025

Choose a reason for hiding this comment

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

So there are 2 ways to extract public key from PKCS8. Are you going to check whether they match?

Also, can we add a test case for this 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.

In this method, there is only one path for SEC1v2 requires the version to be 1, PKCS8 requires the version to be 2. The two should not intersect in this method.

It is true that a version 2 OneAsymmetricKey could contain a SEC1v2 private key encoding. At that point a KeyFactory needs to generate both keys to verify. I think that goes beyond what is necessary for what amounts to a "user error" in putting the wrong public and private key pair together.

I would like to explore redoing PKCS8Key.java as it has taken on many more tasks with encoding & decoding different algorithms. But I don't want that to be tied to a JEP, rather a bug or RFE.

Arrays.fill(oct, (byte) 0x0);
if (seq.data.available() != 0) {
DerValue derValue = seq.data.getDerValue();
if (derValue.isContextSpecific((byte) 1)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If any of these ifs is false null is returned. Would you rather throw an IAE?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see there could be a

parameters [0] ECDomainParameters {{ SECGCurveNames }} OPTIONAL,

Shall we skip it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This only checks if one is available in the private key material. If there is none, null is fine.

The domain parameters are kept as part of private key SEC1v2 encoding and can be read when generating a private key with a KeyFactory. Translating the encoding could be error-prone, and maybe incompatible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I meant if both [0] and [1] are there you can skip [0] and read [1]. Currently you just check for [1].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes, I didn't add a skip.

* CERTIFICATE, CERTIFICATE REQUEST, ATTRIBUTE CERTIFICATE, X509 CRL, PKCS7, CMS,
* PRIVATE KEY, ENCRYPTED PRIVATE KEY, RSA PRIVATE KEY, or PUBLIC KEY.
*
* <p> {@code leadingData} may be null if no non-PEM data preceded PEM header
Copy link
Member

Choose a reason for hiding this comment

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

Suggest rewording as "... if there was no data preceding the PEM header ..."

Why do you allow null when there is a ctor that does not have a leadingData parameter - is there a reason for this?

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 thought it was more friendly to specify a constructor when leadingData was known to be not needed. The other constructor is used by PEMDecoder decoding is not known until decoding the stream occurs.

Copy link
Contributor

@wangweij wangweij left a comment

Choose a reason for hiding this comment

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

Very minor comments.

Copy link
Contributor

@wangweij wangweij left a comment

Choose a reason for hiding this comment

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

Some tiny comments.

Copy link
Contributor

@koushikthirupattur koushikthirupattur left a comment

Choose a reason for hiding this comment

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

LGTM!

* @return a decoded byte array
* @throws IllegalArgumentException if decoding fails
*/
final public byte[] decode() {
Copy link
Member

Choose a reason for hiding this comment

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

This method doesn't seem to be covered by the tests. I have created a ticket to add this https://bugs.openjdk.org/browse/JDK-8371574.
It also covers the 2 methods in PEM.java in utils, which could be covered further

@openjdk openjdk bot added ready Pull request is ready to be integrated and removed csr Pull request needs approved CSR before integration labels Nov 14, 2025
@ascarpino
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Nov 14, 2025

Going to push as commit ad3dfaf.
Since your change was applied there have been 98 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 Nov 14, 2025
@openjdk openjdk bot closed this Nov 14, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Nov 14, 2025
@openjdk
Copy link

openjdk bot commented Nov 14, 2025

@ascarpino Pushed as commit ad3dfaf.

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

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 [email protected]

Development

Successfully merging this pull request may close these issues.

6 participants