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

AWS KMS: Get a Public Key and Verify an ECDSA Signature Locally

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.

Uses the AWS JSON 1.1 protocol with TrentService targets, service kms, HTTPS certificate verification, and Signature Version 4. When using temporary credentials, also provide a nonempty session_token; this Rest example includes it in the signed query on each request. Never log that URL. The endpoint shown is for the commercial AWS partition; use the appropriate endpoint for other partitions. Failures stop the sample; production retry policies should use bounded backoff with jitter and reconcile uncertain writes.

Where a key is required, set string key_id to its fixed key ARN in the configured region. Additional named fields and input files are described below. Keep output files private when they contain plaintext or policies. The code does not automatically retry mutations, refresh credentials, or create output directories. A successful request can precede propagation of a state change. Discard sensitive objects promptly; clearing one buffer does not erase copies held in strings, JSON, or runtime memory.

Uses an enabled ECC_NIST_P256 SIGN_VERIFY key. RAW messages are limited to 4096 bytes. DIGEST mode sends a SHA-256 digest rather than hashing it again; it can sign a larger local message. The SHA-256 algorithm is explicit and the returned key/algorithm are checked. GetPublicKey is obtained from the authenticated KMS endpoint for the pinned key. The public key is DER SubjectPublicKeyInfo; ECDSA signatures are ASN.1 DER, not a raw r||s blob.

API actions used: kms:DescribeKey, kms:Sign, kms:GetPublicKey. IAM permissions and key policies must allow the actual resources; grant and multi-Region workflows can need permissions in more than one region/account.

DescribeKey | Sign | GetPublicKey

Chilkat Unicode C Downloads

Unicode C
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkRestW.h>
#include <C_CkSocketW.h>
#include <C_CkBinDataW.h>
#include <C_CkCrypt2W.h>
#include <C_CkPublicKeyW.h>
#include <C_CkEccW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW stringCompare;
    HCkJsonObjectW credentials;
    BOOL stringMatch1;
    BOOL stringMatch2;
    const wchar_t *region;
    HCkStringBuilderW endpoint;
    HCkAuthAwsW aws;
    HCkRestW rest;
    BOOL stringMatch3;
    HCkSocketW connection;
    HCkJsonObjectW body;
    HCkJsonObjectW result;
    const wchar_t *responseJson;
    BOOL stringMatch4;
    const wchar_t *keyId;
    BOOL stringMatch5;
    BOOL stringMatch6;
    BOOL stringMatch7;
    BOOL stringMatch8;
    BOOL stringMatch9;
    BOOL stringMatch10;
    BOOL stringMatch11;
    BOOL stringMatch12;
    HCkBinDataW message;
    HCkCrypt2W hash;
    const wchar_t *digestBase64;
    BOOL stringMatch13;
    BOOL stringMatch14;
    BOOL stringMatch15;
    BOOL stringMatch16;
    BOOL stringMatch17;
    HCkBinDataW signature;
    BOOL stringMatch18;
    BOOL stringMatch19;
    BOOL stringMatch20;
    BOOL stringMatch21;
    BOOL stringMatch22;
    BOOL stringMatch23;
    BOOL stringMatch24;
    HCkPublicKeyW publicKey;
    HCkEccW ecc;
    int verification;

    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";
    endpoint = CkStringBuilderW_Create();
    CkStringBuilderW_Append(endpoint,L"kms.");
    CkStringBuilderW_Append(endpoint,region);
    CkStringBuilderW_Append(endpoint,L".amazonaws.com");
    aws = CkAuthAwsW_Create();
    CkAuthAwsW_putAccessKey(aws,CkJsonObjectW_stringOf(credentials,L"access_key"));
    CkAuthAwsW_putSecretKey(aws,CkJsonObjectW_stringOf(credentials,L"secret_key"));
    CkAuthAwsW_putRegion(aws,region);
    CkAuthAwsW_putServiceName(aws,L"kms");
    CkAuthAwsW_putSignatureVersion(aws,4);
    rest = CkRestW_Create();
    CkRestW_putConnectTimeoutMs(rest,20000);
    CkRestW_putIdleTimeoutMs(rest,60000);
    CkRestW_putVerboseLogging(rest,FALSE);
    success = CkRestW_SetAuthAws(rest,aws);
    if (success != TRUE) {
        wprintf(L"Could not configure AWS signing.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        return;
    }

    if (CkJsonObjectW_HasMember(credentials,L"session_token") == TRUE) {
        CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(credentials,L"session_token"));
        stringMatch3 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
        if ((CkJsonObjectW_JsonTypeOf(credentials,L"session_token") != 1) || (stringMatch3 == TRUE)) {
            wprintf(L"Missing or invalid session_token.\n");
            CkStringBuilderW_Dispose(stringCompare);
            CkJsonObjectW_Dispose(credentials);
            CkStringBuilderW_Dispose(endpoint);
            CkAuthAwsW_Dispose(aws);
            CkRestW_Dispose(rest);
            return;
        }

    }

    connection = CkSocketW_Create();
    CkSocketW_putRequireSslCertVerify(connection,TRUE);
    CkSocketW_putMaxReadIdleMs(connection,60000);
    CkSocketW_putMaxSendIdleMs(connection,60000);
    success = CkSocketW_Connect(connection,CkStringBuilderW_getAsString(endpoint),443,TRUE,20000);
    if (success != TRUE) {
        wprintf(L"TLS connection failed; inspect diagnostics privately.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        return;
    }

    success = CkRestW_UseConnection(rest,connection,TRUE);
    if (success != TRUE) {
        wprintf(L"Could not attach TLS connection.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        return;
    }

    //  Keep connection alive while Rest uses it.
    body = CkJsonObjectW_Create();
    result = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(result,TRUE);

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(credentials,L"key_id"));
    stringMatch4 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(credentials,L"key_id") != 1) || (stringMatch4 == TRUE)) {
        wprintf(L"Missing or invalid key_id.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    keyId = CkJsonObjectW_stringOf(credentials,L"key_id");
    CkJsonObjectW_Clear(body);
    success = CkJsonObjectW_UpdateString(body,L"KeyId",keyId);
    if (success != TRUE) {
        wprintf(L"Could not set KeyId.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    success = CkRestW_ClearAllQueryParams(rest);
    if (success != TRUE) {
        wprintf(L"Could not clear previous query parameters.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    //  Keep the JSON operation body separate from authentication parameters.
    //  Include STS credentials in the signed query; never log the request URL.
    if (CkJsonObjectW_HasMember(credentials,L"session_token") == TRUE) {
        success = CkRestW_AddQueryParam(rest,L"X-Amz-Security-Token",CkJsonObjectW_stringOf(credentials,L"session_token"));
        if (success != TRUE) {
            wprintf(L"Could not set session token.\n");
            CkStringBuilderW_Dispose(stringCompare);
            CkJsonObjectW_Dispose(credentials);
            CkStringBuilderW_Dispose(endpoint);
            CkAuthAwsW_Dispose(aws);
            CkRestW_Dispose(rest);
            CkSocketW_Dispose(connection);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            return;
        }

    }

    success = CkRestW_AddHeader(rest,L"X-Amz-Target",L"TrentService.DescribeKey");
    if (success != TRUE) {
        wprintf(L"Could not set operation.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    success = CkRestW_AddHeader(rest,L"Content-Type",L"application/x-amz-json-1.1");
    if (success != TRUE) {
        wprintf(L"Could not set content type.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    responseJson = CkRestW_fullRequestString(rest,L"POST",L"/",CkJsonObjectW_emit(body));
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"KMS HTTP status: %d\n",CkRestW_getResponseStatusCode(rest));
        success = CkJsonObjectW_Load(result,responseJson);
        if (success == TRUE) {
            if (CkJsonObjectW_JsonTypeOf(result,L"__type") == 1) {
                wprintf(L"AWS error code: %s\n",CkJsonObjectW_stringOf(result,L"__type"));
            }

        }

        wprintf(L"AWS request ID: %s\n",CkRestW_responseHdrByName(rest,L"x-amzn-RequestId"));
        //  Do not print full bodies, headers, credentials, key material, or tokens.
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    success = CkJsonObjectW_Load(result,responseJson);
    if (success != TRUE) {
        wprintf(L"Expected a well-formed JSON response.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyMetadata.Arn"));
    stringMatch5 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"KeyMetadata.Arn") != 1) || (stringMatch5 == TRUE)) {
        wprintf(L"Missing or invalid KeyMetadata.Arn.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyMetadata.KeyId"));
    stringMatch6 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"KeyMetadata.KeyId") != 1) || (stringMatch6 == TRUE)) {
        wprintf(L"Missing or invalid KeyMetadata.KeyId.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyMetadata.KeySpec"));
    stringMatch7 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"KeyMetadata.KeySpec") != 1) || (stringMatch7 == TRUE)) {
        wprintf(L"Missing or invalid KeyMetadata.KeySpec.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyMetadata.KeySpec"));
    stringMatch8 = CkStringBuilderW_ContentsEqual(stringCompare,L"ECC_NIST_P256",TRUE);
    if (stringMatch8 != TRUE) {
        wprintf(L"Unexpected KeyMetadata.KeySpec.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyMetadata.KeyUsage"));
    stringMatch9 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"KeyMetadata.KeyUsage") != 1) || (stringMatch9 == TRUE)) {
        wprintf(L"Missing or invalid KeyMetadata.KeyUsage.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyMetadata.KeyUsage"));
    stringMatch10 = CkStringBuilderW_ContentsEqual(stringCompare,L"SIGN_VERIFY",TRUE);
    if (stringMatch10 != TRUE) {
        wprintf(L"Unexpected KeyMetadata.KeyUsage.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyMetadata.KeyState"));
    stringMatch11 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"KeyMetadata.KeyState") != 1) || (stringMatch11 == TRUE)) {
        wprintf(L"Missing or invalid KeyMetadata.KeyState.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyMetadata.KeyState"));
    stringMatch12 = CkStringBuilderW_ContentsEqual(stringCompare,L"Enabled",TRUE);
    if (stringMatch12 != TRUE) {
        wprintf(L"Unexpected KeyMetadata.KeyState.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    keyId = CkJsonObjectW_stringOf(result,L"KeyMetadata.Arn");
    message = CkBinDataW_Create();
    success = CkBinDataW_LoadFile(message,L"qa_data/kms/message.bin");
    if (success != TRUE) {
        wprintf(L"Could not load qa_data/kms/message.bin.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        return;
    }

    hash = CkCrypt2W_Create();
    CkCrypt2W_putHashAlgorithm(hash,L"sha256");
    CkCrypt2W_putEncodingMode(hash,L"base64");
    digestBase64 = CkCrypt2W_hashBdENC(hash,message);
    if (CkCrypt2W_getLastMethodSuccess(hash) != TRUE) {
        wprintf(L"Could not hash message.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    CkJsonObjectW_Clear(body);
    success = CkJsonObjectW_UpdateString(body,L"KeyId",keyId);
    if (success != TRUE) {
        wprintf(L"Could not set KeyId.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    success = CkJsonObjectW_UpdateString(body,L"Message",digestBase64);
    if (success != TRUE) {
        wprintf(L"Could not set Message.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    success = CkJsonObjectW_UpdateString(body,L"MessageType",L"DIGEST");
    if (success != TRUE) {
        wprintf(L"Could not set MessageType.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    success = CkJsonObjectW_UpdateString(body,L"SigningAlgorithm",L"ECDSA_SHA_256");
    if (success != TRUE) {
        wprintf(L"Could not set SigningAlgorithm.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    success = CkRestW_ClearAllQueryParams(rest);
    if (success != TRUE) {
        wprintf(L"Could not clear previous query parameters.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    //  Keep the JSON operation body separate from authentication parameters.
    //  Include STS credentials in the signed query; never log the request URL.
    if (CkJsonObjectW_HasMember(credentials,L"session_token") == TRUE) {
        success = CkRestW_AddQueryParam(rest,L"X-Amz-Security-Token",CkJsonObjectW_stringOf(credentials,L"session_token"));
        if (success != TRUE) {
            wprintf(L"Could not set session token.\n");
            CkStringBuilderW_Dispose(stringCompare);
            CkJsonObjectW_Dispose(credentials);
            CkStringBuilderW_Dispose(endpoint);
            CkAuthAwsW_Dispose(aws);
            CkRestW_Dispose(rest);
            CkSocketW_Dispose(connection);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkBinDataW_Dispose(message);
            CkCrypt2W_Dispose(hash);
            return;
        }

    }

    success = CkRestW_AddHeader(rest,L"X-Amz-Target",L"TrentService.Sign");
    if (success != TRUE) {
        wprintf(L"Could not set operation.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    success = CkRestW_AddHeader(rest,L"Content-Type",L"application/x-amz-json-1.1");
    if (success != TRUE) {
        wprintf(L"Could not set content type.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    responseJson = CkRestW_fullRequestString(rest,L"POST",L"/",CkJsonObjectW_emit(body));
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"KMS HTTP status: %d\n",CkRestW_getResponseStatusCode(rest));
        success = CkJsonObjectW_Load(result,responseJson);
        if (success == TRUE) {
            if (CkJsonObjectW_JsonTypeOf(result,L"__type") == 1) {
                wprintf(L"AWS error code: %s\n",CkJsonObjectW_stringOf(result,L"__type"));
            }

        }

        wprintf(L"AWS request ID: %s\n",CkRestW_responseHdrByName(rest,L"x-amzn-RequestId"));
        //  Do not print full bodies, headers, credentials, key material, or tokens.
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    success = CkJsonObjectW_Load(result,responseJson);
    if (success != TRUE) {
        wprintf(L"Expected a well-formed JSON response.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyId"));
    stringMatch13 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"KeyId") != 1) || (stringMatch13 == TRUE)) {
        wprintf(L"Missing or invalid KeyId.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyId"));
    stringMatch14 = CkStringBuilderW_ContentsEqual(stringCompare,keyId,TRUE);
    if (stringMatch14 != TRUE) {
        wprintf(L"Response belongs to another key; use a fixed key ARN in configuration.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"SigningAlgorithm"));
    stringMatch15 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"SigningAlgorithm") != 1) || (stringMatch15 == TRUE)) {
        wprintf(L"Missing or invalid SigningAlgorithm.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"SigningAlgorithm"));
    stringMatch16 = CkStringBuilderW_ContentsEqual(stringCompare,L"ECDSA_SHA_256",TRUE);
    if (stringMatch16 != TRUE) {
        wprintf(L"Unexpected SigningAlgorithm.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"Signature"));
    stringMatch17 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"Signature") != 1) || (stringMatch17 == TRUE)) {
        wprintf(L"Missing or invalid Signature.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        return;
    }

    signature = CkBinDataW_Create();
    success = CkBinDataW_AppendEncoded(signature,CkJsonObjectW_stringOf(result,L"Signature"),L"base64");
    if (success != TRUE) {
        wprintf(L"Invalid base64 Signature.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    if (CkBinDataW_getNumBytes(signature) == 0) {
        wprintf(L"Unexpected byte length for Signature.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    CkJsonObjectW_Clear(body);
    success = CkJsonObjectW_UpdateString(body,L"KeyId",keyId);
    if (success != TRUE) {
        wprintf(L"Could not set KeyId.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    success = CkRestW_ClearAllQueryParams(rest);
    if (success != TRUE) {
        wprintf(L"Could not clear previous query parameters.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    //  Keep the JSON operation body separate from authentication parameters.
    //  Include STS credentials in the signed query; never log the request URL.
    if (CkJsonObjectW_HasMember(credentials,L"session_token") == TRUE) {
        success = CkRestW_AddQueryParam(rest,L"X-Amz-Security-Token",CkJsonObjectW_stringOf(credentials,L"session_token"));
        if (success != TRUE) {
            wprintf(L"Could not set session token.\n");
            CkStringBuilderW_Dispose(stringCompare);
            CkJsonObjectW_Dispose(credentials);
            CkStringBuilderW_Dispose(endpoint);
            CkAuthAwsW_Dispose(aws);
            CkRestW_Dispose(rest);
            CkSocketW_Dispose(connection);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkBinDataW_Dispose(message);
            CkCrypt2W_Dispose(hash);
            CkBinDataW_Dispose(signature);
            return;
        }

    }

    success = CkRestW_AddHeader(rest,L"X-Amz-Target",L"TrentService.GetPublicKey");
    if (success != TRUE) {
        wprintf(L"Could not set operation.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    success = CkRestW_AddHeader(rest,L"Content-Type",L"application/x-amz-json-1.1");
    if (success != TRUE) {
        wprintf(L"Could not set content type.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    responseJson = CkRestW_fullRequestString(rest,L"POST",L"/",CkJsonObjectW_emit(body));
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"KMS HTTP status: %d\n",CkRestW_getResponseStatusCode(rest));
        success = CkJsonObjectW_Load(result,responseJson);
        if (success == TRUE) {
            if (CkJsonObjectW_JsonTypeOf(result,L"__type") == 1) {
                wprintf(L"AWS error code: %s\n",CkJsonObjectW_stringOf(result,L"__type"));
            }

        }

        wprintf(L"AWS request ID: %s\n",CkRestW_responseHdrByName(rest,L"x-amzn-RequestId"));
        //  Do not print full bodies, headers, credentials, key material, or tokens.
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    success = CkJsonObjectW_Load(result,responseJson);
    if (success != TRUE) {
        wprintf(L"Expected a well-formed JSON response.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyId"));
    stringMatch18 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"KeyId") != 1) || (stringMatch18 == TRUE)) {
        wprintf(L"Missing or invalid KeyId.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyId"));
    stringMatch19 = CkStringBuilderW_ContentsEqual(stringCompare,keyId,TRUE);
    if (stringMatch19 != TRUE) {
        wprintf(L"Response belongs to another key; use a fixed key ARN in configuration.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeySpec"));
    stringMatch20 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"KeySpec") != 1) || (stringMatch20 == TRUE)) {
        wprintf(L"Missing or invalid KeySpec.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeySpec"));
    stringMatch21 = CkStringBuilderW_ContentsEqual(stringCompare,L"ECC_NIST_P256",TRUE);
    if (stringMatch21 != TRUE) {
        wprintf(L"Unexpected KeySpec.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyUsage"));
    stringMatch22 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"KeyUsage") != 1) || (stringMatch22 == TRUE)) {
        wprintf(L"Missing or invalid KeyUsage.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"KeyUsage"));
    stringMatch23 = CkStringBuilderW_ContentsEqual(stringCompare,L"SIGN_VERIFY",TRUE);
    if (stringMatch23 != TRUE) {
        wprintf(L"Unexpected KeyUsage.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(result,L"PublicKey"));
    stringMatch24 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
    if ((CkJsonObjectW_JsonTypeOf(result,L"PublicKey") != 1) || (stringMatch24 == TRUE)) {
        wprintf(L"Missing or invalid PublicKey.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        return;
    }

    publicKey = CkPublicKeyW_Create();
    success = CkPublicKeyW_LoadBase64(publicKey,CkJsonObjectW_stringOf(result,L"PublicKey"));
    if (success != TRUE) {
        wprintf(L"Could not load SPKI public key.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        CkPublicKeyW_Dispose(publicKey);
        return;
    }

    ecc = CkEccW_Create();
    verification = CkEccW_VerifyHashENC(ecc,digestBase64,CkBinDataW_getEncoded(signature,L"base64"),L"base64",publicKey);
    if (verification != 1) {
        wprintf(L"ECDSA signature verification failed or reported an error.\n");
        CkStringBuilderW_Dispose(stringCompare);
        CkJsonObjectW_Dispose(credentials);
        CkStringBuilderW_Dispose(endpoint);
        CkAuthAwsW_Dispose(aws);
        CkRestW_Dispose(rest);
        CkSocketW_Dispose(connection);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkBinDataW_Dispose(message);
        CkCrypt2W_Dispose(hash);
        CkBinDataW_Dispose(signature);
        CkPublicKeyW_Dispose(publicKey);
        CkEccW_Dispose(ecc);
        return;
    }

    wprintf(L"Signature verified.\n");


    CkStringBuilderW_Dispose(stringCompare);
    CkJsonObjectW_Dispose(credentials);
    CkStringBuilderW_Dispose(endpoint);
    CkAuthAwsW_Dispose(aws);
    CkRestW_Dispose(rest);
    CkSocketW_Dispose(connection);
    CkJsonObjectW_Dispose(body);
    CkJsonObjectW_Dispose(result);
    CkBinDataW_Dispose(message);
    CkCrypt2W_Dispose(hash);
    CkBinDataW_Dispose(signature);
    CkPublicKeyW_Dispose(publicKey);
    CkEccW_Dispose(ecc);

    }