我想使用位于 p12 包中的自定义证书进行 https 连接。我已经在 iPhone 上做过了(所以我可以验证,证书、服务器等一切都很好),但是 Android 有一些问题。
我遵循了如何请求需要客户端证书进行身份验证的 URL,但结果出现以下异常:
12-13 12:32:44.545: W/System.err(4407): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: 找不到证书路径的信任锚。12-13 12:32:44.545: W/System.err(4407): 在 org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:374) 12-13 12:32:44.545: W/System.err(4407): 在 libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209) 12-13 12:32:44.545: W/System.err(4407): 在 libcore.net.http .HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478) 12-13 12:32:44.545: W/System.err(4407): 在 libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 12-13 12:32:44.545: W/System.err(4407): 在 libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java: 856) 12-13 12:32:44.555: W/System.err(4407): 由: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: 找不到证书路径的信任锚。12-13 12:32:44.560: W/System.err(4407): 在 org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:192) 12-13 12:32:44.560: W/System.err(4407): 在 org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:163) 12-13 12:32:44.560: W/System.err(4407):在 org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:573) 12-13 12:32:44.560: W/System.err(4407): 在 org.apache.harmony.xnet。 provider.jsse.NativeCrypto.SSL_do_handshake(本机方法)12-13 12:32:44.560:W/System.err(4407):在 org.apache.harmony.xnet。provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:371) 12-13 12:32:44.560: W/System.err(4407): ... 18 更多 12-13 12:32:44.560: W/System。错误(4407):原因:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。12-13 12:32:44.560: W/System.err(4407): ... 23 更多
我的连接代码如下所示:
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(context.getResources().openRawResource(R.raw.gecko_cert_1), "gecko_cert_1".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
kmf.init(ks, "gecko_cert_1".toCharArray());
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);
//request
URL serverURL = new URL(myurl);
HttpsURLConnection conn = (HttpsURLConnection)serverURL.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
//conn.setHostnameVerifier(DO_NOT_VERIFY);
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
干杯,马尔辛