Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Unicode C++ Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Unicode C++) UBL XAdES Enveloped Signature

Demonstrates how to create a UBL XAdES enveloped signature.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <CkXmlW.h>
#include <CkXmlDSigGenW.h>
#include <CkCertW.h>
#include <CkStringBuilderW.h>
#include <CkXmlDSigW.h>

void ChilkatSample(void)
    {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    bool 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

    CkXmlW xmlToSign;
    xmlToSign.put_Tag(L"CreditNote");
    xmlToSign.AddAttribute(L"xmlns",L"urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2");
    xmlToSign.AddAttribute(L"xmlns:cac",L"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2");
    xmlToSign.AddAttribute(L"xmlns:cbc",L"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
    xmlToSign.AddAttribute(L"xmlns:ext",L"urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2");
    xmlToSign.UpdateAttrAt(L"ext:UBLExtensions|ext:UBLExtension",true,L"xmlns:sac",L"urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2");
    xmlToSign.UpdateAttrAt(L"ext:UBLExtensions|ext:UBLExtension",true,L"xmlns:sig",L"urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2");
    xmlToSign.UpdateChildContent(L"ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation",L"");
    xmlToSign.UpdateChildContent(L"cbc:CustomizationID",L"urn:cen.eu:en16931:2017");
    xmlToSign.UpdateChildContent(L"cbc:ProfileID",L"P1");
    xmlToSign.UpdateChildContent(L"cbc:ID",L"c2ad1540-cf12-4e83-b47c-906aac70242e");
    xmlToSign.UpdateChildContent(L"cbc:IssueDate",L"2009-12-15");
    xmlToSign.UpdateAttrAt(L"cbc:Note",true,L"languageID",L"en");
    xmlToSign.UpdateChildContent(L"cbc:Note",L"Ordered in our booth at the convention.");
    xmlToSign.UpdateAttrAt(L"cbc:DocumentCurrencyCode",true,L"listAgencyID",L"6");
    xmlToSign.UpdateAttrAt(L"cbc:DocumentCurrencyCode",true,L"listID",L"ISO 4217 Alpha");
    xmlToSign.UpdateChildContent(L"cbc:DocumentCurrencyCode",L"HRK");
    xmlToSign.UpdateChildContent(L"cbc:AccountingCost",L"Project cost code 123");
    xmlToSign.UpdateChildContent(L"cac:InvoicePeriod|cbc:StartDate",L"2009-11-01");
    xmlToSign.UpdateChildContent(L"cac:InvoicePeriod|cbc:EndDate",L"2009-11-30");
    xmlToSign.UpdateChildContent(L"cac:OrderReference|cbc:ID",L"123");
    xmlToSign.UpdateChildContent(L"cac:ContractDocumentReference|cbc:ID",L"Contract321");
    xmlToSign.UpdateChildContent(L"cac:ContractDocumentReference|cbc:DocumentType",L"Framework agreement");
    xmlToSign.UpdateChildContent(L"cac:AdditionalDocumentReference|cbc:ID",L"Doc1");
    xmlToSign.UpdateChildContent(L"cac:AdditionalDocumentReference|cbc:DocumentType",L"Timesheet");
    xmlToSign.UpdateChildContent(L"cac:AdditionalDocumentReference|cac:Attachment|cac:ExternalReference|cbc:URI",L"http://www.suppliersite.eu/sheet001.html");
    xmlToSign.UpdateChildContent(L"cac:AdditionalDocumentReference[1]|cbc:ID",L"Doc2");
    xmlToSign.UpdateChildContent(L"cac:AdditionalDocumentReference[1]|cbc:DocumentType",L"Drawing");
    xmlToSign.UpdateAttrAt(L"cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject",true,L"mimeCode",L"application/pdf");
    xmlToSign.UpdateChildContent(L"cac:AdditionalDocumentReference[1]|cac:Attachment|cbc:EmbeddedDocumentBinaryObject",L"UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:PartyIdentification|cbc:ID",L"9934:18683136487::HR99:276");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:StreetName",L"KATANCICEVA");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:CityName",L"ZAGREB");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cbc:PostalZone",L"10000");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode",L"HR");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID",L"HR18683136487");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID",L"FRE");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName",L"MINISTARSTVO FINANCIJA");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:Name",L"JURAJ MARKOVIC");
    xmlToSign.UpdateChildContent(L"cac:AccountingSupplierParty|cac:Party|cac:Contact|cbc:ElectronicMail",L"juraj.markovic@fina.hr");
    xmlToSign.UpdateChildContent(L"cac:AccountingCustomerParty|cac:Party|cac:PartyIdentification|cbc:ID",L"9934:49811265576::HR99:NOVO1");
    xmlToSign.UpdateChildContent(L"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:StreetName",L"GETALDICEVA 4");
    xmlToSign.UpdateChildContent(L"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:CityName",L"ZAGREB");
    xmlToSign.UpdateChildContent(L"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cbc:PostalZone",L"10000");
    xmlToSign.UpdateChildContent(L"cac:AccountingCustomerParty|cac:Party|cac:PostalAddress|cac:Country|cbc:IdentificationCode",L"HR");
    xmlToSign.UpdateChildContent(L"cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cbc:CompanyID",L"HR49811265576");
    xmlToSign.UpdateChildContent(L"cac:AccountingCustomerParty|cac:Party|cac:PartyTaxScheme|cac:TaxScheme|cbc:ID",L"VAT");
    xmlToSign.UpdateChildContent(L"cac:AccountingCustomerParty|cac:Party|cac:PartyLegalEntity|cbc:RegistrationName",L"NOVO1");
    xmlToSign.UpdateChildContent(L"cac:Delivery|cac:DeliveryLocation|cac:Address|cac:Country|cbc:IdentificationCode",L"HR");
    xmlToSign.UpdateChildContent(L"cac:PaymentMeans|cbc:PaymentMeansCode",L"30");
    xmlToSign.UpdateChildContent(L"cac:PaymentMeans|cbc:InstructionNote",L"Neki opis placanja");
    xmlToSign.UpdateChildContent(L"cac:PaymentMeans|cbc:PaymentID",L"HR00 12456");
    xmlToSign.UpdateChildContent(L"cac:PaymentMeans|cac:PayeeFinancialAccount|cbc:ID",L"HR1210010051863000160");
    xmlToSign.UpdateChildContent(L"cac:PaymentTerms|cbc:Note",L"Neki uvjeti placanja");
    xmlToSign.UpdateAttrAt(L"cac:TaxTotal|cbc:TaxAmount",true,L"currencyID",L"HRK");
    xmlToSign.UpdateChildContent(L"cac:TaxTotal|cbc:TaxAmount",L"25.00");
    xmlToSign.UpdateAttrAt(L"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount",true,L"currencyID",L"HRK");
    xmlToSign.UpdateChildContent(L"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxableAmount",L"100.00");
    xmlToSign.UpdateAttrAt(L"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount",true,L"currencyID",L"HRK");
    xmlToSign.UpdateChildContent(L"cac:TaxTotal|cac:TaxSubtotal|cbc:TaxAmount",L"25.00");
    xmlToSign.UpdateChildContent(L"cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:ID",L"S");
    xmlToSign.UpdateChildContent(L"cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cbc:Percent",L"25");
    xmlToSign.UpdateChildContent(L"cac:TaxTotal|cac:TaxSubtotal|cac:TaxCategory|cac:TaxScheme|cbc:ID",L"VAT");
    xmlToSign.UpdateAttrAt(L"cac:LegalMonetaryTotal|cbc:LineExtensionAmount",true,L"currencyID",L"HRK");
    xmlToSign.UpdateChildContent(L"cac:LegalMonetaryTotal|cbc:LineExtensionAmount",L"100.00");
    xmlToSign.UpdateAttrAt(L"cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount",true,L"currencyID",L"HRK");
    xmlToSign.UpdateChildContent(L"cac:LegalMonetaryTotal|cbc:TaxExclusiveAmount",L"100.00");
    xmlToSign.UpdateAttrAt(L"cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount",true,L"currencyID",L"HRK");
    xmlToSign.UpdateChildContent(L"cac:LegalMonetaryTotal|cbc:TaxInclusiveAmount",L"125.00");
    xmlToSign.UpdateAttrAt(L"cac:LegalMonetaryTotal|cbc:PayableAmount",true,L"currencyID",L"HRK");
    xmlToSign.UpdateChildContent(L"cac:LegalMonetaryTotal|cbc:PayableAmount",L"125.00");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cbc:ID",L"1");
    xmlToSign.UpdateAttrAt(L"cac:CreditNoteLine|cbc:CreditedQuantity",true,L"unitCode",L"H87");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cbc:CreditedQuantity",L"1.000");
    xmlToSign.UpdateAttrAt(L"cac:CreditNoteLine|cbc:LineExtensionAmount",true,L"currencyID",L"HRK");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cbc:LineExtensionAmount",L"100.00");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cbc:Description",L"Neki detaljniji opis proizvoda ide ovdje");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cbc:Name",L"Neki proizvod");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cac:OriginCountry|cbc:IdentificationCode",L"HR");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:ID",L"S");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cbc:Percent",L"25");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cac:ClassifiedTaxCategory|cac:TaxScheme|cbc:ID",L"VAT");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Name",L"Boja");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty|cbc:Value",L"Plava");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Name",L"Masa");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Item|cac:AdditionalItemProperty[1]|cbc:Value",L"1,2 Kg");
    xmlToSign.UpdateAttrAt(L"cac:CreditNoteLine|cac:Price|cbc:PriceAmount",true,L"currencyID",L"HRK");
    xmlToSign.UpdateChildContent(L"cac:CreditNoteLine|cac:Price|cbc:PriceAmount",L"100.000000");
    xmlToSign.UpdateAttrAt(L"cac:CreditNoteLine|cac:Price|cbc:BaseQuantity",true,L"unitCode",L"H87");
    xmlToSign.UpdateChildContent(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.

    CkXmlDSigGenW gen;

    gen.put_SigLocation(L"CreditNote|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation");
    gen.put_SigLocationMod(0);
    gen.put_SigId(L"Signature-7f4c4719515a4a0a8ce4d1b983a2ec69");
    gen.put_SigNamespacePrefix(L"");
    gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#");
    gen.put_SignedInfoCanonAlg(L"C14N");
    gen.put_SignedInfoDigestMethod(L"sha256");

    // Create an Object to be added to the Signature.
    CkXmlW object1;
    object1.put_Tag(L"xades:QualifyingProperties");
    object1.AddAttribute(L"xmlns:ds",L"http://www.w3.org/2000/09/xmldsig#");
    object1.AddAttribute(L"xmlns:xades",L"http://uri.etsi.org/01903/v1.3.2#");
    object1.AddAttribute(L"Target",L"#Signature-7f4c4719515a4a0a8ce4d1b983a2ec69");
    object1.UpdateAttrAt(L"xades:SignedProperties",true,L"Id",L"SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc");
    object1.UpdateChildContent(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
    object1.UpdateAttrAt(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");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateAttrAt(L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat",true,L"ObjectReference",L"");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description",L"document");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType",L"application/xml");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding",L"UTF-8");

    gen.AddObject(L"",object1.getXml(),L"",L"");

    // -------- Reference 1 --------
    gen.AddSameDocRef(L"",L"sha256",L"",L"",L"");

    // -------- Reference 2 --------
    gen.AddObjectRef(L"SignedProperties-6573207f4ad64b49b0310f7a9e2dfadc",L"sha256",L"",L"",L"http://uri.etsi.org/01903#SignedProperties");

    // Provide a certificate + private key. (PFX password is test123)
    CkCertW cert;
    success = cert.LoadPfxFile(L"qa_data/pfx/cert_test123.pfx",L"test123");
    if (success != true) {
        wprintf(L"%s\n",cert.lastErrorText());
        return;
    }

    gen.SetX509Cert(cert,true);

    gen.put_KeyInfoType(L"X509Data+KeyValue");
    gen.put_X509Type(L"IssuerSerial,SubjectName,SKI,Certificate");

    // Load XML to be signed...
    CkStringBuilderW sbXml;
    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]) &gt; count(ancestor-or-self::sig:UBLDocumentSignatures)</XPath></Transform>
    gen.put_Behaviors(L"IndentedSignature,UBLDocumentSignatures");

    // Sign the XML...
    success = gen.CreateXmlDSigSb(sbXml);
    if (success != true) {
        wprintf(L"%s\n",gen.lastErrorText());
        return;
    }

    // -----------------------------------------------

    // Save the signed XML to a file.
    success = sbXml.WriteFile(L"qa_output/signedXml.xml",L"utf-8",false);

    wprintf(L"%s\n",sbXml.getAsString());

    // ----------------------------------------
    // Verify the signature we just produced...
    CkXmlDSigW verifier;
    success = verifier.LoadSignatureSb(sbXml);
    if (success != true) {
        wprintf(L"%s\n",verifier.lastErrorText());
        return;
    }

    // We should have just one signature..
    int numSigs = verifier.get_NumSignatures();
    int verifyIdx = 0;
    while (verifyIdx < numSigs) {
        verifier.put_Selector(verifyIdx);
        bool verified = verifier.VerifySignature(true);
        if (verified != true) {
            wprintf(L"%s\n",verifier.lastErrorText());
            return;
        }

        verifyIdx = verifyIdx + 1;
    }

    wprintf(L"The signature was successfully verified.\n");
    }

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.