0
votes

I'm generating a XML Xades signature. I need to add the namespace http://uri.etsi.org/01903/v1.3.2# in the TAG signature.

If I add this TAG after signing the doc, I get invalid signature error.

I don't know how to add it before, because I only have the TAG Signature after signing.´

This is the code to add namespace

void addNamespace(doc)
{

    NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");

    Node a = nl.item(0);

    Element sig = (Element)a;

sig.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:etsi","http://uri.etsi.org/01903/v1.3.2#");

}

The code for signature:

signContext = new DOMSignContext(pk, parentElement);
signContext.putNamespacePrefix("http://uri.etsi.org/01903/v1.3.2#","etsi");
XMLSignature signature;
signature.sign(signContext);
addNamespace(doc);

The XML:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="Signature-830342">

 <ds:SignedInfo Id="SignedInfo-830342">
 <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
 <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
 <ds:Reference URI="">
 <ds:Transforms>
 <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
 </ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>tKDaVHCywRrFbblaDIKZjUviXkI=</ds:DigestValue>
</ds:Reference>
<ds:Reference Type="http://uri.etsi.org/01903#SignedProperties" URI="#SignedProperties-830342">
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>M/coSDm1tqC4DKkbCyXUP82fB58=</ds:DigestValue>
</ds:Reference>
<ds:Reference URI="#KeyInfo-830342">
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>pInn5xZepngScAKAse0zZPuhyNU=</ds:Digest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w HQYDVR0OBBYEFCc/oVSDFtH46U81hILH78cwLnR9MB8GA1UdIwQYMBaAFLIkRa3EwPkvEabba+7h fY4FDjr9MA0GCSqGSIb3DQEBBQUAA4IBAQB1TpQ66zJL+69iooqSQIryzLykWqcXeVeJOAWbGwbP x2P5LVboqXRxj5j9jmlTyqQLRoHLS5BaExq+P+2sPd/BOU2/QV9yrv7uAFkxyQjcMip8eqj1pc0X gqW+JoIkeeqa+UWj+fVQg/YqHsFNgkSoxCf4aPWC2x5X33SH6D8QkXQw4FJcRppfmr4ljsbPmuvV V1uk34I6PTb3gRlVIiTvUWtRaOLQ0lDlFa5bO5rd9nQV7UsK2e7ghXZpUtdW6OAYIuP3ASweOlE1 6z2DiMlYMGJ8Ci/vdcG1eEhVQoV0+z2s78ybJXelTiuxHvuJLPTNk5eoG5zAOAz8oFoQi+51
</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
<ds:Object Id="Signature20257-data545881">
<xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#"Target="#Signature-830342">
<xades:SignedProperties Id="SignedProperties-830342">
<xades:SignedSignatureProperties>
<xades:SigningTime>2014-07-08T12:40:02+02:00</xades:SigningTime>
<xades:SigningCertificate>
<xades:Cert>
<xades:CertDigest>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>ivK7JSJgkt1YHLuXeUphxagMGcI=</ds:DigestValue>
</xades:CertDigest>
</xades:Cert>
</xades:SigningCertificate>
<xades:SignaturePolicyIdentifier>
<xades:SignaturePolicyId>
<xades:SigPolicyId>
<xades:Identifier>
http://www.facturae.es/politica_de_firma_formato_facturae/politica_de_firma_formato_facturae_v3_1.pdf
</xades:Identifier>
<xades:Description>facturae31</xades:Description>
</xades:SigPolicyId>
<xades:SigPolicyHash>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>Ohixl6upD6av8N7pEvDABhEL6hM=</ds:DigestValue>
</xades:SigPolicyHash>
</xades:SignaturePolicyId>
</xades:SignaturePolicyIdentifier>
<xades:SignerRole>
<xades:ClaimedRoles>
<xades:ClaimedRole>supplier</xades:ClaimedRole>
</xades:ClaimedRoles>
</xades:SignerRole>
</xades:SignedSignatureProperties>
<xades:SignedDataObjectProperties/>
</xades:SignedProperties>
<xades:UnsignedProperties/>
</xades:QualifyingProperties>
</ds:Object>
</ds:Signature>
</note>

I need that namespace will be in the TAG Signature instead of the TAG Object

1
XMLSignature signature; signature.sign(signContext); should fail, shouldn't itmkl
@mkl I don't understand what you mean. This is not failing right now. The signature method is involving the hash of the doc, put the references... and I need to set the namespace in the middle of signature processTimeStron
I don't understand what you mean - i meant to say that the code as present in your question calls sign of an unassigned variable. As it is not failing, i assume that you simply did not provide the assignment of the signature object.mkl
Yes, you're right, but doesn't matter this assignment to solve the question.TimeStron
Ok. Can you provide a sample signed document before and after that addNamespace call? Furthermore, which verifier gives you that invalid signature error?mkl

1 Answers

0
votes

Namespaces are used to calculate hash value. Canonicalization shoud add them to document (additionally in alphabetical order). Adding namespaces to signed document obviously will change its hash