Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.6.0+

AWS KMS: Import New Symmetric Key Material for Rotation

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.

Use an existing EXTERNAL symmetric ENCRYPT_DECRYPT key and a protected, durably backed-up file qa_data/kms/import_aes256.bin containing exactly 32 raw bytes. This imports an existing file rather than generating unrecoverable material at runtime. Uses RSAES_OAEP_SHA_256 with RSA_4096. For multi-Region keys, run on the primary first. Sends NEW_KEY_MATERIAL; for an existing enabled key, the new material must reach PENDING_ROTATION. Record the returned material ID; import those same bytes into every replica before rotation. This does not trigger rotation. Nonexpiring material is an explicit policy choice; use KEY_MATERIAL_EXPIRES/ValidTo if expiration is required.

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
string ls_Region
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
long li_StringMatch3
oleobject loo_Connection
oleobject loo_Body
oleobject loo_Result
string ls_ResponseJson
long li_StringMatch4
string ls_KeyId
long li_StringMatch5
long li_StringMatch6
long li_StringMatch7
long li_StringMatch8
long li_StringMatch9
long li_StringMatch10
long li_StringMatch11
long li_StringMatch12
long li_StringMatch13
long li_StringMatch14
oleobject loo_Material
long li_StringMatch15
long li_StringMatch16
long li_StringMatch17
long li_StringMatch18
long li_StringMatch19
long li_StringMatch20
oleobject loo_Now
oleobject loo_Expires
string ls_ImportToken
oleobject loo_WrappingKey
oleobject loo_Rsa
long li_StringMatch21
long li_StringMatch22

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"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

loo_Endpoint.Append("kms.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")

loo_Aws.AccessKey = loo_Credentials.StringOf("access_key")
loo_Aws.SecretKey = loo_Credentials.StringOf("secret_key")
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "kms"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
loo_Rest.VerboseLogging = 0
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
    Write-Debug "Could not configure AWS signing."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    return
end if

if loo_Credentials.HasMember("session_token") = 1 then
    loo_StringCompare.SetString(loo_Credentials.StringOf("session_token"))
    li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
    if (loo_Credentials.JsonTypeOf("session_token") <> 1) OR (li_StringMatch3 = 1) then
        Write-Debug "Missing or invalid session_token."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        return
    end if

end if

loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
    Write-Debug "TLS connection failed; inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

li_Success = loo_Rest.UseConnection(loo_Connection,1)
if li_Success <> 1 then
    Write-Debug "Could not attach TLS connection."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

//  Keep connection alive while Rest uses it.
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

loo_Result.Strict = 1

loo_StringCompare.SetString(loo_Credentials.StringOf("key_id"))
li_StringMatch4 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("key_id") <> 1) OR (li_StringMatch4 = 1) then
    Write-Debug "Missing or invalid key_id."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

ls_KeyId = loo_Credentials.StringOf("key_id")
loo_Body.Clear()
li_Success = loo_Body.UpdateString("KeyId",ls_KeyId)
if li_Success <> 1 then
    Write-Debug "Could not set KeyId."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

li_Success = loo_Rest.ClearAllQueryParams()
if li_Success <> 1 then
    Write-Debug "Could not clear previous query parameters."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

//  Keep the JSON operation body separate from authentication parameters.
//  Include STS credentials in the signed query; never log the request URL.
if loo_Credentials.HasMember("session_token") = 1 then
    li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
    if li_Success <> 1 then
        Write-Debug "Could not set session token."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        return
    end if

end if

li_Success = loo_Rest.AddHeader("X-Amz-Target","TrentService.DescribeKey")
if li_Success <> 1 then
    Write-Debug "Could not set operation."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

li_Success = loo_Rest.AddHeader("Content-Type","application/x-amz-json-1.1")
if li_Success <> 1 then
    Write-Debug "Could not set content type."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

ls_ResponseJson = loo_Rest.FullRequestString("POST","/",loo_Body.Emit())
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "KMS HTTP status: " + string(loo_Rest.ResponseStatusCode)
    li_Success = loo_Result.Load(ls_ResponseJson)
    if li_Success = 1 then
        if loo_Result.JsonTypeOf("__type") = 1 then
            Write-Debug "AWS error code: " + loo_Result.StringOf("__type")
        end if

    end if

    Write-Debug "AWS request ID: " + loo_Rest.ResponseHdrByName("x-amzn-RequestId")
    //  Do not print full bodies, headers, credentials, key material, or tokens.
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

li_Success = loo_Result.Load(ls_ResponseJson)
if li_Success <> 1 then
    Write-Debug "Expected a well-formed JSON response."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.Arn"))
li_StringMatch5 = loo_StringCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("KeyMetadata.Arn") <> 1) OR (li_StringMatch5 = 1) then
    Write-Debug "Missing or invalid KeyMetadata.Arn."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.KeyId"))
li_StringMatch6 = loo_StringCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("KeyMetadata.KeyId") <> 1) OR (li_StringMatch6 = 1) then
    Write-Debug "Missing or invalid KeyMetadata.KeyId."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.KeySpec"))
li_StringMatch7 = loo_StringCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("KeyMetadata.KeySpec") <> 1) OR (li_StringMatch7 = 1) then
    Write-Debug "Missing or invalid KeyMetadata.KeySpec."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.KeySpec"))
li_StringMatch8 = loo_StringCompare.ContentsEqual("SYMMETRIC_DEFAULT",1)
if li_StringMatch8 <> 1 then
    Write-Debug "Unexpected KeyMetadata.KeySpec."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.KeyUsage"))
li_StringMatch9 = loo_StringCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("KeyMetadata.KeyUsage") <> 1) OR (li_StringMatch9 = 1) then
    Write-Debug "Missing or invalid KeyMetadata.KeyUsage."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.KeyUsage"))
li_StringMatch10 = loo_StringCompare.ContentsEqual("ENCRYPT_DECRYPT",1)
if li_StringMatch10 <> 1 then
    Write-Debug "Unexpected KeyMetadata.KeyUsage."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.Origin"))
li_StringMatch11 = loo_StringCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("KeyMetadata.Origin") <> 1) OR (li_StringMatch11 = 1) then
    Write-Debug "Missing or invalid KeyMetadata.Origin."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.Origin"))
li_StringMatch12 = loo_StringCompare.ContentsEqual("EXTERNAL",1)
if li_StringMatch12 <> 1 then
    Write-Debug "Unexpected KeyMetadata.Origin."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.KeyManager"))
li_StringMatch13 = loo_StringCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("KeyMetadata.KeyManager") <> 1) OR (li_StringMatch13 = 1) then
    Write-Debug "Missing or invalid KeyMetadata.KeyManager."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.KeyManager"))
li_StringMatch14 = loo_StringCompare.ContentsEqual("CUSTOMER",1)
if li_StringMatch14 <> 1 then
    Write-Debug "Unexpected KeyMetadata.KeyManager."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    return
end if

ls_KeyId = loo_Result.StringOf("KeyMetadata.Arn")
loo_Material = create oleobject
li_rc = loo_Material.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Material.LoadFile("qa_data/kms/import_aes256.bin")
if li_Success <> 1 then
    Write-Debug "Could not load qa_data/kms/import_aes256.bin."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

if loo_Material.NumBytes <> 32 then
    Write-Debug "Provide exactly 32 bytes of backed-up AES-256 key material."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

if loo_Result.BoolOf("KeyMetadata.MultiRegion") = 1 then
    loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType"))
    li_StringMatch15 = loo_StringCompare.ContentsEqual("",1)
    if (loo_Result.JsonTypeOf("KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType") <> 1) OR (li_StringMatch15 = 1) then
        Write-Debug "Missing or invalid KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_Material
        return
    end if

    loo_StringCompare.SetString(loo_Result.StringOf("KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType"))
    li_StringMatch16 = loo_StringCompare.ContentsEqual("PRIMARY",1)
    if li_StringMatch16 <> 1 then
        Write-Debug "Unexpected KeyMetadata.MultiRegionConfiguration.MultiRegionKeyType."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_Material
        return
    end if

end if

loo_Body.Clear()
li_Success = loo_Body.UpdateString("KeyId",ls_KeyId)
if li_Success <> 1 then
    Write-Debug "Could not set KeyId."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

li_Success = loo_Body.UpdateString("WrappingAlgorithm","RSAES_OAEP_SHA_256")
if li_Success <> 1 then
    Write-Debug "Could not set WrappingAlgorithm."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

li_Success = loo_Body.UpdateString("WrappingKeySpec","RSA_4096")
if li_Success <> 1 then
    Write-Debug "Could not set WrappingKeySpec."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

li_Success = loo_Rest.ClearAllQueryParams()
if li_Success <> 1 then
    Write-Debug "Could not clear previous query parameters."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

//  Keep the JSON operation body separate from authentication parameters.
//  Include STS credentials in the signed query; never log the request URL.
if loo_Credentials.HasMember("session_token") = 1 then
    li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
    if li_Success <> 1 then
        Write-Debug "Could not set session token."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_Material
        return
    end if

end if

li_Success = loo_Rest.AddHeader("X-Amz-Target","TrentService.GetParametersForImport")
if li_Success <> 1 then
    Write-Debug "Could not set operation."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

li_Success = loo_Rest.AddHeader("Content-Type","application/x-amz-json-1.1")
if li_Success <> 1 then
    Write-Debug "Could not set content type."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

ls_ResponseJson = loo_Rest.FullRequestString("POST","/",loo_Body.Emit())
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "KMS HTTP status: " + string(loo_Rest.ResponseStatusCode)
    li_Success = loo_Result.Load(ls_ResponseJson)
    if li_Success = 1 then
        if loo_Result.JsonTypeOf("__type") = 1 then
            Write-Debug "AWS error code: " + loo_Result.StringOf("__type")
        end if

    end if

    Write-Debug "AWS request ID: " + loo_Rest.ResponseHdrByName("x-amzn-RequestId")
    //  Do not print full bodies, headers, credentials, key material, or tokens.
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

li_Success = loo_Result.Load(ls_ResponseJson)
if li_Success <> 1 then
    Write-Debug "Expected a well-formed JSON response."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyId"))
li_StringMatch17 = loo_StringCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("KeyId") <> 1) OR (li_StringMatch17 = 1) then
    Write-Debug "Missing or invalid KeyId."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("KeyId"))
li_StringMatch18 = loo_StringCompare.ContentsEqual(ls_KeyId,1)
if li_StringMatch18 <> 1 then
    Write-Debug "Response belongs to another key; use a fixed key ARN in configuration."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("ImportToken"))
li_StringMatch19 = loo_StringCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("ImportToken") <> 1) OR (li_StringMatch19 = 1) then
    Write-Debug "Missing or invalid ImportToken."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

loo_StringCompare.SetString(loo_Result.StringOf("PublicKey"))
li_StringMatch20 = loo_StringCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("PublicKey") <> 1) OR (li_StringMatch20 = 1) then
    Write-Debug "Missing or invalid PublicKey."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

if loo_Result.JsonTypeOf("ParametersValidTo") <> 2 then
    Write-Debug "Missing or invalid ParametersValidTo."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    return
end if

loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")

loo_Expires = create oleobject
li_rc = loo_Expires.ConnectToNewObject("Chilkat.CkDateTime")

li_Success = loo_Now.SetFromCurrentSystemTime()
if li_Success <> 1 then
    Write-Debug "Could not read current time."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    return
end if

li_Success = loo_Result.DateOf("ParametersValidTo",loo_Expires)
if li_Success <> 1 then
    Write-Debug "Invalid parameter expiration."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    return
end if

if loo_Expires.DiffSeconds(loo_Now) <= 0 then
    Write-Debug "Wrapping parameters expired."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    return
end if

ls_ImportToken = loo_Result.StringOf("ImportToken")
loo_WrappingKey = create oleobject
li_rc = loo_WrappingKey.ConnectToNewObject("Chilkat.PublicKey")

li_Success = loo_WrappingKey.LoadBase64(loo_Result.StringOf("PublicKey"))
if li_Success <> 1 then
    Write-Debug "Invalid RSA wrapping key."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    return
end if

if loo_WrappingKey.KeySize <> 4096 then
    Write-Debug "Expected RSA-4096 wrapping key."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    return
end if

loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")

loo_Rsa.PkcsPadding = 0
loo_Rsa.OaepHash = "sha256"
loo_Rsa.OaepMgfHash = "sha256"
li_Success = loo_Rsa.UsePublicKey(loo_WrappingKey)
if li_Success <> 1 then
    Write-Debug "Could not select wrapping key."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

li_Success = loo_Rsa.EncryptBd(loo_Material,0)
if li_Success <> 1 then
    Write-Debug "Could not wrap AES key material."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

loo_Body.Clear()
li_Success = loo_Body.UpdateString("KeyId",ls_KeyId)
if li_Success <> 1 then
    Write-Debug "Could not set KeyId."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

li_Success = loo_Body.UpdateString("ImportToken",ls_ImportToken)
if li_Success <> 1 then
    Write-Debug "Could not set ImportToken."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

li_Success = loo_Body.UpdateString("EncryptedKeyMaterial",loo_Material.GetEncoded("base64"))
if li_Success <> 1 then
    Write-Debug "Could not set EncryptedKeyMaterial."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

li_Success = loo_Body.UpdateString("ExpirationModel","KEY_MATERIAL_DOES_NOT_EXPIRE")
if li_Success <> 1 then
    Write-Debug "Could not set ExpirationModel."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

li_Success = loo_Body.UpdateString("ImportType","NEW_KEY_MATERIAL")
if li_Success <> 1 then
    Write-Debug "Could not set ImportType."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

li_Success = loo_Body.UpdateString("KeyMaterialDescription","Application rotation material")
if li_Success <> 1 then
    Write-Debug "Could not set KeyMaterialDescription."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

li_Success = loo_Rest.ClearAllQueryParams()
if li_Success <> 1 then
    Write-Debug "Could not clear previous query parameters."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

//  Keep the JSON operation body separate from authentication parameters.
//  Include STS credentials in the signed query; never log the request URL.
if loo_Credentials.HasMember("session_token") = 1 then
    li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
    if li_Success <> 1 then
        Write-Debug "Could not set session token."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_Material
        destroy loo_Now
        destroy loo_Expires
        destroy loo_WrappingKey
        destroy loo_Rsa
        return
    end if

end if

li_Success = loo_Rest.AddHeader("X-Amz-Target","TrentService.ImportKeyMaterial")
if li_Success <> 1 then
    Write-Debug "Could not set operation."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

li_Success = loo_Rest.AddHeader("Content-Type","application/x-amz-json-1.1")
if li_Success <> 1 then
    Write-Debug "Could not set content type."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

ls_ResponseJson = loo_Rest.FullRequestString("POST","/",loo_Body.Emit())
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "KMS HTTP status: " + string(loo_Rest.ResponseStatusCode)
    li_Success = loo_Result.Load(ls_ResponseJson)
    if li_Success = 1 then
        if loo_Result.JsonTypeOf("__type") = 1 then
            Write-Debug "AWS error code: " + loo_Result.StringOf("__type")
        end if

    end if

    Write-Debug "AWS request ID: " + loo_Rest.ResponseHdrByName("x-amzn-RequestId")
    //  Do not print full bodies, headers, credentials, key material, or tokens.
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_Material
    destroy loo_Now
    destroy loo_Expires
    destroy loo_WrappingKey
    destroy loo_Rsa
    return
end if

//  ImportKeyMaterial acceptance does not mean rotation has completed.
loo_StringCompare.SetString(ls_ResponseJson)
li_StringMatch21 = loo_StringCompare.ContentsEqual("",1)
if li_StringMatch21 <> 1 then
    li_Success = loo_Result.Load(ls_ResponseJson)
    if li_Success <> 1 then
        Write-Debug "Import accepted, but the response could not be parsed; reconcile before retrying."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_Material
        destroy loo_Now
        destroy loo_Expires
        destroy loo_WrappingKey
        destroy loo_Rsa
        return
    end if

    loo_StringCompare.SetString(loo_Result.StringOf("KeyMaterialId"))
    li_StringMatch22 = loo_StringCompare.ContentsEqual("",1)
    if (loo_Result.JsonTypeOf("KeyMaterialId") <> 1) OR (li_StringMatch22 = 1) then
        Write-Debug "Missing or invalid KeyMaterialId."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Body
        destroy loo_Result
        destroy loo_Material
        destroy loo_Now
        destroy loo_Expires
        destroy loo_WrappingKey
        destroy loo_Rsa
        return
    end if

    Write-Debug "Imported material ID: " + loo_Result.StringOf("KeyMaterialId")
else
    Write-Debug "Import accepted; retrieve the material identifier/state with ListKeyRotations."
end if

Write-Debug "Symmetric material import accepted; inspect ListKeyRotations before rotation."


destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_Material
destroy loo_Now
destroy loo_Expires
destroy loo_WrappingKey
destroy loo_Rsa