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
(Unicode C) UBL XAdES Enveloped SignatureDemonstrates how to create a UBL XAdES enveloped signature.
#include <C_CkXmlW.h> #include <C_CkXmlDSigGenW.h> #include <C_CkCertW.h> #include <C_CkStringBuilderW.h> #include <C_CkXmlDSigW.h> void ChilkatSample(void) { BOOL success; HCkXmlW xmlToSign; HCkXmlDSigGenW gen; HCkXmlW object1; HCkCertW cert; HCkStringBuilderW sbXml; HCkXmlDSigW verifier; int numSigs; int verifyIdx; BOOL verified; // 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 = CkXmlW_Create(); CkXmlW_putTag(xmlToSign,L"CreditNote"); CkXmlW_AddAttribute(xmlToSign,L"xmlns",L"urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"); CkXmlW_AddAttribute(xmlToSign,L"xmlns:cac",L"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"); CkXmlW_AddAttribute(xmlToSign,L"xmlns:cbc",L"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"); CkXmlW_AddAttribute(xmlToSign,L"xmlns:ext",L"urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"); CkXmlW_UpdateAttrAt(xmlToSign,L"ext:UBLExtensions|ext:UBLExtension",TRUE,L"xmlns:sac",L"urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2"); CkXmlW_UpdateAttrAt(xmlToSign,L"ext:UBLExtensions|ext:UBLExtension",TRUE,L"xmlns:sig",L"urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"); CkXmlW_UpdateChildContent(xmlToSign,L"ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation",L""); CkXmlW_UpdateChildContent(xmlToSign,L"cbc:CustomizationID",L"urn:cen.eu:en16931:2017"); CkXmlW_UpdateChildContent(xmlToSign,L"cbc:ProfileID",L"P1"); CkXmlW_UpdateChildContent(xmlToSign,L"cbc:ID",L"c2ad1540-cf12-4e83-b47c-906aac70242e"); CkXmlW_UpdateChildContent(xmlToSign,L"cbc:IssueDate",L"2009-12-15"); CkXmlW_UpdateAttrAt(xmlToSign,L"cbc:Note",TRUE,L"languageID",L"en"); CkXmlW_UpdateChildContent(xmlToSign,L"cbc:Note",L"Ordered in our booth at the convention."); CkXmlW_UpdateAttrAt(xmlToSign,L"cbc:DocumentCurrencyCode",TRUE,L"listAgencyID",L"6"); CkXmlW_UpdateAttrAt(xmlToSign,L"cbc:DocumentCurrencyCode",TRUE,L"listID",L"ISO 4217 Alpha"); CkXmlW_UpdateChildContent(xmlToSign,L"cbc:DocumentCurrencyCode",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cbc:AccountingCost",L"Project cost code 123"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:InvoicePeriod|cbc:StartDate",L"2009-11-01"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:InvoicePeriod|cbc:EndDate",L"2009-11-30"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:OrderReference|cbc:ID",L"123"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:ContractDocumentReference|cbc:ID",L"Contract321"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:ContractDocumentReference|cbc:DocumentType",L"Framework agreement"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AdditionalDocumentReference|cbc:ID",L"Doc1"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AdditionalDocumentReference|cbc:DocumentType",L"Timesheet"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI",L"http://www.suppliersite.eu/sheet001.html"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AdditionalDocumentReference[1]|cbc:ID",L"Doc2"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AdditionalDocumentReference[1]|cbc:DocumentType",L"Drawing"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject",TRUE,L"mimeCode",L"application/pdf"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject",L"UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID",L"9934:18683136487::HR99:276"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName",L"KATANCICEVA"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName",L"ZAGREB"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone",L"10000"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode",L"HR"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID",L"HR18683136487"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID",L"FRE"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName",L"MINISTARSTVO FINANCIJA"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name",L"JURAJ MARKOVIC"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail",L"juraj.markovic@fina.hr"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID",L"9934:49811265576::HR99:NOVO1"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName",L"GETALDICEVA 4"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName",L"ZAGREB"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone",L"10000"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode",L"HR"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID",L"HR49811265576"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID",L"VAT"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName",L"NOVO1"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode",L"HR"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:PaymentMeans|cbc:PaymentMeansCode",L"30"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:PaymentMeans|cbc:InstructionNote",L"Neki opis placanja"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:PaymentMeans|cbc:PaymentID",L"HR00 12456"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID",L"HR1210010051863000160"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:PaymentTerms|cbc:Note",L"Neki uvjeti placanja"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:TaxTotal|cbc:TaxAmount",TRUE,L"currencyID",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:TaxTotal|cbc:TaxAmount",L"25.00"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount",TRUE,L"currencyID",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount",L"100.00"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount",TRUE,L"currencyID",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount",L"25.00"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID",L"S"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent",L"25"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID",L"VAT"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:LegalMonetaryTotal|cbc:LineExtensionAmount",TRUE,L"currencyID",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:LegalMonetaryTotal|cbc:LineExtensionAmount",L"100.00"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount",TRUE,L"currencyID",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount",L"100.00"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount",TRUE,L"currencyID",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount",L"125.00"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:LegalMonetaryTotal|cbc:PayableAmount",TRUE,L"currencyID",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:LegalMonetaryTotal|cbc:PayableAmount",L"125.00"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cbc:ID",L"1"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:CreditNoteLine|cbc:CreditedQuantity",TRUE,L"unitCode",L"H87"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cbc:CreditedQuantity",L"1.000"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:CreditNoteLine|cbc:LineExtensionAmount",TRUE,L"currencyID",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cbc:LineExtensionAmount",L"100.00"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cbc:Description",L"Neki detaljniji opis proizvoda ide ovdje"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cbc:Name",L"Neki proizvod"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode",L"HR"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID",L"S"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent",L"25"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID",L"VAT"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name",L"Boja"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value",L"Plava"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name",L"Masa"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value",L"1,2 Kg"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:CreditNoteLine|cac:Price|cbc:PriceAmount",TRUE,L"currencyID",L"HRK"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Price|cbc:PriceAmount",L"100.000000"); CkXmlW_UpdateAttrAt(xmlToSign,L"cac:CreditNoteLine|cac:Price|cbc:BaseQuantity",TRUE,L"unitCode",L"H87"); CkXmlW_UpdateChildContent(xmlToSign,L"cac:CreditNoteLine|cac:Price|cbc:BaseQuantity",L"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 = CkXmlDSigGenW_Create(); CkXmlDSigGenW_putSigLocation(gen,L"CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation"); CkXmlDSigGenW_putSigLocationMod(gen,0); CkXmlDSigGenW_putSigId(gen,L"Signature-7f4c4719515a4a0a8ce4d1b983a2ec69"); CkXmlDSigGenW_putSigNamespacePrefix(gen,L""); CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#"); CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"C14N"); CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256"); // Create an Object to be added to the Signature. object1 = CkXmlW_Create(); CkXmlW_putTag(object1,L"xades:QualifyingProperties"); CkXmlW_AddAttribute(object1,L"xmlns:ds",L"http://www.w3.org/2000/09/xmldsig#"); CkXmlW_AddAttribute(object1,L"xmlns:xades",L"http://uri.etsi.org/01903/v1.3.2#"); CkXmlW_AddAttribute(object1,L"Target",L"#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69"); CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties",TRUE,L"Id",L"SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc"); CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime",L"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 CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod",TRUE,L"Algorithm",L"http://www.w3.org/2000/09/xmldsig#sha1"); CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue",L"TO BE GENERATED BY CHILKAT"); CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName",L"TO BE GENERATED BY CHILKAT"); CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber",L"TO BE GENERATED BY CHILKAT"); CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat",TRUE,L"ObjectReference",L""); CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description",L"document"); CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType",L"application/xml"); CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding",L"UTF-8"); CkXmlDSigGenW_AddObject(gen,L"",CkXmlW_getXml(object1),L"",L""); // -------- Reference 1 -------- CkXmlDSigGenW_AddSameDocRef(gen,L"",L"sha256",L"",L"",L""); // -------- Reference 2 -------- CkXmlDSigGenW_AddObjectRef(gen,L"SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc",L"sha256",L"",L"",L"http://uri.etsi.org/01903#SignedProperties"); // Provide a certificate + private key. (PFX password is test123) cert = CkCertW_Create(); success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/cert_test123.pfx",L"test123"); if (success != TRUE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkCertW_Dispose(cert); return; } CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE); CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data+KeyValue"); CkXmlDSigGenW_putX509Type(gen,L"IssuerSerial,SubjectName,SKI,Certificate"); // Load XML to be signed... sbXml = CkStringBuilderW_Create(); CkXmlW_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> CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature,UBLDocumentSignatures"); // Sign the XML... success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkCertW_Dispose(cert); CkStringBuilderW_Dispose(sbXml); return; } // ----------------------------------------------- // Save the signed XML to a file. success = CkStringBuilderW_WriteFile(sbXml,L"qa_output/signedXml.xml",L"utf-8",FALSE); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml)); // ---------------------------------------- // Verify the signature we just produced... verifier = CkXmlDSigW_Create(); success = CkXmlDSigW_LoadSignatureSb(verifier,sbXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkCertW_Dispose(cert); CkStringBuilderW_Dispose(sbXml); CkXmlDSigW_Dispose(verifier); return; } // We should have just one signature.. numSigs = CkXmlDSigW_getNumSignatures(verifier); verifyIdx = 0; while (verifyIdx < numSigs) { CkXmlDSigW_putSelector(verifier,verifyIdx); verified = CkXmlDSigW_VerifySignature(verifier,TRUE); if (verified != TRUE) { wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkCertW_Dispose(cert); CkStringBuilderW_Dispose(sbXml); CkXmlDSigW_Dispose(verifier); return; } verifyIdx = verifyIdx + 1; } wprintf(L"The signature was successfully verified.\n"); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkXmlW_Dispose(object1); CkCertW_Dispose(cert); CkStringBuilderW_Dispose(sbXml); CkXmlDSigW_Dispose(verifier); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.