提问者:小点点

XADES签名验证


我使用下面java代码来验证使用xades4jXML签名

    NodeList nl = doc.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature");

    FileSystemDirectoryCertStore certStore = new
            FileSystemDirectoryCertStore("cert/store/");
    KeyStore ks;
    try (FileInputStream fis = new FileInputStream("cert/store/myStore")) {
        ks = KeyStore.getInstance("jks");
        ks.load(fis, "password".toCharArray());
    } 
    CertificateValidationProvider provider = new PKIXCertificateValidationProvider( ks, false,certStore.getStore() );

    XadesVerificationProfile profile = new XadesVerificationProfile(provider);
    org.w3c.dom.Element sigElem = (org.w3c.dom.Element) nl.item(0);
    XAdESVerificationResult r = profile.newVerifier().verify(sigElem, null);

但是我得到了下面的例外,它说摘要值不相等。

xades4j.verification.ReferenceValueException: Reference '#xmldsig-xxxx-abc-object11' cannot be validated

下面是XML文件(我用mock数据替换了实际数据)

    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig-abc">
    <ds:SignedInfo>
        <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
        <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
        <ds:Reference Id="xmldsig-aee6fb07-8566-47eb-acb7-abc-ref0"
                      Type="http://www.w3.org/2000/09/xmldsig#Object"
                      URI="#xmldsig-xxx-abc-object11">
            <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
            <ds:DigestValue>XXXX</ds:DigestValue>
        </ds:Reference>
        <ds:Reference Type="http://uri.etsi.org/01903#SignedProperties"
                      URI="#xmldsig-xxx-abc-signedprops">
            <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
            <ds:DigestValue>XXXXX</ds:DigestValue>
        </ds:Reference>
    </ds:SignedInfo>
    <ds:SignatureValue Id="xmldsig-xxxx-sigvalue">XXXXX
    </ds:SignatureValue>
    <ds:KeyInfo>
        <ds:X509Data>
            <ds:X509Certificate>xXxxxxxx
            </ds:X509Certificate>
            <ds:X509IssuerSerial>
                <ds:X509IssuerName>xxxxx</ds:X509IssuerName>
                <ds:X509SerialNumber>xxxx</ds:X509SerialNumber>
            </ds:X509IssuerSerial>
            <ds:X509SubjectName>xxxxx
            </ds:X509SubjectName>
        </ds:X509Data>
    </ds:KeyInfo>
    <ds:Object Id="xmldsig-xxx-abc-object11">
        <ns2:Vehicle xmlns:ns2="xx:xx"
                            xmlns="xx:xx"> 
                            .................................
        </ns2:Vehicle>
    </ds:Object>
    <ds:Object>
        <xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#"
                                    xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#"
                                    Target="#xmldsig-xxx-abc">
            <xades:SignedProperties Id="xmldsig-xxx-abc-signedprops">
                <xades:SignedSignatureProperties>
                    <xades:SigningTime>xxxxxxx</xades:SigningTime>
                    <xades:SigningCertificate>
                        <xades:Cert>
                            <xades:CertDigest>
                                <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                                <ds:DigestValue>xxxxxxxxxx</ds:DigestValue>
                            </xades:CertDigest>
                            <xades:IssuerSerial>
                                <ds:X509IssuerName>xxxxxxxxxxx
                                </ds:X509IssuerName>
                                <ds:X509SerialNumber>xxxxxxxxxxx</ds:X509SerialNumber>
                            </xades:IssuerSerial>
                        </xades:Cert>
                        <xades:Cert>
                            <xades:CertDigest>
                                <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                                <ds:DigestValue>xxxxxxxxx</ds:DigestValue>
                            </xades:CertDigest>
                            <xades:IssuerSerial>
                                <ds:X509IssuerName>xxxxxxxxxx
                                </ds:X509IssuerName>
                                <ds:X509SerialNumber>xxxxxxxxxxxx</ds:X509SerialNumber>
                            </xades:IssuerSerial>
                        </xades:Cert>
                        <xades:Cert>
                            <xades:CertDigest>
                                <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                                <ds:DigestValue>xxxxxxxxx</ds:DigestValue>
                            </xades:CertDigest>
                            <xades:IssuerSerial>
                                <ds:X509IssuerName>xxxxxxxxxxxx
                                </ds:X509IssuerName>
                                <ds:X509SerialNumber>xxxxxxxxxxxx</ds:X509SerialNumber>
                            </xades:IssuerSerial>
                        </xades:Cert>
                    </xades:SigningCertificate>
                </xades:SignedSignatureProperties>
                <xades:SignedDataObjectProperties>
                    <xades:CommitmentTypeIndication>
                        <xades:CommitmentTypeId>
                            <xades:Identifier>http://uri.etsi.org/01903/v1.2.2#ProofOfOrigin</xades:Identifier>
                            <xades:Description>xxxxxxxxxxxxx
                            </xades:Description>
                        </xades:CommitmentTypeId>
                        <xades:AllSignedDataObjects/>
                    </xades:CommitmentTypeIndication>
                </xades:SignedDataObjectProperties>
            </xades:SignedProperties>
        </xades:QualifyingProperties>
    </ds:Object>
</ds:Signature>

我有两个问题,试图弄清楚。

  1. 我用于XML文档验证的方法是否正确?
  2. 获取“xades4j. verfication.RecentceValueException:引用'#xmldsig-xxxx-abc-object t11'无法验证”和摘要值不匹配的原因。

编辑(14-11-22):我能够解决xade验证问题。以下是我犯的两个错误。

  1. 从磁盘读取签名的XML文件。我认为该文件已被更改或无法正确读取。而是从磁盘读取,我直接验证从API返回的字符串。
  2. 原来我应该使用根证书进行验证。

现在我正在尝试使用以下代码验证XMLDsig。

    NodeList nl = doc.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature");
DOMValidateContext valContext = new DOMValidateContext(new KeyValueKeySelector(), nl.item(0));
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
 XMLSignature signature = fac.unmarshalXMLSignature(valContext);
signature.validate(valContext);

  

它给了我以下例外

om.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID xmldsig-xxx-abc-signedprops

共1个答案

匿名用户

我能够找到xade验证的答案。下面是我犯的两个错误。

  1. 从磁盘读取签名的XML文件。我认为该文件已被更改或无法正确读取。而是从磁盘读取,我直接验证从API返回的字符串。
  2. 原来我应该使用根证书进行验证。