Skip to content

Commit 478aee9

Browse files
committed
[refactor] SecureRandom retrieval (from JRuby)
1 parent e9bb63b commit 478aee9

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ static SecureRandom getSecureRandom(final Ruby runtime) {
315315
return getSecureRandom(runtime, false);
316316
}
317317

318-
319318
static SecureRandom getSecureRandom(final Ruby runtime, final boolean nullByDefault) {
320319
if ( tryContextSecureRandom ) {
321320
SecureRandom random = getSecureRandomFrom(runtime.getCurrentContext());
@@ -324,19 +323,21 @@ static SecureRandom getSecureRandom(final Ruby runtime, final boolean nullByDefa
324323
return nullByDefault ? null : new SecureRandom();
325324
}
326325

327-
static SecureRandom getSecureRandomFrom(final ThreadContext context) {
326+
static SecureRandom getSecureRandom(final ThreadContext context) {
328327
if ( tryContextSecureRandom ) {
329-
try {
330-
SecureRandom random = context.secureRandom;
331-
if (random == null) { // public SecureRandom getSecureRandom() on 9K
332-
random = (SecureRandom) context.getClass().getMethod("getSecureRandom").invoke(context);
333-
}
334-
return random;
335-
}
336-
catch (Throwable ex) {
337-
tryContextSecureRandom = false;
338-
debug(context.runtime, "JRuby-OpenSSL failed to retrieve secure random from thread-context", ex);
339-
}
328+
SecureRandom random = getSecureRandomFrom(context);
329+
if ( random != null ) return random;
330+
}
331+
return new SecureRandom();
332+
}
333+
334+
private static SecureRandom getSecureRandomFrom(final ThreadContext context) {
335+
try {
336+
return context.getSecureRandom();
337+
}
338+
catch (Throwable ex) {
339+
tryContextSecureRandom = false;
340+
debug(context.runtime, "JRuby-OpenSSL failed to retrieve secure random from thread-context", ex);
340341
}
341342
return null;
342343
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ private RubyArray matchedCiphersWithCache(final ThreadContext context) {
534534
private RubyArray matchedCiphers(final ThreadContext context) {
535535
final Ruby runtime = context.runtime;
536536
try {
537-
final String[] supported = getSupportedCipherSuites(runtime, protocol);
537+
final String[] supported = getSupportedCipherSuites(context, protocol);
538538
final Collection<CipherStrings.Def> cipherDefs =
539539
CipherStrings.matchingCiphers(this.ciphers, supported, false);
540540

@@ -751,14 +751,14 @@ private void setApplicationProtocols(final SSLEngine engine) {
751751
}
752752
}
753753

754-
private static String[] getSupportedCipherSuites(Ruby runtime, final String protocol)
754+
private static String[] getSupportedCipherSuites(ThreadContext context, final String protocol)
755755
throws GeneralSecurityException {
756-
return dummySSLEngine(runtime, protocol).getSupportedCipherSuites();
756+
return dummySSLEngine(context, protocol).getSupportedCipherSuites();
757757
}
758758

759-
private static SSLEngine dummySSLEngine(Ruby runtime, final String protocol) throws GeneralSecurityException {
759+
private static SSLEngine dummySSLEngine(ThreadContext context, final String protocol) throws GeneralSecurityException {
760760
javax.net.ssl.SSLContext sslContext = SecurityHelper.getSSLContext(protocol);
761-
sslContext.init(null, null, OpenSSL.getSecureRandom(runtime));
761+
sslContext.init(null, null, OpenSSL.getSecureRandom(context));
762762
return sslContext.createSSLEngine();
763763
}
764764

@@ -1017,7 +1017,7 @@ void initSSLContext(final ThreadContext context) throws KeyManagementException {
10171017
// SSLContext (internals) on Sun JDK :
10181018
// private final java.security.Provider provider; "SunJSSE"
10191019
// private final javax.net.ssl.SSLContextSpi; sun.security.ssl.SSLContextImpl
1020-
sslContext.init(keyManager, trustManager, OpenSSL.getSecureRandomFrom(context));
1020+
sslContext.init(keyManager, trustManager, OpenSSL.getSecureRandom(context));
10211021
// if secureRandom == null JSSE will try :
10221022
// - new SecureRandom();
10231023
// - SecureRandom.getInstance("PKCS11", cryptoProvider);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public static SecureRandom getSecureRandom() {
334334
}
335335
}
336336
catch (NoSuchAlgorithmException e) { }
337-
return new SecureRandom(); // likely "SHA1PRNG" from SPI sun.security.provider.SecureRandom
337+
return new SecureRandom();
338338
}
339339

340340
private static SecureRandom getSecureRandom(final String algorithm, final Provider provider)

0 commit comments

Comments
 (0)