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 821e8e0 commit 1aa85d0
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 @@ -427,7 +424,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 @@ -537,11 +534,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 1aa85d0

Please sign in to comment.