Sample code for 30+ languages & platforms
Lua

Get Certificates within XML Signature

See more XML Digital Signatures Examples

Demonstrates how to get the certificates contained within an XML signature.

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.

local sbXml = chilkat.StringBuilder.new()

-- Load XML containing one or more signatures.
local success = sbXml:load_file("qa_data/xml_dsig_valid_samples/multipleSigners/sp.pdf.XAdES.xml", "utf-8")
if not success then
    print("Failed to load the XML file..")
    return
end

local dsig = chilkat.XmlDSig.new()

-- First load the XML containing the signatures to be verified.
-- Note that this particular Signature already contains the RSA public key that will be used
-- for verification.
success = dsig:load_signature_sb(sbXml)
if not success then
    print(dsig.last_error_text)
    return
end

-- For each signature, verify and also get the certificate(s) contained within each Signature.
local i = 0
local saCerts = chilkat.StringArray.new()
local cert = chilkat.Cert.new()

print("numSignatures = " .. dsig.num_signatures)

while i < dsig.num_signatures do
    -- Select the Nth signature by setting the Selector property.
    dsig.selector = i

    local bVerifyReferenceDigests = true
    local bVerified = dsig:verify_signature(bVerifyReferenceDigests)
    print("Signature " .. (i + 1) .. " verified = " .. tostring(bVerified))

    -- Get the certificates embedded in this signature.
    saCerts:clear()
    success = dsig:get_certs(saCerts)
    if success then
        local j = 0
        while j < saCerts.count do
            success = cert:load_from_base64(saCerts:get_string(j))
            if success then
                print("    " .. cert.subject_dn)
            end

            j = j + 1
        end
    end

    i = i + 1
end