2017-08-29 15:57:51 +02:00
|
|
|
package fr.gouv.etalab.mastodon.client;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.SSLSocket;
|
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 29/08/2017.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class TLSSocketFactory extends SSLSocketFactory {
|
|
|
|
|
2017-12-31 19:04:52 +01:00
|
|
|
private SSLSocketFactory sSLSocketFactory;
|
2017-08-29 15:57:51 +02:00
|
|
|
|
|
|
|
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
|
|
|
|
|
2017-12-31 19:04:52 +01:00
|
|
|
SSLContext context = SSLContext.getInstance("TLS");
|
|
|
|
context.init(null, null, null);
|
|
|
|
sSLSocketFactory = context.getSocketFactory();
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] getDefaultCipherSuites() {
|
2017-12-31 19:04:52 +01:00
|
|
|
return sSLSocketFactory.getDefaultCipherSuites();
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String[] getSupportedCipherSuites() {
|
2017-12-31 19:04:52 +01:00
|
|
|
return sSLSocketFactory.getSupportedCipherSuites();
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Socket createSocket() throws IOException {
|
2017-12-31 19:04:52 +01:00
|
|
|
return enableTLSOnSocket(sSLSocketFactory.createSocket());
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
|
2017-12-31 19:04:52 +01:00
|
|
|
return enableTLSOnSocket(sSLSocketFactory.createSocket(s, host, port, autoClose));
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-02 08:39:01 +01:00
|
|
|
public Socket createSocket(String host, int port) throws IOException {
|
2017-12-31 19:04:52 +01:00
|
|
|
return enableTLSOnSocket(sSLSocketFactory.createSocket(host, port));
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-02 08:39:01 +01:00
|
|
|
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
|
2017-12-31 19:04:52 +01:00
|
|
|
return enableTLSOnSocket(sSLSocketFactory.createSocket(host, port, localHost, localPort));
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Socket createSocket(InetAddress host, int port) throws IOException {
|
2017-12-31 19:04:52 +01:00
|
|
|
return enableTLSOnSocket(sSLSocketFactory.createSocket(host, port));
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
|
2017-12-31 19:04:52 +01:00
|
|
|
return enableTLSOnSocket(sSLSocketFactory.createSocket(address, port, localAddress, localPort));
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private Socket enableTLSOnSocket(Socket socket) {
|
|
|
|
if(socket != null && (socket instanceof SSLSocket)) {
|
2018-02-10 16:36:22 +01:00
|
|
|
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"});
|
2017-08-29 15:57:51 +02:00
|
|
|
}
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
}
|