繁体   English   中英

具有自签名证书的Android Https连接:hostName未验证

[英]Android Https Connection with self signed certificate : hostName not verified

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

我已经从android开发人员网站上获取了此代码,以使用我的证书通过https连接(我不想信任我只想使用原始文件夹中的证书来验证证书是否正确的所有内容)。

因此,当我连接时,我得到: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的Samsung 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)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM