Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Node.js Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Node.js) Sign with BinarySecurityToken

Demonstrates creating an XML signature using a BinarySecurityToken.

Install Chilkat for Node.js and Electron using npm at

Chilkat npm packages for Node.js

Chilkat npm packages for Electron

on Windows, Linux, MacOSX, and ARM

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node21-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node21-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node21-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node21-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node21-linux64');
    }
} else if (os.platform() == 'darwin') {
    if (os.arch() == 'arm64') {
        var chilkat = require('@chilkat/ck-node21-mac-m1');
    } else {
        var chilkat = require('@chilkat/ck-node21-macosx');
    }
}

function chilkatExample() {

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

    var success;

    // -------------------------------------------------------------------------
    // Load a cert + private key from a PFX.
    var cert = new chilkat.Cert();
    success = cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123");
    if (success !== true) {
        console.log(cert.LastErrorText);
        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.
    // 

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

    // Get the current date/time in timestamp format, such as "2018-05-23T02:38:27Z"
    var dt = new chilkat.CkDateTime();
    dt.SetFromCurrentSystemTime();
    xml.UpdateChildContent("S:Header|wsse:Security|wsu:Timestamp|wsu:Created",dt.GetAsTimestamp(false));
    // Add 5 minutes.
    dt.AddSeconds(300);
    xml.UpdateChildContent("S:Header|wsse:Security|wsu:Timestamp|wsu:Expires",dt.GetAsTimestamp(false));

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

    var bdCert = new chilkat.BinData();
    cert.ExportCertDerBd(bdCert);

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

    dt.SetFromCurrentSystemTime();
    xml.UpdateAttrAt("S:Body|RequestSecurityToken|Lifetime|wsu:Created",true,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    xml.UpdateChildContent("S:Body|RequestSecurityToken|Lifetime|wsu:Created",dt.GetAsTimestamp(false));

    // Add 40 minutes.
    dt.AddSeconds(2400);
    xml.UpdateAttrAt("S:Body|RequestSecurityToken|Lifetime|wsu:Expires",true,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    xml.UpdateChildContent("S:Body|RequestSecurityToken|Lifetime|wsu:Expires",dt.GetAsTimestamp(false));

    xml.UpdateChildContent("S:Body|RequestSecurityToken|KeyType","http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey");
    xml.UpdateChildContent("S:Body|RequestSecurityToken|KeySize","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>

    var keyInfoXml = new chilkat.Xml();
    keyInfoXml.Tag = "wsse:SecurityTokenReference";
    keyInfoXml.UpdateAttrAt("wsse:Reference",true,"URI","#uuid_43470044-78b4-4b23-926a-b7f590d24cb8");
    keyInfoXml.UpdateAttrAt("wsse:Reference",true,"ValueType","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.
    // 
    var gen = new chilkat.XmlDSigGen();
    gen.SigLocation = "S:Envelope|S:Header|wsse:Security";
    gen.SignedInfoPrefixList = "wsse S";

    gen.AddSameDocRef("_1","sha1","EXCL_C14N","wsu wsse S","");
    gen.AddSameDocRef("_5002","sha1","EXCL_C14N","S","");

    gen.KeyInfoType = "Custom";
    keyInfoXml.EmitXmlDecl = false;
    gen.CustomKeyInfoXml = keyInfoXml.GetXml();

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

    // Indicated we want an indented signature for easier human reading.
    gen.Behaviors = "IndentedSignature";

    // Sign the XML..
    var sbXml = new chilkat.StringBuilder();
    xml.GetXmlSb(sbXml);

    success = gen.CreateXmlDSigSb(sbXml);
    if (success !== true) {
        console.log(gen.LastErrorText);
        return;
    }

    // Examine the signed XML
    console.log(sbXml.GetAsString());

    // 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>
    // 

}

chilkatExample();

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.