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

AWS KMS: Derive an ECDH Secret and Apply HKDF-SHA256

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.

Demonstrates both peers in one process using a NIST P-256 KEY_AGREEMENT KMS key and a fresh local EC private key. Retrieves the KMS public key through authenticated KMS and compares both shared secrets. For a remote peer, validate/authenticate its public key and bind identities and protocol transcript into KDF context. HKDF extract/expand derives one 32-byte key; communicate the public salt and context to the other party. No secret is printed. This is not a complete authenticated exchange protocol.

API actions used: kms:DescribeKey, kms:DeriveSharedSecret, kms:GetPublicKey. 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 | DeriveSharedSecret | GetPublicKey

Chilkat DataFlex Downloads

DataFlex
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
    Boolean iStringMatch4
    String sKeyId
    Boolean iStringMatch5
    Boolean iStringMatch6
    Boolean iStringMatch7
    Boolean iStringMatch8
    Boolean iStringMatch9
    Boolean iStringMatch10
    Boolean iStringMatch11
    Boolean iStringMatch12
    Handle hoEcc
    Variant vPrng
    Handle hoPrng
    Variant vPeerPrivate
    Handle hoPeerPrivate
    Variant vPeerPublic
    Handle hoPeerPublic
    Variant vPeerDer
    Handle hoPeerDer
    Boolean iStringMatch13
    Boolean iStringMatch14
    Boolean iStringMatch15
    Boolean iStringMatch16
    Boolean iStringMatch17
    Variant vShared
    Handle hoShared
    Boolean iStringMatch18
    Boolean iStringMatch19
    Boolean iStringMatch20
    Boolean iStringMatch21
    Boolean iStringMatch22
    Boolean iStringMatch23
    Variant vKmsPublic
    Handle hoKmsPublic
    String sPeerSecret
    Variant vLocalShared
    Handle hoLocalShared
    String sSalt
    Handle hoHkdf
    String sPrk
    Variant vInfo
    Handle hoInfo
    String sDerivedKey
    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

    Get ComStringOf Of hoCredentials "key_id" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch4
    Get ComJsonTypeOf Of hoCredentials "key_id" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch4 = True)) Begin
        Showln "Missing or invalid key_id."
        Procedure_Return
    End

    Get ComStringOf Of hoCredentials "key_id" 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 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.Arn" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch5
    Get ComJsonTypeOf Of hoResult "KeyMetadata.Arn" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch5 = True)) Begin
        Showln "Missing or invalid KeyMetadata.Arn."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyMetadata.KeyId" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch6
    Get ComJsonTypeOf Of hoResult "KeyMetadata.KeyId" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch6 = True)) Begin
        Showln "Missing or invalid KeyMetadata.KeyId."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyMetadata.KeySpec" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch7
    Get ComJsonTypeOf Of hoResult "KeyMetadata.KeySpec" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch7 = True)) Begin
        Showln "Missing or invalid KeyMetadata.KeySpec."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyMetadata.KeySpec" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "ECC_NIST_P256" True To iStringMatch8
    If (iStringMatch8 <> True) Begin
        Showln "Unexpected KeyMetadata.KeySpec."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyMetadata.KeyUsage" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch9
    Get ComJsonTypeOf Of hoResult "KeyMetadata.KeyUsage" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch9 = True)) Begin
        Showln "Missing or invalid KeyMetadata.KeyUsage."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyMetadata.KeyUsage" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "KEY_AGREEMENT" True To iStringMatch10
    If (iStringMatch10 <> True) Begin
        Showln "Unexpected KeyMetadata.KeyUsage."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyMetadata.KeyState" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch11
    Get ComJsonTypeOf Of hoResult "KeyMetadata.KeyState" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch11 = True)) Begin
        Showln "Missing or invalid KeyMetadata.KeyState."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyMetadata.KeyState" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "Enabled" True To iStringMatch12
    If (iStringMatch12 <> True) Begin
        Showln "Unexpected KeyMetadata.KeyState."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyMetadata.Arn" To sKeyId
    //  This example controls the peer key locally; authenticate remote peer keys separately.
    Get Create (RefClass(cComChilkatEcc)) To hoEcc
    If (Not(IsComObjectCreated(hoEcc))) Begin
        Send CreateComObject of hoEcc
    End
    Get Create (RefClass(cComChilkatPrng)) To hoPrng
    If (Not(IsComObjectCreated(hoPrng))) Begin
        Send CreateComObject of hoPrng
    End
    Get Create (RefClass(cComChilkatPrivateKey)) To hoPeerPrivate
    If (Not(IsComObjectCreated(hoPeerPrivate))) Begin
        Send CreateComObject of hoPeerPrivate
    End
    Get Create (RefClass(cComChilkatPublicKey)) To hoPeerPublic
    If (Not(IsComObjectCreated(hoPeerPublic))) Begin
        Send CreateComObject of hoPeerPublic
    End
    Get Create (RefClass(cComChilkatBinData)) To hoPeerDer
    If (Not(IsComObjectCreated(hoPeerDer))) Begin
        Send CreateComObject of hoPeerDer
    End
    Get pvComObject of hoPrng to vPrng
    Get pvComObject of hoPeerPrivate to vPeerPrivate
    Get ComGenKey Of hoEcc "secp256r1" vPrng vPeerPrivate To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not generate peer key."
        Procedure_Return
    End

    Get pvComObject of hoPeerPublic to vPeerPublic
    Get ComToPublicKey Of hoPeerPrivate vPeerPublic To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not get peer public key."
        Procedure_Return
    End

    Get pvComObject of hoPeerDer to vPeerDer
    Get ComGetDerBd Of hoPeerPublic False vPeerDer To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not encode SPKI peer 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 "KeyAgreementAlgorithm" "ECDH" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set KeyAgreementAlgorithm."
        Procedure_Return
    End

    Get ComGetEncoded Of hoPeerDer "base64" To sTemp1
    Get ComUpdateString Of hoBody "PublicKey" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set PublicKey."
        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.DeriveSharedSecret" 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 iStringMatch13
    Get ComJsonTypeOf Of hoResult "KeyId" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch13 = 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 iStringMatch14
    If (iStringMatch14 <> True) Begin
        Showln "Response belongs to another key; use a fixed key ARN in configuration."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyAgreementAlgorithm" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch15
    Get ComJsonTypeOf Of hoResult "KeyAgreementAlgorithm" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch15 = True)) Begin
        Showln "Missing or invalid KeyAgreementAlgorithm."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyAgreementAlgorithm" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "ECDH" True To iStringMatch16
    If (iStringMatch16 <> True) Begin
        Showln "Unexpected KeyAgreementAlgorithm."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "SharedSecret" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch17
    Get ComJsonTypeOf Of hoResult "SharedSecret" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch17 = True)) Begin
        Showln "Missing or invalid SharedSecret."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoShared
    If (Not(IsComObjectCreated(hoShared))) Begin
        Send CreateComObject of hoShared
    End
    Get ComStringOf Of hoResult "SharedSecret" To sTemp1
    Get ComAppendEncoded Of hoShared sTemp1 "base64" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Invalid base64 SharedSecret."
        Procedure_Return
    End

    Get ComNumBytes Of hoShared To iTemp1
    If (iTemp1 <> 32) Begin
        Showln "Unexpected byte length for SharedSecret."
        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 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.GetPublicKey" 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 iStringMatch18
    Get ComJsonTypeOf Of hoResult "KeyId" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch18 = 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 iStringMatch19
    If (iStringMatch19 <> True) Begin
        Showln "Response belongs to another key; use a fixed key ARN in configuration."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeySpec" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch20
    Get ComJsonTypeOf Of hoResult "KeySpec" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch20 = True)) Begin
        Showln "Missing or invalid KeySpec."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeySpec" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "ECC_NIST_P256" True To iStringMatch21
    If (iStringMatch21 <> True) Begin
        Showln "Unexpected KeySpec."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyUsage" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "" True To iStringMatch22
    Get ComJsonTypeOf Of hoResult "KeyUsage" To iTemp1
    If ((iTemp1 <> 1) Or (iStringMatch22 = True)) Begin
        Showln "Missing or invalid KeyUsage."
        Procedure_Return
    End

    Get ComStringOf Of hoResult "KeyUsage" To sTemp1
    Get ComSetString Of hoStringCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoStringCompare "KEY_AGREEMENT" True To iStringMatch23
    If (iStringMatch23 <> True) Begin
        Showln "Unexpected KeyUsage."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPublicKey)) To hoKmsPublic
    If (Not(IsComObjectCreated(hoKmsPublic))) Begin
        Send CreateComObject of hoKmsPublic
    End
    Get ComStringOf Of hoResult "PublicKey" To sTemp1
    Get ComLoadBase64 Of hoKmsPublic sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load KMS public key."
        Procedure_Return
    End

    Get pvComObject of hoPeerPrivate to vPeerPrivate
    Get pvComObject of hoKmsPublic to vKmsPublic
    Get ComSharedSecretENC Of hoEcc vPeerPrivate vKmsPublic "base64" To sPeerSecret
    Get ComLastMethodSuccess Of hoEcc To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Local ECDH failed."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoLocalShared
    If (Not(IsComObjectCreated(hoLocalShared))) Begin
        Send CreateComObject of hoLocalShared
    End
    Get ComAppendEncoded Of hoLocalShared sPeerSecret "base64" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not decode local shared secret."
        Procedure_Return
    End

    Get ComContentsEqual Of hoShared     Get pvComObject of hoLocalShared to vLocalShared
vLocalShared To bTemp1
    If (bTemp1 <> True) Begin
        Showln "ECDH secrets did not match."
        Procedure_Return
    End

    //  RFC 5869 HKDF-SHA256, one 32-byte output block. Salt is public and random.
    Get ComGenRandom Of hoPrng 32 "base64" To sSalt
    Get ComLastMethodSuccess Of hoPrng To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Could not generate salt."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCrypt2)) To hoHkdf
    If (Not(IsComObjectCreated(hoHkdf))) Begin
        Send CreateComObject of hoHkdf
    End
    Set ComMacAlgorithm Of hoHkdf To "hmac"
    Set ComHashAlgorithm Of hoHkdf To "sha256"
    Set ComEncodingMode Of hoHkdf To "base64"
    Get ComSetMacKeyEncoded Of hoHkdf sSalt "base64" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set HKDF salt."
        Procedure_Return
    End

    Get pvComObject of hoShared to vShared
    Get ComMacBdENC Of hoHkdf vShared To sPrk
    Get ComLastMethodSuccess Of hoHkdf To bTemp1
    If (bTemp1 <> True) Begin
        Showln "HKDF extract failed."
        Procedure_Return
    End

    Get ComSetMacKeyEncoded Of hoHkdf sPrk "base64" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set HKDF PRK."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoInfo
    If (Not(IsComObjectCreated(hoInfo))) Begin
        Send CreateComObject of hoInfo
    End
    Get ComAppendString Of hoInfo "chilkat-kms-ecdh-v1" "utf-8" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not set KDF context."
        Procedure_Return
    End

    Get ComAppendEncoded Of hoInfo "01" "hex" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not append HKDF block counter."
        Procedure_Return
    End

    Get pvComObject of hoInfo to vInfo
    Get ComMacBdENC Of hoHkdf vInfo To sDerivedKey
    Get ComLastMethodSuccess Of hoHkdf To bTemp1
    If (bTemp1 <> True) Begin
        Showln "HKDF expand failed."
        Procedure_Return
    End

    Get ComSecureClear Of hoShared To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not clear shared secret buffer."
        Procedure_Return
    End

    Get ComSecureClear Of hoLocalShared To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not clear local secret buffer."
        Procedure_Return
    End

    Showln "ECDH secrets matched and a 32-byte application key was derived."


End_Procedure