【问题标题】:Check in the onReceivedSslError() method of a WebViewClient if a certificate is signed from a specific self-signed CA如果证书是从特定的自签名 CA 签名的,请检查 WebViewClient 的 onReceivedSslError() 方法
【发布时间】:2016-08-01 21:07:57
【问题描述】:

我想覆盖 WebViewClient 的 onReceivedSslError()。在这里,我想检查 error.getCertificate() 证书是否由自签名 CA 签名,仅在这种情况下,调用 handler.proceed()。在伪代码中:

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    SslCertificate serverCertificate = error.getCertificate();

    if (/* signed from my self-signed CA */) {
        handler.proceed();
    }
    else {
        super.onReceivedSslError(view, handler, error);
    }
}

我的 CA 的公钥保存在名为 rootca.bks 的 BouncyCastle 资源中。我该怎么办?

【问题讨论】:

    标签: android ssl-certificate self-signed webviewclient ca


    【解决方案1】:

    我认为您可以尝试以下方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        try {
            WebView webView = (WebView) findViewById(R.id.webView);
            if (webView != null) {
                // Get cert from raw resource...
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                InputStream caInput = getResources().openRawResource(R.raw.rootca); // stored at \app\src\main\res\raw
                final Certificate certificate = cf.generateCertificate(caInput);
                caInput.close();
    
                String url = "https://www.yourserver.com";
                webView.setWebViewClient(new WebViewClient() {                    
                    @Override
                    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                        // Get cert from SslError
                        SslCertificate sslCertificate = error.getCertificate();
                        Certificate cert = getX509Certificate(sslCertificate);
                        if (cert != null && certificate != null){
                            try {
                                // Reference: https://developer.android.com/reference/java/security/cert/Certificate.html#verify(java.security.PublicKey)
                                cert.verify(certificate.getPublicKey()); // Verify here...
                                handler.proceed();
                            } catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException e) {
                                super.onReceivedSslError(view, handler, error);
                                e.printStackTrace();
                            }
                        } else {
                            super.onReceivedSslError(view, handler, error);
                        }
                    }
                });
    
                webView.loadUrl(url);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    
    // credits to @Heath Borders at http://stackoverflow.com/questions/20228800/how-do-i-validate-an-android-net-http-sslcertificate-with-an-x509trustmanager
    private Certificate getX509Certificate(SslCertificate sslCertificate){
        Bundle bundle = SslCertificate.saveState(sslCertificate);
        byte[] bytes = bundle.getByteArray("x509-certificate");
        if (bytes == null) {
            return null;
        } else {
            try {
                CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
                return certFactory.generateCertificate(new ByteArrayInputStream(bytes));
            } catch (CertificateException e) {
                return null;
            }
        }
    }
    

    如果验证失败,logcat会有java.security.SignatureException: Signature was not verified...等一些信息

    如果成功,这里是截图:

    【讨论】:

    • 是的!!!该解决方案实际上有效并且对我来说看起来很安全 - 它验证 serer 证书是否确实使用 CA 密钥(给定 CA 公钥)签名。非常感谢!!! :-)
    • 我应该在这里保留什么 --> R.raw.rootca ?
    • @jeet.chanchawat 它的证书文件,例如“rootca.crt”,您可以在developer.android.com/training/articles/…找到更多信息
    • @BNK 我被签名异常卡住了。我已经更改了 cert.verify(cert.getPublicKey());只是为了确认。我仍然得到相同的签名异常。请帮忙。
    【解决方案2】:

    我认为这应该可行(SSL_IDMISMATCH 表示“主机名不匹配”)。

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        SslCertificate serverCertificate = error.getCertificate();
    
        if (error.hasError(SSL_UNTRUSTED)) {
            // Check if Cert-Domain equals the Uri-Domain
            String certDomain = serverCertificate.getIssuedTo().getCName();
            if(certDomain.equals(new URL(error.getUrl()).getHost())) {
              handler.proceed();
            }
        }
        else {
            super.onReceivedSslError(view, handler, error);
        }
    }
    

    如果“hasError()”不起作用,请尝试error.getPrimaryError() == SSL_IDMISMATCH

    检查Documentation of SslError 了解所有错误类型。

    编辑:我在我自己的自我认证服务器(它是一个 Xampp)上测试了这个函数,我得到了错误 #3。 这意味着您必须检查 error.hasError(SslError.SSL_UNTRUSTED) 以获得自签名证书。

    【讨论】:

    • 不幸的是,这并没有回答原始问题。我不想检查证书颁发机构是否不受信任,在这种情况下继续(这将使 SSL 的使用无效),但我想检查证书是否是从我知道的自签名 CA 签署的公钥。
    • 您可以添加添加支票:serverCertificate.getIssuedTo().getCName().equals(new URL(error.getUrl()).getHost())
    【解决方案3】:

    基于文档:

    您是否尝试过使用 SslCertificate 类的 getIssuedBy().getDName() 方法。此方法返回一个字符串,表示“颁发此证书的实体”。

    看这里:http://developer.android.com/reference/android/net/http/SslCertificate.html#getIssuedBy()

    那么你只需要知道自签名时返回的是哪个字符串。

    编辑:我认为如果它是自签名的,那应该返回空字符串,如果不是,它会返回实体

    问候

    【讨论】:

    • 这不是一个好的解决方案。任何人都可以“伪造”这个名字。由于我们拥有根 CA 的公钥,因此正确的解决方案是检查证书是否由它签名。这让我们回到了最初的问题......
    • 早上好!只是为了自我信息,有人怎么能设置呢?该值没有设置方法。问候
    • 无论如何,如果您更改方式条件,它可以解决您的问题,只需检查 ...getDName 是否等于您已知的验证者实体。或者我可能不了解你的问题。如果是这个问题请见谅
    • 任何人都可以创建自签名证书,因此任何人都可以创建具有特定验证者实体的证书。简而言之:您的建议不可靠,并且使 SSL 的使用无效。
    • 好的。让我们知道您如何获得它!问候
    猜你喜欢
    • 2012-05-05
    • 1970-01-01
    • 2020-07-30
    • 2016-07-08
    • 2016-05-20
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2011-04-30
    相关资源
    最近更新 更多