Sample code for 30+ languages & platforms
C++ 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 C++ Downloads

C++
#include <CkStringBuilder.h>
#include <CkJsonObject.h>
#include <CkCert.h>
#include <CkXmlDSigGen.h>
#include <CkXmlDSig.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkStringBuilder stringCompare;
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Keep this configuration private, outside public web content and source control.
    CkJsonObject config;
    config.put_Strict(true);
    success = config.LoadFile("qa_data/azure/keyvault.json");
    if (success != true) {
        std::cout << "Could not load private Key Vault configuration." << "\r\n";
        return;
    }

    stringCompare.SetString(config.stringOf("vault_name"));
    bool stringMatch1 = stringCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("vault_name") != 1) || (stringMatch1 == true)) {
        std::cout << "Missing or invalid vault_name." << "\r\n";
        return;
    }

    CkStringBuilder validate1;
    CkStringBuilder alphabet1;
    success = validate1.Append(config.stringOf("vault_name"));
    if (success != true) {
        std::cout << "Could not validate vault_name." << "\r\n";
        return;
    }

    success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-");
    if (success != true) {
        std::cout << "Could not initialize validation." << "\r\n";
        return;
    }

    if ((validate1.get_Length() < 3) || (validate1.get_Length() > 24)) {
        std::cout << "Invalid vault_name length." << "\r\n";
        return;
    }

    int charIndex1 = 0;
    while ((charIndex1 < validate1.get_Length())) {
        if (alphabet1.Contains(validate1.getRange(charIndex1,1,false),true) != true) {
            std::cout << "Invalid character in vault_name." << "\r\n";
            return;
        }

        charIndex1 = charIndex1 + 1;
    }

    CkStringBuilder vaultName;
    vaultName.Append(config.stringOf("vault_name"));
    CkStringBuilder letters;
    letters.Append("abcdefghijklmnopqrstuvwxyz");
    if ((letters.Contains(vaultName.getRange(0,1,false),true) != true) || (vaultName.EndsWith("-",true) == true) || (vaultName.Contains("--",true) == true)) {
        std::cout << "Invalid vault_name." << "\r\n";
        return;
    }

    CkStringBuilder origin;
    success = origin.Append("https://");
    if (success != true) {
        std::cout << "Could not construct URL or identifier." << "\r\n";
        return;
    }

    success = origin.Append(config.stringOf("vault_name"));
    if (success != true) {
        std::cout << "Could not construct URL or identifier." << "\r\n";
        return;
    }

    success = origin.Append(".vault.azure.net");
    if (success != true) {
        std::cout << "Could not construct URL or identifier." << "\r\n";
        return;
    }

    stringCompare.SetString(config.stringOf("cert_name"));
    bool stringMatch2 = stringCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("cert_name") != 1) || (stringMatch2 == true)) {
        std::cout << "Missing or invalid cert_name." << "\r\n";
        return;
    }

    CkStringBuilder validate2;
    CkStringBuilder alphabet2;
    success = validate2.Append(config.stringOf("cert_name"));
    if (success != true) {
        std::cout << "Could not validate cert_name." << "\r\n";
        return;
    }

    success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
    if (success != true) {
        std::cout << "Could not initialize validation." << "\r\n";
        return;
    }

    if ((validate2.get_Length() < 1) || (validate2.get_Length() > 127)) {
        std::cout << "Invalid cert_name length." << "\r\n";
        return;
    }

    int charIndex2 = 0;
    while ((charIndex2 < validate2.get_Length())) {
        if (alphabet2.Contains(validate2.getRange(charIndex2,1,false),true) != true) {
            std::cout << "Invalid character in cert_name." << "\r\n";
            return;
        }

        charIndex2 = charIndex2 + 1;
    }

    stringCompare.SetString(config.stringOf("cert_version"));
    bool stringMatch3 = stringCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("cert_version") != 1) || (stringMatch3 == true)) {
        std::cout << "Missing or invalid cert_version." << "\r\n";
        return;
    }

    CkStringBuilder validate3;
    CkStringBuilder alphabet3;
    success = validate3.Append(config.stringOf("cert_version"));
    if (success != true) {
        std::cout << "Could not validate cert_version." << "\r\n";
        return;
    }

    success = alphabet3.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
    if (success != true) {
        std::cout << "Could not initialize validation." << "\r\n";
        return;
    }

    if ((validate3.get_Length() < 1) || (validate3.get_Length() > 127)) {
        std::cout << "Invalid cert_version length." << "\r\n";
        return;
    }

    int charIndex3 = 0;
    while ((charIndex3 < validate3.get_Length())) {
        if (alphabet3.Contains(validate3.getRange(charIndex3,1,false),true) != true) {
            std::cout << "Invalid character in cert_version." << "\r\n";
            return;
        }

        charIndex3 = charIndex3 + 1;
    }

    stringCompare.SetString(config.stringOf("client_id"));
    bool stringMatch4 = stringCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("client_id") != 1) || (stringMatch4 == true)) {
        std::cout << "Missing or invalid client_id." << "\r\n";
        return;
    }

    stringCompare.SetString(config.stringOf("client_secret"));
    bool stringMatch5 = stringCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("client_secret") != 1) || (stringMatch5 == true)) {
        std::cout << "Missing or invalid client_secret." << "\r\n";
        return;
    }

    stringCompare.SetString(config.stringOf("tenant_id"));
    bool stringMatch6 = stringCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("tenant_id") != 1) || (stringMatch6 == true)) {
        std::cout << "Missing or invalid tenant_id." << "\r\n";
        return;
    }

    CkStringBuilder validate4;
    CkStringBuilder alphabet4;
    success = validate4.Append(config.stringOf("tenant_id"));
    if (success != true) {
        std::cout << "Could not validate tenant GUID." << "\r\n";
        return;
    }

    success = alphabet4.Append("0123456789abcdefABCDEF-");
    if (success != true) {
        std::cout << "Could not initialize validation." << "\r\n";
        return;
    }

    if ((validate4.get_Length() < 36) || (validate4.get_Length() > 36)) {
        std::cout << "Invalid tenant GUID length." << "\r\n";
        return;
    }

    int charIndex4 = 0;
    while ((charIndex4 < validate4.get_Length())) {
        if (alphabet4.Contains(validate4.getRange(charIndex4,1,false),true) != true) {
            std::cout << "Invalid character in tenant GUID." << "\r\n";
            return;
        }

        charIndex4 = charIndex4 + 1;
    }

    CkCert cert;
    success = cert.LoadFromFile("qa_data/certs/myCert.cer");
    if (success != true) {
        std::cout << "Could not load the public signing certificate." << "\r\n";
        return;
    }

    CkJsonObject cloud;
    success = cloud.UpdateString("service","azure_keyvault");
    if (success != true) {
        std::cout << "Could not set service." << "\r\n";
        return;
    }

    success = cloud.UpdateString("client_id",config.stringOf("client_id"));
    if (success != true) {
        std::cout << "Could not set client_id." << "\r\n";
        return;
    }

    success = cloud.UpdateString("client_secret",config.stringOf("client_secret"));
    if (success != true) {
        std::cout << "Could not set client_secret." << "\r\n";
        return;
    }

    success = cloud.UpdateString("tenant_id",config.stringOf("tenant_id"));
    if (success != true) {
        std::cout << "Could not set tenant_id." << "\r\n";
        return;
    }

    success = cloud.UpdateString("vault_name",config.stringOf("vault_name"));
    if (success != true) {
        std::cout << "Could not set vault_name." << "\r\n";
        return;
    }

    success = cloud.UpdateString("cert_name",config.stringOf("cert_name"));
    if (success != true) {
        std::cout << "Could not set cert_name." << "\r\n";
        return;
    }

    success = cloud.UpdateString("cert_version",config.stringOf("cert_version"));
    if (success != true) {
        std::cout << "Could not set cert_version." << "\r\n";
        return;
    }

    success = cert.SetCloudSigner(cloud);
    if (success != true) {
        std::cout << "Could not configure the Key Vault signer; inspect diagnostics privately." << "\r\n";
        return;
    }

    //  Create the XML to be signed.
    CkStringBuilder sbXml;

    bool 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;
    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) {
        std::cout << "Could not add the SOAP Body reference." << "\r\n";
        return;
    }

    success = gen.SetX509Cert(cert,true);
    if (success != true) {
        std::cout << "Could not select signing certificate." << "\r\n";
        return;
    }

    success = gen.CreateXmlDSigSb(sbXml);
    if (success != true) {
        std::cout << "XML signing failed; inspect diagnostics privately." << "\r\n";
        return;
    }

    //  Verify the signature and references before saving. This does not validate trust.
    CkXmlDSig verifier;
    success = verifier.LoadSignatureSb(sbXml);
    if (success != true) {
        std::cout << "Could not load generated signature." << "\r\n";
        return;
    }

    success = verifier.VerifySignature(true);
    if (success != true) {
        std::cout << "Generated XML signature or reference verification failed." << "\r\n";
        return;
    }

    success = sbXml.WriteFile("qa_output/azure_signed.xml","utf-8",false);
    if (success != true) {
        std::cout << "Could not save signed XML." << "\r\n";
        return;
    }

    std::cout << "Verified XMLDSig saved to qa_output/azure_signed.xml" << "\r\n";
    }