1

I have recently migrated from Java8 to Java11. The below piece of code is giving an issue:

One of my methods to load the provider looks like below:

        Provider prototype = Security.getProvider("SunPKCS11");
        final String providerConfig = config.getProviderConfig();
        LOGGER.debug("providerConfig={}", providerConfig);
        prototype.load(resourceLoader.getResource(providerConfig).getInputStream());
        Security.addProvider(prototype);
        LOGGER.debug("prototype={}", prototype);
        return prototype;

And place where I am encountering exception in another method:

        **KeyStore keyStore = KeyStore.getInstance("PKCS11", provider);**
        keyStore.load(null, keyPassword.toCharArray());

I am getting below exception in the highlighted line of code:

java.security.KeyStoreException: PKCS11 not found
at java.security.KeyStore.getInstance(KeyStore.java:974) ~[?:?]

Caused by: java.security.NoSuchAlgorithmException: no such algorithm: PKCS11 for 
provider SunPKCS11
at sun.security.jca.GetInstance.getService(GetInstance.java:101) ~[?:?]
at sun.security.jca.GetInstance.getInstance(GetInstance.java:218) ~[?:?]
at java.security.Security.getImpl(Security.java:727) ~[?:?]
at java.security.KeyStore.getInstance(KeyStore.java:971) ~[?:?]

Kindly advice.

  • 1
    You need to `.configure` (not `.load`) a SunPKCS11 provider from a `File` (i.e. name) not an `InputStream`, and use the returned object not the initial one; see the documentation. This is not new; your code should not work by itself in any Java version. However, the provider can also be configured in the java.security file; perhaps you have a installed 8 that does this and and installed 11 that does not. (The file _location_ does change between 8 and 9.) – dave_thompson_085 May 27 '22 at 20:31

0 Answers0