gpt4 book ai didi

Android 2.2 SSL 库错误

转载 作者:行者123 更新时间:2023-11-29 14:03:24 25 4
gpt4 key购买 nike

首先,我知道有很多关于此的主题。他们中的大多数默认情况下都信任所有证书/主机,考虑到环境和安全性是完全不可能的。

在 2.3 中,我有一个功能齐全的 SSL 客户端,但是当我尝试在 2.2 上运行它时,我得到了一个读取错误:SSL 库失败,然后下一行显示 (openssl_v3) unkown ca。

问题是我已经逐字逐句地关注了 Crazybob 和 Antoine 的博客好几次都没有影响(这就是我在 2.3 上工作的方式)

我工作的组织是我们自己的 CA,我连接的服务器端口被限制为只允许授权的客户端证书,我在 keystore 中有一个。 2.3 版本有 2 个 BKS 商店,一个带有客户端证书,另一个带有特定服务器的证书。

我也尝试将 RootCA 和 Trusted Authority 证书放入服务器存储中,但它仍然有同样的错误,所以除非它们以准确的顺序进入,否则我很难过。

public class WebService 
{
Context context;
InputStream serverin;
InputStream clientin;
DefaultHttpClient httpClient;

public WebService(Context context, InputStream serverin, InputStream clientin)
{
this.context = context;
this.serverin = serverin;
this.clientin = clientin;
this.httpClient = newConnection();
}

public DefaultHttpClient newConnection() {
// Set basic data
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams.setUserAgent(params, "Android app/1.0.0");
// Make pool

ConnPerRoute connPerRoute = new ConnPerRouteBean(12);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
ConnManagerParams.setMaxTotalConnections(params, 20);
// Set timeout
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
HttpConnectionParams.setSoTimeout(params, 20 * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);

// Some client params
HttpClientParams.setRedirecting(params, false);
// Register http/s shemas!
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
schReg.register(new Scheme("https", newSSLSocketFactory(), 3400));

ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
params, schReg);
DefaultHttpClient sClient = new DefaultHttpClient(conMgr, params);
return sClient;
}

private SSLSocketFactory newSSLSocketFactory() {
try {
// setup truststore to provide trust for the server certificate
// load truststore certificate

KeyStore trustStore = null;
trustStore = KeyStore.getInstance("BKS");
trustStore.load(serverin, "(not telling)".toCharArray());

System.out.println("Loaded server certificates: "
+ trustStore.size());

// initialize trust manager factory with the read truststore
TrustManagerFactory trustManagerFactory = null;
trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);

// setup client certificate
// load client certificate
KeyStore keyStore = null;
keyStore = KeyStore.getInstance("BKS");
keyStore.load(clientin, "(not telling)".toCharArray());
System.out
.println("Loaded client certificates: " + keyStore.size());
// initialize key manager factory with the read client certificate

KeyManagerFactory keyManagerFactory = null;
keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory
.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "(not telling)".toCharArray());

// initialize SSLSocketFactory to use the certificates
SSLSocketFactory socketFactory = null;
socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS,
keyStore, "(not telling)", trustStore, null, null);

return socketFactory;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

Stacktrace 如下所示:

12-06 09:13:47.739: W/System.err(280): java.io.IOException: Read error: Failure in SSL library, usually a protocol error
12-06 09:13:47.799: W/System.err(280): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativeread(Native Method)
12-06 09:13:47.810: W/System.err(280): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.access$300(OpenSSLSocketImpl.java:55)
12-06 09:13:47.810: W/System.err(280): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:542)
12-06 09:13:47.819: W/System.err(280): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
12-06 09:13:47.819: W/System.err(280): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
12-06 09:13:47.819: W/System.err(280): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
12-06 09:13:47.819: W/System.err(280): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
12-06 09:13:47.829: W/System.err(280): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
12-06 09:13:47.829: W/System.err(280): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
12-06 09:13:47.829: W/System.err(280): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
12-06 09:13:47.829: W/System.err(280): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
12-06 09:13:47.829: W/System.err(280): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
12-06 09:13:47.829: W/System.err(280): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
12-06 09:13:47.829: W/System.err(280): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-06 09:13:47.829: W/System.err(280): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-06 09:13:47.829: W/System.err(280): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-06 09:13:47.829: W/System.err(280): at (my package).WebService.webGet(WebService.java:75)
12-06 09:13:47.829: W/System.err(280): at (my package).HardwareHoundActivity.onCreate(HardwareHoundActivity.java:107)
12-06 09:13:47.829: W/System.err(280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-06 09:13:47.829: W/System.err(280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-06 09:13:47.829: W/System.err(280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-06 09:13:47.829: W/System.err(280): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-06 09:13:47.829: W/System.err(280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-06 09:13:47.829: W/System.err(280): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 09:13:47.829: W/System.err(280): at android.os.Looper.loop(Looper.java:123)
12-06 09:13:47.839: W/System.err(280): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-06 09:13:47.839: W/System.err(280): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 09:13:47.839: W/System.err(280): at java.lang.reflect.Method.invoke(Method.java:521)
12-06 09:13:47.839: W/System.err(280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-06 09:13:47.839: W/System.err(280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-06 09:13:47.839: W/System.err(280): at dalvik.system.NativeStart.main(Native Method)

它在 WebService 中引用的行是:

HttpResponse response = httpClient.execute(httpGet);

最佳答案

看起来像是 native OpenSSL 级别的错误。你能发布完整的堆栈跟踪吗?您的代码看起来正确,我认为您不需要 KeyManagerFactory 位,如果您通过 keystore (按原样),httpclient 的 SSLSocketFactory 会处理此问题。

关于Android 2.2 SSL 库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8396548/

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