GeezerVison is showing github.com — Add support for specifying SecureRandom in SSLContext initialization … · netty/netty@f3ff91c
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty5/handler/ssl test/java/io/netty5/handler/ssl
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
| 25 | 25 | import java.security.KeyStore; | 26 | 26 | import java.security.PrivateKey; | 27 | 27 | import java.security.Provider; | | 28 | +import java.security.SecureRandom; | 28 | 29 | import java.security.cert.X509Certificate; | 29 | 30 |
| 30 | 31 | /** | @@ -44,7 +45,7 @@ final class JdkSslClientContext extends JdkSslContext { | 44 | 45 | throws Exception { | 45 | 46 | super(newSSLContext(provider, toX509CertificatesInternal(trustCertCollectionFile), | 46 | 47 | trustManagerFactory, null, null, | 47 | | - null, null, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), true, | | 48 | + null, null, sessionCacheSize, sessionTimeout, null, KeyStore.getDefaultType()), true, | 48 | 49 | ciphers, cipherFilter, apn, ClientAuth.NONE, null, false); | 49 | 50 | } | 50 | 51 |
| @@ -61,10 +62,11 @@ final class JdkSslClientContext extends JdkSslContext { | 61 | 62 | String[] protocols, | 62 | 63 | long sessionCacheSize, | 63 | 64 | long sessionTimeout, | | 65 | + SecureRandom secureRandom, | 64 | 66 | String keyStore) | 65 | 67 | throws Exception { | 66 | 68 | super(newSSLContext(sslContextProvider, trustCertCollection, trustManagerFactory, | 67 | | - keyCertChain, key, keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, keyStore), | | 69 | + keyCertChain, key, keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, secureRandom, keyStore), | 68 | 70 | true, ciphers, cipherFilter, toNegotiator(apn, false), ClientAuth.NONE, protocols, false); | 69 | 71 | } | 70 | 72 |
| @@ -73,7 +75,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider, | 73 | 75 | TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain, | 74 | 76 | PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory, | 75 | 77 | long sessionCacheSize, long sessionTimeout, | 76 | | - String keyStore) throws SSLException { | | 78 | + SecureRandom secureRandom, String keyStore) throws SSLException { | 77 | 79 | try { | 78 | 80 | if (trustCertCollection != null) { | 79 | 81 | trustManagerFactory = buildTrustManagerFactory(trustCertCollection, trustManagerFactory, keyStore); | @@ -86,7 +88,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider, | 86 | 88 | : SSLContext.getInstance(PROTOCOL, sslContextProvider); | 87 | 89 | ctx.init(keyManagerFactory == null ? null : keyManagerFactory.getKeyManagers(), | 88 | 90 | trustManagerFactory == null ? null : trustManagerFactory.getTrustManagers(), | 89 | | - null); | | 91 | + secureRandom); | 90 | 92 |
| 91 | 93 | SSLSessionContext sessCtx = ctx.getClientSessionContext(); | 92 | 94 | if (sessionCacheSize > 0) { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 34 | 34 | import java.security.NoSuchAlgorithmException; | 35 | 35 | import java.security.PrivateKey; | 36 | 36 | import java.security.Provider; | | 37 | +import java.security.SecureRandom; | 37 | 38 | import java.security.UnrecoverableKeyException; | 38 | 39 | import java.security.cert.CertificateException; | 39 | 40 | import java.security.cert.X509Certificate; | @@ -91,8 +92,8 @@ static void checkIfWrappingTrustManagerIsSupported() throws CertificateException | 91 | 92 | throws Exception { | 92 | 93 | super(newSSLContext(provider, null, null, | 93 | 94 | 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); | 96 | 97 | } | 97 | 98 |
| 98 | 99 | JdkSslServerContext(Provider provider, | @@ -110,17 +111,19 @@ static void checkIfWrappingTrustManagerIsSupported() throws CertificateException | 110 | 111 | ClientAuth clientAuth, | 111 | 112 | String[] protocols, | 112 | 113 | boolean startTls, | | 114 | + SecureRandom secureRandom, | 113 | 115 | String keyStore) | 114 | 116 | throws Exception { | 115 | 117 | super(newSSLContext(provider, trustCertCollection, trustManagerFactory, keyCertChain, key, | 116 | | - keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, keyStore), false, | | 118 | + keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, secureRandom, keyStore), false, | 117 | 119 | ciphers, cipherFilter, toNegotiator(apn, true), clientAuth, protocols, startTls); | 118 | 120 | } | 119 | 121 |
| 120 | 122 | private static SSLContext newSSLContext(Provider sslContextProvider, X509Certificate[] trustCertCollection, | 121 | 123 | TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain, | 122 | 124 | PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory, | 123 | | - long sessionCacheSize, long sessionTimeout, String keyStore) | | 125 | + long sessionCacheSize, long sessionTimeout, SecureRandom secureRandom, | | 126 | + String keyStore) | 124 | 127 | throws SSLException { | 125 | 128 | if (key == null && keyManagerFactory == null) { | 126 | 129 | throw new NullPointerException("key, keyManagerFactory"); | @@ -144,7 +147,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider, X509Certifi | 144 | 147 | SSLContext ctx = sslContextProvider == null ? SSLContext.getInstance(PROTOCOL) | 145 | 148 | : SSLContext.getInstance(PROTOCOL, sslContextProvider); | 146 | 149 | ctx.init(keyManagerFactory.getKeyManagers(), | 147 | | - wrapTrustManagerIfNeeded(trustManagerFactory.getTrustManagers()), null); | | 150 | + wrapTrustManagerIfNeeded(trustManagerFactory.getTrustManagers()), secureRandom); | 148 | 151 |
| 149 | 152 | SSLSessionContext sessCtx = ctx.getServerSessionContext(); | 150 | 153 | if (sessionCacheSize > 0) { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 56 | 56 | import java.security.NoSuchAlgorithmException; | 57 | 57 | import java.security.PrivateKey; | 58 | 58 | import java.security.Provider; | | 59 | +import java.security.SecureRandom; | 59 | 60 | import java.security.UnrecoverableKeyException; | 60 | 61 | import java.security.cert.CertificateException; | 61 | 62 | import java.security.cert.CertificateFactory; | @@ -439,7 +440,7 @@ trustManagerFactory, toX509Certificates(keyCertChainFile), | 439 | 440 | toPrivateKey(keyFile, keyPassword), | 440 | 441 | keyPassword, keyManagerFactory, ciphers, cipherFilter, apn, | 441 | 442 | sessionCacheSize, sessionTimeout, ClientAuth.NONE, null, | 442 | | - false, false, keyStore); | | 443 | + false, false, null, keyStore); | 443 | 444 | } catch (Exception e) { | 444 | 445 | if (e instanceof SSLException) { | 445 | 446 | throw (SSLException) e; | @@ -455,7 +456,8 @@ static SslContext newServerContextInternal( | 455 | 456 | X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory, | 456 | 457 | Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn, | 457 | 458 | 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) | 459 | 461 | throws SSLException { | 460 | 462 |
| 461 | 463 | if (provider == null) { | @@ -471,7 +473,7 @@ static SslContext newServerContextInternal( | 471 | 473 | return new JdkSslServerContext(sslContextProvider, | 472 | 474 | trustCertCollection, trustManagerFactory, keyCertChain, key, keyPassword, | 473 | 475 | keyManagerFactory, ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout, | 474 | | - clientAuth, protocols, startTls, keyStoreType); | | 476 | + clientAuth, protocols, startTls, secureRandom, keyStoreType); | 475 | 477 | } catch (SSLException e) { | 476 | 478 | throw e; | 477 | 479 | } catch (Exception e) { | @@ -805,7 +807,7 @@ public static SslContext newClientContext( | 805 | 807 | toX509Certificates(keyCertChainFile), toPrivateKey(keyFile, keyPassword), | 806 | 808 | keyPassword, keyManagerFactory, ciphers, cipherFilter, | 807 | 809 | apn, null, sessionCacheSize, sessionTimeout, false, | 808 | | - KeyStore.getDefaultType()); | | 810 | + null, KeyStore.getDefaultType()); | 809 | 811 | } catch (Exception e) { | 810 | 812 | if (e instanceof SSLException) { | 811 | 813 | throw (SSLException) e; | @@ -820,7 +822,8 @@ static SslContext newClientContextInternal( | 820 | 822 | X509Certificate[] trustCert, TrustManagerFactory trustManagerFactory, | 821 | 823 | X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory, | 822 | 824 | 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, | 824 | 827 | Map.Entry<SslContextOption<?>, Object>... options) throws SSLException { | 825 | 828 | if (provider == null) { | 826 | 829 | provider = defaultClientProvider(); | @@ -834,7 +837,7 @@ static SslContext newClientContextInternal( | 834 | 837 | return new JdkSslClientContext(sslContextProvider, | 835 | 838 | trustCert, trustManagerFactory, keyCertChain, key, keyPassword, | 836 | 839 | keyManagerFactory, ciphers, cipherFilter, apn, protocols, sessionCacheSize, | 837 | | - sessionTimeout, keyStoreType); | | 840 | + sessionTimeout, secureRandom, keyStoreType); | 838 | 841 | } catch (SSLException e) { | 839 | 842 | throw e; | 840 | 843 | } catch (Exception e) { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 30 | 30 | import java.security.KeyStore; | 31 | 31 | import java.security.PrivateKey; | 32 | 32 | import java.security.Provider; | | 33 | +import java.security.SecureRandom; | 33 | 34 | import java.security.cert.X509Certificate; | 34 | 35 | import java.util.ArrayList; | 35 | 36 | import java.util.HashMap; | @@ -205,6 +206,7 @@ public static SslContextBuilder forServer(KeyManager keyManager) { | 205 | 206 | private String[] protocols; | 206 | 207 | private boolean startTls; | 207 | 208 | private boolean enableOcsp; | | 209 | + private SecureRandom secureRandom; | 208 | 210 | private String keyStoreType = KeyStore.getDefaultType(); | 209 | 211 | private final Map<SslContextOption<?>, Object> options = new HashMap<>(); | 210 | 212 |
| @@ -600,6 +602,21 @@ public SslContextBuilder enableOcsp(boolean enableOcsp) { | 600 | 602 | return this; | 601 | 603 | } | 602 | 604 |
| | 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 | + | 603 | 620 | /** | 604 | 621 | * Create new {@code SslContext} instance with configured settings. | 605 | 622 | * <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 { | 610 | 627 | return SslContext.newServerContextInternal(provider, sslContextProvider, trustCertCollection, | 611 | 628 | trustManagerFactory, keyCertChain, key, keyPassword, keyManagerFactory, | 612 | 629 | 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)); | 614 | 631 | } else { | 615 | 632 | return SslContext.newClientContextInternal(provider, sslContextProvider, trustCertCollection, | 616 | 633 | 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, | 618 | 636 | toArray(options.entrySet(), EMPTY_ENTRIES)); | 619 | 637 | } | 620 | 638 | } | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 30 | 30 | import java.nio.charset.StandardCharsets; | 31 | 31 | import java.security.Principal; | 32 | 32 | import java.security.PrivateKey; | | 33 | +import java.security.SecureRandom; | 33 | 34 | import java.security.cert.CertificateException; | 34 | 35 | import java.security.cert.X509Certificate; | 35 | 36 | import java.util.Collections; | @@ -245,6 +246,16 @@ public void testInvalidCipherOpenSSL() throws Exception { | 245 | 246 | } | 246 | 247 | } | 247 | 248 |
| | 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 | + | 248 | 259 | private static void testKeyStoreType(SslProvider provider) throws Exception { | 249 | 260 | SelfSignedCertificate cert = new SelfSignedCertificate(); | 250 | 261 | SslContextBuilder builder = SslContextBuilder.forServer(cert.certificate(), cert.privateKey()) | @@ -327,6 +338,41 @@ private static void testServerContext(SslProvider provider) throws Exception { | 327 | 338 | engine.closeOutbound(); | 328 | 339 | } | 329 | 340 |
| | 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 | + | 330 | 376 | private static void testContextFromManagers(SslProvider provider) throws Exception { | 331 | 377 | final SelfSignedCertificate cert = new SelfSignedCertificate(); | 332 | 378 | KeyManager customKeyManager = new X509ExtendedKeyManager() { | @@ -426,4 +472,54 @@ public X509Certificate[] getAcceptedIssuers() { | 426 | 472 | server_engine.closeInbound(); | 427 | 473 | server_engine.closeOutbound(); | 428 | 474 | } | | 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 | + } | 429 | 525 | } |
|