Sample code for 30+ languages & platforms
Unicode C++

Create JPK_VAT XaDES-BES Signed XML

See more XAdES Examples

Demonstrates how to sign XML for JPK_VAT.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkXmlW.h>
#include <CkXmlDSigGenW.h>
#include <CkCertW.h>
#include <CkStringBuilderW.h>
#include <CkXmlDSigW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    // This example will sign the following XML document:

    // <?xml version="1.0" encoding="utf-8"?>
    // <InitUpload xmlns="http://e-dokumenty.mf.gov.pl">
    //     <DocumentType>JPK</DocumentType>
    //     <Version>01.02.01.20160617</Version>
    //     <EncryptionKey algorithm="RSA" encoding="Base64" mode="ECB" padding="PKCS#1">...</EncryptionKey>
    //     <DocumentList>
    //         <Document>
    //             <FormCode schemaVersion="1-1" systemCode="JPK_VAT (3)">JPK_VAT</FormCode>
    //             <FileName>JPK_VAT_3_v1-1_20181208.xml</FileName>
    //             <ContentLength>8736</ContentLength>
    //             <HashValue algorithm="SHA-256" encoding="Base64">JEEI1pItwh6dj/Xe1uts/x61qnjZ4DLHpkRMhmf1oQQ=</HashValue>
    //             <FileSignatureList filesNumber="1">
    //                 <Packaging>
    //                     <SplitZip mode="zip" type="split"/>
    //                 </Packaging>
    //                 <Encryption>
    //                     <AES block="16" mode="CBC" padding="PKCS#7" size="256">
    //                         <IV bytes="16" encoding="Base64">FFsCRAPYJD3J6cRvd44UDA==</IV>
    //                     </AES>
    //                 </Encryption>
    //                 <FileSignature>
    //                     <OrdinalNumber>1</OrdinalNumber>
    //                     <FileName>JPK_VAT_3_v1-1_20181208-000.xml.zip.aes</FileName>
    //                     <ContentLength>16</ContentLength>
    //                     <HashValue algorithm="MD5" encoding="Base64">BX2DTD3ASC/zF6aq/012Cg==</HashValue>
    //                 </FileSignature>
    //             </FileSignatureList>
    //         </Document>
    //     </DocumentList>
    // </InitUpload>

    // First we build the XML to be signed.
    // 
    // Use this online tool to generate the code from sample XML: 
    // Generate Code to Create XML

    success = true;
    CkXmlW xmlToSign;
    xmlToSign.put_Tag(L"InitUpload");
    xmlToSign.AddAttribute(L"xmlns",L"http://e-dokumenty.mf.gov.pl");
    xmlToSign.UpdateChildContent(L"DocumentType",L"JPK");
    xmlToSign.UpdateChildContent(L"Version",L"01.02.01.20160617");
    xmlToSign.UpdateAttrAt(L"EncryptionKey",true,L"algorithm",L"RSA");
    xmlToSign.UpdateAttrAt(L"EncryptionKey",true,L"encoding",L"Base64");
    xmlToSign.UpdateAttrAt(L"EncryptionKey",true,L"mode",L"ECB");
    xmlToSign.UpdateAttrAt(L"EncryptionKey",true,L"padding",L"PKCS#1");
    xmlToSign.UpdateChildContent(L"EncryptionKey",L"...");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FormCode",true,L"schemaVersion",L"1-1");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FormCode",true,L"systemCode",L"JPK_VAT (3)");
    xmlToSign.UpdateChildContent(L"DocumentList|Document|FormCode",L"JPK_VAT");
    xmlToSign.UpdateChildContent(L"DocumentList|Document|FileName",L"JPK_VAT_3_v1-1_20181208.xml");
    xmlToSign.UpdateChildContent(L"DocumentList|Document|ContentLength",L"8736");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|HashValue",true,L"algorithm",L"SHA-256");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|HashValue",true,L"encoding",L"Base64");
    xmlToSign.UpdateChildContent(L"DocumentList|Document|HashValue",L"JEEI1pItwh6dj/Xe1uts/x61qnjZ4DLHpkRMhmf1oQQ=");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList",true,L"filesNumber",L"1");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",true,L"mode",L"zip");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",true,L"type",L"split");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"block",L"16");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"mode",L"CBC");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"padding",L"PKCS#7");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES",true,L"size",L"256");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",true,L"bytes",L"16");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",true,L"encoding",L"Base64");
    xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",L"FFsCRAPYJD3J6cRvd44UDA==");
    xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber",L"1");
    xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|FileName",L"JPK_VAT_3_v1-1_20181208-000.xml.zip.aes");
    xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|ContentLength",L"16");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",true,L"algorithm",L"MD5");
    xmlToSign.UpdateAttrAt(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",true,L"encoding",L"Base64");
    xmlToSign.UpdateChildContent(L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",L"BX2DTD3ASC/zF6aq/012Cg==");

    // Also see the online tool to generate the code from sample already-signed XML: 
    // Generate XML Signature Creation Code from an Already-Signed XML Sample

    CkXmlDSigGenW gen;

    gen.put_SigLocation(L"InitUpload");
    gen.put_SigId(L"id-1234");
    gen.put_SigNamespacePrefix(L"ds");
    gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#");
    gen.put_SignedInfoCanonAlg(L"EXCL_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"Target",L"#id-1234");
    object1.AddAttribute(L"xmlns:xades",L"http://uri.etsi.org/01903/v1.3.2#");
    object1.UpdateAttrAt(L"xades:SignedProperties",true,L"Id",L"xades-id-1234");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateAttrAt(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod",true,L"Algorithm",L"http://www.w3.org/2001/04/xmlenc#sha256");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateAttrAt(L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat",true,L"ObjectReference",L"#r-id-1");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType",L"text/xml");

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

    // -------- Reference 1 --------
    CkXmlW xml1;
    xml1.put_Tag(L"ds:Transforms");
    xml1.UpdateAttrAt(L"ds:Transform",true,L"Algorithm",L"http://www.w3.org/TR/1999/REC-xpath-19991116");
    xml1.UpdateChildContent(L"ds:Transform|ds:XPath",L"not(ancestor-or-self::ds:Signature)");
    xml1.UpdateAttrAt(L"ds:Transform[1]",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");

    gen.AddSameDocRef2(L"",L"sha256",xml1,L"");
    gen.SetRefIdAttr(L"",L"r-id-1");

    // -------- Reference 2 --------
    CkXmlW xml2;
    xml2.put_Tag(L"ds:Transforms");
    xml2.UpdateAttrAt(L"ds:Transform",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");

    gen.AddObjectRef2(L"xades-id-1234",L"sha256",xml2,L"http://uri.etsi.org/01903#SignedProperties");

    // Provide a certificate + private key. (PFX password is test123)
    // See Load Certificate on Smartcard for an example showing how to load the cert from a smartcard..
    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");
    gen.put_X509Type(L"Certificate");

    // Load XML to be signed...
    CkStringBuilderW sbXml;
    xmlToSign.GetXmlSb(sbXml);

    gen.put_Behaviors(L"IndentedSignature,TransformSignatureXPath,IssuerSerialHex");

    // 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;
    }

    bool verified = verifier.VerifySignature(true);
    if (verified != true) {
        wprintf(L"%s\n",verifier.lastErrorText());
        return;
    }

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