gpt4 book ai didi

运行时下载的自签名证书的 Android N 网络安全配置

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:19 30 4
gpt4 key购买 nike

在用于 SSL 证书的 Android N 中,我必须添加此代码-(根据给定的 android developer link )

 <?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config"
... >
...
</application>

xml 文件夹中的文件 network_security_config.xml-

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">example.com</domain>
<trust-anchors>
<certificates src="@raw/my_ca"/>
</trust-anchors>
</domain-config>

这对于一个静态域来说工作正常,但我的问题是 - 服务器域在我的应用程序中每次都不会相同。第二个问题是我在运行时从我的服务器域下载 SSL 证书,所以我怎么能每次都更新原始文件夹中的证书文件,因为我们知道我们不能在运行时在原始文件夹中写入文件。

所以对于动态流程,我如何使用不同的不同证书连接到我在 Android N 中的服务器。

Edit1:如果我没有使用这个配置文件代码,我的应用程序通信就会从服务器停止,所以无论如何我都需要使用这个代码。

编辑2:这是我的代码,我在其中连接到我的服务器,它给我响应 200 表示正常,而无需更改 android N(旧代码)的代码。

  CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");

InputStream caInput = new BufferedInputStream(new FileInputStream(certFile));
X509Certificate ca =(X509Certificate) cf.generateCertificate(caInput);
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
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 {
}
}};
// Create an SSLContext that uses our TrustManager
HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, trustAllCerts, new java.security.SecureRandom());

// Tell the URLConnection to use a SocketFactory from our SSLContext
url = new URL(wsdlUrl);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(sslcontext.getSocketFactory());
urlConnection.setConnectTimeout(5000);
urlConnection.connect();

if (urlConnection.getResponseCode() == 200) { //Successful response.
result = true;
} else {
result = false;
}

但是在 200 响应时我调用我的 SOAP 服务方法到那个服务器,在这种情况下我得到异常

 com.neurospeech.wsclient.SoapFaultException: Server Error 
at com.neurospeech.wsclient.SoapWebService.postXML(SoapWebService.java:225)
at com.neurospeech.wsclient.SoapWebService.getSoapResponse(SoapWebService.java:157)
at com.vxlsoftware.fudmagent.serviceclasses.AndroidServiceAsync.access$1300(AndroidServiceAsync.java:6)
at com.vxlsoftware.fudmagent.serviceclasses.AndroidServiceAsync$setAndroidClient HeartbitRequest.executeRequest(AndroidServiceAsync.java:367)
at com.neurospeech.wsclient.ServiceRequest.run(ServiceRequest.java:20)
at java.lang.Thread.run(Thread.java:761)

意味着我的连接 url 代码在 Android N 中也能正常工作,但 SOAP 服务给了我异常。

我想我没有很好地定义我的问题,但请尝试过的人给我任何线索。

最佳答案

网络安全配置不支持动态定义的信任 anchor 。因此,您需要使用通常的方法创建一个信任自定义信任 anchor 的自定义 X509TrustManager,从中初始化 SSLContext,从中获取 SSLSocketFactory 或 SSLEngine,并将其传递到您的 HTTPS 堆栈中。您必须小心,只为与特定主机的连接信任此类自定义信任 anchor 。否则,拥有与自定义信任 anchor 对应的私钥的实体可以对来自您应用的所有网络流量进行中间人攻击。

有关示例,请参阅 https://developer.android.com/training/articles/security-ssl.html#UnknownCa .我建议您修改示例代码以禁用 HttpsURLConnection 上的重定向。

关于运行时下载的自签名证书的 Android N 网络安全配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39246848/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com