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

AWS KMS: Import a PFX Key into an Existing External 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.

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.

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.

First create a compatible Origin=EXTERNAL, KeyUsage=SIGN_VERIFY KMS key with the correct KeySpec and provide its ID or ARN in key_id. This example requires PendingImport, checks metadata, pins the resolved ARN, obtains a fresh wrapping key/token pair, and imports PKCS#8 using RSA_AES_KEY_WRAP_SHA_256 and RSA_4096. It creates no new KMS key and makes no automatic retries. Required actions: kms:DescribeKey, kms:GetParametersForImport, and kms:ImportKeyMaterial.

Uses RFC 5649 AES key wrap with padding and RSA-OAEP with SHA-256 for both OAEP and MGF1. Hybrid RSA/AES wrapping is not supported in China Regions. The token and wrapping key must come from the same response for the same key and be used before ParametersValidTo (normally 24 hours). The sample chooses KEY_MATERIAL_DOES_NOT_EXPIRE explicitly; applications needing expiration must use KEY_MATERIAL_EXPIRES and a valid future ValidTo. Acceptance is not an immediate Enabled-state guarantee. Reimports and imported symmetric-key rotation require separate handling; this sample is for an initial asymmetric import.

AWS ImportKeyMaterial reference

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

set stringCompare = Server.CreateObject("Chilkat.StringBuilder")
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
'  Keep this credentials file outside public web content and source control.
set credentials = Server.CreateObject("Chilkat.JsonObject")
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/kms.json")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load private KMS configuration.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(credentials.StringOf("access_key"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (stringMatch1 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_key.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(credentials.StringOf("secret_key"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (stringMatch2 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid secret_key.") & "</pre>"
    Response.End
End If

region = "us-west-2"
set endpoint = Server.CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("kms.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
set aws = Server.CreateObject("Chilkat.AuthAws")
aws.AccessKey = credentials.StringOf("access_key")
aws.SecretKey = credentials.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "kms"
aws.SignatureVersion = 4
set rest = Server.CreateObject("Chilkat.Rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
rest.VerboseLogging = 0
success = rest.SetAuthAws(aws)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not configure AWS signing.") & "</pre>"
    Response.End
End If

If (credentials.HasMember("session_token") = 1) Then
    success = stringCompare.SetString(credentials.StringOf("session_token"))
    stringMatch3 = stringCompare.ContentsEqual("",1)
    If ((credentials.JsonTypeOf("session_token") <> 1) Or (stringMatch3 = 1)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid session_token.") & "</pre>"
        Response.End
    End If

End If

set connection = Server.CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "TLS connection failed; inspect diagnostics privately.") & "</pre>"
    Response.End
End If

success = rest.UseConnection(connection,1)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not attach TLS connection.") & "</pre>"
    Response.End
End If

'  Keep connection alive while Rest uses it.
set body = Server.CreateObject("Chilkat.JsonObject")
set result = Server.CreateObject("Chilkat.JsonObject")
result.Strict = 1

'  pfx_password is a string; an empty string is allowed for an unprotected PFX.
If (credentials.JsonTypeOf("pfx_password") <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid pfx_password.") & "</pre>"
    Response.End
End If

set cert = Server.CreateObject("Chilkat.Cert")
success = cert.LoadPfxFile("qa_data/pfx/myCertAndKey.pfx",credentials.StringOf("pfx_password"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load PFX; inspect diagnostics privately.") & "</pre>"
    Response.End
End If

set privKey = Server.CreateObject("Chilkat.PrivateKey")
success = cert.GetPrivateKey(privKey)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "PFX does not contain a usable private key.") & "</pre>"
    Response.End
End If

'  Only inspect the PUBLIC JWK to identify a supported key type and curve.
set sourcePublic = Server.CreateObject("Chilkat.PublicKey")
success = privKey.ToPublicKey(sourcePublic)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not get the public key.") & "</pre>"
    Response.End
End If

publicJwk = sourcePublic.GetJwk()
If (sourcePublic.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not inspect public key type.") & "</pre>"
    Response.End
End If

set keyInfo = Server.CreateObject("Chilkat.JsonObject")
keyInfo.Strict = 1
success = keyInfo.Load(publicJwk)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not parse public key description.") & "</pre>"
    Response.End
End If

keySpec = ""
success = stringCompare.SetString(keyInfo.StringOf("kty"))
stringMatch4 = stringCompare.ContentsEqual("RSA",1)
If (stringMatch4 = 1) 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

success = stringCompare.SetString(keyInfo.StringOf("kty"))
stringMatch5 = stringCompare.ContentsEqual("EC",1)
If (stringMatch5 = 1) Then
    success = stringCompare.SetString(keyInfo.StringOf("crv"))
    stringMatch6 = stringCompare.ContentsEqual("P-256",1)
    If (stringMatch6 = 1) Then
        keySpec = "ECC_NIST_P256"
    End If

    success = stringCompare.SetString(keyInfo.StringOf("crv"))
    stringMatch7 = stringCompare.ContentsEqual("P-384",1)
    If (stringMatch7 = 1) Then
        keySpec = "ECC_NIST_P384"
    End If

    success = stringCompare.SetString(keyInfo.StringOf("crv"))
    stringMatch8 = stringCompare.ContentsEqual("P-521",1)
    If (stringMatch8 = 1) Then
        keySpec = "ECC_NIST_P521"
    End If

End If

success = stringCompare.SetString(keySpec)
stringMatch9 = stringCompare.ContentsEqual("",1)
If (stringMatch9 = 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Use RSA 2048/3072/4096 or a NIST P-256/P-384/P-521 EC private key.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(credentials.StringOf("key_id"))
stringMatch10 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("key_id") <> 1) Or (stringMatch10 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid key_id.") & "</pre>"
    Response.End
End If

keyId = credentials.StringOf("key_id")
success = body.UpdateString("KeyId",keyId)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set KeyId.") & "</pre>"
    Response.End
End If

success = rest.ClearAllQueryParams()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not clear previous query parameters.") & "</pre>"
    Response.End
End If

'  Keep the JSON operation body separate from authentication parameters.
'  Include STS credentials in the signed query; never log the request URL.
If (credentials.HasMember("session_token") = 1) Then
    success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Could not set session token.") & "</pre>"
        Response.End
    End If

End If

success = rest.AddHeader("X-Amz-Target","TrentService.DescribeKey")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set operation.") & "</pre>"
    Response.End
End If

success = rest.AddHeader("Content-Type","application/x-amz-json-1.1")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set content type.") & "</pre>"
    Response.End
End If

responseJson = rest.FullRequestString("POST","/",body.Emit())
If (rest.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.") & "</pre>"
    Response.End
End If

If (rest.ResponseStatusCode <> 200) Then
    Response.Write "<pre>" & Server.HTMLEncode( "KMS HTTP status: " & rest.ResponseStatusCode) & "</pre>"
    success = result.Load(responseJson)
    If (success = 1) Then
        If (result.JsonTypeOf("__type") = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "AWS error code: " & result.StringOf("__type")) & "</pre>"
        End If

    End If

    Response.Write "<pre>" & Server.HTMLEncode( "AWS request ID: " & rest.ResponseHdrByName("x-amzn-RequestId")) & "</pre>"
    '  Do not print full bodies, headers, credentials, key material, or tokens.
    Response.End
End If

success = result.Load(responseJson)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a well-formed JSON response.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("KeyMetadata.KeyId"))
stringMatch11 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("KeyMetadata.KeyId") <> 1) Or (stringMatch11 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid KeyMetadata.KeyId.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("KeyMetadata.Arn"))
stringMatch12 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("KeyMetadata.Arn") <> 1) Or (stringMatch12 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid KeyMetadata.Arn.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("KeyMetadata.Origin"))
stringMatch13 = stringCompare.ContentsEqual("EXTERNAL",1)
If (stringMatch13 <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Destination must have EXTERNAL origin.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("KeyMetadata.KeyState"))
stringMatch14 = stringCompare.ContentsEqual("PendingImport",1)
If (stringMatch14 <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "This initial-import sample requires PendingImport; it does not replace existing material.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("KeyMetadata.KeyUsage"))
stringMatch15 = stringCompare.ContentsEqual("SIGN_VERIFY",1)
If (stringMatch15 <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Destination must have SIGN_VERIFY usage.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("KeyMetadata.KeySpec"))
stringMatch16 = stringCompare.ContentsEqual(keySpec,1)
If (stringMatch16 <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "PFX private key does not match the destination KeySpec.") & "</pre>"
    Response.End
End If

'  Pin subsequent requests to the resolved ARN, not a mutable alias.
keyId = result.StringOf("KeyMetadata.Arn")
body.Clear 
success = body.UpdateString("KeyId",keyId)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set KeyId.") & "</pre>"
    Response.End
End If

success = body.UpdateString("WrappingAlgorithm","RSA_AES_KEY_WRAP_SHA_256")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set WrappingAlgorithm.") & "</pre>"
    Response.End
End If

success = body.UpdateString("WrappingKeySpec","RSA_4096")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set WrappingKeySpec.") & "</pre>"
    Response.End
End If

success = rest.ClearAllQueryParams()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not clear previous query parameters.") & "</pre>"
    Response.End
End If

'  Keep the JSON operation body separate from authentication parameters.
'  Include STS credentials in the signed query; never log the request URL.
If (credentials.HasMember("session_token") = 1) Then
    success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Could not set session token.") & "</pre>"
        Response.End
    End If

End If

success = rest.AddHeader("X-Amz-Target","TrentService.GetParametersForImport")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set operation.") & "</pre>"
    Response.End
End If

success = rest.AddHeader("Content-Type","application/x-amz-json-1.1")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set content type.") & "</pre>"
    Response.End
End If

responseJson = rest.FullRequestString("POST","/",body.Emit())
If (rest.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.") & "</pre>"
    Response.End
End If

If (rest.ResponseStatusCode <> 200) Then
    Response.Write "<pre>" & Server.HTMLEncode( "KMS HTTP status: " & rest.ResponseStatusCode) & "</pre>"
    success = result.Load(responseJson)
    If (success = 1) Then
        If (result.JsonTypeOf("__type") = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "AWS error code: " & result.StringOf("__type")) & "</pre>"
        End If

    End If

    Response.Write "<pre>" & Server.HTMLEncode( "AWS request ID: " & rest.ResponseHdrByName("x-amzn-RequestId")) & "</pre>"
    '  Do not print full bodies, headers, credentials, key material, or tokens.
    Response.End
End If

success = result.Load(responseJson)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a well-formed JSON response.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("KeyId"))
stringMatch17 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("KeyId") <> 1) Or (stringMatch17 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid KeyId.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("KeyId"))
stringMatch18 = stringCompare.ContentsEqual(keyId,1)
If (stringMatch18 <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Import parameters belong to another key.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("ImportToken"))
stringMatch19 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("ImportToken") <> 1) Or (stringMatch19 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid ImportToken.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("PublicKey"))
stringMatch20 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("PublicKey") <> 1) Or (stringMatch20 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid PublicKey.") & "</pre>"
    Response.End
End If

If (result.JsonTypeOf("ParametersValidTo") <> 2) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing import-parameter expiration.") & "</pre>"
    Response.End
End If

set now = Server.CreateObject("Chilkat.CkDateTime")
set expires = Server.CreateObject("Chilkat.CkDateTime")
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read current time.") & "</pre>"
    Response.End
End If

success = result.DateOf("ParametersValidTo",expires)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Invalid import-parameter expiration.") & "</pre>"
    Response.End
End If

If (expires.DiffSeconds(now) <= 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Import parameters have expired; obtain a fresh token and wrapping key together.") & "</pre>"
    Response.End
End If

importToken = result.StringOf("ImportToken")
set wrappingKey = Server.CreateObject("Chilkat.PublicKey")
success = wrappingKey.LoadBase64(result.StringOf("PublicKey"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load the RSA wrapping key.") & "</pre>"
    Response.End
End If

If (wrappingKey.KeySize <> 4096) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected the requested 4096-bit RSA wrapping key.") & "</pre>"
    Response.End
End If

'  Export PKCS#8 DER in memory only; never print or write unencrypted key material.
pkcs8Base64 = privKey.GetPkcs8ENC("base64")
If (privKey.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not encode the private key as PKCS#8.") & "</pre>"
    Response.End
End If

set prng = Server.CreateObject("Chilkat.Prng")
'  A fresh 32-byte (256-bit) AES key, not a 32-bit key.
aesKeyBase64 = prng.GenRandom(32,"base64")
If (prng.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not generate wrapping key.") & "</pre>"
    Response.End
End If

set crypt = Server.CreateObject("Chilkat.Crypt2")
wrappedPrivateKey = crypt.AesKeyWrapWithPadding(aesKeyBase64,pkcs8Base64,"base64")
If (crypt.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "AES key wrapping failed.") & "</pre>"
    Response.End
End If

set wrappedAesKey = Server.CreateObject("Chilkat.BinData")
success = wrappedAesKey.AppendEncoded(aesKeyBase64,"base64")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not decode AES key.") & "</pre>"
    Response.End
End If

set rsa = Server.CreateObject("Chilkat.Rsa")
success = rsa.UsePublicKey(wrappingKey)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not select the RSA wrapping key.") & "</pre>"
    Response.End
End If

rsa.PkcsPadding = 0
rsa.OaepHash = "sha256"
rsa.OaepMgfHash = "sha256"
success = rsa.EncryptBd(wrappedAesKey,0)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "RSA-OAEP AES-key wrapping failed.") & "</pre>"
    Response.End
End If

'  Wire order is RSA-wrapped AES key FIRST, followed by AES-wrapped PKCS#8.
set encryptedMaterial = Server.CreateObject("Chilkat.BinData")
success = encryptedMaterial.AppendBd(wrappedAesKey)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not assemble encrypted material.") & "</pre>"
    Response.End
End If

success = encryptedMaterial.AppendEncoded(wrappedPrivateKey,"base64")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not append wrapped private key.") & "</pre>"
    Response.End
End If

body.Clear 
success = body.UpdateString("KeyId",keyId)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set KeyId.") & "</pre>"
    Response.End
End If

success = body.UpdateString("ImportToken",importToken)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set ImportToken.") & "</pre>"
    Response.End
End If

success = body.UpdateString("EncryptedKeyMaterial",encryptedMaterial.GetEncoded("base64"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set EncryptedKeyMaterial.") & "</pre>"
    Response.End
End If

success = body.UpdateString("ExpirationModel","KEY_MATERIAL_DOES_NOT_EXPIRE")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set ExpirationModel.") & "</pre>"
    Response.End
End If

success = rest.ClearAllQueryParams()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not clear previous query parameters.") & "</pre>"
    Response.End
End If

'  Keep the JSON operation body separate from authentication parameters.
'  Include STS credentials in the signed query; never log the request URL.
If (credentials.HasMember("session_token") = 1) Then
    success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Could not set session token.") & "</pre>"
        Response.End
    End If

End If

success = rest.AddHeader("X-Amz-Target","TrentService.ImportKeyMaterial")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set operation.") & "</pre>"
    Response.End
End If

success = rest.AddHeader("Content-Type","application/x-amz-json-1.1")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set content type.") & "</pre>"
    Response.End
End If

responseJson = rest.FullRequestString("POST","/",body.Emit())
If (rest.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.") & "</pre>"
    Response.End
End If

If (rest.ResponseStatusCode <> 200) Then
    Response.Write "<pre>" & Server.HTMLEncode( "KMS HTTP status: " & rest.ResponseStatusCode) & "</pre>"
    success = result.Load(responseJson)
    If (success = 1) Then
        If (result.JsonTypeOf("__type") = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "AWS error code: " & result.StringOf("__type")) & "</pre>"
        End If

    End If

    Response.Write "<pre>" & Server.HTMLEncode( "AWS request ID: " & rest.ResponseHdrByName("x-amzn-RequestId")) & "</pre>"
    '  Do not print full bodies, headers, credentials, key material, or tokens.
    Response.End
End If

'  HTTP 200 indicates acceptance; response content can be empty.
Response.Write "<pre>" & Server.HTMLEncode( "Key material import accepted for: " & keyId) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Use DescribeKey to confirm Enabled before signing; KMS is eventually consistent.") & "</pre>"
'  This does not securely erase copies of sensitive strings from managed memory.

%>
</body>
</html>