Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
string ls_Region
long li_StringMatch3
oleobject loo_Cert
oleobject loo_Cloud
oleobject loo_SbXml
long li_BCrlf
oleobject loo_Gen
oleobject loo_Verifier

li_Success = 0

loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_StringCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this credentials file outside public web content and source control.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")

loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/kms.json")
if li_Success <> 1 then
    Write-Debug "Could not load private KMS configuration."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("access_key"))
li_StringMatch1 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_StringMatch1 = 1) then
    Write-Debug "Missing or invalid access_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_StringMatch2 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch2 = 1) then
    Write-Debug "Missing or invalid secret_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

ls_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 loo_Credentials.HasMember("session_token") = 1 then
    Write-Debug "This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("key_id"))
li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("key_id") <> 1) OR (li_StringMatch3 = 1) then
    Write-Debug "Missing or invalid key_id."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadFromFile("qa_data/certs/myCert.cer")
if li_Success <> 1 then
    Write-Debug "Could not load the public signing certificate."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    return
end if

loo_Cloud = create oleobject
li_rc = loo_Cloud.ConnectToNewObject("Chilkat.JsonObject")

//  SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
li_Success = loo_Cloud.UpdateString("service","aws_kms")
if li_Success <> 1 then
    Write-Debug "Could not set service."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cloud.UpdateString("access_key",loo_Credentials.StringOf("access_key"))
if li_Success <> 1 then
    Write-Debug "Could not set access_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cloud.UpdateString("secret_key",loo_Credentials.StringOf("secret_key"))
if li_Success <> 1 then
    Write-Debug "Could not set secret_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cloud.UpdateString("region",ls_Region)
if li_Success <> 1 then
    Write-Debug "Could not set region."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cloud.UpdateString("key_id",loo_Credentials.StringOf("key_id"))
if li_Success <> 1 then
    Write-Debug "Could not set key_id."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

li_Success = loo_Cert.SetCloudSigner(loo_Cloud)
if li_Success <> 1 then
    Write-Debug "Could not configure the AWS KMS signer; inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    return
end if

//  Create the XML to be signed.
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

li_BCrlf = 1
loo_SbXml.AppendLine("<?xml version=~"1.0~" encoding=~"UTF-8~" standalone=~"no~" ?>",li_BCrlf)
loo_SbXml.AppendLine("<SOAP-ENV:Envelope xmlns:SOAP-ENV=~"http://schemas.xmlsoap.org/soap/envelope/~">",li_BCrlf)
loo_SbXml.AppendLine("    <SOAP-ENV:Header>",li_BCrlf)
loo_SbXml.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>",li_BCrlf)
loo_SbXml.AppendLine("    </SOAP-ENV:Header>",li_BCrlf)
loo_SbXml.AppendLine("    <SOAP-ENV:Body xmlns:SOAP-SEC=~"http://schemas.xmlsoap.org/soap/security/2000-12~" SOAP-SEC:id=~"Body~">",li_BCrlf)
loo_SbXml.AppendLine("        <z:FooBar xmlns:z=~"http://example.com~" />",li_BCrlf)
loo_SbXml.AppendLine("    </SOAP-ENV:Body>",li_BCrlf)
loo_SbXml.AppendLine("</SOAP-ENV:Envelope>",li_BCrlf)

loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")

loo_Gen.SigLocation = "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"
loo_Gen.SignedInfoCanonAlg = "EXCL_C14N"
loo_Gen.SignedInfoDigestMethod = "sha256"
loo_Gen.SignedInfoPrefixList = "wsse SOAP-ENV"
loo_Gen.KeyInfoType = "X509Data"
loo_Gen.X509Type = "Certificate"
//  The reference selects the element with SOAP-SEC:id="Body".
li_Success = loo_Gen.AddSameDocRef("Body","sha256","EXCL_C14N","","")
if li_Success <> 1 then
    Write-Debug "Could not add the SOAP Body reference."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_SbXml
    destroy loo_Gen
    return
end if

li_Success = loo_Gen.SetX509Cert(loo_Cert,1)
if li_Success <> 1 then
    Write-Debug "Could not select signing certificate."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_SbXml
    destroy loo_Gen
    return
end if

li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug "XML signing failed; inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_SbXml
    destroy loo_Gen
    return
end if

//  Verify the signature and references before saving. This does not validate trust.
loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.XmlDSig")

li_Success = loo_Verifier.LoadSignatureSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug "Could not load generated signature."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_SbXml
    destroy loo_Gen
    destroy loo_Verifier
    return
end if

li_Success = loo_Verifier.VerifySignature(1)
if li_Success <> 1 then
    Write-Debug "Generated XML signature or reference verification failed."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_SbXml
    destroy loo_Gen
    destroy loo_Verifier
    return
end if

li_Success = loo_SbXml.WriteFile("qa_output/kms_signed.xml","utf-8",0)
if li_Success <> 1 then
    Write-Debug "Could not save signed XML."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Cert
    destroy loo_Cloud
    destroy loo_SbXml
    destroy loo_Gen
    destroy loo_Verifier
    return
end if

Write-Debug "Verified XMLDSig saved to qa_output/kms_signed.xml"


destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Cert
destroy loo_Cloud
destroy loo_SbXml
destroy loo_Gen
destroy loo_Verifier