Sample code for 30+ languages & platforms
Unicode C

Sign SOAP XML for New Zealand Customs Service

See more XAdES Examples

Demonstrates how to create an XAdES signed SOAP XML pertaining to the New Zealand Customs Service.

Note: This example requires Chilkat v9.5.0.96 or later.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkStringBuilderW.h>
#include <C_CkDateTimeW.h>
#include <C_CkXmlW.h>
#include <C_CkXmlDSigGenW.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW tsId;
    HCkStringBuilderW strId;
    HCkStringBuilderW keyInfoId;
    HCkDateTimeW dt;
    HCkStringBuilderW sbNow;
    int n;
    HCkStringBuilderW sbNowPlusOneHour;
    HCkXmlW xmlToSign;
    HCkXmlDSigGenW gen;
    HCkXmlW xml1;
    HCkXmlW xml2;
    HCkCertW cert;
    HCkXmlW xmlCustomKeyInfo;
    HCkStringBuilderW sbXml;

    success = FALSE;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    success = TRUE;

    // Create the following XML to be signed:

    // <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    //     xmlns:v1="http://customs.govt.nz/jbms/msggate/reqresp/v1">
    //     <soapenv:Header>
    //         <wsse:Security soapenv:mustUnderstand="1"
    //             xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    //             xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    //             <wsu:Timestamp wsu:Id="TS-037E78514E9B9132CB16817563559151">
    //                 <wsu:Created>2023-04-17T18:32:35.913Z</wsu:Created>
    //                 <wsu:Expires>2023-04-17T19:32:35.913Z</wsu:Expires>
    //             </wsu:Timestamp>
    //         </wsse:Security>
    //     </soapenv:Header>
    //     <soapenv:Body wsu:Id="id-8"
    //         xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    //         <v1:RequestResponse>
    //             <v1:Submitter>TEST1234</v1:Submitter>
    //             <v1:MailboxMsgId>999999</v1:MailboxMsgId>
    //         </v1:RequestResponse>
    //     </soapenv:Body>
    // </soapenv:Envelope>

    // Create a random ID like this: TS-037E78514E9B9132CB16817563559151
    tsId = CkStringBuilderW_Create();
    CkStringBuilderW_Append(tsId,L"TS-");
    CkStringBuilderW_AppendRandom(tsId,16,L"hex");

    // STR-037E78514E9B9132CB16817563559614
    strId = CkStringBuilderW_Create();
    CkStringBuilderW_Append(strId,L"STR-");
    CkStringBuilderW_AppendRandom(strId,16,L"hex");

    // KI-037E78514E9B9132CB16817563559583
    keyInfoId = CkStringBuilderW_Create();
    CkStringBuilderW_Append(keyInfoId,L"KI-");
    CkStringBuilderW_AppendRandom(keyInfoId,16,L"hex");

    // Create a date/time for the current time with this format:  2023-04-17T18:32:35.913Z
    dt = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(dt);

    sbNow = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbNow,CkDateTimeW_getAsTimestamp(dt,FALSE));
    // If we really need the milliseconds, we can replace the "Z" with ".000Z"
    // The server will also likely accept a timestamp without milliseconds, such as 2023-04-17T18:32:35Z
    n = CkStringBuilderW_Replace(sbNow,L"Z",L".000Z");

    sbNowPlusOneHour = CkStringBuilderW_Create();
    CkDateTimeW_AddSeconds(dt,3600);
    CkStringBuilderW_Append(sbNowPlusOneHour,CkDateTimeW_getAsTimestamp(dt,FALSE));
    n = CkStringBuilderW_Replace(sbNowPlusOneHour,L"Z",L".000Z");

    xmlToSign = CkXmlW_Create();
    CkXmlW_putTag(xmlToSign,L"soapenv:Envelope");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:soapenv",L"http://schemas.xmlsoap.org/soap/envelope/");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:v1",L"http://customs.govt.nz/jbms/msggate/reqresp/v1");
    CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security",TRUE,L"soapenv:mustUnderstand",L"1");
    CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security",TRUE,L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security|wsu:Timestamp",TRUE,L"wsu:Id",CkStringBuilderW_getAsString(tsId));
    CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created",CkStringBuilderW_getAsString(sbNow));
    CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires",CkStringBuilderW_getAsString(sbNowPlusOneHour));
    CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body",TRUE,L"wsu:Id",L"id-8");
    CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Body|v1:RequestResponse|v1:Submitter",L"TEST1234");
    CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Body|v1:RequestResponse|v1:MailboxMsgId",L"999999");

    gen = CkXmlDSigGenW_Create();

    CkXmlDSigGenW_putSigLocation(gen,L"soapenv:Envelope|soapenv:Header|wsse:Security");
    CkXmlDSigGenW_putSigLocationMod(gen,0);
    CkXmlDSigGenW_putSigId(gen,L"SIG-037E78514E9B9132CB16817563559695");
    CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds");
    CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGenW_putSignedInfoPrefixList(gen,L"soapenv v1");
    CkXmlDSigGenW_putIncNamespacePrefix(gen,L"ec");
    CkXmlDSigGenW_putIncNamespaceUri(gen,L"http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N");
    CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256");

    // Set the KeyInfoId before adding references..
    CkXmlDSigGenW_putKeyInfoId(gen,CkStringBuilderW_getAsString(keyInfoId));

    // -------- Reference 1 --------
    xml1 = CkXmlW_Create();
    CkXmlW_putTag(xml1,L"ds:Transforms");
    CkXmlW_UpdateAttrAt(xml1,L"ds:Transform",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXmlW_UpdateAttrAt(xml1,L"ds:Transform|ec:InclusiveNamespaces",TRUE,L"PrefixList",L"wsse soapenv v1");
    CkXmlW_UpdateAttrAt(xml1,L"ds:Transform|ec:InclusiveNamespaces",TRUE,L"xmlns:ec",L"http://www.w3.org/2001/10/xml-exc-c14n#");

    CkXmlDSigGenW_AddSameDocRef2(gen,CkStringBuilderW_getAsString(tsId),L"sha256",xml1,L"");

    // -------- Reference 2 --------
    xml2 = CkXmlW_Create();
    CkXmlW_putTag(xml2,L"ds:Transforms");
    CkXmlW_UpdateAttrAt(xml2,L"ds:Transform",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXmlW_UpdateAttrAt(xml2,L"ds:Transform|ec:InclusiveNamespaces",TRUE,L"PrefixList",L"v1");
    CkXmlW_UpdateAttrAt(xml2,L"ds:Transform|ec:InclusiveNamespaces",TRUE,L"xmlns:ec",L"http://www.w3.org/2001/10/xml-exc-c14n#");

    CkXmlDSigGenW_AddSameDocRef2(gen,L"id-8",L"sha256",xml2,L"");

    // Provide a certificate + private key. (PFX password is test123)
    cert = CkCertW_Create();
    success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/cert_test123.pfx",L"test123");
    if (success != TRUE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkStringBuilderW_Dispose(tsId);
        CkStringBuilderW_Dispose(strId);
        CkStringBuilderW_Dispose(keyInfoId);
        CkDateTimeW_Dispose(dt);
        CkStringBuilderW_Dispose(sbNow);
        CkStringBuilderW_Dispose(sbNowPlusOneHour);
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(xml1);
        CkXmlW_Dispose(xml2);
        CkCertW_Dispose(cert);
        return;
    }

    CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGenW_putKeyInfoType(gen,L"Custom");

    // Create the custom KeyInfo XML..
    xmlCustomKeyInfo = CkXmlW_Create();
    CkXmlW_putTag(xmlCustomKeyInfo,L"wsse:SecurityTokenReference");
    CkXmlW_AddAttribute(xmlCustomKeyInfo,L"wsu:Id",CkStringBuilderW_getAsString(strId));
    CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:KeyIdentifier",TRUE,L"EncodingType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
    CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:KeyIdentifier",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
    // Insert the single-line base64 of the signing certificate's DER
    CkCertW_putUncommonOptions(cert,L"Base64CertNoCRLF");
    CkXmlW_UpdateChildContent(xmlCustomKeyInfo,L"wsse:KeyIdentifier",CkCertW_getEncoded(cert));

    CkXmlW_putEmitXmlDecl(xmlCustomKeyInfo,FALSE);
    CkXmlDSigGenW_putCustomKeyInfoXml(gen,CkXmlW_getXml(xmlCustomKeyInfo));

    // Load XML to be signed...
    sbXml = CkStringBuilderW_Create();
    CkXmlW_GetXmlSb(xmlToSign,sbXml);

    CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature");

    // Sign the XML...
    CkXmlDSigGenW_putVerboseLogging(gen,TRUE);
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkStringBuilderW_Dispose(tsId);
        CkStringBuilderW_Dispose(strId);
        CkStringBuilderW_Dispose(keyInfoId);
        CkDateTimeW_Dispose(dt);
        CkStringBuilderW_Dispose(sbNow);
        CkStringBuilderW_Dispose(sbNowPlusOneHour);
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(xml1);
        CkXmlW_Dispose(xml2);
        CkCertW_Dispose(cert);
        CkXmlW_Dispose(xmlCustomKeyInfo);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    // Save the signed XML to a file.
    success = CkStringBuilderW_WriteFile(sbXml,L"c:/temp/qa_output/signedXml.xml",L"utf-8",FALSE);

    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml));


    CkStringBuilderW_Dispose(tsId);
    CkStringBuilderW_Dispose(strId);
    CkStringBuilderW_Dispose(keyInfoId);
    CkDateTimeW_Dispose(dt);
    CkStringBuilderW_Dispose(sbNow);
    CkStringBuilderW_Dispose(sbNowPlusOneHour);
    CkXmlW_Dispose(xmlToSign);
    CkXmlDSigGenW_Dispose(gen);
    CkXmlW_Dispose(xml1);
    CkXmlW_Dispose(xml2);
    CkCertW_Dispose(cert);
    CkXmlW_Dispose(xmlCustomKeyInfo);
    CkStringBuilderW_Dispose(sbXml);

    }