Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading keystore file, no alias found

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.

like image 821
Radu Avatar asked Apr 30 '14 19:04

Radu


People also ask

What is the alias of a keystore?

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.

How do I remove alias from keystore?

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.


1 Answers

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.

like image 55
Oleg Estekhin Avatar answered Sep 28 '22 10:09

Oleg Estekhin