title: Thread-6, RECV TLSv1 ALERT: fatal, handshake_failure
body: <p>what is wrong with this code, it is supposed to trust all hosts, but it doesn't..</p>
  
  <p>It works fine with for example google.com but not with an API Gateway service running locally on my machine, why?</p>
  
  <p><strong>SSL DEBUG OUTPUT</strong></p>
  
  <blockquote>
    <p>trigger seeding of SecureRandom done seeding SecureRandom Ignoring
    unsupported cipher suite: TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 ...
    Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_128_CBC_SHA256
    Allow unsafe renegotiation: false Allow legacy hello messages: true Is
    initial handshake: true Is secure renegotiation: false Thread-6,
    setSoTimeout(0) called %% No cached client session
    *** ClientHello, TLSv1 RandomCookie:  GMT: 1434280256 bytes = { 216 ... 40 } Session ID:  {} Cipher Suites:
    [TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, ....
    SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_RC4_128_MD5,
    TLS_EMPTY_RENEGOTIATION_INFO_SCSV] Compression Methods:  { 0 }
    Extension elliptic_curves, curve names: {secp256r1 .. secp256k1}
    Extension ec_point_formats, formats: [uncompressed]</p>
    
    <p>Thread-6, WRITE: TLSv1 Handshake, length = 163 Thread-6, READ: TLSv1
    Alert, length = 2 Thread-6, RECV TLSv1 ALERT:  fatal,
    handshake_failure Thread-6, called closeSocket() Thread-6, handling
    exception: javax.net.ssl.SSLHandshakeException: **</p>
    
    <p>Received fatal alert: handshake_failure</p>
  </blockquote>
  
  <hr>
  
  <p>**</p>
  
  <pre><code>import java.io.InputStreamReader;
  import java.io.Reader;
  import java.net.URL;
  import java.net.URLConnection;
  
  import javax.net.ssl.HostnameVerifier;
  import javax.net.ssl.HttpsURLConnection;
  import javax.net.ssl.SSLContext;
  import javax.net.ssl.SSLSession;
  import javax.net.ssl.TrustManager;
  import javax.net.ssl.X509TrustManager;
  import java.security.cert.X509Certificate;
  
  public class ConnectHttps {
    public static void main(String[] args) throws Exception {
      /*
       *  fix for
       *    Exception in thread "main" javax.net.ssl.SSLHandshakeException:
       *       sun.security.validator.ValidatorException:
       *           PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
       *               unable to find valid certification path to requested target
       */
      TrustManager[] trustAllCerts = [
          [ getAcceptedIssuers: { -&gt; null },
            checkClientTrusted: { X509Certificate[] certs, String authType -&gt; },
            checkServerTrusted: { X509Certificate[] certs, String authType -&gt; } ] as X509TrustManager
      ]
  
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, new java.security.SecureRandom());
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  
      // Create all-trusting host name verifier
      HostnameVerifier allHostsValid = new HostnameVerifier() {
          public boolean verify(String hostname, SSLSession session) {
            return true;
          }
      };
      // Install the all-trusting host verifier
      HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
      /*
       * end of the fix
       */
  
      //URL url = new URL("https://google.com"); //WORKS
      URL url = new URL("https://localhost:8090");   // DOES NOT WORK, WHY?
      URLConnection con = url.openConnection();
      Reader reader = new InputStreamReader(con.getInputStream());
      while (true) {
        int ch = reader.read();
        if (ch==-1) {
          break;
        }
        System.out.print((char)ch);
      }
    }
  }
  </code></pre>
  
  <p>Running <a href="https://bugs.launchpad.net/ubuntu/+source/openjdk-7/+bug/1314113" rel="nofollow">the code found here</a> it shows that TLSv1.2 is not enabled on the client side:</p>
  
  <blockquote>
    <p>Supported Protocols: 5<br>
    SSLv2Hello<br>
    SSLv3<br>
    TLSv1<br>
    TLSv1.1<br>
    TLSv1.2</p>
    
    <p>Enabled Protocols: 2<br>
    SSLv3<br>
    TLSv1</p>
  </blockquote>