Search code examples
javacertificatekeystorejks

Loading a Certificate from a Keystore


I have a Base-64 encoded X.509 (.CER) certificate that is causing a NullPointerException on the below java code. The line causing it is PublicKey pubKey = pubCert.getPublicKey();

Is there a way to verify that myfile.keystore has the "xyz" certificate?

final FileInputStream keyFile = new FileInputStream(filePath
                    + "myfile.keystore");
final KeyStore keyStore = KeyStore.getInstance("JKS");
String storepass = "mypass";
keyStore.load(keyFile, storepass.toCharArray());
Certificate pubCert = keyStore.getCertificate("xyz");
PublicKey pubKey = pubCert.getPublicKey();

Solution

  • Try the keytool command that should be in your JRE or JDK bin directory, see output below:

    @raspbmc:~$ /opt/jdk1.8.0/bin/keytool  -list -help
    keytool -list [OPTION]...
    
    Lists entries in a keystore
    
    Options:
    
     -rfc                            output in RFC style
     -alias <alias>                  alias name of the entry to process
     -keystore <keystore>            keystore name
     -storepass <arg>                keystore password
     -storetype <storetype>          keystore type
     -providername <providername>    provider name
     -providerclass <providerclass>  provider class name
     -providerarg <arg>              provider argument
     -providerpath <pathlist>        provider classpath
     -v                              verbose output
     -protected                      password through protected mechanism
    
    Use "keytool -help" for all available commands
    

    Reference:

    http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/keytool.html