android开发分享如何创build包含客户端证书链的BKS(BouncyCastle)格式的Java Keystore

我正在编写一个需要SSL客户端身份validation的Android应用程序。 我知道如何为桌面Java应用程序创build一个JKS密钥库,但是Android只支持BKS格式。 我试图创build密钥库结果的每一种方法导致以下错误:
handling exception: javax.net.ssl.SSLHandshakeException: null cert chain

所以看起来客户端永远不会发送正确的证书链,可能是因为我没有正确地创build密钥存储。 我无法启动SSLdebugging,就像我可以在dekstop上一样,所以这使得这比应该更困难。

以下是创buildBKS 信任库的命令:
keytool -importcert -v -trustcacerts -file "cacert.pem" -alias ca -keystore "mySrvTruststore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "bcprov-jdk16-145.jar" -storetype BKS -storepass testtest


这是我试过的命令不能创build一个BKS客户机密钥库

 cat clientkey.pem clientcert.pem cacert.pem > client.pem keytool -import -v -file <(openssl x509 -in client.pem) -alias client -keystore "clientkeystore" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "bcprov-jdk16-145.jar" -storetype BKS -storepass testtest 

    我详细的一步一步的指示,以实现这一点

    MyHttpClient.java

     package com.arisglobal.aglite.network; import java.io.InputStream; import java.security.KeyStore; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.SingleClientConnManager; import com.arisglobal.aglite.activity.R; import android.content.Context; public class MyHttpClient extends DefaultHttpClient { final Context context; public MyHttpClient(Context context) { this.context = context; } @Override protected ClientConnectionManager createClientConnectionManager() { SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); // Register for port 443 our SSLSocketFactory with our keystore to the ConnectionManager registry.register(new Scheme("https", newSslSocketFactory(), 443)); return new SingleClientConnManager(getParams(), registry); } private SSLSocketFactory newSslSocketFactory() { try { // Get an instance of the Bouncy Castle KeyStore format KeyStore trusted = KeyStore.getInstance("BKS"); // Get the raw resource, which contains the keystore with your trusted certificates (root and any intermediate certs) InputStream in = context.getResources().openRawResource(R.raw.aglite); try { // Initialize the keystore with the provided trusted certificates. // Also provide the password of the keystore trusted.load(in, "aglite".toCharArray()); } finally { in.close(); } // Pass the keystore to the SSLSocketFactory. The factory is responsible for the verification of the server certificate. SSLSocketFactory sf = new SSLSocketFactory(trusted); // Hostname verification from certificate // https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506 sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); return sf; } catch (Exception e) { throw new AssertionError(e); } } } 

    如何在Activity类中调用上面的代码:

     DefaultHttpClient client = new MyHttpClient(getApplicationContext()); HttpResponse response = client.execute(...); 

    我使用Portecle ,它像一个魅力。

    我不认为你的问题是与BouncyCastle密钥库; 我认为这个问题是由Android中的javax.net.ssl包破坏的。 BouncyCastle密钥库是一个极大的烦恼,因为Android改变了默认的Java行为而没有logging任何地方 – 并删除了默认的提供者 – 但它确实有效。

    请注意,对于SSL身份validation,您可能需要2个密钥库。 包含CA证书的“TrustManager”密钥库和包含客户端公钥/私钥的“KeyManager”密钥库。 (该文档在KeyManager密钥库中需要的内容有点模糊。)理论上,如果您的所有证书都由“知名”证书颁发机构签名,则不需要TrustManager密钥库,例如Verisign,Thawte,等等。 让我知道这对你有用。 您的服务器还需要CA用于签署客户端的任何内容。

    我根本无法使用javax.net.ssl创buildSSL连接。 我禁用了服务器端的客户端SSLauthentication,而且我仍然无法创build连接。 由于我的最终目标是HTTPS GET,因此我尝试使用与Android捆绑在一起的Apache HTTP Client。 那种工作。 我可以使HTTPS连接,但我仍然无法使用SSL身份validation。 如果我在我的服务器上启用了客户端SSL身份validation,则连接将失败。 我没有检查Apache HTTP客户端代码,但我怀疑他们正在使用他们自己的SSL实现,并且不使用javax.net.ssl。

    不知道你是否解决了这个问题,但这是我如何做,它适用于Android:

    命令行:

     keytool -genseckey -alias aliasName -keystore truststore.bks -providerclass org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath /path/to/jar/bcprov-jdk16-1.46.jar -storetype BKS 

    您创buildBKS密钥库的命令对我来说看起来是正确的。

    你如何初始化密钥库。

    您需要craeate并传递您自己的SSLSocketFactory。 这是一个使用Apache的org.apache.http.conn.ssl.SSLSocketFactory的例子

    但是我认为你可以在javax.net.ssl.SSLSocketFactory上做相同的事情

      private SSLSocketFactory newSslSocketFactory() { try { // Get an instance of the Bouncy Castle KeyStore format KeyStore trusted = KeyStore.getInstance("BKS"); // Get the raw resource, which contains the keystore with // your trusted certificates (root and any intermediate certs) InputStream in = context.getResources().openRawResource(R.raw.mykeystore); try { // Initialize the keystore with the provided trusted certificates // Also provide the password of the keystore trusted.load(in, "testtest".toCharArray()); } finally { in.close(); } // Pass the keystore to the SSLSocketFactory. The factory is responsible // for the verification of the server certificate. SSLSocketFactory sf = new SSLSocketFactory(trusted); // Hostname verification from certificate // https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506 sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); return sf; } catch (Exception e) { throw new AssertionError(e); } } 

    请让我知道,如果它的工作。

    使用本手册; 在商店中观察一系列证书是很重要的。 例如:先导入最下面的中间CA证书,然后导入根CA证书

      以上就是android开发分享如何创build包含客户端证书链的BKS(BouncyCastle)格式的Java Keystore相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/520382.html

      (0)
      上一篇 2020年12月6日
      下一篇 2020年12月6日

      精彩推荐