以https(没有对方的证书)的Andr​​oid的问题 [英] Problems with https (No peer certificate) in android

查看:292
本文介绍了以https(没有对方的证书)的Andr​​oid的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我要发送HTTPS请求到现场 https://10.2.20.20/fido/EzPay/login.php 我自己的服务器,并从它那里得到响应并保存,例如在一个字符串。我已经发现了一些例如C $ CS $在互联网和尝试,以测试他们对我的问题,但他们没有帮助。下面我present的codeS一些地方,我已经测试。


code例:

我尝试这个code,但我总是得到相同的异常没有对方的证书为什么?

 尝试
{
    的HostnameVerifier的HostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

    DefaultHttpClient客户端=新DefaultHttpClient();

    SchemeRegistry注册表=新SchemeRegistry();
    SSLSocketFactory的的SocketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier)的HostnameVerifier);
    registry.register(新计划(https开头,的SocketFactory,443));
    SingleClientConnManager经理=新SingleClientConnManager(client.getParams(),注册);
    DefaultHttpClient的HttpClient =新DefaultHttpClient(经理,client.getParams());

    //设置验证
    HttpsURLConnection.setDefaultHostnameVerifier(的HostnameVerifier);

    //例发送HTTP请求
    最终的字符串URL =htt​​ps://10.2.20.20/fido/EzPay/login.php;
    HttpPost httpPost =新HttpPost(URL);
    HTT presponse响应= httpClient.execute(httpPost);

    的BufferedReader RD =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));
    串线=;
    而((行= rd.readLine())!= NULL){
        Log.i(DownloadImageTask.class.getName(),线);
    }

}
赶上(IOException异常前)
{
    Log.e(DownloadImageTask.class.getName(),ex.getMessage());
}
 

例外。

  

03-02 16:58:25.234:W / System.err的(1868年):   javax.net.ssl​​.SSLPeerUnverifiedException:没有对方的证书03-02   16:58:25.238:W / System.err的(1868年):在   org.apache.harmony.xnet.provider.jsse.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:137)   03-02 16:58:25.238:W / System.err的(1868年):在   org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)   03-02 16:58:25.238:W / System.err的(1868年):在   org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)   03-02 16:58:25.238:W / System.err的(1868年):在   org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)   03-02 16:58:25.250:W / System.err的(1868年):在   org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)   03-02 16:58:25.250:W / System.err的(1868年):在   org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)   03-02 16:58:25.250:W / System.err的(1868年):在   org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)   03-02 16:58:25.250:W / System.err的(1868年):在   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)   03-02 16:58:25.250:W / System.err的(1868年):在   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)   03-02 16:58:25.250:W / System.err的(1868年):在   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)   03-02 16:58:25.250:W / System.err的(1868年):在   com.https.test.DownloadImageTask.doInBackground(Https_testActivity.java:78)   03-02 16:58:25.250:W / System.err的(1868年):在   com.https.test.DownloadImageTask.doInBackground(Https_testActivity.java:1)   03-02 16:58:25.250:W / System.err的(1868年):在   android.os.AsyncTask $ 2.call(AsyncTask.java:264)03-02 16:58:25.253:   W / System.err的(1868年):在   java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)   03-02 16:58:25.253:W / System.err的(1868年):在   java.util.concurrent.FutureTask.run(FutureTask.java:137)03-02   16:58:25.253:W / System.err的(1868年):在   android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:208)03-02   16:58:25.257:W / System.err的(1868年):在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)   03-02 16:58:25.257:W / System.err的(1868年):在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)   03-02 16:58:25.257:W / System.err的(1868年):在   java.lang.Thread.run(Thread.java:856)


问题

我在做什么错了,我怎么能解决这个问题。为什么我得到的没有对方的证书例外呢?

感谢。

编辑


在Windows Server的设置。

 <虚拟主机*:443>
  服务器名10.2.20.20

 别名/菲/ EzPay /D:/菲/ EzPay /
别名/菲/ EzPayD:/菲/ EzPay /

<目录D:/菲/ EzPay />
    选择了FollowSymLinks指标多视图
    所有的AllowOverride
        订购允许,拒绝
    所有允许
< /目录>


  #这是需要得到它的所有工作实际的SSL指令!
  的SSLEngine上
  了SSLCertificateFile C:/wamp/bin/apache/apache2.2.17/conf/ssl/fidoserver.crt
  了SSLCertificateKeyFile C:/wamp/bin/apache/apache2.2.17/conf/ssl/fidoserver.pem
< /虚拟主机>
 

解决方案

最后,我已经解决了HTTPS的问题。当我打的主要问题是在服务器上,具体的证明。 Android支持只有BKS证书,这是我们无法得到回应的原因 服务器。为了解决这个问题,我看了30多篇,并最终找到解决方案。

这是我做的来解决这个问题,你可以看到下面的步骤:

第一件事,我要做的就是生成.bks密钥库文件从我们fidoserver.crt证书,为了做到这一点我已经阅读这篇文章,做以下操作:

  1. 开启CMD
  2. 转到JDK文件夹CD X:\程序\的Java \ JDK6 \ BIN
  3. 呼叫以下命令:
  

密钥工具-import -alias tomcat的-file X://KeyStore/fidoserver.crt   -keypass密码 - 密钥库X://KeyStore/keystore.bks -storetype BKS -storepass 222222 -providerClass   org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath   X://KeyStore/bcprov-jdk16-146.jar

在运行此命令之前,我有下载充气城堡 .jar文件,并把它与证书的文件夹中。这样做的所有步骤后,我得到keystore.bks文件,该文件是Android应用程序的权利证明文件。我把这个文件中的机器人MNC / SD卡的文件夹。在java中code我有写操作之后code来读取keystore.bbk文件

 密钥库的trustStore = KeyStore.getInstance(BKS/*KeyStore.getDefaultType()*/);
的FileInputStream河道=新的FileInputStream(新文件(到/ mnt / SD卡/ keystore.bks));
尝试 {
    trustStore.load(插播,222222.toCharArray());
}赶上(抛出:NoSuchAlgorithmException E){
    e.printStackTrace();
}赶上(CertificateException E){
    e.printStackTrace();
}赶上(IOException异常E){
    e.printStackTrace();
} 最后 {
    尝试{instream.close(); }赶上(异常忽略){}
}

//创建套接字工厂定的密钥存储。
SSLSocketFactory的的SocketFactory =新的SSLSocketFactory(的trustStore);

SSLSocketFactory的的SocketFactory =新的SSLSocketFactory(的trustStore);
计划SCH =新计划(https开头,的SocketFactory,443);
。httpclient.getConnectionManager()getSchemeRegistry()寄存器(SCH);

HTTPGET HTTPGET =新HTTPGET(https://10.2.20.20/fido/EzPay/login.php);

的System.out.println(执行请求+ httpget.getRequestLine());

HTT presponse响应= httpclient.execute(HTTPGET);
HttpEntity实体= response.getEntity();

的System.out.println(----------------------------------------);
的System.out.println(response.getStatusLine());
如果(实体!= NULL){
    的System.out.println(响应内容长度:+ entity.getContentLength());
}

//打印HTML。
的BufferedReader在=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));
串线=;
而((行= in.readLine())!= NULL){
     的System.out.println(线);
}
附寄();
 

这一切都让米至与给定的密码222222(密码我们给当创建密钥工具密钥库)载入我们的证书。

这都是我的测试应用程序后开始正常工作。现在,我可以发送请求,HTTPS和从它那里得到回应。我已经测试 应用程序FIDO服务器,一切都很正常!我想,在周一我会在EzPay应用程序,它的一些变化 将启动HTTPS连接工作。

参考

Problem

I want to send https request to the site https://10.2.20.20/fido/EzPay/login.php my own server and get response from it and save it for example in a string. I have found some example codes in internet and try to test them for my problem but they are not helpful. Below I present some parts of codes which I have tested.


Code Example:

I try this code, but I always get same exception "No peer certificate" Why ?

try
{
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

    DefaultHttpClient client = new DefaultHttpClient();

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
    DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

    // Set verifier      
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

    // Example send http request
    final String url = "https://10.2.20.20/fido/EzPay/login.php";
    HttpPost httpPost = new HttpPost(url);
    HttpResponse response = httpClient.execute(httpPost);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String line = "";
    while ((line = rd.readLine()) != null) {
        Log.i(DownloadImageTask.class.getName(), line);
    }

}
catch(IOException ex)
{
    Log.e(DownloadImageTask.class.getName(), ex.getMessage());
}

Exception.

03-02 16:58:25.234: W/System.err(1868): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate 03-02 16:58:25.238: W/System.err(1868): at org.apache.harmony.xnet.provider.jsse.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:137) 03-02 16:58:25.238: W/System.err(1868): at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93) 03-02 16:58:25.238: W/System.err(1868): at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381) 03-02 16:58:25.238: W/System.err(1868): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165) 03-02 16:58:25.250: W/System.err(1868): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 03-02 16:58:25.250: W/System.err(1868): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 03-02 16:58:25.250: W/System.err(1868): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 03-02 16:58:25.250: W/System.err(1868): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 03-02 16:58:25.250: W/System.err(1868): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 03-02 16:58:25.250: W/System.err(1868): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 03-02 16:58:25.250: W/System.err(1868): at com.https.test.DownloadImageTask.doInBackground(Https_testActivity.java:78) 03-02 16:58:25.250: W/System.err(1868): at com.https.test.DownloadImageTask.doInBackground(Https_testActivity.java:1) 03-02 16:58:25.250: W/System.err(1868): at android.os.AsyncTask$2.call(AsyncTask.java:264) 03-02 16:58:25.253: W/System.err(1868): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 03-02 16:58:25.253: W/System.err(1868): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 03-02 16:58:25.253: W/System.err(1868): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 03-02 16:58:25.257: W/System.err(1868): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 03-02 16:58:25.257: W/System.err(1868): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 03-02 16:58:25.257: W/System.err(1868): at java.lang.Thread.run(Thread.java:856)


Question

What I'm doing wrong and how I can solve this issue. Why I get "No peer certificate" exception ?

Thanks.

Edited


Windows Server settings.

<VirtualHost *:443>
  ServerName 10.2.20.20

 Alias /fido/EzPay/ "d:/fido/EzPay/" 
Alias /fido/EzPay "d:/fido/EzPay/" 

<Directory "d:/fido/EzPay/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order allow,deny
    Allow from all
</Directory>


  # These are the actual SSL directives needed to get it all working!
  SSLEngine on
  SSLCertificateFile C:/wamp/bin/apache/apache2.2.17/conf/ssl/fidoserver.crt
  SSLCertificateKeyFile C:/wamp/bin/apache/apache2.2.17/conf/ssl/fidoserver.pem
</VirtualHost>

解决方案

Finally I have solved https problem. As I fought the main problem was in server, concretely in certificate. Android supports only "BKS" certificate and that’s was the reason that we can’t get response from the server. In order to solve this issue I have read more then 30 articles and finally found solution.

The steps which I done to solve this issue you can see below:

First thing that I do was generating .bks keystore file from our fidoserver.crt certificate, in order to do that I have read this article and do following:

  1. Open cmd
  2. Go to JDK folder "cd X:\Programs\Java\Jdk6\bin"
  3. Call following command:

keytool -import -alias tomcat -file X://KeyStore/fidoserver.crt -keypass password - keystore X://KeyStore/keystore.bks -storetype BKS -storepass 222222 -providerClass org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath X://KeyStore/bcprov-jdk16-146.jar

Before running this command I have download Bouncy Castle .jar file and put it in the folder with certificates. After doing this all steps I get keystore.bks file which is the right certificate file for Android application. I put this file in Androids mnc/sdcard folder. In java code I have write following code to read that keystore.bbk file

KeyStore trustStore  = KeyStore.getInstance( "BKS" /*KeyStore.getDefaultType()*/ );
FileInputStream instream = new FileInputStream(new File("/mnt/sdcard/keystore.bks"));
try {
    trustStore.load(instream, "222222".toCharArray());
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
} catch (CertificateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try { instream.close(); } catch (Exception ignore) {}
}

// Create socket factory with given keystore.
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);

HttpGet httpget = new HttpGet("https://10.2.20.20/fido/EzPay/login.php");

System.out.println("executing request " + httpget.getRequestLine());

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
    System.out.println("Response content length:  " + entity.getContentLength());
}

// Print html.
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = in.readLine()) != null) {
     System.out.println(line);
}
in.close();

This all allow m to load our certificate with given password 222222 (password we give when create a keystore with keytool).

After this all my test application start to work correctly. Now I can send request to https and get response from it. I have tested application with FIDO server, everything works great! I think on Monday I will make some changes in EzPay application and it will start working with https connections.

References

这篇关于以https(没有对方的证书)的Andr​​oid的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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