【问题标题】:SSLPeerUnverifiedException while creating an SSL connection on Android在 Android 上创建 SSL 连接时出现 SSLPeerUnverifiedException
【发布时间】:2012-02-01 08:30:25
【问题描述】:

我已经尝试执行此操作 2 天了,但我无法使其正常工作。

我有一个真实的证书,由 COMODO 签名,它本身由 USERTrust 签名。但是,当我尝试与我的域建立连接时,我得到的是:

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

我用来得到这个的代码是:

public void postData() {

    // Add your data

    try {

       HttpPost post = new HttpPost(new URI("https://example.com"));


        KeyStore trusted = KeyStore.getInstance("BKS");
        trusted.load(null, "".toCharArray());
        SSLSocketFactory sslf = new SSLSocketFactory(trusted);
        sslf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme ("https", sslf, 443));
        SingleClientConnManager cm = new SingleClientConnManager(post.getParams(),
                schemeRegistry);

        HttpClient client = new DefaultHttpClient(cm, post.getParams());

        // Execute HTTP Post Request
        @SuppressWarnings("unused")
        HttpResponse result = client.execute(post);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        Log.e(TAG,e.getMessage());
        Log.e(TAG,e.toString());
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e(TAG,e.getMessage());
        Log.e(TAG,e.toString());
        e.printStackTrace();
    } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
        Log.e(TAG,e.getMessage());
        Log.e(TAG,e.toString());
        e.printStackTrace();
        } catch (KeyStoreException e) {
            // TODO Auto-generated catch block
            Log.e(TAG,e.getMessage());
            Log.e(TAG,e.toString());
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            Log.e(TAG,e.getMessage());
            Log.e(TAG,e.toString());
            e.printStackTrace();
        } catch (CertificateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e(TAG,e.toString());
            Log.e(TAG,e.getMessage());
        } catch (KeyManagementException e) {
            // TODO Auto-generated catch block
            Log.e(TAG,e.getMessage());
            Log.e(TAG,e.toString());
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            // TODO Auto-generated catch block
            Log.e(TAG,e.getMessage());
            Log.e(TAG,e.toString());
            e.printStackTrace();
        }
    }

我已经尝试了我的域和https://google.com。他们都返回相同的。这是堆栈:

02-01 10:24:30.067: W/System.err(15560): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
02-01 10:24:30.088: W/System.err(15560):    at org.apache.harmony.xnet.provider.jsse.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:258)
02-01 10:24:30.098: W/System.err(15560):    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
02-01 10:24:30.098: W/System.err(15560):    at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
02-01 10:24:30.108: W/System.err(15560):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:164)
02-01 10:24:30.108: W/System.err(15560):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-01 10:24:30.128: W/System.err(15560):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-01 10:24:30.138: W/System.err(15560):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
02-01 10:24:30.148: W/System.err(15560):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-01 10:24:30.158: W/System.err(15560):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-01 10:24:30.158: W/System.err(15560):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-01 10:24:30.178: W/System.err(15560):    at com.example.Preferences.postData(Preferences.java:103)
02-01 10:24:30.178: W/System.err(15560):    at com.example.Preferences.onCreate(Preferences.java:52)
02-01 10:24:30.178: W/System.err(15560):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
02-01 10:24:30.178: W/System.err(15560):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
02-01 10:24:30.178: W/System.err(15560):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
02-01 10:24:30.178: W/System.err(15560):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
02-01 10:24:30.178: W/System.err(15560):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
02-01 10:24:30.178: W/System.err(15560):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 10:24:30.178: W/System.err(15560):    at android.os.Looper.loop(Looper.java:150)
02-01 10:24:30.178: W/System.err(15560):    at android.app.ActivityThread.main(ActivityThread.java:4385)
02-01 10:24:30.188: W/System.err(15560):    at java.lang.reflect.Method.invokeNative(Native Method)
02-01 10:24:30.188: W/System.err(15560):    at java.lang.reflect.Method.invoke(Method.java:507)
02-01 10:24:30.188: W/System.err(15560):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
02-01 10:24:30.188: W/System.err(15560):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)

此外,在查看日志时,我发现我认为是导致此问题的错误:

02-01 10:44:13.122: W/System.err(15746): Catch exception while startHandshake: javax.net.ssl.SSLHandshakeException: java.security.InvalidAlgorithmParameterException: trustAnchors.isEmpty()
02-01 10:44:13.122: W/System.err(15746): return an invalid session with invalid cipher suite of SSL_NULL_WITH_NULL_NULL

在这两天里,我阅读了大量解释如何连接到自签名 SSL 保护网站的文章。我还找到了this 问题,这是我获得代码的地方,但我一生都无法弄清楚using the default Android verification mechanism 的含义以及如何实现它。

有人可以提供一段代码来修复或实现连接到 REAL 证书吗?

谢谢!

【问题讨论】:

    标签: android ssl https


    【解决方案1】:

    我做错了什么,没有将 TrustManager 提供给 SSLContextFactory。对my blog 的更全面解释。太长了,不能在这里发帖。

    【讨论】:

    • 我检查了您的博客,您最终信任所有证书,因此您删除了任何安全性。您的代码基于此权利吗? stackoverflow.com/a/4837230/891479
    • 确实如此,而且它确实消除了所有的安全性。我将在其中添加一条通知,警告不要在生产中使用它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多