title: How to enable TLS 1.2 support in an Android application (running on Android 4.1 JB)
body: <p>As per the docs in Android for <code>SSLSocket</code> and <code>SSLContext</code>, TLS v1.1 and v1.2 protocols are supported in API level 16+, but are not enabled by default.
  <a href="http://developer.android.com/reference/javax/net/ssl/SSLSocket.html" rel="noreferrer">http://developer.android.com/reference/javax/net/ssl/SSLSocket.html</a>
  <a href="http://developer.android.com/reference/javax/net/ssl/SSLContext.html" rel="noreferrer">http://developer.android.com/reference/javax/net/ssl/SSLContext.html</a></p>
  <p>How do I enable it on a device running Android 4.1 or later (but below 5.0)?</p>
  <p>I have tried creating a custom SSLSocketFactory which enables all the supported protocols when <code>Socket</code>'s are created and later use my custom implementation as:</p>
  <blockquote>
  <p>HttpsURLConnection.setDefaultSSLSocketFactory(new MySSLSocketFactory());</p>
  </blockquote>
  <pre><code>public class MySSLSocketFactory extends SSLSocketFactory {
          
          private SSLContext sc;
          private SSLSocketFactory ssf;  
          
          public MySSLSocketFactory() {
              try {
                  sc = SSLContext.getInstance(&quot;TLS&quot;);
                  sc.init(null, null, null);
                  ssf = sc.getSocketFactory();
  
              } catch (NoSuchAlgorithmException e) {
                  e.printStackTrace();
              } catch (KeyManagementException e) {
                  e.printStackTrace();
              }  
          }
          
          @Override
          public Socket createSocket(Socket s, String host, int port, boolean autoClose)
                  throws IOException {
              SSLSocket ss = (SSLSocket) ssf.createSocket(s, host, port, autoClose);
              ss.setEnabledProtocols(ss.getSupportedProtocols());
              ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
              return ss;
          }
  
          @Override
          public String[] getDefaultCipherSuites() {
              return ssf.getDefaultCipherSuites();
          }
  
          @Override
          public String[] getSupportedCipherSuites() {
              return ssf.getSupportedCipherSuites();
          }
  
          @Override
          public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
              SSLSocket ss = (SSLSocket) ssf.createSocket(host, port);
              ss.setEnabledProtocols(ss.getSupportedProtocols());
              ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
              return ss;
          }
  
          @Override
          public Socket createSocket(InetAddress host, int port) throws IOException {
              SSLSocket ss = (SSLSocket) ssf.createSocket(host, port);
              ss.setEnabledProtocols(ss.getSupportedProtocols());
              ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
              return ss;
          }
  
          @Override
          public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
                  throws IOException, UnknownHostException {
              SSLSocket ss = (SSLSocket) ssf.createSocket(host, port, localHost, localPort);
              ss.setEnabledProtocols(ss.getSupportedProtocols());
              ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
              return ss;
          }
  
          @Override
          public Socket createSocket(InetAddress address, int port, InetAddress localAddress,
                  int localPort) throws IOException {
              SSLSocket ss = (SSLSocket) ssf.createSocket(address, port, localAddress, localPort);
              ss.setEnabledProtocols(ss.getSupportedProtocols());
              ss.setEnabledCipherSuites(ss.getSupportedCipherSuites());
              return ss;
          }
      }
  </code></pre>
  <p>But it still gives an exception while trying to establish a connection with a server on which <strong>Only TLS 1.2</strong> is enabled.</p>
  <p>Here is the exception I get:</p>
  <blockquote>
  <p>03-09 09:21:38.427: W/System.err(2496):
  javax.net.ssl.SSLHandshakeException:
  javax.net.ssl.SSLProtocolException: SSL handshake aborted:
  ssl=0xb7fa0620: Failure in SSL library, usually a protocol error</p>
  <p>03-09 09:21:38.427: W/System.err(2496): error:14077410:SSL
  routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
  (external/openssl/ssl/s23_clnt.c:741 0xa90e6990:0x00000000)</p>
  </blockquote>