gpt4 book ai didi

android - 如果证书是从特定的自签名 CA 签名的,请检查 WebViewClient 的 onReceivedSslError() 方法

转载 作者:可可西里 更新时间:2023-11-01 18:45:24 25 4
gpt4 key购买 nike

我想覆盖 WebViewClientonReceivedSslError()。在这里,我想检查 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 资源中。我该怎么办?

最佳答案

我想你可以尝试如下:

@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...

如果成功,截图如下:

BNK's screenshot

关于android - 如果证书是从特定的自签名 CA 签名的,请检查 WebViewClient 的 onReceivedSslError() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com