Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) UBL XAdES Enveloped SignatureDemonstrates how to create a UBL XAdES enveloped signature.
IncludeFile "CkCert.pb" IncludeFile "CkXml.pb" IncludeFile "CkXmlDSigGen.pb" IncludeFile "CkXmlDSig.pb" IncludeFile "CkStringBuilder.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. success.i = 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 xmlToSign.i = CkXml::ckCreate() If xmlToSign.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xmlToSign, "CreditNote") CkXml::ckAddAttribute(xmlToSign,"xmlns","urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2") CkXml::ckAddAttribute(xmlToSign,"xmlns:cac","urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2") CkXml::ckAddAttribute(xmlToSign,"xmlns:cbc","urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2") CkXml::ckAddAttribute(xmlToSign,"xmlns:ext","urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2") CkXml::ckUpdateAttrAt(xmlToSign,"ext:UBLExtensions|ext:UBLExtension",1,"xmlns:sac","urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2") CkXml::ckUpdateAttrAt(xmlToSign,"ext:UBLExtensions|ext:UBLExtension",1,"xmlns:sig","urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2") CkXml::ckUpdateChildContent(xmlToSign,"ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation","") CkXml::ckUpdateChildContent(xmlToSign,"cbc:CustomizationID","urn:cen.eu:en16931:2017") CkXml::ckUpdateChildContent(xmlToSign,"cbc:ProfileID","P1") CkXml::ckUpdateChildContent(xmlToSign,"cbc:ID","c2ad1540-cf12-4e83-b47c-906aac70242e") CkXml::ckUpdateChildContent(xmlToSign,"cbc:IssueDate","2009-12-15") CkXml::ckUpdateAttrAt(xmlToSign,"cbc:Note",1,"languageID","en") CkXml::ckUpdateChildContent(xmlToSign,"cbc:Note","Ordered in our booth at the convention.") CkXml::ckUpdateAttrAt(xmlToSign,"cbc:DocumentCurrencyCode",1,"listAgencyID","6") CkXml::ckUpdateAttrAt(xmlToSign,"cbc:DocumentCurrencyCode",1,"listID","ISO 4217 Alpha") CkXml::ckUpdateChildContent(xmlToSign,"cbc:DocumentCurrencyCode","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cbc:AccountingCost","Project cost code 123") CkXml::ckUpdateChildContent(xmlToSign,"cac:InvoicePeriod|cbc:StartDate","2009-11-01") CkXml::ckUpdateChildContent(xmlToSign,"cac:InvoicePeriod|cbc:EndDate","2009-11-30") CkXml::ckUpdateChildContent(xmlToSign,"cac:OrderReference|cbc:ID","123") CkXml::ckUpdateChildContent(xmlToSign,"cac:ContractDocumentReference|cbc:ID","Contract321") CkXml::ckUpdateChildContent(xmlToSign,"cac:ContractDocumentReference|cbc:DocumentType","Framework agreement") CkXml::ckUpdateChildContent(xmlToSign,"cac:AdditionalDocumentReference|cbc:ID","Doc1") CkXml::ckUpdateChildContent(xmlToSign,"cac:AdditionalDocumentReference|cbc:DocumentType","Timesheet") CkXml::ckUpdateChildContent(xmlToSign,"cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI","http://www.suppliersite.eu/sheet001.html") CkXml::ckUpdateChildContent(xmlToSign,"cac:AdditionalDocumentReference[1]|cbc:ID","Doc2") CkXml::ckUpdateChildContent(xmlToSign,"cac:AdditionalDocumentReference[1]|cbc:DocumentType","Drawing") CkXml::ckUpdateAttrAt(xmlToSign,"cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject",1,"mimeCode","application/pdf") CkXml::ckUpdateChildContent(xmlToSign,"cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject","UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID","9934:18683136487::HR99:276") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName","KATANCICEVA") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName","ZAGREB") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone","10000") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode","HR") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID","HR18683136487") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID","FRE") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName","MINISTARSTVO FINANCIJA") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name","JURAJ MARKOVIC") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail","juraj.markovic@fina.hr") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID","9934:49811265576::HR99:NOVO1") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName","GETALDICEVA 4") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName","ZAGREB") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone","10000") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode","HR") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID","HR49811265576") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID","VAT") CkXml::ckUpdateChildContent(xmlToSign,"cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName","NOVO1") CkXml::ckUpdateChildContent(xmlToSign,"cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode","HR") CkXml::ckUpdateChildContent(xmlToSign,"cac:PaymentMeans|cbc:PaymentMeansCode","30") CkXml::ckUpdateChildContent(xmlToSign,"cac:PaymentMeans|cbc:InstructionNote","Neki opis placanja") CkXml::ckUpdateChildContent(xmlToSign,"cac:PaymentMeans|cbc:PaymentID","HR00 12456") CkXml::ckUpdateChildContent(xmlToSign,"cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID","HR1210010051863000160") CkXml::ckUpdateChildContent(xmlToSign,"cac:PaymentTerms|cbc:Note","Neki uvjeti placanja") CkXml::ckUpdateAttrAt(xmlToSign,"cac:TaxTotal|cbc:TaxAmount",1,"currencyID","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cac:TaxTotal|cbc:TaxAmount","25.00") CkXml::ckUpdateAttrAt(xmlToSign,"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount",1,"currencyID","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount","100.00") CkXml::ckUpdateAttrAt(xmlToSign,"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount",1,"currencyID","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount","25.00") CkXml::ckUpdateChildContent(xmlToSign,"cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID","S") CkXml::ckUpdateChildContent(xmlToSign,"cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent","25") CkXml::ckUpdateChildContent(xmlToSign,"cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID","VAT") CkXml::ckUpdateAttrAt(xmlToSign,"cac:LegalMonetaryTotal|cbc:LineExtensionAmount",1,"currencyID","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cac:LegalMonetaryTotal|cbc:LineExtensionAmount","100.00") CkXml::ckUpdateAttrAt(xmlToSign,"cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount",1,"currencyID","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount","100.00") CkXml::ckUpdateAttrAt(xmlToSign,"cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount",1,"currencyID","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount","125.00") CkXml::ckUpdateAttrAt(xmlToSign,"cac:LegalMonetaryTotal|cbc:PayableAmount",1,"currencyID","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cac:LegalMonetaryTotal|cbc:PayableAmount","125.00") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cbc:ID","1") CkXml::ckUpdateAttrAt(xmlToSign,"cac:CreditNoteLine|cbc:CreditedQuantity",1,"unitCode","H87") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cbc:CreditedQuantity","1.000") CkXml::ckUpdateAttrAt(xmlToSign,"cac:CreditNoteLine|cbc:LineExtensionAmount",1,"currencyID","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cbc:LineExtensionAmount","100.00") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cbc:Description","Neki detaljniji opis proizvoda ide ovdje") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cbc:Name","Neki proizvod") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode","HR") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID","S") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent","25") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID","VAT") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name","Boja") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value","Plava") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name","Masa") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value","1,2 Kg") CkXml::ckUpdateAttrAt(xmlToSign,"cac:CreditNoteLine|cac:Price|cbc:PriceAmount",1,"currencyID","HRK") CkXml::ckUpdateChildContent(xmlToSign,"cac:CreditNoteLine|cac:Price|cbc:PriceAmount","100.000000") CkXml::ckUpdateAttrAt(xmlToSign,"cac:CreditNoteLine|cac:Price|cbc:BaseQuantity",1,"unitCode","H87") CkXml::ckUpdateChildContent(xmlToSign,"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. gen.i = CkXmlDSigGen::ckCreate() If gen.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXmlDSigGen::setCkSigLocation(gen, "CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation") CkXmlDSigGen::setCkSigLocationMod(gen, 0) CkXmlDSigGen::setCkSigId(gen, "Signature-7f4c4719515a4a0a8ce4d1b983a2ec69") CkXmlDSigGen::setCkSigNamespacePrefix(gen, "") CkXmlDSigGen::setCkSigNamespaceUri(gen, "http://www.w3.org/2000/09/xmldsig#") CkXmlDSigGen::setCkSignedInfoCanonAlg(gen, "C14N") CkXmlDSigGen::setCkSignedInfoDigestMethod(gen, "sha256") ; Create an Object to be added to the Signature. object1.i = CkXml::ckCreate() If object1.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(object1, "xades:QualifyingProperties") CkXml::ckAddAttribute(object1,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#") CkXml::ckAddAttribute(object1,"xmlns:xades","http://uri.etsi.org/01903/v1.3.2#") CkXml::ckAddAttribute(object1,"Target","#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69") CkXml::ckUpdateAttrAt(object1,"xades:SignedProperties",1,"Id","SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc") CkXml::ckUpdateChildContent(object1,"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 CkXml::ckUpdateAttrAt(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod",1,"Algorithm","http://www.w3.org/2000/09/xmldsig#sha1") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateAttrAt(object1,"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat",1,"ObjectReference","") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description","document") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType","application/xml") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding","UTF-8") CkXmlDSigGen::ckAddObject(gen,"",CkXml::ckGetXml(object1),"","") ; -------- Reference 1 -------- CkXmlDSigGen::ckAddSameDocRef(gen,"","sha256","","","") ; -------- Reference 2 -------- CkXmlDSigGen::ckAddObjectRef(gen,"SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc","sha256","","","http://uri.etsi.org/01903#SignedProperties") ; Provide a certificate + private key. (PFX password is test123) cert.i = CkCert::ckCreate() If cert.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123") If success <> 1 Debug CkCert::ckLastErrorText(cert) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkCert::ckDispose(cert) ProcedureReturn EndIf CkXmlDSigGen::ckSetX509Cert(gen,cert,1) CkXmlDSigGen::setCkKeyInfoType(gen, "X509Data+KeyValue") CkXmlDSigGen::setCkX509Type(gen, "IssuerSerial,SubjectName,SKI,Certificate") ; Load XML to be signed... sbXml.i = CkStringBuilder::ckCreate() If sbXml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::ckGetXmlSb(xmlToSign,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> CkXmlDSigGen::setCkBehaviors(gen, "IndentedSignature,UBLDocumentSignatures") ; Sign the XML... success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml) If success <> 1 Debug CkXmlDSigGen::ckLastErrorText(gen) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndIf ; ----------------------------------------------- ; Save the signed XML to a file. success = CkStringBuilder::ckWriteFile(sbXml,"qa_output/signedXml.xml","utf-8",0) Debug CkStringBuilder::ckGetAsString(sbXml) ; ---------------------------------------- ; Verify the signature we just produced... verifier.i = CkXmlDSig::ckCreate() If verifier.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkXmlDSig::ckLoadSignatureSb(verifier,sbXml) If success <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf ; We should have just one signature.. numSigs.i = CkXmlDSig::ckNumSignatures(verifier) verifyIdx.i = 0 While verifyIdx < numSigs CkXmlDSig::setCkSelector(verifier, verifyIdx) verified.i = CkXmlDSig::ckVerifySignature(verifier,1) If verified <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf verifyIdx = verifyIdx + 1 Wend Debug "The signature was successfully verified." CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.