Sample code for 30+ languages & platforms
Lianja

UBL XAdES Enveloped Signature

See more XAdES Examples

Demonstrates how to create a UBL XAdES enveloped signature.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

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

llSuccess = .T.

// 
// 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

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

loGen = createobject("CkXmlDSigGen")

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

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

loGen.AddObject("",loObject1.GetXml(),"","")

// -------- Reference 1 --------
loGen.AddSameDocRef("","sha256","","","")

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

// Provide a certificate + private key. (PFX password is test123)
loCert = createobject("CkCert")
llSuccess = loCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if (llSuccess <> .T.) then
    ? loCert.LastErrorText
    release loXmlToSign
    release loGen
    release loObject1
    release loCert
    return
endif

loGen.SetX509Cert(loCert,.T.)

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

// Load XML to be signed...
loSbXml = createobject("CkStringBuilder")
loXmlToSign.GetXmlSb(loSbXml)

// 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>
loGen.Behaviors = "IndentedSignature,UBLDocumentSignatures"

// Sign the XML...
llSuccess = loGen.CreateXmlDSigSb(loSbXml)
if (llSuccess <> .T.) then
    ? loGen.LastErrorText
    release loXmlToSign
    release loGen
    release loObject1
    release loCert
    release loSbXml
    return
endif

// -----------------------------------------------

// Save the signed XML to a file.
llSuccess = loSbXml.WriteFile("qa_output/signedXml.xml","utf-8",.F.)

? loSbXml.GetAsString()

// ----------------------------------------
// Verify the signature we just produced...
loVerifier = createobject("CkXmlDSig")
llSuccess = loVerifier.LoadSignatureSb(loSbXml)
if (llSuccess <> .T.) then
    ? loVerifier.LastErrorText
    release loXmlToSign
    release loGen
    release loObject1
    release loCert
    release loSbXml
    release loVerifier
    return
endif

// We should have just one signature..
lnNumSigs = loVerifier.NumSignatures
lnVerifyIdx = 0
do while lnVerifyIdx < lnNumSigs
    loVerifier.Selector = lnVerifyIdx
    llVerified = loVerifier.VerifySignature(.T.)
    if (llVerified <> .T.) then
        ? loVerifier.LastErrorText
        release loXmlToSign
        release loGen
        release loObject1
        release loCert
        release loSbXml
        release loVerifier
        return
    endif

    lnVerifyIdx = lnVerifyIdx + 1
enddo

? "The signature was successfully verified."


release loXmlToSign
release loGen
release loObject1
release loCert
release loSbXml
release loVerifier