尝试失败后在 Java 上更新安全令牌

发布于 2024-12-12 17:22:01 字数 6638 浏览 0 评论 0原文

我开发了代码来检查在执行特定操作之前是否在 PC 中找到了特定的安全令牌。我现在使用的获取密钥库的代码是:

    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);
    }

如果应用程序第一次尝试访问密钥库时存在安全令牌(USB 智能卡),则它可以顺利运行,不会出现错误;但是,当应用程序第一次尝试获取令牌时智能卡不存在时,KeyStore.getInstance 会引发此异常:

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

在第一次尝试之后,如果我插回安全令牌,然后重试操作,我得到完全相同的错误。

SunPKCS11 类是否缓存了一些数据?如果是这样,有什么办法可以清除这个缓存吗?

感谢您的关注,请原谅我的拼写。英语不是我的母语。

编辑:

完整的堆栈跟踪:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文