Android Https与自签名证书的连接:hostName未经验证

时间:2013-11-28 15:42:24

标签: java android ssl https ubuntu-12.04

您好我正在使用证书测试Android连接。 我在我的ubuntu桌面上创建了一个默认的ssl服务器。启用ssl并创建默认的自签名证书。然后我用firefox连接到https://localhost,将证书添加到异常,然后我使用Firefox将证书保存为.pem文件。 我在我的android projetc res / raw

中添加了certificate.pem

我从Android开发者网站获取此代码,使用我的证书通过https进行连接(我不想信任所有我只想验证使用raw文件夹中的证书验证是否正确)。

所以当我连接时,我得到: java.lang.RuntimeException:java.io.IOException:主机名'192.168.1.111'未经验证

这是我用来验证证书的类

public class VerifyKey extends AsyncTask<Void, Void, Void>{

    public static final String CERTIFICATE_TYPE_X_509 = "X.509";
    public static final String CERTIFICATE_ALIAS = "user_desktop";
    public static final String SERVER_URL = "https://192.168.1.111";
    @Override
    protected Void doInBackground(Void... params) {
        // Load CAs from an InputStream
        // (could be from a resource or ByteArrayInputStream or ...)
        CertificateFactory cf = null;
        InputStream certificateInputStream = getApplicationContext().getResources().openRawResource(R.raw.user_desktop);
        Certificate certificate = null;
        try {
            cf = CertificateFactory.getInstance(CERTIFICATE_TYPE_X_509);
            certificate = cf.generateCertificate(certificateInputStream);
            Log.d(TAG, "Certificate : " + certificate.toString());
            Log.d(TAG, "Certificate public key : " + certificate.getPublicKey());
        } catch (CertificateException e) {
            throw new RuntimeException(e);
        } finally {
            if (certificateInputStream != null) {
                try {
                    certificateInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        // Create a KeyStore containing our trusted CAs
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = null;
        try {
            keyStore = KeyStore.getInstance(keyStoreType);
            if (keyStore != null) {
                keyStore.load(null, null);
                keyStore.setCertificateEntry(CERTIFICATE_ALIAS, certificate);
            } else {
                throw new RuntimeException("KeyStore is null");
            }
        } catch (KeyStoreException e) {
            throw new RuntimeException(e);
        } catch (CertificateException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // Create a TrustManager that trusts the CAs in our KeyStore
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = null;
        try {
            tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            if (tmf != null) {
                tmf.init(keyStore);
            } else {
                throw new RuntimeException("TrustManagerFactory is null");
            }
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (KeyStoreException e) {
            throw new RuntimeException(e);
        }

        // Create an SSLContext that uses our TrustManager
        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("TLS");

            TrustManager[] trustManagers = tmf.getTrustManagers();
            sslContext.init(null, trustManagers, null);

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        }

        // Tell the URLConnection to use a SocketFactory from our SSLContext
        URL url = null;
        HttpsURLConnection httpsURLConnection =
                null;
        InputStream in = null;
        try {
            url = new URL(SERVER_URL);
            Log.d(TAG, "URL : "+url.toString());
            httpsURLConnection = (HttpsURLConnection)url.openConnection();
            SSLSocketFactory socketFactory = sslContext.getSocketFactory();
            Log.d(TAG, "Socket factory : "+socketFactory.toString());
            httpsURLConnection.setSSLSocketFactory(socketFactory);

            in = httpsURLConnection.getInputStream(); //IOException exception gets triggered here

            Toast.makeText(getApplicationContext(), in.toString(), Toast.LENGTH_SHORT).show();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (SSLHandshakeException e){
            throw new RuntimeException(e);
        } catch(UnknownHostException e){
            throw new RuntimeException(e);
        } catch (ConnectException e1){
            throw new RuntimeException(e1);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}

我从http://developer.android.com/training/articles/security-ssl.html#SelfSigned

获得了此代码

我在使用Android 4.3的三星Galaxy s4上出现此错误

我没有很多使用HTTPS的经验,所以我想在这里实现的是提供应用程序的证书,以便验证服务器证书。 如果有人可以在代码中建议我可以修改的内容。

我也有一个疑问,因为我的服务器是.local服务器,但是我使用ip连接,目标是能够使用ip和主机名连接,这是非常有效的主机名吗? 非常感谢提前

编辑:我添加了代码来获取主机名:

InetAddress addr = InetAddress.getByName(SERVER_URL);
String hostName = addr.getHostName();

我尝试使用主机名而不是ip,但我仍然得到相同的例外:

Caused by: java.io.IOException: Hostname '<user.hostname.com>' was not verified
    at libcore.net.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:223)
    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:446)
    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
    at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题,这实际上是证书的问题:

sudo a2enmod ssl
sudo rm -rf /etc/apache2/ssl
sudo mkdir /etc/apache2/ssl
sudo openssl req -new -x509 -days 365 -nodes -out /etc/apache2/ssl/apache.pem -keyout /etc/apache2/ssl/apache.key

将/etc/apache2/ssl/apache.pem复制到其他地方,将其权限更改为777(???) 然后将新的apache.pem添加到应用程序的res / raw文件夹

然后在通用名称字段中我设置了我的服务器的FQDN,例如host.name.com,然后我更新了/ etc / apache2 / sites-available / default-ssl

中的证书和密钥设置

所有这一切都归功于网站https://library.linode.com/web-servers/apache/ssl-guides/ubuntu-10.04-lucid