为多个主机和自签名证书自定义 HostNameVerifier
Customize HostNameVerifier for multiple hosts and self-signed certificate
我有一个问题,需要 community.Now 的帮助以更改 Google data protection 我在 Google 开发者控制台中收到警告。
Security warning
Your app uses an unsafe implementation of HostnameVerifier.
For more information, including the deadline for resolving the vulnerability, please see this article in Google Help.
Applies to APK version 3.
之所以没有正确使用HostnameVerifier
是因为我们公司无法提供签名
将 CA 证书发给购买我们产品的每一位客户。 (这取决于他的目的,如果他愿意的话)。
我们的产品包含安全网络服务 (Restful API) 以与应用程序通信。
所以在每次安装时都会生成新的自签名证书,我应该通过应用程序接受它,显然最简单的解决方案是通过以下方式禁用 HostnameVerifier
:
private final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
并信任所有主机:
private static void trustAllHosts() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
谁有类似的加密经验?我应该如何进一步处理我的情况?提前致谢!
听说过 Let's Encrypt CA 吗?
如果没有,请查看。
因此您的说法“我们公司无法向我们的每位客户提供签名的 CA 证书”是错误的。
您只需将 Let's Encrypt 客户端集成到您的产品中,所有服务器都将免费获得有效证书。
无论如何,即使没有有效的 CA,也有一些简单的措施可以使 SSL 连接安全,即使使用自签名证书也是如此。结果将不如真正的 SSL 证书,但比您的 "we disable the security of SSL/TLS" 方法好得多:
"Trust on first use"(类似于 SSH 的工作方式)。在第一次连接时(假设在受信任的环境中)保存服务器提供的 SHA-256 哈希(或证书本身)并保存它。下次建立连接并执行 TrustManager 的 checkServerTrusted
时,您将叶证书与保存的证书进行比较。
我有一个问题,需要 community.Now 的帮助以更改 Google data protection 我在 Google 开发者控制台中收到警告。
Security warning Your app uses an unsafe implementation of HostnameVerifier.
For more information, including the deadline for resolving the vulnerability, please see this article in Google Help. Applies to APK version 3.
之所以没有正确使用HostnameVerifier
是因为我们公司无法提供签名
将 CA 证书发给购买我们产品的每一位客户。 (这取决于他的目的,如果他愿意的话)。
我们的产品包含安全网络服务 (Restful API) 以与应用程序通信。
所以在每次安装时都会生成新的自签名证书,我应该通过应用程序接受它,显然最简单的解决方案是通过以下方式禁用 HostnameVerifier
:
private final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
并信任所有主机:
private static void trustAllHosts() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
谁有类似的加密经验?我应该如何进一步处理我的情况?提前致谢!
听说过 Let's Encrypt CA 吗? 如果没有,请查看。
因此您的说法“我们公司无法向我们的每位客户提供签名的 CA 证书”是错误的。 您只需将 Let's Encrypt 客户端集成到您的产品中,所有服务器都将免费获得有效证书。
无论如何,即使没有有效的 CA,也有一些简单的措施可以使 SSL 连接安全,即使使用自签名证书也是如此。结果将不如真正的 SSL 证书,但比您的 "we disable the security of SSL/TLS" 方法好得多:
"Trust on first use"(类似于 SSH 的工作方式)。在第一次连接时(假设在受信任的环境中)保存服务器提供的 SHA-256 哈希(或证书本身)并保存它。下次建立连接并执行 TrustManager 的 checkServerTrusted
时,您将叶证书与保存的证书进行比较。