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@f3ff91c

File tree

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

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty5/handler/ssl/JdkSslClientContext.java
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.security.KeyStore;
2626
import java.security.PrivateKey;
2727
import java.security.Provider;
28+
import java.security.SecureRandom;
2829
import java.security.cert.X509Certificate;
2930

3031
/**
@@ -44,7 +45,7 @@ final class JdkSslClientContext extends JdkSslContext {
4445
throws Exception {
4546
super(newSSLContext(provider, toX509CertificatesInternal(trustCertCollectionFile),
4647
trustManagerFactory, null, null,
47-
null, null, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), true,
48+
null, null, sessionCacheSize, sessionTimeout, null, KeyStore.getDefaultType()), true,
4849
ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
4950
}
5051

@@ -61,10 +62,11 @@ final class JdkSslClientContext extends JdkSslContext {
6162
String[] protocols,
6263
long sessionCacheSize,
6364
long sessionTimeout,
65+
SecureRandom secureRandom,
6466
String keyStore)
6567
throws Exception {
6668
super(newSSLContext(sslContextProvider, trustCertCollection, trustManagerFactory,
67-
keyCertChain, key, keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, keyStore),
69+
keyCertChain, key, keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, secureRandom, keyStore),
6870
true, ciphers, cipherFilter, toNegotiator(apn, false), ClientAuth.NONE, protocols, false);
6971
}
7072

@@ -73,7 +75,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider,
7375
TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain,
7476
PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
7577
long sessionCacheSize, long sessionTimeout,
76-
String keyStore) throws SSLException {
78+
SecureRandom secureRandom, String keyStore) throws SSLException {
7779
try {
7880
if (trustCertCollection != null) {
7981
trustManagerFactory = buildTrustManagerFactory(trustCertCollection, trustManagerFactory, keyStore);
@@ -86,7 +88,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider,
8688
: SSLContext.getInstance(PROTOCOL, sslContextProvider);
8789
ctx.init(keyManagerFactory == null ? null : keyManagerFactory.getKeyManagers(),
8890
trustManagerFactory == null ? null : trustManagerFactory.getTrustManagers(),
89-
null);
91+
secureRandom);
9092

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

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty5/handler/ssl/JdkSslServerContext.java
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.security.NoSuchAlgorithmException;
3535
import java.security.PrivateKey;
3636
import java.security.Provider;
37+
import java.security.SecureRandom;
3738
import java.security.UnrecoverableKeyException;
3839
import java.security.cert.CertificateException;
3940
import java.security.cert.X509Certificate;
@@ -91,8 +92,8 @@ static void checkIfWrappingTrustManagerIsSupported() throws CertificateException
9192
throws Exception {
9293
super(newSSLContext(provider, null, null,
9394
toX509CertificatesInternal(certChainFile), toPrivateKeyInternal(keyFile, keyPassword),
94-
keyPassword, null, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), false,
95-
ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
95+
keyPassword, null, sessionCacheSize, sessionTimeout, null, KeyStore.getDefaultType()),
96+
false, ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
9697
}
9798

9899
JdkSslServerContext(Provider provider,
@@ -110,17 +111,19 @@ static void checkIfWrappingTrustManagerIsSupported() throws CertificateException
110111
ClientAuth clientAuth,
111112
String[] protocols,
112113
boolean startTls,
114+
SecureRandom secureRandom,
113115
String keyStore)
114116
throws Exception {
115117
super(newSSLContext(provider, trustCertCollection, trustManagerFactory, keyCertChain, key,
116-
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, keyStore), false,
118+
keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, secureRandom, keyStore), false,
117119
ciphers, cipherFilter, toNegotiator(apn, true), clientAuth, protocols, startTls);
118120
}
119121

120122
private static SSLContext newSSLContext(Provider sslContextProvider, X509Certificate[] trustCertCollection,
121123
TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain,
122124
PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
123-
long sessionCacheSize, long sessionTimeout, String keyStore)
125+
long sessionCacheSize, long sessionTimeout, SecureRandom secureRandom,
126+
String keyStore)
124127
throws SSLException {
125128
if (key == null && keyManagerFactory == null) {
126129
throw new NullPointerException("key, keyManagerFactory");
@@ -144,7 +147,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider, X509Certifi
144147
SSLContext ctx = sslContextProvider == null ? SSLContext.getInstance(PROTOCOL)
145148
: SSLContext.getInstance(PROTOCOL, sslContextProvider);
146149
ctx.init(keyManagerFactory.getKeyManagers(),
147-
wrapTrustManagerIfNeeded(trustManagerFactory.getTrustManagers()), null);
150+
wrapTrustManagerIfNeeded(trustManagerFactory.getTrustManagers()), secureRandom);
148151

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

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty5/handler/ssl/SslContext.java
+9-6Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.security.NoSuchAlgorithmException;
5757
import java.security.PrivateKey;
5858
import java.security.Provider;
59+
import java.security.SecureRandom;
5960
import java.security.UnrecoverableKeyException;
6061
import java.security.cert.CertificateException;
6162
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) {
@@ -471,7 +473,7 @@ static SslContext newServerContextInternal(
471473
return new JdkSslServerContext(sslContextProvider,
472474
trustCertCollection, trustManagerFactory, keyCertChain, key, keyPassword,
473475
keyManagerFactory, ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout,
474-
clientAuth, protocols, startTls, keyStoreType);
476+
clientAuth, protocols, startTls, secureRandom, keyStoreType);
475477
} catch (SSLException e) {
476478
throw e;
477479
} catch (Exception e) {
@@ -805,7 +807,7 @@ public static SslContext newClientContext(
805807
toX509Certificates(keyCertChainFile), toPrivateKey(keyFile, keyPassword),
806808
keyPassword, keyManagerFactory, ciphers, cipherFilter,
807809
apn, null, sessionCacheSize, sessionTimeout, false,
808-
KeyStore.getDefaultType());
810+
null, KeyStore.getDefaultType());
809811
} catch (Exception e) {
810812
if (e instanceof SSLException) {
811813
throw (SSLException) e;
@@ -820,7 +822,8 @@ static SslContext newClientContextInternal(
820822
X509Certificate[] trustCert, TrustManagerFactory trustManagerFactory,
821823
X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
822824
Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn, String[] protocols,
823-
long sessionCacheSize, long sessionTimeout, boolean enableOcsp, String keyStoreType,
825+
long sessionCacheSize, long sessionTimeout, boolean enableOcsp,
826+
SecureRandom secureRandom, String keyStoreType,
824827
Map.Entry<SslContextOption<?>, Object>... options) throws SSLException {
825828
if (provider == null) {
826829
provider = defaultClientProvider();
@@ -834,7 +837,7 @@ static SslContext newClientContextInternal(
834837
return new JdkSslClientContext(sslContextProvider,
835838
trustCert, trustManagerFactory, keyCertChain, key, keyPassword,
836839
keyManagerFactory, ciphers, cipherFilter, apn, protocols, sessionCacheSize,
837-
sessionTimeout, keyStoreType);
840+
sessionTimeout, secureRandom, keyStoreType);
838841
} catch (SSLException e) {
839842
throw e;
840843
} catch (Exception e) {
Collapse file

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

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

@@ -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/netty5/handler/ssl/SslContextBuilderTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty5/handler/ssl/SslContextBuilderTest.java
+96Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.nio.charset.StandardCharsets;
3131
import java.security.Principal;
3232
import java.security.PrivateKey;
33+
import java.security.SecureRandom;
3334
import java.security.cert.CertificateException;
3435
import java.security.cert.X509Certificate;
3536
import java.util.Collections;
@@ -245,6 +246,16 @@ public void testInvalidCipherOpenSSL() throws Exception {
245246
}
246247
}
247248

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

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

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.