XmlDSig Lua Reference Documentation

chilkat.XmlDSig

Current Version: 11.7.0

Markdown version of this page for AI tools (v 11.7.0) · all AI docs

Chilkat.XmlDSig

Load, inspect, canonicalize, and verify XML Digital Signatures.

Chilkat.XmlDSig loads and verifies XML Digital Signatures. It can inspect signed XML, select among multiple signatures in one document, verify the overall SignatureValue, verify individual Reference digests, handle external references, extract KeyInfo, retrieve embedded certificates or public keys, canonicalize XML, and add an EncapsulatedTimeStamp to an existing signature.

Load signed XML

Load an XML document containing one or more digital signatures and inspect the signature structure before verification.

Select a signature

Work with documents that contain multiple signatures by selecting the specific signature to inspect or verify.

Verify SignatureValue

Verify the cryptographic signature over the canonicalized SignedInfo block.

Verify Reference digests

Check individual Reference digest values to confirm that each signed item still matches what was originally signed.

Certificates and keys

Extract KeyInfo, embedded signing certificates, or public keys used to validate the signature.

Canonicalization and timestamps

Canonicalize XML for signature analysis and add an EncapsulatedTimeStamp when timestamping an existing signature.

Common pattern: Load the signed XML, select the desired signature if more than one is present, inspect the signing certificate or public key information, verify the SignatureValue, then verify the individual reference digests. Use XmlDSig for verification and inspection; use Chilkat.XmlDSigGen when creating new XML Digital Signatures.

Object Creation

local chilkat = require("chilkat")

local xml_dsig = chilkat.XmlDSig.new()
local xml_dsig = chilkat.XmlDSig.new()

Creates the object (chilkat.XmlDSig() does the same). Lua's garbage collector frees it when it is no longer referenced; xml_dsig:dispose() frees it at once, and in Lua 5.4 and later local xml_dsig <close> = chilkat.XmlDSig.new() frees it when the block ends. Calling a method of a disposed object raises an error.

Errors

A method that fails returns nil followed by the error text, as io.open does: local body, err = xml_dsig:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.

Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.

Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (xml_dsig.verbose_logging = true) and methods use the colon (xml_dsig:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.

Properties

DebugLogFilePath
-- strVal is a string
local strVal = xml_dsig.debug_log_file_path
xml_dsig.debug_log_file_path = strVal

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
ExternalRefDirs
-- strVal is a string
local strVal = xml_dsig.external_ref_dirs
xml_dsig.external_ref_dirs = strVal
Introduced in version 9.5.0.77

May contain a set of directory paths specifying where referenced external files are located. Directory paths should be separated using a semicolon character. The default value of this property is the empty string which means no directories are automatically searched.

This property can be used if the external file referenced in the XML signature has the same filename as the file in the local filesystem.

More Information and Examples
top
IgnoreExternalRefs
-- boolVal is a boolean
local boolVal = xml_dsig.ignore_external_refs
xml_dsig.ignore_external_refs = boolVal
Introduced in version 9.5.0.77

If true, then ignore failures caused by external references not being available. This allows for the XML signature to be at least partially validated if the external referenced files are not available. The default value of this property is false.

top
LastErrorHtml
-- strVal is a string (read-only)
local strVal = xml_dsig.last_error_html

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
-- strVal is a string (read-only)
local strVal = xml_dsig.last_error_text

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
-- strVal is a string (read-only)
local strVal = xml_dsig.last_error_xml

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
-- boolVal is a boolean
local boolVal = xml_dsig.last_method_success
xml_dsig.last_method_success = boolVal

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
NumReferences
-- intVal is an integer (read-only)
local intVal = xml_dsig.num_references
Introduced in version 9.5.0.69

The number of data objects referenced in the XML digital signature. A data object may be self-contained within the loaded XML signature, or it may be an external URI reference. An application can check each reference to see if it's external by calling IsReferenceExternal, and can get each reference URI by calling ReferenceUri.

top
NumSignatures
-- intVal is an integer (read-only)
local intVal = xml_dsig.num_signatures
Introduced in version 9.5.0.69

The number of digital signatures found within the loaded XML. Each digital signature is composed of XML having the following structure:

  <Signature ID?> 
     <SignedInfo>
       <CanonicalizationMethod/>
       <SignatureMethod/>
       (<Reference URI? >
         (<Transforms>)?
         <DigestMethod>
         <DigestValue>
       </Reference>)+
     </SignedInfo>
     <SignatureValue> 
    (<KeyInfo>)?
    (<Object ID?>)*
 </Signature>
Note: The Signature and other XML tags may be namespace prefixed.

The Selector property is used to select which XML signature is in effect when validating or calling other methods or properties.

More Information and Examples
top
RefFailReason
-- intVal is an integer (read-only)
local intVal = xml_dsig.ref_fail_reason
Introduced in version 9.5.0.77

Indicates the failure reason for the last call to VerifyReferenceDigest. Possible values are:

  • 0: No failure, the reference digest was valid.
  • 1: The computed digest differs from the digest stored in the XML.
  • 2: An external file is referenced, but it is unavailable for computing the digest.
  • 3: The index argument passed to VerifyReferenceDigest was out of range.
  • 4: Unable to find the Signature.
  • 5: A transformation specified some sort of XML canonicalization that is not supported.
  • 99: Unknown. (Should never get this value.)

top
Selector
-- intVal is an integer
local intVal = xml_dsig.selector
xml_dsig.selector = intVal
Introduced in version 9.5.0.69

If the loaded XML contains multiple signatures, this property can be set to specify which signature is in effect when calling other methods or properties. In most cases, the loaded XML contains a single signature and this property will remain at the default value of 0.

More Information and Examples
top
UncommonOptions
-- strVal is a string
local strVal = xml_dsig.uncommon_options
xml_dsig.uncommon_options = strVal
Introduced in version 9.5.0.90

This is a catch-all property to be used for uncommon needs. This property defaults to the empty string, and should typically remain empty.

top
VerboseLogging
-- boolVal is a boolean
local boolVal = xml_dsig.verbose_logging
xml_dsig.verbose_logging = boolVal

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
-- strVal is a string (read-only)
local strVal = xml_dsig.version

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

AddEncapsulatedTimeStamp
-- json is a chilkat.JsonObject
-- sb_out is a chilkat.StringBuilder
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:add_encapsulated_time_stamp(json, sb_out)
Introduced in version 9.5.0.90

Adds an EncapsulatedTimeStamp to the signature indicated by the Selector property. The updated signed XML with the added EncapsulatedTimeStamp is returned in sb_out. See the example below for details.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
CanonicalizeFragment
-- xml is a string
-- fragment_id is a string
-- version is a string
-- prefix_list is a string
-- with_comments is a boolean
-- str is a string, or nil and err on failure
local str, err = xml_dsig:canonicalize_fragment(xml, fragment_id, version, prefix_list, with_comments)

Applies XML canonicalization to a fragment of the passed-in XML, and returns the canonicalized XML string. The fragment_id identifies the XML element where output begins. It is the XML element having an id attribute equal to fragment_id. The version may be one of the following:

  • C14N -- for Inclusive Canonical XML
  • EXCL_C14N -- for Exclusive Canonical XML

The prefix_list only applies when the version is set to EXCL_C14N. The prefix_list may be an empty string, or a SPACE separated list of namespace prefixes. It is the InclusiveNamespaces PrefixList, which is the list of namespaces that are propagated from ancestor elements for canonicalization purposes.

If with_comments is true, then XML comments are included in the output. If with_comments is false, then XML comments are excluded from the output.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
CanonicalizeXml
-- xml is a string
-- version is a string
-- with_comments is a boolean
-- str is a string, or nil and err on failure
local str, err = xml_dsig:canonicalize_xml(xml, version, with_comments)

Applies XML canonicalization to the passed-in XML, and returns the canonicalized XML string. The version may be one of the following:

  • C14N -- for Inclusive Canonical XML
  • EXCL_C14N -- for Exclusive Canonical XML

If with_comments is true, then XML comments are included in the output. If with_comments is false, then XML comments are excluded from the output.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
GetCerts
-- sa is a chilkat.StringArray
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:get_certs(sa)
Introduced in version 9.5.0.76

Returns the certificates found in the signature indicated by the Selector property. The base64 representation of each certificate is returned.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
GetKeyInfo
-- xml is a chilkat.Xml, or nil and err on failure
local xml, err = xml_dsig:get_key_info()
Introduced in version 9.5.0.69

Returns the KeyInfo XML for the signature indicated by the Selector property. Returns nil if no KeyInfo exists.

Returns a new object, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead. The returned object is independent of this one.

More Information and Examples
top
GetPublicKey
-- public_key is a chilkat.PublicKey, or nil and err on failure
local public_key, err = xml_dsig:get_public_key()
Introduced in version 9.5.0.69

Returns the public key from the KeyInfo XML for the signature indicated by the Selector property. Returns nil if no KeyInfo exists, or if no public key is actually contained in the KeyInfo.

Returns a new object, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead. The returned object is independent of this one.

top
HasEncapsulatedTimeStamp
-- flag is true or false
local flag = xml_dsig:has_encapsulated_time_stamp()
Introduced in version 9.5.0.90

Returns true if the signature indicated by the Selector property contains an EncapsulatedTimeStamp. Otherwise returns false.

Returns true or false: an answer, not a success flag.

top
IsReferenceExternal
-- index is an integer
-- flag is true or false
local flag = xml_dsig:is_reference_external(index)
Introduced in version 9.5.0.69

Returns true if the reference at index is external. Each external reference would first need to be provided by the application prior to validating the signature.

Returns true or false: an answer, not a success flag.

top
LoadSignature
-- xml_sig is a string
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:load_signature(xml_sig)
Introduced in version 9.5.0.69

Loads an XML document containing 1 or more XML digital signatures. An application would first load XML containing digital signature(s), then validate. After loading, it is also possible to use various methods and properties to get information about the signature, such as the key info, references, etc. If external data is referenced by the signature, it may be necessary to provide the referenced data prior to validating.

Note: When loading an XML document, the Selector property is automatically reset to 0, and the NumSignatures property is set to the number of XML digital signatures found.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
LoadSignatureBd
-- bin_data is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:load_signature_bd(bin_data)
Introduced in version 9.5.0.69

Loads an XML document containing one or more XML digital signatures from the contents of a BinData object. An application would first load the XML, then validate. After loading, it is also possible to use various methods and properties to get information about the signature, such as the key info, references, etc. If external data is referenced by the signature, it may be necessary to provide the referenced data prior to validating.

Note: When loading an XML document, the Selector property is automatically reset to 0, and the NumSignatures property is set to the number of XML digital signatures found.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
LoadSignatureSb
-- sb_xml_sig is a chilkat.StringBuilder
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:load_signature_sb(sb_xml_sig)
Introduced in version 9.5.0.69

Loads an XML document containing one or more XML digital signatures from the contents of a StringBuilder object. An application would first load the XML, then validate. After loading, it is also possible to use various methods and properties to get information about the signature, such as the key info, references, etc. If external data is referenced by the signature, it may be necessary to provide the referenced data prior to validating.

Note: When loading an XML document, the Selector property is automatically reset to 0, and the NumSignatures property is set to the number of XML digital signatures found.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
ReferenceUri
-- index is an integer
-- str is a string, or nil and err on failure
local str, err = xml_dsig:reference_uri(index)
Introduced in version 9.5.0.69

Returns the URI of the Nth reference specified by index. (The 1st reference is at index 0.) URI's beginning with a pound sign ('#') are internal references.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetHmacKey
-- key is a string
-- encoding is a string
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:set_hmac_key(key, encoding)
Introduced in version 9.5.0.69

Sets the HMAC key to be used if the Signature used an HMAC signing algorithm. The encoding specifies the encoding of key, and can be hex, base64, ascii, or any of the binary encodings supported by Chilkat in the link below.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetHttpObj
-- http is a chilkat.Http
xml_dsig:set_http_obj(http)
Introduced in version 9.5.0.90

Sets the HTTP object to be used to communicate with OCSP responders, CRL distribution points, or timestamp authority (TSA) servers if needed. The http is used to send the requests, and it allows for connection related settings and timeouts to be set. For example, if HTTP or SOCKS proxies are required, these features can be specified on the http.

top
SetPublicKey
-- pub_key is a chilkat.PublicKey
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:set_public_key(pub_key)
Introduced in version 9.5.0.69

Sets the public key to be used for verifying the signature indicated by the Selector property. A public key only needs to be explicitly provided by the application for those XML signatures where the public key is not already available within the KeyInfo of the Signature. In some cases, the KeyInfo within the Signature contains information about what public key was used for signing. The application can use this information to retrieve the matching public key and provide it via this method.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
SetRefDataBd
-- index is an integer
-- bin_data is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:set_ref_data_bd(index, bin_data)
Introduced in version 9.5.0.69

Provides the binary data for the external reference at index.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetRefDataFile
-- index is an integer
-- path is a string
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:set_ref_data_file(index, path)
Introduced in version 9.5.0.69

Provides the data for the external reference at index. When validating the signature, the data contained in path will be streamed to compute the digest for validation.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetRefDataSb
-- index is an integer
-- sb is a chilkat.StringBuilder
-- charset is a string
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:set_ref_data_sb(index, sb, charset)
Introduced in version 9.5.0.69

Provides the text data for the external reference at index. The charset specifies the byte representation of the text, such as utf-8, utf-16, windows-1252, etc. (If in doubt, try utf-8 first.)

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
UseCertVault
-- cert_vault is a chilkat.XmlCertVault
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:use_cert_vault(cert_vault)
Introduced in version 9.5.0.69

Adds an XML certificate vault to the object's internal list of sources to be searched for certificates having public keys when verifying an XML signature. A single XML certificate vault may be used. If UseCertVault is called multiple times, only the last certificate vault will be used, as each call to UseCertVault will replace the certificate vault provided in previous calls.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
VerifyReferenceDigest
-- index is an integer
-- flag is true or false
local flag = xml_dsig:verify_reference_digest(index)
Introduced in version 9.5.0.69

This method allows for an application to verify the digest for each reference separately. This can be helpful if the full XMLDSIG validation fails, then one can test each referenced data's digest to see which, if any, fail to match.

Returns true or false: an answer, not a success flag.

top
VerifySignature
-- verify_reference_digests is a boolean
-- ok is true, or nil and err on failure
local ok, err = xml_dsig:verify_signature(verify_reference_digests)
Introduced in version 9.5.0.69

Verifies the signature indicated by the Selector property. If verify_reference_digests is true, then the digest of each Reference within the signature's SignedInfo is also validated.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top