gpt4 book ai didi

java - 无法将 X509 证书加载到 XML 数字签名中

转载 作者:行者123 更新时间:2023-12-04 18:00:24 34 4
gpt4 key购买 nike

我正在使用 JDK 1.8.0_74。

我有基于标准做事方式(例如 http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html)创建 XML 数字签名的代码。

但是,我不想为签名 block 的 X509Data 元素加载 key 存储和 key ,我只想加载 X509Certificate 作为签名 block 的一部分,这会产生如下内容:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="#body">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>d6xssQEvw9mMXEbQ1+/jpYTFbqY=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>HwQ5o5RzCauJSIcyGyzPlIJHMYtaA2spBmnRvTmcL0S+bfb/UovUwBAn7WAKckUH
Qv0TuRMMZG3xaV5h4tdrW3hgSw1wZFfEG9cxViz6cr7FOTOEfOAjtU3M8v2/f21i
4o7w5ZORwAlUONamQ0C9x5CNccvNZln5vrpdcL+vqSc=</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<X509Certificate xmlns="http://www.w3.org/2000/09/xmldsig#">MIIC9TCCAl6gAwIBAgICFNAwDQYJKoZIhvcNAQEEBQAwgYcxCzAJBgNVBAYTAlpB
MSIwIAYDVQQIExlGT1IgVEVTVElORyBQVVJQT1NFUyBPTkxZMR0wGwYDVQQKExRU
aGF3dGUgQ2VydGlmaWNhdGlvbjEXMBUGA1UECxMOVEVTVCBURVNUIFRFU1QxHDAa
BgNVBAMTE1RoYXd0ZSBUZXN0IENBIFJvb3QwHhcNMDUwNTI2MTkxNzE2WhcNMDUw
NjE2MTkxNzE2WjCBkTELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNBTElGT1JOSUEx
EjAQBgNVBAcTCVNBTiBQRURSTzEPMA0GA1UEChMGQURWRU5UMR8wHQYDVQQLFBZS
RVNFQVJDSCAmIERFVkVMT1BNRU5UMScwJQYDVQQDEx5hZHZjZW50cmFsLmFkdmVu
dHJlc291cmNlcy5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANQQh/K9
GBezwbZuES/zX1exs+9/RDGH2Gv5b5mzSsytg4AY/gYN65tbL5PT/ZsfMs0QYpl0
KGxJFnZ5HHlR/90Ut86PhQPsVsr3S0iWJCrPHaPE8yWt4MKWP/5ERIf1C11cvRvh
bYmrIab5+bxOhYzci3t7aRBf2Er6m89Fd5SjAgMBAAGjZDBiMAwGA1UdEwEB/wQC
MAAwMwYDVR0fBCwwKjAooCagJIYiaHR0cDovL3d3dy50aGF3dGUuY29tL3Rlc3Rj
ZXJ0LmNybDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwDQYJKoZIhvcN
AQEEBQADgYEAJz3h8DT7kTvCPVhxOpRV1pE31QQaf4kD5/qI0D9yRYck2RkLH+ow
U1OQODhRyAkJUhbjfkWJPnjJUoYTmtlJioWq0r6lYJPv9QKgg4LwMo/RGvhdfHMc
83/Q5HSKWdat3US79R8K6FALTq3Ij7m4nJZZ6Zi+Ev8JBsSGsedSt+o=</X509Certificate>
<X509IssuerSerial xmlns="http://www.w3.org/2000/09/xmldsig#">
<X509IssuerName>CN=Thawte Test CA Root,OU=TEST TEST TEST,O=Thawte Certification,ST=FOR TESTING PURPOSES ONLY,C=ZA</X509IssuerName>
<X509SerialNumber>5328</X509SerialNumber>
</X509IssuerSerial>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>

(顺便说一句,这是我之前验证过的有效签名。)

现在,我的代码看起来像这样:

javax.xml.crypto.dsig.keyinfo.KeyInfoFactory kif = fac.getKeyInfoFactory();
List x509Content = new ArrayList();

X509Certificate certObj1 = null;

try {
InputStream fis = this.getClass().getClassLoader().getResourceAsStream("test.cert");
certObj1 = (X509Certificate)(X509Certificate.getInstance(fis));
fis.close();
fis = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (javax.security.cert.CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

x509Content.add(certObj1);
x509Content.add(certObj1.getSubjectDN().getName());

javax.xml.crypto.dsig.keyinfo.X509Data xd = kif.newX509Data(x509Content);
javax.xml.crypto.dsig.keyinfo.KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));

但是,当这一行被执行时:

javax.xml.crypto.dsig.keyinfo.X509Data xd = kif.newX509Data(x509Content);

我得到一个异常(exception):

java.lang.ClassCastException: content[0] is not a valid X509Data type
at org.jcp.xml.dsig.internal.dom.DOMX509Data.<init>(DOMX509Data.java:90)
at org.jcp.xml.dsig.internal.dom.DOMKeyInfoFactory.newX509Data(DOMKeyInfoFactory.java:106)
at com.abc.service.TestService.generateSignature(TestService.java:33...

查看了相应的代码(包括在线代码和反编译的实际 .class 文件),它在 DOMX509Data 构造函数的这一部分失败了:

if (x509Type instanceof String) {
new X500Principal((String)x509Type);
} else if (!(x509Type instanceof byte[]) &&
!(x509Type instanceof X509Certificate) &&
!(x509Type instanceof X509CRL) &&
!(x509Type instanceof XMLStructure)) {
throw new ClassCastException
("content["+i+"] is not a valid X509Data type");
}

但是,如果我在自己的代码中复制/粘贴并重新创建相同的代码,则永远不会发生该异常!

对于我的生活,我无法弄清楚为什么会这样。我已经检查过 content[0] 的类型是 com.sun.security.cert.internal.x509.X509V1CertImpl ,它肯定扩展了 X509Certificate.

我做错了什么?我希望我忽略了某些事情,或者我正在做一些完全违法的事情。

如有任何帮助,我们将不胜感激。

提前致谢。

更新

虽然我仍然想知道这是为什么,但在感兴趣的时间里,我已经按照此处的示例 (http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html) 将其加载到证书主体中,但您首先必须将证书到 keystore (这可能有帮助:importing an existing x509 certificate and private key in Java keystore to use in ssl)。

此外,如果您只想加载序列信息而不是证书主体本身,请尝试这样的操作:

String dn = cert.getIssuerDN().toString();
BigInteger sn = cert.getSerialNumber();
X509IssuerSerial xd = kif.newX509IssuerSerial(dn, sn);

// next commented line was the original, replaced with the above line
//javax.xml.crypto.dsig.keyinfo.X509Data xd = kif.newX509Data(x509Content);
javax.xml.crypto.dsig.keyinfo.KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kif.newX509Data(Collections.singletonList(xd))));

最佳答案

我知道这有点晚了,但有一天可能会对某人有所帮助。

我遇到了同样的错误,在我的例子中,我传递了错误的别名。要查找别名,请在终端 (Windows) 上执行此命令:

keytool -v -list -keystore <jks file>

别名就在开头:

keytool -v -list -keystore keystore.jks

Enter keystore password:

Keystore type: JKS

Keystore type: JKS Keystore provider: SUN

Your keystore contains 1 entry

Alias name: (*bunch of numbers*) *alias name*

关于java - 无法将 X509 证书加载到 XML 数字签名中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36164499/

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