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 <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkCertW.h>
#include <CkPrivateKeyW.h>
#include <CkPublicKeyW.h>
void ChilkatSample(void)
{
bool success = false;
CkStringBuilderW stringCompare;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this credentials file outside public web content and source control.
CkJsonObjectW credentials;
credentials.put_Strict(true);
success = credentials.LoadFile(L"qa_data/aws/kms.json");
if (success != true) {
wprintf(L"Could not load private KMS configuration.\n");
return;
}
stringCompare.SetString(credentials.stringOf(L"access_key"));
bool stringMatch1 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"access_key") != 1) || (stringMatch1 == true)) {
wprintf(L"Missing or invalid access_key.\n");
return;
}
stringCompare.SetString(credentials.stringOf(L"secret_key"));
bool stringMatch2 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"secret_key") != 1) || (stringMatch2 == true)) {
wprintf(L"Missing or invalid secret_key.\n");
return;
}
const wchar_t *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 (credentials.HasMember(L"session_token") == true) {
wprintf(L"This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.\n");
return;
}
// pfx_password is a string; an empty string is allowed for an unprotected PFX.
if (credentials.JsonTypeOf(L"pfx_password") != 1) {
wprintf(L"Missing or invalid pfx_password.\n");
return;
}
CkCertW cert;
success = cert.LoadPfxFile(L"qa_data/pfx/myCertAndKey.pfx",credentials.stringOf(L"pfx_password"));
if (success != true) {
wprintf(L"Could not load PFX; inspect diagnostics privately.\n");
return;
}
CkPrivateKeyW privKey;
success = cert.GetPrivateKey(privKey);
if (success != true) {
wprintf(L"PFX does not contain a usable private key.\n");
return;
}
// Only inspect the PUBLIC JWK to identify a supported key type and curve.
CkPublicKeyW sourcePublic;
success = privKey.ToPublicKey(sourcePublic);
if (success != true) {
wprintf(L"Could not get the public key.\n");
return;
}
const wchar_t *publicJwk = sourcePublic.getJwk();
if (sourcePublic.get_LastMethodSuccess() != true) {
wprintf(L"Could not inspect public key type.\n");
return;
}
CkJsonObjectW keyInfo;
keyInfo.put_Strict(true);
success = keyInfo.Load(publicJwk);
if (success != true) {
wprintf(L"Could not parse public key description.\n");
return;
}
const wchar_t *keySpec = L"";
stringCompare.SetString(keyInfo.stringOf(L"kty"));
bool stringMatch3 = stringCompare.ContentsEqual(L"RSA",true);
if (stringMatch3 == true) {
if (privKey.get_BitLength() == 2048) {
keySpec = L"RSA_2048";
}
if (privKey.get_BitLength() == 3072) {
keySpec = L"RSA_3072";
}
if (privKey.get_BitLength() == 4096) {
keySpec = L"RSA_4096";
}
}
stringCompare.SetString(keyInfo.stringOf(L"kty"));
bool stringMatch4 = stringCompare.ContentsEqual(L"EC",true);
if (stringMatch4 == true) {
stringCompare.SetString(keyInfo.stringOf(L"crv"));
bool stringMatch5 = stringCompare.ContentsEqual(L"P-256",true);
if (stringMatch5 == true) {
keySpec = L"ECC_NIST_P256";
}
stringCompare.SetString(keyInfo.stringOf(L"crv"));
bool stringMatch6 = stringCompare.ContentsEqual(L"P-384",true);
if (stringMatch6 == true) {
keySpec = L"ECC_NIST_P384";
}
stringCompare.SetString(keyInfo.stringOf(L"crv"));
bool stringMatch7 = stringCompare.ContentsEqual(L"P-521",true);
if (stringMatch7 == true) {
keySpec = L"ECC_NIST_P521";
}
}
stringCompare.SetString(keySpec);
bool stringMatch8 = stringCompare.ContentsEqual(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");
return;
}
CkJsonObjectW options;
success = options.UpdateString(L"service",L"aws-kms");
if (success != true) {
wprintf(L"Could not set service.\n");
return;
}
success = options.UpdateString(L"auth.access_key",credentials.stringOf(L"access_key"));
if (success != true) {
wprintf(L"Could not set auth.access_key.\n");
return;
}
success = options.UpdateString(L"auth.secret_key",credentials.stringOf(L"secret_key"));
if (success != true) {
wprintf(L"Could not set auth.secret_key.\n");
return;
}
success = options.UpdateString(L"auth.region",region);
if (success != true) {
wprintf(L"Could not set auth.region.\n");
return;
}
success = options.UpdateString(L"description",L"Imported certificate signing key");
if (success != true) {
wprintf(L"Could not set description.\n");
return;
}
success = options.UpdateString(L"tags[0].key",L"Purpose");
if (success != true) {
wprintf(L"Could not set tags[0].key.\n");
return;
}
success = options.UpdateString(L"tags[0].value",L"DocumentSigning");
if (success != true) {
wprintf(L"Could not set tags[0].value.\n");
return;
}
success = options.UpdateString(L"keyUsage",L"SIGN_VERIFY");
if (success != true) {
wprintf(L"Could not set keyUsage.\n");
return;
}
CkJsonObjectW imported;
// Creates a NEW KMS key. Do not blindly rerun after an uncertain failure.
success = privKey.UploadToCloud(options,imported);
if (success != true) {
wprintf(L"Import failed or its outcome is uncertain. Check KMS/CloudTrail before retrying; inspect diagnostics privately.\n");
return;
}
stringCompare.SetString(imported.stringOf(L"KeyId"));
bool stringMatch9 = stringCompare.ContentsEqual(L"",true);
if ((imported.JsonTypeOf(L"KeyId") != 1) || (stringMatch9 == true)) {
wprintf(L"Missing or invalid KeyId.\n");
return;
}
wprintf(L"Imported KMS key ID: %s\n",imported.stringOf(L"KeyId"));
// Save this identifier securely for subsequent signing and lifecycle management.
}