Android 2.2 SSL Library错误

时间:2011-12-06 07:23:19

标签: android ssl android-2.2-froyo

首先,我知道有很多主题。其中大部分是anwser默认信任所有证书/主机,因为环境和安全性是完全不可能的。

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

事情是,我已经多次跟随Crazybob和Antoine的博客,没有任何影响(这就是我如何让它在2.3上工作)

我工作的组织是我们自己的CA,我连接的服务器端口仅限于允许我在密钥库中拥有的授权客户端证书。 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);

1 个答案:

答案 0 :(得分:0)

在本机OpenSSL级别看起来像是一个错误。你可以发布完整的堆栈跟踪吗?你的代码看起来是正确的,我认为你不需要KeyManagerFactory位,httpclient的SSLSocketFactory会在你通过密钥库时照顾这个(就像你一样)。