Sample code for 30+ languages & platforms
Xbase++

Sign with BinarySecurityToken

See more XML Digital Signatures Examples

Demonstrates creating an XML signature using a BinarySecurityToken.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCert
LOCAL oXml
LOCAL oDt
LOCAL oBdCert
LOCAL oKeyInfoXml
LOCAL oGen
LOCAL oSbXml

nSuccess := 0

//  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.
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadPfxFile("qa_data/pfx/cert_test123.pfx", "test123")
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oCert:destroy()
    RETURN
ENDIF

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

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

//  Get the current date/time in timestamp format, such as "2018-05-23T02:38:27Z"
oDt := CreateObject("Chilkat.CkDateTime")
oDt:SetFromCurrentSystemTime()
oXml:UpdateChildContent("S:Header|wsse:Security|wsu:Timestamp|wsu:Created", oDt:GetAsTimestamp(0))
//  Add 5 minutes.
oDt:AddSeconds(300)
oXml:UpdateChildContent("S:Header|wsse:Security|wsu:Timestamp|wsu:Expires", oDt:GetAsTimestamp(0))

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

oBdCert := CreateObject("Chilkat.BinData")
oCert:ExportCertDerBd(oBdCert)

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

oDt:SetFromCurrentSystemTime()
oXml:UpdateAttrAt("S:Body|RequestSecurityToken|Lifetime|wsu:Created", 1, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
oXml:UpdateChildContent("S:Body|RequestSecurityToken|Lifetime|wsu:Created", oDt:GetAsTimestamp(0))

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

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

oKeyInfoXml := CreateObject("Chilkat.Xml")
oKeyInfoXml:Tag := "wsse:SecurityTokenReference"
oKeyInfoXml:UpdateAttrAt("wsse:Reference", 1, "URI", "#uuid_43470044-78b4-4b23-926a-b7f590d24cb8")
oKeyInfoXml:UpdateAttrAt("wsse:Reference", 1, "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.
//  
oGen := CreateObject("Chilkat.XmlDSigGen")
oGen:SigLocation := "S:Envelope|S:Header|wsse:Security"
oGen:SignedInfoPrefixList := "wsse S"

oGen:AddSameDocRef("_1", "sha1", "EXCL_C14N", "wsu wsse S", "")
oGen:AddSameDocRef("_5002", "sha1", "EXCL_C14N", "S", "")

oGen:KeyInfoType := "Custom"
oKeyInfoXml:EmitXmlDecl := 0
oGen:CustomKeyInfoXml := oKeyInfoXml:GetXml()

//  Specify the cert for signing (which has a private key because it was loaded from a PFX)
oGen:SetX509Cert(oCert, 1)

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

//  Sign the XML..
oSbXml := CreateObject("Chilkat.StringBuilder")
oXml:GetXmlSb(oSbXml)

nSuccess := oGen:CreateXmlDSigSb(oSbXml)
IF (nSuccess != 1)
    ? oGen:LastErrorText
    oCert:destroy()
    oXml:destroy()
    oDt:destroy()
    oBdCert:destroy()
    oKeyInfoXml:destroy()
    oGen:destroy()
    oSbXml:destroy()
    RETURN
ENDIF

//  Examine the signed XML
? oSbXml: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>
//

oCert:destroy()
oXml:destroy()
oDt:destroy()
oBdCert:destroy()
oKeyInfoXml:destroy()
oGen:destroy()
oSbXml:destroy()