Sample code for 30+ languages & platforms
Lianja

Sign with BinarySecurityToken

See more XML Digital Signatures Examples

Demonstrates creating an XML signature using a BinarySecurityToken.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

// 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.
loCert = createobject("CkCert")
llSuccess = loCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if (llSuccess <> .T.) then
    ? loCert.LastErrorText
    release loCert
    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.
// 

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

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

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

loBdCert = createobject("CkBinData")
loCert.ExportCertDerBd(loBdCert)

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

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

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

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

loKeyInfoXml = createobject("CkXml")
loKeyInfoXml.Tag = "wsse:SecurityTokenReference"
loKeyInfoXml.UpdateAttrAt("wsse:Reference",.T.,"URI","#uuid_43470044-78b4-4b23-926a-b7f590d24cb8")
loKeyInfoXml.UpdateAttrAt("wsse:Reference",.T.,"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.
// 
loGen = createobject("CkXmlDSigGen")
loGen.SigLocation = "S:Envelope|S:Header|wsse:Security"
loGen.SignedInfoPrefixList = "wsse S"

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

loGen.KeyInfoType = "Custom"
loKeyInfoXml.EmitXmlDecl = .F.
loGen.CustomKeyInfoXml = loKeyInfoXml.GetXml()

// Specify the cert for signing (which has a private key because it was loaded from a PFX)
loGen.SetX509Cert(loCert,.T.)

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

// Sign the XML..
loSbXml = createobject("CkStringBuilder")
loXml.GetXmlSb(loSbXml)

llSuccess = loGen.CreateXmlDSigSb(loSbXml)
if (llSuccess <> .T.) then
    ? loGen.LastErrorText
    release loCert
    release loXml
    release loDt
    release loBdCert
    release loKeyInfoXml
    release loGen
    release loSbXml
    return
endif

// Examine the signed XML
? loSbXml.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>
// 


release loCert
release loXml
release loDt
release loBdCert
release loKeyInfoXml
release loGen
release loSbXml