Skip to content

Commit 6253685

Browse files
committed
move (thread) shared SecureRandom retrieval to OpenSSL class
1 parent 28e08a8 commit 6253685

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/main/java/org/jruby/ext/openssl/OpenSSL.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.jruby.ext.openssl;
2525

2626
import java.security.NoSuchProviderException;
27+
import java.security.SecureRandom;
2728
import java.util.Map;
2829

2930
import org.jruby.CompatVersion;
@@ -269,7 +270,41 @@ static boolean javaOpenJDK() {
269270
return javaName("").contains("OpenJDK");
270271
}
271272

272-
//
273+
// shared secure-random :
274+
275+
private static boolean tryContextSecureRandom = true;
276+
277+
static SecureRandom getSecureRandom(final Ruby runtime) {
278+
return getSecureRandom(runtime, false);
279+
}
280+
281+
282+
static SecureRandom getSecureRandom(final Ruby runtime, final boolean nullByDefault) {
283+
if ( tryContextSecureRandom ) {
284+
SecureRandom random = getSecureRandomFrom(runtime.getCurrentContext());
285+
if ( random != null ) return random;
286+
}
287+
return nullByDefault ? null : new SecureRandom();
288+
}
289+
290+
static SecureRandom getSecureRandomFrom(final ThreadContext context) {
291+
if ( tryContextSecureRandom ) {
292+
try {
293+
SecureRandom random = context.secureRandom;
294+
if (random == null) { // public SecureRandom getSecureRandom() on 9K
295+
random = (SecureRandom) context.getClass().getMethod("getSecureRandom").invoke(context);
296+
}
297+
return random;
298+
}
299+
catch (Throwable ex) {
300+
tryContextSecureRandom = false;
301+
debug(context.runtime, "JRuby-OpenSSL failed to retrieve secure random from thread-context", ex);
302+
}
303+
}
304+
return null;
305+
}
306+
307+
// internals
273308

274309
static IRubyObject to_der_if_possible(final ThreadContext context, IRubyObject obj) {
275310
if ( ! obj.respondsTo("to_der")) return obj;

src/main/java/org/jruby/ext/openssl/PKey.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -255,24 +255,8 @@ static boolean verify(final String signAlg, final PublicKey publicKey, final Byt
255255
return signature.verify(sign.getUnsafeBytes(), sign.getBegin(), sign.getRealSize());
256256
}
257257

258-
private static boolean tryContextSecureRandom = true;
259-
260258
static SecureRandom getSecureRandom(final Ruby runtime) {
261-
if ( tryContextSecureRandom ) {
262-
final ThreadContext context = runtime.getCurrentContext();
263-
try {
264-
SecureRandom random = context.secureRandom;
265-
if (random == null) { // public SecureRandom getSecureRandom() on 9K
266-
random = (SecureRandom) context.getClass().getMethod("getSecureRandom").invoke(context);
267-
}
268-
return random;
269-
}
270-
catch (Throwable ex) {
271-
tryContextSecureRandom = false;
272-
debug(runtime, "PKey falling back to using new SecureRandom()", ex);
273-
}
274-
}
275-
return new SecureRandom();
259+
return OpenSSL.getSecureRandom(runtime);
276260
}
277261

278262
// shared Helpers for PKeyRSA / PKEyDSA :

0 commit comments

Comments
 (0)