Sample code for 30+ languages & platforms
Unicode C

Sign with BinarySecurityToken

See more XML Digital Signatures Examples

Demonstrates creating an XML signature using a BinarySecurityToken.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkCertW cert;
    HCkXmlW xml;
    HCkDateTimeW dt;
    HCkBinDataW bdCert;
    HCkXmlW keyInfoXml;
    HCkXmlDSigGenW gen;
    HCkStringBuilderW sbXml;

    success = FALSE;

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

    // -------------------------------------------------------------------------
    // Load a cert + private key from a PFX.
    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));
        CkCertW_Dispose(cert);
        return;
    }

    // -------------------------------------------------------------------------
    // Create the XML that is to be signed.
    // 
    // The XML we're creating can be found at Sample Pre-Signed XML with BinarySecurityToken
    // The online tool at http://tools.chilkat.io/xmlCreate.cshtml can be used to generate the following XML creation code.
    // 

    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"S:Envelope");
    CkXmlW_AddAttribute(xml,L"xmlns:S",L"http://www.w3.org/2003/05/soap-envelope");
    CkXmlW_AddAttribute(xml,L"xmlns:wsse11",L"http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd");
    CkXmlW_AddAttribute(xml,L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    CkXmlW_AddAttribute(xml,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXmlW_AddAttribute(xml,L"xmlns:xs",L"http://www.w3.org/2001/XMLSchema");
    CkXmlW_AddAttribute(xml,L"xmlns:ds",L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlW_AddAttribute(xml,L"xmlns:exc14n",L"http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|To",TRUE,L"xmlns",L"http://www.w3.org/2005/08/addressing");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|To",TRUE,L"wsu:Id",L"_5002");
    CkXmlW_UpdateChildContent(xml,L"S:Header|To",L"https://XXXXXXXXX");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|Action",TRUE,L"xmlns",L"http://www.w3.org/2005/08/addressing");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|Action",TRUE,L"xmlns:S",L"http://www.w3.org/2003/05/soap-envelope");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|Action",TRUE,L"S:mustUnderstand",L"true");
    CkXmlW_UpdateChildContent(xml,L"S:Header|Action",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|ReplyTo",TRUE,L"xmlns",L"http://www.w3.org/2005/08/addressing");
    CkXmlW_UpdateChildContent(xml,L"S:Header|ReplyTo|Address",L"http://www.w3.org/2005/08/addressing/anonymous");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|FaultTo",TRUE,L"xmlns",L"http://www.w3.org/2005/08/addressing");
    CkXmlW_UpdateChildContent(xml,L"S:Header|FaultTo|Address",L"http://www.w3.org/2005/08/addressing/anonymous");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|MessageID",TRUE,L"xmlns",L"http://www.w3.org/2005/08/addressing");
    CkXmlW_UpdateChildContent(xml,L"S:Header|MessageID",L"uuid:e9033251-4ff0-4618-8baf-4952ab5fd207");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|wsse:Security",TRUE,L"S:mustUnderstand",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|wsse:Security|wsu:Timestamp",TRUE,L"xmlns:ns17",L"http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|wsse:Security|wsu:Timestamp",TRUE,L"xmlns:ns16",L"http://schemas.xmlsoap.org/soap/envelope/");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|wsse:Security|wsu:Timestamp",TRUE,L"wsu:Id",L"_1");

    // Get the current date/time in timestamp format, such as "2018-05-23T02:38:27Z"
    dt = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(dt);
    CkXmlW_UpdateChildContent(xml,L"S:Header|wsse:Security|wsu:Timestamp|wsu:Created",CkDateTimeW_getAsTimestamp(dt,FALSE));
    // Add 5 minutes.
    CkDateTimeW_AddSeconds(dt,300);
    CkXmlW_UpdateChildContent(xml,L"S:Header|wsse:Security|wsu:Timestamp|wsu:Expires",CkDateTimeW_getAsTimestamp(dt,FALSE));

    CkXmlW_UpdateAttrAt(xml,L"S:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"xmlns:ns17",L"http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"xmlns:ns16",L"http://schemas.xmlsoap.org/soap/envelope/");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"EncodingType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
    CkXmlW_UpdateAttrAt(xml,L"S:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"wsu:Id",L"uuid_43470044-78b4-4b23-926a-b7f590d24cb8");

    bdCert = CkBinDataW_Create();
    CkCertW_ExportCertDerBd(cert,bdCert);

    // Get the cert as base64 on one line.
    CkXmlW_UpdateChildContent(xml,L"S:Header|wsse:Security|wsse:BinarySecurityToken",CkBinDataW_getEncoded(bdCert,L"base64"));
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken",TRUE,L"xmlns",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512");
    CkXmlW_UpdateChildContent(xml,L"S:Body|RequestSecurityToken|RequestType",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|wsp:AppliesTo",TRUE,L"xmlns:wsp",L"http://schemas.xmlsoap.org/ws/2004/09/policy");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference",TRUE,L"xmlns:EndpointReference",L"http://www.w3.org/2005/08/addressing");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference",TRUE,L"xmlns",L"http://www.w3.org/2005/08/addressing");
    CkXmlW_UpdateChildContent(xml,L"S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference|Address",L"https://XXXXXXXXX/services");
    CkXmlW_UpdateChildContent(xml,L"S:Body|RequestSecurityToken|TokenType",L"http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims",TRUE,L"xmlns:i",L"http://schemas.xmlsoap.org/ws/2005/05/identity");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims",TRUE,L"Dialect",L"http://schemas.xmlsoap.org/ws/2005/05/identity");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType",TRUE,L"Optional",L"false");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/abn");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[1]",TRUE,L"Optional",L"false");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[1]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/commonname");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[2]",TRUE,L"Optional",L"false");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[2]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/credentialtype");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[3]",TRUE,L"Optional",L"false");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[3]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/samlsubjectid");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[4]",TRUE,L"Optional",L"false");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[4]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/fingerprint");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[5]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[5]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/sbr_personid");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[6]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[6]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/givennames");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[7]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[7]",TRUE,L"Uri",L"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[8]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[8]",TRUE,L"Uri",L"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[9]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[9]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/credentialadministrator");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[10]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[10]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/stalecrlminutes");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[11]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[11]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/subjectdn");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[12]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[12]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/issuerdn");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[13]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[13]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/notafterdate");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[14]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[14]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/certificateserialnumber");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[15]",TRUE,L"Optional",L"true");
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Claims|i:ClaimType[15]",TRUE,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/previoussubject");

    CkDateTimeW_SetFromCurrentSystemTime(dt);
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Lifetime|wsu:Created",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXmlW_UpdateChildContent(xml,L"S:Body|RequestSecurityToken|Lifetime|wsu:Created",CkDateTimeW_getAsTimestamp(dt,FALSE));

    // Add 40 minutes.
    CkDateTimeW_AddSeconds(dt,2400);
    CkXmlW_UpdateAttrAt(xml,L"S:Body|RequestSecurityToken|Lifetime|wsu:Expires",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXmlW_UpdateChildContent(xml,L"S:Body|RequestSecurityToken|Lifetime|wsu:Expires",CkDateTimeW_getAsTimestamp(dt,FALSE));

    CkXmlW_UpdateChildContent(xml,L"S:Body|RequestSecurityToken|KeyType",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey");
    CkXmlW_UpdateChildContent(xml,L"S:Body|RequestSecurityToken|KeySize",L"512");

    // Examine the pre-signed XML
    // println xml.GetXml();

    // Build the custom KeyInfo XML we'll use:
    // 
    // 	    <wsse:SecurityTokenReference>
    // 	        <wsse:Reference URI="#uuid_43470044-78b4-4b23-926a-b7f590d24cb8" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" /></wsse:SecurityTokenReference>

    keyInfoXml = CkXmlW_Create();
    CkXmlW_putTag(keyInfoXml,L"wsse:SecurityTokenReference");
    CkXmlW_UpdateAttrAt(keyInfoXml,L"wsse:Reference",TRUE,L"URI",L"#uuid_43470044-78b4-4b23-926a-b7f590d24cb8");
    CkXmlW_UpdateAttrAt(keyInfoXml,L"wsse:Reference",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");

    // -------------------------------------------------------------------------
    // Setup the XML Digital Signature Generator and add the XML Signature.
    // 
    gen = CkXmlDSigGenW_Create();
    CkXmlDSigGenW_putSigLocation(gen,L"S:Envelope|S:Header|wsse:Security");
    CkXmlDSigGenW_putSignedInfoPrefixList(gen,L"wsse S");

    CkXmlDSigGenW_AddSameDocRef(gen,L"_1",L"sha1",L"EXCL_C14N",L"wsu wsse S",L"");
    CkXmlDSigGenW_AddSameDocRef(gen,L"_5002",L"sha1",L"EXCL_C14N",L"S",L"");

    CkXmlDSigGenW_putKeyInfoType(gen,L"Custom");
    CkXmlW_putEmitXmlDecl(keyInfoXml,FALSE);
    CkXmlDSigGenW_putCustomKeyInfoXml(gen,CkXmlW_getXml(keyInfoXml));

    // Specify the cert for signing (which has a private key because it was loaded from a PFX)
    CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);

    // Indicated we want an indented signature for easier human reading.
    CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature");

    // Sign the XML..
    sbXml = CkStringBuilderW_Create();
    CkXmlW_GetXmlSb(xml,sbXml);

    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkCertW_Dispose(cert);
        CkXmlW_Dispose(xml);
        CkDateTimeW_Dispose(dt);
        CkBinDataW_Dispose(bdCert);
        CkXmlW_Dispose(keyInfoXml);
        CkXmlDSigGenW_Dispose(gen);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    // Examine the signed XML
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml));

    // The resulting signature (extracted from the surrounding XML) looks something like this:

    // <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    //     <ds:SignedInfo>
    //         <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
    //             <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="wsse S" />
    //         </ds:CanonicalizationMethod>
    //         <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
    //         <ds:Reference URI="#_1">
    //             <ds:Transforms>
    //                 <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
    //                     <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="wsu wsse S" />
    //                 </ds:Transform>
    //             </ds:Transforms>
    //             <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
    //             <ds:DigestValue>VAJMC/L/BDvml7Qv5CBMePbKDE8=</ds:DigestValue>
    //         </ds:Reference>
    //         <ds:Reference URI="#_5002">
    //             <ds:Transforms>
    //                 <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
    //                     <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="S" />
    //                 </ds:Transform>
    //             </ds:Transforms>
    //             <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
    //             <ds:DigestValue>sW/QFsk6kGv1dzeu0H9Qc/2kvAQ=</ds:DigestValue>
    //         </ds:Reference>
    //     </ds:SignedInfo>
    //     <ds:SignatureValue>....</ds:SignatureValue>
    //     <ds:KeyInfo>
    //         <wsse:SecurityTokenReference>
    //             <wsse:Reference URI="#uuid_43470044-78b4-4b23-926a-b7f590d24cb8" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" />
    //         </wsse:SecurityTokenReference>
    //     </ds:KeyInfo>
    // </ds:Signature>
    // 


    CkCertW_Dispose(cert);
    CkXmlW_Dispose(xml);
    CkDateTimeW_Dispose(dt);
    CkBinDataW_Dispose(bdCert);
    CkXmlW_Dispose(keyInfoXml);
    CkXmlDSigGenW_Dispose(gen);
    CkStringBuilderW_Dispose(sbXml);

    }