Swift
Swift
Create XAdES with ec:InclusiveNamespaces within the Transforms
See more XML Digital Signatures Examples
Demonstrates how to generate XAdES that has a Reference that has Transforms that include an ec:InclusiveNamespaces.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This is the XML we'll be signing:
// <soapenv:Envelope xmlns:obs="http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
// <soapenv:Header>
// <wsse:Security
// 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" soapenv:mustUnderstand="1">
// <wsse:BinarySecurityToken
// EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
// ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1"
// wsu:Id="X509-02BF0107214FC61449FD0013DF68F0359">MII...</wsse:BinarySecurityToken>
// </wsse:Security>
// </soapenv:Header>
// <soapenv:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
// wsu:Id="id-396BB6026342EB5C0E1EA73593B3CC098">
// <obs:zapiszKomunikatOS>
// <komunikatOS>
// <idPodmiotuRaportujacego>
// <idBiznesowy>000000011986</idBiznesowy>
// <rodzajPodmiotuRaportujacego>PA</rodzajPodmiotuRaportujacego>
// </idPodmiotuRaportujacego>
// </komunikatOS>
// </obs:zapiszKomunikatOS>
// </soapenv:Body>
// </soapenv:Envelope>
//
// ----------------------------------------------------------------------
// IMPORTANT: This generated example requires Chilkat v9.5.0.77 or later.
// ----------------------------------------------------------------------
success = true
// Create the above XML to be signed...
let xmlToSign = CkoXml()!
xmlToSign.tag = "soapenv:Envelope"
xmlToSign.addAttribute(name: "xmlns:obs", value: "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/")
xmlToSign.addAttribute(name: "xmlns:soapenv", value: "http://schemas.xmlsoap.org/soap/envelope/")
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", autoCreate: true, attrName: "soapenv:mustUnderstand", attrValue: "1")
xmlToSign.updateAttrAt(tagPath: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "EncodingType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
xmlToSign.updateAttrAt(tagPath: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "ValueType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")
xmlToSign.updateAttrAt(tagPath: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "wsu:Id", attrValue: "X509-02BF0107214FC61449FD0013DF68F0359")
// Note: The content of this XML node is a placeholder that will be updated below with the X509PKIPathv1 for the signing certificate.
xmlToSign.updateChildContent(tagPath: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", value: "BinarySecurityToken_Base64Binary_Content")
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.updateAttrAt(tagPath: "soapenv:Body", autoCreate: true, attrName: "wsu:Id", attrValue: "id-396BB6026342EB5C0E1EA73593B3CC098")
xmlToSign.updateChildContent(tagPath: "soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy", value: "000000011986")
xmlToSign.updateChildContent(tagPath: "soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego", value: "PA")
let gen = CkoXmlDSigGen()!
gen.sigLocation = "soapenv:Envelope|soapenv:Header|wsse:Security"
gen.sigLocationMod = 0
gen.sigId = "SIG-BB965DFC3C8AAF87903C0ED898B8D2A8D"
gen.sigNamespacePrefix = "ds"
gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
gen.signedInfoCanonAlg = "EXCL_C14N"
gen.signedInfoDigestMethod = "sha1"
// Set the KeyInfoId before adding references..
gen.keyInfoId = "KI-9D95C38916099AD2EE87DDAC1A76E97E4"
// -------- Reference 1 --------
gen.addSameDocRef(id: "id-396BB6026342EB5C0E1EA73593B3CC098", digestMethod: "sha1", canonMethod: "EXCL_C14N", prefixList: "obs", refType: "")
// The reference to be produced in the Signature should look like this:
// <ds:Reference URI="#id-396BB6026342EB5C0E1EA73593B3CC098">
// <ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
// <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="obs"></ec:InclusiveNamespaces>
// </ds:Transform></ds:Transforms>
// <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></ds:DigestMethod>
// <ds:DigestValue>2e9hZYj/CN2nPsgQqUraU43k3ds=</ds:DigestValue>
// </ds:Reference>
//
// 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: "wsse11:TokenType", value: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")
xmlCustomKeyInfo.addAttribute(name: "xmlns:wsse11", value: "http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd")
xmlCustomKeyInfo.addAttribute(name: "wsu:Id", value: "STR-FF238E7C061332C5B19752C2FBC8CDEF2")
xmlCustomKeyInfo.updateAttrAt(tagPath: "wsse:Reference", autoCreate: true, attrName: "URI", attrValue: "#X509-02BF0107214FC61449FD0013DF68F0359")
xmlCustomKeyInfo.updateAttrAt(tagPath: "wsse:Reference", autoCreate: true, attrName: "ValueType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")
xmlCustomKeyInfo.emitXmlDecl = false
gen.customKeyInfoXml = xmlCustomKeyInfo.getXml()
// Load XML to be signed...
let sbXml = CkoStringBuilder()!
xmlToSign.getSb(sb: sbXml)
// Update BinarySecurityToken_Base64Binary_Content with the actual X509PKIPathv1 of the signing cert.
var nReplaced: Int = sbXml.replace(value: "BinarySecurityToken_Base64Binary_Content", replacement: cert.x509PKIPathv1()).intValue
gen.behaviors = "IndentedSignature"
// Sign the XML...
success = gen.createXmlDSigSb(sbXml: sbXml)
if success != true {
print("\(gen.lastErrorText!)")
return
}
// -----------------------------------------------
// Save the signed XML to a file.
success = sbXml.writeFile(path: "qa_output/signedXml.xml", charset: "utf-8", emitBom: false)
print("\(sbXml.getAsString()!)")
// ----------------------------------------
// Verify the signatures we just produced...
let verifier = CkoXmlDSig()!
success = verifier.loadSignatureSb(sbXmlSig: sbXml)
if success != true {
print("\(verifier.lastErrorText!)")
return
}
var numSigs: Int = verifier.numSignatures.intValue
var verifyIdx: Int = 0
while verifyIdx < numSigs {
verifier.selector = verifyIdx
var verified: Bool = verifier.verifySignature(verifyReferenceDigests: true)
if verified != true {
print("\(verifier.lastErrorText!)")
return
}
verifyIdx = verifyIdx + 1
}
print("All signatures were successfully verified.")
}