gpt4 book ai didi

java - 需要帮助理解 getAcceptedIssuers 方法

转载 作者:行者123 更新时间:2023-12-01 12:44:11 26 4
gpt4 key购买 nike

我正在编写代码以从 Android 设备获取所有受信任的证书。

这是我的代码,

public class MyX509TrustManager implements X509TrustManager {
X509TrustManager pkixTrustManager;

public MyX509TrustManager(KeyStore keystore)
throws Exception {

TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init(keystore);

TrustManager tms[] = tmf.getTrustManagers();

for (int i = 0; i < tms.length; i++) {
if (tms[i] instanceof X509TrustManager) {
pkixTrustManager = (X509TrustManager) tms[i];

System.out.println("The class is " + pkixTrustManager.getClass());
return;
}
}

throw new Exception("Couldn't initialize");
}

public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {

try {
pkixTrustManager.checkClientTrusted(arg0, arg1);
} catch (CertificateException excep) {

}

}

public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
try {
pkixTrustManager.checkServerTrusted(arg0, arg1);
} catch (CertificateException excep) {

}
}

public X509Certificate[] getAcceptedIssuers() {


System.out.println("Control inside the issuers method");

System.out.println( "Number of trusted certificates is "+ pkixTrustManager.getAcceptedIssuers().length);
return pkixTrustManager.getAcceptedIssuers();

}
}

当我调用 getAcceptedIssuers 方法时,我得到空数组(由 getacceptedIssuers 方法返回的数组长度确认)

我在这里做错了什么。

任何人都可以帮我从 Android 设备获取受信任的证书列表吗?

编辑

我在另一个类中调用 MyX509TrustManager 的构造函数:

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
// KeyStore.getDefaultType() returns "BKS"

MyX509TrustManager mm = new MyX509TrustManager(ks);

X509Certificate [] trucerts = m.getAcceptedIssuers();

如何加载带有受信任证书的 keystore ?

最佳答案

您在调用构造函数 MyX509TrustManager(KeyStore keystore) 时提供的 keystore 未持有任何证书颁发机构的任何证书。

例如,您可以创建一个 KeyStore 对象并用文件系统中 keystore 的内容( key 、证书、CA 证书...)填充它:

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

// get user password and file input stream
char[] password = "password of the keystore in the file system";

java.io.FileInputStream fis = null;
try {
fis = new java.io.FileInputStream("keyStoreName");
ks.load(fis, password);
} finally {
if (fis != null) {
fis.close();
}
}

关于java - 需要帮助理解 getAcceptedIssuers 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24843825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com