Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
success = 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
let tsId = CkoStringBuilder()!
tsId.append(value: "TS-")
tsId.appendRandom(numBytes: 16, encoding: "hex")
// STR-037E78514E9B9132CB16817563559614
let strId = CkoStringBuilder()!
strId.append(value: "STR-")
strId.appendRandom(numBytes: 16, encoding: "hex")
// KI-037E78514E9B9132CB16817563559583
let keyInfoId = CkoStringBuilder()!
keyInfoId.append(value: "KI-")
keyInfoId.appendRandom(numBytes: 16, encoding: "hex")
// Create a date/time for the current time with this format: 2023-04-17T18:32:35.913Z
let dt = CkoDateTime()!
dt.setFromCurrentSystemTime()
let sbNow = CkoStringBuilder()!
sbNow.append(value: dt.get(asTimestamp: 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
var n: Int = sbNow.replace(value: "Z", replacement: ".000Z").intValue
let sbNowPlusOneHour = CkoStringBuilder()!
dt.addSeconds(numSeconds: 3600)
sbNowPlusOneHour.append(value: dt.get(asTimestamp: false))
n = sbNowPlusOneHour.replace(value: "Z", replacement: ".000Z").intValue
let xmlToSign = CkoXml()!
xmlToSign.tag = "soapenv:Envelope"
xmlToSign.addAttribute(name: "xmlns:soapenv", value: "http://schemas.xmlsoap.org/soap/envelope/")
xmlToSign.addAttribute(name: "xmlns:v1", value: "http://customs.govt.nz/jbms/msggate/reqresp/v1")
xmlToSign.updateAttrAt(tagPath: "soapenv:Header|wsse:Security", autoCreate: true, attrName: "soapenv:mustUnderstand", attrValue: "1")
xmlToSign.updateAttrAt(tagPath: "soapenv:Header|wsse:Security", autoCreate: true, attrName: "xmlns:wsse", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
xmlToSign.updateAttrAt(tagPath: "soapenv:Header|wsse:Security", autoCreate: true, attrName: "xmlns:wsu", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
xmlToSign.updateAttrAt(tagPath: "soapenv:Header|wsse:Security|wsu:Timestamp", autoCreate: true, attrName: "wsu:Id", attrValue: tsId.getAsString())
xmlToSign.updateChildContent(tagPath: "soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created", value: sbNow.getAsString())
xmlToSign.updateChildContent(tagPath: "soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires", value: sbNowPlusOneHour.getAsString())
xmlToSign.updateAttrAt(tagPath: "soapenv:Body", autoCreate: true, attrName: "wsu:Id", attrValue: "id-8")
xmlToSign.updateAttrAt(tagPath: "soapenv:Body", autoCreate: true, attrName: "xmlns:wsu", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
xmlToSign.updateChildContent(tagPath: "soapenv:Body|v1:RequestResponse|v1:Submitter", value: "TEST1234")
xmlToSign.updateChildContent(tagPath: "soapenv:Body|v1:RequestResponse|v1:MailboxMsgId", value: "999999")
let gen = CkoXmlDSigGen()!
gen.sigLocation = "soapenv:Envelope|soapenv:Header|wsse:Security"
gen.sigLocationMod = 0
gen.sigId = "SIG-037E78514E9B9132CB16817563559695"
gen.sigNamespacePrefix = "ds"
gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
gen.signedInfoPrefixList = "soapenv v1"
gen.incNamespacePrefix = "ec"
gen.incNamespaceUri = "http://www.w3.org/2001/10/xml-exc-c14n#"
gen.signedInfoCanonAlg = "EXCL_C14N"
gen.signedInfoDigestMethod = "sha256"
// Set the KeyInfoId before adding references..
gen.keyInfoId = keyInfoId.getAsString()
// -------- Reference 1 --------
let xml1 = CkoXml()!
xml1.tag = "ds:Transforms"
xml1.updateAttrAt(tagPath: "ds:Transform", autoCreate: true, attrName: "Algorithm", attrValue: "http://www.w3.org/2001/10/xml-exc-c14n#")
xml1.updateAttrAt(tagPath: "ds:Transform|ec:InclusiveNamespaces", autoCreate: true, attrName: "PrefixList", attrValue: "wsse soapenv v1")
xml1.updateAttrAt(tagPath: "ds:Transform|ec:InclusiveNamespaces", autoCreate: true, attrName: "xmlns:ec", attrValue: "http://www.w3.org/2001/10/xml-exc-c14n#")
gen.addSameDocRef2(id: tsId.getAsString(), digestMethod: "sha256", transforms: xml1, refType: "")
// -------- Reference 2 --------
let xml2 = CkoXml()!
xml2.tag = "ds:Transforms"
xml2.updateAttrAt(tagPath: "ds:Transform", autoCreate: true, attrName: "Algorithm", attrValue: "http://www.w3.org/2001/10/xml-exc-c14n#")
xml2.updateAttrAt(tagPath: "ds:Transform|ec:InclusiveNamespaces", autoCreate: true, attrName: "PrefixList", attrValue: "v1")
xml2.updateAttrAt(tagPath: "ds:Transform|ec:InclusiveNamespaces", autoCreate: true, attrName: "xmlns:ec", attrValue: "http://www.w3.org/2001/10/xml-exc-c14n#")
gen.addSameDocRef2(id: "id-8", digestMethod: "sha256", transforms: xml2, refType: "")
// Provide a certificate + private key. (PFX password is test123)
let cert = CkoCert()!
success = cert.loadPfxFile(path: "qa_data/pfx/cert_test123.pfx", password: "test123")
if success != true {
print("\(cert.lastErrorText!)")
return
}
gen.setX509Cert(cert: cert, usePrivateKey: true)
gen.keyInfoType = "Custom"
// Create the custom KeyInfo XML..
let xmlCustomKeyInfo = CkoXml()!
xmlCustomKeyInfo.tag = "wsse:SecurityTokenReference"
xmlCustomKeyInfo.addAttribute(name: "wsu:Id", value: strId.getAsString())
xmlCustomKeyInfo.updateAttrAt(tagPath: "wsse:KeyIdentifier", autoCreate: true, attrName: "EncodingType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
xmlCustomKeyInfo.updateAttrAt(tagPath: "wsse:KeyIdentifier", autoCreate: true, attrName: "ValueType", attrValue: "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
cert.uncommonOptions = "Base64CertNoCRLF"
xmlCustomKeyInfo.updateChildContent(tagPath: "wsse:KeyIdentifier", value: cert.getEncoded())
xmlCustomKeyInfo.emitXmlDecl = false
gen.customKeyInfoXml = xmlCustomKeyInfo.getXml()
// Load XML to be signed...
let sbXml = CkoStringBuilder()!
xmlToSign.getSb(sb: sbXml)
gen.behaviors = "IndentedSignature"
// Sign the XML...
gen.verboseLogging = true
success = gen.createXmlDSigSb(sbXml: sbXml)
if success != true {
print("\(gen.lastErrorText!)")
return
}
// Save the signed XML to a file.
success = sbXml.writeFile(path: "c:/temp/qa_output/signedXml.xml", charset: "utf-8", emitBom: false)
print("\(sbXml.getAsString()!)")
}