DataFlex
DataFlex
UBL XAdES Enveloped Signature
See more XAdES Examples
Demonstrates how to create a UBL XAdES enveloped signature.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoXmlToSign
Handle hoGen
Handle hoObject1
Variant vCert
Handle hoCert
Variant vSbXml
Handle hoSbXml
Handle hoVerifier
Integer iNumSigs
Integer iVerifyIdx
Boolean iVerified
String sTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Move True To iSuccess
//
// 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
Get Create (RefClass(cComChilkatXml)) To hoXmlToSign
If (Not(IsComObjectCreated(hoXmlToSign))) Begin
Send CreateComObject of hoXmlToSign
End
Set ComTag Of hoXmlToSign To "CreditNote"
Get ComAddAttribute Of hoXmlToSign "xmlns" "urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2" To iSuccess
Get ComAddAttribute Of hoXmlToSign "xmlns:cac" "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" To iSuccess
Get ComAddAttribute Of hoXmlToSign "xmlns:cbc" "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" To iSuccess
Get ComAddAttribute Of hoXmlToSign "xmlns:ext" "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" To iSuccess
Get ComUpdateAttrAt Of hoXmlToSign "ext:UBLExtensions|ext:UBLExtension" True "xmlns:sac" "urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" To iSuccess
Get ComUpdateAttrAt Of hoXmlToSign "ext:UBLExtensions|ext:UBLExtension" True "xmlns:sig" "urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation" ""
Send ComUpdateChildContent To hoXmlToSign "cbc:CustomizationID" "urn:cen.eu:en16931:2017"
Send ComUpdateChildContent To hoXmlToSign "cbc:ProfileID" "P1"
Send ComUpdateChildContent To hoXmlToSign "cbc:ID" "c2ad1540-cf12-4e83-b47c-906aac70242e"
Send ComUpdateChildContent To hoXmlToSign "cbc:IssueDate" "2009-12-15"
Get ComUpdateAttrAt Of hoXmlToSign "cbc:Note" True "languageID" "en" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cbc:Note" "Ordered in our booth at the convention."
Get ComUpdateAttrAt Of hoXmlToSign "cbc:DocumentCurrencyCode" True "listAgencyID" "6" To iSuccess
Get ComUpdateAttrAt Of hoXmlToSign "cbc:DocumentCurrencyCode" True "listID" "ISO 4217 Alpha" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cbc:DocumentCurrencyCode" "HRK"
Send ComUpdateChildContent To hoXmlToSign "cbc:AccountingCost" "Project cost code 123"
Send ComUpdateChildContent To hoXmlToSign "cac:InvoicePeriod|cbc:StartDate" "2009-11-01"
Send ComUpdateChildContent To hoXmlToSign "cac:InvoicePeriod|cbc:EndDate" "2009-11-30"
Send ComUpdateChildContent To hoXmlToSign "cac:OrderReference|cbc:ID" "123"
Send ComUpdateChildContent To hoXmlToSign "cac:ContractDocumentReference|cbc:ID" "Contract321"
Send ComUpdateChildContent To hoXmlToSign "cac:ContractDocumentReference|cbc:DocumentType" "Framework agreement"
Send ComUpdateChildContent To hoXmlToSign "cac:AdditionalDocumentReference|cbc:ID" "Doc1"
Send ComUpdateChildContent To hoXmlToSign "cac:AdditionalDocumentReference|cbc:DocumentType" "Timesheet"
Send ComUpdateChildContent To hoXmlToSign "cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI" "http://www.suppliersite.eu/sheet001.html"
Send ComUpdateChildContent To hoXmlToSign "cac:AdditionalDocumentReference[1]|cbc:ID" "Doc2"
Send ComUpdateChildContent To hoXmlToSign "cac:AdditionalDocumentReference[1]|cbc:DocumentType" "Drawing"
Get ComUpdateAttrAt Of hoXmlToSign "cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject" True "mimeCode" "application/pdf" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject" "UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID" "9934:18683136487::HR99:276"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName" "KATANCICEVA"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName" "ZAGREB"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone" "10000"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode" "HR"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID" "HR18683136487"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID" "FRE"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName" "MINISTARSTVO FINANCIJA"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name" "JURAJ MARKOVIC"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail" "juraj.markovic@fina.hr"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID" "9934:49811265576::HR99:NOVO1"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName" "GETALDICEVA 4"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName" "ZAGREB"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone" "10000"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode" "HR"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID" "HR49811265576"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID" "VAT"
Send ComUpdateChildContent To hoXmlToSign "cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName" "NOVO1"
Send ComUpdateChildContent To hoXmlToSign "cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode" "HR"
Send ComUpdateChildContent To hoXmlToSign "cac:PaymentMeans|cbc:PaymentMeansCode" "30"
Send ComUpdateChildContent To hoXmlToSign "cac:PaymentMeans|cbc:InstructionNote" "Neki opis placanja"
Send ComUpdateChildContent To hoXmlToSign "cac:PaymentMeans|cbc:PaymentID" "HR00 12456"
Send ComUpdateChildContent To hoXmlToSign "cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID" "HR1210010051863000160"
Send ComUpdateChildContent To hoXmlToSign "cac:PaymentTerms|cbc:Note" "Neki uvjeti placanja"
Get ComUpdateAttrAt Of hoXmlToSign "cac:TaxTotal|cbc:TaxAmount" True "currencyID" "HRK" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:TaxTotal|cbc:TaxAmount" "25.00"
Get ComUpdateAttrAt Of hoXmlToSign "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount" True "currencyID" "HRK" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount" "100.00"
Get ComUpdateAttrAt Of hoXmlToSign "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount" True "currencyID" "HRK" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount" "25.00"
Send ComUpdateChildContent To hoXmlToSign "cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID" "S"
Send ComUpdateChildContent To hoXmlToSign "cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent" "25"
Send ComUpdateChildContent To hoXmlToSign "cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID" "VAT"
Get ComUpdateAttrAt Of hoXmlToSign "cac:LegalMonetaryTotal|cbc:LineExtensionAmount" True "currencyID" "HRK" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:LegalMonetaryTotal|cbc:LineExtensionAmount" "100.00"
Get ComUpdateAttrAt Of hoXmlToSign "cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount" True "currencyID" "HRK" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount" "100.00"
Get ComUpdateAttrAt Of hoXmlToSign "cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount" True "currencyID" "HRK" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount" "125.00"
Get ComUpdateAttrAt Of hoXmlToSign "cac:LegalMonetaryTotal|cbc:PayableAmount" True "currencyID" "HRK" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:LegalMonetaryTotal|cbc:PayableAmount" "125.00"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cbc:ID" "1"
Get ComUpdateAttrAt Of hoXmlToSign "cac:CreditNoteLine|cbc:CreditedQuantity" True "unitCode" "H87" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cbc:CreditedQuantity" "1.000"
Get ComUpdateAttrAt Of hoXmlToSign "cac:CreditNoteLine|cbc:LineExtensionAmount" True "currencyID" "HRK" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cbc:LineExtensionAmount" "100.00"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cbc:Description" "Neki detaljniji opis proizvoda ide ovdje"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cbc:Name" "Neki proizvod"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode" "HR"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID" "S"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent" "25"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID" "VAT"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name" "Boja"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value" "Plava"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name" "Masa"
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value" "1,2 Kg"
Get ComUpdateAttrAt Of hoXmlToSign "cac:CreditNoteLine|cac:Price|cbc:PriceAmount" True "currencyID" "HRK" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "cac:CreditNoteLine|cac:Price|cbc:PriceAmount" "100.000000"
Get ComUpdateAttrAt Of hoXmlToSign "cac:CreditNoteLine|cac:Price|cbc:BaseQuantity" True "unitCode" "H87" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "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.
Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
If (Not(IsComObjectCreated(hoGen))) Begin
Send CreateComObject of hoGen
End
Set ComSigLocation Of hoGen To "CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation"
Set ComSigLocationMod Of hoGen To 0
Set ComSigId Of hoGen To "Signature-7f4c4719515a4a0a8ce4d1b983a2ec69"
Set ComSigNamespacePrefix Of hoGen To ""
Set ComSigNamespaceUri Of hoGen To "http://www.w3.org/2000/09/xmldsig#"
Set ComSignedInfoCanonAlg Of hoGen To "C14N"
Set ComSignedInfoDigestMethod Of hoGen To "sha256"
// Create an Object to be added to the Signature.
Get Create (RefClass(cComChilkatXml)) To hoObject1
If (Not(IsComObjectCreated(hoObject1))) Begin
Send CreateComObject of hoObject1
End
Set ComTag Of hoObject1 To "xades:QualifyingProperties"
Get ComAddAttribute Of hoObject1 "xmlns:ds" "http://www.w3.org/2000/09/xmldsig#" To iSuccess
Get ComAddAttribute Of hoObject1 "xmlns:xades" "http://uri.etsi.org/01903/v1.3.2#" To iSuccess
Get ComAddAttribute Of hoObject1 "Target" "#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69" To iSuccess
Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties" True "Id" "SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc" To iSuccess
Send ComUpdateChildContent To hoObject1 "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
Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod" True "Algorithm" "http://www.w3.org/2000/09/xmldsig#sha1" To iSuccess
Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue" "TO BE GENERATED BY CHILKAT"
Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2" "TO BE GENERATED BY CHILKAT"
Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat" True "ObjectReference" "" To iSuccess
Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description" "document"
Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType" "application/xml"
Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding" "UTF-8"
Get ComGetXml Of hoObject1 To sTemp1
Get ComAddObject Of hoGen "" sTemp1 "" "" To iSuccess
// -------- Reference 1 --------
Get ComAddSameDocRef Of hoGen "" "sha256" "" "" "" To iSuccess
// -------- Reference 2 --------
Get ComAddObjectRef Of hoGen "SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc" "sha256" "" "" "http://uri.etsi.org/01903#SignedProperties" To iSuccess
// Provide a certificate + private key. (PFX password is test123)
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get ComLoadPfxFile Of hoCert "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCert To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoCert to vCert
Get ComSetX509Cert Of hoGen vCert True To iSuccess
Set ComKeyInfoType Of hoGen To "X509Data+KeyValue"
Set ComX509Type Of hoGen To "IssuerSerial,SubjectName,SKI,Certificate"
// Load XML to be signed...
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
If (Not(IsComObjectCreated(hoSbXml))) Begin
Send CreateComObject of hoSbXml
End
Get pvComObject of hoSbXml to vSbXml
Get ComGetXmlSb Of hoXmlToSign vSbXml To iSuccess
// 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]) > count(ancestor-or-self::sig:UBLDocumentSignatures)</XPath></Transform>
Set ComBehaviors Of hoGen To "IndentedSignature,UBLDocumentSignatures"
// Sign the XML...
Get pvComObject of hoSbXml to vSbXml
Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoGen To sTemp1
Showln sTemp1
Procedure_Return
End
// -----------------------------------------------
// Save the signed XML to a file.
Get ComWriteFile Of hoSbXml "qa_output/signedXml.xml" "utf-8" False To iSuccess
Get ComGetAsString Of hoSbXml To sTemp1
Showln sTemp1
// ----------------------------------------
// Verify the signature we just produced...
Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier
If (Not(IsComObjectCreated(hoVerifier))) Begin
Send CreateComObject of hoVerifier
End
Get pvComObject of hoSbXml to vSbXml
Get ComLoadSignatureSb Of hoVerifier vSbXml To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoVerifier To sTemp1
Showln sTemp1
Procedure_Return
End
// We should have just one signature..
Get ComNumSignatures Of hoVerifier To iNumSigs
Move 0 To iVerifyIdx
While (iVerifyIdx < iNumSigs)
Set ComSelector Of hoVerifier To iVerifyIdx
Get ComVerifySignature Of hoVerifier True To iVerified
If (iVerified <> True) Begin
Get ComLastErrorText Of hoVerifier To sTemp1
Showln sTemp1
Procedure_Return
End
Move (iVerifyIdx + 1) To iVerifyIdx
Loop
Showln "The signature was successfully verified."
End_Procedure