简体   繁体   English

如何在Java KeyStore中加载多个SSL证书?

[英]How to load multiple SSL certificates in Java KeyStore?

I have two SSL certificate files. 我有两个SSL证书文件。 The first is marked as "OU=Certificate Authority" and the second one is marked as "OU=Root Certificate." 第一个标记为“ OU =证书颁发机构”,第二个标记为“ OU =根证书”。 Our C++ application loads both these certificates for proper client/server handshake. 我们的C ++应用程序会同时加载这两个证书,以进行正确的客户端/服务器握手。

I now need to use these certificates in my Android code. 现在,我需要在我的Android代码中使用这些证书。 I have been able to read these certificates successfully using CertificateFactory.generateCertificate() method. 我已经能够使用CertificateFactory.generateCertificate()方法成功读取这些证书。

Next, I need to store these certificates in a keystore. 接下来,我需要将这些证书存储在密钥库中。 Here is the sample code I found: 这是我发现的示例代码:

    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

I am confused on how to store the root certificate. 我对如何存储根证书感到困惑。 Do I just call setCertificateEntry() again for the second certificate and pass in some random alias name? 我是否再次调用setCertificateEntry()作为第二个证书并传入一些随机别名?

I see another method on KeyStore called setEntry . 我在KeyStore上看到了另一个名为setEntry方法。 Should I be using this method instead? 我应该改用这种方法吗? Regards. 问候。

Just call it again with another alias. 只需使用另一个别名再次调用即可。

Example: 例:

keyStore.setCertificateEntry("ca", ca);
keyStore.setCertificateEntry("debug", ca2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM