Sample code for 30+ languages & platforms
Unicode C

Create a CAdES-T Signature

See more CAdES Examples

Demonstrates how to create a signature with an external timestamp that certifies the time of signing. This requires an online TSA (Time Stamping Authority) service that is capable of producing RFC 3161 compliant timestamps.

Note: This example requires Chilkat v9.5.0.78 or greater.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkCrypt2W.h>
#include <C_CkCertW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCrypt2W crypt;
    HCkCertW cert;
    const wchar_t *pfxPath;
    const wchar_t *pfxPassword;
    HCkJsonObjectW attrs;
    const wchar_t *inFile;
    const wchar_t *outFile;

    success = FALSE;

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

    crypt = CkCrypt2W_Create();

    // This example will use a certificate + private key from a .pfx/.p12 file.
    // On Windows systems, it is also possible to use certs on smartcards/usb tokens or certs pre-installed in the Windows registry.
    cert = CkCertW_Create();

    pfxPath = L"qa_data/pfx/myCertAndKey.p12";
    pfxPassword = L"test123";

    success = CkCertW_LoadPfxFile(cert,pfxPath,pfxPassword);
    if (success != TRUE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkCrypt2W_Dispose(crypt);
        CkCertW_Dispose(cert);
        return;
    }

    success = CkCrypt2W_SetSigningCert(crypt,cert);

    // Use SHA-256 rather than the default of SHA-1
    CkCrypt2W_putHashAlgorithm(crypt,L"sha256");

    // Create JSON that tells Chilkat what signing attributes to include:
    attrs = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateBool(attrs,L"contentType",TRUE);
    CkJsonObjectW_UpdateBool(attrs,L"signingTime",TRUE);
    CkJsonObjectW_UpdateBool(attrs,L"messageDigest",TRUE);
    CkJsonObjectW_UpdateBool(attrs,L"signingCertificateV2",TRUE);

    // A CAdES-T signature is one that includes a timestampToken created by an online TSA (time stamping authority).
    // We must include the TSA's URL, as well as a few options to indicate what is desired.
    // Except for the TSA URL, the options shown here are typically what you would need.
    CkJsonObjectW_UpdateBool(attrs,L"timestampToken.enabled",TRUE);
    CkJsonObjectW_UpdateString(attrs,L"timestampToken.tsaUrl",L"https://freetsa.org/tsr");
    CkJsonObjectW_UpdateBool(attrs,L"timestampToken.addNonce",FALSE);
    CkJsonObjectW_UpdateBool(attrs,L"timestampToken.requestTsaCert",TRUE);
    CkJsonObjectW_UpdateString(attrs,L"timestampToken.hashAlg",L"sha256");

    CkCrypt2W_putSigningAttributes(crypt,CkJsonObjectW_emit(attrs));

    inFile = L"qa_data/json/sample.json";
    outFile = L"qa_output/sample_cades_t.p7m";

    // This creates the CAdES-T signature.  During the signature creation, it
    // communicates with the TSA to get a timestampToken.
    success = CkCrypt2W_CreateP7M(crypt,inFile,outFile);
    if (success != TRUE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkCrypt2W_Dispose(crypt);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(attrs);
        return;
    }

    wprintf(L"Success.\n");


    CkCrypt2W_Dispose(crypt);
    CkCertW_Dispose(cert);
    CkJsonObjectW_Dispose(attrs);

    }