私はこれを扱っており、かなり率直にMITM攻撃を許可していますo-no。ここでは、ピニングをサポートするよりクリーンなソリューションがあります。生のリソースフォルダに証明書を保存します。
注:悲しいことに、SSLErrorはgetCertificate()を呼び出すとSslCertificateを返します。 SslCertificateは無用です。公開APIは、公開鍵、発行日、期限日、発行元、発行元のみを確認することはできません。ただし、このクラスを開くと、公開されていないX509Certificateメンバー変数が表示されます。なぜこの設計決定がなされたのか?しかし、Bundleを取得するためのAPIがあり、X509 Certificateメンバー変数がそこに格納されます。証明書にはより多くの便利なメソッドがあるので、そのようにアクセスします。
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
SslCertificate sslCertificateServer = error.getCertificate();
Certificate pinnedCert = getCertificateForRawResource(R.raw.your_cert, mContext);
Certificate serverCert = convertSSLCertificateToCertificate(sslCertificateServer);
if(pinnedCert.equals(serverCert)) {
handler.proceed();
} else {
super.onReceivedSslError(view, handler, error);
}
}
public static Certificate getCertificateForRawResource(int resourceId, Context context) {
CertificateFactory cf = null;
Certificate ca = null;
Resources resources = context.getResources();
InputStream caInput = resources.openRawResource(resourceId);
try {
cf = CertificateFactory.getInstance("X.509");
ca = cf.generateCertificate(caInput);
} catch (CertificateException e) {
Log.e(TAG, "exception", e);
} finally {
try {
caInput.close();
} catch (IOException e) {
Log.e(TAG, "exception", e);
}
}
return ca;
}
public static Certificate convertSSLCertificateToCertificate(SslCertificate sslCertificate) {
CertificateFactory cf = null;
Certificate certificate = null;
Bundle bundle = sslCertificate.saveState(sslCertificate);
byte[] bytes = bundle.getByteArray("x509-certificate");
if (bytes != null) {
try {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes));
certificate = cert;
} catch (CertificateException e) {
Log.e(TAG, "exception", e);
}
}
return certificate;
}
... –
はアノン '' 'webView.setWebViewClient(新WebViewClient(){@Override ます。public void onReceivedSslError(WebViewのビュー、SslErrorHandlerハンドラ、SslErrorエラー)と同じになります受け入れてください。 { handler.proceed(); } }); '' ' – tyoc213
私が必要とするもの.. – MKY