无法将X509Certificate加载到XML数字签名中

时间:2016-03-22 20:26:11

标签: java xml digital-signature

我正在使用JDK 1.8.0_74。

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

但是,我只是想加载X509Certificate作为签名块的一部分,而不是为签名块的X509Data元素加载密钥库和密钥,这将产生如下内容:

<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);

我得到一个例外:

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)并将其加载到证书正文中,但你首先必须将证书转换为密钥库(这可能会有所帮助: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))));

1 个答案:

答案 0 :(得分:1)

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

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

Number of values?
>3 (for example)

Insert values:
4 6 9

别名一开始就对了:

<块引用>

keytool -v -list -keystore <jks file>

输入密钥库密码:

密钥库类型:JKS

密钥库类型:JKS 密钥库提供者:SUN

您的密钥库包含 1 个条目

别名:(*一堆数字*) *别名*