在 Android 上创建 SSL 连接时出现 SSLPeerUnverifiedException 错误

4

我已经尝试了两天,但是我仍然无法让它正常工作。

我拥有一个真实的证书,由COMODO签名,而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保护的网站。我还发现了这个问题,我从中获取了代码,但我无法想象“使用默认的Android验证机制”是什么意思以及如何实施它。
有人能提供一段代码来修复或实现连接到真正的证书吗?
谢谢!
1个回答

2

我做错的事情是没有将TrustManager提供给SSLContextFactory。在我的博客上有更详细的解释。这里贴出来太长了。


我查看了你的博客,你最终选择信任所有证书,因此删除了任何安全性。你的代码是基于这个链接吗?https://dev59.com/b3E85IYBdhLWcg3wtV_1#4837230 - L. G.
它确实会移除所有安全性。我会添加一个提示,警告不要在生产中使用它们。 - Janis Peisenieks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接