GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for specifying SecureRandom in SSLContext initialization … · netty/netty@9841b19

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • JdkSslClientContext.java
      • JdkSslServerContext.java
      • SslContext.java
      • SslContextBuilder.java
    • test/java/io/netty/handler/ssl
      • SslContextBuilderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/JdkSslClientContext.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/JdkSslClientContext.java
+8-6Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.net.ssl.TrustManagerFactory;
2828
import java.io.File;
2929
import java.security.PrivateKey;
30+
import java.security.SecureRandom;
3031
import java.security.cert.X509Certificate;
3132

3233
/**
@@ -175,7 +176,7 @@ public JdkSslClientContext(
175176
long sessionCacheSize, long sessionTimeout) throws SSLException {
176177
super(newSSLContext(provider, toX509CertificatesInternal(trustCertCollectionFile),
177178
trustManagerFactory, null, null,
178-
null, null, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), true,
179+
null, null, sessionCacheSize, sessionTimeout, null, KeyStore.getDefaultType()), true,
179180
ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
180181
}
181182

@@ -258,7 +259,8 @@ public JdkSslClientContext(File trustCertCollectionFile, TrustManagerFactory tru
258259
super(newSSLContext(null, toX509CertificatesInternal(
259260
trustCertCollectionFile), trustManagerFactory,
260261
toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword),
261-
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), true,
262+
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout,
263+
null, KeyStore.getDefaultType()), true,
262264
ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
263265
}
264266

@@ -267,11 +269,11 @@ public JdkSslClientContext(File trustCertCollectionFile, TrustManagerFactory tru
267269
X509Certificate[] keyCertChain, PrivateKey key, String keyPassword,
268270
KeyManagerFactory keyManagerFactory, Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
269271
ApplicationProtocolConfig apn, String[] protocols, long sessionCacheSize, long sessionTimeout,
270-
String keyStoreType)
272+
SecureRandom secureRandom, String keyStoreType)
271273
throws SSLException {
272274
super(newSSLContext(sslContextProvider, trustCertCollection, trustManagerFactory,
273275
keyCertChain, key, keyPassword, keyManagerFactory, sessionCacheSize,
274-
sessionTimeout, keyStoreType),
276+
sessionTimeout, secureRandom, keyStoreType),
275277
true, ciphers, cipherFilter, toNegotiator(apn, false), ClientAuth.NONE, protocols, false);
276278
}
277279

@@ -280,7 +282,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider,
280282
TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain,
281283
PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
282284
long sessionCacheSize, long sessionTimeout,
283-
String keyStore) throws SSLException {
285+
SecureRandom secureRandom, String keyStore) throws SSLException {
284286
try {
285287
if (trustCertCollection != null) {
286288
trustManagerFactory = buildTrustManagerFactory(trustCertCollection, trustManagerFactory, keyStore);
@@ -293,7 +295,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider,
293295
: SSLContext.getInstance(PROTOCOL, sslContextProvider);
294296
ctx.init(keyManagerFactory == null ? null : keyManagerFactory.getKeyManagers(),
295297
trustManagerFactory == null ? null : trustManagerFactory.getTrustManagers(),
296-
null);
298+
secureRandom);
297299

298300
SSLSessionContext sessCtx = ctx.getClientSessionContext();
299301
if (sessionCacheSize > 0) {
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/JdkSslServerContext.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/JdkSslServerContext.java
+12-9Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.security.NoSuchAlgorithmException;
2929
import java.security.PrivateKey;
3030
import java.security.Provider;
31+
import java.security.SecureRandom;
3132
import java.security.UnrecoverableKeyException;
3233
import java.security.cert.CertificateException;
3334
import java.security.cert.X509Certificate;
@@ -201,7 +202,7 @@ public JdkSslServerContext(
201202
long sessionCacheSize, long sessionTimeout, String keyStore) throws SSLException {
202203
super(newSSLContext(provider, null, null,
203204
toX509CertificatesInternal(certChainFile), toPrivateKeyInternal(keyFile, keyPassword),
204-
keyPassword, null, sessionCacheSize, sessionTimeout, keyStore), false,
205+
keyPassword, null, sessionCacheSize, sessionTimeout, null, keyStore), false,
205206
ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
206207
}
207208

@@ -241,7 +242,7 @@ public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory tru
241242
long sessionCacheSize, long sessionTimeout) throws SSLException {
242243
super(newSSLContext(null, toX509CertificatesInternal(trustCertCollectionFile), trustManagerFactory,
243244
toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword),
244-
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, null), false,
245+
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, null, null), false,
245246
ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
246247
}
247248

@@ -282,7 +283,8 @@ public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory tru
282283
long sessionCacheSize, long sessionTimeout) throws SSLException {
283284
super(newSSLContext(null, toX509CertificatesInternal(trustCertCollectionFile), trustManagerFactory,
284285
toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword),
285-
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), false,
286+
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout,
287+
null, KeyStore.getDefaultType()), false,
286288
ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
287289
}
288290

@@ -292,16 +294,17 @@ public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory tru
292294
KeyManagerFactory keyManagerFactory, Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
293295
ApplicationProtocolConfig apn, long sessionCacheSize, long sessionTimeout,
294296
ClientAuth clientAuth, String[] protocols, boolean startTls,
295-
String keyStore) throws SSLException {
297+
SecureRandom secureRandom, String keyStore) throws SSLException {
296298
super(newSSLContext(provider, trustCertCollection, trustManagerFactory, keyCertChain, key,
297-
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, keyStore), false,
299+
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, secureRandom, keyStore), false,
298300
ciphers, cipherFilter, toNegotiator(apn, true), clientAuth, protocols, startTls);
299301
}
300302

301303
private static SSLContext newSSLContext(Provider sslContextProvider, X509Certificate[] trustCertCollection,
302-
TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain,
303-
PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
304-
long sessionCacheSize, long sessionTimeout, String keyStore)
304+
TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain,
305+
PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
306+
long sessionCacheSize, long sessionTimeout,
307+
SecureRandom secureRandom, String keyStore)
305308
throws SSLException {
306309
if (key == null && keyManagerFactory == null) {
307310
throw new NullPointerException("key, keyManagerFactory");
@@ -327,7 +330,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider, X509Certifi
327330
: SSLContext.getInstance(PROTOCOL, sslContextProvider);
328331
ctx.init(keyManagerFactory.getKeyManagers(),
329332
wrapTrustManagerIfNeeded(trustManagerFactory.getTrustManagers()),
330-
null);
333+
secureRandom);
331334

332335
SSLSessionContext sessCtx = ctx.getServerSessionContext();
333336
if (sessionCacheSize > 0) {
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/SslContext.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/SslContext.java
+9-6Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.security.NoSuchAlgorithmException;
4343
import java.security.PrivateKey;
4444
import java.security.Provider;
45+
import java.security.SecureRandom;
4546
import java.security.UnrecoverableKeyException;
4647
import java.security.cert.CertificateException;
4748
import java.security.cert.CertificateFactory;
@@ -439,7 +440,7 @@ trustManagerFactory, toX509Certificates(keyCertChainFile),
439440
toPrivateKey(keyFile, keyPassword),
440441
keyPassword, keyManagerFactory, ciphers, cipherFilter, apn,
441442
sessionCacheSize, sessionTimeout, ClientAuth.NONE, null,
442-
false, false, keyStore);
443+
false, false, null, keyStore);
443444
} catch (Exception e) {
444445
if (e instanceof SSLException) {
445446
throw (SSLException) e;
@@ -455,7 +456,8 @@ static SslContext newServerContextInternal(
455456
X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
456457
Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
457458
long sessionCacheSize, long sessionTimeout, ClientAuth clientAuth, String[] protocols, boolean startTls,
458-
boolean enableOcsp, String keyStoreType, Map.Entry<SslContextOption<?>, Object>... ctxOptions)
459+
boolean enableOcsp, SecureRandom secureRandom, String keyStoreType,
460+
Map.Entry<SslContextOption<?>, Object>... ctxOptions)
459461
throws SSLException {
460462

461463
if (provider == null) {
@@ -470,7 +472,7 @@ static SslContext newServerContextInternal(
470472
return new JdkSslServerContext(sslContextProvider,
471473
trustCertCollection, trustManagerFactory, keyCertChain, key, keyPassword,
472474
keyManagerFactory, ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout,
473-
clientAuth, protocols, startTls, keyStoreType);
475+
clientAuth, protocols, startTls, secureRandom, keyStoreType);
474476
case OPENSSL:
475477
verifyNullSslContextProvider(provider, sslContextProvider);
476478
return new OpenSslServerContext(
@@ -799,7 +801,7 @@ public static SslContext newClientContext(
799801
toX509Certificates(keyCertChainFile), toPrivateKey(keyFile, keyPassword),
800802
keyPassword, keyManagerFactory, ciphers, cipherFilter,
801803
apn, null, sessionCacheSize, sessionTimeout, false,
802-
KeyStore.getDefaultType());
804+
null, KeyStore.getDefaultType());
803805
} catch (Exception e) {
804806
if (e instanceof SSLException) {
805807
throw (SSLException) e;
@@ -814,7 +816,8 @@ static SslContext newClientContextInternal(
814816
X509Certificate[] trustCert, TrustManagerFactory trustManagerFactory,
815817
X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
816818
Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn, String[] protocols,
817-
long sessionCacheSize, long sessionTimeout, boolean enableOcsp, String keyStoreType,
819+
long sessionCacheSize, long sessionTimeout, boolean enableOcsp,
820+
SecureRandom secureRandom, String keyStoreType,
818821
Map.Entry<SslContextOption<?>, Object>... options) throws SSLException {
819822
if (provider == null) {
820823
provider = defaultClientProvider();
@@ -827,7 +830,7 @@ static SslContext newClientContextInternal(
827830
return new JdkSslClientContext(sslContextProvider,
828831
trustCert, trustManagerFactory, keyCertChain, key, keyPassword,
829832
keyManagerFactory, ciphers, cipherFilter, apn, protocols, sessionCacheSize,
830-
sessionTimeout, keyStoreType);
833+
sessionTimeout, secureRandom, keyStoreType);
831834
case OPENSSL:
832835
verifyNullSslContextProvider(provider, sslContextProvider);
833836
OpenSsl.ensureAvailability();
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/SslContextBuilder.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/SslContextBuilder.java
+20-2Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.security.KeyStore;
3232
import java.security.PrivateKey;
3333
import java.security.Provider;
34+
import java.security.SecureRandom;
3435
import java.security.cert.X509Certificate;
3536
import java.util.ArrayList;
3637
import java.util.HashMap;
@@ -206,6 +207,7 @@ public static SslContextBuilder forServer(KeyManager keyManager) {
206207
private String[] protocols;
207208
private boolean startTls;
208209
private boolean enableOcsp;
210+
private SecureRandom secureRandom;
209211
private String keyStoreType = KeyStore.getDefaultType();
210212
private final Map<SslContextOption<?>, Object> options = new HashMap<SslContextOption<?>, Object>();
211213

@@ -600,6 +602,21 @@ public SslContextBuilder enableOcsp(boolean enableOcsp) {
600602
return this;
601603
}
602604

605+
/**
606+
* Specify a non-default source of randomness for the {@link JdkSslContext}
607+
* <p>
608+
* In general, the best practice is to leave this unspecified, or to assign a new random source using the
609+
* default {@code new SecureRandom()} constructor.
610+
* Only assign this something when you have a good reason to.
611+
*
612+
* @param secureRandom the source of randomness for {@link JdkSslContext}
613+
*
614+
*/
615+
public SslContextBuilder secureRandom(SecureRandom secureRandom) {
616+
this.secureRandom = secureRandom;
617+
return this;
618+
}
619+
603620
/**
604621
* Create new {@code SslContext} instance with configured settings.
605622
* <p>If {@link #sslProvider(SslProvider)} is set to {@link SslProvider#OPENSSL_REFCNT} then the caller is
@@ -610,11 +627,12 @@ public SslContext build() throws SSLException {
610627
return SslContext.newServerContextInternal(provider, sslContextProvider, trustCertCollection,
611628
trustManagerFactory, keyCertChain, key, keyPassword, keyManagerFactory,
612629
ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout, clientAuth, protocols, startTls,
613-
enableOcsp, keyStoreType, toArray(options.entrySet(), EMPTY_ENTRIES));
630+
enableOcsp, secureRandom, keyStoreType, toArray(options.entrySet(), EMPTY_ENTRIES));
614631
} else {
615632
return SslContext.newClientContextInternal(provider, sslContextProvider, trustCertCollection,
616633
trustManagerFactory, keyCertChain, key, keyPassword, keyManagerFactory,
617-
ciphers, cipherFilter, apn, protocols, sessionCacheSize, sessionTimeout, enableOcsp, keyStoreType,
634+
ciphers, cipherFilter, apn, protocols, sessionCacheSize,
635+
sessionTimeout, enableOcsp, secureRandom, keyStoreType,
618636
toArray(options.entrySet(), EMPTY_ENTRIES));
619637
}
620638
}
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/SslContextBuilderTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/SslContextBuilderTest.java
+96Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.Socket;
3232
import java.security.Principal;
3333
import java.security.PrivateKey;
34+
import java.security.SecureRandom;
3435
import java.security.cert.CertificateException;
3536
import java.security.cert.X509Certificate;
3637
import java.util.Collections;
@@ -244,6 +245,16 @@ public void testInvalidCipherOpenSSL() throws Exception {
244245
}
245246
}
246247

248+
@Test
249+
public void testServerContextWithSecureRandom() throws Exception {
250+
testServerContextWithSecureRandom(SslProvider.JDK, new SpySecureRandom());
251+
}
252+
253+
@Test
254+
public void testClientContextWithSecureRandom() throws Exception {
255+
testClientContextWithSecureRandom(SslProvider.JDK, new SpySecureRandom());
256+
}
257+
247258
private static void testKeyStoreType(SslProvider provider) throws Exception {
248259
SelfSignedCertificate cert = new SelfSignedCertificate();
249260
SslContextBuilder builder = SslContextBuilder.forServer(cert.certificate(), cert.privateKey())
@@ -326,6 +337,41 @@ private static void testServerContext(SslProvider provider) throws Exception {
326337
engine.closeOutbound();
327338
}
328339

340+
private static void testServerContextWithSecureRandom(SslProvider provider,
341+
SpySecureRandom secureRandom) throws Exception {
342+
SelfSignedCertificate cert = new SelfSignedCertificate();
343+
SslContextBuilder builder = SslContextBuilder.forServer(cert.key(), cert.cert())
344+
.sslProvider(provider)
345+
.secureRandom(secureRandom)
346+
.trustManager(cert.cert())
347+
.clientAuth(ClientAuth.REQUIRE);
348+
SslContext context = builder.build();
349+
SSLEngine engine = context.newEngine(UnpooledByteBufAllocator.DEFAULT);
350+
assertFalse(engine.getWantClientAuth());
351+
assertTrue(engine.getNeedClientAuth());
352+
assertTrue(secureRandom.getCount() > 0);
353+
engine.closeInbound();
354+
engine.closeOutbound();
355+
}
356+
357+
private static void testClientContextWithSecureRandom(SslProvider provider,
358+
SpySecureRandom secureRandom) throws Exception {
359+
SelfSignedCertificate cert = new SelfSignedCertificate();
360+
SslContextBuilder builder = SslContextBuilder.forClient()
361+
.sslProvider(provider)
362+
.secureRandom(secureRandom)
363+
.keyManager(cert.key(), cert.cert())
364+
.trustManager(cert.cert())
365+
.clientAuth(ClientAuth.OPTIONAL);
366+
SslContext context = builder.build();
367+
SSLEngine engine = context.newEngine(UnpooledByteBufAllocator.DEFAULT);
368+
assertFalse(engine.getWantClientAuth());
369+
assertFalse(engine.getNeedClientAuth());
370+
assertTrue(secureRandom.getCount() > 0);
371+
engine.closeInbound();
372+
engine.closeOutbound();
373+
}
374+
329375
private static void testContextFromManagers(SslProvider provider) throws Exception {
330376
final SelfSignedCertificate cert = new SelfSignedCertificate();
331377
KeyManager customKeyManager = new X509ExtendedKeyManager() {
@@ -425,4 +471,54 @@ public X509Certificate[] getAcceptedIssuers() {
425471
server_engine.closeInbound();
426472
server_engine.closeOutbound();
427473
}
474+
475+
private static final class SpySecureRandom extends SecureRandom {
476+
private int count;
477+
478+
@Override
479+
public int nextInt() {
480+
count++;
481+
return super.nextInt();
482+
}
483+
484+
@Override
485+
public int nextInt(int bound) {
486+
count++;
487+
return super.nextInt(bound);
488+
}
489+
490+
@Override
491+
public long nextLong() {
492+
count++;
493+
return super.nextLong();
494+
}
495+
496+
@Override
497+
public boolean nextBoolean() {
498+
count++;
499+
return super.nextBoolean();
500+
}
501+
502+
@Override
503+
public float nextFloat() {
504+
count++;
505+
return super.nextFloat();
506+
}
507+
508+
@Override
509+
public double nextDouble() {
510+
count++;
511+
return super.nextDouble();
512+
}
513+
514+
@Override
515+
public double nextGaussian() {
516+
count++;
517+
return super.nextGaussian();
518+
}
519+
520+
public int getCount() {
521+
return count;
522+
}
523+
}
428524
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.