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 SOAP XML for New Zealand Customs Service

See more XAdES Examples

Demonstrates how to create an XAdES signed SOAP XML pertaining to the New Zealand Customs Service.

Note: This example requires Chilkat v9.5.0.96 or later.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
integer li_Success
oleobject loo_TsId
oleobject loo_StrId
oleobject loo_KeyInfoId
oleobject loo_Dt
oleobject loo_SbNow
integer n
oleobject loo_SbNowPlusOneHour
oleobject loo_XmlToSign
oleobject loo_Gen
oleobject loo_Xml1
oleobject loo_Xml2
oleobject loo_Cert
oleobject loo_XmlCustomKeyInfo
oleobject loo_SbXml

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

li_Success = 1

// Create the following XML to be signed:

// <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
//     xmlns:v1="http://customs.govt.nz/jbms/msggate/reqresp/v1">
//     <soapenv:Header>
//         <wsse:Security soapenv:mustUnderstand="1"
//             xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
//             xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
//             <wsu:Timestamp wsu:Id="TS-037E78514E9B9132CB16817563559151">
//                 <wsu:Created>2023-04-17T18:32:35.913Z</wsu:Created>
//                 <wsu:Expires>2023-04-17T19:32:35.913Z</wsu:Expires>
//             </wsu:Timestamp>
//         </wsse:Security>
//     </soapenv:Header>
//     <soapenv:Body wsu:Id="id-8"
//         xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
//         <v1:RequestResponse>
//             <v1:Submitter>TEST1234</v1:Submitter>
//             <v1:MailboxMsgId>999999</v1:MailboxMsgId>
//         </v1:RequestResponse>
//     </soapenv:Body>
// </soapenv:Envelope>

// Create a random ID like this: TS-037E78514E9B9132CB16817563559151
loo_TsId = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_TsId.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_TsId
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_TsId.Append("TS-")
loo_TsId.AppendRandom(16,"hex")

// STR-037E78514E9B9132CB16817563559614
loo_StrId = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_StrId.ConnectToNewObject("Chilkat.StringBuilder")

loo_StrId.Append("STR-")
loo_StrId.AppendRandom(16,"hex")

// KI-037E78514E9B9132CB16817563559583
loo_KeyInfoId = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_KeyInfoId.ConnectToNewObject("Chilkat.StringBuilder")

loo_KeyInfoId.Append("KI-")
loo_KeyInfoId.AppendRandom(16,"hex")

// Create a date/time for the current time with this format:  2023-04-17T18:32:35.913Z
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_SbNow = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbNow.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbNow.Append(loo_Dt.GetAsTimestamp(0))
// If we really need the milliseconds, we can replace the "Z" with ".000Z"
// The server will also likely accept a timestamp without milliseconds, such as 2023-04-17T18:32:35Z
n = loo_SbNow.Replace("Z",".000Z")

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

loo_Dt.AddSeconds(3600)
loo_SbNowPlusOneHour.Append(loo_Dt.GetAsTimestamp(0))
n = loo_SbNowPlusOneHour.Replace("Z",".000Z")

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

loo_XmlToSign.Tag = "soapenv:Envelope"
loo_XmlToSign.AddAttribute("xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/")
loo_XmlToSign.AddAttribute("xmlns:v1","http://customs.govt.nz/jbms/msggate/reqresp/v1")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",1,"soapenv:mustUnderstand","1")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",1,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsu:Timestamp",1,"wsu:Id",loo_TsId.GetAsString())
loo_XmlToSign.UpdateChildContent("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created",loo_SbNow.GetAsString())
loo_XmlToSign.UpdateChildContent("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires",loo_SbNowPlusOneHour.GetAsString())
loo_XmlToSign.UpdateAttrAt("soapenv:Body",1,"wsu:Id","id-8")
loo_XmlToSign.UpdateAttrAt("soapenv:Body",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlToSign.UpdateChildContent("soapenv:Body|v1:RequestResponse|v1:Submitter","TEST1234")
loo_XmlToSign.UpdateChildContent("soapenv:Body|v1:RequestResponse|v1:MailboxMsgId","999999")

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 = "soapenv:Envelope|soapenv:Header|wsse:Security"
loo_Gen.SigLocationMod = 0
loo_Gen.SigId = "SIG-037E78514E9B9132CB16817563559695"
loo_Gen.SigNamespacePrefix = "ds"
loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loo_Gen.SignedInfoPrefixList = "soapenv v1"
loo_Gen.IncNamespacePrefix = "ec"
loo_Gen.IncNamespaceUri = "http://www.w3.org/2001/10/xml-exc-c14n#"
loo_Gen.SignedInfoCanonAlg = "EXCL_C14N"
loo_Gen.SignedInfoDigestMethod = "sha256"

// Set the KeyInfoId before adding references..
loo_Gen.KeyInfoId = loo_KeyInfoId.GetAsString()

// -------- Reference 1 --------
loo_Xml1 = create oleobject
// Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
li_rc = loo_Xml1.ConnectToNewObject("Chilkat.Xml")

loo_Xml1.Tag = "ds:Transforms"
loo_Xml1.UpdateAttrAt("ds:Transform",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#")
loo_Xml1.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",1,"PrefixList","wsse soapenv v1")
loo_Xml1.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",1,"xmlns:ec","http://www.w3.org/2001/10/xml-exc-c14n#")

loo_Gen.AddSameDocRef2(loo_TsId.GetAsString(),"sha256",loo_Xml1,"")

// -------- Reference 2 --------
loo_Xml2 = create oleobject
// Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
li_rc = loo_Xml2.ConnectToNewObject("Chilkat.Xml")

loo_Xml2.Tag = "ds:Transforms"
loo_Xml2.UpdateAttrAt("ds:Transform",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#")
loo_Xml2.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",1,"PrefixList","v1")
loo_Xml2.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",1,"xmlns:ec","http://www.w3.org/2001/10/xml-exc-c14n#")

loo_Gen.AddSameDocRef2("id-8","sha256",loo_Xml2,"")

// Provide a certificate + private key. (PFX password is test123)
loo_Cert = create oleobject
// Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_TsId
    destroy loo_StrId
    destroy loo_KeyInfoId
    destroy loo_Dt
    destroy loo_SbNow
    destroy loo_SbNowPlusOneHour
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Xml1
    destroy loo_Xml2
    destroy loo_Cert
    return
end if

loo_Gen.SetX509Cert(loo_Cert,1)

loo_Gen.KeyInfoType = "Custom"

// Create the custom KeyInfo XML..
loo_XmlCustomKeyInfo = create oleobject
// Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
li_rc = loo_XmlCustomKeyInfo.ConnectToNewObject("Chilkat.Xml")

loo_XmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference"
loo_XmlCustomKeyInfo.AddAttribute("wsu:Id",loo_StrId.GetAsString())
loo_XmlCustomKeyInfo.UpdateAttrAt("wsse:KeyIdentifier",1,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
loo_XmlCustomKeyInfo.UpdateAttrAt("wsse:KeyIdentifier",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3")
// Insert the single-line base64 of the signing certificate's DER
loo_Cert.UncommonOptions = "Base64CertNoCRLF"
loo_XmlCustomKeyInfo.UpdateChildContent("wsse:KeyIdentifier",loo_Cert.GetEncoded())

loo_XmlCustomKeyInfo.EmitXmlDecl = 0
loo_Gen.CustomKeyInfoXml = loo_XmlCustomKeyInfo.GetXml()

// Load XML to be signed...
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_XmlToSign.GetXmlSb(loo_SbXml)

loo_Gen.Behaviors = "IndentedSignature"

// Sign the XML...
loo_Gen.VerboseLogging = 1
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_TsId
    destroy loo_StrId
    destroy loo_KeyInfoId
    destroy loo_Dt
    destroy loo_SbNow
    destroy loo_SbNowPlusOneHour
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Xml1
    destroy loo_Xml2
    destroy loo_Cert
    destroy loo_XmlCustomKeyInfo
    destroy loo_SbXml
    return
end if

// Save the signed XML to a file.
li_Success = loo_SbXml.WriteFile("c:/temp/qa_output/signedXml.xml","utf-8",0)

Write-Debug loo_SbXml.GetAsString()


destroy loo_TsId
destroy loo_StrId
destroy loo_KeyInfoId
destroy loo_Dt
destroy loo_SbNow
destroy loo_SbNowPlusOneHour
destroy loo_XmlToSign
destroy loo_Gen
destroy loo_Xml1
destroy loo_Xml2
destroy loo_Cert
destroy loo_XmlCustomKeyInfo
destroy loo_SbXml

 

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