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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoCredentials
Boolean iStringMatch1
Boolean iStringMatch2
String sRegion
Handle hoCert
Variant vPrivKey
Handle hoPrivKey
Variant vSourcePublic
Handle hoSourcePublic
String sPublicJwk
Handle hoKeyInfo
String sKeySpec
Boolean iStringMatch3
Boolean iStringMatch4
Boolean iStringMatch5
Boolean iStringMatch6
Boolean iStringMatch7
Boolean iStringMatch8
Variant vOptions
Handle hoOptions
Variant vImported
Handle hoImported
Boolean iStringMatch9
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
If (Not(IsComObjectCreated(hoStringCompare))) Begin
Send CreateComObject of hoStringCompare
End
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this credentials file outside public web content and source control.
Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
If (Not(IsComObjectCreated(hoCredentials))) Begin
Send CreateComObject of hoCredentials
End
Set ComStrict Of hoCredentials To True
Get ComLoadFile Of hoCredentials "qa_data/aws/kms.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private KMS configuration."
Procedure_Return
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
Move "us-west-2" To sRegion
// These convenience-method examples use an access-key/secret-key pair.
// Do not silently discard the token belonging to temporary credentials.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Showln "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration."
Procedure_Return
End
// pfx_password is a string; an empty string is allowed for an unprotected PFX.
Get ComJsonTypeOf Of hoCredentials "pfx_password" To iTemp1
If (iTemp1 <> 1) Begin
Showln "Missing or invalid pfx_password."
Procedure_Return
End
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get ComStringOf Of hoCredentials "pfx_password" To sTemp1
Get ComLoadPfxFile Of hoCert "qa_data/pfx/myCertAndKey.pfx" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load PFX; inspect diagnostics privately."
Procedure_Return
End
Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
If (Not(IsComObjectCreated(hoPrivKey))) Begin
Send CreateComObject of hoPrivKey
End
Get pvComObject of hoPrivKey to vPrivKey
Get ComGetPrivateKey Of hoCert vPrivKey To iSuccess
If (iSuccess <> True) Begin
Showln "PFX does not contain a usable private key."
Procedure_Return
End
// Only inspect the PUBLIC JWK to identify a supported key type and curve.
Get Create (RefClass(cComChilkatPublicKey)) To hoSourcePublic
If (Not(IsComObjectCreated(hoSourcePublic))) Begin
Send CreateComObject of hoSourcePublic
End
Get pvComObject of hoSourcePublic to vSourcePublic
Get ComToPublicKey Of hoPrivKey vSourcePublic To iSuccess
If (iSuccess <> True) Begin
Showln "Could not get the public key."
Procedure_Return
End
Get ComGetJwk Of hoSourcePublic To sPublicJwk
Get ComLastMethodSuccess Of hoSourcePublic To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not inspect public key type."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoKeyInfo
If (Not(IsComObjectCreated(hoKeyInfo))) Begin
Send CreateComObject of hoKeyInfo
End
Set ComStrict Of hoKeyInfo To True
Get ComLoad Of hoKeyInfo sPublicJwk To iSuccess
If (iSuccess <> True) Begin
Showln "Could not parse public key description."
Procedure_Return
End
Move "" To sKeySpec
Get ComStringOf Of hoKeyInfo "kty" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "RSA" True To iStringMatch3
If (iStringMatch3 = True) Begin
Get ComBitLength Of hoPrivKey To iTemp1
If (iTemp1 = 2048) Begin
Move "RSA_2048" To sKeySpec
End
Get ComBitLength Of hoPrivKey To iTemp1
If (iTemp1 = 3072) Begin
Move "RSA_3072" To sKeySpec
End
Get ComBitLength Of hoPrivKey To iTemp1
If (iTemp1 = 4096) Begin
Move "RSA_4096" To sKeySpec
End
End
Get ComStringOf Of hoKeyInfo "kty" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "EC" True To iStringMatch4
If (iStringMatch4 = True) Begin
Get ComStringOf Of hoKeyInfo "crv" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "P-256" True To iStringMatch5
If (iStringMatch5 = True) Begin
Move "ECC_NIST_P256" To sKeySpec
End
Get ComStringOf Of hoKeyInfo "crv" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "P-384" True To iStringMatch6
If (iStringMatch6 = True) Begin
Move "ECC_NIST_P384" To sKeySpec
End
Get ComStringOf Of hoKeyInfo "crv" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "P-521" True To iStringMatch7
If (iStringMatch7 = True) Begin
Move "ECC_NIST_P521" To sKeySpec
End
End
Get ComSetString Of hoStringCompare sKeySpec To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch8
If (iStringMatch8 = True) Begin
Showln "Use RSA 2048/3072/4096 or a NIST P-256/P-384/P-521 EC private key."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoOptions
If (Not(IsComObjectCreated(hoOptions))) Begin
Send CreateComObject of hoOptions
End
Get ComUpdateString Of hoOptions "service" "aws-kms" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set service."
Procedure_Return
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComUpdateString Of hoOptions "auth.access_key" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set auth.access_key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComUpdateString Of hoOptions "auth.secret_key" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set auth.secret_key."
Procedure_Return
End
Get ComUpdateString Of hoOptions "auth.region" sRegion To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set auth.region."
Procedure_Return
End
Get ComUpdateString Of hoOptions "description" "Imported certificate signing key" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set description."
Procedure_Return
End
Get ComUpdateString Of hoOptions "tags[0].key" "Purpose" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set tags[0].key."
Procedure_Return
End
Get ComUpdateString Of hoOptions "tags[0].value" "DocumentSigning" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set tags[0].value."
Procedure_Return
End
Get ComUpdateString Of hoOptions "keyUsage" "SIGN_VERIFY" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set keyUsage."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoImported
If (Not(IsComObjectCreated(hoImported))) Begin
Send CreateComObject of hoImported
End
// Creates a NEW KMS key. Do not blindly rerun after an uncertain failure.
Get pvComObject of hoOptions to vOptions
Get pvComObject of hoImported to vImported
Get ComUploadToCloud Of hoPrivKey vOptions vImported To iSuccess
If (iSuccess <> True) Begin
Showln "Import failed or its outcome is uncertain. Check KMS/CloudTrail before retrying; inspect diagnostics privately."
Procedure_Return
End
Get ComStringOf Of hoImported "KeyId" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch9
Get ComJsonTypeOf Of hoImported "KeyId" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch9 = True)) Begin
Showln "Missing or invalid KeyId."
Procedure_Return
End
Get ComStringOf Of hoImported "KeyId" To sTemp1
Showln "Imported KMS key ID: " sTemp1
// Save this identifier securely for subsequent signing and lifecycle management.
End_Procedure