Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Force BC provider use #76

Merged
merged 1 commit into from
Jun 13, 2024
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 @@ -6,6 +6,7 @@
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
import org.bouncycastle.crypto.params.HKDFParameters;
import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;
Expand Down Expand Up @@ -37,6 +38,8 @@ public enum ECAlgorithm {
ECDSA
}

private static final BouncyCastleProvider BOUNCY_CASTLE_PROVIDER = new BouncyCastleProvider();

public enum NanoTDFECCurve {
SECP256R1("secp256r1"),
PRIME256V1("prime256v1"),
Expand Down Expand Up @@ -65,15 +68,14 @@ public ECKeyPair(String curveName, ECAlgorithm algorithm) {
KeyPairGenerator generator;

try {
// Should this just use the algorithm vs use ECDH only for ECDH and ECDSA for everything else.
if (algorithm == ECAlgorithm.ECDH) {
generator = KeyPairGenerator.getInstance("ECDH", "BC");
generator = KeyPairGeneratorSpi.getInstance(ECAlgorithm.ECDH.name(), BOUNCY_CASTLE_PROVIDER);
} else {
generator = KeyPairGenerator.getInstance("ECDSA", "BC");
generator = KeyPairGeneratorSpi.getInstance(ECAlgorithm.ECDSA.name(), BOUNCY_CASTLE_PROVIDER);
}
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
}

ECGenParameterSpec spec = new ECGenParameterSpec(curveName);
Expand Down Expand Up @@ -160,7 +162,7 @@ public static String getPEMPublicKeyFromX509Cert(String pemInX509Format) {
X509CertificateHolder x509CertificateHolder = (X509CertificateHolder) parser.readObject();
parser.close();
SubjectPublicKeyInfo publicKeyInfo = x509CertificateHolder.getSubjectPublicKeyInfo();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(BOUNCY_CASTLE_PROVIDER);
ECPublicKey publicKey = null;
try {
publicKey = (ECPublicKey)converter.getPublicKey(publicKeyInfo);
Expand Down Expand Up @@ -232,7 +234,7 @@ public static ECPublicKey publicKeyFromPem(String pemEncoding) {
SubjectPublicKeyInfo publicKeyInfo = (SubjectPublicKeyInfo) parser.readObject();
parser.close();

JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(BOUNCY_CASTLE_PROVIDER);
return (ECPublicKey) converter.getPublicKey(publicKeyInfo);
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -245,7 +247,7 @@ public static ECPrivateKey privateKeyFromPem(String pemEncoding) {
PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo)parser.readObject();
parser.close();

JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(BOUNCY_CASTLE_PROVIDER);;
return (ECPrivateKey) converter.getPrivateKey(privateKeyInfo);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Loading