Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loTsId
LOCAL loStrId
LOCAL loKeyInfoId
LOCAL loDt
LOCAL loSbNow
LOCAL n
LOCAL loSbNowPlusOneHour
LOCAL loXmlToSign
LOCAL loGen
LOCAL loXml1
LOCAL loXml2
LOCAL loCert
LOCAL loXmlCustomKeyInfo
LOCAL loSbXml
lnSuccess = 0
* This example assumes the Chilkat API to have been previously unlocked.
* See Global Unlock Sample for sample code.
lnSuccess = 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
loTsId = CreateObject('Chilkat.StringBuilder')
loTsId.Append("TS-")
loTsId.AppendRandom(16,"hex")
* STR-037E78514E9B9132CB16817563559614
loStrId = CreateObject('Chilkat.StringBuilder')
loStrId.Append("STR-")
loStrId.AppendRandom(16,"hex")
* KI-037E78514E9B9132CB16817563559583
loKeyInfoId = CreateObject('Chilkat.StringBuilder')
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('Chilkat.CkDateTime')
loDt.SetFromCurrentSystemTime()
loSbNow = CreateObject('Chilkat.StringBuilder')
loSbNow.Append(loDt.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 = loSbNow.Replace("Z",".000Z")
loSbNowPlusOneHour = CreateObject('Chilkat.StringBuilder')
loDt.AddSeconds(3600)
loSbNowPlusOneHour.Append(loDt.GetAsTimestamp(0))
n = loSbNowPlusOneHour.Replace("Z",".000Z")
loXmlToSign = CreateObject('Chilkat.Xml')
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",1,"soapenv:mustUnderstand","1")
loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",1,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
loXmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",1,"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",1,"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",1,"wsu:Id","id-8")
loXmlToSign.UpdateAttrAt("soapenv:Body",1,"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('Chilkat.XmlDSigGen')
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('Chilkat.Xml')
loXml1.Tag = "ds:Transforms"
loXml1.UpdateAttrAt("ds:Transform",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#")
loXml1.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",1,"PrefixList","wsse soapenv v1")
loXml1.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",1,"xmlns:ec","http://www.w3.org/2001/10/xml-exc-c14n#")
loGen.AddSameDocRef2(loTsId.GetAsString(),"sha256",loXml1,"")
* -------- Reference 2 --------
loXml2 = CreateObject('Chilkat.Xml')
loXml2.Tag = "ds:Transforms"
loXml2.UpdateAttrAt("ds:Transform",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#")
loXml2.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",1,"PrefixList","v1")
loXml2.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",1,"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('Chilkat.Cert')
lnSuccess = loCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
IF (lnSuccess <> 1) 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
CANCEL
ENDIF
loGen.SetX509Cert(loCert,1)
loGen.KeyInfoType = "Custom"
* Create the custom KeyInfo XML..
loXmlCustomKeyInfo = CreateObject('Chilkat.Xml')
loXmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference"
loXmlCustomKeyInfo.AddAttribute("wsu:Id",loStrId.GetAsString())
loXmlCustomKeyInfo.UpdateAttrAt("wsse:KeyIdentifier",1,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
loXmlCustomKeyInfo.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
loCert.UncommonOptions = "Base64CertNoCRLF"
loXmlCustomKeyInfo.UpdateChildContent("wsse:KeyIdentifier",loCert.GetEncoded())
loXmlCustomKeyInfo.EmitXmlDecl = 0
loGen.CustomKeyInfoXml = loXmlCustomKeyInfo.GetXml()
* Load XML to be signed...
loSbXml = CreateObject('Chilkat.StringBuilder')
loXmlToSign.GetXmlSb(loSbXml)
loGen.Behaviors = "IndentedSignature"
* Sign the XML...
loGen.VerboseLogging = 1
lnSuccess = loGen.CreateXmlDSigSb(loSbXml)
IF (lnSuccess <> 1) 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
CANCEL
ENDIF
* Save the signed XML to a file.
lnSuccess = loSbXml.WriteFile("c:/temp/qa_output/signedXml.xml","utf-8",0)
? 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