Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

PowerBuilder Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(PowerBuilder) Sign with BinarySecurityToken

Demonstrates creating an XML signature using a BinarySecurityToken.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
integer li_Success
oleobject loo_Cert
oleobject loo_Xml
oleobject loo_Dt
oleobject loo_BdCert
oleobject loo_KeyInfoXml
oleobject loo_Gen
oleobject loo_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.
loo_Cert = create oleobject
// Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
if li_rc < 0 then
    destroy loo_Cert
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Cert
    return
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.
// 

loo_Xml = create oleobject
// Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

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

loo_Dt.SetFromCurrentSystemTime()
loo_Xml.UpdateChildContent("S:Header|wsse:Security|wsu:Timestamp|wsu:Created",loo_Dt.GetAsTimestamp(0))
// Add 5 minutes.
loo_Dt.AddSeconds(300)
loo_Xml.UpdateChildContent("S:Header|wsse:Security|wsu:Timestamp|wsu:Expires",loo_Dt.GetAsTimestamp(0))

loo_Xml.UpdateAttrAt("S:Header|wsse:Security|wsse:BinarySecurityToken",1,"xmlns:ns17","http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512")
loo_Xml.UpdateAttrAt("S:Header|wsse:Security|wsse:BinarySecurityToken",1,"xmlns:ns16","http://schemas.xmlsoap.org/soap/envelope/")
loo_Xml.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")
loo_Xml.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")
loo_Xml.UpdateAttrAt("S:Header|wsse:Security|wsse:BinarySecurityToken",1,"wsu:Id","uuid_43470044-78b4-4b23-926a-b7f590d24cb8")

loo_BdCert = create oleobject
// Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
li_rc = loo_BdCert.ConnectToNewObject("Chilkat.BinData")

loo_Cert.ExportCertDerBd(loo_BdCert)

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

loo_Dt.SetFromCurrentSystemTime()
loo_Xml.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")
loo_Xml.UpdateChildContent("S:Body|RequestSecurityToken|Lifetime|wsu:Created",loo_Dt.GetAsTimestamp(0))

// Add 40 minutes.
loo_Dt.AddSeconds(2400)
loo_Xml.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")
loo_Xml.UpdateChildContent("S:Body|RequestSecurityToken|Lifetime|wsu:Expires",loo_Dt.GetAsTimestamp(0))

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

loo_KeyInfoXml = create oleobject
// Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
li_rc = loo_KeyInfoXml.ConnectToNewObject("Chilkat.Xml")

loo_KeyInfoXml.Tag = "wsse:SecurityTokenReference"
loo_KeyInfoXml.UpdateAttrAt("wsse:Reference",1,"URI","#uuid_43470044-78b4-4b23-926a-b7f590d24cb8")
loo_KeyInfoXml.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.
// 
loo_Gen = create oleobject
// Use "Chilkat_9_5_0.XmlDSigGen" for versions of Chilkat < 10.0.0
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")

loo_Gen.SigLocation = "S:Envelope|S:Header|wsse:Security"
loo_Gen.SignedInfoPrefixList = "wsse S"

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

loo_Gen.KeyInfoType = "Custom"
loo_KeyInfoXml.EmitXmlDecl = 0
loo_Gen.CustomKeyInfoXml = loo_KeyInfoXml.GetXml()

// Specify the cert for signing (which has a private key because it was loaded from a PFX)
loo_Gen.SetX509Cert(loo_Cert,1)

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

// Sign the XML..
loo_SbXml = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

loo_Xml.GetXmlSb(loo_SbXml)

li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_Cert
    destroy loo_Xml
    destroy loo_Dt
    destroy loo_BdCert
    destroy loo_KeyInfoXml
    destroy loo_Gen
    destroy loo_SbXml
    return
end if

// Examine the signed XML
Write-Debug loo_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>
// 


destroy loo_Cert
destroy loo_Xml
destroy loo_Dt
destroy loo_BdCert
destroy loo_KeyInfoXml
destroy loo_Gen
destroy loo_SbXml

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.