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

AWS KMS: Sign a SOAP Body with XMLDSig and 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.

Signs the SOAP Body by its ID using exclusive canonicalization and SHA-256 for both the reference digest and SignedInfo signature. Embeds the public certificate and verifies the signature and reference before saving. This is XMLDSig, not XAdES: no XAdES qualifying properties are created. Adapt WS-Security namespaces and profile requirements to the receiving service; the unsigned SOAP headers are not covered by this Body-only signature.

AWS Sign reference

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW stringCompare;
    HCkJsonObjectW credentials;
    BOOL stringMatch1;
    BOOL stringMatch2;
    const wchar_t *region;
    BOOL stringMatch3;
    HCkCertW cert;
    HCkJsonObjectW cloud;
    HCkStringBuilderW sbXml;
    BOOL bCrlf;
    HCkXmlDSigGenW gen;
    HCkXmlDSigW verifier;

    success = FALSE;

    stringCompare = CkStringBuilderW_Create();
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Keep this credentials file outside public web content and source control.
    credentials = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(credentials,TRUE);
    success = CkJsonObjectW_LoadFile(credentials,L"qa_data/aws/kms.json");
    if (success != TRUE) {
        wprintf(L"Could not load private KMS configuration.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(credentials,L"access_key"));
    stringMatch1 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(credentials,L"access_key") != 1) || (stringMatch1 == TRUE)) {
        wprintf(L"Missing or invalid access_key.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(credentials,L"secret_key"));
    stringMatch2 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(credentials,L"secret_key") != 1) || (stringMatch2 == TRUE)) {
        wprintf(L"Missing or invalid secret_key.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        return;
    }

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

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(credentials,L"key_id"));
    stringMatch3 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(credentials,L"key_id") != 1) || (stringMatch3 == TRUE)) {
        wprintf(L"Missing or invalid key_id.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        return;
    }

    cert = CkCertW_Create();
    success = CkCertW_LoadFromFile(cert,L"qa_data/certs/myCert.cer");
    if (success != TRUE) {
        wprintf(L"Could not load the public signing certificate.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        return;
    }

    cloud = CkJsonObjectW_Create();
    //  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
    success = CkJsonObjectW_UpdateString(cloud,L"service",L"aws_kms");
    if (success != TRUE) {
        wprintf(L"Could not set service.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        return;
    }

    success = CkJsonObjectW_UpdateString(cloud,L"access_key",CkJsonObjectW_stringOf(credentials,L"access_key"));
    if (success != TRUE) {
        wprintf(L"Could not set access_key.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        return;
    }

    success = CkJsonObjectW_UpdateString(cloud,L"secret_key",CkJsonObjectW_stringOf(credentials,L"secret_key"));
    if (success != TRUE) {
        wprintf(L"Could not set secret_key.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        return;
    }

    success = CkJsonObjectW_UpdateString(cloud,L"region",region);
    if (success != TRUE) {
        wprintf(L"Could not set region.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        return;
    }

    success = CkJsonObjectW_UpdateString(cloud,L"key_id",CkJsonObjectW_stringOf(credentials,L"key_id"));
    if (success != TRUE) {
        wprintf(L"Could not set key_id.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        return;
    }

    success = CkCertW_SetCloudSigner(cert,cloud);
    if (success != TRUE) {
        wprintf(L"Could not configure the AWS KMS signer; inspect diagnostics privately.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        return;
    }

    //  Create the XML to be signed.
    sbXml = CkStringBuilderW_Create();

    bCrlf = TRUE;
    CkStringBuilderW_AppendLine(sbXml,L"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>",bCrlf);
    CkStringBuilderW_AppendLine(sbXml,L"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">",bCrlf);
    CkStringBuilderW_AppendLine(sbXml,L"    <SOAP-ENV:Header>",bCrlf);
    CkStringBuilderW_AppendLine(sbXml,L"        <wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" SOAP-ENV:mustUnderstand=\"1\"></wsse:Security>",bCrlf);
    CkStringBuilderW_AppendLine(sbXml,L"    </SOAP-ENV:Header>",bCrlf);
    CkStringBuilderW_AppendLine(sbXml,L"    <SOAP-ENV:Body xmlns:SOAP-SEC=\"http://schemas.xmlsoap.org/soap/security/2000-12\" SOAP-SEC:id=\"Body\">",bCrlf);
    CkStringBuilderW_AppendLine(sbXml,L"        <z:FooBar xmlns:z=\"http://example.com\" />",bCrlf);
    CkStringBuilderW_AppendLine(sbXml,L"    </SOAP-ENV:Body>",bCrlf);
    CkStringBuilderW_AppendLine(sbXml,L"</SOAP-ENV:Envelope>",bCrlf);

    gen = CkXmlDSigGenW_Create();
    CkXmlDSigGenW_putSigLocation(gen,L"SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security");
    CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N");
    CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256");
    CkXmlDSigGenW_putSignedInfoPrefixList(gen,L"wsse SOAP-ENV");
    CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data");
    CkXmlDSigGenW_putX509Type(gen,L"Certificate");
    //  The reference selects the element with SOAP-SEC:id="Body".
    success = CkXmlDSigGenW_AddSameDocRef(gen,L"Body",L"sha256",L"EXCL_C14N",L"",L"");
    if (success != TRUE) {
        wprintf(L"Could not add the SOAP Body reference.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        return;
    }

    success = CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);
    if (success != TRUE) {
        wprintf(L"Could not select signing certificate.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        return;
    }

    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        wprintf(L"XML signing failed; inspect diagnostics privately.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        return;
    }

    //  Verify the signature and references before saving. This does not validate trust.
    verifier = CkXmlDSigW_Create();
    success = CkXmlDSigW_LoadSignatureSb(verifier,sbXml);
    if (success != TRUE) {
        wprintf(L"Could not load generated signature.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlDSigW_Dispose(verifier);
        return;
    }

    success = CkXmlDSigW_VerifySignature(verifier,TRUE);
    if (success != TRUE) {
        wprintf(L"Generated XML signature or reference verification failed.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlDSigW_Dispose(verifier);
        return;
    }

    success = CkStringBuilderW_WriteFile(sbXml,L"qa_output/kms_signed.xml",L"utf-8",FALSE);
    if (success != TRUE) {
        wprintf(L"Could not save signed XML.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkCertW_Dispose(cert);
        CkJsonObjectW_Dispose(cloud);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlDSigW_Dispose(verifier);
        return;
    }

    wprintf(L"Verified XMLDSig saved to qa_output/kms_signed.xml\n");


    CkStringBuilderW_Dispose(stringCompare);
    CkJsonObjectW_Dispose(credentials);
    CkCertW_Dispose(cert);
    CkJsonObjectW_Dispose(cloud);
    CkStringBuilderW_Dispose(sbXml);
    CkXmlDSigGenW_Dispose(gen);
    CkXmlDSigW_Dispose(verifier);

    }