Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Lianja) Sign SOAP XML for New Zealand Customs ServiceSee more XAdES ExamplesDemonstrates 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.
// This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. llSuccess = .T. // 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 loTsId = createobject("CkStringBuilder") loTsId.Append("TS-") loTsId.AppendRandom(16,"hex") // STR-037E78514E9B9132CB16817563559614 loStrId = createobject("CkStringBuilder") loStrId.Append("STR-") loStrId.AppendRandom(16,"hex") // KI-037E78514E9B9132CB16817563559583 loKeyInfoId = createobject("CkStringBuilder") loKeyInfoId.Append("KI-") loKeyInfoId.AppendRandom(16,"hex") // Create a date/time for the current time with this format: 2023-04-17T18:32:35.913Z loDt = createobject("CkDateTime") loDt.SetFromCurrentSystemTime() loSbNow = createobject("CkStringBuilder") loSbNow.Append(loDt.GetAsTimestamp(.F.)) // 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 = loSbNow.Replace("Z",".000Z") loSbNowPlusOneHour = createobject("CkStringBuilder") loDt.AddSeconds(3600) loSbNowPlusOneHour.Append(loDt.GetAsTimestamp(.F.)) n = loSbNowPlusOneHour.Replace("Z",".000Z") loXmlToSign = createobject("CkXml") loXmlToSign.Tag = "soapenv:Envelope" loXmlToSign.AddAttribute("xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/") loXmlToSign.AddAttribute("xmlns:v1","http://customs.govt.nz/jbms/msggate/reqresp/v1") loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",.T.,"soapenv:mustUnderstand","1") loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",.T.,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",.T.,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsu:Timestamp",.T.,"wsu:Id",loTsId.GetAsString()) loXmlToSign.UpdateChildContent("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created",loSbNow.GetAsString()) loXmlToSign.UpdateChildContent("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires",loSbNowPlusOneHour.GetAsString()) loXmlToSign.UpdateAttrAt("soapenv:Body",.T.,"wsu:Id","id-8") loXmlToSign.UpdateAttrAt("soapenv:Body",.T.,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") loXmlToSign.UpdateChildContent("soapenv:Body|v1:RequestResponse|v1:Submitter","TEST1234") loXmlToSign.UpdateChildContent("soapenv:Body|v1:RequestResponse|v1:MailboxMsgId","999999") loGen = createobject("CkXmlDSigGen") loGen.SigLocation = "soapenv:Envelope|soapenv:Header|wsse:Security" loGen.SigLocationMod = 0 loGen.SigId = "SIG-037E78514E9B9132CB16817563559695" loGen.SigNamespacePrefix = "ds" loGen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#" loGen.SignedInfoPrefixList = "soapenv v1" loGen.IncNamespacePrefix = "ec" loGen.IncNamespaceUri = "http://www.w3.org/2001/10/xml-exc-c14n#" loGen.SignedInfoCanonAlg = "EXCL_C14N" loGen.SignedInfoDigestMethod = "sha256" // Set the KeyInfoId before adding references.. loGen.KeyInfoId = loKeyInfoId.GetAsString() // -------- Reference 1 -------- loXml1 = createobject("CkXml") loXml1.Tag = "ds:Transforms" loXml1.UpdateAttrAt("ds:Transform",.T.,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#") loXml1.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",.T.,"PrefixList","wsse soapenv v1") loXml1.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",.T.,"xmlns:ec","http://www.w3.org/2001/10/xml-exc-c14n#") loGen.AddSameDocRef2(loTsId.GetAsString(),"sha256",loXml1,"") // -------- Reference 2 -------- loXml2 = createobject("CkXml") loXml2.Tag = "ds:Transforms" loXml2.UpdateAttrAt("ds:Transform",.T.,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#") loXml2.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",.T.,"PrefixList","v1") loXml2.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",.T.,"xmlns:ec","http://www.w3.org/2001/10/xml-exc-c14n#") loGen.AddSameDocRef2("id-8","sha256",loXml2,"") // Provide a certificate + private key. (PFX password is test123) loCert = createobject("CkCert") llSuccess = loCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123") if (llSuccess <> .T.) then ? loCert.LastErrorText release loTsId release loStrId release loKeyInfoId release loDt release loSbNow release loSbNowPlusOneHour release loXmlToSign release loGen release loXml1 release loXml2 release loCert return endif loGen.SetX509Cert(loCert,.T.) loGen.KeyInfoType = "Custom" // Create the custom KeyInfo XML.. loXmlCustomKeyInfo = createobject("CkXml") loXmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference" loXmlCustomKeyInfo.AddAttribute("wsu:Id",loStrId.GetAsString()) loXmlCustomKeyInfo.UpdateAttrAt("wsse:KeyIdentifier",.T.,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary") loXmlCustomKeyInfo.UpdateAttrAt("wsse:KeyIdentifier",.T.,"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 loCert.UncommonOptions = "Base64CertNoCRLF" loXmlCustomKeyInfo.UpdateChildContent("wsse:KeyIdentifier",loCert.GetEncoded()) loXmlCustomKeyInfo.EmitXmlDecl = .F. loGen.CustomKeyInfoXml = loXmlCustomKeyInfo.GetXml() // Load XML to be signed... loSbXml = createobject("CkStringBuilder") loXmlToSign.GetXmlSb(loSbXml) loGen.Behaviors = "IndentedSignature" // Sign the XML... loGen.VerboseLogging = .T. llSuccess = loGen.CreateXmlDSigSb(loSbXml) if (llSuccess <> .T.) then ? loGen.LastErrorText release loTsId release loStrId release loKeyInfoId release loDt release loSbNow release loSbNowPlusOneHour release loXmlToSign release loGen release loXml1 release loXml2 release loCert release loXmlCustomKeyInfo release loSbXml return endif // Save the signed XML to a file. llSuccess = loSbXml.WriteFile("c:/temp/qa_output/signedXml.xml","utf-8",.F.) ? loSbXml.GetAsString() release loTsId release loStrId release loKeyInfoId release loDt release loSbNow release loSbNowPlusOneHour release loXmlToSign release loGen release loXml1 release loXml2 release loCert release loXmlCustomKeyInfo release loSbXml |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.