package nerd.tuxmobil.fahrplan.congress.net;

import android.support.annotation.NonNull;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

class SslSocketFactory {

    private static final String PROTOCOL_NAME_TLS = "TLS";

    @NonNull
    static SSLSocketFactory createSSLSocketFactory(@NonNull TrustManager trustManager)
            throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = SSLContext.getInstance(PROTOCOL_NAME_TLS);
        TrustManager[] trustManagers = {trustManager};
        sslContext.init(null, trustManagers, new SecureRandom());
        return sslContext.getSocketFactory();
    }

}

@@ -39,16 +39,41 @@
 s.net;%0A%0A
+import android.os.Build;%0A
 import a
@@ -108,16 +108,97 @@
 nNull;%0A%0A
+import java.io.IOException;%0Aimport java.net.InetAddress;%0Aimport java.net.Socket;%0A
 import j
@@ -350,16 +350,48 @@
 ontext;%0A
+import javax.net.ssl.SSLSocket;%0A
 import j
@@ -457,16 +457,23 @@
 nager;%0A%0A
+public 
 class Ss
@@ -487,16 +487,41 @@
 Factory 
+extends SSLSocketFactory 
 %7B%0A%0A    p
@@ -573,16 +573,315 @@
 = %22TLS%22;
+%0A    private static final String PROTOCOL_NAME_TLS_1_1 = %22TLSv1.1%22;%0A    private static final String PROTOCOL_NAME_TLS_1_2 = %22TLSv1.2%22;%0A%0A    private static final String%5B%5D ENABLED_PROTOCOLS = %7BPROTOCOL_NAME_TLS_1_1, PROTOCOL_NAME_TLS_1_2%7D;%0A%0A    private final SSLSocketFactory internalSslSocketFactory;
 %0A%0A    @N
@@ -1252,44 +1252,2150 @@
     
-return sslContext.getSocketFactory()
+if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.LOLLIPOP) %7B%0A            return new SslSocketFactory(sslContext);%0A        %7D else %7B%0A            return sslContext.getSocketFactory();%0A        %7D%0A    %7D%0A%0A    private SslSocketFactory(@NonNull SSLContext sslContext)%0A            throws KeyManagementException, NoSuchAlgorithmException %7B%0A        internalSslSocketFactory = sslContext.getSocketFactory();%0A    %7D%0A%0A    @Override%0A    public String%5B%5D getDefaultCipherSuites() %7B%0A        return internalSslSocketFactory.getDefaultCipherSuites();%0A    %7D%0A%0A    @Override%0A    public String%5B%5D getSupportedCipherSuites() %7B%0A        return internalSslSocketFactory.getSupportedCipherSuites();%0A    %7D%0A%0A    @Override%0A    public Socket createSocket() throws IOException %7B%0A        return enableTlsOnSocket(internalSslSocketFactory.createSocket());%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException %7B%0A        return enableTlsOnSocket(internalSslSocketFactory.createSocket(socket, host, port, autoClose));%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(String host, int port) throws IOException %7B%0A        return enableTlsOnSocket(internalSslSocketFactory.createSocket(host, port));%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException %7B%0A        return enableTlsOnSocket(internalSslSocketFactory.createSocket(host, port, localHost, localPort));%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(InetAddress host, int port) throws IOException %7B%0A        return enableTlsOnSocket(internalSslSocketFactory.createSocket(host, port));%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException %7B%0A        return enableTlsOnSocket(internalSslSocketFactory.createSocket(address, port, localAddress, localPort));%0A    %7D%0A%0A    private Socket enableTlsOnSocket(Socket socket) %7B%0A        if (socket != null && socket instanceof SSLSocket) %7B%0A            ((SSLSocket) socket).setEnabledProtocols(ENABLED_PROTOCOLS);%0A        %7D%0A        return socket
 ;%0A  

Fix SSLHandshakeException / SSLProtocolException on Android 4.