Sample code for 30+ languages & platforms
PowerBuilder

UBL XAdES Enveloped Signature

See more XAdES Examples

Demonstrates how to create a UBL XAdES enveloped signature.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_XmlToSign
oleobject loo_Gen
oleobject loo_Object1
oleobject loo_Cert
oleobject loo_SbXml
oleobject loo_Verifier
integer li_NumSigs
integer li_VerifyIdx
integer li_Verified

li_Success = 0

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

li_Success = 1

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

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

loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")

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

// Create an Object to be added to the Signature.
loo_Object1 = create oleobject
li_rc = loo_Object1.ConnectToNewObject("Chilkat.Xml")

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

loo_Gen.AddObject("",loo_Object1.GetXml(),"","")

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

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

// Provide a certificate + private key. (PFX password is test123)
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Object1
    destroy loo_Cert
    return
end if

loo_Gen.SetX509Cert(loo_Cert,1)

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

// Load XML to be signed...
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

loo_XmlToSign.GetXmlSb(loo_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>
loo_Gen.Behaviors = "IndentedSignature,UBLDocumentSignatures"

// Sign the XML...
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Object1
    destroy loo_Cert
    destroy loo_SbXml
    return
end if

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

// Save the signed XML to a file.
li_Success = loo_SbXml.WriteFile("qa_output/signedXml.xml","utf-8",0)

Write-Debug loo_SbXml.GetAsString()

// ----------------------------------------
// Verify the signature we just produced...
loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.XmlDSig")

li_Success = loo_Verifier.LoadSignatureSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Object1
    destroy loo_Cert
    destroy loo_SbXml
    destroy loo_Verifier
    return
end if

// We should have just one signature..
li_NumSigs = loo_Verifier.NumSignatures
li_VerifyIdx = 0
do while li_VerifyIdx < li_NumSigs
    loo_Verifier.Selector = li_VerifyIdx
    li_Verified = loo_Verifier.VerifySignature(1)
    if li_Verified <> 1 then
        Write-Debug loo_Verifier.LastErrorText
        destroy loo_XmlToSign
        destroy loo_Gen
        destroy loo_Object1
        destroy loo_Cert
        destroy loo_SbXml
        destroy loo_Verifier
        return
    end if

    li_VerifyIdx = li_VerifyIdx + 1
loop

Write-Debug "The signature was successfully verified."


destroy loo_XmlToSign
destroy loo_Gen
destroy loo_Object1
destroy loo_Cert
destroy loo_SbXml
destroy loo_Verifier