Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(Excel) Sign with BinarySecurityTokenDemonstrates creating an XML signature using a BinarySecurityToken.
' 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. Dim cert As Chilkat.Cert Set cert = Chilkat.NewCert success = cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123") If (success <> True) Then Debug.Print cert.LastErrorText Exit Sub End If ' ------------------------------------------------------------------------- ' 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. ' Dim xml As Chilkat.Xml Set xml = Chilkat.NewXml xml.Tag = "S:Envelope" Dim success As Boolean success = xml.AddAttribute("xmlns:S","http://www.w3.org/2003/05/soap-envelope") success = xml.AddAttribute("xmlns:wsse11","http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd") success = xml.AddAttribute("xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") success = xml.AddAttribute("xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") success = xml.AddAttribute("xmlns:xs","http://www.w3.org/2001/XMLSchema") success = xml.AddAttribute("xmlns:ds","http://www.w3.org/2000/09/xmldsig#") success = xml.AddAttribute("xmlns:exc14n","http://www.w3.org/2001/10/xml-exc-c14n#") success = xml.UpdateAttrAt("S:Header|To",True,"xmlns","http://www.w3.org/2005/08/addressing") success = xml.UpdateAttrAt("S:Header|To",True,"wsu:Id","_5002") xml.UpdateChildContent "S:Header|To","https://XXXXXXXXX" success = xml.UpdateAttrAt("S:Header|Action",True,"xmlns","http://www.w3.org/2005/08/addressing") success = xml.UpdateAttrAt("S:Header|Action",True,"xmlns:S","http://www.w3.org/2003/05/soap-envelope") success = 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" success = 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" success = 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" success = 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" success = xml.UpdateAttrAt("S:Header|wsse:Security",True,"S:mustUnderstand","true") success = xml.UpdateAttrAt("S:Header|wsse:Security|wsu:Timestamp",True,"xmlns:ns17","http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512") success = xml.UpdateAttrAt("S:Header|wsse:Security|wsu:Timestamp",True,"xmlns:ns16","http://schemas.xmlsoap.org/soap/envelope/") success = 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" Dim dt As Chilkat.CkDateTime Set dt = Chilkat.NewCkDateTime success = dt.SetFromCurrentSystemTime() xml.UpdateChildContent "S:Header|wsse:Security|wsu:Timestamp|wsu:Created",dt.GetAsTimestamp(False) ' Add 5 minutes. success = dt.AddSeconds(300) xml.UpdateChildContent "S:Header|wsse:Security|wsu:Timestamp|wsu:Expires",dt.GetAsTimestamp(False) success = xml.UpdateAttrAt("S:Header|wsse:Security|wsse:BinarySecurityToken",True,"xmlns:ns17","http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512") success = xml.UpdateAttrAt("S:Header|wsse:Security|wsse:BinarySecurityToken",True,"xmlns:ns16","http://schemas.xmlsoap.org/soap/envelope/") success = 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") success = 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") success = xml.UpdateAttrAt("S:Header|wsse:Security|wsse:BinarySecurityToken",True,"wsu:Id","uuid_43470044-78b4-4b23-926a-b7f590d24cb8") Dim bdCert As Chilkat.BinData Set bdCert = Chilkat.NewBinData success = cert.ExportCertDerBd(bdCert) ' Get the cert as base64 on one line. xml.UpdateChildContent "S:Header|wsse:Security|wsse:BinarySecurityToken",bdCert.GetEncoded("base64") success = 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" success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|wsp:AppliesTo",True,"xmlns:wsp","http://schemas.xmlsoap.org/ws/2004/09/policy") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference",True,"xmlns:EndpointReference","http://www.w3.org/2005/08/addressing") success = 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" success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims",True,"xmlns:i","http://schemas.xmlsoap.org/ws/2005/05/identity") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims",True,"Dialect","http://schemas.xmlsoap.org/ws/2005/05/identity") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType",True,"Optional","false") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/abn") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[1]",True,"Optional","false") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[1]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/commonname") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[2]",True,"Optional","false") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[2]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/credentialtype") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[3]",True,"Optional","false") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[3]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/samlsubjectid") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[4]",True,"Optional","false") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[4]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/fingerprint") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[5]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[5]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/sbr_personid") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[6]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[6]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/givennames") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[7]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[7]",True,"Uri","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[8]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[8]",True,"Uri","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[9]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[9]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/credentialadministrator") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[10]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[10]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/stalecrlminutes") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[11]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[11]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/subjectdn") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[12]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[12]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/issuerdn") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[13]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[13]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/notafterdate") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[14]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[14]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/certificateserialnumber") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[15]",True,"Optional","true") success = xml.UpdateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[15]",True,"Uri","http://XXXXXXXXX/2008/06/identity/claims/previoussubject") success = dt.SetFromCurrentSystemTime() success = 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. success = dt.AddSeconds(2400) success = 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> Dim keyInfoXml As Chilkat.Xml Set keyInfoXml = Chilkat.NewXml keyInfoXml.Tag = "wsse:SecurityTokenReference" success = keyInfoXml.UpdateAttrAt("wsse:Reference",True,"URI","#uuid_43470044-78b4-4b23-926a-b7f590d24cb8") success = 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. ' Dim gen As Chilkat.XmlDSigGen Set gen = Chilkat.NewXmlDSigGen gen.SigLocation = "S:Envelope|S:Header|wsse:Security" gen.SignedInfoPrefixList = "wsse S" success = gen.AddSameDocRef("_1","sha1","EXCL_C14N","wsu wsse S","") success = 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) success = gen.SetX509Cert(cert,True) ' Indicated we want an indented signature for easier human reading. gen.Behaviors = "IndentedSignature" ' Sign the XML.. Dim sbXml As Chilkat.StringBuilder Set sbXml = Chilkat.NewStringBuilder success = xml.GetXmlSb(sbXml) success = gen.CreateXmlDSigSb(sbXml) If (success <> True) Then Debug.Print gen.LastErrorText Exit Sub End If ' Examine the signed XML Debug.Print 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-2022 Chilkat Software, Inc. All Rights Reserved.