AWS KMS: Create a Context-Constrained Decrypt Grant
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.
Set grantee_principal to an existing IAM role/user ARN. Grants only Decrypt with an exact nonsecret encryption context, and designates that principal as retiring principal. The fixed Name helps avoid duplicate grants for identical requests; use a stable application-unique name and reconcile uncertain outcomes. Save the returned GrantId and GrantToken privately. A token accelerates propagation; it does not authenticate the caller or grant access to unrelated identities.
API actions used: kms:CreateGrant. IAM permissions and key policies must allow the actual resources; grant and multi-Region workflows can need permissions in more than one region/account.
Chilkat Unicode C Downloads
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkRestW.h>
#include <C_CkSocketW.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;
const wchar_t *grantee;
BOOL stringMatch6;
BOOL stringMatch7;
HCkJsonObjectW grant;
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");
CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(credentials,L"grantee_principal"));
stringMatch5 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
if ((CkJsonObjectW_JsonTypeOf(credentials,L"grantee_principal") != 1) || (stringMatch5 == TRUE)) {
wprintf(L"Missing or invalid grantee_principal.\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;
}
grantee = CkJsonObjectW_stringOf(credentials,L"grantee_principal");
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 = CkJsonObjectW_UpdateString(body,L"GranteePrincipal",grantee);
if (success != TRUE) {
wprintf(L"Could not set GranteePrincipal.\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 = CkJsonObjectW_UpdateString(body,L"RetiringPrincipal",grantee);
if (success != TRUE) {
wprintf(L"Could not set RetiringPrincipal.\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 = CkJsonObjectW_UpdateString(body,L"Operations[0]",L"Decrypt");
if (success != TRUE) {
wprintf(L"Could not set Operations[0].\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 = CkJsonObjectW_UpdateString(body,L"Constraints.EncryptionContextEquals.Application",L"ChilkatKmsExamples");
if (success != TRUE) {
wprintf(L"Could not set Constraints.EncryptionContextEquals.Application.\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 = CkJsonObjectW_UpdateString(body,L"Name",L"chilkat-decrypt-context-v1");
if (success != TRUE) {
wprintf(L"Could not set Name.\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.CreateGrant");
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"GrantId"));
stringMatch6 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
if ((CkJsonObjectW_JsonTypeOf(result,L"GrantId") != 1) || (stringMatch6 == TRUE)) {
wprintf(L"Missing or invalid GrantId.\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"GrantToken"));
stringMatch7 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
if ((CkJsonObjectW_JsonTypeOf(result,L"GrantToken") != 1) || (stringMatch7 == TRUE)) {
wprintf(L"Missing or invalid GrantToken.\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;
}
grant = CkJsonObjectW_Create();
success = CkJsonObjectW_UpdateString(grant,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);
CkJsonObjectW_Dispose(grant);
return;
}
success = CkJsonObjectW_UpdateString(grant,L"GrantId",CkJsonObjectW_stringOf(result,L"GrantId"));
if (success != TRUE) {
wprintf(L"Could not set GrantId.\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);
CkJsonObjectW_Dispose(grant);
return;
}
success = CkJsonObjectW_UpdateString(grant,L"GrantToken",CkJsonObjectW_stringOf(result,L"GrantToken"));
if (success != TRUE) {
wprintf(L"Could not set GrantToken.\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);
CkJsonObjectW_Dispose(grant);
return;
}
success = CkJsonObjectW_WriteFile(grant,L"qa_output/kms_grant.json");
if (success != TRUE) {
wprintf(L"Could not save qa_output/kms_grant.json.\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);
CkJsonObjectW_Dispose(grant);
return;
}
wprintf(L"Grant created; token saved privately for the grantee.\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);
CkJsonObjectW_Dispose(grant);
}