fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/client/TLSSocketFactory.java

86 lines
2.6 KiB
Java
Raw Normal View History

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;
2018-11-27 18:18:05 +01:00
2017-08-29 15:57:51 +02:00
import javax.net.ssl.SSLContext;
2018-10-30 17:15:29 +01:00
import javax.net.ssl.SSLEngine;
2017-08-29 15:57:51 +02:00
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
/**
* Created by Thomas on 29/08/2017.
*
*/
public class TLSSocketFactory extends SSLSocketFactory {
private SSLSocketFactory sSLSocketFactory;
2018-11-27 18:18:05 +01:00
private SSLContext sslContext;
2017-08-29 15:57:51 +02:00
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
2018-11-27 18:18:05 +01:00
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
sSLSocketFactory = sslContext.getSocketFactory();
2017-08-29 15:57:51 +02:00
}
2018-10-30 13:46:24 +01:00
public SSLContext getSSLContext(){
2018-11-27 18:18:05 +01:00
return this.sslContext;
2018-10-30 13:46:24 +01:00
}
2018-10-30 17:15:29 +01:00
public SSLEngine getSSLEngine(){
2018-11-27 18:18:05 +01:00
return this.sslContext.createSSLEngine();
2018-10-30 17:15:29 +01:00
}
2017-08-29 15:57:51 +02:00
@Override
public String[] getDefaultCipherSuites() {
return sSLSocketFactory.getDefaultCipherSuites();
2017-08-29 15:57:51 +02:00
}
@Override
public String[] getSupportedCipherSuites() {
return sSLSocketFactory.getSupportedCipherSuites();
2017-08-29 15:57:51 +02:00
}
@Override
public Socket createSocket() throws IOException {
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 {
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 {
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 {
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 {
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 {
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;
}
}