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