title: Android 6 can not communicate with secured server using Self Sign Certificate
body: <p>I am unable to establish connection between Android 6 app and Secured Server where the server is using self signed certificate generated by Java (jdk 1.6) keytool command. Android 6 app is showing following error.</p>
  
  <p>Excepton Log:</p>
  
  <pre><code>javax.net.ssl.SSLHandshakeException: Handshake failed : javax.net.ssl.SSLHandshakeException: Handshake failed
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:429)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.Connection.connectTls(Connection.java:235)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.Connection.connectSocket(Connection.java:199)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.Connection.connect(Connection.java:172)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:257)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
  05-23 17:21:03.498 3602-6487/issac.wise.pay I/System.out:     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
  </code></pre>
  
  <p>I am using the below code and it is working fine upto android 5.9 but it is not working with android 6(marshmallow); following link provides the code to communicate to ssl -</p>
  
  <p><a href="https://developer.android.com/training/articles/security-ssl.html" rel="nofollow noreferrer">https://developer.android.com/training/articles/security-ssl.html</a></p>
  
  <pre><code>// Load CAs from an InputStream
  // (could be from a resource or ByteArrayInputStream or ...)
  CertificateFactory cf = CertificateFactory.getInstance("X.509");
  // From https://www.washington.edu/itconnect/security/ca/load-der.crt
  InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
  Certificate ca;
  try {
      ca = cf.generateCertificate(caInput);
      System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
  } finally {
      caInput.close();
  }
  
  // Create a KeyStore containing our trusted CAs
  String keyStoreType = KeyStore.getDefaultType();
  KeyStore keyStore = KeyStore.getInstance(keyStoreType);
  keyStore.load(null, null);
  keyStore.setCertificateEntry("ca", ca);
  
  // Create a TrustManager that trusts the CAs in our KeyStore
  String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
  TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
  tmf.init(keyStore);
  
  // Create an SSLContext that uses our TrustManager
  SSLContext context = SSLContext.getInstance("TLS");
  context.init(null, tmf.getTrustManagers(), null);
  
  // Tell the URLConnection to use a SocketFactory from our SSLContext
  URL url = new URL("https://certs.cac.washington.edu/CAtest/");
  HttpsURLConnection urlConnection =
      (HttpsURLConnection)url.openConnection();
  urlConnection.setSSLSocketFactory(context.getSocketFactory());
  InputStream in = urlConnection.getInputStream();
  copyInputStreamToOutputStream(in, System.out);
  </code></pre>
  
  <p>Can anybody please suggest how to resolve this? Is this any certificate generation problem or do I need to follow any new technique to communicate with server?</p>