gpt4 book ai didi

android - 检查Android套接字连接使用的ssl证书

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:26 25 4
gpt4 key购买 nike

我正在检查建立 ssl 连接的 Android 代码。函数 createSSLSocketFactory 有问题。它加载一个 keystore 。我不知道ssl连接是用来信任服务器证书还是发送客户端证书。

请帮我查一下并给我一些解释。

这里是代码:

private DefaultHttpClient createHttpClient() {
DefaultHttpClient ret = new DefaultHttpClient(){
/* (non-Javadoc)
* @see org.apache.http.impl.client.DefaultHttpClient#createClientConnectionManager()
*/
@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
try {
URI uri;
uri = new URI(getURI());
if (Http.SCHEME_HTTP.equals(uri.getScheme())) {
registry.register(new Scheme(Http.SCHEME_HTTP, PlainSocketFactory.getSocketFactory(), 80));
} else if (Http.SCHEME_HTTPS.equals(uri.getScheme())) {
registry.register(new Scheme(Http.SCHEME_HTTPS, createSSLSocketFactory(), 443));
}
} catch (URISyntaxException e) {
LogUtils.e(e.getMessage(), e);
}
return new SingleClientConnManager(getParams(), registry);
}
private SSLSocketFactory createSSLSocketFactory() {

final Resources resources = mContext.getResources();
final InputStream in = resources.openRawResource(R.raw.ippaps);

ApplicationManeger apm = ApplicationManeger.getInstance();
final char[] passwdchars = apm.getProperty(
PropertiesConstants.SSL_PASSWORD).toCharArray();

SSLSocketFactory socketFactory = null;
try {
KeyStore keyStore = KeyStore.getInstance("BKS");
try {
keyStore.load(in, passwdchars);
} finally {
in.close();
}
socketFactory = new SSLSocketFactory(keyStore);
socketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
} catch (Exception e) {
LogUtils.d(e.getMessage(), e);
}
return socketFactory;
}
};

return ret;
}

谢谢!!!

最佳答案

即使 android javadoc不是很清楚,当使用单个 KeyStore 类型参数创建 SSLSocketFactory 时,此 keystore 用作信任库。这意味着 keystore 将用于验证服务器证书,但如果服务器需要客户端验证,连接将失败。

关于android - 检查Android套接字连接使用的ssl证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20649888/

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