GeezerVison is showing github.com — Add support for specifying SecureRandom in SSLContext initialization … · netty/netty@9841b19
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/ssl test/java/io/netty/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/netty/handler/ssl/JdkSslClientContext.java +8-6Lines changed: 8 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 27 | 27 | import javax.net.ssl.TrustManagerFactory; | 28 | 28 | import java.io.File; | 29 | 29 | import java.security.PrivateKey; | | 30 | +import java.security.SecureRandom; | 30 | 31 | import java.security.cert.X509Certificate; | 31 | 32 |
| 32 | 33 | /** | @@ -175,7 +176,7 @@ public JdkSslClientContext( | 175 | 176 | long sessionCacheSize, long sessionTimeout) throws SSLException { | 176 | 177 | super(newSSLContext(provider, toX509CertificatesInternal(trustCertCollectionFile), | 177 | 178 | trustManagerFactory, null, null, | 178 | | - null, null, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), true, | | 179 | + null, null, sessionCacheSize, sessionTimeout, null, KeyStore.getDefaultType()), true, | 179 | 180 | ciphers, cipherFilter, apn, ClientAuth.NONE, null, false); | 180 | 181 | } | 181 | 182 |
| @@ -258,7 +259,8 @@ public JdkSslClientContext(File trustCertCollectionFile, TrustManagerFactory tru | 258 | 259 | super(newSSLContext(null, toX509CertificatesInternal( | 259 | 260 | trustCertCollectionFile), trustManagerFactory, | 260 | 261 | toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword), | 261 | | - keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), true, | | 262 | + keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, | | 263 | + null, KeyStore.getDefaultType()), true, | 262 | 264 | ciphers, cipherFilter, apn, ClientAuth.NONE, null, false); | 263 | 265 | } | 264 | 266 |
| @@ -267,11 +269,11 @@ public JdkSslClientContext(File trustCertCollectionFile, TrustManagerFactory tru | 267 | 269 | X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, | 268 | 270 | KeyManagerFactory keyManagerFactory, Iterable<String> ciphers, CipherSuiteFilter cipherFilter, | 269 | 271 | ApplicationProtocolConfig apn, String[] protocols, long sessionCacheSize, long sessionTimeout, | 270 | | - String keyStoreType) | | 272 | + SecureRandom secureRandom, String keyStoreType) | 271 | 273 | throws SSLException { | 272 | 274 | super(newSSLContext(sslContextProvider, trustCertCollection, trustManagerFactory, | 273 | 275 | keyCertChain, key, keyPassword, keyManagerFactory, sessionCacheSize, | 274 | | - sessionTimeout, keyStoreType), | | 276 | + sessionTimeout, secureRandom, keyStoreType), | 275 | 277 | true, ciphers, cipherFilter, toNegotiator(apn, false), ClientAuth.NONE, protocols, false); | 276 | 278 | } | 277 | 279 |
| @@ -280,7 +282,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider, | 280 | 282 | TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain, | 281 | 283 | PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory, | 282 | 284 | long sessionCacheSize, long sessionTimeout, | 283 | | - String keyStore) throws SSLException { | | 285 | + SecureRandom secureRandom, String keyStore) throws SSLException { | 284 | 286 | try { | 285 | 287 | if (trustCertCollection != null) { | 286 | 288 | trustManagerFactory = buildTrustManagerFactory(trustCertCollection, trustManagerFactory, keyStore); | @@ -293,7 +295,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider, | 293 | 295 | : SSLContext.getInstance(PROTOCOL, sslContextProvider); | 294 | 296 | ctx.init(keyManagerFactory == null ? null : keyManagerFactory.getKeyManagers(), | 295 | 297 | trustManagerFactory == null ? null : trustManagerFactory.getTrustManagers(), | 296 | | - null); | | 298 | + secureRandom); | 297 | 299 |
| 298 | 300 | SSLSessionContext sessCtx = ctx.getClientSessionContext(); | 299 | 301 | if (sessionCacheSize > 0) { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 28 | 28 | import java.security.NoSuchAlgorithmException; | 29 | 29 | import java.security.PrivateKey; | 30 | 30 | import java.security.Provider; | | 31 | +import java.security.SecureRandom; | 31 | 32 | import java.security.UnrecoverableKeyException; | 32 | 33 | import java.security.cert.CertificateException; | 33 | 34 | import java.security.cert.X509Certificate; | @@ -201,7 +202,7 @@ public JdkSslServerContext( | 201 | 202 | long sessionCacheSize, long sessionTimeout, String keyStore) throws SSLException { | 202 | 203 | super(newSSLContext(provider, null, null, | 203 | 204 | toX509CertificatesInternal(certChainFile), toPrivateKeyInternal(keyFile, keyPassword), | 204 | | - keyPassword, null, sessionCacheSize, sessionTimeout, keyStore), false, | | 205 | + keyPassword, null, sessionCacheSize, sessionTimeout, null, keyStore), false, | 205 | 206 | ciphers, cipherFilter, apn, ClientAuth.NONE, null, false); | 206 | 207 | } | 207 | 208 |
| @@ -241,7 +242,7 @@ public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory tru | 241 | 242 | long sessionCacheSize, long sessionTimeout) throws SSLException { | 242 | 243 | super(newSSLContext(null, toX509CertificatesInternal(trustCertCollectionFile), trustManagerFactory, | 243 | 244 | toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword), | 244 | | - keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, null), false, | | 245 | + keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, null, null), false, | 245 | 246 | ciphers, cipherFilter, apn, ClientAuth.NONE, null, false); | 246 | 247 | } | 247 | 248 |
| @@ -282,7 +283,8 @@ public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory tru | 282 | 283 | long sessionCacheSize, long sessionTimeout) throws SSLException { | 283 | 284 | super(newSSLContext(null, toX509CertificatesInternal(trustCertCollectionFile), trustManagerFactory, | 284 | 285 | toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword), | 285 | | - keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), false, | | 286 | + keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, | | 287 | + null, KeyStore.getDefaultType()), false, | 286 | 288 | ciphers, cipherFilter, apn, ClientAuth.NONE, null, false); | 287 | 289 | } | 288 | 290 |
| @@ -292,16 +294,17 @@ public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory tru | 292 | 294 | KeyManagerFactory keyManagerFactory, Iterable<String> ciphers, CipherSuiteFilter cipherFilter, | 293 | 295 | ApplicationProtocolConfig apn, long sessionCacheSize, long sessionTimeout, | 294 | 296 | ClientAuth clientAuth, String[] protocols, boolean startTls, | 295 | | - String keyStore) throws SSLException { | | 297 | + SecureRandom secureRandom, String keyStore) throws SSLException { | 296 | 298 | super(newSSLContext(provider, trustCertCollection, trustManagerFactory, keyCertChain, key, | 297 | | - keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, keyStore), false, | | 299 | + keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, secureRandom, keyStore), false, | 298 | 300 | ciphers, cipherFilter, toNegotiator(apn, true), clientAuth, protocols, startTls); | 299 | 301 | } | 300 | 302 |
| 301 | 303 | 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) | 305 | 308 | throws SSLException { | 306 | 309 | if (key == null && keyManagerFactory == null) { | 307 | 310 | throw new NullPointerException("key, keyManagerFactory"); | @@ -327,7 +330,7 @@ private static SSLContext newSSLContext(Provider sslContextProvider, X509Certifi | 327 | 330 | : SSLContext.getInstance(PROTOCOL, sslContextProvider); | 328 | 331 | ctx.init(keyManagerFactory.getKeyManagers(), | 329 | 332 | wrapTrustManagerIfNeeded(trustManagerFactory.getTrustManagers()), | 330 | | - null); | | 333 | + secureRandom); | 331 | 334 |
| 332 | 335 | SSLSessionContext sessCtx = ctx.getServerSessionContext(); | 333 | 336 | if (sessionCacheSize > 0) { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 42 | 42 | import java.security.NoSuchAlgorithmException; | 43 | 43 | import java.security.PrivateKey; | 44 | 44 | import java.security.Provider; | | 45 | +import java.security.SecureRandom; | 45 | 46 | import java.security.UnrecoverableKeyException; | 46 | 47 | import java.security.cert.CertificateException; | 47 | 48 | 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) { | @@ -470,7 +472,7 @@ static SslContext newServerContextInternal( | 470 | 472 | return new JdkSslServerContext(sslContextProvider, | 471 | 473 | trustCertCollection, trustManagerFactory, keyCertChain, key, keyPassword, | 472 | 474 | keyManagerFactory, ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout, | 473 | | - clientAuth, protocols, startTls, keyStoreType); | | 475 | + clientAuth, protocols, startTls, secureRandom, keyStoreType); | 474 | 476 | case OPENSSL: | 475 | 477 | verifyNullSslContextProvider(provider, sslContextProvider); | 476 | 478 | return new OpenSslServerContext( | @@ -799,7 +801,7 @@ public static SslContext newClientContext( | 799 | 801 | toX509Certificates(keyCertChainFile), toPrivateKey(keyFile, keyPassword), | 800 | 802 | keyPassword, keyManagerFactory, ciphers, cipherFilter, | 801 | 803 | apn, null, sessionCacheSize, sessionTimeout, false, | 802 | | - KeyStore.getDefaultType()); | | 804 | + null, KeyStore.getDefaultType()); | 803 | 805 | } catch (Exception e) { | 804 | 806 | if (e instanceof SSLException) { | 805 | 807 | throw (SSLException) e; | @@ -814,7 +816,8 @@ static SslContext newClientContextInternal( | 814 | 816 | X509Certificate[] trustCert, TrustManagerFactory trustManagerFactory, | 815 | 817 | X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory, | 816 | 818 | 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, | 818 | 821 | Map.Entry<SslContextOption<?>, Object>... options) throws SSLException { | 819 | 822 | if (provider == null) { | 820 | 823 | provider = defaultClientProvider(); | @@ -827,7 +830,7 @@ static SslContext newClientContextInternal( | 827 | 830 | return new JdkSslClientContext(sslContextProvider, | 828 | 831 | trustCert, trustManagerFactory, keyCertChain, key, keyPassword, | 829 | 832 | keyManagerFactory, ciphers, cipherFilter, apn, protocols, sessionCacheSize, | 830 | | - sessionTimeout, keyStoreType); | | 833 | + sessionTimeout, secureRandom, keyStoreType); | 831 | 834 | case OPENSSL: | 832 | 835 | verifyNullSslContextProvider(provider, sslContextProvider); | 833 | 836 | OpenSsl.ensureAvailability(); | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 31 | 31 | import java.security.KeyStore; | 32 | 32 | import java.security.PrivateKey; | 33 | 33 | import java.security.Provider; | | 34 | +import java.security.SecureRandom; | 34 | 35 | import java.security.cert.X509Certificate; | 35 | 36 | import java.util.ArrayList; | 36 | 37 | import java.util.HashMap; | @@ -206,6 +207,7 @@ public static SslContextBuilder forServer(KeyManager keyManager) { | 206 | 207 | private String[] protocols; | 207 | 208 | private boolean startTls; | 208 | 209 | private boolean enableOcsp; | | 210 | + private SecureRandom secureRandom; | 209 | 211 | private String keyStoreType = KeyStore.getDefaultType(); | 210 | 212 | private final Map<SslContextOption<?>, Object> options = new HashMap<SslContextOption<?>, Object>(); | 211 | 213 |
| @@ -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/netty/handler/ssl/SslContextBuilderTest.java +96Lines changed: 96 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 31 | 31 | import java.net.Socket; | 32 | 32 | import java.security.Principal; | 33 | 33 | import java.security.PrivateKey; | | 34 | +import java.security.SecureRandom; | 34 | 35 | import java.security.cert.CertificateException; | 35 | 36 | import java.security.cert.X509Certificate; | 36 | 37 | import java.util.Collections; | @@ -244,6 +245,16 @@ public void testInvalidCipherOpenSSL() throws Exception { | 244 | 245 | } | 245 | 246 | } | 246 | 247 |
| | 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 | + | 247 | 258 | private static void testKeyStoreType(SslProvider provider) throws Exception { | 248 | 259 | SelfSignedCertificate cert = new SelfSignedCertificate(); | 249 | 260 | SslContextBuilder builder = SslContextBuilder.forServer(cert.certificate(), cert.privateKey()) | @@ -326,6 +337,41 @@ private static void testServerContext(SslProvider provider) throws Exception { | 326 | 337 | engine.closeOutbound(); | 327 | 338 | } | 328 | 339 |
| | 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 | + | 329 | 375 | private static void testContextFromManagers(SslProvider provider) throws Exception { | 330 | 376 | final SelfSignedCertificate cert = new SelfSignedCertificate(); | 331 | 377 | KeyManager customKeyManager = new X509ExtendedKeyManager() { | @@ -425,4 +471,54 @@ public X509Certificate[] getAcceptedIssuers() { | 425 | 471 | server_engine.closeInbound(); | 426 | 472 | server_engine.closeOutbound(); | 427 | 473 | } | | 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 | + } | 428 | 524 | } |
|