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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL lcRegion
LOCAL lnStringMatch3
LOCAL loCert
LOCAL loCloud
LOCAL loSbXml
LOCAL lnBCrlf
LOCAL loGen
LOCAL loVerifier
lnSuccess = 0
loStringCompare = CreateObject('Chilkat.StringBuilder')
* Assumes Chilkat is unlocked. See Global Unlock Sample.
* Keep this credentials file outside public web content and source control.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/kms.json")
IF (lnSuccess <> 1) THEN
? "Could not load private KMS configuration."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loStringCompare.SetString(loCredentials.StringOf("access_key"))
lnStringMatch1 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnStringMatch1 = 1)) THEN
? "Missing or invalid access_key."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loStringCompare.SetString(loCredentials.StringOf("secret_key"))
lnStringMatch2 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnStringMatch2 = 1)) THEN
? "Missing or invalid secret_key."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
lcRegion = "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 (loCredentials.HasMember("session_token") = 1) THEN
? "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loStringCompare.SetString(loCredentials.StringOf("key_id"))
lnStringMatch3 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("key_id") <> 1) OR (lnStringMatch3 = 1)) THEN
? "Missing or invalid key_id."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadFromFile("qa_data/certs/myCert.cer")
IF (lnSuccess <> 1) THEN
? "Could not load the public signing certificate."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
CANCEL
ENDIF
loCloud = CreateObject('Chilkat.JsonObject')
* SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
lnSuccess = loCloud.UpdateString("service","aws_kms")
IF (lnSuccess <> 1) THEN
? "Could not set service."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
CANCEL
ENDIF
lnSuccess = loCloud.UpdateString("access_key",loCredentials.StringOf("access_key"))
IF (lnSuccess <> 1) THEN
? "Could not set access_key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
CANCEL
ENDIF
lnSuccess = loCloud.UpdateString("secret_key",loCredentials.StringOf("secret_key"))
IF (lnSuccess <> 1) THEN
? "Could not set secret_key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
CANCEL
ENDIF
lnSuccess = loCloud.UpdateString("region",lcRegion)
IF (lnSuccess <> 1) THEN
? "Could not set region."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
CANCEL
ENDIF
lnSuccess = loCloud.UpdateString("key_id",loCredentials.StringOf("key_id"))
IF (lnSuccess <> 1) THEN
? "Could not set key_id."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
CANCEL
ENDIF
lnSuccess = loCert.SetCloudSigner(loCloud)
IF (lnSuccess <> 1) THEN
? "Could not configure the AWS KMS signer; inspect diagnostics privately."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
CANCEL
ENDIF
* Create the XML to be signed.
loSbXml = CreateObject('Chilkat.StringBuilder')
lnBCrlf = 1
loSbXml.AppendLine('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>',lnBCrlf)
loSbXml.AppendLine('<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">',lnBCrlf)
loSbXml.AppendLine(" <SOAP-ENV:Header>",lnBCrlf)
loSbXml.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>',lnBCrlf)
loSbXml.AppendLine(" </SOAP-ENV:Header>",lnBCrlf)
loSbXml.AppendLine(' <SOAP-ENV:Body xmlns:SOAP-SEC="http://schemas.xmlsoap.org/soap/security/2000-12" SOAP-SEC:id="Body">',lnBCrlf)
loSbXml.AppendLine(' <z:FooBar xmlns:z="http://example.com" />',lnBCrlf)
loSbXml.AppendLine(" </SOAP-ENV:Body>",lnBCrlf)
loSbXml.AppendLine("</SOAP-ENV:Envelope>",lnBCrlf)
loGen = CreateObject('Chilkat.XmlDSigGen')
loGen.SigLocation = "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"
loGen.SignedInfoCanonAlg = "EXCL_C14N"
loGen.SignedInfoDigestMethod = "sha256"
loGen.SignedInfoPrefixList = "wsse SOAP-ENV"
loGen.KeyInfoType = "X509Data"
loGen.X509Type = "Certificate"
* The reference selects the element with SOAP-SEC:id="Body".
lnSuccess = loGen.AddSameDocRef("Body","sha256","EXCL_C14N","","")
IF (lnSuccess <> 1) THEN
? "Could not add the SOAP Body reference."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
RELEASE loSbXml
RELEASE loGen
CANCEL
ENDIF
lnSuccess = loGen.SetX509Cert(loCert,1)
IF (lnSuccess <> 1) THEN
? "Could not select signing certificate."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
RELEASE loSbXml
RELEASE loGen
CANCEL
ENDIF
lnSuccess = loGen.CreateXmlDSigSb(loSbXml)
IF (lnSuccess <> 1) THEN
? "XML signing failed; inspect diagnostics privately."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
RELEASE loSbXml
RELEASE loGen
CANCEL
ENDIF
* Verify the signature and references before saving. This does not validate trust.
loVerifier = CreateObject('Chilkat.XmlDSig')
lnSuccess = loVerifier.LoadSignatureSb(loSbXml)
IF (lnSuccess <> 1) THEN
? "Could not load generated signature."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
RELEASE loSbXml
RELEASE loGen
RELEASE loVerifier
CANCEL
ENDIF
lnSuccess = loVerifier.VerifySignature(1)
IF (lnSuccess <> 1) THEN
? "Generated XML signature or reference verification failed."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
RELEASE loSbXml
RELEASE loGen
RELEASE loVerifier
CANCEL
ENDIF
lnSuccess = loSbXml.WriteFile("qa_output/kms_signed.xml","utf-8",0)
IF (lnSuccess <> 1) THEN
? "Could not save signed XML."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
RELEASE loSbXml
RELEASE loGen
RELEASE loVerifier
CANCEL
ENDIF
? "Verified XMLDSig saved to qa_output/kms_signed.xml"
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loCert
RELEASE loCloud
RELEASE loSbXml
RELEASE loGen
RELEASE loVerifier