Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C++) Sign with BinarySecurityTokenDemonstrates creating an XML signature using a BinarySecurityToken.
#include <CkCertW.h> #include <CkXmlW.h> #include <CkDateTimeW.h> #include <CkBinDataW.h> #include <CkXmlDSigGenW.h> #include <CkStringBuilderW.h> void ChilkatSample(void) { // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. bool success; // ------------------------------------------------------------------------- // Load a cert + private key from a PFX. CkCertW cert; success = cert.LoadPfxFile(L"qa_data/pfx/cert_test123.pfx",L"test123"); if (success != true) { wprintf(L"%s\n",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. // CkXmlW xml; xml.put_Tag(L"S:Envelope"); xml.AddAttribute(L"xmlns:S",L"http://www.w3.org/2003/05/soap-envelope"); xml.AddAttribute(L"xmlns:wsse11",L"http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd"); xml.AddAttribute(L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); xml.AddAttribute(L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); xml.AddAttribute(L"xmlns:xs",L"http://www.w3.org/2001/XMLSchema"); xml.AddAttribute(L"xmlns:ds",L"http://www.w3.org/2000/09/xmldsig#"); xml.AddAttribute(L"xmlns:exc14n",L"http://www.w3.org/2001/10/xml-exc-c14n#"); xml.UpdateAttrAt(L"S:Header|To",true,L"xmlns",L"http://www.w3.org/2005/08/addressing"); xml.UpdateAttrAt(L"S:Header|To",true,L"wsu:Id",L"_5002"); xml.UpdateChildContent(L"S:Header|To",L"https://XXXXXXXXX"); xml.UpdateAttrAt(L"S:Header|Action",true,L"xmlns",L"http://www.w3.org/2005/08/addressing"); xml.UpdateAttrAt(L"S:Header|Action",true,L"xmlns:S",L"http://www.w3.org/2003/05/soap-envelope"); xml.UpdateAttrAt(L"S:Header|Action",true,L"S:mustUnderstand",L"true"); xml.UpdateChildContent(L"S:Header|Action",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue"); xml.UpdateAttrAt(L"S:Header|ReplyTo",true,L"xmlns",L"http://www.w3.org/2005/08/addressing"); xml.UpdateChildContent(L"S:Header|ReplyTo|Address",L"http://www.w3.org/2005/08/addressing/anonymous"); xml.UpdateAttrAt(L"S:Header|FaultTo",true,L"xmlns",L"http://www.w3.org/2005/08/addressing"); xml.UpdateChildContent(L"S:Header|FaultTo|Address",L"http://www.w3.org/2005/08/addressing/anonymous"); xml.UpdateAttrAt(L"S:Header|MessageID",true,L"xmlns",L"http://www.w3.org/2005/08/addressing"); xml.UpdateChildContent(L"S:Header|MessageID",L"uuid:e9033251-4ff0-4618-8baf-4952ab5fd207"); xml.UpdateAttrAt(L"S:Header|wsse:Security",true,L"S:mustUnderstand",L"true"); xml.UpdateAttrAt(L"S:Header|wsse:Security|wsu:Timestamp",true,L"xmlns:ns17",L"http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512"); xml.UpdateAttrAt(L"S:Header|wsse:Security|wsu:Timestamp",true,L"xmlns:ns16",L"http://schemas.xmlsoap.org/soap/envelope/"); xml.UpdateAttrAt(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" CkDateTimeW dt; dt.SetFromCurrentSystemTime(); xml.UpdateChildContent(L"S:Header|wsse:Security|wsu:Timestamp|wsu:Created",dt.getAsTimestamp(false)); // Add 5 minutes. dt.AddSeconds(300); xml.UpdateChildContent(L"S:Header|wsse:Security|wsu:Timestamp|wsu:Expires",dt.getAsTimestamp(false)); xml.UpdateAttrAt(L"S:Header|wsse:Security|wsse:BinarySecurityToken",true,L"xmlns:ns17",L"http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512"); xml.UpdateAttrAt(L"S:Header|wsse:Security|wsse:BinarySecurityToken",true,L"xmlns:ns16",L"http://schemas.xmlsoap.org/soap/envelope/"); xml.UpdateAttrAt(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"); xml.UpdateAttrAt(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"); xml.UpdateAttrAt(L"S:Header|wsse:Security|wsse:BinarySecurityToken",true,L"wsu:Id",L"uuid_43470044-78b4-4b23-926a-b7f590d24cb8"); CkBinDataW bdCert; cert.ExportCertDerBd(bdCert); // Get the cert as base64 on one line. xml.UpdateChildContent(L"S:Header|wsse:Security|wsse:BinarySecurityToken",bdCert.getEncoded(L"base64")); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken",true,L"xmlns",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512"); xml.UpdateChildContent(L"S:Body|RequestSecurityToken|RequestType",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|wsp:AppliesTo",true,L"xmlns:wsp",L"http://schemas.xmlsoap.org/ws/2004/09/policy"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference",true,L"xmlns:EndpointReference",L"http://www.w3.org/2005/08/addressing"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference",true,L"xmlns",L"http://www.w3.org/2005/08/addressing"); xml.UpdateChildContent(L"S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference|Address",L"https://XXXXXXXXX/services"); xml.UpdateChildContent(L"S:Body|RequestSecurityToken|TokenType",L"http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims",true,L"xmlns:i",L"http://schemas.xmlsoap.org/ws/2005/05/identity"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims",true,L"Dialect",L"http://schemas.xmlsoap.org/ws/2005/05/identity"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType",true,L"Optional",L"false"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/abn"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[1]",true,L"Optional",L"false"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[1]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/commonname"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[2]",true,L"Optional",L"false"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[2]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/credentialtype"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[3]",true,L"Optional",L"false"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[3]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/samlsubjectid"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[4]",true,L"Optional",L"false"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[4]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/fingerprint"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[5]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[5]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/sbr_personid"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[6]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[6]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/givennames"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[7]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[7]",true,L"Uri",L"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[8]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[8]",true,L"Uri",L"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[9]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[9]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/credentialadministrator"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[10]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[10]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/stalecrlminutes"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[11]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[11]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/subjectdn"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[12]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[12]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/issuerdn"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[13]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[13]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/notafterdate"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[14]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[14]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/certificateserialnumber"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[15]",true,L"Optional",L"true"); xml.UpdateAttrAt(L"S:Body|RequestSecurityToken|Claims|i:ClaimType[15]",true,L"Uri",L"http://XXXXXXXXX/2008/06/identity/claims/previoussubject"); dt.SetFromCurrentSystemTime(); xml.UpdateAttrAt(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"); xml.UpdateChildContent(L"S:Body|RequestSecurityToken|Lifetime|wsu:Created",dt.getAsTimestamp(false)); // Add 40 minutes. dt.AddSeconds(2400); xml.UpdateAttrAt(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"); xml.UpdateChildContent(L"S:Body|RequestSecurityToken|Lifetime|wsu:Expires",dt.getAsTimestamp(false)); xml.UpdateChildContent(L"S:Body|RequestSecurityToken|KeyType",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey"); xml.UpdateChildContent(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> CkXmlW keyInfoXml; keyInfoXml.put_Tag(L"wsse:SecurityTokenReference"); keyInfoXml.UpdateAttrAt(L"wsse:Reference",true,L"URI",L"#uuid_43470044-78b4-4b23-926a-b7f590d24cb8"); keyInfoXml.UpdateAttrAt(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. // CkXmlDSigGenW gen; gen.put_SigLocation(L"S:Envelope|S:Header|wsse:Security"); gen.put_SignedInfoPrefixList(L"wsse S"); gen.AddSameDocRef(L"_1",L"sha1",L"EXCL_C14N",L"wsu wsse S",L""); gen.AddSameDocRef(L"_5002",L"sha1",L"EXCL_C14N",L"S",L""); gen.put_KeyInfoType(L"Custom"); keyInfoXml.put_EmitXmlDecl(false); gen.put_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.put_Behaviors(L"IndentedSignature"); // Sign the XML.. CkStringBuilderW sbXml; xml.GetXmlSb(sbXml); success = gen.CreateXmlDSigSb(sbXml); if (success != true) { wprintf(L"%s\n",gen.lastErrorText()); return; } // Examine the signed XML wprintf(L"%s\n",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> // } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.