|
24 | 24 | package org.jruby.ext.openssl;
|
25 | 25 |
|
26 | 26 | import java.security.NoSuchProviderException;
|
| 27 | +import java.security.SecureRandom; |
27 | 28 | import java.util.Map;
|
28 | 29 |
|
29 | 30 | import org.jruby.CompatVersion;
|
@@ -269,7 +270,41 @@ static boolean javaOpenJDK() {
|
269 | 270 | return javaName("").contains("OpenJDK");
|
270 | 271 | }
|
271 | 272 |
|
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 |
273 | 308 |
|
274 | 309 | static IRubyObject to_der_if_possible(final ThreadContext context, IRubyObject obj) {
|
275 | 310 | if ( ! obj.respondsTo("to_der")) return obj;
|
|
0 commit comments