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 CkJava Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
CkStringBuilder stringCompare = new CkStringBuilder();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this credentials file outside public web content and source control.
CkJsonObject credentials = new CkJsonObject();
credentials.put_Strict(true);
success = credentials.LoadFile("qa_data/aws/kms.json");
if (success != true) {
System.out.println("Could not load private KMS configuration.");
return;
}
stringCompare.SetString(credentials.stringOf("access_key"));
boolean stringMatch1 = stringCompare.ContentsEqual("",true);
if ((credentials.JsonTypeOf("access_key") != 1) || (stringMatch1 == true)) {
System.out.println("Missing or invalid access_key.");
return;
}
stringCompare.SetString(credentials.stringOf("secret_key"));
boolean stringMatch2 = stringCompare.ContentsEqual("",true);
if ((credentials.JsonTypeOf("secret_key") != 1) || (stringMatch2 == true)) {
System.out.println("Missing or invalid secret_key.");
return;
}
String 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 (credentials.HasMember("session_token") == true) {
System.out.println("This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.");
return;
}
stringCompare.SetString(credentials.stringOf("key_id"));
boolean stringMatch3 = stringCompare.ContentsEqual("",true);
if ((credentials.JsonTypeOf("key_id") != 1) || (stringMatch3 == true)) {
System.out.println("Missing or invalid key_id.");
return;
}
CkCert cert = new CkCert();
success = cert.LoadFromFile("qa_data/certs/myCert.cer");
if (success != true) {
System.out.println("Could not load the public signing certificate.");
return;
}
CkJsonObject cloud = new CkJsonObject();
// SetCloudSigner uses aws_kms (underscore); UploadToCloud uses aws-kms.
success = cloud.UpdateString("service","aws_kms");
if (success != true) {
System.out.println("Could not set service.");
return;
}
success = cloud.UpdateString("access_key",credentials.stringOf("access_key"));
if (success != true) {
System.out.println("Could not set access_key.");
return;
}
success = cloud.UpdateString("secret_key",credentials.stringOf("secret_key"));
if (success != true) {
System.out.println("Could not set secret_key.");
return;
}
success = cloud.UpdateString("region",region);
if (success != true) {
System.out.println("Could not set region.");
return;
}
success = cloud.UpdateString("key_id",credentials.stringOf("key_id"));
if (success != true) {
System.out.println("Could not set key_id.");
return;
}
success = cert.SetCloudSigner(cloud);
if (success != true) {
System.out.println("Could not configure the AWS KMS signer; inspect diagnostics privately.");
return;
}
// Create the XML to be signed.
CkStringBuilder sbXml = new CkStringBuilder();
boolean bCrlf = true;
sbXml.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>",bCrlf);
sbXml.AppendLine("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">",bCrlf);
sbXml.AppendLine(" <SOAP-ENV:Header>",bCrlf);
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>",bCrlf);
sbXml.AppendLine(" </SOAP-ENV:Header>",bCrlf);
sbXml.AppendLine(" <SOAP-ENV:Body xmlns:SOAP-SEC=\"http://schemas.xmlsoap.org/soap/security/2000-12\" SOAP-SEC:id=\"Body\">",bCrlf);
sbXml.AppendLine(" <z:FooBar xmlns:z=\"http://example.com\" />",bCrlf);
sbXml.AppendLine(" </SOAP-ENV:Body>",bCrlf);
sbXml.AppendLine("</SOAP-ENV:Envelope>",bCrlf);
CkXmlDSigGen gen = new CkXmlDSigGen();
gen.put_SigLocation("SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security");
gen.put_SignedInfoCanonAlg("EXCL_C14N");
gen.put_SignedInfoDigestMethod("sha256");
gen.put_SignedInfoPrefixList("wsse SOAP-ENV");
gen.put_KeyInfoType("X509Data");
gen.put_X509Type("Certificate");
// The reference selects the element with SOAP-SEC:id="Body".
success = gen.AddSameDocRef("Body","sha256","EXCL_C14N","","");
if (success != true) {
System.out.println("Could not add the SOAP Body reference.");
return;
}
success = gen.SetX509Cert(cert,true);
if (success != true) {
System.out.println("Could not select signing certificate.");
return;
}
success = gen.CreateXmlDSigSb(sbXml);
if (success != true) {
System.out.println("XML signing failed; inspect diagnostics privately.");
return;
}
// Verify the signature and references before saving. This does not validate trust.
CkXmlDSig verifier = new CkXmlDSig();
success = verifier.LoadSignatureSb(sbXml);
if (success != true) {
System.out.println("Could not load generated signature.");
return;
}
success = verifier.VerifySignature(true);
if (success != true) {
System.out.println("Generated XML signature or reference verification failed.");
return;
}
success = sbXml.WriteFile("qa_output/kms_signed.xml","utf-8",false);
if (success != true) {
System.out.println("Could not save signed XML.");
return;
}
System.out.println("Verified XMLDSig saved to qa_output/kms_signed.xml");
}
}