From 0826b1e2282a479f9b2b1e333f57d9cef68c4266 Mon Sep 17 00:00:00 2001 From: zzz Date: Wed, 25 May 2022 09:12:12 -0400 Subject: [PATCH] Crypto: Throw checked exception for unimplemented ElG import, so CertUtil.loadPrivateKey() will continue processing and then fail with the correct exception and error message on bad input. Pull constant KeySpec out of loop. --- core/java/src/net/i2p/crypto/CertUtil.java | 2 +- .../net/i2p/crypto/elgamal/impl/ElGamalPrivateKeyImpl.java | 5 +++-- .../net/i2p/crypto/elgamal/impl/ElGamalPublicKeyImpl.java | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/java/src/net/i2p/crypto/CertUtil.java b/core/java/src/net/i2p/crypto/CertUtil.java index 3c4c5ab90..cdbd8c76b 100644 --- a/core/java/src/net/i2p/crypto/CertUtil.java +++ b/core/java/src/net/i2p/crypto/CertUtil.java @@ -349,9 +349,9 @@ public final class CertUtil { throw new CertificateEncodingException("bad base64 cert"); PrivateKey rv = null; // try all the types + KeySpec ks = new PKCS8EncodedKeySpec(data); for (SigAlgo algo : EnumSet.allOf(SigAlgo.class)) { try { - KeySpec ks = new PKCS8EncodedKeySpec(data); String alg = algo.getName(); KeyFactory kf = KeyFactory.getInstance(alg); rv = kf.generatePrivate(ks); diff --git a/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPrivateKeyImpl.java b/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPrivateKeyImpl.java index 0076c58e0..df1d7d74f 100644 --- a/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPrivateKeyImpl.java +++ b/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPrivateKeyImpl.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.BigInteger; +import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import javax.crypto.interfaces.DHPrivateKey; @@ -65,9 +66,9 @@ public class ElGamalPrivateKeyImpl } public ElGamalPrivateKeyImpl( - PKCS8EncodedKeySpec spec) + PKCS8EncodedKeySpec spec) throws InvalidKeySpecException { - throw new UnsupportedOperationException("todo"); + throw new InvalidKeySpecException("todo"); //this.x = spec.getX(); //this.elSpec = new ElGamalParameterSpec(spec.getP(), spec.getG()); } diff --git a/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPublicKeyImpl.java b/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPublicKeyImpl.java index 8df61ec50..28028e47c 100644 --- a/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPublicKeyImpl.java +++ b/core/java/src/net/i2p/crypto/elgamal/impl/ElGamalPublicKeyImpl.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.BigInteger; +import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; import javax.crypto.interfaces.DHPublicKey; @@ -60,9 +61,9 @@ public class ElGamalPublicKeyImpl } public ElGamalPublicKeyImpl( - X509EncodedKeySpec spec) + X509EncodedKeySpec spec) throws InvalidKeySpecException { - throw new UnsupportedOperationException("todo"); + throw new InvalidKeySpecException("todo"); //this.y = y; //this.elSpec = elSpec; }