我不知道是否有某种KeyStore
它允许您访问 Mac OS X 系统根证书,但您可以尝试其他方式。
在 Mac OS X 中,您可以使用以下命令从任何钥匙串获取证书列表security
命令。
例如,此命令将为您提供有关系统根钥匙串中安装的不同证书的信息:
security find-certificate -a "/System/Library/Keychains/SystemRootCertificates.keychain"
该实用程序有两个标志,-p
,这会将每个证书输出为 PEM 编码,并且-a
,这允许我们按名称过滤结果 - 由于系统中安装了大量 CA,这很方便。
这个想法是使用 Java 中的这个实用程序。
不久前,我偶然发现了一个名为客户公司 https://github.com/ctt-gob-es/clienteafirma旨在处理数字签名。
这个库有一个类叫做苹果脚本 https://github.com/ctt-gob-es/clienteafirma/blob/d7b211e2ce50bbd9cdbda20e95746122f8fac783/afirma-keystores-mozilla/src/main/java/es/gob/afirma/keystores/mozilla/apple/AppleScript.java。这个类基本上是一个包装器Process
这允许我们运行任意命令。
以下代码使用该类和security
命令获取所有颁发的证书,例如,VeriSign
:
public static void main(String... args) {
// Keychains that we can use
final String KEYCHAIN_PATH = "/Library/Keychains/System.keychain";
final String SYSTEM_KEYCHAIN_PATH = "/System/Library/Keychains/SystemRootCertificates.keychain";
// Show me only certificates from VeriSign
final String AUTHORITY = "VeriSign";
final String OSX_SEC_COMMAND = "security find-certificate -a -p -c %AUTHORITY% %KEYCHAIN%";
final String cmd = OSX_SEC_COMMAND.replace("%AUTHORITY%", AUTHORITY).replace("%KEYCHAIN%", SYSTEM_KEYCHAIN_PATH);
System.out.println(cmd);
System.out.println();
final AppleScript script = new AppleScript(cmd);
InputStream certificateStream = null;
try {
// Run script
final String result = script.run();
certificateStream = new ByteArrayInputStream(result.getBytes());
// Process the output of the command
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
final Collection<X509Certificate> certificates = (Collection<X509Certificate>) certificateFactory.generateCertificates(certificateStream);
// Use the certificates as you need
for (X509Certificate certificate : certificates) {
String alias = certificate.getSubjectX500Principal().getName();
System.out.println("Certificate: " + alias);
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (certificateStream != null) {
try {
certificateStream.close();
} catch (IOException io) {
io.printStackTrace();
}
}
}
}