首页 > 解决方案 > 使用 BouncyCastle 加载 pkcs12 文件在未知 PRF 算法 (hmacWithSHA256) 上失败

问题描述

我们有一个pkcs#12格式的证书,由第三方(非我们管理)提供。在从我们的平台将电子邮件发送给特定客户之前,我们必须使用此证书签署电子邮件。

旧证书没有问题,但由于它即将到期,我们需要用更新的证书替换它,它具有以下密钥加密模式(通过 openssl 提取):

PBES2、PBKDF2、AES-256-CBC、迭代 2000、PRF hmacWithSHA256

尝试使用 bouncycastle 安全提供程序加载此密钥库时,我们收到以下错误

Caused by: java.io.IOException: exception unwrapping private key - java.security.spec.InvalidKeySpecException: Invalid KeySpec: unknown PRF algorithm 1.2.840.113549.2.9
    at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.unwrapKey(Unknown Source)
    at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.engineLoad(Unknown Source)
    at java.security.KeyStore.load(KeyStore.java:1445)
    at org.obfuscated.SignEmailGenerator.loadKeyStore(SignEmailGenerator.java:130)

代码(为简洁起见,删除了异常处理和其他实用程序代码):

KeyStore keystore = KeyStore.getInstance("PKCS12", BouncyCastleProvider.PROVIDER_NAME);        
InputStream trustStoreInputStream = MethodHandles.lookup().lookupClass()
                .getResourceAsStream(mailSigningConfiguration.getKeyStorePath());
keystore.load(trustStoreInputStream, mailSigningConfiguration.getKeyStorePassword().toCharArray());

1.2.840.113549.2.9是 hmacWithSHA256 的 OID,这让我想到了这个问题充气城堡不支持这个算法吗?基于http://www.bouncycastle.org/specifications.html我猜它应该?如果它确实支持它,为什么我无法加载这样的文件?

jdk 1.8.0 bcmail-jdk15on 1.66版

任何意见表示赞赏,谢谢。

编辑(私钥是文件的一部分):

c:\Programy\OpenSSL-Win64\bin>openssl pkcs12 -info -in PrivateKey.pfx -nodes -nocerts
Enter Import Password:
MAC: sha256, Iteration 2000
MAC length: 32, salt length: 20
PKCS7 Data
Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC, Iteration 2000, PRF hmacWithSHA256
Bag Attributes
    localKeyID: 01 00 00 00
    friendlyName: ---ommited---
    Microsoft CSP Name: Microsoft Enhanced Cryptographic Provider v1.0
Key Attributes
    X509v3 Key Usage: 10
-----BEGIN PRIVATE KEY-----
-- data is here, but I've ommited it ---
-----END PRIVATE KEY-----
PKCS7 Encrypted data: PBES2, PBKDF2, AES-256-CBC, Iteration 2000, PRF hmacWithSHA256
Certificate bag
Certificate bag
Certificate bag

标签: javabouncycastlepkcs#12

解决方案


今天遇到这个问题,使用的BouncyCastle提供者是1.51. 服务器日志中的异常是:

2022-01-13 14:28:28,699 ERROR (default task-46) getKeyStore,load. location:xxx.p12 at xxx: java.io.IOException: exception unwrapping private key - java.security.spec.InvalidKeySpecException: Invalid KeySpec: unknown PRF algorithm 1.2.840.113549.2.9
    at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.unwrapKey(Unknown Source)
    at org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.engineLoad(Unknown Source)
    at java.security.KeyStore.load(KeyStore.java:1445)

用 .检查 p12 后openssl

$> openssl pkcs12 -info -in xxx.p12 -nodes -nocerts
Enter Import Password:
MAC: sha1, Iteration 100000
MAC length: 20, salt length: 20
PKCS7 Data
Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC, Iteration 10000, PRF hmacWithSHA256
Bag Attributes

我在另一个环境中使用 BouncyCastle provider 对其进行了测试1.69,效果很好。但是由于提供程序无法在服务器上升级,我不得不重新创建 p12 密钥库,openssl以使加密与服务器中的一些旧密钥库保持一致,然后它就可以工作了。

openssl pkcs12 -export -inkey <private> -in <cert> -name <alias> -out <keystore>.p12

检查重新创建的密钥库信息:

$> openssl pkcs12 -info -in xxx.p12 -nodes -nocerts
Enter Import Password:
MAC: sha1, Iteration 2048
MAC length: 20, salt length: 8
PKCS7 Encrypted data: pbeWithSHA1And40BitRC2-CBC, Iteration 2048

推荐阅读