title: How to bypass SSL certificate validation in Android app?
body: <p>My Android application should be able to communicate to any SSL enabled servers. As my app is demo application and my customers add their own SSL server details in the app while logging in, so upfront I don't know which SSL certificate I need to validate.</p>
  
  <p>Following is my earlier code.</p>
  
  <pre><code>public SSLSocketFactory getSSLSocketFactory(String hostname) {
          try {
              HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                  public boolean verify(String hostname, SSLSession session) {
                      return true;
                  }
              });
              SSLContext context = SSLContext.getInstance("TLS");
              context.init(null, new X509TrustManager[] { new X509TrustManager() {
                  public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                  }
  
                  public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                  }
  
                  public X509Certificate[] getAcceptedIssuers() {
                      return new X509Certificate[0];
                  }
              } }, new SecureRandom());
              HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
          } catch (Exception e) {
              e.printStackTrace();
          }
          return  HttpsURLConnection.getDefaultSSLSocketFactory();
      }
  </code></pre>
  
  <p>When I updated my to playstore, it was rejected with below reason</p>
  
  <p><em>To properly handle SSL certificate validation, change your code in the checkServerTrusted method of your custom X509TrustManager interface to raise either CertificateException or IllegalArgumentException whenever the certificate presented by the server does not meet your expectations. For technical questions, you can post to Stack Overflow and use the tags “android-security” and “TrustManager."</em></p>
  
  <p>I would like to update my code similar to this</p>
  
  <pre><code>public static HttpClient wrapClient(HttpClient base) {
  try {
      SSLContext ctx = SSLContext.getInstance("TLS");
      X509TrustManager tm = new X509TrustManager() {
          public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { }
  
          public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { }
  
          public X509Certificate[] getAcceptedIssuers() {
              return null;
          }
      };
      ctx.init(null, new TrustManager[]{tm}, null);
      SSLSocketFactory ssf = new SSLSocketFactory(ctx);
      ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      ClientConnectionManager ccm = base.getConnectionManager();
      SchemeRegistry sr = ccm.getSchemeRegistry();
      sr.register(new Scheme("https", ssf, 443));
      return new DefaultHttpClient(ccm, base.getParams());
  } catch (Exception ex) {
      return null;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Does playstore accept this? is there any better way to handle this?</p>
  
  <p>Thanks in Advance.</p>