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.
An alias is specified when you add an entity to the keystore using the -genseckey command to generate a secret key, -genkeypair command to generate a key pair (public and private key) or the -importcert command to add a certificate or certificate chain to the list of trusted certificates.
Check the contents of the trust store by entering the following in the command prompt: <JAVA_HOME>\bin\keytool -list -v -keystore truststore -storepass access . Note the alias names of the certificates you want to remove. Enter <JAVA_HOME>\bin\keytool -delete -alias <alias name> -keystore truststore.
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.
getResourceAsStream()
returned non-null value before passing it value into KeyStore.load()
.There are some questions about getResourceAsStream()
that can be of help for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With