How to load multiple SSL certificates in Java KeyStore?

android, java, ssl

Solution

Just call it again with another alias.

Example:

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

Problem

I have two SSL certificate files. The first is marked as "OU=Certificate Authority" and the second one is marked as "OU=Root Certificate." Our C++ application loads both these certificates for proper client/server handshake. I now need to use these certificates in my Android code. I have been able to read these certificates successfully using `CertificateFactory.generateCertificate()` method. 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? I see another method on KeyStore called `setEntry`. Should I be using this method instead? Regards.

Original source