Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(Excel) UBL XAdES Enveloped SignatureDemonstrates how to create a UBL XAdES enveloped signature. Note: This example requires Chilkat v9.5.0.80 or greater.
' This example requires the Chilkat API to have been previously unlocked. ' See Global Unlock Sample for sample code. 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 Dim xmlToSign As Chilkat.Xml Set xmlToSign = Chilkat.NewXml xmlToSign.Tag = "CreditNote" success = xmlToSign.AddAttribute("xmlns","urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2") success = xmlToSign.AddAttribute("xmlns:cac","urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2") success = xmlToSign.AddAttribute("xmlns:cbc","urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2") success = xmlToSign.AddAttribute("xmlns:ext","urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2") success = xmlToSign.UpdateAttrAt("ext:UBLExtensions|ext:UBLExtension",True,"xmlns:sac","urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2") success = xmlToSign.UpdateAttrAt("ext:UBLExtensions|ext:UBLExtension",True,"xmlns:sig","urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2") xmlToSign.UpdateChildContent "ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation","" xmlToSign.UpdateChildContent "cbc:CustomizationID","urn:cen.eu:en16931:2017" xmlToSign.UpdateChildContent "cbc:ProfileID","P1" xmlToSign.UpdateChildContent "cbc:ID","c2ad1540-cf12-4e83-b47c-906aac70242e" xmlToSign.UpdateChildContent "cbc:IssueDate","2009-12-15" success = xmlToSign.UpdateAttrAt("cbc:Note",True,"languageID","en") xmlToSign.UpdateChildContent "cbc:Note","Ordered in our booth at the convention." success = xmlToSign.UpdateAttrAt("cbc:DocumentCurrencyCode",True,"listAgencyID","6") success = xmlToSign.UpdateAttrAt("cbc:DocumentCurrencyCode",True,"listID","ISO 4217 Alpha") xmlToSign.UpdateChildContent "cbc:DocumentCurrencyCode","HRK" xmlToSign.UpdateChildContent "cbc:AccountingCost","Project cost code 123" xmlToSign.UpdateChildContent "cac:InvoicePeriod|cbc:StartDate","2009-11-01" xmlToSign.UpdateChildContent "cac:InvoicePeriod|cbc:EndDate","2009-11-30" xmlToSign.UpdateChildContent "cac:OrderReference|cbc:ID","123" xmlToSign.UpdateChildContent "cac:ContractDocumentReference|cbc:ID","Contract321" xmlToSign.UpdateChildContent "cac:ContractDocumentReference|cbc:DocumentType","Framework agreement" xmlToSign.UpdateChildContent "cac:AdditionalDocumentReference|cbc:ID","Doc1" xmlToSign.UpdateChildContent "cac:AdditionalDocumentReference|cbc:DocumentType","Timesheet" xmlToSign.UpdateChildContent "cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI","http://www.suppliersite.eu/sheet001.html" xmlToSign.UpdateChildContent "cac:AdditionalDocumentReference[1]|cbc:ID","Doc2" xmlToSign.UpdateChildContent "cac:AdditionalDocumentReference[1]|cbc:DocumentType","Drawing" success = xmlToSign.UpdateAttrAt("cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject",True,"mimeCode","application/pdf") xmlToSign.UpdateChildContent "cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject","UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID","9934:18683136487::HR99:276" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName","KATANCICEVA" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName","ZAGREB" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone","10000" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode","HR" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID","HR18683136487" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID","FRE" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName","MINISTARSTVO FINANCIJA" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name","JURAJ MARKOVIC" xmlToSign.UpdateChildContent "cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail","juraj.markovic@fina.hr" xmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID","9934:49811265576::HR99:NOVO1" xmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName","GETALDICEVA 4" xmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName","ZAGREB" xmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone","10000" xmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode","HR" xmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID","HR49811265576" xmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID","VAT" xmlToSign.UpdateChildContent "cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName","NOVO1" xmlToSign.UpdateChildContent "cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode","HR" xmlToSign.UpdateChildContent "cac:PaymentMeans|cbc:PaymentMeansCode","30" xmlToSign.UpdateChildContent "cac:PaymentMeans|cbc:InstructionNote","Neki opis placanja" xmlToSign.UpdateChildContent "cac:PaymentMeans|cbc:PaymentID","HR00 12456" xmlToSign.UpdateChildContent "cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID","HR1210010051863000160" xmlToSign.UpdateChildContent "cac:PaymentTerms|cbc:Note","Neki uvjeti placanja" success = xmlToSign.UpdateAttrAt("cac:TaxTotal|cbc:TaxAmount",True,"currencyID","HRK") xmlToSign.UpdateChildContent "cac:TaxTotal|cbc:TaxAmount","25.00" success = xmlToSign.UpdateAttrAt("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount",True,"currencyID","HRK") xmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount","100.00" success = xmlToSign.UpdateAttrAt("cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount",True,"currencyID","HRK") xmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount","25.00" xmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID","S" xmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent","25" xmlToSign.UpdateChildContent "cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID","VAT" success = xmlToSign.UpdateAttrAt("cac:LegalMonetaryTotal|cbc:LineExtensionAmount",True,"currencyID","HRK") xmlToSign.UpdateChildContent "cac:LegalMonetaryTotal|cbc:LineExtensionAmount","100.00" success = xmlToSign.UpdateAttrAt("cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount",True,"currencyID","HRK") xmlToSign.UpdateChildContent "cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount","100.00" success = xmlToSign.UpdateAttrAt("cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount",True,"currencyID","HRK") xmlToSign.UpdateChildContent "cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount","125.00" success = xmlToSign.UpdateAttrAt("cac:LegalMonetaryTotal|cbc:PayableAmount",True,"currencyID","HRK") xmlToSign.UpdateChildContent "cac:LegalMonetaryTotal|cbc:PayableAmount","125.00" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cbc:ID","1" success = xmlToSign.UpdateAttrAt("cac:CreditNoteLine|cbc:CreditedQuantity",True,"unitCode","H87") xmlToSign.UpdateChildContent "cac:CreditNoteLine|cbc:CreditedQuantity","1.000" success = xmlToSign.UpdateAttrAt("cac:CreditNoteLine|cbc:LineExtensionAmount",True,"currencyID","HRK") xmlToSign.UpdateChildContent "cac:CreditNoteLine|cbc:LineExtensionAmount","100.00" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cbc:Description","Neki detaljniji opis proizvoda ide ovdje" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cbc:Name","Neki proizvod" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode","HR" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID","S" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent","25" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID","VAT" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name","Boja" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value","Plava" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name","Masa" xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value","1,2 Kg" success = xmlToSign.UpdateAttrAt("cac:CreditNoteLine|cac:Price|cbc:PriceAmount",True,"currencyID","HRK") xmlToSign.UpdateChildContent "cac:CreditNoteLine|cac:Price|cbc:PriceAmount","100.000000" success = xmlToSign.UpdateAttrAt("cac:CreditNoteLine|cac:Price|cbc:BaseQuantity",True,"unitCode","H87") 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. Dim gen As Chilkat.XmlDSigGen Set gen = Chilkat.NewXmlDSigGen gen.SigLocation = "CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation" gen.SigLocationMod = 0 gen.SigId = "Signature-7f4c4719515a4a0a8ce4d1b983a2ec69" gen.SigNamespacePrefix = "" gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#" gen.SignedInfoCanonAlg = "C14N" gen.SignedInfoDigestMethod = "sha256" ' Create an Object to be added to the Signature. Dim object1 As Chilkat.Xml Set object1 = Chilkat.NewXml object1.Tag = "xades:QualifyingProperties" success = object1.AddAttribute("xmlns:ds","http://www.w3.org/2000/09/xmldsig#") success = object1.AddAttribute("xmlns:xades","http://uri.etsi.org/01903/v1.3.2#") success = object1.AddAttribute("Target","#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69") success = object1.UpdateAttrAt("xades:SignedProperties",True,"Id","SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc") object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT" success = object1.UpdateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod",True,"Algorithm","http://www.w3.org/2000/09/xmldsig#sha1") object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT" object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName","TO BE GENERATED BY CHILKAT" object1.UpdateChildContent "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber","TO BE GENERATED BY CHILKAT" success = object1.UpdateAttrAt("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat",True,"ObjectReference","") object1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description","document" object1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType","application/xml" object1.UpdateChildContent "xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding","UTF-8" success = gen.AddObject("",object1.GetXml(),"","") ' -------- Reference 1 -------- success = gen.AddSameDocRef("","sha256","","","") ' -------- Reference 2 -------- success = gen.AddObjectRef("SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc","sha256","","","http://uri.etsi.org/01903#SignedProperties") ' Provide a certificate + private key. (PFX password is test123) Dim cert As Chilkat.Cert Set cert = Chilkat.NewCert success = cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123") If (success <> True) Then Debug.Print cert.LastErrorText Exit Sub End If success = gen.SetX509Cert(cert,True) gen.KeyInfoType = "X509Data+KeyValue" gen.X509Type = "IssuerSerial,SubjectName,SKI,Certificate" ' Load XML to be signed... Dim sbXml As Chilkat.StringBuilder Set sbXml = Chilkat.NewStringBuilder success = xmlToSign.GetXmlSb(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]) > count(ancestor-or-self::sig:UBLDocumentSignatures)</XPath></Transform> gen.Behaviors = "IndentedSignature,UBLDocumentSignatures" ' Sign the XML... success = gen.CreateXmlDSigSb(sbXml) If (success <> True) Then Debug.Print gen.LastErrorText Exit Sub End If ' ----------------------------------------------- ' Save the signed XML to a file. success = sbXml.WriteFile("qa_output/signedXml.xml","utf-8",False) Debug.Print sbXml.GetAsString() ' ---------------------------------------- ' Verify the signature we just produced... Dim verifier As Chilkat.XmlDSig Set verifier = Chilkat.NewXmlDSig success = verifier.LoadSignatureSb(sbXml) If (success <> True) Then Debug.Print verifier.LastErrorText Exit Sub End If ' We should have just one signature.. numSigs = verifier.NumSignatures verifyIdx = 0 Do While verifyIdx < numSigs verifier.Selector = verifyIdx verified = verifier.VerifySignature(True) If (verified <> True) Then Debug.Print verifier.LastErrorText Exit Sub End If verifyIdx = verifyIdx + 1 Loop Debug.Print "The signature was successfully verified." |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.