Search code examples
javasslkeystorepkcs#12

NPE when obtaining KeyStore instance


I have this simple code that was working a while back. Basically I am trying to get the keystore instance so that I can use it to enable ssl. My problem now is I get the null pointer exception below and can't seem to find a solution anywhere. I use openjdk 8 on ubuntu 15.04 32 bit.

//Do other initializations things
...
KeyStore ks = KeyStore.getInstance("PKCS12");
KeyStore ts = KeyStore.getInstance("PKCS12");

char[] keymanagerPassPhrase = keymanagerPassPhraseString.toCharArray();
char[] keystorePassPhrase = keystorePassPhraseString.toCharArray();
char[] truststorePassPhrase = truststorePassPhraseString.toCharArray();

ks.load(new FileInputStream(keystoreFile), keystorePassPhrase);

KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keymanagerPassPhrase);

TrustManager[] trustManagers = null;
    if( useCustomTrustStore ) {
         ts.load(new FileInputStream(truststoreFile), truststorePassPhrase);

         TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
            tmf.init(ts);

         trustManagers = tmf.getTrustManagers();
        }
        SSLContext sslContext = SSLContext.getInstance(protocal);

        sslContext.init(kmf.getKeyManagers(), trustManagers , null);

        return sslContext.createSSLEngine();

The exception thrown is:

Caused by: java.lang.NullPointerException
at java.security.Provider$ServiceKey.<init>(Provider.java:872)
at java.security.Provider$ServiceKey.<init>(Provider.java:865)
at java.security.Provider.getService(Provider.java:1039)
at sun.security.jca.ProviderList.getService(ProviderList.java:332)
at sun.security.jca.GetInstance.getInstance(GetInstance.java:157)
at java.security.Security.getImpl(Security.java:695)
at java.security.KeyStore.getInstance(KeyStore.java:848)

How can I proceed?


Solution

  • This Q's a little old but near the top of the Google results for this problem, so I'll try to help.

    To debug a NPE in Java's codebase, you just need to make sure you have the Java source code available in your IDE; then you can check for a hint on what is null. You seem to be using JDK 8, so it's this line:

    algorithm = algorithm.toUpperCase(ENGLISH);
    

    Plus, there's nothing else in the ServiceKey init method (constructor) that could cause a NPE. So... where does the String algorithm parameter come from, passed into this constructor?

    It looks like it's just passed through all of these layers; see at java.security.Security.getImpl(Security.java:695) -- algorithm is the first param passed in there.

    Unwrap one more: at java.security.KeyStore.getInstance(KeyStore.java:848) -- the param you pass in here is called "type", but that's what's passed into Security.getImpl as algorithm.

    So the bug is that you're passing null for the type param when you call KeyStore.getInstance(). This doesn't seem to match up with the source code you listed, so you may not have compiled your code properly. Add in a "sanity check" -- e.g., a println earlier in your code; and see what line number in your code is indicated further down the stack trace.