我Certificate Pining
在我的 android 应用程序中使用 SSL,现在当我更新 SSL 证书时,我想因为我只是Certificate Pining
不在Public Key Pining
我的 Android 应用程序中,所以我需要在我的 android 项目中更改证书并再次在 Play 商店中更新我的应用程序,但是问题是我的应用程序运行良好,无需在我的 Android 项目中更新证书文件。
谁能告诉我这是正常行为吗?
代码
public Certificate findCertificate() throws CertificateException {
CertificateFactory instance = CertificateFactory.getInstance("X.509");
InputStream resourceAsStream = getResources().openRawResource(R.folder.certificate_name);
InputStream bufferedInputStream = new BufferedInputStream(resourceAsStream);
try {
Certificate generateCertificate = instance.generateCertificate(bufferedInputStream);
try {
resourceAsStream.close();
} catch (IOException e) {
}
return generateCertificate;
} finally {
try {
bufferedInputStream.close();
} catch (IOException e2) {
}
try {
resourceAsStream.close();
} catch (IOException e3) {
}
}
}
public SSLContext findSSLConfiguration(Context context) throws CertificateException, IOException,
KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
CertificateFactory cf = null;
cf = CertificateFactory.getInstance("X.509");
Certificate ca = findCertificate();
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
return sslContext;
}
public void server_call() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
OkHttpClient okHttp = new OkHttpClient.Builder()
.sslSocketFactory(findSSLConfiguration(getBaseContext()).getSocketFactory())
.build();
}