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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
set stringCompare = Server.CreateObject("Chilkat.StringBuilder")
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this configuration private, outside public web content and source control.
set config = Server.CreateObject("Chilkat.JsonObject")
config.Strict = 1
success = config.LoadFile("qa_data/azure/keyvault.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load private Key Vault configuration.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("vault_name"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid vault_name.") & "</pre>"
Response.End
End If
set validate1 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet1 = Server.CreateObject("Chilkat.StringBuilder")
success = validate1.Append(config.StringOf("vault_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate vault_name.") & "</pre>"
Response.End
End If
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate1.Length < 3) Or (validate1.Length > 24)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid vault_name length.") & "</pre>"
Response.End
End If
charIndex1 = 0
Do While (charIndex1 < validate1.Length)
If (alphabet1.Contains(validate1.GetRange(charIndex1,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in vault_name.") & "</pre>"
Response.End
End If
charIndex1 = charIndex1 + 1
Loop
set vaultName = Server.CreateObject("Chilkat.StringBuilder")
success = vaultName.Append(config.StringOf("vault_name"))
set letters = Server.CreateObject("Chilkat.StringBuilder")
success = letters.Append("abcdefghijklmnopqrstuvwxyz")
If ((letters.Contains(vaultName.GetRange(0,1,0),1) <> 1) Or (vaultName.EndsWith("-",1) = 1) Or (vaultName.Contains("--",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid vault_name.") & "</pre>"
Response.End
End If
set origin = Server.CreateObject("Chilkat.StringBuilder")
success = origin.Append("https://")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = origin.Append(config.StringOf("vault_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = origin.Append(".vault.azure.net")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("cert_name"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("cert_name") <> 1) Or (stringMatch2 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid cert_name.") & "</pre>"
Response.End
End If
set validate2 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet2 = Server.CreateObject("Chilkat.StringBuilder")
success = validate2.Append(config.StringOf("cert_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate cert_name.") & "</pre>"
Response.End
End If
success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate2.Length < 1) Or (validate2.Length > 127)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid cert_name length.") & "</pre>"
Response.End
End If
charIndex2 = 0
Do While (charIndex2 < validate2.Length)
If (alphabet2.Contains(validate2.GetRange(charIndex2,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in cert_name.") & "</pre>"
Response.End
End If
charIndex2 = charIndex2 + 1
Loop
success = stringCompare.SetString(config.StringOf("cert_version"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("cert_version") <> 1) Or (stringMatch3 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid cert_version.") & "</pre>"
Response.End
End If
set validate3 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet3 = Server.CreateObject("Chilkat.StringBuilder")
success = validate3.Append(config.StringOf("cert_version"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate cert_version.") & "</pre>"
Response.End
End If
success = alphabet3.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate3.Length < 1) Or (validate3.Length > 127)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid cert_version length.") & "</pre>"
Response.End
End If
charIndex3 = 0
Do While (charIndex3 < validate3.Length)
If (alphabet3.Contains(validate3.GetRange(charIndex3,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in cert_version.") & "</pre>"
Response.End
End If
charIndex3 = charIndex3 + 1
Loop
success = stringCompare.SetString(config.StringOf("client_id"))
stringMatch4 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("client_id") <> 1) Or (stringMatch4 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid client_id.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("client_secret"))
stringMatch5 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("client_secret") <> 1) Or (stringMatch5 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid client_secret.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("tenant_id"))
stringMatch6 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("tenant_id") <> 1) Or (stringMatch6 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid tenant_id.") & "</pre>"
Response.End
End If
set validate4 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet4 = Server.CreateObject("Chilkat.StringBuilder")
success = validate4.Append(config.StringOf("tenant_id"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate tenant GUID.") & "</pre>"
Response.End
End If
success = alphabet4.Append("0123456789abcdefABCDEF-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate4.Length < 36) Or (validate4.Length > 36)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid tenant GUID length.") & "</pre>"
Response.End
End If
charIndex4 = 0
Do While (charIndex4 < validate4.Length)
If (alphabet4.Contains(validate4.GetRange(charIndex4,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in tenant GUID.") & "</pre>"
Response.End
End If
charIndex4 = charIndex4 + 1
Loop
set cert = Server.CreateObject("Chilkat.Cert")
success = cert.LoadFromFile("qa_data/certs/myCert.cer")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load the public signing certificate.") & "</pre>"
Response.End
End If
set cloud = Server.CreateObject("Chilkat.JsonObject")
success = cloud.UpdateString("service","azure_keyvault")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set service.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("client_id",config.StringOf("client_id"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set client_id.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("client_secret",config.StringOf("client_secret"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set client_secret.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("tenant_id",config.StringOf("tenant_id"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set tenant_id.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("vault_name",config.StringOf("vault_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set vault_name.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("cert_name",config.StringOf("cert_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set cert_name.") & "</pre>"
Response.End
End If
success = cloud.UpdateString("cert_version",config.StringOf("cert_version"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set cert_version.") & "</pre>"
Response.End
End If
success = cert.SetCloudSigner(cloud)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not configure the Key Vault signer; inspect diagnostics privately.") & "</pre>"
Response.End
End If
' Create the XML to be signed.
set sbXml = Server.CreateObject("Chilkat.StringBuilder")
bCrlf = 1
success = sbXml.AppendLine("<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no"" ?>",bCrlf)
success = sbXml.AppendLine("<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"">",bCrlf)
success = sbXml.AppendLine(" <SOAP-ENV:Header>",bCrlf)
success = 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)
success = sbXml.AppendLine(" </SOAP-ENV:Header>",bCrlf)
success = sbXml.AppendLine(" <SOAP-ENV:Body xmlns:SOAP-SEC=""http://schemas.xmlsoap.org/soap/security/2000-12"" SOAP-SEC:id=""Body"">",bCrlf)
success = sbXml.AppendLine(" <z:FooBar xmlns:z=""http://example.com"" />",bCrlf)
success = sbXml.AppendLine(" </SOAP-ENV:Body>",bCrlf)
success = sbXml.AppendLine("</SOAP-ENV:Envelope>",bCrlf)
set gen = Server.CreateObject("Chilkat.XmlDSigGen")
gen.SigLocation = "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"
gen.SignedInfoCanonAlg = "EXCL_C14N"
gen.SignedInfoDigestMethod = "sha256"
gen.SignedInfoPrefixList = "wsse SOAP-ENV"
gen.KeyInfoType = "X509Data"
gen.X509Type = "Certificate"
' The reference selects the element with SOAP-SEC:id="Body".
success = gen.AddSameDocRef("Body","sha256","EXCL_C14N","","")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not add the SOAP Body reference.") & "</pre>"
Response.End
End If
success = gen.SetX509Cert(cert,1)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not select signing certificate.") & "</pre>"
Response.End
End If
success = gen.CreateXmlDSigSb(sbXml)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "XML signing failed; inspect diagnostics privately.") & "</pre>"
Response.End
End If
' Verify the signature and references before saving. This does not validate trust.
set verifier = Server.CreateObject("Chilkat.XmlDSig")
success = verifier.LoadSignatureSb(sbXml)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load generated signature.") & "</pre>"
Response.End
End If
success = verifier.VerifySignature(1)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Generated XML signature or reference verification failed.") & "</pre>"
Response.End
End If
success = sbXml.WriteFile("qa_output/azure_signed.xml","utf-8",0)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not save signed XML.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Verified XMLDSig saved to qa_output/azure_signed.xml") & "</pre>"
%>
</body>
</html>