Java:Luna HSM使用公钥验证签名



使用Java API,我正在尝试访问存储在Luna HSM中的公钥。即使我能够打印相应的公钥标签名称,但是当我尝试获取公钥时,我无法获取对该公钥的引用。以下是代码片段:

        KeyStore ks = KeyStore.getInstance("Luna");
        ks.load(null, null); 
        lunaProvider = ks.getProvider();
        publicKey = (PublicKey) ks.getKey(alipayImpl.getHsmKeyStorePublicEntryName(), null);
        // ****************************************************************************
        // ** If the private keystore is not found, return original barcode string.  **
        // ****************************************************************************
        if (publicKey == null) {
            throw new Exception("Unable to acquire the Public Key " + alipayImpl.getHsmKeyStorePublicEntryName() + ", Hash will not be verified.");
        }
        // ***********************************************************
        // ** Create a Signature Object and sign the encrypted text **
        // ***********************************************************
        Signature signatureObject = Signature.getInstance(alipayImpl.getAlipaySignAlgorithm(), lunaProvider);
        signatureObject.initVerify(publicKey);
        signatureObject.update(signedMessage
                .getBytes(AlipayConstants.INPUT_CHARSET_VALUE));
        isValidSign = signatureObject.verify(Base64.decode(hash));

我正在正确登录到 HSM。在访问私钥时,我没有任何问题。Luna HSM是否有任何限制,只能通过证书访问公钥?

提前谢谢。

正确答案是>

LunaKey lk= LunaKey.LocateKeyByAlias("publicKeyName");

但建议在查询 HSM 之前使密钥持久化。

你有没有尝试过这样的事情:

final KeyStore keyStore = KeyStore.getInstance("Luna");
keyStore.load(null, null);
final Certificate certificate = keyStore.getCertificate(alias);
if (certificate == null) {
   throw new IllegalArgumentException(String.format("Certificate '%s' does not exists", alias));
}
final PublicKey publicKey = certificate.getPublicKey();
// TODO Working with the public key...

在Java密钥库中没有PublicKeyEntry,这就是您无法访问公钥的原因。

https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.Entry.html

Java的KeyStore.KeyEntry接口有三个实现类。

  1. 私钥入口
  2. 密钥条目
  3. 受信任的证书条目。

Java 希望您从证书中检索公钥。塞巴斯蒂安·范梅赫伦(Sebastien Vanmechelen)为您提供了如何做到这一点的完美例子。

如果碰巧您的Luna HSM分区没有X509证书,那么使用LunaKey是唯一的选择。

最新更新