[Solved]Why keystore.getkey() returns null?

May 26, 2011 at 5:53 PM
Edited May 26, 2011 at 6:03 PM

When we installed our app to a new Mac, we get the following error.  Our app runs fine on the Mac that generated the key. What steps did we miss on the new Mac?

It appears that it found the keystore file from the resource path but it could not get the key using the alias? we also ran keytool -list -keystore <our_keystore_file> and it shows that the keystore has the entry with that alias. The alias and path & keystore name are all passed by the app (same values for both machines). 

...

Caused by: com.microsoft.hsg.HVException: Key not found
    at com.microsoft.hsg.DefaultPrivateKeyStore.initData(DefaultPrivateKeyStore.java:140)

The SDK code that returned the expcetion:

 

if (!initialized)
{
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(Connection.class
          .getResourceAsStream(filename), 
          password.toCharArray());
    Key key = keystore.getKey(alias, password.toCharArray());
    if (key == null)
    {
    	throw new HVException("Key not found");
    }
Coordinator
Jun 1, 2011 at 5:29 AM

I don't know what might be going on here.

Jun 4, 2011 at 2:55 AM

Hi Rob,

We added debug statements and found the problem. The problem was that the Connection.class.getResourceAsStream returned null (did not find the keystore file from the classpath) but the keystore.load did not complain on the null input. Naturally the keystore.getKey() returned null.

The keystore file was not on the classpath because our maven pom.xml filters resources and the keystore file was not copied to the target directory during the build on the new machine. Once we added explicit copying of the keystore in the resources section of pom.xml (and made sure that filter was set to false for copying the keystore), everything worked.

For your next release, could you add a check on if the Connection.class.getResourceAsStream returns null and throw a "keystore file not found" message if it is? It will help folks to pin point the true problem quicker.

By the way, our app works great now using your library. Thank you for providing such a great SDK!

May