Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.77+

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

Lua
local chilkat = require("chilkat")

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

local success = true
-- Create the above XML to be signed...
local xmlToSign = chilkat.Xml.new()
xmlToSign.tag = "soapenv:Envelope"
xmlToSign:add_attribute("xmlns:obs", "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/")
xmlToSign:add_attribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/")
xmlToSign:update_attr_at("soapenv:Header|wsse:Security", true, "xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
xmlToSign:update_attr_at("soapenv:Header|wsse:Security", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
xmlToSign:update_attr_at("soapenv:Header|wsse:Security", true, "soapenv:mustUnderstand", "1")
xmlToSign:update_attr_at("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
xmlToSign:update_attr_at("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")
xmlToSign:update_attr_at("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "wsu:Id", "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:update_child_content("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", "BinarySecurityToken_Base64Binary_Content")
xmlToSign:update_attr_at("soapenv:Body", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
xmlToSign:update_attr_at("soapenv:Body", true, "wsu:Id", "id-396BB6026342EB5C0E1EA73593B3CC098")
xmlToSign:update_child_content("soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy", "000000011986")
xmlToSign:update_child_content("soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego", "PA")

local gen = chilkat.XmlDSigGen.new()

gen.sig_location = "soapenv:Envelope|soapenv:Header|wsse:Security"
gen.sig_location_mod = 0
gen.sig_id = "SIG-BB965DFC3C8AAF87903C0ED898B8D2A8D"
gen.sig_namespace_prefix = "ds"
gen.sig_namespace_uri = "http://www.w3.org/2000/09/xmldsig#"
gen.signed_info_canon_alg = "EXCL_C14N"
gen.signed_info_digest_method = "sha1"

-- Set the KeyInfoId before adding references..
gen.key_info_id = "KI-9D95C38916099AD2EE87DDAC1A76E97E4"

-- -------- Reference 1 --------
gen:add_same_doc_ref("id-396BB6026342EB5C0E1EA73593B3CC098", "sha1", "EXCL_C14N", "obs", "")

-- 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)
local cert = chilkat.Cert.new()
success = cert:load_pfx_file("qa_data/pfx/cert_test123.pfx", "test123")
if not success then
    print(cert.last_error_text)
    return
end

gen:set_x509_cert(cert, true)

gen.key_info_type = "Custom"

-- Create the custom KeyInfo XML..
local xmlCustomKeyInfo = chilkat.Xml.new()
xmlCustomKeyInfo.tag = "wsse:SecurityTokenReference"
xmlCustomKeyInfo:add_attribute("wsse11:TokenType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")
xmlCustomKeyInfo:add_attribute("xmlns:wsse11", "http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd")
xmlCustomKeyInfo:add_attribute("wsu:Id", "STR-FF238E7C061332C5B19752C2FBC8CDEF2")
xmlCustomKeyInfo:update_attr_at("wsse:Reference", true, "URI", "#X509-02BF0107214FC61449FD0013DF68F0359")
xmlCustomKeyInfo:update_attr_at("wsse:Reference", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1")

xmlCustomKeyInfo.emit_xml_decl = false
gen.custom_key_info_xml = xmlCustomKeyInfo:get_xml()

-- Load XML to be signed...
local sbXml = chilkat.StringBuilder.new()
xmlToSign:get_xml_sb(sbXml)

-- Update BinarySecurityToken_Base64Binary_Content with the actual X509PKIPathv1 of the signing cert.
local nReplaced = sbXml:replace("BinarySecurityToken_Base64Binary_Content", cert:x509_pki_pathv1())

gen.behaviors = "IndentedSignature"

-- Sign the XML...
success = gen:create_xml_dsig_sb(sbXml)
if not success then
    print(gen.last_error_text)
    return
end

-- -----------------------------------------------

-- Save the signed XML to a file.
success = sbXml:write_file("qa_output/signedXml.xml", "utf-8", false)

print(sbXml:get_as_string())

-- ----------------------------------------
-- Verify the signatures we just produced...
local verifier = chilkat.XmlDSig.new()
success = verifier:load_signature_sb(sbXml)
if not success then
    print(verifier.last_error_text)
    return
end

local numSigs = verifier.num_signatures
local verifyIdx = 0
while verifyIdx < numSigs do
    verifier.selector = verifyIdx
    local verified = verifier:verify_signature(true)
    if not verified then
        print(verifier.last_error_text)
        return
    end

    verifyIdx = verifyIdx + 1
end

print("All signatures were successfully verified.")