Support easily configuring the TLS cipher suites in MockWebServer
I'm trying to use MockWebServer to test my OkHttpClient configuration. The real web server my code hits just dropped support for TLS v1.0, so I'm changing my code to use TLS v1.2 and use the server's preferred cipher suites. I'd like to test this change with an in-memory web server that mimics the real server (in that it uses TLS 1.1 or 1.2, and supports certain cipher suites), but there's not an easy way to tell MockWebServer which cipher suites to use.
This isn't easy, because MockWebServer#useHttps expects an SSLSocketFactory, and there's no good way to configure the cipher suites that it will use. The way I did it was to wrap the underlying SSLSocketFactory, and call sslSocket.setEnabledCipherSuites(..) on the SSLSocket before returning it, like so:
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[] {...};
}
}
It's already kind of a hassle to set up a MockWebServer with HTTPS in a test, with the cert, keystore, trustmanager, etc., and this just makes it more complicated. It would be great if the cipher suites (and TLS version while we're at it) were easily configurable, but if not, then at least having a recipe to follow would help a lot.
If we supported ConnectionSpec in MockWebServer, that would go a long way? https://square.github.io/okhttp/3.x/okhttp/okhttp3/ConnectionSpec.html
That would be awesome! Especially since I’m using the same class to configure the OkHttpClient object that I’m testing.
On Jul 12, 2018, at 9:08 PM, Jesse Wilson [email protected] wrote:
If we supported ConnectionSpec in MockWebServer, that would go a long way? https://square.github.io/okhttp/3.x/okhttp/okhttp3/ConnectionSpec.html
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Won't fix, MockWebserver is secondary to client functionality here.