title: SSLHandshakeException when sending POST request via https
body: <p>I tryed to send a POST request to a remote secure server ( https call).
  First i used the OkHTTP lib to make the call, as this lib worked to make http calls in a previous app, but with https it does not work anymore, see my previous post : <a href="https://stackoverflow.com/questions/48931244/cannot-make-https-calls-with-okhttp-lib">Cannot make HTTPS calls with OKHTTP lib</a></p>
  
  <p>I decided to try the HttpURLConnection to make the call as explained here : <a href="https://developer.android.com/reference/java/net/HttpURLConnection.html" rel="nofollow noreferrer">https://developer.android.com/reference/java/net/HttpURLConnection.html</a>
  The code I am using only works on certain devices. </p>
  
  <p>It does work with a Wiko on android 6.0 equiped with SIM card and wifi and does not work on a Samsung on Android 4.2.2 only linked to internet via Wifi.</p>
  
  <p>Here is a logcat of the problem I encounter  :</p>
  
  <pre><code>02-23 12:29:59.893 11026-11951 W/System.err: javax.net.ssl.SSLHandshakeException: com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate signature.
  02-23 12:29:59.923 11026-11951 W/System.err:     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:382)
  02-23 12:29:59.923 11026-11951 W/System.err:     at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:231)
  02-23 12:29:59.923 11026-11951 W/System.err:     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478)
  02-23 12:29:59.923 11026-11951 W/System.err:     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
  02-23 12:29:59.923 11026-11951 W/System.err:     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
  02-23 12:29:59.923 11026-11951 W/System.err:     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
  02-23 12:29:59.923 11026-11951 W/System.err:     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
  02-23 12:29:59.923 11026-11951 W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
  02-23 12:29:59.923 11026-11951 W/System.err:     at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
  02-23 12:29:59.923 11026-11951 W/System.err:     at com.gulplug.gulplugtoolbox.DiagnosisOnline$NetworkAsyncTask.doInBackground(DiagnosisOnline.java:109)
  02-23 12:29:59.923 11026-11951 W/System.err:     at com.gulplug.gulplugtoolbox.DiagnosisOnline$NetworkAsyncTask.doInBackground(DiagnosisOnline.java:82)
  02-23 12:29:59.923 11026-11951 W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
  02-23 12:29:59.923 11026-11951 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  02-23 12:29:59.923 11026-11951 W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  02-23 12:29:59.923 11026-11951 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  02-23 12:29:59.923 11026-11951 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  02-23 12:29:59.933 11026-11951 W/System.err:     at java.lang.Thread.run(Thread.java:856)
  02-23 12:29:59.933 11026-11951 W/System.err: Caused by: java.security.cert.CertificateException: com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate signature.
  02-23 12:29:59.933 11026-11951 W/System.err:     at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:296)
  02-23 12:29:59.933 11026-11951 W/System.err:     at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:197)
  02-23 12:29:59.933 11026-11951 W/System.err:     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:598)
  02-23 12:29:59.933 11026-11951 W/System.err:     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
  02-23 12:29:59.933 11026-11951 W/System.err:     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:379)
  02-23 12:29:59.933 11026-11951 W/System.err:    ... 16 more
  02-23 12:29:59.933 11026-11951 W/System.err: Caused by: com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate signature.
  02-23 12:29:59.943 11026-11951 W/System.err:     at com.android.org.bouncycastle.jce.provider.RFC3280CertPathUtilities.processCertA(RFC3280CertPathUtilities.java:1475)
  02-23 12:29:59.943 11026-11951 W/System.err:     at com.android.org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi.engineValidate(PKIXCertPathValidatorSpi.java:305)
  02-23 12:29:59.943 11026-11951 W/System.err:     at com.sec.android.security.pkix.SecCertPathValidatorSpi.engineValidate(SecCertPathValidatorSpi.java:99)
  02-23 12:29:59.953 11026-11951 W/System.err:     at java.security.cert.CertPathValidator.validate(CertPathValidator.java:190)
  02-23 12:29:59.953 11026-11951 W/System.err:     at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:283)
  02-23 12:29:59.953 11026-11951 W/System.err:    ... 20 more
  02-23 12:29:59.953 11026-11951 W/System.err: Caused by: java.security.SignatureException: Signature was not verified
  02-23 12:29:59.953 11026-11951 W/System.err:     at org.apache.harmony.security.provider.cert.X509CertImpl.verify(X509CertImpl.java:384)
  02-23 12:29:59.953 11026-11951 W/System.err:     at com.android.org.bouncycastle.jce.provider.CertPathValidatorUtilities.verifyX509Certificate(CertPathValidatorUtilities.java:1428)
  02-23 12:29:59.953 11026-11951 W/System.err:     at com.android.org.bouncycastle.jce.provider.RFC3280CertPathUtilities.processCertA(RFC3280CertPathUtilities.java:1470)
  02-23 12:29:59.953 11026-11951 W/System.err:    ... 24 more
  </code></pre>
  
  <p>and the code attached to this log : </p>
  
  <pre><code>class NetworkAsyncTask extends AsyncTask&lt;Void, Void, Void&gt; {
      @Override
      protected Void doInBackground(Void... voids) {
          Authentification authentification = new Authentification();
          authentification.setApplicationToken(applicationtoken);
          Gson gson = new Gson();
          //String json = gson.toJson(authentification);
          String json = "";
          Log.d(TAG, json);
          JSONObject auth = new JSONObject();
          try {
              auth.put("application_token",mytoken);
          } catch (JSONException e) {
              e.printStackTrace();
          }
          String url = myurl;
  
          try {
              URL object = new URL(url);
              HttpsURLConnection con = (HttpsURLConnection) object.openConnection();
  
              con.setDoOutput(true);
              con.setDoInput(true);
              con.setRequestProperty("Content-Type", "application/json");
              con.setRequestProperty("Accept", "application/json");
              con.setRequestProperty("Content-Lenght", String.valueOf(json.getBytes("UTF-8").length));
              con.setRequestMethod("POST");
              con.setSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
  
              OutputStreamWriter wr = null;
              wr = new OutputStreamWriter(con.getOutputStream());
              wr.write(auth.toString());
              wr.flush();
  
              StringBuilder sb = new StringBuilder();
              int HttpResult = 0;
  
              HttpResult = con.getResponseCode();
  
              if (HttpResult == HttpURLConnection.HTTP_OK) {
                  BufferedReader br = new BufferedReader(
                          new InputStreamReader(con.getInputStream(), "utf-8"));
                  String line = null;
                  while ((line = br.readLine()) != null) {
                      sb.append(line + "\n");
                  }
                  br.close();
                  Log.d(TAG, "First log");
                  Log.d(TAG, "" + sb.toString());
              } else {
                  Log.d(TAG, "response code : " + HttpResult);
                  Log.d(TAG, "Http not ok");
                  Log.d(TAG, con.getResponseMessage());
              }
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
  }
  </code></pre>
  
  <p>I don't understand why my first device can validate the signature and the other not.
  I tryed to sync the date and time as explained here: <a href="https://stackoverflow.com/questions/7588082/could-not-validate-certificate-signature">Could not validate certificate signature?</a> </p>
  
  <p>A possible explanation of my problem here but i don't get it <a href="https://groups.google.com/forum/#!topic/android-security-discuss/C_cm3k9SdaM" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/android-security-discuss/C_cm3k9SdaM</a></p>
  
  <p>Does someone encountered the same problem and fixed it ?</p>
  
  <p><strong>UPDT :</strong>
  Both of the methods presented by Alex and Anton are not working with my device. 
  Using Google play service says that provider is up to date. And I still get no signature validation when enabling TLS 1.1 and 1.2 with the method described here : <a href="https://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/" rel="nofollow noreferrer">https://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/</a></p>
  
  <p><strong>UPDT 2:</strong>
  Following this link, the server should use TLS 1.1 : <a href="https://www.ssllabs.com" rel="nofollow noreferrer">https://www.ssllabs.com</a>
  <a href="https://i.stack.imgur.com/kWxNx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kWxNx.png" alt="TLS enabled"></a></p>