Skip to content

Commit

Permalink
[refactor] re-use SecureRandom + house-keeping
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Feb 13, 2024
1 parent 831100e commit 33d754f
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/main/java/org/jruby/ext/openssl/PKeyEC.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECGenParameterSpec;
Expand Down Expand Up @@ -430,7 +427,7 @@ public PKeyEC generate_key(final ThreadContext context) {
try {
ECGenParameterSpec genSpec = new ECGenParameterSpec(getCurveName());
KeyPairGenerator gen = SecurityHelper.getKeyPairGenerator("EC"); // "BC"
gen.initialize(genSpec, new SecureRandom());
gen.initialize(genSpec, OpenSSL.getSecureRandom(context));
KeyPair pair = gen.generateKeyPair();
this.publicKey = (ECPublicKey) pair.getPublic();
this.privateKey = pair.getPrivate();
Expand Down Expand Up @@ -540,11 +537,8 @@ public IRubyObject dh_compute_key(final ThreadContext context, final IRubyObject
final byte[] secret = agreement.generateSecret();
return StringHelper.newString(context.runtime, secret);
}
catch (NoSuchAlgorithmException ex) {
throw newECError(context.runtime, ex.toString());
}
catch (InvalidKeyException ex) {
throw newECError(context.runtime, ex.toString());
throw newECError(context.runtime, "invalid key: " + ex.getMessage());
}
catch (GeneralSecurityException ex) {
throw newECError(context.runtime, ex.toString());
Expand Down

0 comments on commit 33d754f

Please sign in to comment.