Lua
Lua
Create XAdES using Smart Card or USB Token
See more XAdES Examples
Demonstrates how to create an XAdES signed XML document using a certificate located on a smartcard or USB token.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Load the XML to be signed.
local xmlToSign = chilkat.Xml.new()
local success = xmlToSign:load_xml_file("qa_data/fattura_electronica/docToSign.xml")
if not success then
print(xmlToSign.last_error_text)
return
end
local gen = chilkat.XmlDSigGen.new()
gen.sig_location = "p:FatturaElettronica"
gen.sig_id = "xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504"
gen.sig_namespace_prefix = "ds"
gen.sig_namespace_uri = "http://www.w3.org/2000/09/xmldsig#"
gen.sig_value_id = "xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-sigvalue"
gen.signed_info_canon_alg = "C14N"
gen.signed_info_digest_method = "sha256"
-- Create an Object to be added to the Signature.
-- Note: Chilkat will automatically populate the strings indicated by "TO BE GENERATED BY CHILKAT" with actual/correct values
-- when the XML is signed.
local object1 = chilkat.Xml.new()
object1.tag = "xades:QualifyingProperties"
object1:add_attribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#")
object1:add_attribute("xmlns:xades141", "http://uri.etsi.org/01903/v1.4.1#")
object1:add_attribute("Target", "#xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504")
object1:update_attr_at("xades:SignedProperties", true, "Id", "xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops")
object1:update_child_content("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", "TO BE GENERATED BY CHILKAT")
object1:update_attr_at("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod", true, "Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256")
object1:update_child_content("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue", "TO BE GENERATED BY CHILKAT")
object1:update_child_content("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2", "TO BE GENERATED BY CHILKAT")
gen:add_object("", object1:get_xml(), "", "")
-- -------- Reference 1 --------
gen.key_info_id = "xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo"
gen:add_same_doc_ref("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo", "sha256", "", "", "")
-- -------- Reference 2 --------
gen:add_same_doc_ref("", "sha256", "", "", "")
gen:set_ref_id_attr("", "xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-ref0")
-- -------- Reference 3 --------
gen:add_object_ref("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops", "sha256", "", "", "http://uri.etsi.org/01903#SignedProperties")
-- ----------------------------------------------------------------
-- Load a certificate that has been pre-installed on the Windows system
-- This includes certificates on smartcards and USB tokens
local cert = chilkat.Cert.new()
-- You may provide the PIN here..
cert.smart_card_pin = "000000"
-- Load the certificate on the smartcard currently in the reader (or on the USB token).
-- Pass an empty string to allow Chilkat to automatically choose the CSP (Cryptographi Service Provider).
-- See Load Certificate on Smartcard for information about explicitly selecting a particular CSP.
success = cert:load_from_smartcard("")
if not success then
print(cert.last_error_text)
return
end
gen:set_x509_cert(cert, true)
gen.key_info_type = "X509Data"
gen.x509_type = "Certificate"
-- Load XML to be signed...
local sbXml = chilkat.StringBuilder.new()
xmlToSign:get_xml_sb(sbXml)
gen.behaviors = "IndentedSignature,ForceAddEnvelopedSignatureTransform"
-- 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 signature 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 verified = verifier:verify_signature(true)
if not verified then
print(verifier.last_error_text)
return
end
print("This signature was successfully verified.")