Question

I'm trying to use KeyStore in order to get info from a keystore. I've generated the keystore using this command:

keytool -genkey -alias server -keyalg RSA -keystore server.keystore -validity 365 taken this page.

Checking its info keytool -list -v -keystore server.keystore I get the following:

Alias name: server
Creation date: Apr 30, 2014
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
(other info here)

Using this command: keytool -list -keystore server.keystore -alias server I get this:

server, Apr 30, 2014, PrivateKeyEntry, Certificate fingerprint (SHA1): 28:65:5B:0C:B3:3C:C9:AA:F1:7C:CE:91:23:77:DD:0D:F8:54:70:B9

Now, my java code:

keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(getClass().getResourceAsStream(KEYSTORE_FILE_PATH), "myPass".toCharArray());
keyStore.getCertificate("server").getPublicKey().getEncoded(); //here I get a null pointer exception - keystore.getCertificate("server") returns null. 

Doing keyStore.aliases() returns an EmptyEnumeration.

The application uses maven, java ee 7 and I've copied the keystore file in the resources folder of my application. KEYSTORE_FILE_PATH has the value of "/server.keystore".

Thanks.

Was it helpful?

Solution

Class.getResourceAsStream() returns null when there is no resource with the specified name. KeyStore.load() resets the key store to the empty state when passed a null input stream.

It means that at runtime your code does not find the keystore resource and silently proceeds with the empty keystore.

  • add a guarding condition that checks that getResourceAsStream() returned non-null value before passing it value into KeyStore.load().
  • review your code and building/packaging process in maven to ensure that keystore file is present at the proper location.

There are some questions about getResourceAsStream() that can be of help for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top