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 Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
credentials: HCkJsonObject;
stringMatch1: Boolean;
stringMatch2: Boolean;
region: PWideChar;
cert: HCkCert;
privKey: HCkPrivateKey;
sourcePublic: HCkPublicKey;
publicJwk: PWideChar;
keyInfo: HCkJsonObject;
keySpec: PWideChar;
stringMatch3: Boolean;
stringMatch4: Boolean;
stringMatch5: Boolean;
stringMatch6: Boolean;
stringMatch7: Boolean;
stringMatch8: Boolean;
options: HCkJsonObject;
imported: HCkJsonObject;
stringMatch9: Boolean;
begin
success := False;
stringCompare := CkStringBuilder_Create();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this credentials file outside public web content and source control.
credentials := CkJsonObject_Create();
CkJsonObject_putStrict(credentials,True);
success := CkJsonObject_LoadFile(credentials,'qa_data/aws/kms.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private KMS configuration.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'access_key'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'access_key') <> 1) or (stringMatch1 = True)) then
begin
Memo1.Lines.Add('Missing or invalid access_key.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'secret_key'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'secret_key') <> 1) or (stringMatch2 = True)) then
begin
Memo1.Lines.Add('Missing or invalid secret_key.');
Exit;
end;
region := '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 (CkJsonObject_HasMember(credentials,'session_token') = True) then
begin
Memo1.Lines.Add('This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.');
Exit;
end;
// pfx_password is a string; an empty string is allowed for an unprotected PFX.
if (CkJsonObject_JsonTypeOf(credentials,'pfx_password') <> 1) then
begin
Memo1.Lines.Add('Missing or invalid pfx_password.');
Exit;
end;
cert := CkCert_Create();
success := CkCert_LoadPfxFile(cert,'qa_data/pfx/myCertAndKey.pfx',CkJsonObject__stringOf(credentials,'pfx_password'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not load PFX; inspect diagnostics privately.');
Exit;
end;
privKey := CkPrivateKey_Create();
success := CkCert_GetPrivateKey(cert,privKey);
if (success <> True) then
begin
Memo1.Lines.Add('PFX does not contain a usable private key.');
Exit;
end;
// Only inspect the PUBLIC JWK to identify a supported key type and curve.
sourcePublic := CkPublicKey_Create();
success := CkPrivateKey_ToPublicKey(privKey,sourcePublic);
if (success <> True) then
begin
Memo1.Lines.Add('Could not get the public key.');
Exit;
end;
publicJwk := CkPublicKey__getJwk(sourcePublic);
if (CkPublicKey_getLastMethodSuccess(sourcePublic) <> True) then
begin
Memo1.Lines.Add('Could not inspect public key type.');
Exit;
end;
keyInfo := CkJsonObject_Create();
CkJsonObject_putStrict(keyInfo,True);
success := CkJsonObject_Load(keyInfo,publicJwk);
if (success <> True) then
begin
Memo1.Lines.Add('Could not parse public key description.');
Exit;
end;
keySpec := '';
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'kty'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'RSA',True);
if (stringMatch3 = True) then
begin
if (CkPrivateKey_getBitLength(privKey) = 2048) then
begin
keySpec := 'RSA_2048';
end;
if (CkPrivateKey_getBitLength(privKey) = 3072) then
begin
keySpec := 'RSA_3072';
end;
if (CkPrivateKey_getBitLength(privKey) = 4096) then
begin
keySpec := 'RSA_4096';
end;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'kty'));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'EC',True);
if (stringMatch4 = True) then
begin
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'crv'));
stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'P-256',True);
if (stringMatch5 = True) then
begin
keySpec := 'ECC_NIST_P256';
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'crv'));
stringMatch6 := CkStringBuilder_ContentsEqual(stringCompare,'P-384',True);
if (stringMatch6 = True) then
begin
keySpec := 'ECC_NIST_P384';
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyInfo,'crv'));
stringMatch7 := CkStringBuilder_ContentsEqual(stringCompare,'P-521',True);
if (stringMatch7 = True) then
begin
keySpec := 'ECC_NIST_P521';
end;
end;
CkStringBuilder_SetString(stringCompare,keySpec);
stringMatch8 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if (stringMatch8 = True) then
begin
Memo1.Lines.Add('Use RSA 2048/3072/4096 or a NIST P-256/P-384/P-521 EC private key.');
Exit;
end;
options := CkJsonObject_Create();
success := CkJsonObject_UpdateString(options,'service','aws-kms');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set service.');
Exit;
end;
success := CkJsonObject_UpdateString(options,'auth.access_key',CkJsonObject__stringOf(credentials,'access_key'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set auth.access_key.');
Exit;
end;
success := CkJsonObject_UpdateString(options,'auth.secret_key',CkJsonObject__stringOf(credentials,'secret_key'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set auth.secret_key.');
Exit;
end;
success := CkJsonObject_UpdateString(options,'auth.region',region);
if (success <> True) then
begin
Memo1.Lines.Add('Could not set auth.region.');
Exit;
end;
success := CkJsonObject_UpdateString(options,'description','Imported certificate signing key');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set description.');
Exit;
end;
success := CkJsonObject_UpdateString(options,'tags[0].key','Purpose');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set tags[0].key.');
Exit;
end;
success := CkJsonObject_UpdateString(options,'tags[0].value','DocumentSigning');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set tags[0].value.');
Exit;
end;
success := CkJsonObject_UpdateString(options,'keyUsage','SIGN_VERIFY');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set keyUsage.');
Exit;
end;
imported := CkJsonObject_Create();
// Creates a NEW KMS key. Do not blindly rerun after an uncertain failure.
success := CkPrivateKey_UploadToCloud(privKey,options,imported);
if (success <> True) then
begin
Memo1.Lines.Add('Import failed or its outcome is uncertain. Check KMS/CloudTrail before retrying; inspect diagnostics privately.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(imported,'KeyId'));
stringMatch9 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(imported,'KeyId') <> 1) or (stringMatch9 = True)) then
begin
Memo1.Lines.Add('Missing or invalid KeyId.');
Exit;
end;
Memo1.Lines.Add('Imported KMS key ID: ' + CkJsonObject__stringOf(imported,'KeyId'));
// Save this identifier securely for subsequent signing and lifecycle management.
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkCert_Dispose(cert);
CkPrivateKey_Dispose(privKey);
CkPublicKey_Dispose(sourcePublic);
CkJsonObject_Dispose(keyInfo);
CkJsonObject_Dispose(options);
CkJsonObject_Dispose(imported);