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
(Delphi DLL) UBL XAdES Enveloped SignatureDemonstrates how to create a UBL XAdES enveloped signature.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, XmlDSigGen, Xml, XmlDSig, Cert; ... procedure TForm1.Button1Click(Sender: TObject); var success: Boolean; xmlToSign: HCkXml; gen: HCkXmlDSigGen; object1: HCkXml; cert: HCkCert; sbXml: HCkStringBuilder; verifier: HCkXmlDSig; numSigs: Integer; verifyIdx: Integer; verified: Boolean; begin // 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 xmlToSign := CkXml_Create(); CkXml_putTag(xmlToSign,'CreditNote'); CkXml_AddAttribute(xmlToSign,'xmlns','urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2'); CkXml_AddAttribute(xmlToSign,'xmlns:cac','urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'); CkXml_AddAttribute(xmlToSign,'xmlns:cbc','urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'); CkXml_AddAttribute(xmlToSign,'xmlns:ext','urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2'); CkXml_UpdateAttrAt(xmlToSign,'ext:UBLExtensions|ext:UBLExtension',True,'xmlns:sac','urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2'); CkXml_UpdateAttrAt(xmlToSign,'ext:UBLExtensions|ext:UBLExtension',True,'xmlns:sig','urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2'); CkXml_UpdateChildContent(xmlToSign,'ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation',''); CkXml_UpdateChildContent(xmlToSign,'cbc:CustomizationID','urn:cen.eu:en16931:2017'); CkXml_UpdateChildContent(xmlToSign,'cbc:ProfileID','P1'); CkXml_UpdateChildContent(xmlToSign,'cbc:ID','c2ad1540-cf12-4e83-b47c-906aac70242e'); CkXml_UpdateChildContent(xmlToSign,'cbc:IssueDate','2009-12-15'); CkXml_UpdateAttrAt(xmlToSign,'cbc:Note',True,'languageID','en'); CkXml_UpdateChildContent(xmlToSign,'cbc:Note','Ordered in our booth at the convention.'); CkXml_UpdateAttrAt(xmlToSign,'cbc:DocumentCurrencyCode',True,'listAgencyID','6'); CkXml_UpdateAttrAt(xmlToSign,'cbc:DocumentCurrencyCode',True,'listID','ISO 4217 Alpha'); CkXml_UpdateChildContent(xmlToSign,'cbc:DocumentCurrencyCode','HRK'); CkXml_UpdateChildContent(xmlToSign,'cbc:AccountingCost','Project cost code 123'); CkXml_UpdateChildContent(xmlToSign,'cac:InvoicePeriod|cbc:StartDate','2009-11-01'); CkXml_UpdateChildContent(xmlToSign,'cac:InvoicePeriod|cbc:EndDate','2009-11-30'); CkXml_UpdateChildContent(xmlToSign,'cac:OrderReference|cbc:ID','123'); CkXml_UpdateChildContent(xmlToSign,'cac:ContractDocumentReference|cbc:ID','Contract321'); CkXml_UpdateChildContent(xmlToSign,'cac:ContractDocumentReference|cbc:DocumentType','Framework agreement'); CkXml_UpdateChildContent(xmlToSign,'cac:AdditionalDocumentReference|cbc:ID','Doc1'); CkXml_UpdateChildContent(xmlToSign,'cac:AdditionalDocumentReference|cbc:DocumentType','Timesheet'); CkXml_UpdateChildContent(xmlToSign,'cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI','http://www.suppliersite.eu/sheet001.html'); CkXml_UpdateChildContent(xmlToSign,'cac:AdditionalDocumentReference[1]|cbc:ID','Doc2'); CkXml_UpdateChildContent(xmlToSign,'cac:AdditionalDocumentReference[1]|cbc:DocumentType','Drawing'); CkXml_UpdateAttrAt(xmlToSign,'cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject',True,'mimeCode','application/pdf'); CkXml_UpdateChildContent(xmlToSign,'cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject','UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID','9934:18683136487::HR99:276'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName','KATANCICEVA'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName','ZAGREB'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone','10000'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode','HR'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID','HR18683136487'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID','FRE'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName','MINISTARSTVO FINANCIJA'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name','JURAJ MARKOVIC'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail','juraj.markovic@fina.hr'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID','9934:49811265576::HR99:NOVO1'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName','GETALDICEVA 4'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName','ZAGREB'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone','10000'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode','HR'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID','HR49811265576'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID','VAT'); CkXml_UpdateChildContent(xmlToSign,'cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName','NOVO1'); CkXml_UpdateChildContent(xmlToSign,'cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode','HR'); CkXml_UpdateChildContent(xmlToSign,'cac:PaymentMeans|cbc:PaymentMeansCode','30'); CkXml_UpdateChildContent(xmlToSign,'cac:PaymentMeans|cbc:InstructionNote','Neki opis placanja'); CkXml_UpdateChildContent(xmlToSign,'cac:PaymentMeans|cbc:PaymentID','HR00 12456'); CkXml_UpdateChildContent(xmlToSign,'cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID','HR1210010051863000160'); CkXml_UpdateChildContent(xmlToSign,'cac:PaymentTerms|cbc:Note','Neki uvjeti placanja'); CkXml_UpdateAttrAt(xmlToSign,'cac:TaxTotal|cbc:TaxAmount',True,'currencyID','HRK'); CkXml_UpdateChildContent(xmlToSign,'cac:TaxTotal|cbc:TaxAmount','25.00'); CkXml_UpdateAttrAt(xmlToSign,'cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount',True,'currencyID','HRK'); CkXml_UpdateChildContent(xmlToSign,'cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount','100.00'); CkXml_UpdateAttrAt(xmlToSign,'cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount',True,'currencyID','HRK'); CkXml_UpdateChildContent(xmlToSign,'cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount','25.00'); CkXml_UpdateChildContent(xmlToSign,'cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID','S'); CkXml_UpdateChildContent(xmlToSign,'cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent','25'); CkXml_UpdateChildContent(xmlToSign,'cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID','VAT'); CkXml_UpdateAttrAt(xmlToSign,'cac:LegalMonetaryTotal|cbc:LineExtensionAmount',True,'currencyID','HRK'); CkXml_UpdateChildContent(xmlToSign,'cac:LegalMonetaryTotal|cbc:LineExtensionAmount','100.00'); CkXml_UpdateAttrAt(xmlToSign,'cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount',True,'currencyID','HRK'); CkXml_UpdateChildContent(xmlToSign,'cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount','100.00'); CkXml_UpdateAttrAt(xmlToSign,'cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount',True,'currencyID','HRK'); CkXml_UpdateChildContent(xmlToSign,'cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount','125.00'); CkXml_UpdateAttrAt(xmlToSign,'cac:LegalMonetaryTotal|cbc:PayableAmount',True,'currencyID','HRK'); CkXml_UpdateChildContent(xmlToSign,'cac:LegalMonetaryTotal|cbc:PayableAmount','125.00'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cbc:ID','1'); CkXml_UpdateAttrAt(xmlToSign,'cac:CreditNoteLine|cbc:CreditedQuantity',True,'unitCode','H87'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cbc:CreditedQuantity','1.000'); CkXml_UpdateAttrAt(xmlToSign,'cac:CreditNoteLine|cbc:LineExtensionAmount',True,'currencyID','HRK'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cbc:LineExtensionAmount','100.00'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cbc:Description','Neki detaljniji opis proizvoda ide ovdje'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cbc:Name','Neki proizvod'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode','HR'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID','S'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent','25'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID','VAT'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name','Boja'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value','Plava'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name','Masa'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value','1,2 Kg'); CkXml_UpdateAttrAt(xmlToSign,'cac:CreditNoteLine|cac:Price|cbc:PriceAmount',True,'currencyID','HRK'); CkXml_UpdateChildContent(xmlToSign,'cac:CreditNoteLine|cac:Price|cbc:PriceAmount','100.000000'); CkXml_UpdateAttrAt(xmlToSign,'cac:CreditNoteLine|cac:Price|cbc:BaseQuantity',True,'unitCode','H87'); CkXml_UpdateChildContent(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 := CkXmlDSigGen_Create(); CkXmlDSigGen_putSigLocation(gen,'CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation'); CkXmlDSigGen_putSigLocationMod(gen,0); CkXmlDSigGen_putSigId(gen,'Signature-7f4c4719515a4a0a8ce4d1b983a2ec69'); CkXmlDSigGen_putSigNamespacePrefix(gen,''); CkXmlDSigGen_putSigNamespaceUri(gen,'http://www.w3.org/2000/09/xmldsig#'); CkXmlDSigGen_putSignedInfoCanonAlg(gen,'C14N'); CkXmlDSigGen_putSignedInfoDigestMethod(gen,'sha256'); // Create an Object to be added to the Signature. object1 := CkXml_Create(); CkXml_putTag(object1,'xades:QualifyingProperties'); CkXml_AddAttribute(object1,'xmlns:ds','http://www.w3.org/2000/09/xmldsig#'); CkXml_AddAttribute(object1,'xmlns:xades','http://uri.etsi.org/01903/v1.3.2#'); CkXml_AddAttribute(object1,'Target','#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69'); CkXml_UpdateAttrAt(object1,'xades:SignedProperties',True,'Id','SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc'); CkXml_UpdateChildContent(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_UpdateAttrAt(object1,'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod',True,'Algorithm','http://www.w3.org/2000/09/xmldsig#sha1'); CkXml_UpdateChildContent(object1,'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue','TO BE GENERATED BY CHILKAT'); CkXml_UpdateChildContent(object1,'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2','TO BE GENERATED BY CHILKAT'); CkXml_UpdateAttrAt(object1,'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat',True,'ObjectReference',''); CkXml_UpdateChildContent(object1,'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description','document'); CkXml_UpdateChildContent(object1,'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType','application/xml'); CkXml_UpdateChildContent(object1,'xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding','UTF-8'); CkXmlDSigGen_AddObject(gen,'',CkXml__getXml(object1),'',''); // -------- Reference 1 -------- CkXmlDSigGen_AddSameDocRef(gen,'','sha256','','',''); // -------- Reference 2 -------- CkXmlDSigGen_AddObjectRef(gen,'SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc','sha256','','','http://uri.etsi.org/01903#SignedProperties'); // Provide a certificate + private key. (PFX password is test123) cert := CkCert_Create(); success := CkCert_LoadPfxFile(cert,'qa_data/pfx/cert_test123.pfx','test123'); if (success <> True) then begin Memo1.Lines.Add(CkCert__lastErrorText(cert)); Exit; end; CkXmlDSigGen_SetX509Cert(gen,cert,True); CkXmlDSigGen_putKeyInfoType(gen,'X509Data+KeyValue'); CkXmlDSigGen_putX509Type(gen,'IssuerSerial,SubjectName,SKI,Certificate'); // Load XML to be signed... sbXml := CkStringBuilder_Create(); CkXml_GetXmlSb(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_putBehaviors(gen,'IndentedSignature,UBLDocumentSignatures'); // Sign the XML... success := CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml); if (success <> True) then begin Memo1.Lines.Add(CkXmlDSigGen__lastErrorText(gen)); Exit; end; // ----------------------------------------------- // Save the signed XML to a file. success := CkStringBuilder_WriteFile(sbXml,'qa_output/signedXml.xml','utf-8',False); Memo1.Lines.Add(CkStringBuilder__getAsString(sbXml)); // ---------------------------------------- // Verify the signature we just produced... verifier := CkXmlDSig_Create(); success := CkXmlDSig_LoadSignatureSb(verifier,sbXml); if (success <> True) then begin Memo1.Lines.Add(CkXmlDSig__lastErrorText(verifier)); Exit; end; // We should have just one signature.. numSigs := CkXmlDSig_getNumSignatures(verifier); verifyIdx := 0; while verifyIdx < numSigs do begin CkXmlDSig_putSelector(verifier,verifyIdx); verified := CkXmlDSig_VerifySignature(verifier,True); if (verified <> True) then begin Memo1.Lines.Add(CkXmlDSig__lastErrorText(verifier)); Exit; end; verifyIdx := verifyIdx + 1; end; Memo1.Lines.Add('The signature was successfully verified.'); CkXml_Dispose(xmlToSign); CkXmlDSigGen_Dispose(gen); CkXml_Dispose(object1); CkCert_Dispose(cert); CkStringBuilder_Dispose(sbXml); CkXmlDSig_Dispose(verifier); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.