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 VB.NET Downloads
Dim success As Boolean = False
Dim stringCompare As New Chilkat.StringBuilder
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this configuration private, outside public web content and source control.
Dim config As New Chilkat.JsonObject
config.Strict = True
success = config.LoadFile("qa_data/azure/keyvault.json")
If (success <> True) Then
Debug.WriteLine("Could not load private Key Vault configuration.")
Exit Sub
End If
stringCompare.SetString(config.StringOf("vault_name"))
Dim stringMatch1 As Boolean = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = True)) Then
Debug.WriteLine("Missing or invalid vault_name.")
Exit Sub
End If
Dim validate1 As New Chilkat.StringBuilder
Dim alphabet1 As New Chilkat.StringBuilder
success = validate1.Append(config.StringOf("vault_name"))
If (success <> True) Then
Debug.WriteLine("Could not validate vault_name.")
Exit Sub
End If
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If (success <> True) Then
Debug.WriteLine("Could not initialize validation.")
Exit Sub
End If
If ((validate1.Length < 3) Or (validate1.Length > 24)) Then
Debug.WriteLine("Invalid vault_name length.")
Exit Sub
End If
Dim charIndex1 As Integer = 0
While (charIndex1 < validate1.Length)
If (alphabet1.Contains(validate1.GetRange(charIndex1,1,False),True) <> True) Then
Debug.WriteLine("Invalid character in vault_name.")
Exit Sub
End If
charIndex1 = charIndex1 + 1
End While
Dim vaultName As New Chilkat.StringBuilder
vaultName.Append(config.StringOf("vault_name"))
Dim letters As New Chilkat.StringBuilder
letters.Append("abcdefghijklmnopqrstuvwxyz")
If ((letters.Contains(vaultName.GetRange(0,1,False),True) <> True) Or (vaultName.EndsWith("-",True) = True) Or (vaultName.Contains("--",True) = True)) Then
Debug.WriteLine("Invalid vault_name.")
Exit Sub
End If
Dim origin As New Chilkat.StringBuilder
success = origin.Append("https://")
If (success <> True) Then
Debug.WriteLine("Could not construct URL or identifier.")
Exit Sub
End If
success = origin.Append(config.StringOf("vault_name"))
If (success <> True) Then
Debug.WriteLine("Could not construct URL or identifier.")
Exit Sub
End If
success = origin.Append(".vault.azure.net")
If (success <> True) Then
Debug.WriteLine("Could not construct URL or identifier.")
Exit Sub
End If
stringCompare.SetString(config.StringOf("cert_name"))
Dim stringMatch2 As Boolean = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("cert_name") <> 1) Or (stringMatch2 = True)) Then
Debug.WriteLine("Missing or invalid cert_name.")
Exit Sub
End If
Dim validate2 As New Chilkat.StringBuilder
Dim alphabet2 As New Chilkat.StringBuilder
success = validate2.Append(config.StringOf("cert_name"))
If (success <> True) Then
Debug.WriteLine("Could not validate cert_name.")
Exit Sub
End If
success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> True) Then
Debug.WriteLine("Could not initialize validation.")
Exit Sub
End If
If ((validate2.Length < 1) Or (validate2.Length > 127)) Then
Debug.WriteLine("Invalid cert_name length.")
Exit Sub
End If
Dim charIndex2 As Integer = 0
While (charIndex2 < validate2.Length)
If (alphabet2.Contains(validate2.GetRange(charIndex2,1,False),True) <> True) Then
Debug.WriteLine("Invalid character in cert_name.")
Exit Sub
End If
charIndex2 = charIndex2 + 1
End While
stringCompare.SetString(config.StringOf("cert_version"))
Dim stringMatch3 As Boolean = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("cert_version") <> 1) Or (stringMatch3 = True)) Then
Debug.WriteLine("Missing or invalid cert_version.")
Exit Sub
End If
Dim validate3 As New Chilkat.StringBuilder
Dim alphabet3 As New Chilkat.StringBuilder
success = validate3.Append(config.StringOf("cert_version"))
If (success <> True) Then
Debug.WriteLine("Could not validate cert_version.")
Exit Sub
End If
success = alphabet3.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> True) Then
Debug.WriteLine("Could not initialize validation.")
Exit Sub
End If
If ((validate3.Length < 1) Or (validate3.Length > 127)) Then
Debug.WriteLine("Invalid cert_version length.")
Exit Sub
End If
Dim charIndex3 As Integer = 0
While (charIndex3 < validate3.Length)
If (alphabet3.Contains(validate3.GetRange(charIndex3,1,False),True) <> True) Then
Debug.WriteLine("Invalid character in cert_version.")
Exit Sub
End If
charIndex3 = charIndex3 + 1
End While
stringCompare.SetString(config.StringOf("client_id"))
Dim stringMatch4 As Boolean = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("client_id") <> 1) Or (stringMatch4 = True)) Then
Debug.WriteLine("Missing or invalid client_id.")
Exit Sub
End If
stringCompare.SetString(config.StringOf("client_secret"))
Dim stringMatch5 As Boolean = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("client_secret") <> 1) Or (stringMatch5 = True)) Then
Debug.WriteLine("Missing or invalid client_secret.")
Exit Sub
End If
stringCompare.SetString(config.StringOf("tenant_id"))
Dim stringMatch6 As Boolean = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("tenant_id") <> 1) Or (stringMatch6 = True)) Then
Debug.WriteLine("Missing or invalid tenant_id.")
Exit Sub
End If
Dim validate4 As New Chilkat.StringBuilder
Dim alphabet4 As New Chilkat.StringBuilder
success = validate4.Append(config.StringOf("tenant_id"))
If (success <> True) Then
Debug.WriteLine("Could not validate tenant GUID.")
Exit Sub
End If
success = alphabet4.Append("0123456789abcdefABCDEF-")
If (success <> True) Then
Debug.WriteLine("Could not initialize validation.")
Exit Sub
End If
If ((validate4.Length < 36) Or (validate4.Length > 36)) Then
Debug.WriteLine("Invalid tenant GUID length.")
Exit Sub
End If
Dim charIndex4 As Integer = 0
While (charIndex4 < validate4.Length)
If (alphabet4.Contains(validate4.GetRange(charIndex4,1,False),True) <> True) Then
Debug.WriteLine("Invalid character in tenant GUID.")
Exit Sub
End If
charIndex4 = charIndex4 + 1
End While
Dim cert As New Chilkat.Cert
success = cert.LoadFromFile("qa_data/certs/myCert.cer")
If (success <> True) Then
Debug.WriteLine("Could not load the public signing certificate.")
Exit Sub
End If
Dim cloud As New Chilkat.JsonObject
success = cloud.UpdateString("service","azure_keyvault")
If (success <> True) Then
Debug.WriteLine("Could not set service.")
Exit Sub
End If
success = cloud.UpdateString("client_id",config.StringOf("client_id"))
If (success <> True) Then
Debug.WriteLine("Could not set client_id.")
Exit Sub
End If
success = cloud.UpdateString("client_secret",config.StringOf("client_secret"))
If (success <> True) Then
Debug.WriteLine("Could not set client_secret.")
Exit Sub
End If
success = cloud.UpdateString("tenant_id",config.StringOf("tenant_id"))
If (success <> True) Then
Debug.WriteLine("Could not set tenant_id.")
Exit Sub
End If
success = cloud.UpdateString("vault_name",config.StringOf("vault_name"))
If (success <> True) Then
Debug.WriteLine("Could not set vault_name.")
Exit Sub
End If
success = cloud.UpdateString("cert_name",config.StringOf("cert_name"))
If (success <> True) Then
Debug.WriteLine("Could not set cert_name.")
Exit Sub
End If
success = cloud.UpdateString("cert_version",config.StringOf("cert_version"))
If (success <> True) Then
Debug.WriteLine("Could not set cert_version.")
Exit Sub
End If
success = cert.SetCloudSigner(cloud)
If (success <> True) Then
Debug.WriteLine("Could not configure the Key Vault signer; inspect diagnostics privately.")
Exit Sub
End If
' Create the XML to be signed.
Dim sbXml As New Chilkat.StringBuilder
Dim bCrlf As Boolean = 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)
Dim gen As New 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 <> True) Then
Debug.WriteLine("Could not add the SOAP Body reference.")
Exit Sub
End If
success = gen.SetX509Cert(cert,True)
If (success <> True) Then
Debug.WriteLine("Could not select signing certificate.")
Exit Sub
End If
success = gen.CreateXmlDSigSb(sbXml)
If (success <> True) Then
Debug.WriteLine("XML signing failed; inspect diagnostics privately.")
Exit Sub
End If
' Verify the signature and references before saving. This does not validate trust.
Dim verifier As New Chilkat.XmlDSig
success = verifier.LoadSignatureSb(sbXml)
If (success <> True) Then
Debug.WriteLine("Could not load generated signature.")
Exit Sub
End If
success = verifier.VerifySignature(True)
If (success <> True) Then
Debug.WriteLine("Generated XML signature or reference verification failed.")
Exit Sub
End If
success = sbXml.WriteFile("qa_output/azure_signed.xml","utf-8",False)
If (success <> True) Then
Debug.WriteLine("Could not save signed XML.")
Exit Sub
End If
Debug.WriteLine("Verified XMLDSig saved to qa_output/azure_signed.xml")