AWS KMS: Import a PFX Signing Key with a 30-Day Expiration
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.
Uses the AWS JSON 1.1 protocol with TrentService targets, service kms, HTTPS certificate verification, and Signature Version 4. When using temporary credentials, also provide a nonempty session_token; this Rest example includes it in the signed query on each request. Never log that URL. The endpoint shown is for the commercial AWS partition; use the appropriate endpoint for other partitions. Failures stop the sample; production retry policies should use bounded backoff with jitter and reconcile uncertain writes.
Where a key is required, set string key_id to its fixed key ARN in the configured region. Additional named fields and input files are described below. Keep output files private when they contain plaintext or policies. The code does not automatically retry mutations, refresh credentials, or create output directories. A successful request can precede propagation of a state change. Discard sensitive objects promptly; clearing one buffer does not erase copies held in strings, JSON, or runtime memory.
Uses the same checked RSA/AES wrapping flow as the existing low-level import example. Provide pfx_password and key_id and the private qa_data/pfx/myCertAndKey.pfx. Requires an EXTERNAL PendingImport signing key matching the PFX key specification. Material expires 30 days from execution (distinct from the wrapping-token lifetime). Expiration disables cryptographic use until the same material is reimported. Retain a protected original backup. Hybrid wrapping is unavailable in China Regions.
API actions used: kms:DescribeKey, kms:GetParametersForImport, kms:ImportKeyMaterial. IAM permissions and key policies must allow the actual resources; grant and multi-Region workflows can need permissions in more than one region/account.
DescribeKey | GetParametersForImport | ImportKeyMaterial
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoCredentials
Boolean iStringMatch1
Boolean iStringMatch2
String sRegion
Handle hoEndpoint
Variant vAws
Handle hoAws
Handle hoRest
Boolean iStringMatch3
Variant vConnection
Handle hoConnection
Handle hoBody
Handle hoResult
String sResponseJson
Handle hoCert
Variant vPrivKey
Handle hoPrivKey
Variant vSourcePublic
Handle hoSourcePublic
String sPublicJwk
Handle hoKeyInfo
String sKeySpec
Boolean iStringMatch4
Boolean iStringMatch5
Boolean iStringMatch6
Boolean iStringMatch7
Boolean iStringMatch8
Boolean iStringMatch9
Boolean iStringMatch10
String sKeyId
Boolean iStringMatch11
Boolean iStringMatch12
Boolean iStringMatch13
Boolean iStringMatch14
Boolean iStringMatch15
Boolean iStringMatch16
Boolean iStringMatch17
Boolean iStringMatch18
Boolean iStringMatch19
Boolean iStringMatch20
Variant vNow
Handle hoNow
Variant vExpires
Handle hoExpires
String sImportToken
Variant vWrappingKey
Handle hoWrappingKey
String sPkcs8Base64
Handle hoPrng
String sAesKeyBase64
Handle hoCrypt
String sWrappedPrivateKey
Variant vWrappedAesKey
Handle hoWrappedAesKey
Handle hoRsa
Handle hoEncryptedMaterial
Handle hoValidTo
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
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "kms." To iSuccess
Get ComAppend Of hoEndpoint sRegion To iSuccess
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
Get Create (RefClass(cComChilkatAuthAws)) To hoAws
If (Not(IsComObjectCreated(hoAws))) Begin
Send CreateComObject of hoAws
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAccessKey Of hoAws To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComSecretKey Of hoAws To sTemp1
Set ComRegion Of hoAws To sRegion
Set ComServiceName Of hoAws To "kms"
Set ComSignatureVersion Of hoAws To 4
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Set ComConnectTimeoutMs Of hoRest To 20000
Set ComIdleTimeoutMs Of hoRest To 60000
Set ComVerboseLogging Of hoRest To False
Get pvComObject of hoAws to vAws
Get ComSetAuthAws Of hoRest vAws To iSuccess
If (iSuccess <> True) Begin
Showln "Could not configure AWS signing."
Procedure_Return
End
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
Showln "Missing or invalid session_token."
Procedure_Return
End
End
Get Create (RefClass(cComChilkatSocket)) To hoConnection
If (Not(IsComObjectCreated(hoConnection))) Begin
Send CreateComObject of hoConnection
End
Set ComRequireSslCertVerify Of hoConnection To True
Set ComMaxReadIdleMs Of hoConnection To 60000
Set ComMaxSendIdleMs Of hoConnection To 60000
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
If (iSuccess <> True) Begin
Showln "TLS connection failed; inspect diagnostics privately."
Procedure_Return
End
Get pvComObject of hoConnection to vConnection
Get ComUseConnection Of hoRest vConnection True To iSuccess
If (iSuccess <> True) Begin
Showln "Could not attach TLS connection."
Procedure_Return
End
// Keep connection alive while Rest uses it.
Get Create (RefClass(cComChilkatJsonObject)) To hoBody
If (Not(IsComObjectCreated(hoBody))) Begin
Send CreateComObject of hoBody
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Set ComStrict Of hoResult To True
// 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 iStringMatch4
If (iStringMatch4 = 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 iStringMatch5
If (iStringMatch5 = True) Begin
Get ComStringOf Of hoKeyInfo "crv" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "P-256" True To iStringMatch6
If (iStringMatch6 = 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 iStringMatch7
If (iStringMatch7 = 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 iStringMatch8
If (iStringMatch8 = True) Begin
Move "ECC_NIST_P521" To sKeySpec
End
End
Get ComSetString Of hoStringCompare sKeySpec To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch9
If (iStringMatch9 = True) Begin
Showln "Use RSA 2048/3072/4096 or a NIST P-256/P-384/P-521 EC private key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "key_id" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch10
Get ComJsonTypeOf Of hoCredentials "key_id" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch10 = True)) Begin
Showln "Missing or invalid key_id."
Procedure_Return
End
Get ComStringOf Of hoCredentials "key_id" To sKeyId
Get ComUpdateString Of hoBody "KeyId" sKeyId To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set KeyId."
Procedure_Return
End
Get ComClearAllQueryParams Of hoRest To iSuccess
If (iSuccess <> True) Begin
Showln "Could not clear previous query parameters."
Procedure_Return
End
// Keep the JSON operation body separate from authentication parameters.
// Include STS credentials in the signed query; never log the request URL.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set session token."
Procedure_Return
End
End
Get ComAddHeader Of hoRest "X-Amz-Target" "TrentService.DescribeKey" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set operation."
Procedure_Return
End
Get ComAddHeader Of hoRest "Content-Type" "application/x-amz-json-1.1" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set content type."
Procedure_Return
End
Get ComEmit Of hoBody To sTemp1
Get ComFullRequestString Of hoRest "POST" "/" sTemp1 To sResponseJson
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "KMS HTTP status: " iTemp1
Get ComLoad Of hoResult sResponseJson To iSuccess
If (iSuccess = True) Begin
Get ComJsonTypeOf Of hoResult "__type" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOf Of hoResult "__type" To sTemp1
Showln "AWS error code: " sTemp1
End
End
Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
Showln "AWS request ID: " sTemp1
// Do not print full bodies, headers, credentials, key material, or tokens.
Procedure_Return
End
Get ComLoad Of hoResult sResponseJson To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a well-formed JSON response."
Procedure_Return
End
Get ComStringOf Of hoResult "KeyMetadata.KeyId" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch11
Get ComJsonTypeOf Of hoResult "KeyMetadata.KeyId" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch11 = True)) Begin
Showln "Missing or invalid KeyMetadata.KeyId."
Procedure_Return
End
Get ComStringOf Of hoResult "KeyMetadata.Arn" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch12
Get ComJsonTypeOf Of hoResult "KeyMetadata.Arn" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch12 = True)) Begin
Showln "Missing or invalid KeyMetadata.Arn."
Procedure_Return
End
Get ComStringOf Of hoResult "KeyMetadata.Origin" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "EXTERNAL" True To iStringMatch13
If (iStringMatch13 <> True) Begin
Showln "Destination must have EXTERNAL origin."
Procedure_Return
End
Get ComStringOf Of hoResult "KeyMetadata.KeyState" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "PendingImport" True To iStringMatch14
If (iStringMatch14 <> True) Begin
Showln "This initial-import sample requires PendingImport; it does not replace existing material."
Procedure_Return
End
Get ComStringOf Of hoResult "KeyMetadata.KeyUsage" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "SIGN_VERIFY" True To iStringMatch15
If (iStringMatch15 <> True) Begin
Showln "Destination must have SIGN_VERIFY usage."
Procedure_Return
End
Get ComStringOf Of hoResult "KeyMetadata.KeySpec" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare sKeySpec True To iStringMatch16
If (iStringMatch16 <> True) Begin
Showln "PFX private key does not match the destination KeySpec."
Procedure_Return
End
// Pin subsequent requests to the resolved ARN, not a mutable alias.
Get ComStringOf Of hoResult "KeyMetadata.Arn" To sKeyId
Send ComClear To hoBody
Get ComUpdateString Of hoBody "KeyId" sKeyId To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set KeyId."
Procedure_Return
End
Get ComUpdateString Of hoBody "WrappingAlgorithm" "RSA_AES_KEY_WRAP_SHA_256" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set WrappingAlgorithm."
Procedure_Return
End
Get ComUpdateString Of hoBody "WrappingKeySpec" "RSA_4096" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set WrappingKeySpec."
Procedure_Return
End
Get ComClearAllQueryParams Of hoRest To iSuccess
If (iSuccess <> True) Begin
Showln "Could not clear previous query parameters."
Procedure_Return
End
// Keep the JSON operation body separate from authentication parameters.
// Include STS credentials in the signed query; never log the request URL.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set session token."
Procedure_Return
End
End
Get ComAddHeader Of hoRest "X-Amz-Target" "TrentService.GetParametersForImport" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set operation."
Procedure_Return
End
Get ComAddHeader Of hoRest "Content-Type" "application/x-amz-json-1.1" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set content type."
Procedure_Return
End
Get ComEmit Of hoBody To sTemp1
Get ComFullRequestString Of hoRest "POST" "/" sTemp1 To sResponseJson
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "KMS HTTP status: " iTemp1
Get ComLoad Of hoResult sResponseJson To iSuccess
If (iSuccess = True) Begin
Get ComJsonTypeOf Of hoResult "__type" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOf Of hoResult "__type" To sTemp1
Showln "AWS error code: " sTemp1
End
End
Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
Showln "AWS request ID: " sTemp1
// Do not print full bodies, headers, credentials, key material, or tokens.
Procedure_Return
End
Get ComLoad Of hoResult sResponseJson To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a well-formed JSON response."
Procedure_Return
End
Get ComStringOf Of hoResult "KeyId" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch17
Get ComJsonTypeOf Of hoResult "KeyId" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch17 = True)) Begin
Showln "Missing or invalid KeyId."
Procedure_Return
End
Get ComStringOf Of hoResult "KeyId" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare sKeyId True To iStringMatch18
If (iStringMatch18 <> True) Begin
Showln "Import parameters belong to another key."
Procedure_Return
End
Get ComStringOf Of hoResult "ImportToken" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch19
Get ComJsonTypeOf Of hoResult "ImportToken" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch19 = True)) Begin
Showln "Missing or invalid ImportToken."
Procedure_Return
End
Get ComStringOf Of hoResult "PublicKey" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch20
Get ComJsonTypeOf Of hoResult "PublicKey" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch20 = True)) Begin
Showln "Missing or invalid PublicKey."
Procedure_Return
End
Get ComJsonTypeOf Of hoResult "ParametersValidTo" To iTemp1
If (iTemp1 <> 2) Begin
Showln "Missing import-parameter expiration."
Procedure_Return
End
Get Create (RefClass(cComCkDateTime)) To hoNow
If (Not(IsComObjectCreated(hoNow))) Begin
Send CreateComObject of hoNow
End
Get Create (RefClass(cComCkDateTime)) To hoExpires
If (Not(IsComObjectCreated(hoExpires))) Begin
Send CreateComObject of hoExpires
End
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read current time."
Procedure_Return
End
Get pvComObject of hoExpires to vExpires
Get ComDateOf Of hoResult "ParametersValidTo" vExpires To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid import-parameter expiration."
Procedure_Return
End
Get ComDiffSeconds Of hoExpires Get pvComObject of hoNow to vNow
vNow To iTemp1
If (iTemp1 <= 0) Begin
Showln "Import parameters have expired; obtain a fresh token and wrapping key together."
Procedure_Return
End
Get ComStringOf Of hoResult "ImportToken" To sImportToken
Get Create (RefClass(cComChilkatPublicKey)) To hoWrappingKey
If (Not(IsComObjectCreated(hoWrappingKey))) Begin
Send CreateComObject of hoWrappingKey
End
Get ComStringOf Of hoResult "PublicKey" To sTemp1
Get ComLoadBase64 Of hoWrappingKey sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load the RSA wrapping key."
Procedure_Return
End
Get ComKeySize Of hoWrappingKey To iTemp1
If (iTemp1 <> 4096) Begin
Showln "Expected the requested 4096-bit RSA wrapping key."
Procedure_Return
End
// Export PKCS#8 DER in memory only; never print or write unencrypted key material.
Get ComGetPkcs8ENC Of hoPrivKey "base64" To sPkcs8Base64
Get ComLastMethodSuccess Of hoPrivKey To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not encode the private key as PKCS#8."
Procedure_Return
End
Get Create (RefClass(cComChilkatPrng)) To hoPrng
If (Not(IsComObjectCreated(hoPrng))) Begin
Send CreateComObject of hoPrng
End
// A fresh 32-byte (256-bit) AES key, not a 32-bit key.
Get ComGenRandom Of hoPrng 32 "base64" To sAesKeyBase64
Get ComLastMethodSuccess Of hoPrng To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not generate wrapping key."
Procedure_Return
End
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Get ComAesKeyWrapWithPadding Of hoCrypt sAesKeyBase64 sPkcs8Base64 "base64" To sWrappedPrivateKey
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "AES key wrapping failed."
Procedure_Return
End
Get Create (RefClass(cComChilkatBinData)) To hoWrappedAesKey
If (Not(IsComObjectCreated(hoWrappedAesKey))) Begin
Send CreateComObject of hoWrappedAesKey
End
Get ComAppendEncoded Of hoWrappedAesKey sAesKeyBase64 "base64" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not decode AES key."
Procedure_Return
End
Get Create (RefClass(cComChilkatRsa)) To hoRsa
If (Not(IsComObjectCreated(hoRsa))) Begin
Send CreateComObject of hoRsa
End
Get pvComObject of hoWrappingKey to vWrappingKey
Get ComUsePublicKey Of hoRsa vWrappingKey To iSuccess
If (iSuccess <> True) Begin
Showln "Could not select the RSA wrapping key."
Procedure_Return
End
Set ComPkcsPadding Of hoRsa To False
Set ComOaepHash Of hoRsa To "sha256"
Set ComOaepMgfHash Of hoRsa To "sha256"
Get pvComObject of hoWrappedAesKey to vWrappedAesKey
Get ComEncryptBd Of hoRsa vWrappedAesKey False To iSuccess
If (iSuccess <> True) Begin
Showln "RSA-OAEP AES-key wrapping failed."
Procedure_Return
End
// Wire order is RSA-wrapped AES key FIRST, followed by AES-wrapped PKCS#8.
Get Create (RefClass(cComChilkatBinData)) To hoEncryptedMaterial
If (Not(IsComObjectCreated(hoEncryptedMaterial))) Begin
Send CreateComObject of hoEncryptedMaterial
End
Get pvComObject of hoWrappedAesKey to vWrappedAesKey
Get ComAppendBd Of hoEncryptedMaterial vWrappedAesKey To iSuccess
If (iSuccess <> True) Begin
Showln "Could not assemble encrypted material."
Procedure_Return
End
Get ComAppendEncoded Of hoEncryptedMaterial sWrappedPrivateKey "base64" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not append wrapped private key."
Procedure_Return
End
Send ComClear To hoBody
Get ComUpdateString Of hoBody "KeyId" sKeyId To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set KeyId."
Procedure_Return
End
Get ComUpdateString Of hoBody "ImportToken" sImportToken To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set ImportToken."
Procedure_Return
End
Get ComGetEncoded Of hoEncryptedMaterial "base64" To sTemp1
Get ComUpdateString Of hoBody "EncryptedKeyMaterial" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set EncryptedKeyMaterial."
Procedure_Return
End
Get Create (RefClass(cComCkDateTime)) To hoValidTo
If (Not(IsComObjectCreated(hoValidTo))) Begin
Send CreateComObject of hoValidTo
End
Get ComSetFromCurrentSystemTime Of hoValidTo To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set expiration time."
Procedure_Return
End
Get ComAddDays Of hoValidTo 30 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not compute expiration."
Procedure_Return
End
Get ComGetAsUnixTimeStr Of hoValidTo False To sTemp1
Get ComUpdateNumber Of hoBody "ValidTo" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set material expiration."
Procedure_Return
End
Get ComUpdateString Of hoBody "ExpirationModel" "KEY_MATERIAL_EXPIRES" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set ExpirationModel."
Procedure_Return
End
Get ComClearAllQueryParams Of hoRest To iSuccess
If (iSuccess <> True) Begin
Showln "Could not clear previous query parameters."
Procedure_Return
End
// Keep the JSON operation body separate from authentication parameters.
// Include STS credentials in the signed query; never log the request URL.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set session token."
Procedure_Return
End
End
Get ComAddHeader Of hoRest "X-Amz-Target" "TrentService.ImportKeyMaterial" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set operation."
Procedure_Return
End
Get ComAddHeader Of hoRest "Content-Type" "application/x-amz-json-1.1" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set content type."
Procedure_Return
End
Get ComEmit Of hoBody To sTemp1
Get ComFullRequestString Of hoRest "POST" "/" sTemp1 To sResponseJson
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "KMS HTTP status: " iTemp1
Get ComLoad Of hoResult sResponseJson To iSuccess
If (iSuccess = True) Begin
Get ComJsonTypeOf Of hoResult "__type" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOf Of hoResult "__type" To sTemp1
Showln "AWS error code: " sTemp1
End
End
Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
Showln "AWS request ID: " sTemp1
// Do not print full bodies, headers, credentials, key material, or tokens.
Procedure_Return
End
// HTTP 200 indicates acceptance; response content can be empty.
Showln "Key material import accepted for: " sKeyId
Showln "Use DescribeKey to confirm Enabled before signing; KMS is eventually consistent."
// This does not securely erase copies of sensitive strings from managed memory.
Showln "Example completed."
End_Procedure