Skip to content

Commit 06431cd

Browse files
committed
try tuning JRuby's SecureRandom preferred instance logic a bit
- default to Windows-PRNG on Windows - empty setting will simply use `new SecureRandom()` - include some debug logging around fall-backs
1 parent 2e6f6a7 commit 06431cd

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private static Holder createHolderImpl() {
6565
if (HOLDER_TYPE.equals("shared")) {
6666
return new SharedHolder();
6767
}
68-
if (HOLDER_TYPE.equals("strong")) {
68+
if (HOLDER_TYPE.equals("strong")) { // TODO strong (thread-local) makes sense
6969
return new StrongHolder();
7070
}
7171
if (ThreadLocalHolder.secureRandomField == null) {
@@ -148,13 +148,28 @@ private static void setSecureRandom(ThreadContext context, java.security.SecureR
148148
try {
149149
secureRandomField.set(context, secureRandom);
150150
}
151-
catch (Exception ex) { /* IllegalAccessException should not happen */ }
151+
catch (IllegalAccessException ex) { Utils.throwException(ex); /* should not happen */ }
152152
}
153153
}
154154

155155
private static final String PREFERRED_PRNG;
156156
static {
157-
PREFERRED_PRNG = SafePropertyAccessor.getProperty("jruby.preferred.prng", "NativePRNGNonBlocking");
157+
String prng = SafePropertyAccessor.getProperty("jruby.preferred.prng", null);
158+
159+
if (prng == null) { // make sure the default experience is non-blocking for users
160+
prng = "NativePRNGNonBlocking";
161+
if (SafePropertyAccessor.getProperty("os.name") != null) {
162+
if (jnr.posix.util.Platform.IS_WINDOWS) { // System.getProperty("os.name") won't fail
163+
prng = "Windows-PRNG";
164+
}
165+
}
166+
}
167+
// setting it to "" (empty) or "default" should just use new SecureRandom() :
168+
if (prng.isEmpty() || prng.equalsIgnoreCase("default")) {
169+
prng = null; tryPreferredPRNG = false; trySHA1PRNG = false;
170+
}
171+
172+
PREFERRED_PRNG = prng;
158173

159174
Field secureRandom = null;
160175
try {
@@ -169,6 +184,7 @@ private static void setSecureRandom(ThreadContext context, java.security.SecureR
169184

170185
private static boolean tryPreferredPRNG = true;
171186
private static boolean trySHA1PRNG = true;
187+
private static boolean tryStrongPRNG = false; // NOT-YET-IMPLEMENTED
172188

173189
// copied from JRuby (not available in all 1.7.x) :
174190
public java.security.SecureRandom getSecureRandomImpl() {
@@ -178,15 +194,21 @@ public java.security.SecureRandom getSecureRandomImpl() {
178194
try {
179195
secureRandom = java.security.SecureRandom.getInstance(PREFERRED_PRNG);
180196
}
181-
catch (Exception e) { tryPreferredPRNG = false; }
197+
catch (Exception e) {
198+
tryPreferredPRNG = false;
199+
OpenSSL.debug("SecureRandom '"+ PREFERRED_PRNG +"' failed:", e);
200+
}
182201
}
183202

184203
// Try SHA1PRNG
185204
if (secureRandom == null && trySHA1PRNG) {
186205
try {
187206
secureRandom = java.security.SecureRandom.getInstance("SHA1PRNG");
188207
}
189-
catch (Exception e) { trySHA1PRNG = false; }
208+
catch (Exception e) {
209+
trySHA1PRNG = false;
210+
OpenSSL.debug("SecureRandom SHA1PRNG failed:", e);
211+
}
190212
}
191213

192214
// Just let JDK do whatever it does

0 commit comments

Comments
 (0)