Search code examples
javaxmlxades4j

How do I put a Type attribute in the Reference tag when signing a xml file?


I use the following code to sign an xml file:

public void firmar(String rutaAlXml, String rutaAlXmlFirmado, PrivateKey key, Provider p, Certificate[] chain, PublicKey llavePublica) 
{    
    File fXmlFile = new File(rutaAlXml);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);    
    DOMSignContext dsc = new DOMSignContext(key, doc.getDocumentElement());

    XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
    javax.xml.crypto.dsig.Reference ref = fac.newReference(
        "", 
        fac.newDigestMethod(DigestMethod.SHA1, null), 
        Collections.singletonList(
        fac.newTransform(Transform.ENVELOPED, 
        (TransformParameterSpec) null)), 
        null, null);

    SignedInfo si = fac.newSignedInfo(
                    fac.newCanonicalizationMethod(
                    CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, 
                    (C14NMethodParameterSpec) null), 
                    fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), 
                    Collections.singletonList(ref));

    KeyInfoFactory kif = fac.getKeyInfoFactory();
    KeyValue kv = kif.newKeyValue(llavePublica);
    KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
    XMLSignature signature = fac.newXMLSignature(si, ki);

    signature.sign(dsc);    
    OutputStream os = new FileOutputStream(rutaAlXmlFirmado);

    TransformerFactory tf = TransformerFactory.newInstance();
    javax.xml.transform.Transformer trans = tf.newTransformer();
    trans.transform(new DOMSource(doc), new StreamResult(os));
}

The resulting xml file is valid and looks like this:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><root>
<nombre>cesar</nombre>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#" Id="EMPRESA"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><DigestValue>digest goes here</DigestValue></Reference></SignedInfo><SignatureValue>signature goes here</SignatureValue><KeyInfo><X509Data><X509SubjectName>subject's goes here</X509SubjectName><X509Certificate>certificate goes here</X509Certificate></X509Data></KeyInfo></Signature></root>

My problem happens when I try to validate my xml file with Xades4j. I'm using the code in this link:

XadesVerificationProfile p = new XadesVerificationProfile(certValidator);
XadesVerifier verifier = p.newVerifier();
XAdESVerificationResult r = null;

SignatureSpecificVerificationOptions ssvo = 
new SignatureSpecificVerificationOptions();
ssvo.useDataForAnonymousReference(xmlFile);
Element sigElem = (Element) nl.item(0); //Which contains the complete signature segment from the xml       
r = verifier.verify(sigElem, ssvo); // this is the problematic line

When I try to verify the signature, I get the following exception:

Exception in thread "main" xades4j.verification.QualifyingPropertiesIncorporationException: SignedProperties reference not found
    at xades4j.verification.SignatureUtils.processReferences(SignatureUtils.java:230)
    at xades4j.verification.XadesVerifierImpl.verify(XadesVerifierImpl.java:132)
    at myCode.main(myCode.java:11)
Java Result: 1

I read here this is caused because there's no Type attribute in the Reference tag.

I searched on Internet, and I couldn't find how to put a valid Type attribute in the Reference tag. How can I do this to validate my xml file?


Solution

  • Your code produces a simple XML-DSIG singature. Xades4j is intended for advanced XML signatures (XAdES) and is not capable of verifying simple XML-DSIGs.

    The question you mentioned refers to a particular Reference element that should be added on XAdES, in addition to your data object references. This reference covers a specific part of the signature that contains signed properties.

    Nevertheless, the Type is actually one of the null arguments you're passing into the newReference call.