Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.6.0+

AWS KMS: Sign a PDF with SHA-256

See more AWS KMS Examples

Uses the current AWS KMS API. Configure the region and keep credentials in private qa_data/aws/kms.json, with nonempty string fields access_key and secret_key. The examples do not obtain or refresh credentials. Keep diagnostics private; they may contain credentials or sensitive key data. Create the output directory before running file-writing examples.

This convenience-method example configures an access-key/secret-key pair. It explicitly rejects a supplied session_token instead of silently omitting it: token support for this helper configuration has not been established here. Prefer temporary role credentials in production using a documented token-capable integration. The separate Rest examples support a session token. No credentials or private key material are printed.

Load a local public certificate whose public key matches an enabled asymmetric KMS key with KeyUsage=SIGN_VERIFY. These certificate signing examples target RSA or NIST EC signing keys; they do not demonstrate every KMS algorithm (such as ML-DSA). Set the nonempty string key_id in the private configuration to the actual key ID or ARN in the configured region. A key ARN avoids alias retargeting. The caller needs kms:Sign and access through the key policy. KMS holds the private key and signs the digest; Chilkat creates the document locally. This does not upload the whole document to KMS. A valid signature alone does not establish certificate trust, revocation status, or a trusted signing time.

Creates a visible signature on page 1. The displayed time is local signing time, not a trusted timestamp. This example does not claim LTV or PAdES conformance; OCSP evidence, certificate validation, and a trusted timestamp require additional configuration and validation. It intentionally does not label the result LTV-enabled merely because signing succeeded.

AWS Sign reference

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkCertW.h>
#include <CkPdfW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkStringBuilderW stringCompare;
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Keep this credentials file outside public web content and source control.
    CkJsonObjectW credentials;
    credentials.put_Strict(true);
    success = credentials.LoadFile(L"qa_data/aws/kms.json");
    if (success != true) {
        wprintf(L"Could not load private KMS configuration.\n");
        return;
    }

    stringCompare.SetString(credentials.stringOf(L"access_key"));
    bool stringMatch1 = stringCompare.ContentsEqual(L"",true);
    if ((credentials.JsonTypeOf(L"access_key") != 1) || (stringMatch1 == true)) {
        wprintf(L"Missing or invalid access_key.\n");
        return;
    }

    stringCompare.SetString(credentials.stringOf(L"secret_key"));
    bool stringMatch2 = stringCompare.ContentsEqual(L"",true);
    if ((credentials.JsonTypeOf(L"secret_key") != 1) || (stringMatch2 == true)) {
        wprintf(L"Missing or invalid secret_key.\n");
        return;
    }

    const wchar_t *region = L"us-west-2";
    //  These convenience-method examples use an access-key/secret-key pair.
    //  Do not silently discard the token belonging to temporary credentials.
    if (credentials.HasMember(L"session_token") == true) {
        wprintf(L"This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.\n");
        return;
    }

    stringCompare.SetString(credentials.stringOf(L"key_id"));
    bool stringMatch3 = stringCompare.ContentsEqual(L"",true);
    if ((credentials.JsonTypeOf(L"key_id") != 1) || (stringMatch3 == true)) {
        wprintf(L"Missing or invalid key_id.\n");
        return;
    }

    CkCertW cert;
    success = cert.LoadFromFile(L"qa_data/certs/myCert.cer");
    if (success != true) {
        wprintf(L"Could not load the public signing certificate.\n");
        return;
    }

    CkJsonObjectW cloud;
    //  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
    success = cloud.UpdateString(L"service",L"aws_kms");
    if (success != true) {
        wprintf(L"Could not set service.\n");
        return;
    }

    success = cloud.UpdateString(L"access_key",credentials.stringOf(L"access_key"));
    if (success != true) {
        wprintf(L"Could not set access_key.\n");
        return;
    }

    success = cloud.UpdateString(L"secret_key",credentials.stringOf(L"secret_key"));
    if (success != true) {
        wprintf(L"Could not set secret_key.\n");
        return;
    }

    success = cloud.UpdateString(L"region",region);
    if (success != true) {
        wprintf(L"Could not set region.\n");
        return;
    }

    success = cloud.UpdateString(L"key_id",credentials.stringOf(L"key_id"));
    if (success != true) {
        wprintf(L"Could not set key_id.\n");
        return;
    }

    success = cert.SetCloudSigner(cloud);
    if (success != true) {
        wprintf(L"Could not configure the AWS KMS signer; inspect diagnostics privately.\n");
        return;
    }

    CkPdfW pdf;
    success = pdf.LoadFile(L"qa_data/pdf/helloWorld.pdf");
    if (success != true) {
        wprintf(L"Could not load PDF.\n");
        return;
    }

    CkJsonObjectW options;
    success = options.UpdateInt(L"page",1);
    if (success != true) {
        wprintf(L"Could not set signature page.\n");
        return;
    }

    success = options.UpdateString(L"hashAlgorithm",L"sha256");
    if (success != true) {
        wprintf(L"Could not set hashAlgorithm.\n");
        return;
    }

    success = options.UpdateString(L"appearance.y",L"top");
    if (success != true) {
        wprintf(L"Could not set appearance.y.\n");
        return;
    }

    success = options.UpdateString(L"appearance.x",L"left");
    if (success != true) {
        wprintf(L"Could not set appearance.x.\n");
        return;
    }

    success = options.UpdateString(L"appearance.fontScale",L"10.0");
    if (success != true) {
        wprintf(L"Could not set appearance.fontScale.\n");
        return;
    }

    success = options.UpdateString(L"appearance.text[0]",L"Digitally signed by: cert_cn");
    if (success != true) {
        wprintf(L"Could not set appearance.text[0].\n");
        return;
    }

    success = options.UpdateString(L"appearance.text[1]",L"current_dt");
    if (success != true) {
        wprintf(L"Could not set appearance.text[1].\n");
        return;
    }

    success = pdf.SetSigningCert(cert);
    if (success != true) {
        wprintf(L"Could not select signing certificate.\n");
        return;
    }

    success = pdf.SignPdf(options,L"qa_output/hello_kms_signed.pdf");
    if (success != true) {
        wprintf(L"PDF signing failed; inspect diagnostics privately.\n");
        return;
    }

    wprintf(L"Signed PDF saved to qa_output/hello_kms_signed.pdf\n");
    }