Android自签名证书:未找到证书路径的信任锚

时间:2013-09-23 12:48:07

标签: android ssl certificate httpsurlconnection sslhandshakeexception

我知道这个主题在很多地方都有讨论,但在我经历了几乎所有这些之后,我决定创建我的第一个StackOverflow问题......

问题如下:

我想连接到使用证书来限制访问的安全Web服务(https),以及用于验证用户身份的用户名/密码。所以我有一个客户端证书(p12文件)和一个服务器证书(pem或der文件)。我尝试使用HttpURLConnection类,因为从我所听到的,Android上将不再支持Apache库。

所以这是我的实现(serverCert和clientCert是我文件的完整路径):

        // Load CAs from our reference to the file
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        InputStream caInput = new BufferedInputStream(new FileInputStream(serverCert));
        X509Certificate serverCertificate;

        try {
            serverCertificate = (X509Certificate)cf.generateCertificate(caInput);
            System.out.println("ca=" + serverCertificate.getSubjectDN());
        } finally {
            caInput.close();
        }
        Log.d(TAG, "Server Cert: " + serverCertificate);

        // Create a KeyStore containing our trusted CAs
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null);
        trustStore.setCertificateEntry("my ca", serverCertificate);

        //Load the Client certificate in the keystore
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        FileInputStream fis = new FileInputStream(clientCert);
        keyStore.load(fis,CLIENT_PASSWORD);

        // Create a TrustManager that trusts the CAs in our KeyStore
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(trustStore);

        //Build the SSL Context
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(keyStore, pref.getString(Constants.clientCertificatePassword, "").toCharArray

());


    //Create the SSL context
                SSLContext sslContext = SSLContext.getInstance("TLS");
                sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
...
    //And later, we use that sslContext to initiatize the socketFactory

                urlConnection = (HttpsURLConnection) requestedUrl.openConnection();
         urlConnection.setSSLSocketFactory(CertificateManager.getInstance().getSslContext().getSocketFactory());
...

所以我可以创建我的SSLContext,并显示我的两个证书内容。但是当我尝试执行我的HTTPS连接时,我得到以下异常:

09-23 13:43:30.283:W / System.err(19422):javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。

你们其中一个人是否遇到过以下错误?你的解决方案是什么?

这些是我经历的网站(没有成功):

http://blog.chariotsolutions.com/2013/01/https-with-client-certificates-on.html

http://nelenkov.blogspot.ch/2011/12/using-custom-certificate-trust-store-on.html

1 个答案:

答案 0 :(得分:1)

在您的代码中,您正在创建并初始化SSLContext但不使用它。也许你应该替换:

urlConnection.setSSLSocketFactory(CertificateManager.getInstance().getSslContext().getSocketFactory());

通过

urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());

我还建议您尽可能将选项-Djavax.net.debug=all传递给JVM。它将在标准输出上打印有关SSL连接和握手的详细信息。