如何为Java HTTPS服务器配置SSL密码套件?

6

注意:有关Java HTTPS客户端的问题已经被问及多次。我的问题是关于配置服务器的。

我正在尝试使用MockWebServer测试我的OkHttpClient配置。我的代码所连接的真实Web服务器已经停用了TLS v1.0支持,因此我正在更改我的代码以使用TLS v1.2并使用服务器首选的密码套件。我想使用模拟真实服务器的内存Web服务器来测试这个更改,但我无法在我的测试中配置一个带有特定密码套件列表的SSLContext。(我需要访问的所有方法都嵌套在SSLContextImpl及其内部类中并且都有很好的保护。)

我能够找到的最好解决方案就是完全包装一个SSLServerSocketFactory,并覆盖4个createServerSocket()方法,在返回之前在SSLServerSocket上调用setEnabledCipherSuites(),类似于此答案如何在客户端上使用SSLSocketFactoryEx:https://dev59.com/RnNA5IYBdhLWcg3wPbWe#23365536

令人沮丧的是,使用特定的TLS版本就像调用例如SSLContext.getInstance("TLSv1.2")一样简单,但没有类似于它那样容易的方法来配置密码套件。

2个回答

3
我最终采取的方法是包装底层的SSLSocketFactory,并在返回它之前在SSLSocket上调用sslSocket.setEnabledCipherSuites(..),代码如下:
final class MySSLSocketFactory extends SSLSocketFactory {
    final SSLSocketFactory wrappedSSLSocketFactory;

    MySSLSocketFactory(SSLSocketFactory wrappedSSLSocketFactory) {
        this.wrappedSSLSocketFactory = wrappedSSLSocketFactory;
    }

    @Override
    public String[] getDefaultCipherSuites() {
        return getMyCipherSuites();
    }

    @Override
    public String[] getSupportedCipherSuites() {
        return getMyCipherSuites();
    }

    @Override
    public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
        final SSLSocket sslSocket = (SSLSocket) wrappedSSLSocketFactory.createSocket(s, host, port, autoClose);

        // change the supported cipher suites on the socket, *before* it's returned to the client
        sslSocket.setEnabledCipherSuites(getMyCipherSuites());

        return sslSocket;
    }

    // other overloaded createSocket() methods do the same

    private String[] getMyCipherSuites() {
        // TODO: change this to return whatever cipher suites you want the server to use, from CipherSuite
        return new String[] {...};
    }
}

只是一个小问题,您包装了SSLSocketFactory而不是SSLServerSocketFactory... - Douglas Held

-3

1
这只是针对单个套接字进行更改,而不是整个服务器。您将如何在类似于OkHttp的MockWebServer(甚至是WireMock服务器)中配置密码套件? - Steve K
1
想要为OkHttp提出一个功能请求吗?看起来这是我们应该做的事情。 - Jesse Wilson
@JesseWilson 这就是已经在这里进行的工作吗?https://github.com/square/okhttp/tree/master/okhttp-tls - Steve K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接