title: How to fix unsafe implementation of X509TrustManager in Android app
body: <p>Google has advised that I have an unsafe implementation of the interface X509TrustManager in my Android application and need to change my code as follows:</p>
  
  <blockquote>
    <p>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.”</p>
  </blockquote>
  
  <p>How can the following code be modified to fix the above issue?</p>
  
  <pre><code>public EasySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
      super(truststore);
  
      TrustManager tm = new X509TrustManager()  {
          public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
          }
  
          public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
          }
  
          public X509Certificate[] getAcceptedIssuers() {
              return null;
          }
      };
  
      mContext.init(null, new TrustManager[] { tm }, null);
  }
  </code></pre>