Unicode C
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
#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;
HCkXmlW xml1;
HCkXmlW xml2;
HCkCertW cert;
HCkStringBuilderW sbXml;
HCkXmlDSigW verifier;
BOOL verified;
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;
xmlToSign = CkXmlW_Create();
CkXmlW_putTag(xmlToSign,L"InitUpload");
CkXmlW_AddAttribute(xmlToSign,L"xmlns",L"http://e-dokumenty.mf.gov.pl");
CkXmlW_UpdateChildContent(xmlToSign,L"DocumentType",L"JPK");
CkXmlW_UpdateChildContent(xmlToSign,L"Version",L"01.02.01.20160617");
CkXmlW_UpdateAttrAt(xmlToSign,L"EncryptionKey",TRUE,L"algorithm",L"RSA");
CkXmlW_UpdateAttrAt(xmlToSign,L"EncryptionKey",TRUE,L"encoding",L"Base64");
CkXmlW_UpdateAttrAt(xmlToSign,L"EncryptionKey",TRUE,L"mode",L"ECB");
CkXmlW_UpdateAttrAt(xmlToSign,L"EncryptionKey",TRUE,L"padding",L"PKCS#1");
CkXmlW_UpdateChildContent(xmlToSign,L"EncryptionKey",L"...");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FormCode",TRUE,L"schemaVersion",L"1-1");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FormCode",TRUE,L"systemCode",L"JPK_VAT (3)");
CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FormCode",L"JPK_VAT");
CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileName",L"JPK_VAT_3_v1-1_20181208.xml");
CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|ContentLength",L"8736");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|HashValue",TRUE,L"algorithm",L"SHA-256");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|HashValue",TRUE,L"encoding",L"Base64");
CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|HashValue",L"JEEI1pItwh6dj/Xe1uts/x61qnjZ4DLHpkRMhmf1oQQ=");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList",TRUE,L"filesNumber",L"1");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",TRUE,L"mode",L"zip");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Packaging|SplitZip",TRUE,L"type",L"split");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES",TRUE,L"block",L"16");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES",TRUE,L"mode",L"CBC");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES",TRUE,L"padding",L"PKCS#7");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES",TRUE,L"size",L"256");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",TRUE,L"bytes",L"16");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",TRUE,L"encoding",L"Base64");
CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileSignatureList|Encryption|AES|IV",L"FFsCRAPYJD3J6cRvd44UDA==");
CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|OrdinalNumber",L"1");
CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|FileName",L"JPK_VAT_3_v1-1_20181208-000.xml.zip.aes");
CkXmlW_UpdateChildContent(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|ContentLength",L"16");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",TRUE,L"algorithm",L"MD5");
CkXmlW_UpdateAttrAt(xmlToSign,L"DocumentList|Document|FileSignatureList|FileSignature|HashValue",TRUE,L"encoding",L"Base64");
CkXmlW_UpdateChildContent(xmlToSign,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
gen = CkXmlDSigGenW_Create();
CkXmlDSigGenW_putSigLocation(gen,L"InitUpload");
CkXmlDSigGenW_putSigId(gen,L"id-1234");
CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds");
CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_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"Target",L"#id-1234");
CkXmlW_AddAttribute(object1,L"xmlns:xades",L"http://uri.etsi.org/01903/v1.3.2#");
CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties",TRUE,L"Id",L"xades-id-1234");
CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime",L"TO BE GENERATED BY CHILKAT");
CkXmlW_UpdateAttrAt(object1,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");
CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue",L"TO BE GENERATED BY CHILKAT");
CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2",L"TO BE GENERATED BY CHILKAT");
CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat",TRUE,L"ObjectReference",L"#r-id-1");
CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType",L"text/xml");
CkXmlDSigGenW_AddObject(gen,L"",CkXmlW_getXml(object1),L"",L"");
// -------- Reference 1 --------
xml1 = CkXmlW_Create();
CkXmlW_putTag(xml1,L"ds:Transforms");
CkXmlW_UpdateAttrAt(xml1,L"ds:Transform",TRUE,L"Algorithm",L"http://www.w3.org/TR/1999/REC-xpath-19991116");
CkXmlW_UpdateChildContent(xml1,L"ds:Transform|ds:XPath",L"not(ancestor-or-self::ds:Signature)");
CkXmlW_UpdateAttrAt(xml1,L"ds:Transform[1]",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");
CkXmlDSigGenW_AddSameDocRef2(gen,L"",L"sha256",xml1,L"");
CkXmlDSigGenW_SetRefIdAttr(gen,L"",L"r-id-1");
// -------- Reference 2 --------
xml2 = CkXmlW_Create();
CkXmlW_putTag(xml2,L"ds:Transforms");
CkXmlW_UpdateAttrAt(xml2,L"ds:Transform",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");
CkXmlDSigGenW_AddObjectRef2(gen,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..
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);
CkXmlW_Dispose(xml1);
CkXmlW_Dispose(xml2);
CkCertW_Dispose(cert);
return;
}
CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);
CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data");
CkXmlDSigGenW_putX509Type(gen,L"Certificate");
// Load XML to be signed...
sbXml = CkStringBuilderW_Create();
CkXmlW_GetXmlSb(xmlToSign,sbXml);
CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature,TransformSignatureXPath,IssuerSerialHex");
// 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);
CkXmlW_Dispose(xml1);
CkXmlW_Dispose(xml2);
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);
CkXmlW_Dispose(xml1);
CkXmlW_Dispose(xml2);
CkCertW_Dispose(cert);
CkStringBuilderW_Dispose(sbXml);
CkXmlDSigW_Dispose(verifier);
return;
}
verified = CkXmlDSigW_VerifySignature(verifier,TRUE);
if (verified != TRUE) {
wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(object1);
CkXmlW_Dispose(xml1);
CkXmlW_Dispose(xml2);
CkCertW_Dispose(cert);
CkStringBuilderW_Dispose(sbXml);
CkXmlDSigW_Dispose(verifier);
return;
}
wprintf(L"This signature was successfully verified.\n");
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(object1);
CkXmlW_Dispose(xml1);
CkXmlW_Dispose(xml2);
CkCertW_Dispose(cert);
CkStringBuilderW_Dispose(sbXml);
CkXmlDSigW_Dispose(verifier);
}