Skip to content

Commit 2ec2dba

Browse files
authored
Remove work arounds for Java versions less than 15.
1 parent fb88202 commit 2ec2dba

File tree

6 files changed

+19
-41
lines changed

6 files changed

+19
-41
lines changed

kse/src/main/java/org/kse/crypto/ecc/EccUtil.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.security.interfaces.ECKey;
3131
import java.security.interfaces.ECPrivateKey;
3232
import java.security.interfaces.ECPublicKey;
33+
import java.security.interfaces.EdECPrivateKey;
3334
import java.security.spec.ECParameterSpec;
3435
import java.security.spec.PKCS8EncodedKeySpec;
3536
import java.util.List;
@@ -310,9 +311,11 @@ public static EdDSACurves detectEdDSACurve(PublicKey publicKey) {
310311
}
311312

312313
/**
313-
* Converts an Ed25519 or Ed448 PrivateKey object to a BC EdDSAPrivateKey.
314+
* Converts an Ed25519 or Ed448 PrivateKey object to a BC EdDSAPrivateKey. This method is
315+
* usually used to determine the Edwards curve since the BC EdDSAPrivatey provides
316+
* "Ed25519" or "Ed448" in the algorithm, whereas the JDK EdECPrivateKey only says "EdDSA".
314317
* <br>
315-
* If the given privateKey is a JDK EdDSA key (Java 15+), convert it to a BouncyCastle EdDSA key.
318+
* If the given privateKey is a JDK EdDSA key, convert it to a BouncyCastle EdDSA key.
316319
* If the given privateKey is already a BouncyCastle EdDSA key, return it as-is.
317320
* Otherwise (no known implementation class) return null.
318321
*
@@ -325,12 +328,9 @@ public static EdDSAPrivateKey getEdPrivateKey(PrivateKey privateKey) {
325328
}
326329

327330
try {
328-
// Use reflection so that KSE can still compile with JDK 11.
329-
Class<?> c = Class.forName("java.security.interfaces.EdECPrivateKey");
330-
if (c.isAssignableFrom(privateKey.getClass())) {
331-
// Quickest way to convert to a BC EdDSA key. Doesn't require importing any
332-
// Ed25519 or Ed448 specific classes, and it doesn't require using reflection
333-
// to access the JDK 15+ EC crypto provider.
331+
if (privateKey instanceof EdECPrivateKey) {
332+
// Shortest way to convert to a BC EdDSA key. Doesn't require importing any
333+
// Ed25519 or Ed448 specific classes.
334334
KeyFactory kf = KeyFactory.getInstance(privateKey.getAlgorithm(), KSE.BC);
335335
PrivateKey bcPrivateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privateKey.getEncoded()));
336336
return (EdDSAPrivateKey) bcPrivateKey;
@@ -340,14 +340,4 @@ public static EdDSAPrivateKey getEdPrivateKey(PrivateKey privateKey) {
340340
}
341341
return null;
342342
}
343-
344-
/**
345-
* Checks if the given privateKey is an EdDSA private key (Ed25519 or Ed448).
346-
*
347-
* @param key A key
348-
* @return True, if the given key is an EdDSA private key
349-
*/
350-
public static boolean isEdPrivateKey(Key key) {
351-
return key instanceof PrivateKey && getEdPrivateKey((PrivateKey) key) != null;
352-
}
353343
}

kse/src/main/java/org/kse/crypto/keypair/KeyPairUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.security.interfaces.DSAPrivateKey;
4848
import java.security.interfaces.ECPrivateKey;
4949
import java.security.interfaces.ECPublicKey;
50+
import java.security.interfaces.EdECPrivateKey;
5051
import java.security.interfaces.RSAPrivateCrtKey;
5152
import java.security.interfaces.RSAPrivateKey;
5253
import java.security.spec.DSAPrivateKeySpec;
@@ -467,7 +468,7 @@ public static KeyPair generateKeyPair(PrivateKey privateKey) throws CryptoExcept
467468
PublicKey publicKey = kf.generatePublic(publicSpec);
468469
keyPair = new KeyPair(publicKey, privateKey);
469470
}
470-
if (EccUtil.isEdPrivateKey(privateKey)) {
471+
if (privateKey instanceof EdECPrivateKey) {
471472
EdDSAPrivateKey edPrivate = EccUtil.getEdPrivateKey(privateKey);
472473
byte[] pubKeyBytes = edPrivate.getPublicKey().getEncoded();
473474
KeyFactory kf = KeyFactory.getInstance(edPrivate.getAlgorithm(), KSE.BC);

kse/src/main/java/org/kse/crypto/signing/SignatureType.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.bouncycastle.asn1.rosstandart.RosstandartObjectIdentifiers;
5050
import org.kse.crypto.digest.DigestType;
5151
import org.kse.crypto.ecc.EdDSACurves;
52-
import org.kse.version.JavaVersion;
5352

5453
/**
5554
* Enumeration of Signature Types supported by the X509CertUtil class.
@@ -269,14 +268,10 @@ public static List<SignatureType> rsaSignatureTypes() {
269268
signatureTypes.add(SHA384WITHRSAANDMGF1);
270269
signatureTypes.add(SHA512WITHRSAANDMGF1);
271270

272-
// SHA3 signatures cause problems when reading certificates with standard providers (e.g. in P12 keystore)
273-
// because at least up to Java 15 there is no support for SHA3 signatures (see http://openjdk.java.net/jeps/287)
274-
if (JavaVersion.getJreVersion().isAtLeast(JavaVersion.JRE_VERSION_17)) {
275-
signatureTypes.add(SHA3_224WITHRSAANDMGF1);
276-
signatureTypes.add(SHA3_256WITHRSAANDMGF1);
277-
signatureTypes.add(SHA3_384WITHRSAANDMGF1);
278-
signatureTypes.add(SHA3_512WITHRSAANDMGF1);
279-
}
271+
signatureTypes.add(SHA3_224WITHRSAANDMGF1);
272+
signatureTypes.add(SHA3_256WITHRSAANDMGF1);
273+
signatureTypes.add(SHA3_384WITHRSAANDMGF1);
274+
signatureTypes.add(SHA3_512WITHRSAANDMGF1);
280275

281276
return signatureTypes;
282277
}

kse/src/main/java/org/kse/gui/dialogs/DViewAsymmetricKeyFields.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.security.interfaces.DSAPublicKey;
3737
import java.security.interfaces.ECPrivateKey;
3838
import java.security.interfaces.ECPublicKey;
39+
import java.security.interfaces.EdECPrivateKey;
3940
import java.security.interfaces.RSAPrivateCrtKey;
4041
import java.security.interfaces.RSAPrivateKey;
4142
import java.security.interfaces.RSAPublicKey;
@@ -132,7 +133,7 @@ private static String getTitle(Key key) {
132133
return MessageFormat.format(res.getString("DViewAsymmetricKeyFields.PrivateKey.title"), "EC");
133134
} else if (key instanceof EdDSAPublicKey) {
134135
return MessageFormat.format(res.getString("DViewAsymmetricKeyFields.PublicKey.title"), getEdAlg(key));
135-
} else if (EccUtil.isEdPrivateKey(key)) {
136+
} else if (key instanceof EdECPrivateKey) {
136137
return MessageFormat.format(res.getString("DViewAsymmetricKeyFields.PrivateKey.title"), getEdAlg(key));
137138
} else if (key instanceof MLDSAPublicKey) {
138139
return MessageFormat.format(res.getString("DViewAsymmetricKeyFields.PublicKey.title"), "ML-DSA");
@@ -239,7 +240,7 @@ private void populateFields() throws IOException {
239240
fields = getEcPrivateFields();
240241
} else if (key instanceof EdDSAPublicKey) {
241242
fields = getEdPubFields();
242-
} else if (EccUtil.isEdPrivateKey(key)) {
243+
} else if (key instanceof EdECPrivateKey) {
243244
fields = getEdPrivateFields();
244245
} else if (key instanceof MLDSAPublicKey) {
245246
fields = getMLDSAPublicFields();

kse/src/main/java/org/kse/gui/dialogs/DViewPrivateKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.security.PrivateKey;
3434
import java.security.interfaces.DSAPrivateKey;
3535
import java.security.interfaces.ECPrivateKey;
36+
import java.security.interfaces.EdECPrivateKey;
3637
import java.security.interfaces.RSAPrivateKey;
3738
import java.text.MessageFormat;
3839
import java.util.Optional;
@@ -54,7 +55,6 @@
5455
import org.kse.KSE;
5556
import org.kse.crypto.CryptoException;
5657
import org.kse.crypto.KeyInfo;
57-
import org.kse.crypto.ecc.EccUtil;
5858
import org.kse.crypto.keypair.KeyPairUtil;
5959
import org.kse.crypto.privatekey.PrivateKeyFormat;
6060
import org.kse.gui.CursorUtil;
@@ -311,7 +311,7 @@ private void populateDialog() throws CryptoException {
311311
jtaEncoded.setCaretPosition(0);
312312

313313
jbFields.setEnabled((privateKey instanceof RSAPrivateKey) || (privateKey instanceof DSAPrivateKey)
314-
|| (privateKey instanceof ECPrivateKey) || (EccUtil.isEdPrivateKey(privateKey))
314+
|| (privateKey instanceof ECPrivateKey) || (privateKey instanceof EdECPrivateKey)
315315
|| (privateKey instanceof MLDSAPrivateKey) || (privateKey instanceof SLHDSAPrivateKey));
316316
}
317317

kse/src/test/java/org/kse/crypto/ecc/EccUtilTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222

2323
import static org.assertj.core.api.Assertions.assertThat;
2424
import static org.junit.jupiter.api.Assertions.assertEquals;
25-
import static org.junit.jupiter.api.Assertions.assertFalse;
2625
import static org.junit.jupiter.api.Assertions.assertNotNull;
2726
import static org.junit.jupiter.api.Assertions.assertThrows;
28-
import static org.junit.jupiter.api.Assertions.assertTrue;
2927

3028
import java.math.BigInteger;
3129
import java.security.InvalidParameterException;
@@ -184,11 +182,4 @@ void detectEDDSACurveNotEd() throws Exception {
184182
assertThrows(InvalidParameterException.class, () -> EccUtil.detectEdDSACurve(ecKeyPair.getPublic()));
185183
}
186184

187-
@Test
188-
void isEdPrivateKey() throws Exception {
189-
KeyPair edKeyPair = KeyPairUtil.generateECKeyPair("Ed448", KSE.BC);
190-
assertTrue(EccUtil.isEdPrivateKey(edKeyPair.getPrivate()));
191-
assertFalse(EccUtil.isEdPrivateKey(edKeyPair.getPublic()));
192-
assertFalse(EccUtil.isEdPrivateKey(KeyPairUtil.generateECKeyPair("secp384r1", KSE.BC).getPrivate()));
193-
}
194185
}

0 commit comments

Comments
 (0)