Skip to content

Commit 06e6800

Browse files
committed
re-use secure random from thread-context on SSL context initialization
1 parent e197afe commit 06e6800

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,7 @@ public IRubyObject setup(final ThreadContext context) {
446446
*/
447447

448448
try {
449-
internalContext = new InternalContext(
450-
cert, key, store, clientCert, extraChainCert, verifyMode, timeout
451-
);
449+
internalContext = createInternalContext(context, cert, key, store, clientCert, extraChainCert, verifyMode, timeout);
452450
}
453451
catch (GeneralSecurityException e) {
454452
throw newSSLError(runtime, e);
@@ -821,6 +819,15 @@ static RubyClass _SSLContext(final Ruby runtime) {
821819
return (RubyClass) _SSL(runtime).getConstantAt("SSLContext");
822820
}
823821

822+
private InternalContext createInternalContext(ThreadContext context,
823+
final X509Cert xCert, final PKey pKey, final Store store,
824+
final List<X509AuxCertificate> clientCert, final List<X509AuxCertificate> extraChainCert,
825+
final int verifyMode, final int timeout) throws NoSuchAlgorithmException, KeyManagementException {
826+
InternalContext internalContext = new InternalContext(xCert, pKey, store, clientCert, extraChainCert, verifyMode, timeout);
827+
internalContext.initSSLContext(context);
828+
return internalContext;
829+
}
830+
824831
/**
825832
* c: SSL_CTX
826833
*/
@@ -870,17 +877,19 @@ private class InternalContext {
870877
serverContext.setSessionCacheSize(sessionCacheSize);
871878
}
872879
}
873-
this.sslContext = initContext(sslContext);
880+
this.sslContext = sslContext;
874881
}
875882

876-
protected javax.net.ssl.SSLContext initContext(javax.net.ssl.SSLContext sslContext) throws KeyManagementException {
883+
protected void initSSLContext(final ThreadContext context) throws KeyManagementException {
877884
final KeyManager[] keyManager = new KeyManager[] { new KeyManagerImpl(this) };
878885
final TrustManager[] trustManager = new TrustManager[] { new TrustManagerImpl(this) };
879-
// SSLContext on Sun JDK :
886+
// SSLContext (internals) on Sun JDK :
880887
// private final java.security.Provider provider; "SunJSSE"
881888
// private final javax.net.ssl.SSLContextSpi; sun.security.ssl.SSLContextImpl
882-
sslContext.init(keyManager, trustManager, null);
883-
return sslContext;
889+
sslContext.init(keyManager, trustManager, OpenSSL.getSecureRandomFrom(context));
890+
// if secureRandom == null JSSE will try :
891+
// - new SecureRandom();
892+
// - SecureRandom.getInstance("PKCS11", cryptoProvider);
884893
}
885894

886895
final Store store;

0 commit comments

Comments
 (0)