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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
string ls_Region
oleobject loo_Cert
oleobject loo_PrivKey
oleobject loo_SourcePublic
string ls_PublicJwk
oleobject loo_KeyInfo
string ls_KeySpec
long li_StringMatch3
long li_StringMatch4
long li_StringMatch5
long li_StringMatch6
long li_StringMatch7
long li_StringMatch8
oleobject loo_Options
oleobject loo_Imported
long li_StringMatch9
li_Success = 0
loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_StringCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this credentials file outside public web content and source control.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")
loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/kms.json")
if li_Success <> 1 then
Write-Debug "Could not load private KMS configuration."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("access_key"))
li_StringMatch1 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_StringMatch1 = 1) then
Write-Debug "Missing or invalid access_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_StringMatch2 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch2 = 1) then
Write-Debug "Missing or invalid secret_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
ls_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 loo_Credentials.HasMember("session_token") = 1 then
Write-Debug "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
// pfx_password is a string; an empty string is allowed for an unprotected PFX.
if loo_Credentials.JsonTypeOf("pfx_password") <> 1 then
Write-Debug "Missing or invalid pfx_password."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/myCertAndKey.pfx",loo_Credentials.StringOf("pfx_password"))
if li_Success <> 1 then
Write-Debug "Could not load PFX; inspect diagnostics privately."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
return
end if
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_Cert.GetPrivateKey(loo_PrivKey)
if li_Success <> 1 then
Write-Debug "PFX does not contain a usable private key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
return
end if
// Only inspect the PUBLIC JWK to identify a supported key type and curve.
loo_SourcePublic = create oleobject
li_rc = loo_SourcePublic.ConnectToNewObject("Chilkat.PublicKey")
li_Success = loo_PrivKey.ToPublicKey(loo_SourcePublic)
if li_Success <> 1 then
Write-Debug "Could not get the public key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
return
end if
ls_PublicJwk = loo_SourcePublic.GetJwk()
if loo_SourcePublic.LastMethodSuccess <> 1 then
Write-Debug "Could not inspect public key type."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
return
end if
loo_KeyInfo = create oleobject
li_rc = loo_KeyInfo.ConnectToNewObject("Chilkat.JsonObject")
loo_KeyInfo.Strict = 1
li_Success = loo_KeyInfo.Load(ls_PublicJwk)
if li_Success <> 1 then
Write-Debug "Could not parse public key description."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
return
end if
ls_KeySpec = ""
loo_StringCompare.SetString(loo_KeyInfo.StringOf("kty"))
li_StringMatch3 = loo_StringCompare.ContentsEqual("RSA",1)
if li_StringMatch3 = 1 then
if loo_PrivKey.BitLength = 2048 then
ls_KeySpec = "RSA_2048"
end if
if loo_PrivKey.BitLength = 3072 then
ls_KeySpec = "RSA_3072"
end if
if loo_PrivKey.BitLength = 4096 then
ls_KeySpec = "RSA_4096"
end if
end if
loo_StringCompare.SetString(loo_KeyInfo.StringOf("kty"))
li_StringMatch4 = loo_StringCompare.ContentsEqual("EC",1)
if li_StringMatch4 = 1 then
loo_StringCompare.SetString(loo_KeyInfo.StringOf("crv"))
li_StringMatch5 = loo_StringCompare.ContentsEqual("P-256",1)
if li_StringMatch5 = 1 then
ls_KeySpec = "ECC_NIST_P256"
end if
loo_StringCompare.SetString(loo_KeyInfo.StringOf("crv"))
li_StringMatch6 = loo_StringCompare.ContentsEqual("P-384",1)
if li_StringMatch6 = 1 then
ls_KeySpec = "ECC_NIST_P384"
end if
loo_StringCompare.SetString(loo_KeyInfo.StringOf("crv"))
li_StringMatch7 = loo_StringCompare.ContentsEqual("P-521",1)
if li_StringMatch7 = 1 then
ls_KeySpec = "ECC_NIST_P521"
end if
end if
loo_StringCompare.SetString(ls_KeySpec)
li_StringMatch8 = loo_StringCompare.ContentsEqual("",1)
if li_StringMatch8 = 1 then
Write-Debug "Use RSA 2048/3072/4096 or a NIST P-256/P-384/P-521 EC private key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
return
end if
loo_Options = create oleobject
li_rc = loo_Options.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Options.UpdateString("service","aws-kms")
if li_Success <> 1 then
Write-Debug "Could not set service."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
return
end if
li_Success = loo_Options.UpdateString("auth.access_key",loo_Credentials.StringOf("access_key"))
if li_Success <> 1 then
Write-Debug "Could not set auth.access_key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
return
end if
li_Success = loo_Options.UpdateString("auth.secret_key",loo_Credentials.StringOf("secret_key"))
if li_Success <> 1 then
Write-Debug "Could not set auth.secret_key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
return
end if
li_Success = loo_Options.UpdateString("auth.region",ls_Region)
if li_Success <> 1 then
Write-Debug "Could not set auth.region."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
return
end if
li_Success = loo_Options.UpdateString("description","Imported certificate signing key")
if li_Success <> 1 then
Write-Debug "Could not set description."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
return
end if
li_Success = loo_Options.UpdateString("tags[0].key","Purpose")
if li_Success <> 1 then
Write-Debug "Could not set tags[0].key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
return
end if
li_Success = loo_Options.UpdateString("tags[0].value","DocumentSigning")
if li_Success <> 1 then
Write-Debug "Could not set tags[0].value."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
return
end if
li_Success = loo_Options.UpdateString("keyUsage","SIGN_VERIFY")
if li_Success <> 1 then
Write-Debug "Could not set keyUsage."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
return
end if
loo_Imported = create oleobject
li_rc = loo_Imported.ConnectToNewObject("Chilkat.JsonObject")
// Creates a NEW KMS key. Do not blindly rerun after an uncertain failure.
li_Success = loo_PrivKey.UploadToCloud(loo_Options,loo_Imported)
if li_Success <> 1 then
Write-Debug "Import failed or its outcome is uncertain. Check KMS/CloudTrail before retrying; inspect diagnostics privately."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
destroy loo_Imported
return
end if
loo_StringCompare.SetString(loo_Imported.StringOf("KeyId"))
li_StringMatch9 = loo_StringCompare.ContentsEqual("",1)
if (loo_Imported.JsonTypeOf("KeyId") <> 1) OR (li_StringMatch9 = 1) then
Write-Debug "Missing or invalid KeyId."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
destroy loo_Imported
return
end if
Write-Debug "Imported KMS key ID: " + loo_Imported.StringOf("KeyId")
// Save this identifier securely for subsequent signing and lifecycle management.
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_PrivKey
destroy loo_SourcePublic
destroy loo_KeyInfo
destroy loo_Options
destroy loo_Imported