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.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
set stringCompare [new_CkStringBuilder]
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Keep this credentials file outside public web content and source control.
set credentials [new_CkJsonObject]
CkJsonObject_put_Strict $credentials 1
set success [CkJsonObject_LoadFile $credentials "qa_data/aws/kms.json"]
if {$success != 1} then {
puts "Could not load private KMS configuration."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "access_key"]
set stringMatch1 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "access_key"] != 1] || [$stringMatch1 == 1]} then {
puts "Missing or invalid access_key."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "secret_key"]
set stringMatch2 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "secret_key"] != 1] || [$stringMatch2 == 1]} then {
puts "Missing or invalid secret_key."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
set region "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 {[CkJsonObject_HasMember $credentials "session_token"] == 1} then {
puts "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "key_id"]
set stringMatch3 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "key_id"] != 1] || [$stringMatch3 == 1]} then {
puts "Missing or invalid key_id."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
set cert [new_CkCert]
set success [CkCert_LoadFromFile $cert "qa_data/certs/myCert.cer"]
if {$success != 1} then {
puts "Could not load the public signing certificate."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
exit
}
set cloud [new_CkJsonObject]
# SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
set success [CkJsonObject_UpdateString $cloud "service" "aws_kms"]
if {$success != 1} then {
puts "Could not set service."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
exit
}
set success [CkJsonObject_UpdateString $cloud "access_key" [CkJsonObject_stringOf $credentials "access_key"]]
if {$success != 1} then {
puts "Could not set access_key."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
exit
}
set success [CkJsonObject_UpdateString $cloud "secret_key" [CkJsonObject_stringOf $credentials "secret_key"]]
if {$success != 1} then {
puts "Could not set secret_key."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
exit
}
set success [CkJsonObject_UpdateString $cloud "region" $region]
if {$success != 1} then {
puts "Could not set region."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
exit
}
set success [CkJsonObject_UpdateString $cloud "key_id" [CkJsonObject_stringOf $credentials "key_id"]]
if {$success != 1} then {
puts "Could not set key_id."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
exit
}
set success [CkCert_SetCloudSigner $cert $cloud]
if {$success != 1} then {
puts "Could not configure the AWS KMS signer; inspect diagnostics privately."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
exit
}
# Create the XML to be signed.
set sbXml [new_CkStringBuilder]
set bCrlf 1
CkStringBuilder_AppendLine $sbXml "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>" $bCrlf
CkStringBuilder_AppendLine $sbXml "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" $bCrlf
CkStringBuilder_AppendLine $sbXml " <SOAP-ENV:Header>" $bCrlf
CkStringBuilder_AppendLine $sbXml " <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
CkStringBuilder_AppendLine $sbXml " </SOAP-ENV:Header>" $bCrlf
CkStringBuilder_AppendLine $sbXml " <SOAP-ENV:Body xmlns:SOAP-SEC=\"http://schemas.xmlsoap.org/soap/security/2000-12\" SOAP-SEC:id=\"Body\">" $bCrlf
CkStringBuilder_AppendLine $sbXml " <z:FooBar xmlns:z=\"http://example.com\" />" $bCrlf
CkStringBuilder_AppendLine $sbXml " </SOAP-ENV:Body>" $bCrlf
CkStringBuilder_AppendLine $sbXml "</SOAP-ENV:Envelope>" $bCrlf
set gen [new_CkXmlDSigGen]
CkXmlDSigGen_put_SigLocation $gen "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"
CkXmlDSigGen_put_SignedInfoCanonAlg $gen "EXCL_C14N"
CkXmlDSigGen_put_SignedInfoDigestMethod $gen "sha256"
CkXmlDSigGen_put_SignedInfoPrefixList $gen "wsse SOAP-ENV"
CkXmlDSigGen_put_KeyInfoType $gen "X509Data"
CkXmlDSigGen_put_X509Type $gen "Certificate"
# The reference selects the element with SOAP-SEC:id="Body".
set success [CkXmlDSigGen_AddSameDocRef $gen "Body" "sha256" "EXCL_C14N" "" ""]
if {$success != 1} then {
puts "Could not add the SOAP Body reference."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
delete_CkStringBuilder $sbXml
delete_CkXmlDSigGen $gen
exit
}
set success [CkXmlDSigGen_SetX509Cert $gen $cert 1]
if {$success != 1} then {
puts "Could not select signing certificate."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
delete_CkStringBuilder $sbXml
delete_CkXmlDSigGen $gen
exit
}
set success [CkXmlDSigGen_CreateXmlDSigSb $gen $sbXml]
if {$success != 1} then {
puts "XML signing failed; inspect diagnostics privately."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
delete_CkStringBuilder $sbXml
delete_CkXmlDSigGen $gen
exit
}
# Verify the signature and references before saving. This does not validate trust.
set verifier [new_CkXmlDSig]
set success [CkXmlDSig_LoadSignatureSb $verifier $sbXml]
if {$success != 1} then {
puts "Could not load generated signature."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
delete_CkStringBuilder $sbXml
delete_CkXmlDSigGen $gen
delete_CkXmlDSig $verifier
exit
}
set success [CkXmlDSig_VerifySignature $verifier 1]
if {$success != 1} then {
puts "Generated XML signature or reference verification failed."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
delete_CkStringBuilder $sbXml
delete_CkXmlDSigGen $gen
delete_CkXmlDSig $verifier
exit
}
set success [CkStringBuilder_WriteFile $sbXml "qa_output/kms_signed.xml" "utf-8" 0]
if {$success != 1} then {
puts "Could not save signed XML."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
delete_CkStringBuilder $sbXml
delete_CkXmlDSigGen $gen
delete_CkXmlDSig $verifier
exit
}
puts "Verified XMLDSig saved to qa_output/kms_signed.xml"
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkCert $cert
delete_CkJsonObject $cloud
delete_CkStringBuilder $sbXml
delete_CkXmlDSigGen $gen
delete_CkXmlDSig $verifier