C# UBL 2.1 signature

4.1k Views Asked by At

I need to sign an UBL 2.1 Invoice using c#. The proble is that, after signing i need the Signature xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" to be embeded in

an UBLExtensions element, like so

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">

I have tried to add these elements at a later time, but the signature is not considered valid.

I don't have any experience signing XML files, so any help would be much apreciated

EDIT

The initial XML file is

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
   ... Invoice Elements
</Invoice>

I need to produce something like

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
   <ext:UBLExtensions>
      <ext:UBLExtension xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2">
         <ext:ExtensionContent>
            <sig:UBLDocumentSignatures>
               <sac:SignatureInformation>
                  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                     ... signature contents produced by signed xml class
                  </Signature>
               </sac:SignatureInformation>
            </sig:UBLDocumentSignatures>
         </ext:ExtensionContent>
      </ext:UBLExtension>
   </ext:UBLExtensions>
    ... Invoice Elements
</Invoice>

If I add these elements at a later time, the signature is considered invalid.

Again, any help will be highly appreciated.

The link to the actual signed file is this one: signed_xml

2

There are 2 best solutions below

1
MSantos On

The problem was that the extension elements must be placed on the document before the signing process. Shame on me! Thak you for your time!

0
Josip Sibenik On

How about this...

var nodes = xmlDoc.GetElementsByTagName("Invoice");
nodes(0).AppendChild(signature);
xmlDoc.Save(xmlFilePath);