gpt4 book ai didi

java - 如何使用智能卡对 jar 进行签名

转载 作者:行者123 更新时间:2023-11-30 08:09:01 25 4
gpt4 key购买 nike

我在工作中使用的是 PKCS11 智能卡,我想使用 jarsigner 使用我卡上的证书对 jar 文件进行签名。

我主要在 Linux 上工作。 Coolkey 可以看到卡片。

Oracle's documentation提到智能卡:

jarsigner -keystore NONE -storetype PKCS11 \
-providerName SunPKCS11-SmartCard \
-list

但是显然没有那个名字的实际提供者,而且 jarsigner 的 -list 参数似乎不存在。

我终于让 jarsigner 看到了卡片,但它报告说它不是有效条目并且没有私钥:

jarsigner -keystore NONE -storetype PKCS11 \
-providerClass sun.security.pkcs11.SunPKCS11 \
-providerArg smartcard.config \
-storepass notmyrealpass \
myjarfile.jar 'Identity #0'

jarsigner: Certificate chain not found for: Identity #0.
Identity #0 must reference a valid KeyStore key entry
containing a private key and corresponding public key
certificate chain.

smartcard.config 在哪里:

name=Kittens
library=/usr/lib/pkcs11/libcoolkeypk11.so

我通过在 Java 中加载卡片,以编程方式获得了别名列表 ["Identity #0"的来源]:

String conf = "name=Kittens\nlibrary=/usr/lib/pkcs11/libcoolkeypk11.so";
InputStream s = new ByteArrayInputStream(conf.getBytes());
Provider p = new sun.security.pkcs11.SunPKCS11(s);
KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
keyStore.load(null, "notmyrealpass".toCharArray());
Enumeration<String> aliases = keyStore.aliases();
// Aliases contains "Identity #0", "Identity #1", "Identity #2"

这些智能卡的全部意义在于私钥保留在卡上,而卡就是签名;有没有办法让 jarsigner 做我想做的事?

编辑:

经过更多的摸索,我注意到我似乎无法使用 SHA1withDSA,只能使用 SHA1withRSA:

Set<Provider.Service> services = p.getServices();
for(Provider.Service service : services) {
System.out.println(service.getAlgorithm());
}

打印此列表:SHA512withRSA、SHA256withRSA、SHA1withRSA、MD5withRSA、RSA/ECB/PKCS1Padding、SHA384withRSA、MD2withRSA、RSA、PKCS11

但我注意到签名的 jar 似乎都使用 DSA;这可能是问题所在吗?

最佳答案

我将 -sigalg 添加到可用的算法 [在本例中为 SHA256withRSA],这很有帮助。还有:

jarsigner: This jar contains entries whose certificate chain is not validated

我偶然混合了来自几个不同 JDK 的二进制文件。

所以,我使用的最终命令行是:

jarsigner \
-tsa http://timestamp.digicert.com \
-keystore NONE \
-storetype PKCS11 \
-providerClass sun.security.pkcs11.SunPKCS11 \
-providerArg card_linux.config \
-storepass `cat ~/cardpass` \
-sigalg SHA256withRSA \
dist/sup2rtam.jar \
'Identity #0'

其中 ~/cardpass 是一个只包含 notmyrealpass 的文件,而 card_linux.config 是

name=CAC
library=/usr/lib/pkcs11/libcoolkeypk11.so

关于java - 如何使用智能卡对 jar 进行签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32771909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com