Sample code for 30+ languages & platforms
Xojo Plugin Requires Chilkat v11.6.0+

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.

Microsoft reference

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = 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
    System.DebugLog("Could not load private Key Vault configuration.")
    Return
End If

success = stringCompare.SetString(config.StringOf("vault_name"))
Dim stringMatch1 As Boolean
stringMatch1 = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = True)) Then
    System.DebugLog("Missing or invalid vault_name.")
    Return
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
    System.DebugLog("Could not validate vault_name.")
    Return
End If

success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If (success <> True) Then
    System.DebugLog("Could not initialize validation.")
    Return
End If

If ((validate1.Length < 3) Or (validate1.Length > 24)) Then
    System.DebugLog("Invalid vault_name length.")
    Return
End If

Dim charIndex1 As Int32
charIndex1 = 0
While (charIndex1 < validate1.Length)
    If (alphabet1.Contains(validate1.GetRange(charIndex1,1,False),True) <> True) Then
        System.DebugLog("Invalid character in vault_name.")
        Return
    End If

    charIndex1 = charIndex1 + 1
Wend
Dim vaultName As New Chilkat.StringBuilder
success = vaultName.Append(config.StringOf("vault_name"))
Dim letters As New Chilkat.StringBuilder
success = letters.Append("abcdefghijklmnopqrstuvwxyz")
If ((letters.Contains(vaultName.GetRange(0,1,False),True) <> True) Or (vaultName.EndsWith("-",True) = True) Or (vaultName.Contains("--",True) = True)) Then
    System.DebugLog("Invalid vault_name.")
    Return
End If

Dim origin As New Chilkat.StringBuilder
success = origin.Append("https://")
If (success <> True) Then
    System.DebugLog("Could not construct URL or identifier.")
    Return
End If

success = origin.Append(config.StringOf("vault_name"))
If (success <> True) Then
    System.DebugLog("Could not construct URL or identifier.")
    Return
End If

success = origin.Append(".vault.azure.net")
If (success <> True) Then
    System.DebugLog("Could not construct URL or identifier.")
    Return
End If

success = stringCompare.SetString(config.StringOf("cert_name"))
Dim stringMatch2 As Boolean
stringMatch2 = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("cert_name") <> 1) Or (stringMatch2 = True)) Then
    System.DebugLog("Missing or invalid cert_name.")
    Return
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
    System.DebugLog("Could not validate cert_name.")
    Return
End If

success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> True) Then
    System.DebugLog("Could not initialize validation.")
    Return
End If

If ((validate2.Length < 1) Or (validate2.Length > 127)) Then
    System.DebugLog("Invalid cert_name length.")
    Return
End If

Dim charIndex2 As Int32
charIndex2 = 0
While (charIndex2 < validate2.Length)
    If (alphabet2.Contains(validate2.GetRange(charIndex2,1,False),True) <> True) Then
        System.DebugLog("Invalid character in cert_name.")
        Return
    End If

    charIndex2 = charIndex2 + 1
Wend
success = stringCompare.SetString(config.StringOf("cert_version"))
Dim stringMatch3 As Boolean
stringMatch3 = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("cert_version") <> 1) Or (stringMatch3 = True)) Then
    System.DebugLog("Missing or invalid cert_version.")
    Return
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
    System.DebugLog("Could not validate cert_version.")
    Return
End If

success = alphabet3.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> True) Then
    System.DebugLog("Could not initialize validation.")
    Return
End If

If ((validate3.Length < 1) Or (validate3.Length > 127)) Then
    System.DebugLog("Invalid cert_version length.")
    Return
End If

Dim charIndex3 As Int32
charIndex3 = 0
While (charIndex3 < validate3.Length)
    If (alphabet3.Contains(validate3.GetRange(charIndex3,1,False),True) <> True) Then
        System.DebugLog("Invalid character in cert_version.")
        Return
    End If

    charIndex3 = charIndex3 + 1
Wend
success = stringCompare.SetString(config.StringOf("client_id"))
Dim stringMatch4 As Boolean
stringMatch4 = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("client_id") <> 1) Or (stringMatch4 = True)) Then
    System.DebugLog("Missing or invalid client_id.")
    Return
End If

success = stringCompare.SetString(config.StringOf("client_secret"))
Dim stringMatch5 As Boolean
stringMatch5 = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("client_secret") <> 1) Or (stringMatch5 = True)) Then
    System.DebugLog("Missing or invalid client_secret.")
    Return
End If

success = stringCompare.SetString(config.StringOf("tenant_id"))
Dim stringMatch6 As Boolean
stringMatch6 = stringCompare.ContentsEqual("",True)
If ((config.JsonTypeOf("tenant_id") <> 1) Or (stringMatch6 = True)) Then
    System.DebugLog("Missing or invalid tenant_id.")
    Return
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
    System.DebugLog("Could not validate tenant GUID.")
    Return
End If

success = alphabet4.Append("0123456789abcdefABCDEF-")
If (success <> True) Then
    System.DebugLog("Could not initialize validation.")
    Return
End If

If ((validate4.Length < 36) Or (validate4.Length > 36)) Then
    System.DebugLog("Invalid tenant GUID length.")
    Return
End If

Dim charIndex4 As Int32
charIndex4 = 0
While (charIndex4 < validate4.Length)
    If (alphabet4.Contains(validate4.GetRange(charIndex4,1,False),True) <> True) Then
        System.DebugLog("Invalid character in tenant GUID.")
        Return
    End If

    charIndex4 = charIndex4 + 1
Wend
Dim cert As New Chilkat.Cert
success = cert.LoadFromFile("qa_data/certs/myCert.cer")
If (success <> True) Then
    System.DebugLog("Could not load the public signing certificate.")
    Return
End If

Dim cloud As New Chilkat.JsonObject
success = cloud.UpdateString("service","azure_keyvault")
If (success <> True) Then
    System.DebugLog("Could not set service.")
    Return
End If

success = cloud.UpdateString("client_id",config.StringOf("client_id"))
If (success <> True) Then
    System.DebugLog("Could not set client_id.")
    Return
End If

success = cloud.UpdateString("client_secret",config.StringOf("client_secret"))
If (success <> True) Then
    System.DebugLog("Could not set client_secret.")
    Return
End If

success = cloud.UpdateString("tenant_id",config.StringOf("tenant_id"))
If (success <> True) Then
    System.DebugLog("Could not set tenant_id.")
    Return
End If

success = cloud.UpdateString("vault_name",config.StringOf("vault_name"))
If (success <> True) Then
    System.DebugLog("Could not set vault_name.")
    Return
End If

success = cloud.UpdateString("cert_name",config.StringOf("cert_name"))
If (success <> True) Then
    System.DebugLog("Could not set cert_name.")
    Return
End If

success = cloud.UpdateString("cert_version",config.StringOf("cert_version"))
If (success <> True) Then
    System.DebugLog("Could not set cert_version.")
    Return
End If

success = cert.SetCloudSigner(cloud)
If (success <> True) Then
    System.DebugLog("Could not configure the Key Vault signer; inspect diagnostics privately.")
    Return
End If

//  Create the XML to be signed.
Dim sbXml As New Chilkat.StringBuilder

Dim bCrlf As Boolean
bCrlf = True
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)

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
    System.DebugLog("Could not add the SOAP Body reference.")
    Return
End If

success = gen.SetX509Cert(cert,True)
If (success <> True) Then
    System.DebugLog("Could not select signing certificate.")
    Return
End If

success = gen.CreateXmlDSigSb(sbXml)
If (success <> True) Then
    System.DebugLog("XML signing failed; inspect diagnostics privately.")
    Return
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
    System.DebugLog("Could not load generated signature.")
    Return
End If

success = verifier.VerifySignature(True)
If (success <> True) Then
    System.DebugLog("Generated XML signature or reference verification failed.")
    Return
End If

success = sbXml.WriteFile("qa_output/azure_signed.xml","utf-8",False)
If (success <> True) Then
    System.DebugLog("Could not save signed XML.")
    Return
End If

System.DebugLog("Verified XMLDSig saved to qa_output/azure_signed.xml")