Failed to read certificate from KeyStore when running Unit Test
For some reason, loading KeyStore in Unit Test seems empty and I have no idea why.
I have a keystore file with a certificate in it: src/test/resources/public-keystore-name
So, by running this command in the terminal:
../src/test/resources$ keytool -list -keystore public-keystore-name -storetype PKCS12
I am getting the following output:
Keystore type: PKCS12
Keystore provider: SunJSSE
Your keystore contains 1 entry
aliasname, May 22, 2015, trustedCertEntry,
Certificate fingerprint (SHA1): 4E:87:CF:EF:FC:E1:37:63:36:E0:26:0C:1E:B3:65:BB:48:3A:83:1A
In my unit test, I can download and run the KeyStore from this file ok, but I canβt get the certificate I saved in it. The certificate has an alias "aliasname" and a password "password".
@Test
public void testUtil() throws Exception {
KeyStore publicKS = KeyStore.getInstance("PKCS12");
File publicKeyStoreFile = FileUtils.getFile("src/test/resources/public-keystore-name");
FileInputStream fisPublic = new FileInputStream(publicKeyStoreFile);
publicKS.load(fisPublic, "password".toCharArray());
Certificate cert = publicKS.getCertificate("aliasname");
System.out.println("Cert is: " + cert);
}
Always prints: "Certificate: null"
Why is KeyStore empty in this Unit Test? (searching for aliases using Keystore.aliases () returns an empty set).
source to share
The KeyStore was created using the BouncyCastle stuff, so you had to go into this KeyStore setting:
...
KeyStore publicKS = KeyStore.getInstance("PKCS12", "BC");
...
"BC" is shorthand for BouncyCastle and can be used after startup:
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
I think the default KeyStore is probably some Java standard?
source to share