我已使用ECDH密钥协议在HSM密钥库中设置了私钥,我试图从JBOSS应用程序服务器中获取秘密密钥。当我作为独立应用程序运行时,以下代码可正常工作,但在JBOSS EAP 7.1中则无效。
这是代码
LunaSlotManager manager = LunaSlotManager.getInstance();
manager.login(0, "pass");
KeyStore lunaStore = KeyStore.getInstance("Luna", "LunaProvider");
ByteArrayInputStream is1 = new ByteArrayInputStream(("slot:" + 0).getBytes());
lunaStore.load(is1, "crypto1".toCharArray());
manager.setSecretKeysExtractable(true);
KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH", lunaStore.getProvider());
keyAgreement.init((PrivateKey)lunaStore.getKey("TestPrivateKey", "crypto1".toCharArray()));
String publicKey = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXjx+yO+dCt5HGPlFncNSt3oTM0cBLbgqedBW/3HVraL8qiHmMB2PVDzyLiBHOYYuZNe07vZLpdBcT9RB+dzmiA==";
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
KeyFactory factory = KeyFactory.getInstance("EC");
keyAgreement.doPhase(factory.generatePublic(publicKeySpec), true);
byte[] secretKey = keyAgreement.generateSecret();
当我从JBOSS执行keyAgreement.generateSecret()的行时遇到错误,但它在独立应用程序中正常工作。
这是我从HSM检索密钥时遇到的错误,
com.safenetinc.luna.exception.LunaException:无法导出密钥 [2019-01-16 05:21:16 21916 @ USSY-6XLJ9N2-D默认任务2标准输出写入信息]在com.safenetinc.luna.provider.keyagree.LunaKeyAgreementEcDh.engineGenerateSecretWantBytes(LunaKeyAgreementEcDh.java:355) [2019-01-16 05:21:16 21916 @ USSY-6XLJ9N2-D默认任务2标准输出写入信息]在com.safenetinc.luna.provider.keyagree.LunaKeyAgreementEcDh.engineGenerateSecret(LunaKeyAgreementEcDh.java:393) [2019-01-16 05:21:16 21916 @ USSY-6XLJ9N2-D默认任务2标准输出写入信息]在javax.crypto.KeyAgreement.generateSecret(KeyAgreement.java:586) [2019-01-16 05:21:16 21916 @ USSY-6XLJ9N2-D默认任务2标准输出写入信息]在com.sjm.patientPortal.services.ngq.tool.HSMClinetServiceTool.validateHSM(HSMClinetServiceTool.java:103) [2019-01-16 05:21:16 21916 @ USSY-6XLJ9N2-D默认任务2标准输出写入INFO]在com.sjm.patientPortal.services.ngq.tool.HSMClinetServiceTool $$ FastClassBySpringCGLIB $$ 1b5687a0.invoke() 引起原因:com.safenetinc.luna.exception.LunaCryptokiException:函数'CA_DeriveKeyAndWrap'在key = 2305上返回0x6a [2019-01-16 05:21:16 21916 @ USSY-6XLJ9N2-D默认任务2标准输出写入INFO]在com.safenetinc.luna.exception.LunaCryptokiException.ThrowNew(LunaCryptokiException.java:91) [2019-01-16 05:21:16 21916 @ USSY-6XLJ9N2-D默认任务2标准输出写入INFO]在com.safenetinc.luna.LunaAPI.DeriveKeyAndWrapEcDh(本机方法) [2019-01-16 05:21:16 21916 @ USSY-6XLJ9N2-D默认任务2标准输出写入信息]在com.safenetinc.luna.provider.keyagree.LunaKeyAgreementEcDh.engineGenerateSecretWantBytes(LunaKeyAgreementEcDh.java:320)
我尝试了所有选项。并且需要您的帮助来解决此问题。
谢谢
答案 0 :(得分:0)
两种解决此问题的方法:-
在java.security中添加com.safenetinc.luna.provider.createExtractableSecretKeys=true
。此选项会将所有新生成的秘密密钥标记为可提取。
如果您不希望所有键都将CKA_EXTRACTABLE设置为true,则需要像这样更改代码...
LunaSlotManager manager = LunaSlotManager.getInstance();
manager.setSecretKeysExtractable(true);
manager.login(0, "pass");