采用了Android信任库的aSmack在Android 4以上(ICS)(Using the An

2019-07-29 16:32发布

I am not an expert on keystores and have a hard time understanding the nuances of this but this is how far I got:

In creating a xmpp-connection using the asmack build found here one still has to change the truststore, which usually, so say multiple sources on the web, is done using these commands

ConnectionConfiguration config = new ConnectionConfiguration(host, Integer.parseInt(port), service);
config.setTruststorePath("/system/etc/security/cacerts.bks");
config.setTruststorePassword("changeit");
config.setTruststoreType("bks");
XMPPConnection connection = new XMPPConnection(connConfig);
connection.connect();

This works find for older Android versions but under ICS they changed some things and now it does not anymore.The path now is diferent.

Apparently this can be fixed but I have no earthly idea how.

What is needed, obviously, is a method that returns the path depending on SDK version that returns the needed string to set the sdk-path since you can not just return the keystore itself to the xmpp-connection.

In reference to this that method would look like this:

private String getTrustStorePath() 
{
 String path = System.getProperty("javax.net.ssl.trustStore");

 if (path == null) 
 {
  if ( Build.VERSION.SDK_INT >= 14 ) 
  {
   //THIS IS THE PART I DONT KNOW
   path="";
  }
  else
  {
   path = "/system/etc/security/cacerts.bks";
  }

  return path;
}

Here a commenter says that under Android "4.x; /etc/security/cacerts.bks was replaced with the directory/etc/security/cacerts/ containing the certs as individual PEM encoded files." however, I do not know what relevance, if any, this has.

I have also checked out the code of two projects using xmpp and asmack (gtalksms and yaxim but did not see how they avoid this problem.

Answer 1:

试试这个:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    connectionConfiguration.setTruststoreType("AndroidCAStore");
    connectionConfiguration.setTruststorePassword(null);
    connectionConfiguration.setTruststorePath(null);
} else {
    connectionConfiguration.setTruststoreType("BKS");
    String path = System.getProperty("javax.net.ssl.trustStore");
    if (path == null)
        path = System.getProperty("java.home") + File.separator + "etc"
            + File.separator + "security" + File.separator
            + "cacerts.bks";
    connectionConfiguration.setTruststorePath(path);
}

见https://github.com/Flowdalic/asmack/wiki/Truststore和一些背景解释http://nelenkov.blogspot.com/2011/12/ics-trust-store-implementation.html 。



Answer 2:

在ICS的信任存储不是一个单一的文件.bks任何更多的,但在不同的PEM编码的文件/system/etc/security/cacerts目录。 用户添加的证书可以被放置在/data/misc/keychain/cacerts-added 。 更多细节可以发现在这里

您的证书文件必须被命名为: subject-hash.N其中N是一个整数的顺序从0开始(通常只是0,但如果为0已被使用,则1等)。

为了让您的证书的主题-哈希值,你可以使用OpenSSL这样的: openssl x509 -noout -subject_hash_old -in my-cert-file.pem



文章来源: Using the Android TrustStore for aSmack in Android 4+ (ICS)