@@ -65,7 +65,7 @@ private static Holder createHolderImpl() {
65
65
if (HOLDER_TYPE .equals ("shared" )) {
66
66
return new SharedHolder ();
67
67
}
68
- if (HOLDER_TYPE .equals ("strong" )) {
68
+ if (HOLDER_TYPE .equals ("strong" )) { // TODO strong (thread-local) makes sense
69
69
return new StrongHolder ();
70
70
}
71
71
if (ThreadLocalHolder .secureRandomField == null ) {
@@ -148,13 +148,28 @@ private static void setSecureRandom(ThreadContext context, java.security.SecureR
148
148
try {
149
149
secureRandomField .set (context , secureRandom );
150
150
}
151
- catch (Exception ex ) { /* IllegalAccessException should not happen */ }
151
+ catch (IllegalAccessException ex ) { Utils . throwException ( ex ); /* should not happen */ }
152
152
}
153
153
}
154
154
155
155
private static final String PREFERRED_PRNG ;
156
156
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 ;
158
173
159
174
Field secureRandom = null ;
160
175
try {
@@ -169,6 +184,7 @@ private static void setSecureRandom(ThreadContext context, java.security.SecureR
169
184
170
185
private static boolean tryPreferredPRNG = true ;
171
186
private static boolean trySHA1PRNG = true ;
187
+ private static boolean tryStrongPRNG = false ; // NOT-YET-IMPLEMENTED
172
188
173
189
// copied from JRuby (not available in all 1.7.x) :
174
190
public java .security .SecureRandom getSecureRandomImpl () {
@@ -178,15 +194,21 @@ public java.security.SecureRandom getSecureRandomImpl() {
178
194
try {
179
195
secureRandom = java .security .SecureRandom .getInstance (PREFERRED_PRNG );
180
196
}
181
- catch (Exception e ) { tryPreferredPRNG = false ; }
197
+ catch (Exception e ) {
198
+ tryPreferredPRNG = false ;
199
+ OpenSSL .debug ("SecureRandom '" + PREFERRED_PRNG +"' failed:" , e );
200
+ }
182
201
}
183
202
184
203
// Try SHA1PRNG
185
204
if (secureRandom == null && trySHA1PRNG ) {
186
205
try {
187
206
secureRandom = java .security .SecureRandom .getInstance ("SHA1PRNG" );
188
207
}
189
- catch (Exception e ) { trySHA1PRNG = false ; }
208
+ catch (Exception e ) {
209
+ trySHA1PRNG = false ;
210
+ OpenSSL .debug ("SecureRandom SHA1PRNG failed:" , e );
211
+ }
190
212
}
191
213
192
214
// Just let JDK do whatever it does
0 commit comments