javaxmlsignaturexades

XADES Signature Verification


I use below java code to verify XML signature using xades4j

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

But I get the below exception and it says digest values are not equal.

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

Below is the XML file ( I replaced the actual data with mock data)

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

I have two questions and trying to figure out.

  1. The approach I use for this XML document verifcation is correct?
  2. The reason of getting " xades4j.verification.ReferenceValueException: Reference '#xmldsig-xxxx-abc-object11' cannot be validated" and digest values are not matched.

Edit (14-11-22): I was able to resolve xades verification issue. Below are the two mistakes I did.

  1. Read the signed XML file from the disk. I think the file was altered or could not read correctly. Instead reading from the disk, I validate the string returned from the API directly.
  2. It turns out that I should use the root certificate for validation.

Now i'm trying to valide XMLDsig using the below code.

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

  

It gives me the below exception

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

Solution

  • I was able to find the answer for the xades verification. Below are the two mistakes I did.

    1. Read the signed XML file from the disk. I think the file was altered or could not read correctly. Instead reading from the disk, I validate the string returned from the API directly.
    2. It turns out that I should use the root certificate for validation.