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

AWS KMS: Sign a SOAP Body with XMLDSig and SHA-256

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.

This convenience-method example configures an access-key/secret-key pair. It explicitly rejects a supplied session_token instead of silently omitting it: token support for this helper configuration has not been established here. Prefer temporary role credentials in production using a documented token-capable integration. The separate Rest examples support a session token. No credentials or private key material are printed.

Load a local public certificate whose public key matches an enabled asymmetric KMS key with KeyUsage=SIGN_VERIFY. These certificate signing examples target RSA or NIST EC signing keys; they do not demonstrate every KMS algorithm (such as ML-DSA). Set the nonempty string key_id in the private configuration to the actual key ID or ARN in the configured region. A key ARN avoids alias retargeting. The caller needs kms:Sign and access through the key policy. KMS holds the private key and signs the digest; Chilkat creates the document locally. This does not upload the whole document to KMS. A valid signature alone does not establish certificate trust, revocation status, or a trusted signing time.

Signs the SOAP Body by its ID using exclusive canonicalization and SHA-256 for both the reference digest and SignedInfo signature. Embeds the public certificate and verifies the signature and reference before saving. This is XMLDSig, not XAdES: no XAdES qualifying properties are created. Adapt WS-Security namespaces and profile requirements to the receiving service; the unsigned SOAP headers are not covered by this Body-only signature.

AWS Sign reference

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

$oStringCompare = ObjCreate("Chilkat.StringBuilder")
;  Assumes Chilkat is unlocked. See Global Unlock Sample.
;  Keep this credentials file outside public web content and source control.
$oCredentials = ObjCreate("Chilkat.JsonObject")
$oCredentials.Strict = True
$bSuccess = $oCredentials.LoadFile("qa_data/aws/kms.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load private KMS configuration." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oCredentials.StringOf("access_key"))
Local $bStringMatch1 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("access_key") <> 1) Or ($bStringMatch1 = True)) Then
    ConsoleWrite("Missing or invalid access_key." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oCredentials.StringOf("secret_key"))
Local $bStringMatch2 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("secret_key") <> 1) Or ($bStringMatch2 = True)) Then
    ConsoleWrite("Missing or invalid secret_key." & @CRLF)
    Exit
EndIf

Local $sRegion = "us-west-2"
;  These convenience-method examples use an access-key/secret-key pair.
;  Do not silently discard the token belonging to temporary credentials.
If ($oCredentials.HasMember("session_token") = True) Then
    ConsoleWrite("This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oCredentials.StringOf("key_id"))
Local $bStringMatch3 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("key_id") <> 1) Or ($bStringMatch3 = True)) Then
    ConsoleWrite("Missing or invalid key_id." & @CRLF)
    Exit
EndIf

$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert.LoadFromFile("qa_data/certs/myCert.cer")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load the public signing certificate." & @CRLF)
    Exit
EndIf

$oCloud = ObjCreate("Chilkat.JsonObject")
;  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
$bSuccess = $oCloud.UpdateString("service","aws_kms")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set service." & @CRLF)
    Exit
EndIf

$bSuccess = $oCloud.UpdateString("access_key",$oCredentials.StringOf("access_key"))
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set access_key." & @CRLF)
    Exit
EndIf

$bSuccess = $oCloud.UpdateString("secret_key",$oCredentials.StringOf("secret_key"))
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set secret_key." & @CRLF)
    Exit
EndIf

$bSuccess = $oCloud.UpdateString("region",$sRegion)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set region." & @CRLF)
    Exit
EndIf

$bSuccess = $oCloud.UpdateString("key_id",$oCredentials.StringOf("key_id"))
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set key_id." & @CRLF)
    Exit
EndIf

$bSuccess = $oCert.SetCloudSigner($oCloud)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not configure the AWS KMS signer; inspect diagnostics privately." & @CRLF)
    Exit
EndIf

;  Create the XML to be signed.
$oSbXml = ObjCreate("Chilkat.StringBuilder")

Local $bCrlf = True
$oSbXml.AppendLine("<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no"" ?>",$bCrlf)
$oSbXml.AppendLine("<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"">",$bCrlf)
$oSbXml.AppendLine("    <SOAP-ENV:Header>",$bCrlf)
$oSbXml.AppendLine("        <wsse:Security xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" SOAP-ENV:mustUnderstand=""1""></wsse:Security>",$bCrlf)
$oSbXml.AppendLine("    </SOAP-ENV:Header>",$bCrlf)
$oSbXml.AppendLine("    <SOAP-ENV:Body xmlns:SOAP-SEC=""http://schemas.xmlsoap.org/soap/security/2000-12"" SOAP-SEC:id=""Body"">",$bCrlf)
$oSbXml.AppendLine("        <z:FooBar xmlns:z=""http://example.com"" />",$bCrlf)
$oSbXml.AppendLine("    </SOAP-ENV:Body>",$bCrlf)
$oSbXml.AppendLine("</SOAP-ENV:Envelope>",$bCrlf)

$oGen = ObjCreate("Chilkat.XmlDSigGen")
$oGen.SigLocation = "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"
$oGen.SignedInfoCanonAlg = "EXCL_C14N"
$oGen.SignedInfoDigestMethod = "sha256"
$oGen.SignedInfoPrefixList = "wsse SOAP-ENV"
$oGen.KeyInfoType = "X509Data"
$oGen.X509Type = "Certificate"
;  The reference selects the element with SOAP-SEC:id="Body".
$bSuccess = $oGen.AddSameDocRef("Body","sha256","EXCL_C14N","","")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not add the SOAP Body reference." & @CRLF)
    Exit
EndIf

$bSuccess = $oGen.SetX509Cert($oCert,True)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not select signing certificate." & @CRLF)
    Exit
EndIf

$bSuccess = $oGen.CreateXmlDSigSb($oSbXml)
If ($bSuccess <> True) Then
    ConsoleWrite("XML signing failed; inspect diagnostics privately." & @CRLF)
    Exit
EndIf

;  Verify the signature and references before saving. This does not validate trust.
$oVerifier = ObjCreate("Chilkat.XmlDSig")
$bSuccess = $oVerifier.LoadSignatureSb($oSbXml)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load generated signature." & @CRLF)
    Exit
EndIf

$bSuccess = $oVerifier.VerifySignature(True)
If ($bSuccess <> True) Then
    ConsoleWrite("Generated XML signature or reference verification failed." & @CRLF)
    Exit
EndIf

$bSuccess = $oSbXml.WriteFile("qa_output/kms_signed.xml","utf-8",False)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not save signed XML." & @CRLF)
    Exit
EndIf

ConsoleWrite("Verified XMLDSig saved to qa_output/kms_signed.xml" & @CRLF)