Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkCert.h>
#include <C_CkPdf.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilder stringCompare;
    HCkJsonObject credentials;
    BOOL stringMatch1;
    BOOL stringMatch2;
    const char *region;
    BOOL stringMatch3;
    HCkCert cert;
    HCkJsonObject cloud;
    HCkPdf pdf;
    HCkJsonObject options;

    success = FALSE;

    stringCompare = CkStringBuilder_Create();
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Keep this credentials file outside public web content and source control.
    credentials = CkJsonObject_Create();
    CkJsonObject_putStrict(credentials,TRUE);
    success = CkJsonObject_LoadFile(credentials,"qa_data/aws/kms.json");
    if (success != TRUE) {
        printf("Could not load private KMS configuration.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        return;
    }

    CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"access_key"));
    stringMatch1 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
    if ((CkJsonObject_JsonTypeOf(credentials,"access_key") != 1) || (stringMatch1 == TRUE)) {
        printf("Missing or invalid access_key.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        return;
    }

    CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"secret_key"));
    stringMatch2 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
    if ((CkJsonObject_JsonTypeOf(credentials,"secret_key") != 1) || (stringMatch2 == TRUE)) {
        printf("Missing or invalid secret_key.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        return;
    }

    region = "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 (CkJsonObject_HasMember(credentials,"session_token") == TRUE) {
        printf("This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        return;
    }

    CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"key_id"));
    stringMatch3 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
    if ((CkJsonObject_JsonTypeOf(credentials,"key_id") != 1) || (stringMatch3 == TRUE)) {
        printf("Missing or invalid key_id.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        return;
    }

    cert = CkCert_Create();
    success = CkCert_LoadFromFile(cert,"qa_data/certs/myCert.cer");
    if (success != TRUE) {
        printf("Could not load the public signing certificate.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        return;
    }

    cloud = CkJsonObject_Create();
    //  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
    success = CkJsonObject_UpdateString(cloud,"service","aws_kms");
    if (success != TRUE) {
        printf("Could not set service.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        return;
    }

    success = CkJsonObject_UpdateString(cloud,"access_key",CkJsonObject_stringOf(credentials,"access_key"));
    if (success != TRUE) {
        printf("Could not set access_key.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        return;
    }

    success = CkJsonObject_UpdateString(cloud,"secret_key",CkJsonObject_stringOf(credentials,"secret_key"));
    if (success != TRUE) {
        printf("Could not set secret_key.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        return;
    }

    success = CkJsonObject_UpdateString(cloud,"region",region);
    if (success != TRUE) {
        printf("Could not set region.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        return;
    }

    success = CkJsonObject_UpdateString(cloud,"key_id",CkJsonObject_stringOf(credentials,"key_id"));
    if (success != TRUE) {
        printf("Could not set key_id.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        return;
    }

    success = CkCert_SetCloudSigner(cert,cloud);
    if (success != TRUE) {
        printf("Could not configure the AWS KMS signer; inspect diagnostics privately.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        return;
    }

    pdf = CkPdf_Create();
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/helloWorld.pdf");
    if (success != TRUE) {
        printf("Could not load PDF.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        return;
    }

    options = CkJsonObject_Create();
    success = CkJsonObject_UpdateInt(options,"page",1);
    if (success != TRUE) {
        printf("Could not set signature page.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(options);
        return;
    }

    success = CkJsonObject_UpdateString(options,"hashAlgorithm","sha256");
    if (success != TRUE) {
        printf("Could not set hashAlgorithm.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(options);
        return;
    }

    success = CkJsonObject_UpdateString(options,"appearance.y","top");
    if (success != TRUE) {
        printf("Could not set appearance.y.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(options);
        return;
    }

    success = CkJsonObject_UpdateString(options,"appearance.x","left");
    if (success != TRUE) {
        printf("Could not set appearance.x.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(options);
        return;
    }

    success = CkJsonObject_UpdateString(options,"appearance.fontScale","10.0");
    if (success != TRUE) {
        printf("Could not set appearance.fontScale.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(options);
        return;
    }

    success = CkJsonObject_UpdateString(options,"appearance.text[0]","Digitally signed by: cert_cn");
    if (success != TRUE) {
        printf("Could not set appearance.text[0].\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(options);
        return;
    }

    success = CkJsonObject_UpdateString(options,"appearance.text[1]","current_dt");
    if (success != TRUE) {
        printf("Could not set appearance.text[1].\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(options);
        return;
    }

    success = CkPdf_SetSigningCert(pdf,cert);
    if (success != TRUE) {
        printf("Could not select signing certificate.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(options);
        return;
    }

    success = CkPdf_SignPdf(pdf,options,"qa_output/hello_kms_signed.pdf");
    if (success != TRUE) {
        printf("PDF signing failed; inspect diagnostics privately.\n");
        CkStringBuilder_Dispose(stringCompare);
        CkJsonObject_Dispose(credentials);
        CkCert_Dispose(cert);
        CkJsonObject_Dispose(cloud);
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(options);
        return;
    }

    printf("Signed PDF saved to qa_output/hello_kms_signed.pdf\n");


    CkStringBuilder_Dispose(stringCompare);
    CkJsonObject_Dispose(credentials);
    CkCert_Dispose(cert);
    CkJsonObject_Dispose(cloud);
    CkPdf_Dispose(pdf);
    CkJsonObject_Dispose(options);

    }