AWS KMS: Import a PFX Private Key into a New Signing Key
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.
Loads the certificate's private key from qa_data/pfx/myCertAndKey.pfx using string field pfx_password from the private configuration. Supports RSA 2048/3072/4096 and NIST P-256/P-384/P-521 in this sample. Keep a protected backup of the original material: KMS cannot export it. RSA material must satisfy AWS's current key-material requirements. Tags are visible metadata; do not put passwords or sensitive certificate identity data in them.
The UploadToCloud convenience method creates a new external-origin KMS key and imports the private key, with KeyUsage=SIGN_VERIFY. Rerunning can create additional keys and charges. On failure, inspect KMS/CloudTrail before deciding whether to retry or clean up a pending key. Required permissions include kms:CreateKey, kms:TagResource, kms:GetParametersForImport, and kms:ImportKeyMaterial, subject to IAM and key policy. The certificate itself stays local. Record the returned KeyId; inspect DescribeKey for state and expiration policy. Use the explicit low-level example when you need control of an existing destination key and import settings.
AWS ImportKeyMaterial reference
Chilkat Unicode C Downloads
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkCertW.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkPublicKeyW.h>
void ChilkatSample(void)
{
BOOL success;
HCkStringBuilderW stringCompare;
HCkJsonObjectW credentials;
BOOL stringMatch1;
BOOL stringMatch2;
const wchar_t *region;
HCkCertW cert;
HCkPrivateKeyW privKey;
HCkPublicKeyW sourcePublic;
const wchar_t *publicJwk;
HCkJsonObjectW keyInfo;
const wchar_t *keySpec;
BOOL stringMatch3;
BOOL stringMatch4;
BOOL stringMatch5;
BOOL stringMatch6;
BOOL stringMatch7;
BOOL stringMatch8;
HCkJsonObjectW options;
HCkJsonObjectW imported;
BOOL stringMatch9;
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;
}
// pfx_password is a string; an empty string is allowed for an unprotected PFX.
if (CkJsonObjectW_JsonTypeOf(credentials,L"pfx_password") != 1) {
wprintf(L"Missing or invalid pfx_password.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
return;
}
cert = CkCertW_Create();
success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/myCertAndKey.pfx",CkJsonObjectW_stringOf(credentials,L"pfx_password"));
if (success != TRUE) {
wprintf(L"Could not load PFX; inspect diagnostics privately.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
return;
}
privKey = CkPrivateKeyW_Create();
success = CkCertW_GetPrivateKey(cert,privKey);
if (success != TRUE) {
wprintf(L"PFX does not contain a usable private key.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
return;
}
// Only inspect the PUBLIC JWK to identify a supported key type and curve.
sourcePublic = CkPublicKeyW_Create();
success = CkPrivateKeyW_ToPublicKey(privKey,sourcePublic);
if (success != TRUE) {
wprintf(L"Could not get the public key.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
return;
}
publicJwk = CkPublicKeyW_getJwk(sourcePublic);
if (CkPublicKeyW_getLastMethodSuccess(sourcePublic) != TRUE) {
wprintf(L"Could not inspect public key type.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
return;
}
keyInfo = CkJsonObjectW_Create();
CkJsonObjectW_putStrict(keyInfo,TRUE);
success = CkJsonObjectW_Load(keyInfo,publicJwk);
if (success != TRUE) {
wprintf(L"Could not parse public key description.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
return;
}
keySpec = L"";
CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(keyInfo,L"kty"));
stringMatch3 = CkStringBuilderW_ContentsEqual(stringCompare,L"RSA",TRUE);
if (stringMatch3 == TRUE) {
if (CkPrivateKeyW_getBitLength(privKey) == 2048) {
keySpec = L"RSA_2048";
}
if (CkPrivateKeyW_getBitLength(privKey) == 3072) {
keySpec = L"RSA_3072";
}
if (CkPrivateKeyW_getBitLength(privKey) == 4096) {
keySpec = L"RSA_4096";
}
}
CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(keyInfo,L"kty"));
stringMatch4 = CkStringBuilderW_ContentsEqual(stringCompare,L"EC",TRUE);
if (stringMatch4 == TRUE) {
CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(keyInfo,L"crv"));
stringMatch5 = CkStringBuilderW_ContentsEqual(stringCompare,L"P-256",TRUE);
if (stringMatch5 == TRUE) {
keySpec = L"ECC_NIST_P256";
}
CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(keyInfo,L"crv"));
stringMatch6 = CkStringBuilderW_ContentsEqual(stringCompare,L"P-384",TRUE);
if (stringMatch6 == TRUE) {
keySpec = L"ECC_NIST_P384";
}
CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(keyInfo,L"crv"));
stringMatch7 = CkStringBuilderW_ContentsEqual(stringCompare,L"P-521",TRUE);
if (stringMatch7 == TRUE) {
keySpec = L"ECC_NIST_P521";
}
}
CkStringBuilderW_SetString(stringCompare,keySpec);
stringMatch8 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
if (stringMatch8 == TRUE) {
wprintf(L"Use RSA 2048/3072/4096 or a NIST P-256/P-384/P-521 EC private key.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
return;
}
options = CkJsonObjectW_Create();
success = CkJsonObjectW_UpdateString(options,L"service",L"aws-kms");
if (success != TRUE) {
wprintf(L"Could not set service.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
return;
}
success = CkJsonObjectW_UpdateString(options,L"auth.access_key",CkJsonObjectW_stringOf(credentials,L"access_key"));
if (success != TRUE) {
wprintf(L"Could not set auth.access_key.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
return;
}
success = CkJsonObjectW_UpdateString(options,L"auth.secret_key",CkJsonObjectW_stringOf(credentials,L"secret_key"));
if (success != TRUE) {
wprintf(L"Could not set auth.secret_key.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
return;
}
success = CkJsonObjectW_UpdateString(options,L"auth.region",region);
if (success != TRUE) {
wprintf(L"Could not set auth.region.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
return;
}
success = CkJsonObjectW_UpdateString(options,L"description",L"Imported certificate signing key");
if (success != TRUE) {
wprintf(L"Could not set description.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
return;
}
success = CkJsonObjectW_UpdateString(options,L"tags[0].key",L"Purpose");
if (success != TRUE) {
wprintf(L"Could not set tags[0].key.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
return;
}
success = CkJsonObjectW_UpdateString(options,L"tags[0].value",L"DocumentSigning");
if (success != TRUE) {
wprintf(L"Could not set tags[0].value.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
return;
}
success = CkJsonObjectW_UpdateString(options,L"keyUsage",L"SIGN_VERIFY");
if (success != TRUE) {
wprintf(L"Could not set keyUsage.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
return;
}
imported = CkJsonObjectW_Create();
// Creates a NEW KMS key. Do not blindly rerun after an uncertain failure.
success = CkPrivateKeyW_UploadToCloud(privKey,options,imported);
if (success != TRUE) {
wprintf(L"Import failed or its outcome is uncertain. Check KMS/CloudTrail before retrying; inspect diagnostics privately.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
CkJsonObjectW_Dispose(imported);
return;
}
CkStringBuilderW_SetString(stringCompare,CkJsonObjectW_stringOf(imported,L"KeyId"));
stringMatch9 = CkStringBuilderW_ContentsEqual(stringCompare,L"",TRUE);
if ((CkJsonObjectW_JsonTypeOf(imported,L"KeyId") != 1) || (stringMatch9 == TRUE)) {
wprintf(L"Missing or invalid KeyId.\n");
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
CkJsonObjectW_Dispose(imported);
return;
}
wprintf(L"Imported KMS key ID: %s\n",CkJsonObjectW_stringOf(imported,L"KeyId"));
// Save this identifier securely for subsequent signing and lifecycle management.
CkStringBuilderW_Dispose(stringCompare);
CkJsonObjectW_Dispose(credentials);
CkCertW_Dispose(cert);
CkPrivateKeyW_Dispose(privKey);
CkPublicKeyW_Dispose(sourcePublic);
CkJsonObjectW_Dispose(keyInfo);
CkJsonObjectW_Dispose(options);
CkJsonObjectW_Dispose(imported);
}