Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$bSuccess = True

; 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
$oTsId = ObjCreate("Chilkat.StringBuilder")
$oTsId.Append("TS-")
$oTsId.AppendRandom(16,"hex")

; STR-037E78514E9B9132CB16817563559614
$oStrId = ObjCreate("Chilkat.StringBuilder")
$oStrId.Append("STR-")
$oStrId.AppendRandom(16,"hex")

; KI-037E78514E9B9132CB16817563559583
$oKeyInfoId = ObjCreate("Chilkat.StringBuilder")
$oKeyInfoId.Append("KI-")
$oKeyInfoId.AppendRandom(16,"hex")

; Create a date/time for the current time with this format:  2023-04-17T18:32:35.913Z
$oDt = ObjCreate("Chilkat.CkDateTime")
$oDt.SetFromCurrentSystemTime()

$oSbNow = ObjCreate("Chilkat.StringBuilder")
$oSbNow.Append($oDt.GetAsTimestamp(False))
; 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
Local $iN = $oSbNow.Replace("Z",".000Z")

$oSbNowPlusOneHour = ObjCreate("Chilkat.StringBuilder")
$oDt.AddSeconds(3600)
$oSbNowPlusOneHour.Append($oDt.GetAsTimestamp(False))
$iN = $oSbNowPlusOneHour.Replace("Z",".000Z")

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

$oGen = ObjCreate("Chilkat.XmlDSigGen")

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

; Set the KeyInfoId before adding references..
$oGen.KeyInfoId = $oKeyInfoId.GetAsString()

; -------- Reference 1 --------
$oXml1 = ObjCreate("Chilkat.Xml")
$oXml1.Tag = "ds:Transforms"
$oXml1.UpdateAttrAt("ds:Transform",True,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#")
$oXml1.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",True,"PrefixList","wsse soapenv v1")
$oXml1.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",True,"xmlns:ec","http://www.w3.org/2001/10/xml-exc-c14n#")

$oGen.AddSameDocRef2($oTsId.GetAsString(),"sha256",$oXml1,"")

; -------- Reference 2 --------
$oXml2 = ObjCreate("Chilkat.Xml")
$oXml2.Tag = "ds:Transforms"
$oXml2.UpdateAttrAt("ds:Transform",True,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#")
$oXml2.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",True,"PrefixList","v1")
$oXml2.UpdateAttrAt("ds:Transform|ec:InclusiveNamespaces",True,"xmlns:ec","http://www.w3.org/2001/10/xml-exc-c14n#")

$oGen.AddSameDocRef2("id-8","sha256",$oXml2,"")

; Provide a certificate + private key. (PFX password is test123)
$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
If ($bSuccess <> True) Then
    ConsoleWrite($oCert.LastErrorText & @CRLF)
    Exit
EndIf

$oGen.SetX509Cert($oCert,True)

$oGen.KeyInfoType = "Custom"

; Create the custom KeyInfo XML..
$oXmlCustomKeyInfo = ObjCreate("Chilkat.Xml")
$oXmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference"
$oXmlCustomKeyInfo.AddAttribute("wsu:Id",$oStrId.GetAsString())
$oXmlCustomKeyInfo.UpdateAttrAt("wsse:KeyIdentifier",True,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
$oXmlCustomKeyInfo.UpdateAttrAt("wsse:KeyIdentifier",True,"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
$oCert.UncommonOptions = "Base64CertNoCRLF"
$oXmlCustomKeyInfo.UpdateChildContent "wsse:KeyIdentifier",$oCert.GetEncoded()

$oXmlCustomKeyInfo.EmitXmlDecl = False
$oGen.CustomKeyInfoXml = $oXmlCustomKeyInfo.GetXml()

; Load XML to be signed...
$oSbXml = ObjCreate("Chilkat.StringBuilder")
$oXmlToSign.GetXmlSb($oSbXml)

$oGen.Behaviors = "IndentedSignature"

; Sign the XML...
$oGen.VerboseLogging = True
$bSuccess = $oGen.CreateXmlDSigSb($oSbXml)
If ($bSuccess <> True) Then
    ConsoleWrite($oGen.LastErrorText & @CRLF)
    Exit
EndIf

; Save the signed XML to a file.
$bSuccess = $oSbXml.WriteFile("c:/temp/qa_output/signedXml.xml","utf-8",False)

ConsoleWrite($oSbXml.GetAsString() & @CRLF)