在Android上创建SSL连接时出现SSLPeerUnverifiedException [英] SSLPeerUnverifiedException while creating an SSL connection on Android

查看:166
本文介绍了在Android上创建SSL连接时出现SSLPeerUnverifiedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图这样做2天了,而我却无法做到这一点。

I have been trying to do this for 2 days now, and I just can't get this working.

我有一份真正的证书,由COMODO签署,本身由USERTrust签署。但是当我尝试建立与我的域的连接时,这就是我得到的:

I have a real certificate, signed by COMODO, which itself is signed by USERTrust. But when I try to make a connection to my domain, this is what I get:

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

我用来获取此代码的代码是:

The code I'm using to get this is:

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 。他们都返回相同。这是堆栈:

I have tried both my domain, and https://google.com. They both return the same. And here is the stack:

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)

此外,在查看日志时,我发现,我认为是错误的造成这种情况:

Also, while going over the logs, I found, what I believe to be the error that is causing this:

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验证机制是什么意思以及如何实现它。

During these 2 days I have read TONS of articles explaining how to connect to a Self-signed SSL protected website. I have also found this question, which is where I got the code, but I cant for the life of me figure out what the using the default Android verification mechanism means and how to implement it.

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

Could someone provide a piece of code which fixes, or implements connecting to a REAL certificate?

谢谢!

推荐答案

我做错了什么,没有将TrustManager提供给SSLContextFactory。关于我的博客的更全面的解释。在这里发帖太久了。

What I was doing wrong, was not giving the TrustManager to SSLContextFactory. A fuller explanation on my blog. Too long to post here.

这篇关于在Android上创建SSL连接时出现SSLPeerUnverifiedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆