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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoCredentials
Boolean iStringMatch1
Boolean iStringMatch2
String sRegion
Boolean iStringMatch3
Variant vCert
Handle hoCert
Variant vCloud
Handle hoCloud
Variant vSbXml
Handle hoSbXml
Boolean iBCrlf
Handle hoGen
Handle hoVerifier
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
// These convenience-method examples use an access-key/secret-key pair.
// Do not silently discard the token belonging to temporary credentials.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Showln "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration."
Procedure_Return
End
Get ComStringOf Of hoCredentials "key_id" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
Get ComJsonTypeOf Of hoCredentials "key_id" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
Showln "Missing or invalid key_id."
Procedure_Return
End
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get ComLoadFromFile Of hoCert "qa_data/certs/myCert.cer" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load the public signing certificate."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoCloud
If (Not(IsComObjectCreated(hoCloud))) Begin
Send CreateComObject of hoCloud
End
// SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
Get ComUpdateString Of hoCloud "service" "aws_kms" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set service."
Procedure_Return
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComUpdateString Of hoCloud "access_key" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set access_key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComUpdateString Of hoCloud "secret_key" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set secret_key."
Procedure_Return
End
Get ComUpdateString Of hoCloud "region" sRegion To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set region."
Procedure_Return
End
Get ComStringOf Of hoCredentials "key_id" To sTemp1
Get ComUpdateString Of hoCloud "key_id" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set key_id."
Procedure_Return
End
Get pvComObject of hoCloud to vCloud
Get ComSetCloudSigner Of hoCert vCloud To iSuccess
If (iSuccess <> True) Begin
Showln "Could not configure the AWS KMS signer; inspect diagnostics privately."
Procedure_Return
End
// Create the XML to be signed.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
If (Not(IsComObjectCreated(hoSbXml))) Begin
Send CreateComObject of hoSbXml
End
Move True To iBCrlf
Get ComAppendLine Of hoSbXml '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>' iBCrlf To iSuccess
Get ComAppendLine Of hoSbXml '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">' iBCrlf To iSuccess
Get ComAppendLine Of hoSbXml " <SOAP-ENV:Header>" iBCrlf To iSuccess
Get ComAppendLine Of hoSbXml ' <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>' iBCrlf To iSuccess
Get ComAppendLine Of hoSbXml " </SOAP-ENV:Header>" iBCrlf To iSuccess
Get ComAppendLine Of hoSbXml ' <SOAP-ENV:Body xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12" SOAP-SEC:id="Body">' iBCrlf To iSuccess
Get ComAppendLine Of hoSbXml ' <z:FooBar xmlns:z="http://example.com" />' iBCrlf To iSuccess
Get ComAppendLine Of hoSbXml " </SOAP-ENV:Body>" iBCrlf To iSuccess
Get ComAppendLine Of hoSbXml "</SOAP-ENV:Envelope>" iBCrlf To iSuccess
Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
If (Not(IsComObjectCreated(hoGen))) Begin
Send CreateComObject of hoGen
End
Set ComSigLocation Of hoGen To "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"
Set ComSignedInfoCanonAlg Of hoGen To "EXCL_C14N"
Set ComSignedInfoDigestMethod Of hoGen To "sha256"
Set ComSignedInfoPrefixList Of hoGen To "wsse SOAP-ENV"
Set ComKeyInfoType Of hoGen To "X509Data"
Set ComX509Type Of hoGen To "Certificate"
// The reference selects the element with SOAP-SEC:id="Body".
Get ComAddSameDocRef Of hoGen "Body" "sha256" "EXCL_C14N" "" "" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not add the SOAP Body reference."
Procedure_Return
End
Get pvComObject of hoCert to vCert
Get ComSetX509Cert Of hoGen vCert True To iSuccess
If (iSuccess <> True) Begin
Showln "Could not select signing certificate."
Procedure_Return
End
Get pvComObject of hoSbXml to vSbXml
Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
If (iSuccess <> True) Begin
Showln "XML signing failed; inspect diagnostics privately."
Procedure_Return
End
// Verify the signature and references before saving. This does not validate trust.
Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier
If (Not(IsComObjectCreated(hoVerifier))) Begin
Send CreateComObject of hoVerifier
End
Get pvComObject of hoSbXml to vSbXml
Get ComLoadSignatureSb Of hoVerifier vSbXml To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load generated signature."
Procedure_Return
End
Get ComVerifySignature Of hoVerifier True To iSuccess
If (iSuccess <> True) Begin
Showln "Generated XML signature or reference verification failed."
Procedure_Return
End
Get ComWriteFile Of hoSbXml "qa_output/kms_signed.xml" "utf-8" False To iSuccess
If (iSuccess <> True) Begin
Showln "Could not save signed XML."
Procedure_Return
End
Showln "Verified XMLDSig saved to qa_output/kms_signed.xml"
End_Procedure