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.
This commit is contained in:
zzz
2022-05-25 09:12:12 -04:00
parent 5d3c29fb27
commit 0826b1e228
3 changed files with 7 additions and 5 deletions

View File

@@ -349,9 +349,9 @@ public final class CertUtil {
throw new CertificateEncodingException("bad base64 cert"); throw new CertificateEncodingException("bad base64 cert");
PrivateKey rv = null; PrivateKey rv = null;
// try all the types // try all the types
KeySpec ks = new PKCS8EncodedKeySpec(data);
for (SigAlgo algo : EnumSet.allOf(SigAlgo.class)) { for (SigAlgo algo : EnumSet.allOf(SigAlgo.class)) {
try { try {
KeySpec ks = new PKCS8EncodedKeySpec(data);
String alg = algo.getName(); String alg = algo.getName();
KeyFactory kf = KeyFactory.getInstance(alg); KeyFactory kf = KeyFactory.getInstance(alg);
rv = kf.generatePrivate(ks); rv = kf.generatePrivate(ks);

View File

@@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec;
import javax.crypto.interfaces.DHPrivateKey; import javax.crypto.interfaces.DHPrivateKey;
@@ -65,9 +66,9 @@ public class ElGamalPrivateKeyImpl
} }
public ElGamalPrivateKeyImpl( public ElGamalPrivateKeyImpl(
PKCS8EncodedKeySpec spec) PKCS8EncodedKeySpec spec) throws InvalidKeySpecException
{ {
throw new UnsupportedOperationException("todo"); throw new InvalidKeySpecException("todo");
//this.x = spec.getX(); //this.x = spec.getX();
//this.elSpec = new ElGamalParameterSpec(spec.getP(), spec.getG()); //this.elSpec = new ElGamalParameterSpec(spec.getP(), spec.getG());
} }

View File

@@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;
import javax.crypto.interfaces.DHPublicKey; import javax.crypto.interfaces.DHPublicKey;
@@ -60,9 +61,9 @@ public class ElGamalPublicKeyImpl
} }
public ElGamalPublicKeyImpl( public ElGamalPublicKeyImpl(
X509EncodedKeySpec spec) X509EncodedKeySpec spec) throws InvalidKeySpecException
{ {
throw new UnsupportedOperationException("todo"); throw new InvalidKeySpecException("todo");
//this.y = y; //this.y = y;
//this.elSpec = elSpec; //this.elSpec = elSpec;
} }