title: Android app SSL issue when trusting self signed certificate
body: <p>My app needs to connect to my own server, so I added my server's self signed certificate to the KeyStore. It works very well with my server but the issue is that now my app won't accept all other certificates! For example if I try to connect to <a href="https://maps.googleapis.com/" rel="nofollow">https://maps.googleapis.com/</a> I get a missing certificate exception. How could I fix this? </p>
  
  <p>Here is how I trust my certificate:</p>
  
  <pre><code>public static void setSelfSignedCertSSLContext(AssetManager assets)
          throws Exception {
      // Load self-signed cert from an InputStream
      CertificateFactory cf = CertificateFactory.getInstance("X.509");
      InputStream caInput = assets.open("selfSigned.cer");
      Certificate ca;
      try {
          ca = cf.generateCertificate(caInput);
          Log.d(LOG_TAG, "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);
  
      KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory
              .getDefaultAlgorithm());
      kmf.init(keyStore, "changeit".toCharArray());
  
      // Create an SSLContext that uses our TrustManager
      SSLContext context = SSLContext.getInstance("TLS");
      SSLContext.setDefault(context);
  
      context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
      HttpsURLConnection.setDefaultSSLSocketFactory(context
              .getSocketFactory());
  
      HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
          public boolean verify(String string, SSLSession ssls) {
              return true;
          }
      });
  
      Log.d(LOG_TAG, "SSLContext set successfully");
  }
  </code></pre>
  
  <p>and here is the exception I get when trying to connect to google:</p>
  
  <pre><code>06-29 23:27:59.181: E/AdapterClass(16358): javax.net.ssl.SSLHandshakeException: 
  java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
  </code></pre>