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