当前位置: 首页 > 面试题库 >

初始化后如何完成SunPKCS11 Provider?

毛勇
2023-03-14
问题内容

我通过以下方式初始化了SunPKCS11提供程序:

Provider provider = new sun.security.pkcs11.SunPKCS11("path_to_pkcs11.cfg");
Security.addProvider(provider);

然后,我使用此提供程序初始化KeyStore,以将密钥用于密码操作。

KeyStore ks = KeyStore.getInstance("PKCS11", provider);
ks.load(null, "password".toCharArray());

密码操作完成后, 如何使用PKCS11令牌完成会话?

我曾尝试删除该提供程序,但没有成功。

Security.removeProvider("sunPCKS11ProviderName");

下次我尝试与令牌通信时,我从令牌 CKR_CRYPTOKI_ALREADY_INITIALIZED* 抛出此异常 *

更新

我努力了

sun.security.pkcs11.SunPKCS11.logout();

但它也不起作用。

我有一个用例,其中必须同时使用PKCS#11包装程序和提供程序。为了能够使用包装器,我必须完成提供程序的确定,否则CKR_CRYPTOKI_ALREADY_INITIALIZED
当包装器尝试与令牌通信时,令牌会引发错误。

用代码更新:

我正在使用Sun的PKCS#11提供程序和IAIK的PKCS#11包装程序。

public static void providerAndWrapperIssue() throws Exception
{
    final String name = "ANY_NAME";
    final String library = "LOCATION OF THE TOKENS DLL/SO";
    final String slot = "SLOT NUMBER";

    // SUN PKCS#11 Provider -------------------------------------------

    StringBuilder builder = new StringBuilder();
    builder.append("name=" + name);
    builder.append(System.getProperty("line.separator"));
    builder.append("library=\"" + library + "\"");
    builder.append(System.getProperty("line.separator"));
    builder.append("slot=" + slot);

    ByteArrayInputStream bais = new ByteArrayInputStream(builder.toString().getBytes());
    Provider provider = new sun.security.pkcs11.SunPKCS11(bais);
    Security.addProvider(provider);

    KeyStore ks = KeyStore.getInstance("PKCS11");
    ks.load(null, null);

    Enumeration<String> aliases = ks.aliases();
    while (aliases.hasMoreElements())
        System.out.println(aliases.nextElement());

    // IAIK PKCS#11 Wrapper -------------------------------------------

    Module pkcs11Module = Module.getInstance(library, false);
    pkcs11Module.initialize(null); <-- Exception here.

    Slot[] slots = pkcs11Module.getSlotList(true);

    Session session = slots[0].getToken().openSession(true, true, null, null);
    session.login(Session.UserType.USER, "".toCharArray());

    session.logout();
    session.closeSession();

    slots[0].getToken().closeAllSessions();

    pkcs11Module.finalize(null);
}

由于Sun的提供程序没有注销并关闭会话,因此IAIK无法访问令牌。而且Java的Keystoreapi没有注销方法


问题答案:

终于能够找到解决方案。Sun的提供程序在下面使用包装器。因此,诀窍是使用Sun的PKCS#11包装器来获取当前实例并完成该实例。显然,此会话功能的最终确定未在提供程序中公开。但是有一个解决方法,它看起来像这样:

public static void providerAndWrapperIssue() throws Exception
{
    final String name = "ANY_NAME";
    final String library = "LOCATION OF THE TOKENS DLL/SO";
    final String slot = "SLOT NUMBER";

    // SUN PKCS#11 Provider -------------------------------------------

    StringBuilder builder = new StringBuilder();
    builder.append("name=" + name);
    builder.append(System.getProperty("line.separator"));
    builder.append("library=\"" + library + "\"");
    builder.append(System.getProperty("line.separator"));
    builder.append("slot=" + slot);

    ByteArrayInputStream bais = new ByteArrayInputStream(builder.toString().getBytes());
    Provider provider = new sun.security.pkcs11.SunPKCS11(bais);
    provider.setProperty("pkcs11LibraryPath", library);
    Security.addProvider(provider);

    KeyStore ks = KeyStore.getInstance("PKCS11");
    ks.load(null, null);

    Enumeration<String> aliases = ks.aliases();
    while (aliases.hasMoreElements())
        System.out.println(aliases.nextElement());

    // ====================================
    // Solved it using the SUN PKCS#11 Wrapper

    PKCS11 pkcs11 = PKCS11.getInstance(((sun.security.pkcs11.SunPKCS11) provider).getProperty("pkcs11LibraryPath"), null, null, true);
    pkcs11.C_Finalize(PKCS11Constants.NULL_PTR);

    // ====================================

    // IAIK PKCS#11 Wrapper -------------------------------------------

    Module pkcs11Module = Module.getInstance(library, false);
    pkcs11Module.initialize(null);

    Slot[] slots = pkcs11Module.getSlotList(true);

    Session session = slots[0].getToken().openSession(true, true, null, null);
    session.login(Session.UserType.USER, "".toCharArray());

    session.logout();
    session.closeSession();

    slots[0].getToken().closeAllSessions();

    pkcs11Module.finalize(null);
}


 类似资料:
  • 问题内容: 我有一个用例,需要在ApplicationContext加载一次时在Bean中调用一次(非静态)方法。如果我为此使用MethodInvokingFactoryBean可以吗?还是我们有更好的解决方案? 附带说明一下,我使用ConfigContextLoaderListener在Web应用程序中加载应用程序上下文。并且想要,如果实例化了bean’A’,则只需调用一次methodA()。

  • 在我的AngularJS应用程序中执行任何控制器之前,我有一些全局数据需要加载(即在AngularJS中全局解析依赖项)。 例如,我有一个,它带有方法,它向后端服务器发出请求,以获取关于当前通过身份验证的用户的数据。我有一个控制器需要这些数据来启动另一个请求(例如加载用户的余额)。 我怎样才能做到呢?

  • 我想在部署初始化成功后运行特定的命令。 这是我的yaml文件: 不过,我希望在成功初始化部署并运行POD之后(而不是之前)运行db迁移命令。 我可以为每个pod手动执行(使用kubectl exec),但这不是很好的扩展性。

  • 问题内容: 在AngularJS应用程序中执行任何控制器之前,我需要加载一些全局数据(即,在AngularJS中全局解析依赖项)。 例如,我有一个with 方法,该方法向后端服务器发出请求,以获取有关当前经过身份验证的用户的数据。我有一个控制器,需要该数据才能启动另一个请求(例如,加载用户的余额)。 我该如何实现? 问题答案: 如果可能,请考虑使用《使用服务器端数据异步引导AngularJS应用程

  • 我有一个非常基本的问题。我需要如何为ArrayList键入parrameters才能在BlueJ中使用代码?我有一个方法如下。

  • 我在代码中使用了合成属性。但想知道它如何以及何时初始化Android中的每个视图。 我们只提供导入和访问每个视图的ID。何时为视图对象分配内存?