1

I have developed code to check if a certain security token is found in the PC prior to a certain operation. The code I am using right now to get the keystore is:

    private KeyStore getKeyStore() {
            try {
                    KeyStore keyStore = KeyStore.getInstance("PKCS11",getProvider()); //$NON-NLS-1$
                    char [] pin = "mypin".toCharArray();
                    keyStore.load(null, pin);

                    return keyStore;
            } catch (Exception e) {
                    e.printStackTrace();
            } 
            return null;
    }

    private Provider getProvider() throws ClassNotFoundException, 
        NoSuchMethodException, SecurityException, InstantiationException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Class<?> lclass = ClassLoader.getSystemClassLoader().getParent().loadClass("sun.security.pkcs11.SunPKCS11"); //$NON-NLS-1$
            @SuppressWarnings("rawtypes")
            Constructor constructor = lclass.getConstructor(String.class);
            return (Provider) constructor.newInstance(configName);
    }

If the security token (a USB smartcard) is present the first time the application tries to access the keystore, it runs smoothly without error; but when the smart card is not present the first time the applications tries to get the token, the KeyStore.getInstance raises this exception:

java.security.KeyStoreException: PKCS11 not found
at java.security.KeyStore.getInstance(Unknown Source)
at es.giro.girlabel.token.Token.getKeyStore(Token.java:120)
at es.giro.girlabel.token.Token.getCertificate(Token.java:156)
at es.giro.girlabel.token.Token.hasToken(Token.java:150)
at es.giro.girlabel.jobs.JobList.getInstance(JobList.java:73)
at es.giro.girlabel.editor.view.LabelListView$10.run(LabelListView.java:499)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at es.giro.girlabel.Application.start(Application.java:39)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
at org.eclipse.equinox.launcher.Main.run(Main.java:1408)
at org.eclipse.equinox.launcher.Main.main(Main.java:1384)


Caused by: java.security.NoSuchAlgorithmException: no such algorithm: PKCS11 for provider       SunPKCS11-eToken
at sun.security.jca.GetInstance.getService(Unknown Source)
at sun.security.jca.GetInstance.getInstance(Unknown Source)
at java.security.Security.getImpl(Unknown Source)
... 35 more

After this first try, if I plug back the security token, and retry the operation, I get exactly the same error.

Is the SunPKCS11 class caching some data? If so, is there any way to clear this cache?

Thanks for your attention, and please forgive my spelling. English is not my native language.

EDIT:

The complete stack trace:

java.security.KeyStore.getInstance(Unknown Source)
es.giro.girlabel.token.Token.getKeyStore(Token.java:119)
es.giro.girlabel.token.Token.getCertificate(Token.java:160)
es.giro.girlabel.token.Token.hasToken(Token.java:154)
es.giro.girlabel.jobs.JobList.getInstance(JobList.java:73)
es.giro.girlabel.editor.view.LabelListView$10.run(LabelListView.java:499)
org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
es.giro.girlabel.Application.start(Application.java:39)
org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
org.eclipse.equinox.launcher.Main.run(Main.java:1408)
org.eclipse.equinox.launcher.Main.main(Main.java:1384)
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245

0 Answers0