Sample code for 30+ languages & platforms
Lua

UBL XAdES Enveloped Signature

See more XAdES Examples

Demonstrates how to create a UBL XAdES enveloped signature.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local success = 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

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

local gen = chilkat.XmlDSigGen.new()

gen.sig_location = "CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation"
gen.sig_location_mod = 0
gen.sig_id = "Signature-7f4c4719515a4a0a8ce4d1b983a2ec69"
gen.sig_namespace_prefix = ""
gen.sig_namespace_uri = "http://www.w3.org/2000/09/xmldsig#"
gen.signed_info_canon_alg = "C14N"
gen.signed_info_digest_method = "sha256"

-- Create an Object to be added to the Signature.
local object1 = chilkat.Xml.new()
object1.tag = "xades:QualifyingProperties"
object1:add_attribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#")
object1:add_attribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#")
object1:add_attribute("Target", "#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69")
object1:update_attr_at("xades:SignedProperties", true, "Id", "SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc")
object1:update_child_content("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
object1:update_attr_at("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod", true, "Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1")
object1:update_child_content("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue", "TO BE GENERATED BY CHILKAT")
object1:update_child_content("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2", "TO BE GENERATED BY CHILKAT")
object1:update_attr_at("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat", true, "ObjectReference", "")
object1:update_child_content("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description", "document")
object1:update_child_content("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType", "application/xml")
object1:update_child_content("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding", "UTF-8")

gen:add_object("", object1:get_xml(), "", "")

-- -------- Reference 1 --------
gen:add_same_doc_ref("", "sha256", "", "", "")

-- -------- Reference 2 --------
gen:add_object_ref("SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc", "sha256", "", "", "http://uri.etsi.org/01903#SignedProperties")

-- Provide a certificate + private key. (PFX password is test123)
local cert = chilkat.Cert.new()
success = cert:load_pfx_file("qa_data/pfx/cert_test123.pfx", "test123")
if not success then
    print(cert.last_error_text)
    return
end

gen:set_x509_cert(cert, true)

gen.key_info_type = "X509Data+KeyValue"
gen.x509_type = "IssuerSerial,SubjectName,SKI,Certificate"

-- Load XML to be signed...
local sbXml = chilkat.StringBuilder.new()
xmlToSign:get_xml_sb(sbXml)

-- 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>
gen.behaviors = "IndentedSignature,UBLDocumentSignatures"

-- Sign the XML...
success = gen:create_xml_dsig_sb(sbXml)
if not success then
    print(gen.last_error_text)
    return
end

-- -----------------------------------------------

-- Save the signed XML to a file.
success = sbXml:write_file("qa_output/signedXml.xml", "utf-8", false)

print(sbXml:get_as_string())

-- ----------------------------------------
-- Verify the signature we just produced...
local verifier = chilkat.XmlDSig.new()
success = verifier:load_signature_sb(sbXml)
if not success then
    print(verifier.last_error_text)
    return
end

-- We should have just one signature..
local numSigs = verifier.num_signatures
local verifyIdx = 0
while verifyIdx < numSigs do
    verifier.selector = verifyIdx
    local verified = verifier:verify_signature(true)
    if not verified then
        print(verifier.last_error_text)
        return
    end

    verifyIdx = verifyIdx + 1
end

print("The signature was successfully verified.")