1

我正在尝试在我的项目中使用 Softhsm2。不幸的是,它一直显示

java.security.ProviderException:解析配置时出错

我尝试重新安装应用程序,但问题仍然存在。我可以知道我错过了什么吗?请检查以下源代码、配置文件、令牌、Java 版本和环境变量。

错误结果

public static void main(String[] args) {
    try {
         String configName = "softhsm2.conf";

         Provider p = new SunPKCS11(configName);
         
         if (-1 == Security.addProvider(p)) {
                throw new RuntimeException("could not add security provider");
         }

         // Load the key store
         char[] pin = "mypin".toCharArray();
         KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
         keyStore.load(null, pin);

         // AES key
         SecretKeySpec secretKeySpec = new SecretKeySpec("0123456789ABCDEF".getBytes(), "AES");
         Key key = new SecretKeySpec(secretKeySpec.getEncoded(), "AES");

         keyStore.setKeyEntry("AA", key, "1234".toCharArray(), null);
         keyStore.store(null); //this gives me the exception.                             
    } catch(Exception e) {
        System.out.println("error: " + e.toString());
    }
}

生成的令牌:

    Microsoft Windows [Version 10.0.17134.1304]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\WINDOWS\system32>cd/

C:\>cd softhsm2

C:\SoftHSM2>cd bin

C:\SoftHSM2\bin>softhsm2-util.exe --show-slots
Available slots:
Slot 1759353984
    Slot info:
        Description:      SoftHSM slot ID 0x68dd9c80
        Manufacturer ID:  SoftHSM project
        Hardware version: 2.5
        Firmware version: 2.5
        Token present:    yes
    Token info:
        Manufacturer ID:  SoftHSM project
        Model:            SoftHSM v2
        Hardware version: 2.5
        Firmware version: 2.5
        Serial number:    8414edde68dd9c80
        Initialized:      yes
        User PIN init.:   yes
        Label:            My token 2
Slot 2066642166
    Slot info:
        Description:      SoftHSM slot ID 0x7b2e74f6
        Manufacturer ID:  SoftHSM project
        Hardware version: 2.5
        Firmware version: 2.5
        Token present:    yes
    Token info:
        Manufacturer ID:  SoftHSM project
        Model:            SoftHSM v2
        Hardware version: 2.5
        Firmware version: 2.5
        Serial number:    9176dec57b2e74f6
        Initialized:      yes
        User PIN init.:   yes
        Label:            My token 1
Slot 2
    Slot info:
        Description:      SoftHSM slot ID 0x2
        Manufacturer ID:  SoftHSM project
        Hardware version: 2.5
        Firmware version: 2.5
        Token present:    yes
    Token info:
        Manufacturer ID:  SoftHSM project
        Model:            SoftHSM v2
        Hardware version: 2.5
        Firmware version: 2.5
        Serial number:
        Initialized:      no
        User PIN init.:   no
        Label:

C:\SoftHSM2\bin>

配置:C:\SoftHSM2\etc\softhsm2.conf

# SoftHSM v2 configuration file
directories.tokendir = C:\SoftHSM2\var\softhsm2\tokens\
objectstore.backend = file
log.level = INFO
slots.removable = false
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)

环境变量 SOFTHSM2_CONF C:\SoftHSM2\etc\softhsm2.conf

路径:C:\SoftHSM2\lib\

4

1 回答 1

0

我不是 JAVA 专家,但您似乎将不正确的配置传递给SunPKCS11提供者。

您的方案中似乎有两种不同的配置:

  1. SunPKCS11指示SunPKCS11提供程序加载哪个 PKCS#11 库的配置。
  2. SoftHSM2指示SoftHSM2库在哪里存储数据等的配置。

SunPKCS11文档建议在您的情况下,SunPKCS11配置应类似于以下内容:

name = SofHSM2
library = c:\SoftHSM2\lib\softhsm2-x64.dll
于 2021-08-18T11:39:18.483 回答