Sample code for 30+ languages & platforms
AutoIt

UBL XAdES Enveloped Signature

See more XAdES Examples

Demonstrates how to create a UBL XAdES enveloped signature.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

$bSuccess = True

; 
; The following code creates the XML document to be signed.
; (It is also possible to simply load the XML into the Chilkat XML object by calling the LoadXml method.)

; A sample (already signed) of this XML is available here:  External link: credit-note-en16931-xades-signed.xml

; Also, you may use this online tool to generate code from sample XML: 
; Generate Code to Create XML

$oXmlToSign = ObjCreate("Chilkat.Xml")
$oXmlToSign.Tag = "CreditNote"
$oXmlToSign.AddAttribute("xmlns","urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2")
$oXmlToSign.AddAttribute("xmlns:cac","urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
$oXmlToSign.AddAttribute("xmlns:cbc","urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
$oXmlToSign.AddAttribute("xmlns:ext","urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
$oXmlToSign.UpdateAttrAt("ext:UBLExtensions|ext:UBLExtension",True,"xmlns:sac","urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2")
$oXmlToSign.UpdateAttrAt("ext:UBLExtensions|ext:UBLExtension",True,"xmlns:sig","urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2")
$oXmlToSign.UpdateChildContent "ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation",""
$oXmlToSign.UpdateChildContent "cbc:CustomizationID","urn:cen.eu:en16931:2017"
$oXmlToSign.UpdateChildContent "cbc:ProfileID","P1"
$oXmlToSign.UpdateChildContent "cbc:ID","c2ad1540-cf12-4e83-b47c-906aac70242e"
$oXmlToSign.UpdateChildContent "cbc:IssueDate","2009-12-15"
$oXmlToSign.UpdateAttrAt("cbc:Note",True,"languageID","en")
$oXmlToSign.UpdateChildContent "cbc:Note","Ordered in our booth at the convention."
$oXmlToSign.UpdateAttrAt("cbc:DocumentCurrencyCode",True,"listAgencyID","6")
$oXmlToSign.UpdateAttrAt("cbc:DocumentCurrencyCode",True,"listID","ISO 4217 Alpha")
$oXmlToSign.UpdateChildContent "cbc:DocumentCurrencyCode","HRK"
$oXmlToSign.UpdateChildContent "cbc:AccountingCost","Project cost code 123"
$oXmlToSign.UpdateChildContent "cac:InvoicePeriod|cbc:StartDate","2009-11-01"
$oXmlToSign.UpdateChildContent "cac:InvoicePeriod|cbc:EndDate","2009-11-30"
$oXmlToSign.UpdateChildContent "cac:OrderReference|cbc:ID","123"
$oXmlToSign.UpdateChildContent "cac:ContractDocumentReference|cbc:ID","Contract321"
$oXmlToSign.UpdateChildContent "cac:ContractDocumentReference|cbc:DocumentType","Framework agreement"
$oXmlToSign.UpdateChildContent "cac:AdditionalDocumentReference|cbc:ID","Doc1"
$oXmlToSign.UpdateChildContent "cac:AdditionalDocumentReference|cbc:DocumentType","Timesheet"
$oXmlToSign.UpdateChildContent "cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI","http://www.suppliersite.eu/sheet001.html"
$oXmlToSign.UpdateChildContent "cac:AdditionalDocumentReference[1]|cbc:ID","Doc2"
$oXmlToSign.UpdateChildContent "cac:AdditionalDocumentReference[1]|cbc:DocumentType","Drawing"
$oXmlToSign.UpdateAttrAt("cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject",True,"mimeCode","application/pdf")
$oXmlToSign.UpdateChildContent "cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject","UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID","9934:18683136487::HR99:276"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName","KATANCICEVA"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName","ZAGREB"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone","10000"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode","HR"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID","HR18683136487"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID","FRE"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName","MINISTARSTVO FINANCIJA"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name","JURAJ MARKOVIC"
$oXmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail","juraj.markovic@fina.hr"
$oXmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID","9934:49811265576::HR99:NOVO1"
$oXmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName","GETALDICEVA 4"
$oXmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName","ZAGREB"
$oXmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone","10000"
$oXmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode","HR"
$oXmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID","HR49811265576"
$oXmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID","VAT"
$oXmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName","NOVO1"
$oXmlToSign.UpdateChildContent "cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode","HR"
$oXmlToSign.UpdateChildContent "cac:PaymentMeans|cbc:PaymentMeansCode","30"
$oXmlToSign.UpdateChildContent "cac:PaymentMeans|cbc:InstructionNote","Neki opis placanja"
$oXmlToSign.UpdateChildContent "cac:PaymentMeans|cbc:PaymentID","HR00 12456"
$oXmlToSign.UpdateChildContent "cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID","HR1210010051863000160"
$oXmlToSign.UpdateChildContent "cac:PaymentTerms|cbc:Note","Neki uvjeti placanja"
$oXmlToSign.UpdateAttrAt("cac:TaxTotal|cbc:TaxAmount",True,"currencyID","HRK")
$oXmlToSign.UpdateChildContent "cac:TaxTotal|cbc:TaxAmount","25.00"
$oXmlToSign.UpdateAttrAt("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount",True,"currencyID","HRK")
$oXmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount","100.00"
$oXmlToSign.UpdateAttrAt("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount",True,"currencyID","HRK")
$oXmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount","25.00"
$oXmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID","S"
$oXmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent","25"
$oXmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID","VAT"
$oXmlToSign.UpdateAttrAt("cac:LegalMonetaryTotal|cbc:LineExtensionAmount",True,"currencyID","HRK")
$oXmlToSign.UpdateChildContent "cac:LegalMonetaryTotal|cbc:LineExtensionAmount","100.00"
$oXmlToSign.UpdateAttrAt("cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount",True,"currencyID","HRK")
$oXmlToSign.UpdateChildContent "cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount","100.00"
$oXmlToSign.UpdateAttrAt("cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount",True,"currencyID","HRK")
$oXmlToSign.UpdateChildContent "cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount","125.00"
$oXmlToSign.UpdateAttrAt("cac:LegalMonetaryTotal|cbc:PayableAmount",True,"currencyID","HRK")
$oXmlToSign.UpdateChildContent "cac:LegalMonetaryTotal|cbc:PayableAmount","125.00"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cbc:ID","1"
$oXmlToSign.UpdateAttrAt("cac:CreditNoteLine|cbc:CreditedQuantity",True,"unitCode","H87")
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cbc:CreditedQuantity","1.000"
$oXmlToSign.UpdateAttrAt("cac:CreditNoteLine|cbc:LineExtensionAmount",True,"currencyID","HRK")
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cbc:LineExtensionAmount","100.00"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cbc:Description","Neki detaljniji opis proizvoda ide ovdje"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cbc:Name","Neki proizvod"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode","HR"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID","S"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent","25"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID","VAT"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name","Boja"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value","Plava"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name","Masa"
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value","1,2 Kg"
$oXmlToSign.UpdateAttrAt("cac:CreditNoteLine|cac:Price|cbc:PriceAmount",True,"currencyID","HRK")
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Price|cbc:PriceAmount","100.000000"
$oXmlToSign.UpdateAttrAt("cac:CreditNoteLine|cac:Price|cbc:BaseQuantity",True,"unitCode","H87")
$oXmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Price|cbc:BaseQuantity","1.000"

; Use this online tool to generate XAdES code from a sample signed XML: 
; Generate Code to Creaet XAdES from Sample XAdES

; The following code was generated by the online tool.

$oGen = ObjCreate("Chilkat.XmlDSigGen")

$oGen.SigLocation = "CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation"
$oGen.SigLocationMod = 0
$oGen.SigId = "Signature-7f4c4719515a4a0a8ce4d1b983a2ec69"
$oGen.SigNamespacePrefix = ""
$oGen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
$oGen.SignedInfoCanonAlg = "C14N"
$oGen.SignedInfoDigestMethod = "sha256"

; Create an Object to be added to the Signature.
$oObject1 = ObjCreate("Chilkat.Xml")
$oObject1.Tag = "xades:QualifyingProperties"
$oObject1.AddAttribute("xmlns:ds","http://www.w3.org/2000/09/xmldsig#")
$oObject1.AddAttribute("xmlns:xades","http://uri.etsi.org/01903/v1.3.2#")
$oObject1.AddAttribute("Target","#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69")
$oObject1.UpdateAttrAt("xades:SignedProperties",True,"Id","SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc")
$oObject1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT"
; Note: It may be that http://www.w3.org/2001/04/xmlenc#sha256 is needed in the following line instead of http://www.w3.org/2000/09/xmldsig#sha1
$oObject1.UpdateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod",True,"Algorithm","http://www.w3.org/2000/09/xmldsig#sha1")
$oObject1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT"
$oObject1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2","TO BE GENERATED BY CHILKAT"
$oObject1.UpdateAttrAt("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat",True,"ObjectReference","")
$oObject1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description","document"
$oObject1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType","application/xml"
$oObject1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding","UTF-8"

$oGen.AddObject("",$oObject1.GetXml(),"","")

; -------- Reference 1 --------
$oGen.AddSameDocRef("","sha256","","","")

; -------- Reference 2 --------
$oGen.AddObjectRef("SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc","sha256","","","http://uri.etsi.org/01903#SignedProperties")

; Provide a certificate + private key. (PFX password is test123)
$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
If ($bSuccess <> True) Then
    ConsoleWrite($oCert.LastErrorText & @CRLF)
    Exit
EndIf

$oGen.SetX509Cert($oCert,True)

$oGen.KeyInfoType = "X509Data+KeyValue"
$oGen.X509Type = "IssuerSerial,SubjectName,SKI,Certificate"

; Load XML to be signed...
$oSbXml = ObjCreate("Chilkat.StringBuilder")
$oXmlToSign.GetXmlSb($oSbXml)

; Adding the "UBLDocumentSignatures" behavior causes the following Transform to be used for the 
; first Reference:   
;   <Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116"><XPath>count(ancestor-or-self::sig:UBLDocumentSignatures | here()/ancestor::sig:UBLDocumentSignatures[1]) &gt; count(ancestor-or-self::sig:UBLDocumentSignatures)</XPath></Transform>
$oGen.Behaviors = "IndentedSignature,UBLDocumentSignatures"

; Sign the XML...
$bSuccess = $oGen.CreateXmlDSigSb($oSbXml)
If ($bSuccess <> True) Then
    ConsoleWrite($oGen.LastErrorText & @CRLF)
    Exit
EndIf

; -----------------------------------------------

; Save the signed XML to a file.
$bSuccess = $oSbXml.WriteFile("qa_output/signedXml.xml","utf-8",False)

ConsoleWrite($oSbXml.GetAsString() & @CRLF)

; ----------------------------------------
; Verify the signature we just produced...
$oVerifier = ObjCreate("Chilkat.XmlDSig")
$bSuccess = $oVerifier.LoadSignatureSb($oSbXml)
If ($bSuccess <> True) Then
    ConsoleWrite($oVerifier.LastErrorText & @CRLF)
    Exit
EndIf

; We should have just one signature..
Local $iNumSigs = $oVerifier.NumSignatures
Local $iVerifyIdx = 0
While $iVerifyIdx < $iNumSigs
    $oVerifier.Selector = $iVerifyIdx
Local $bVerified = $oVerifier.VerifySignature(True)
    If ($bVerified <> True) Then
        ConsoleWrite($oVerifier.LastErrorText & @CRLF)
        Exit
    EndIf

    $iVerifyIdx = $iVerifyIdx + 1
Wend

ConsoleWrite("The signature was successfully verified." & @CRLF)