Repository: netty/netty
Issue: Provide a way to disable cipher suites filtering
Labels: feature

### Expected behavior

There should be a way to have client side `SslContext` work against any website, regardless of security.

### Actual behavior

Supported ciphers are filtered against a hard coded whitelist (`SslUtils#DEFAULT_CIPHER_SUITES`).

I wonder if the whitelist way is a good thing. Wouldn't it filter out modern ciphers that would be introduced in JDK updates? Properly maintaining this blacklist would require Netty core devs to frequently update it. Wouldn't a blacklist work better: ban ciphers that are known as unsecure?

Then, my core issue is that there's no way to disable this security. On the client side, sometimes security is not the top most priority and users just want things to work, all the more as they do work with all the other user agents around (browsers, curl, Apache HttpComponents). Typical examples are load tests and web crawlers.

### Steps to reproduce

For example, it's not possible to connect to "https://www.gretalr.com" with a `JdkSslHandler` while it's possible with any other user agent I know of (reported against AHC as https://github.com/AsyncHttpClient/async-http-client/issues/1510).

### Minimal yet complete reproducer code (or URL to code)

```java
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
final SslContext sslContext = SslContextBuilder.forClient().build();
final CountDownLatch latch = new CountDownLatch(1);

Bootstrap b = new Bootstrap()
        .group(eventLoopGroup)
        .channel(NioSocketChannel.class)
        .handler(new ChannelInitializer<NioSocketChannel>() {
          @Override
          protected void initChannel(NioSocketChannel nioSocketChannel) {
            SSLEngine sslEngine = sslContext.newEngine(nioSocketChannel.alloc(), "www.gretalr.com", 443);
            SslHandler sslHandler = new SslHandler(sslEngine);
            sslHandler.handshakeFuture().addListener(whenHandshaked -> {
              if (whenHandshaked.isSuccess()) {
                System.out.println("All good");
              } else {
                whenHandshaked.cause().printStackTrace();
              }
              latch.countDown();
            });
            nioSocketChannel.pipeline().addLast(sslHandler);
          }
        });
Channel channel = b.connect("www.gretalr.com", 443).sync().channel();
latch.await();
channel.close();
eventLoopGroup.shutdownGracefully();
```

Note: I can connect fine if I create the SSLEngine from  a `javax.net.ssl.SSLContext` instead.

### Netty version

4.1.20.Final

### JVM version (e.g. `java -version`)

1.8.0_152

### OS version (e.g. `uname -a`)

not relevant

Issue comments:
Would you be interested in a PR that would introduce a System prop that would disable filtering?
So actually, a way better way to implement this would be to have a `CipherSuiteFilter` that, on contrary to `IdentityCipherSuiteFilter`, falls back to `supportedCiphers` instead of `defaultCiphers` when `ciphers` is null (ie when user didn't explicitly pass a set of ciphers).
It would be more convenient than having users duplicate Netty code for computing `supportedCiphers` for both JDK and OpenSSL providers and explicitly pass them.

@normanmaurer Does it make sense? Should I do a PR for this? Or am I wrong?
Yes this should be achievable via a `CipherSuiteFilter`. I don't think we should change the current behavior of `IdentityCipherSuiteFilter`, but what about creation of a new filter with this new behavior?
Yes, that's exactly what I meant. Sorry if I wasn't clear...
Sure. It doesn't have to be part of Netty, but if you think it is useful for others then feel free to submit a PR.
Will do later tonight 
Fixed by https://github.com/netty/netty/pull/7691

Pull request review comments:

Patch that resolved the issue:
diff --git a/handler/src/main/java/io/netty/handler/ssl/IdentityCipherSuiteFilter.java b/handler/src/main/java/io/netty/handler/ssl/IdentityCipherSuiteFilter.java
index c9ebfb0da26..cfd5bb7b2a9 100644
--- a/handler/src/main/java/io/netty/handler/ssl/IdentityCipherSuiteFilter.java
+++ b/handler/src/main/java/io/netty/handler/ssl/IdentityCipherSuiteFilter.java
@@ -23,15 +23,31 @@
  * This class will not do any filtering of ciphers suites.
  */
 public final class IdentityCipherSuiteFilter implements CipherSuiteFilter {
-    public static final IdentityCipherSuiteFilter INSTANCE = new IdentityCipherSuiteFilter();
 
-    private IdentityCipherSuiteFilter() { }
+    /**
+     * Defaults to default ciphers when provided ciphers are null
+     */
+    public static final IdentityCipherSuiteFilter INSTANCE = new IdentityCipherSuiteFilter(true);
+
+    /**
+     * Defaults to supported ciphers when provided ciphers are null
+     */
+    public static final IdentityCipherSuiteFilter INSTANCE_DEFAULTING_TO_SUPPORTED_CIPHERS =
+            new IdentityCipherSuiteFilter(false);
+
+    private final boolean defaultToDefaultCiphers;
+
+    private IdentityCipherSuiteFilter(boolean defaultToDefaultCiphers) {
+        this.defaultToDefaultCiphers = defaultToDefaultCiphers;
+    }
 
     @Override
     public String[] filterCipherSuites(Iterable<String> ciphers, List<String> defaultCiphers,
             Set<String> supportedCiphers) {
         if (ciphers == null) {
-            return defaultCiphers.toArray(new String[defaultCiphers.size()]);
+            return defaultToDefaultCiphers ?
+                    defaultCiphers.toArray(new String[defaultCiphers.size()]) :
+                    supportedCiphers.toArray(new String[supportedCiphers.size()]);
         } else {
             List<String> newCiphers = new ArrayList<String>(supportedCiphers.size());
             for (String c : ciphers) {
@@ -43,5 +59,4 @@ public String[] filterCipherSuites(Iterable<String> ciphers, List<String> defaul
             return newCiphers.toArray(new String[newCiphers.size()]);
         }
     }
-
 }


Test patch:
diff --git a/handler/src/test/java/io/netty/handler/ssl/IdentityCipherSuiteFilterTest.java b/handler/src/test/java/io/netty/handler/ssl/IdentityCipherSuiteFilterTest.java
new file mode 100644
index 00000000000..fd2dad8dc83
--- /dev/null
+++ b/handler/src/test/java/io/netty/handler/ssl/IdentityCipherSuiteFilterTest.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2018 The Netty Project
+ *
+ * The Netty Project licenses this file to you under the Apache License,
+ * version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at:
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+package io.netty.handler.ssl;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.junit.Assert.assertArrayEquals;
+
+public class IdentityCipherSuiteFilterTest {
+
+    @Test
+    public void regularInstanceDefaultsToDefaultCiphers() {
+        List<String> defaultCiphers = Arrays.asList("FOO", "BAR");
+        Set<String> supportedCiphers = new HashSet<String>(Arrays.asList("BAZ", "QIX"));
+        String[] filtered = IdentityCipherSuiteFilter.INSTANCE
+                .filterCipherSuites(null, defaultCiphers, supportedCiphers);
+        assertArrayEquals(defaultCiphers.toArray(), filtered);
+    }
+
+    @Test
+    public void alternativeInstanceDefaultsToSupportedCiphers() {
+        List<String> defaultCiphers = Arrays.asList("FOO", "BAR");
+        Set<String> supportedCiphers = new HashSet<String>(Arrays.asList("BAZ", "QIX"));
+        String[] filtered = IdentityCipherSuiteFilter.INSTANCE_DEFAULTING_TO_SUPPORTED_CIPHERS
+                .filterCipherSuites(null, defaultCiphers, supportedCiphers);
+        assertArrayEquals(supportedCiphers.toArray(), filtered);
+    }
+}