title: Install a certificate for opening site inside a Webview?
body: <p>I have a website which I want to open in an Android Webview. The website uses a certificate emitted by <code>COMODO RSA Domain Validation Secure Server CA</code>. The problem is that I get an <code>Unkown Certificate Error</code> for devices all running a version less than (including) Android 5. </p>
  
  <p>I serched in the <a href="https://developer.android.com/training/articles/security-ssl.html" rel="noreferrer">documentation</a> and as far as I understood the problem is that the CA was created before the release of Android 5.</p>
  
  <p>I could do a <code>handler.proceed();</code> on <code>onReceivedSslError</code> but I want to keep the app secure and I think Google can reject the app on play store anyway.</p>
  
  <p>I found that I cound do something like this </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>but the problem is that my code is like so</p>
  
  <pre><code>myWebView.loadUrl("https://site.domain.com")
  </code></pre>
  
  <p>How can I install the certificate of the CA  within my Webview?</p>
  
  <p>Thanks</p>