Check in the onReceivedSslError() method of a WebViewClient if a certificate is signed from a specific self-signed CA

后端 未结 3 794
逝去的感伤
逝去的感伤 2020-12-08 16:06

I would like to override the onReceivedSslError() of a WebViewClient. Here I want to check if the error.getCertificate() certificate i

3条回答
  •  青春惊慌失措
    2020-12-08 16:28

    I think this should work (SSL_IDMISMATCH means "Hostname mismatch").

    @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);
        }
    }
    

    If "hasError()" is not working, try error.getPrimaryError() == SSL_IDMISMATCH

    Check Documentation of SslError for all error-types.

    EDIT: I tested the function on my own self-cert server (its a Xampp), and I got Error #3. That means you have to check for error.hasError(SslError.SSL_UNTRUSTED) for a self-signed cert.

提交回复
热议问题