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 Python Downloads
import sys
import chilkat2
success = False
stringCompare = chilkat2.StringBuilder()
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Keep this credentials file outside public web content and source control.
credentials = chilkat2.JsonObject()
credentials.Strict = True
success = credentials.LoadFile("qa_data/aws/kms.json")
if (success != True):
print("Could not load private KMS configuration.")
sys.exit()
stringCompare.SetString(credentials.StringOf("access_key"))
stringMatch1 = stringCompare.ContentsEqual("",True)
if ((credentials.JsonTypeOf("access_key") != 1) or (stringMatch1 == True)):
print("Missing or invalid access_key.")
sys.exit()
stringCompare.SetString(credentials.StringOf("secret_key"))
stringMatch2 = stringCompare.ContentsEqual("",True)
if ((credentials.JsonTypeOf("secret_key") != 1) or (stringMatch2 == True)):
print("Missing or invalid secret_key.")
sys.exit()
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 (credentials.HasMember("session_token") == True):
print("This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.")
sys.exit()
# pfx_password is a string; an empty string is allowed for an unprotected PFX.
if (credentials.JsonTypeOf("pfx_password") != 1):
print("Missing or invalid pfx_password.")
sys.exit()
cert = chilkat2.Cert()
success = cert.LoadPfxFile("qa_data/pfx/myCertAndKey.pfx",credentials.StringOf("pfx_password"))
if (success != True):
print("Could not load PFX; inspect diagnostics privately.")
sys.exit()
privKey = chilkat2.PrivateKey()
success = cert.GetPrivateKey(privKey)
if (success != True):
print("PFX does not contain a usable private key.")
sys.exit()
# Only inspect the PUBLIC JWK to identify a supported key type and curve.
sourcePublic = chilkat2.PublicKey()
success = privKey.ToPublicKey(sourcePublic)
if (success != True):
print("Could not get the public key.")
sys.exit()
publicJwk = sourcePublic.GetJwk()
if (sourcePublic.LastMethodSuccess != True):
print("Could not inspect public key type.")
sys.exit()
keyInfo = chilkat2.JsonObject()
keyInfo.Strict = True
success = keyInfo.Load(publicJwk)
if (success != True):
print("Could not parse public key description.")
sys.exit()
keySpec = ""
stringCompare.SetString(keyInfo.StringOf("kty"))
stringMatch3 = stringCompare.ContentsEqual("RSA",True)
if (stringMatch3 == True):
if (privKey.BitLength == 2048):
keySpec = "RSA_2048"
if (privKey.BitLength == 3072):
keySpec = "RSA_3072"
if (privKey.BitLength == 4096):
keySpec = "RSA_4096"
stringCompare.SetString(keyInfo.StringOf("kty"))
stringMatch4 = stringCompare.ContentsEqual("EC",True)
if (stringMatch4 == True):
stringCompare.SetString(keyInfo.StringOf("crv"))
stringMatch5 = stringCompare.ContentsEqual("P-256",True)
if (stringMatch5 == True):
keySpec = "ECC_NIST_P256"
stringCompare.SetString(keyInfo.StringOf("crv"))
stringMatch6 = stringCompare.ContentsEqual("P-384",True)
if (stringMatch6 == True):
keySpec = "ECC_NIST_P384"
stringCompare.SetString(keyInfo.StringOf("crv"))
stringMatch7 = stringCompare.ContentsEqual("P-521",True)
if (stringMatch7 == True):
keySpec = "ECC_NIST_P521"
stringCompare.SetString(keySpec)
stringMatch8 = stringCompare.ContentsEqual("",True)
if (stringMatch8 == True):
print("Use RSA 2048/3072/4096 or a NIST P-256/P-384/P-521 EC private key.")
sys.exit()
options = chilkat2.JsonObject()
success = options.UpdateString("service","aws-kms")
if (success != True):
print("Could not set service.")
sys.exit()
success = options.UpdateString("auth.access_key",credentials.StringOf("access_key"))
if (success != True):
print("Could not set auth.access_key.")
sys.exit()
success = options.UpdateString("auth.secret_key",credentials.StringOf("secret_key"))
if (success != True):
print("Could not set auth.secret_key.")
sys.exit()
success = options.UpdateString("auth.region",region)
if (success != True):
print("Could not set auth.region.")
sys.exit()
success = options.UpdateString("description","Imported certificate signing key")
if (success != True):
print("Could not set description.")
sys.exit()
success = options.UpdateString("tags[0].key","Purpose")
if (success != True):
print("Could not set tags[0].key.")
sys.exit()
success = options.UpdateString("tags[0].value","DocumentSigning")
if (success != True):
print("Could not set tags[0].value.")
sys.exit()
success = options.UpdateString("keyUsage","SIGN_VERIFY")
if (success != True):
print("Could not set keyUsage.")
sys.exit()
imported = chilkat2.JsonObject()
# Creates a NEW KMS key. Do not blindly rerun after an uncertain failure.
success = privKey.UploadToCloud(options,imported)
if (success != True):
print("Import failed or its outcome is uncertain. Check KMS/CloudTrail before retrying; inspect diagnostics privately.")
sys.exit()
stringCompare.SetString(imported.StringOf("KeyId"))
stringMatch9 = stringCompare.ContentsEqual("",True)
if ((imported.JsonTypeOf("KeyId") != 1) or (stringMatch9 == True)):
print("Missing or invalid KeyId.")
sys.exit()
print("Imported KMS key ID: " + imported.StringOf("KeyId"))
# Save this identifier securely for subsequent signing and lifecycle management.