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

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

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

@openjdk openjdk bot added the security security-dev@openjdk.org label Sep 8, 2025
@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.

* @param tClass the returned object class that implements
* {@code DEREncodable}.
* @return a {@code DEREncodable} typecast to {@code tClass}
* @throws IOException on IO or PEM syntax error where the
Copy link
Member

Choose a reason for hiding this comment

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

You mean they are recoverable? Otherwise it should probably be an IAE.

* @throws IllegalArgumentException on error in decoding
* @throws ClassCastException if {@code tClass} is invalid for the PEM type
* @throws NullPointerException when any input values are null
* @throws ClassCastException if {@code tClass} does not represent the PEM type
Copy link
Member

Choose a reason for hiding this comment

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

It's a little odd this throws a ClassCastException. This seems more like an IllegalArgumentException to me because you are passing in the wrong type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wangweij pushed for CCE instead of IAE. It is a valid argument, but cannot be cast to that object. I think one can make a case for both exception types, but CCE was a bit stronger.

throw new IOException(e);
}
byte[] result = pkcs8Key.generateEncoding().clone();
pkcs8Key.clear();
Copy link
Member

@seanjmullan seanjmullan Oct 21, 2025

Choose a reason for hiding this comment

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

Put this in a finally block so it gets cleared even if generateEncoding throws an exception. You should also clear the return value of generateEncoding (before you clone it). Although I don't think you need the clone, since generateEncoding returns a new array each time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately generateEncoding also assigned the result to the internal field encodedKey.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wangweij is correct, generateEncoding() is setting the instance variable encodedKey. A finally-block is likely necessary if an exception happens after privateKeyMaterial is set in generateEncoding()

Copy link
Member

Choose a reason for hiding this comment

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

Ok, that could be fixed by creating another method that just does the encoding (and change generateEncoding to call that), but maybe we can fix that later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are generic changes to PKCS8Key outside of PEM that I'd like to do, I can look at it then.

* stored in the new instance.
* @return a new {@code PEMEncoder} instance configured for encryption
* @throws NullPointerException when password is {@code null}
* @throws ProviderException if generating the encryption key fails.
Copy link
Member

Choose a reason for hiding this comment

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

This actually throws an IllegalArgumentException if the key derivation fails.

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 wrote the javadoc based on a suggestion, then didn't update when the constructor was changed.

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

csr Pull request needs approved CSR before integration rfr Pull request is ready for review security security-dev@openjdk.org

Development

Successfully merging this pull request may close these issues.

5 participants