Sample code for 30+ languages & platforms
Unicode C

XAdES using TSA Requiring Client Certificate

See more XML Digital Signatures Examples

Demonstrates how to create an XMLDSig (XAdES) signed document which includes an EncapsulatedTimestamp using a TSA (TimeStamp Authority) server requiring client certificate authentication. One such TSA is https://www3.postsignum.cz/TSS/TSS_crt/

Chilkat Unicode C Downloads

Unicode C
#include <C_CkStringBuilderW.h>
#include <C_CkXmlDSigGenW.h>
#include <C_CkXmlW.h>
#include <C_CkCertW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW sbXml;
    HCkXmlDSigGenW gen;
    HCkXmlW object1;
    HCkCertW cert;
    HCkJsonObjectW jsonTsa;
    HCkHttpW http;

    success = FALSE;

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

    success = TRUE;

    // Load the XML to be signed.  For example, the XML to be signed might contain something like this:

    // <?xml version="1.0" encoding="utf-8"?>
    // <TransakcniLogSystemu xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://nsess.public.cz/erms_trans/v_01_01" Id="Signature1">
    //   <TransLogInfo>
    //     <Identifikator>XYZ ABC</Identifikator>
    //     <DatumVzniku>2022-12-20T14:39:02.3625922+01:00</DatumVzniku>
    //     <DatumCasOd>2022-12-20T14:26:26.88</DatumCasOd>
    //     <DatumCasDo>2022-12-20T14:39:02.287</DatumCasDo>
    //     <Software>XYZ</Software>
    //     <VerzeSoftware>2.0.19.32</VerzeSoftware>
    //   </TransLogInfo>
    //   <Udalosti>
    //     <Udalost>
    //       <Poradi>1</Poradi>
    // ...

    // Load the XML to be signed from a file.
    // (XML can be loaded from other source, such as a string variable.)
    sbXml = CkStringBuilderW_Create();
    success = CkStringBuilderW_LoadFile(sbXml,L"xmlToSign.xml",L"utf-8");

    gen = CkXmlDSigGenW_Create();

    CkXmlDSigGenW_putSigLocation(gen,L"TransakcniLogSystemu");
    CkXmlDSigGenW_putSigLocationMod(gen,0);
    CkXmlDSigGenW_putSigId(gen,L"SignatureID-Signature1");
    CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds");
    CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"C14N");
    CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256");

    // Set the KeyInfoId before adding references..
    CkXmlDSigGenW_putKeyInfoId(gen,L"KeyInfoId-Signature-Signature1");

    // Create an Object to be added to the Signature.

    // Note: Chilkat will automatically fill in the values marked as "TO BE GENERATED BY CHILKAT" at the time of signing.
    // The EncapsulatedTimestamp will be automatically generated.

    object1 = CkXmlW_Create();
    CkXmlW_putTag(object1,L"xades:QualifyingProperties");
    CkXmlW_AddAttribute(object1,L"xmlns:xades",L"http://uri.etsi.org/01903/v1.3.2#");
    CkXmlW_AddAttribute(object1,L"Target",L"#Signature1");

    CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties",TRUE,L"Id",L"SignedProperties-Signature-Signature1");
    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");

    // The EncapsulatedTimestamp will be included in the unsigned properties.
    CkXmlW_UpdateAttrAt(object1,L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp",TRUE,L"Id",L"signature-timestamp-5561-8212-3316-5191");
    CkXmlW_UpdateAttrAt(object1,L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXmlW_UpdateAttrAt(object1,L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",TRUE,L"Encoding",L"http://uri.etsi.org/01903/v1.2.2#DER");
    CkXmlW_UpdateChildContent(object1,L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",L"TO BE GENERATED BY CHILKAT");

    CkXmlDSigGenW_AddObject(gen,L"XadesObjectId-Signature1",CkXmlW_getXml(object1),L"",L"");

    // -------- Reference 1 --------
    CkXmlDSigGenW_AddObjectRef(gen,L"SignedProperties-Signature-Signature1",L"sha256",L"EXCL_C14N",L"",L"http://uri.etsi.org/01903#SignedProperties");

    // -------- Reference 2 --------
    CkXmlDSigGenW_AddSameDocRef(gen,L"KeyInfoId-Signature-Signature1",L"sha256",L"EXCL_C14N",L"",L"");
    CkXmlDSigGenW_SetRefIdAttr(gen,L"KeyInfoId-Signature-Signature1",L"ReferenceKeyInfo");

    // -------- Reference 3 --------
    CkXmlDSigGenW_AddSameDocRef(gen,L"",L"sha256",L"EXCL_C14N",L"",L"");
    CkXmlDSigGenW_SetRefIdAttr(gen,L"",L"Reference-Signature1");

    // 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));
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkCertW_Dispose(cert);
        return;
    }

    CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data");
    CkXmlDSigGenW_putX509Type(gen,L"Certificate");

    CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature");

    // -------------------------------------------------------------------------------------------
    // To have the EncapsulatedTimeStamp automatically added... 
    // 1) Add the <xades:EncapsulatedTimeStamp Encoding="http://uri.etsi.org/01903/v1.2.2#DER">TO BE GENERATED BY CHILKAT</xades:EncapsulatedTimeStamp>
    //    to the unsigned properties.  (This was accomplished in the above code.)
    // 2) Specify the TSA URL (Timestamping Authority URL).
    //    Here we specify the TSA URL:
    // -------------------------------------------------------------------------------------------

    jsonTsa = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(jsonTsa,L"timestampToken.tsaUrl",L"https://www3.postsignum.cz/TSS/TSS_crt/");
    CkJsonObjectW_UpdateBool(jsonTsa,L"timestampToken.requestTsaCert",TRUE);
    CkXmlDSigGenW_SetTsa(gen,jsonTsa);

    // -------------------------------------------------------------------------------------------
    // In this case, the TSA requires client certificate authentication.
    // To provide your client certificate, the application will instantiate a Chilkat HTTP object,
    // then set it up with a SSL/TLS client certificate, and then tell the XmlDSigGen object
    // to use the HTTP object for connections to the TSA server.
    // -------------------------------------------------------------------------------------------
    http = CkHttpW_Create();
    success = CkHttpW_SetSslClientCertPfx(http,L"/home/bob/pfxFiles/myClientSideCertWithPrivateKey.pfx",L"pfxPassword");
    if (success != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(jsonTsa);
        CkHttpW_Dispose(http);
        return;
    }

    // Tell the XmlDSigGen object to use the above HTTP object for TSA communications.
    CkXmlDSigGenW_SetHttpObj(gen,http);

    // Sign the XML...
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(jsonTsa);
        CkHttpW_Dispose(http);
        return;
    }

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

    // Save the signed XML to a file.
    success = CkStringBuilderW_WriteFile(sbXml,L"c:/temp/qa_output/signedXml.xml",L"utf-8",FALSE);

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


    CkStringBuilderW_Dispose(sbXml);
    CkXmlDSigGenW_Dispose(gen);
    CkXmlW_Dispose(object1);
    CkCertW_Dispose(cert);
    CkJsonObjectW_Dispose(jsonTsa);
    CkHttpW_Dispose(http);

    }