我的Android項目(OkHttp 3.3.1)目前與我的HTTPS web服務(我的電腦,IIS Web服務器,Asp.Net的Web API,自簽名證書)OkHttp與證書鋼釘
的輔助方法:
private SSLSocketFactory getSSLSocketFactory()
throws CertificateException, KeyStoreException, IOException,
NoSuchAlgorithmException, KeyManagementException {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = getResources().openRawResource(R.raw.iis_cert);
Certificate ca = cf.generateCertificate(caInput);
caInput.close();
KeyStore keyStore = KeyStore.getInstance("BKS");
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.getSocketFactory();
}
private HostnameVerifier getHostnameVerifier() {
return new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify("BNK-PC.LOCALHOST.COM", session);
}
};
}
代碼A:
OkHttpClient client = new OkHttpClient.Builder()
.sslSocketFactory(getSSLSocketFactory())
.hostnameVerifier(getHostnameVerifier())
.build();
閱讀this CertificatePinner guide,我的工程中的後
代碼B:
OkHttpClient client = new OkHttpClient.Builder()
.sslSocketFactory(getSSLSocketFactory())
.certificatePinner(certificatePinner)
.hostnameVerifier(getHostnameVerifier())
.build();
據this Wiki,證書釘扎增加了安全性如下添加.certificatePinner(certificatePinner)
當t也是行之有效的。
然而,實際上我還沒有清楚地理解這種想法。所以我的問題是,當我的應用程序仍然與代碼A一起使用時,是否需要或必須使用certificatePinner
。換句話說,代碼B比有更好的安全性代碼A?
任何說明所理解的,由於提前。
有很多很好的文章,請[點擊這裏](http://blog.lumberlabs.com/2012/04/why-app-developers-should-care-about.html)。 –
@MadhukarHebbar爲你的鏈接非常感謝,我讀書,也許我需要閱讀很多次明白,太多的安全知識,知道什麼時候我的英語也不好:) – BNK