【发布时间】:2017-07-05 09:57:30
【问题描述】:
我们正在开发一个具有安全意识的应用程序,我们需要该应用程序向我们发送用于连接的证书。
是否可以下载证书并在 Android 上发送。我们需要这样做,以便我们可以检查证书是否已被泄露。或者最好从证书中检索详细信息,将其写入日志然后发送。我们有一个可以发送详细信息的 api
【问题讨论】:
标签: android ssl certificate
我们正在开发一个具有安全意识的应用程序,我们需要该应用程序向我们发送用于连接的证书。
是否可以下载证书并在 Android 上发送。我们需要这样做,以便我们可以检查证书是否已被泄露。或者最好从证书中检索详细信息,将其写入日志然后发送。我们有一个可以发送详细信息的 api
【问题讨论】:
标签: android ssl certificate
我看到你之前要求固定...
您可以通过以下方式固定证书
Certificate[] certs = conn.getServerCertificates();
MessageDigest md = MessageDigest.getInstance("SHA-256");
for (Certificate cert : certs) {
X509Certificate x509Certificate = (X509Certificate) cert;
byte[] key = x509Certificate.getPublicKey().getEncoded();
md.update(key, 0, key.length);
byte[] hashBytes = md.digest();
StringBuilder hexHash = new StringBuilder();
for (byte hashByte : hashBytes) {
int k = 0xFF & hashByte;
String tmp = (k < 16) ? "0" : "";
tmp += Integer.toHexString(0xFF & hashByte);
hexHash.append(tmp);
}
// You can even log cert.toString()
Log.d(TAG, hexHash.toString()); get the hash from here
// If you create `pins` as a Set with all the valid pins from above
if (pins.contains(hexHash.toString())) {
return true;
}
}
您可以在连接conn 连接后运行它,然后如果引脚不匹配,您可以在发送数据之前中止。注意:上面的代码固定的是证书的公钥而不是证书。
如果你只想要证书,你可以得到cert.toString()
【讨论】:
URL url = new URL(PATH); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); 和 connection.connect(); 在我的 android 设备(mitmproxy 和 fiddler)上使用代理时,我得到一个 javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found,但在不使用代理时却没有