IBM / OpenJCEPlus

This project makes use of Java and C/C++. This project will create OpenJCEPlus and OpenJCEPlusFIPS cryptographic providers which are implementations of the Java™ Cryptography Extensions (JCE) APIs. The actual cryptographic code will come from the OpenCryptographyKitC project which is based on OpenSSL.
Apache License 2.0
4 stars 12 forks source link

Fix the encoding formats for X25519 and X448 #136

Closed taoliult closed 2 months ago

taoliult commented 3 months ago

This PR fixes the XDH Private Key and Public Key encoding formats issues.

According to the PKCS#8 Private-Key Specification, the new format privateKey is an octet string whose contents are the value of the private key. So, adding the octet string before the private key when passing the private key object to its parent class PKCS8Key key object for 17 and after version.

According to Sun old versions, 11 and before, the new XDH format is not supported. So, adding a DER "null" value on the OID sequence only for 11 and before versions.

jasonkatonica commented 3 months ago

Also please include a fixes link within the above commit and PR text to issue https://github.com/IBM/OpenJCEPlus/issues/100 such that this update gets linked to the issue being fixed.

taoliult commented 2 months ago

I had a couple questions on this code.

Looking through the code I was concerned that this.key was not set to the same key value as k and vice versa at various points in time. I couldn't exactly find a case / path where this would occur but it does seem a bit fragile keeping slightly different copies of the similar values.

@jasonkatonica Checked the XDHPrivateKeyImpl from SunEC codes. From JDK17 and after version, the raw k object in XDHPrivateKeyImpl is different with the key object in PKCS8Key(code links below). So, I am thinking, follow the similar way as SunEC did. And also, run the TestAll tests and all the 3926 tests passed.

https://github.com/ibmruntimes/openj9-openjdk-jdk11/blob/openj9/src/jdk.crypto.ec/share/classes/sun/security/ec/XDHPrivateKeyImpl.java#L52

https://github.com/ibmruntimes/openj9-openjdk-jdk17/blob/openj9/src/jdk.crypto.ec/share/classes/sun/security/ec/XDHPrivateKeyImpl.java#L56