Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Deal with non-supported ECKey subclasses #14

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.security.ProviderException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.ECKey;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec;
Expand Down Expand Up @@ -101,36 +102,45 @@ protected void engineInit(Key key, SecureRandom random)
("Key must be an instance of PrivateKey");
}
/* attempt to translate the key if it is not an ECKey */
this.privateKey = (ECPrivateKeyImpl) ECKeyFactory.toECKey(key);
this.publicKey = null;
ECKey ecKey = ECKeyFactory.toECKey(key);
if (ecKey instanceof ECPrivateKeyImpl keyImpl) {
this.privateKey = keyImpl;
this.publicKey = null;

ECUtil.checkPrivateKey(this.privateKey);
ECUtil.checkPrivateKey(this.privateKey);

ECParameterSpec params = this.privateKey.getParams();
if (params instanceof NamedCurve) {
this.curve = ((NamedCurve) params).getNameAndAliases()[0];
} else {
/* use the OID */
try {
AlgorithmParameters algParams = AlgorithmParameters.getInstance("EC");
algParams.init(this.privateKey.getParams());
this.curve = algParams.getParameterSpec(ECGenParameterSpec.class).getName();
} catch (InvalidParameterSpecException | NoSuchAlgorithmException e) {
/* should not happen */
throw new InternalError(e);
ECParameterSpec params = this.privateKey.getParams();
if (params instanceof NamedCurve nc) {
this.curve = nc.getNameAndAliases()[0];
} else {
/* use the OID */
try {
AlgorithmParameters algParams = AlgorithmParameters.getInstance("EC");
algParams.init(params);
this.curve = algParams.getParameterSpec(ECGenParameterSpec.class).getName();
} catch (InvalidParameterSpecException | NoSuchAlgorithmException e) {
/* should not happen */
throw new InternalError(e);
}
}
}

if ((!nativeGF2m) && this.privateKey.isECFieldF2m()) {
/* only print the first time a curve is used */
if ((curveSupported.putIfAbsent("EC2m", Boolean.FALSE) == null) && (nativeCryptTrace != null)) {
System.err.println("EC2m is not supported by OpenSSL, using Java crypto implementation.");
if ((!nativeGF2m) && this.privateKey.isECFieldF2m()) {
/* only print the first time a curve is used */
if ((curveSupported.putIfAbsent("EC2m", Boolean.FALSE) == null) && (nativeCryptTrace != null)) {
System.err.println("EC2m is not supported by OpenSSL, using Java crypto implementation.");
}
this.initializeJavaImplementation(key, random);
} else if (Boolean.FALSE.equals(curveSupported.get(this.curve))) {
this.initializeJavaImplementation(key, random);
} else {
this.javaImplementation = null;
}
this.initializeJavaImplementation(key, random);
} else if (Boolean.FALSE.equals(curveSupported.get(this.curve))) {
this.initializeJavaImplementation(key, random);
} else {
this.javaImplementation = null;
if ((curveSupported.putIfAbsent("ECKeyImpl", Boolean.FALSE) == null) && (nativeCryptTrace != null)) {
System.err.println("Only ECPrivateKeyImpl and ECPublicKeyImpl are supported by the native implementation,"
+ " using Java crypto implementation.");
}
this.initializeJavaImplementation(key, random);
}
}

Expand Down Expand Up @@ -165,12 +175,22 @@ protected Key engineDoPhase(Key key, boolean lastPhase)
("Key must be an instance of PublicKey");
}
/* attempt to translate the key if it is not an ECKey */
this.publicKey = (ECPublicKeyImpl) ECKeyFactory.toECKey(key);
ECKey ecKey = ECKeyFactory.toECKey(key);
if (ecKey instanceof ECPublicKeyImpl keyImpl) {
this.publicKey = keyImpl;

int keyLenBits = this.publicKey.getParams().getCurve().getField().getFieldSize();
this.secretLen = (keyLenBits + 7) >> 3;
int keyLenBits = this.publicKey.getParams().getCurve().getField().getFieldSize();
this.secretLen = (keyLenBits + 7) >> 3;

return null;
return null;
} else {
if ((curveSupported.putIfAbsent("ECKeyImpl", Boolean.FALSE) == null) && (nativeCryptTrace != null)) {
System.err.println("Only ECPrivateKeyImpl and ECPublicKeyImpl are supported by the native implementation,"
+ " using Java crypto implementation.");
}
this.initializeJavaImplementation(this.privateKey, null);
return this.javaImplementation.engineDoPhase(key, lastPhase);
}
}

@Override
Expand Down