Sample code for 30+ languages & platforms
Delphi ActiveX 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 Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
stringCompare: TChilkatStringBuilder;
credentials: TChilkatJsonObject;
stringMatch1: Integer;
stringMatch2: Integer;
region: WideString;
endpoint: TChilkatStringBuilder;
aws: TChilkatAuthAws;
rest: TChilkatRest;
stringMatch3: Integer;
connection: TChilkatSocket;
body: TChilkatJsonObject;
result: TChilkatJsonObject;
responseJson: WideString;
cert: TChilkatCert;
privKey: TPrivateKey;
sourcePublic: TPublicKey;
publicJwk: WideString;
keyInfo: TChilkatJsonObject;
keySpec: WideString;
stringMatch4: Integer;
stringMatch5: Integer;
stringMatch6: Integer;
stringMatch7: Integer;
stringMatch8: Integer;
stringMatch9: Integer;
stringMatch10: Integer;
keyId: WideString;
stringMatch11: Integer;
stringMatch12: Integer;
stringMatch13: Integer;
stringMatch14: Integer;
stringMatch15: Integer;
stringMatch16: Integer;
stringMatch17: Integer;
stringMatch18: Integer;
stringMatch19: Integer;
stringMatch20: Integer;
now: TCkDateTime;
expires: TCkDateTime;
importToken: WideString;
wrappingKey: TPublicKey;
pkcs8Base64: WideString;
prng: TChilkatPrng;
aesKeyBase64: WideString;
crypt: TChilkatCrypt2;
wrappedPrivateKey: WideString;
wrappedAesKey: TChilkatBinData;
rsa: TChilkatRsa;
encryptedMaterial: TChilkatBinData;

begin
success := 0;

stringCompare := TChilkatStringBuilder.Create(Self);
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this credentials file outside public web content and source control.
credentials := TChilkatJsonObject.Create(Self);
credentials.Strict := 1;
success := credentials.LoadFile('qa_data/aws/kms.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not load private KMS configuration.');
    Exit;
  end;
stringCompare.SetString(credentials.StringOf('access_key'));
stringMatch1 := stringCompare.ContentsEqual('',1);
if ((credentials.JsonTypeOf('access_key') <> 1) or (stringMatch1 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid access_key.');
    Exit;
  end;
stringCompare.SetString(credentials.StringOf('secret_key'));
stringMatch2 := stringCompare.ContentsEqual('',1);
if ((credentials.JsonTypeOf('secret_key') <> 1) or (stringMatch2 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid secret_key.');
    Exit;
  end;
region := 'us-west-2';
endpoint := TChilkatStringBuilder.Create(Self);
endpoint.Append('kms.');
endpoint.Append(region);
endpoint.Append('.amazonaws.com');
aws := TChilkatAuthAws.Create(Self);
aws.AccessKey := credentials.StringOf('access_key');
aws.SecretKey := credentials.StringOf('secret_key');
aws.Region := region;
aws.ServiceName := 'kms';
aws.SignatureVersion := 4;
rest := TChilkatRest.Create(Self);
rest.ConnectTimeoutMs := 20000;
rest.IdleTimeoutMs := 60000;
rest.VerboseLogging := 0;
success := rest.SetAuthAws(aws.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not configure AWS signing.');
    Exit;
  end;
if (credentials.HasMember('session_token') = 1) then
  begin
    stringCompare.SetString(credentials.StringOf('session_token'));
    stringMatch3 := stringCompare.ContentsEqual('',1);
    if ((credentials.JsonTypeOf('session_token') <> 1) or (stringMatch3 = 1)) then
      begin
        Memo1.Lines.Add('Missing or invalid session_token.');
        Exit;
      end;
  end;
connection := TChilkatSocket.Create(Self);
connection.RequireSslCertVerify := 1;
connection.MaxReadIdleMs := 60000;
connection.MaxSendIdleMs := 60000;
success := connection.Connect(endpoint.GetAsString(),443,1,20000);
if (success <> 1) then
  begin
    Memo1.Lines.Add('TLS connection failed; inspect diagnostics privately.');
    Exit;
  end;
success := rest.UseConnection(connection.ControlInterface,1);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not attach TLS connection.');
    Exit;
  end;
//  Keep connection alive while Rest uses it.
body := TChilkatJsonObject.Create(Self);
result := TChilkatJsonObject.Create(Self);
result.Strict := 1;

//  pfx_password is a string; an empty string is allowed for an unprotected PFX.
if (credentials.JsonTypeOf('pfx_password') <> 1) then
  begin
    Memo1.Lines.Add('Missing or invalid pfx_password.');
    Exit;
  end;
cert := TChilkatCert.Create(Self);
success := cert.LoadPfxFile('qa_data/pfx/myCertAndKey.pfx',credentials.StringOf('pfx_password'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not load PFX; inspect diagnostics privately.');
    Exit;
  end;
privKey := TPrivateKey.Create(Self);
success := cert.GetPrivateKey(privKey.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('PFX does not contain a usable private key.');
    Exit;
  end;
//  Only inspect the PUBLIC JWK to identify a supported key type and curve.
sourcePublic := TPublicKey.Create(Self);
success := privKey.ToPublicKey(sourcePublic.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not get the public key.');
    Exit;
  end;
publicJwk := sourcePublic.GetJwk();
if (sourcePublic.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not inspect public key type.');
    Exit;
  end;
keyInfo := TChilkatJsonObject.Create(Self);
keyInfo.Strict := 1;
success := keyInfo.Load(publicJwk);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not parse public key description.');
    Exit;
  end;
keySpec := '';
stringCompare.SetString(keyInfo.StringOf('kty'));
stringMatch4 := stringCompare.ContentsEqual('RSA',1);
if (stringMatch4 = 1) then
  begin
    if (privKey.BitLength = 2048) then
      begin
        keySpec := 'RSA_2048';
      end;
    if (privKey.BitLength = 3072) then
      begin
        keySpec := 'RSA_3072';
      end;
    if (privKey.BitLength = 4096) then
      begin
        keySpec := 'RSA_4096';
      end;
  end;
stringCompare.SetString(keyInfo.StringOf('kty'));
stringMatch5 := stringCompare.ContentsEqual('EC',1);
if (stringMatch5 = 1) then
  begin
    stringCompare.SetString(keyInfo.StringOf('crv'));
    stringMatch6 := stringCompare.ContentsEqual('P-256',1);
    if (stringMatch6 = 1) then
      begin
        keySpec := 'ECC_NIST_P256';
      end;
    stringCompare.SetString(keyInfo.StringOf('crv'));
    stringMatch7 := stringCompare.ContentsEqual('P-384',1);
    if (stringMatch7 = 1) then
      begin
        keySpec := 'ECC_NIST_P384';
      end;
    stringCompare.SetString(keyInfo.StringOf('crv'));
    stringMatch8 := stringCompare.ContentsEqual('P-521',1);
    if (stringMatch8 = 1) then
      begin
        keySpec := 'ECC_NIST_P521';
      end;
  end;
stringCompare.SetString(keySpec);
stringMatch9 := stringCompare.ContentsEqual('',1);
if (stringMatch9 = 1) then
  begin
    Memo1.Lines.Add('Use RSA 2048/3072/4096 or a NIST P-256/P-384/P-521 EC private key.');
    Exit;
  end;
stringCompare.SetString(credentials.StringOf('key_id'));
stringMatch10 := stringCompare.ContentsEqual('',1);
if ((credentials.JsonTypeOf('key_id') <> 1) or (stringMatch10 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid key_id.');
    Exit;
  end;
keyId := credentials.StringOf('key_id');
success := body.UpdateString('KeyId',keyId);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set KeyId.');
    Exit;
  end;
success := rest.ClearAllQueryParams();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not clear previous query parameters.');
    Exit;
  end;
//  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
  begin
    success := rest.AddQueryParam('X-Amz-Security-Token',credentials.StringOf('session_token'));
    if (success <> 1) then
      begin
        Memo1.Lines.Add('Could not set session token.');
        Exit;
      end;
  end;
success := rest.AddHeader('X-Amz-Target','TrentService.DescribeKey');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set operation.');
    Exit;
  end;
success := rest.AddHeader('Content-Type','application/x-amz-json-1.1');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set content type.');
    Exit;
  end;
responseJson := rest.FullRequestString('POST','/',body.Emit());
if (rest.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.');
    Exit;
  end;
if (rest.ResponseStatusCode <> 200) then
  begin
    Memo1.Lines.Add('KMS HTTP status: ' + IntToStr(rest.ResponseStatusCode));
    success := result.Load(responseJson);
    if (success = 1) then
      begin
        if (result.JsonTypeOf('__type') = 1) then
          begin
            Memo1.Lines.Add('AWS error code: ' + result.StringOf('__type'));
          end;
      end;
    Memo1.Lines.Add('AWS request ID: ' + rest.ResponseHdrByName('x-amzn-RequestId'));
    //  Do not print full bodies, headers, credentials, key material, or tokens.
    Exit;
  end;
success := result.Load(responseJson);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected a well-formed JSON response.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('KeyMetadata.KeyId'));
stringMatch11 := stringCompare.ContentsEqual('',1);
if ((result.JsonTypeOf('KeyMetadata.KeyId') <> 1) or (stringMatch11 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid KeyMetadata.KeyId.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('KeyMetadata.Arn'));
stringMatch12 := stringCompare.ContentsEqual('',1);
if ((result.JsonTypeOf('KeyMetadata.Arn') <> 1) or (stringMatch12 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid KeyMetadata.Arn.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('KeyMetadata.Origin'));
stringMatch13 := stringCompare.ContentsEqual('EXTERNAL',1);
if (stringMatch13 <> 1) then
  begin
    Memo1.Lines.Add('Destination must have EXTERNAL origin.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('KeyMetadata.KeyState'));
stringMatch14 := stringCompare.ContentsEqual('PendingImport',1);
if (stringMatch14 <> 1) then
  begin
    Memo1.Lines.Add('This initial-import sample requires PendingImport; it does not replace existing material.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('KeyMetadata.KeyUsage'));
stringMatch15 := stringCompare.ContentsEqual('SIGN_VERIFY',1);
if (stringMatch15 <> 1) then
  begin
    Memo1.Lines.Add('Destination must have SIGN_VERIFY usage.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('KeyMetadata.KeySpec'));
stringMatch16 := stringCompare.ContentsEqual(keySpec,1);
if (stringMatch16 <> 1) then
  begin
    Memo1.Lines.Add('PFX private key does not match the destination KeySpec.');
    Exit;
  end;
//  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
  begin
    Memo1.Lines.Add('Could not set KeyId.');
    Exit;
  end;
success := body.UpdateString('WrappingAlgorithm','RSA_AES_KEY_WRAP_SHA_256');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set WrappingAlgorithm.');
    Exit;
  end;
success := body.UpdateString('WrappingKeySpec','RSA_4096');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set WrappingKeySpec.');
    Exit;
  end;
success := rest.ClearAllQueryParams();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not clear previous query parameters.');
    Exit;
  end;
//  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
  begin
    success := rest.AddQueryParam('X-Amz-Security-Token',credentials.StringOf('session_token'));
    if (success <> 1) then
      begin
        Memo1.Lines.Add('Could not set session token.');
        Exit;
      end;
  end;
success := rest.AddHeader('X-Amz-Target','TrentService.GetParametersForImport');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set operation.');
    Exit;
  end;
success := rest.AddHeader('Content-Type','application/x-amz-json-1.1');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set content type.');
    Exit;
  end;
responseJson := rest.FullRequestString('POST','/',body.Emit());
if (rest.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.');
    Exit;
  end;
if (rest.ResponseStatusCode <> 200) then
  begin
    Memo1.Lines.Add('KMS HTTP status: ' + IntToStr(rest.ResponseStatusCode));
    success := result.Load(responseJson);
    if (success = 1) then
      begin
        if (result.JsonTypeOf('__type') = 1) then
          begin
            Memo1.Lines.Add('AWS error code: ' + result.StringOf('__type'));
          end;
      end;
    Memo1.Lines.Add('AWS request ID: ' + rest.ResponseHdrByName('x-amzn-RequestId'));
    //  Do not print full bodies, headers, credentials, key material, or tokens.
    Exit;
  end;
success := result.Load(responseJson);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected a well-formed JSON response.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('KeyId'));
stringMatch17 := stringCompare.ContentsEqual('',1);
if ((result.JsonTypeOf('KeyId') <> 1) or (stringMatch17 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid KeyId.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('KeyId'));
stringMatch18 := stringCompare.ContentsEqual(keyId,1);
if (stringMatch18 <> 1) then
  begin
    Memo1.Lines.Add('Import parameters belong to another key.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('ImportToken'));
stringMatch19 := stringCompare.ContentsEqual('',1);
if ((result.JsonTypeOf('ImportToken') <> 1) or (stringMatch19 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid ImportToken.');
    Exit;
  end;
stringCompare.SetString(result.StringOf('PublicKey'));
stringMatch20 := stringCompare.ContentsEqual('',1);
if ((result.JsonTypeOf('PublicKey') <> 1) or (stringMatch20 = 1)) then
  begin
    Memo1.Lines.Add('Missing or invalid PublicKey.');
    Exit;
  end;
if (result.JsonTypeOf('ParametersValidTo') <> 2) then
  begin
    Memo1.Lines.Add('Missing import-parameter expiration.');
    Exit;
  end;
now := TCkDateTime.Create(Self);
expires := TCkDateTime.Create(Self);
success := now.SetFromCurrentSystemTime();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not read current time.');
    Exit;
  end;
success := result.DateOf('ParametersValidTo',expires.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Invalid import-parameter expiration.');
    Exit;
  end;
if (expires.DiffSeconds(now.ControlInterface) <= 0) then
  begin
    Memo1.Lines.Add('Import parameters have expired; obtain a fresh token and wrapping key together.');
    Exit;
  end;
importToken := result.StringOf('ImportToken');
wrappingKey := TPublicKey.Create(Self);
success := wrappingKey.LoadBase64(result.StringOf('PublicKey'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not load the RSA wrapping key.');
    Exit;
  end;
if (wrappingKey.KeySize <> 4096) then
  begin
    Memo1.Lines.Add('Expected the requested 4096-bit RSA wrapping key.');
    Exit;
  end;
//  Export PKCS#8 DER in memory only; never print or write unencrypted key material.
pkcs8Base64 := privKey.GetPkcs8ENC('base64');
if (privKey.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not encode the private key as PKCS#8.');
    Exit;
  end;
prng := TChilkatPrng.Create(Self);
//  A fresh 32-byte (256-bit) AES key, not a 32-bit key.
aesKeyBase64 := prng.GenRandom(32,'base64');
if (prng.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Could not generate wrapping key.');
    Exit;
  end;
crypt := TChilkatCrypt2.Create(Self);
wrappedPrivateKey := crypt.AesKeyWrapWithPadding(aesKeyBase64,pkcs8Base64,'base64');
if (crypt.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('AES key wrapping failed.');
    Exit;
  end;
wrappedAesKey := TChilkatBinData.Create(Self);
success := wrappedAesKey.AppendEncoded(aesKeyBase64,'base64');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not decode AES key.');
    Exit;
  end;
rsa := TChilkatRsa.Create(Self);
success := rsa.UsePublicKey(wrappingKey.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not select the RSA wrapping key.');
    Exit;
  end;
rsa.PkcsPadding := 0;
rsa.OaepHash := 'sha256';
rsa.OaepMgfHash := 'sha256';
success := rsa.EncryptBd(wrappedAesKey.ControlInterface,0);
if (success <> 1) then
  begin
    Memo1.Lines.Add('RSA-OAEP AES-key wrapping failed.');
    Exit;
  end;
//  Wire order is RSA-wrapped AES key FIRST, followed by AES-wrapped PKCS#8.
encryptedMaterial := TChilkatBinData.Create(Self);
success := encryptedMaterial.AppendBd(wrappedAesKey.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not assemble encrypted material.');
    Exit;
  end;
success := encryptedMaterial.AppendEncoded(wrappedPrivateKey,'base64');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not append wrapped private key.');
    Exit;
  end;
body.Clear();
success := body.UpdateString('KeyId',keyId);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set KeyId.');
    Exit;
  end;
success := body.UpdateString('ImportToken',importToken);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set ImportToken.');
    Exit;
  end;
success := body.UpdateString('EncryptedKeyMaterial',encryptedMaterial.GetEncoded('base64'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set EncryptedKeyMaterial.');
    Exit;
  end;
success := body.UpdateString('ExpirationModel','KEY_MATERIAL_DOES_NOT_EXPIRE');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set ExpirationModel.');
    Exit;
  end;
success := rest.ClearAllQueryParams();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not clear previous query parameters.');
    Exit;
  end;
//  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
  begin
    success := rest.AddQueryParam('X-Amz-Security-Token',credentials.StringOf('session_token'));
    if (success <> 1) then
      begin
        Memo1.Lines.Add('Could not set session token.');
        Exit;
      end;
  end;
success := rest.AddHeader('X-Amz-Target','TrentService.ImportKeyMaterial');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set operation.');
    Exit;
  end;
success := rest.AddHeader('Content-Type','application/x-amz-json-1.1');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not set content type.');
    Exit;
  end;
responseJson := rest.FullRequestString('POST','/',body.Emit());
if (rest.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.');
    Exit;
  end;
if (rest.ResponseStatusCode <> 200) then
  begin
    Memo1.Lines.Add('KMS HTTP status: ' + IntToStr(rest.ResponseStatusCode));
    success := result.Load(responseJson);
    if (success = 1) then
      begin
        if (result.JsonTypeOf('__type') = 1) then
          begin
            Memo1.Lines.Add('AWS error code: ' + result.StringOf('__type'));
          end;
      end;
    Memo1.Lines.Add('AWS request ID: ' + rest.ResponseHdrByName('x-amzn-RequestId'));
    //  Do not print full bodies, headers, credentials, key material, or tokens.
    Exit;
  end;
//  HTTP 200 indicates acceptance; response content can be empty.
Memo1.Lines.Add('Key material import accepted for: ' + keyId);
Memo1.Lines.Add('Use DescribeKey to confirm Enabled before signing; KMS is eventually consistent.');
//  This does not securely erase copies of sensitive strings from managed memory.