Skip to content
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

Replaced openssl with Java security to add key/cert into keystore #11224

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ppatierno
Copy link
Member

@ppatierno ppatierno commented Mar 4, 2025

This PR is going to replace the usage of OpenSSL tooling for adding a key/cert pair to the keystore with Java security framework.
While running tests I compared what we get by using openssl and what we get with Java.
I was able to tune the algorithm being used to encrypt the private key when it's stored, in order to have AES-128-CBC with 2048 iterations (which are the parameters with current openssl tooling vs the default Java which uses AES-256-CBC and 10000 iterations instead).

PKCS7 Data
Shrouded Keybag: PBES2, PBKDF2, AES-128-CBC, Iteration 2048, PRF hmacWithSHA256
Bag Attributes
    friendlyName: ca
    localKeyID: 54 69 6D 65 20 31 37 34 31 30 38 38 31 39 35 36 32 39 
Key Attributes: <No Attributes>

The only difference is about the HMAC used for the keystore integrity.
The openssl tool uses 2048 iterations and salt length 8 bytes.

MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8

Java defaults to use 10000 iterations and salt length 10 bytes.

MAC: sha256, Iteration 10000
MAC length: 32, salt length: 20

I couldn't find any way to tune them both. Java doesn't expose anything to do so. It's only possible to tune the iterations but by setting a global Java properties like this:

System.setProperty("keystore.pkcs12.macIterationCount", "2048");

Tbh I am not keen to have such addition because it would be a global setting on the JVM.
So compared to openssl with have more secure HMAC which should not be a problem (the opposite I would say).

@ppatierno ppatierno added this to the 0.46.0 milestone Mar 4, 2025
@ppatierno ppatierno requested review from katheris and a team March 4, 2025 16:02
@ppatierno
Copy link
Member Author

ppatierno commented Mar 4, 2025

I am opening this PR to discuss it because we are not going to have exactly the same result as described.

@ppatierno ppatierno marked this pull request as ready for review March 4, 2025 16:05
.optArg("-keypbe", "aes-128-cbc")
.optArg("-macalg", "sha256")
.exec();
public void addKeyAndCertToKeyStore(File keyFile, File certFile, String alias, File keyStoreFile, String keyStorePassword)
Copy link
Member

Choose a reason for hiding this comment

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

Just curious...Shouldn't this method be moved to the CertManager class instead of being in OpenSslCertManager as it does not use any OpenSSL invocation method?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good point, however there are other methods not using openssl anymore already (i.e. addCertToTrustStore, deleteFromTrustStore, ...). At some point we could think about renaming it to not be strictly tight to OpenSsl.

@ppatierno
Copy link
Member Author

/azp run regression

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants