【问题标题】:Trust Certificate Authority in WebView loadUrl()WebView loadUrl() 中的信任证书颁发机构
【发布时间】:2016-10-31 19:23:15
【问题描述】:

根据安全扫描程序的结果,我需要限制应用信任的证书颁发机构。

扫描结果指出webView.loadUrl("https://example.com/page"); 处的行。我知道如何创建一个使用我的 TrustManager 的 SslSocketFactory,但我在 WebView 中看不到允许我设置它的 API。

https://developer.android.com/training/articles/security-ssl.html#UnknownCa

有哪些可能的方法来实现这一目标?

【问题讨论】:

  • 我会避免任何“安全扫描程序”,如果它不提供如何解决它报告的问题的详细信息。
  • @CommonsWare 报告中的“推荐”部分与上面链接中的代码完全相同,只是建议使用证书固定或自定义密钥库。没有其他信息。
  • AFAIK,WebView 都不可能。

标签: android android-webview ssl-certificate x509certificate ca


【解决方案1】:

我认为WebViewClient 的onReceivedSslError 方法将是一个很好的切入点。

首先,按照https://developer.android.com/training/articles/security-ssl.html#UnknownCa中完全相同的sn-p来准备TrustManager。

    TrustManagerFactory tmf = null;

    private void initTrustStore() throws
            java.security.cert.CertificateException, FileNotFoundException,
            IOException, KeyStoreException, NoSuchAlgorithmException {

        // Create a KeyStore containing our trusted CAs
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore trustedKeyStore = KeyStore.getInstance(keyStoreType);
        trustedKeyStore.load(null, null);

        CertificateFactory cf = CertificateFactory.getInstance("X.509");

        InputStream caInput = new BufferedInputStream(
                    getResources().getAssets().open("ca.crt"));
            Certificate ca;
            try {
                ca = cf.generateCertificate(caInput);
                Log.d(TAG, "ca-root DN=" + ((X509Certificate) ca).getSubjectDN());
            }
            finally {
                caInput.close();
            }
            trustedKeyStore.setCertificateEntry("ca", ca);

        // Create a TrustManager that trusts the CAs in our KeyStore
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trustedKeyStore);

    }

然后,扩展自定义 WebViewClient 类,检查来自 https://stackoverflow.com/a/6379434/1099884 的 sn-p

private class CheckServerTrustedWebViewClient extends WebViewClient{
    public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
        Log.d(TAG, "onReceivedSslError");
        boolean passVerify = false;

        if(error.getPrimaryError() == SslError.SSL_UNTRUSTED){
            SslCertificate cert = error.getCertificate();
            String subjectDN = cert.getIssuedTo().getDName();
            Log.d(TAG, "subjectDN: "+subjectDN);
            try{
                Field f = cert.getClass().getDeclaredField("mX509Certificate");
                f.setAccessible(true);
                X509Certificate x509 = (X509Certificate)f.get(cert);

                X509Certificate[] chain = {x509};
                for (TrustManager trustManager: tmf.getTrustManagers()) {
                    if (trustManager instanceof X509TrustManager) {
                        X509TrustManager x509TrustManager = (X509TrustManager)trustManager;
                        try{
                            x509TrustManager.checkServerTrusted(chain, "generic");
                            passVerify = true;break;
                        }catch(Exception e){
                            Log.e(TAG, "verify trustManager failed", e);
                            passVerify = false;
                        }
                    }
                }
                Log.d(TAG, "passVerify: "+passVerify);
            }catch(Exception e){
                Log.e(TAG, "verify cert fail", e);
            }
        }
        if(passVerify == true)handler.proceed();
        else handler.cancel();

    }       

}

最后,将CheckServerTrustedWebViewClient 设置为WebView

webView.setWebViewClient(new CheckServerTrustedWebViewClient());

但是,有一个问题。准备好的 CA 证书是服务器一的确切签名(中间 CA 不是根 CA)。只提供根 CA 证书是行不通的。 TrustManager 不能在运行时下载服务器证书链吗?有什么建议吗?

【讨论】:

  • Identity equality for arguments of types Int and Int is deprecated(错误!!.primaryError === SslError.SSL_UNTRUSTED)
【解决方案2】:

doc 好像更新了:

幸运的是,您可以通过配置应用程序的 Network Security Config 来让应用程序信任自定义 CA,而无需修改应用程序内部的代码。

blog post I think to be related

【讨论】:

    猜你喜欢
    • 2014-07-13
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 2014-12-26
    相关资源
    最近更新 更多