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
(C) Sign with BinarySecurityTokenDemonstrates creating an XML signature using a BinarySecurityToken.
#include <C_CkCert.h> #include <C_CkXml.h> #include <C_CkDateTime.h> #include <C_CkBinData.h> #include <C_CkXmlDSigGen.h> #include <C_CkStringBuilder.h> void ChilkatSample(void) { BOOL success; HCkCert cert; HCkXml xml; HCkDateTime dt; HCkBinData bdCert; HCkXml keyInfoXml; HCkXmlDSigGen gen; HCkStringBuilder sbXml; // 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 = CkCert_Create(); success = CkCert_LoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123"); if (success != TRUE) { printf("%s\n",CkCert_lastErrorText(cert)); CkCert_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 = CkXml_Create(); CkXml_putTag(xml,"S:Envelope"); CkXml_AddAttribute(xml,"xmlns:S","http://www.w3.org/2003/05/soap-envelope"); CkXml_AddAttribute(xml,"xmlns:wsse11","http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd"); CkXml_AddAttribute(xml,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); CkXml_AddAttribute(xml,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); CkXml_AddAttribute(xml,"xmlns:xs","http://www.w3.org/2001/XMLSchema"); CkXml_AddAttribute(xml,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#"); CkXml_AddAttribute(xml,"xmlns:exc14n","http://www.w3.org/2001/10/xml-exc-c14n#"); CkXml_UpdateAttrAt(xml,"S:Header|To",TRUE,"xmlns","http://www.w3.org/2005/08/addressing"); CkXml_UpdateAttrAt(xml,"S:Header|To",TRUE,"wsu:Id","_5002"); CkXml_UpdateChildContent(xml,"S:Header|To","https://XXXXXXXXX"); CkXml_UpdateAttrAt(xml,"S:Header|Action",TRUE,"xmlns","http://www.w3.org/2005/08/addressing"); CkXml_UpdateAttrAt(xml,"S:Header|Action",TRUE,"xmlns:S","http://www.w3.org/2003/05/soap-envelope"); CkXml_UpdateAttrAt(xml,"S:Header|Action",TRUE,"S:mustUnderstand","true"); CkXml_UpdateChildContent(xml,"S:Header|Action","http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue"); CkXml_UpdateAttrAt(xml,"S:Header|ReplyTo",TRUE,"xmlns","http://www.w3.org/2005/08/addressing"); CkXml_UpdateChildContent(xml,"S:Header|ReplyTo|Address","http://www.w3.org/2005/08/addressing/anonymous"); CkXml_UpdateAttrAt(xml,"S:Header|FaultTo",TRUE,"xmlns","http://www.w3.org/2005/08/addressing"); CkXml_UpdateChildContent(xml,"S:Header|FaultTo|Address","http://www.w3.org/2005/08/addressing/anonymous"); CkXml_UpdateAttrAt(xml,"S:Header|MessageID",TRUE,"xmlns","http://www.w3.org/2005/08/addressing"); CkXml_UpdateChildContent(xml,"S:Header|MessageID","uuid:e9033251-4ff0-4618-8baf-4952ab5fd207"); CkXml_UpdateAttrAt(xml,"S:Header|wsse:Security",TRUE,"S:mustUnderstand","true"); CkXml_UpdateAttrAt(xml,"S:Header|wsse:Security|wsu:Timestamp",TRUE,"xmlns:ns17","http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512"); CkXml_UpdateAttrAt(xml,"S:Header|wsse:Security|wsu:Timestamp",TRUE,"xmlns:ns16","http://schemas.xmlsoap.org/soap/envelope/"); CkXml_UpdateAttrAt(xml,"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" dt = CkDateTime_Create(); CkDateTime_SetFromCurrentSystemTime(dt); CkXml_UpdateChildContent(xml,"S:Header|wsse:Security|wsu:Timestamp|wsu:Created",CkDateTime_getAsTimestamp(dt,FALSE)); // Add 5 minutes. CkDateTime_AddSeconds(dt,300); CkXml_UpdateChildContent(xml,"S:Header|wsse:Security|wsu:Timestamp|wsu:Expires",CkDateTime_getAsTimestamp(dt,FALSE)); CkXml_UpdateAttrAt(xml,"S:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"xmlns:ns17","http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512"); CkXml_UpdateAttrAt(xml,"S:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"xmlns:ns16","http://schemas.xmlsoap.org/soap/envelope/"); CkXml_UpdateAttrAt(xml,"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"); CkXml_UpdateAttrAt(xml,"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"); CkXml_UpdateAttrAt(xml,"S:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"wsu:Id","uuid_43470044-78b4-4b23-926a-b7f590d24cb8"); bdCert = CkBinData_Create(); CkCert_ExportCertDerBd(cert,bdCert); // Get the cert as base64 on one line. CkXml_UpdateChildContent(xml,"S:Header|wsse:Security|wsse:BinarySecurityToken",CkBinData_getEncoded(bdCert,"base64")); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken",TRUE,"xmlns","http://docs.oasis-open.org/ws-sx/ws-trust/200512"); CkXml_UpdateChildContent(xml,"S:Body|RequestSecurityToken|RequestType","http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|wsp:AppliesTo",TRUE,"xmlns:wsp","http://schemas.xmlsoap.org/ws/2004/09/policy"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference",TRUE,"xmlns:EndpointReference","http://www.w3.org/2005/08/addressing"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference",TRUE,"xmlns","http://www.w3.org/2005/08/addressing"); CkXml_UpdateChildContent(xml,"S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference|Address","https://XXXXXXXXX/services"); CkXml_UpdateChildContent(xml,"S:Body|RequestSecurityToken|TokenType","http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims",TRUE,"xmlns:i","http://schemas.xmlsoap.org/ws/2005/05/identity"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims",TRUE,"Dialect","http://schemas.xmlsoap.org/ws/2005/05/identity"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType",TRUE,"Optional","false"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/abn"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[1]",TRUE,"Optional","false"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[1]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/commonname"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[2]",TRUE,"Optional","false"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[2]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/credentialtype"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[3]",TRUE,"Optional","false"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[3]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/samlsubjectid"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[4]",TRUE,"Optional","false"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[4]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/fingerprint"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[5]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[5]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/sbr_personid"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[6]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[6]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/givennames"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[7]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[7]",TRUE,"Uri","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[8]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[8]",TRUE,"Uri","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[9]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[9]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/credentialadministrator"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[10]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[10]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/stalecrlminutes"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[11]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[11]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/subjectdn"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[12]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[12]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/issuerdn"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[13]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[13]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/notafterdate"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[14]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[14]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/certificateserialnumber"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[15]",TRUE,"Optional","true"); CkXml_UpdateAttrAt(xml,"S:Body|RequestSecurityToken|Claims|i:ClaimType[15]",TRUE,"Uri","http://XXXXXXXXX/2008/06/identity/claims/previoussubject"); CkDateTime_SetFromCurrentSystemTime(dt); CkXml_UpdateAttrAt(xml,"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"); CkXml_UpdateChildContent(xml,"S:Body|RequestSecurityToken|Lifetime|wsu:Created",CkDateTime_getAsTimestamp(dt,FALSE)); // Add 40 minutes. CkDateTime_AddSeconds(dt,2400); CkXml_UpdateAttrAt(xml,"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"); CkXml_UpdateChildContent(xml,"S:Body|RequestSecurityToken|Lifetime|wsu:Expires",CkDateTime_getAsTimestamp(dt,FALSE)); CkXml_UpdateChildContent(xml,"S:Body|RequestSecurityToken|KeyType","http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey"); CkXml_UpdateChildContent(xml,"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> keyInfoXml = CkXml_Create(); CkXml_putTag(keyInfoXml,"wsse:SecurityTokenReference"); CkXml_UpdateAttrAt(keyInfoXml,"wsse:Reference",TRUE,"URI","#uuid_43470044-78b4-4b23-926a-b7f590d24cb8"); CkXml_UpdateAttrAt(keyInfoXml,"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. // gen = CkXmlDSigGen_Create(); CkXmlDSigGen_putSigLocation(gen,"S:Envelope|S:Header|wsse:Security"); CkXmlDSigGen_putSignedInfoPrefixList(gen,"wsse S"); CkXmlDSigGen_AddSameDocRef(gen,"_1","sha1","EXCL_C14N","wsu wsse S",""); CkXmlDSigGen_AddSameDocRef(gen,"_5002","sha1","EXCL_C14N","S",""); CkXmlDSigGen_putKeyInfoType(gen,"Custom"); CkXml_putEmitXmlDecl(keyInfoXml,FALSE); CkXmlDSigGen_putCustomKeyInfoXml(gen,CkXml_getXml(keyInfoXml)); // Specify the cert for signing (which has a private key because it was loaded from a PFX) CkXmlDSigGen_SetX509Cert(gen,cert,TRUE); // Indicated we want an indented signature for easier human reading. CkXmlDSigGen_putBehaviors(gen,"IndentedSignature"); // Sign the XML.. sbXml = CkStringBuilder_Create(); CkXml_GetXmlSb(xml,sbXml); success = CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml); if (success != TRUE) { printf("%s\n",CkXmlDSigGen_lastErrorText(gen)); CkCert_Dispose(cert); CkXml_Dispose(xml); CkDateTime_Dispose(dt); CkBinData_Dispose(bdCert); CkXml_Dispose(keyInfoXml); CkXmlDSigGen_Dispose(gen); CkStringBuilder_Dispose(sbXml); return; } // Examine the signed XML printf("%s\n",CkStringBuilder_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> // CkCert_Dispose(cert); CkXml_Dispose(xml); CkDateTime_Dispose(dt); CkBinData_Dispose(bdCert); CkXml_Dispose(keyInfoXml); CkXmlDSigGen_Dispose(gen); CkStringBuilder_Dispose(sbXml); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.