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

AWS KMS: Create an Attached CAdES-BES Signature

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 an attached CMS/PKCS#7 .p7m with SHA-256 and the signingCertificateV2 signed attribute. This is a CAdES-BES example, without a TSA timestamp or long-term validation evidence. The signed input can be any file, not only XML. It is not an XMLDSig signature.

AWS Sign reference

Chilkat C++ Downloads

C++
#include <CkStringBuilder.h>
#include <CkJsonObject.h>
#include <CkCert.h>
#include <CkCrypt2.h>

void ChilkatSample(void)
    {
    bool success = false;

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

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

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

    const char *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 (credentials.HasMember("session_token") == true) {
        std::cout << "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration." << "\r\n";
        return;
    }

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

    CkCert cert;
    success = cert.LoadFromFile("qa_data/certs/myCert.cer");
    if (success != true) {
        std::cout << "Could not load the public signing certificate." << "\r\n";
        return;
    }

    CkJsonObject cloud;
    //  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
    success = cloud.UpdateString("service","aws_kms");
    if (success != true) {
        std::cout << "Could not set service." << "\r\n";
        return;
    }

    success = cloud.UpdateString("access_key",credentials.stringOf("access_key"));
    if (success != true) {
        std::cout << "Could not set access_key." << "\r\n";
        return;
    }

    success = cloud.UpdateString("secret_key",credentials.stringOf("secret_key"));
    if (success != true) {
        std::cout << "Could not set secret_key." << "\r\n";
        return;
    }

    success = cloud.UpdateString("region",region);
    if (success != true) {
        std::cout << "Could not set region." << "\r\n";
        return;
    }

    success = cloud.UpdateString("key_id",credentials.stringOf("key_id"));
    if (success != true) {
        std::cout << "Could not set key_id." << "\r\n";
        return;
    }

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

    CkCrypt2 crypt;
    success = crypt.SetSigningCert(cert);
    if (success != true) {
        std::cout << "Could not select signing certificate." << "\r\n";
        return;
    }

    crypt.put_CadesEnabled(true);
    crypt.put_HashAlgorithm("sha256");
    CkJsonObject signedAttrs;
    success = signedAttrs.UpdateInt("contentType",1);
    if (success != true) {
        std::cout << "Could not set signed attribute." << "\r\n";
        return;
    }

    success = signedAttrs.UpdateInt("signingTime",1);
    if (success != true) {
        std::cout << "Could not set signed attribute." << "\r\n";
        return;
    }

    success = signedAttrs.UpdateInt("messageDigest",1);
    if (success != true) {
        std::cout << "Could not set signed attribute." << "\r\n";
        return;
    }

    success = signedAttrs.UpdateInt("signingCertificateV2",1);
    if (success != true) {
        std::cout << "Could not set signed attribute." << "\r\n";
        return;
    }

    crypt.put_SigningAttributes(signedAttrs.emit());
    //  The attached .p7m includes the original file. KMS signs the digest remotely.
    success = crypt.CreateP7M("qa_data/e-Invoice.xml","qa_output/signed.p7m");
    if (success != true) {
        std::cout << "CAdES signing failed; inspect diagnostics privately." << "\r\n";
        return;
    }

    std::cout << "Attached CAdES-BES signature saved to qa_output/signed.p7m" << "\r\n";
    }