Azure Key Vault: Sign a SOAP Body with XMLDSig and SHA-256
See more Azure Key Vault Examples
Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.
Uses Chilkat's cloud convenience integration with client_id, client_secret, tenant_id (directory GUID), vault_name, and cert_name in private configuration. The installed Chilkat library manages authentication and service API versions for this helper; this example does not select a REST api-version. Use the explicit Http examples when that control is required. Keep the runtime current and inspect diagnostics privately.
Set explicit cert_version and load its matching public certificate from qa_data/certs/myCert.cer. Keep the version pinned rather than implicitly following certificate rotation. The backing key must permit signing, and the application needs certificates/get and keys/sign. The private key remains in Key Vault; the document is assembled locally and its digest is signed remotely. A cryptographically valid signature does not establish certificate trust, revocation status, or a trusted signing time.
Uses SHA-256 for the reference and signature, exclusive canonicalization, and verifies the generated signature and reference before saving. This is XMLDSig, not XAdES: it has no XAdES qualifying properties. Adapt WS-Security conventions to your service; SOAP headers are not signed.
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 configuration private, outside public web content and source control.
CkJsonObject config = new CkJsonObject();
config.put_Strict(true);
success = config.LoadFile("qa_data/azure/keyvault.json");
if (success != true) {
System.out.println("Could not load private Key Vault configuration.");
return;
}
stringCompare.SetString(config.stringOf("vault_name"));
boolean stringMatch1 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("vault_name") != 1) || (stringMatch1 == true)) {
System.out.println("Missing or invalid vault_name.");
return;
}
CkStringBuilder validate1 = new CkStringBuilder();
CkStringBuilder alphabet1 = new CkStringBuilder();
success = validate1.Append(config.stringOf("vault_name"));
if (success != true) {
System.out.println("Could not validate vault_name.");
return;
}
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-");
if (success != true) {
System.out.println("Could not initialize validation.");
return;
}
if ((validate1.get_Length() < 3) || (validate1.get_Length() > 24)) {
System.out.println("Invalid vault_name length.");
return;
}
int charIndex1 = 0;
while ((charIndex1 < validate1.get_Length())) {
if (alphabet1.Contains(validate1.getRange(charIndex1,1,false),true) != true) {
System.out.println("Invalid character in vault_name.");
return;
}
charIndex1 = charIndex1+1;
}
CkStringBuilder vaultName = new CkStringBuilder();
vaultName.Append(config.stringOf("vault_name"));
CkStringBuilder letters = new CkStringBuilder();
letters.Append("abcdefghijklmnopqrstuvwxyz");
if ((letters.Contains(vaultName.getRange(0,1,false),true) != true) || (vaultName.EndsWith("-",true) == true) || (vaultName.Contains("--",true) == true)) {
System.out.println("Invalid vault_name.");
return;
}
CkStringBuilder origin = new CkStringBuilder();
success = origin.Append("https://");
if (success != true) {
System.out.println("Could not construct URL or identifier.");
return;
}
success = origin.Append(config.stringOf("vault_name"));
if (success != true) {
System.out.println("Could not construct URL or identifier.");
return;
}
success = origin.Append(".vault.azure.net");
if (success != true) {
System.out.println("Could not construct URL or identifier.");
return;
}
stringCompare.SetString(config.stringOf("cert_name"));
boolean stringMatch2 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("cert_name") != 1) || (stringMatch2 == true)) {
System.out.println("Missing or invalid cert_name.");
return;
}
CkStringBuilder validate2 = new CkStringBuilder();
CkStringBuilder alphabet2 = new CkStringBuilder();
success = validate2.Append(config.stringOf("cert_name"));
if (success != true) {
System.out.println("Could not validate cert_name.");
return;
}
success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
System.out.println("Could not initialize validation.");
return;
}
if ((validate2.get_Length() < 1) || (validate2.get_Length() > 127)) {
System.out.println("Invalid cert_name length.");
return;
}
int charIndex2 = 0;
while ((charIndex2 < validate2.get_Length())) {
if (alphabet2.Contains(validate2.getRange(charIndex2,1,false),true) != true) {
System.out.println("Invalid character in cert_name.");
return;
}
charIndex2 = charIndex2+1;
}
stringCompare.SetString(config.stringOf("cert_version"));
boolean stringMatch3 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("cert_version") != 1) || (stringMatch3 == true)) {
System.out.println("Missing or invalid cert_version.");
return;
}
CkStringBuilder validate3 = new CkStringBuilder();
CkStringBuilder alphabet3 = new CkStringBuilder();
success = validate3.Append(config.stringOf("cert_version"));
if (success != true) {
System.out.println("Could not validate cert_version.");
return;
}
success = alphabet3.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
System.out.println("Could not initialize validation.");
return;
}
if ((validate3.get_Length() < 1) || (validate3.get_Length() > 127)) {
System.out.println("Invalid cert_version length.");
return;
}
int charIndex3 = 0;
while ((charIndex3 < validate3.get_Length())) {
if (alphabet3.Contains(validate3.getRange(charIndex3,1,false),true) != true) {
System.out.println("Invalid character in cert_version.");
return;
}
charIndex3 = charIndex3+1;
}
stringCompare.SetString(config.stringOf("client_id"));
boolean stringMatch4 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("client_id") != 1) || (stringMatch4 == true)) {
System.out.println("Missing or invalid client_id.");
return;
}
stringCompare.SetString(config.stringOf("client_secret"));
boolean stringMatch5 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("client_secret") != 1) || (stringMatch5 == true)) {
System.out.println("Missing or invalid client_secret.");
return;
}
stringCompare.SetString(config.stringOf("tenant_id"));
boolean stringMatch6 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("tenant_id") != 1) || (stringMatch6 == true)) {
System.out.println("Missing or invalid tenant_id.");
return;
}
CkStringBuilder validate4 = new CkStringBuilder();
CkStringBuilder alphabet4 = new CkStringBuilder();
success = validate4.Append(config.stringOf("tenant_id"));
if (success != true) {
System.out.println("Could not validate tenant GUID.");
return;
}
success = alphabet4.Append("0123456789abcdefABCDEF-");
if (success != true) {
System.out.println("Could not initialize validation.");
return;
}
if ((validate4.get_Length() < 36) || (validate4.get_Length() > 36)) {
System.out.println("Invalid tenant GUID length.");
return;
}
int charIndex4 = 0;
while ((charIndex4 < validate4.get_Length())) {
if (alphabet4.Contains(validate4.getRange(charIndex4,1,false),true) != true) {
System.out.println("Invalid character in tenant GUID.");
return;
}
charIndex4 = charIndex4+1;
}
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();
success = cloud.UpdateString("service","azure_keyvault");
if (success != true) {
System.out.println("Could not set service.");
return;
}
success = cloud.UpdateString("client_id",config.stringOf("client_id"));
if (success != true) {
System.out.println("Could not set client_id.");
return;
}
success = cloud.UpdateString("client_secret",config.stringOf("client_secret"));
if (success != true) {
System.out.println("Could not set client_secret.");
return;
}
success = cloud.UpdateString("tenant_id",config.stringOf("tenant_id"));
if (success != true) {
System.out.println("Could not set tenant_id.");
return;
}
success = cloud.UpdateString("vault_name",config.stringOf("vault_name"));
if (success != true) {
System.out.println("Could not set vault_name.");
return;
}
success = cloud.UpdateString("cert_name",config.stringOf("cert_name"));
if (success != true) {
System.out.println("Could not set cert_name.");
return;
}
success = cloud.UpdateString("cert_version",config.stringOf("cert_version"));
if (success != true) {
System.out.println("Could not set cert_version.");
return;
}
success = cert.SetCloudSigner(cloud);
if (success != true) {
System.out.println("Could not configure the Key Vault 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/azure_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/azure_signed.xml");
}
}