CertChain Lua Reference Documentation

chilkat.CertChain

Current Version: 11.7.0

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

Chilkat.CertChain

Inspect, verify, export, and work with X.509 certificate chains.

Chilkat.CertChain is a compact helper class for representing and working with X.509 certificate chains. It lets applications inspect the ordered certificates in a chain, retrieve individual certificates, determine whether the chain reaches a root, check for expired certificates, verify chain signatures, test whether the root is trusted, load certificate chains from JWK x5c data, and export the chain as X509PKIPathv1.

Chain inspection

Examine the certificates in chain order and retrieve individual certificates with CertAt.

Root and trust checks

Determine whether the chain reaches a root certificate and whether that root is trusted.

Signature verification

Verify that each certificate in the chain is signed by the next certificate in the chain.

Expiration checks

Detect whether one or more certificates in the chain are expired.

JWK x5c support

Load certificate chains from JWK x5c arrays used by JWT, JWS, and other security-token workflows.

Chain export

Export the certificate chain in X509PKIPathv1 format when that representation is required.

Common pattern: Use CertChain to reason about the chain as a whole. For certificate-level details such as subject, issuer, SAN values, key usage, validity dates, fingerprints, serial numbers, and extensions, retrieve each certificate with CertAt and inspect it using Chilkat.Cert.

Object Creation

local chilkat = require("chilkat")

local cert_chain = chilkat.CertChain.new()
local cert_chain = chilkat.CertChain.new()

Creates the object (chilkat.CertChain() does the same). Lua's garbage collector frees it when it is no longer referenced; cert_chain:dispose() frees it at once, and in Lua 5.4 and later local cert_chain <close> = chilkat.CertChain.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 = cert_chain: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 (cert_chain.verbose_logging = true) and methods use the colon (cert_chain: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 = cert_chain.debug_log_file_path
cert_chain.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
LastErrorHtml
-- strVal is a string (read-only)
local strVal = cert_chain.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 = cert_chain.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 = cert_chain.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 = cert_chain.last_method_success
cert_chain.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
NumCerts
-- intVal is an integer (read-only)
local intVal = cert_chain.num_certs
Introduced in version 9.5.0.40

The number of certificates currently contained in the chain.

Index Meaning
0 The target or end-entity certificate—the certificate farthest from the root end of the chain
1 ... NumCerts - 2 Intermediate CA certificates, when present
NumCerts - 1 The highest certificate available in the chain
Do not assume the final certificate is trusted: If ReachesRoot is false, the final certificate may only be the highest intermediate that was available. Even when the chain ends in a self-signed root, trust must be established separately.

top
NumExpiredCerts
-- intVal is an integer (read-only)
local intVal = cert_chain.num_expired_certs
Introduced in version 9.5.0.40

The number of certificates in the chain whose validity period has expired.

Expiration is only one validation check: A value of 0 does not mean the chain is valid or trusted. It does not by itself establish that certificates are already valid, correctly signed, unrevoked, issued by an accepted trust anchor, or suitable for the intended identity and key usage.

Retrieve individual certificates with CertAt when the application needs to inspect exact validity dates or identify which certificates are expired.

top
ReachesRoot
-- boolVal is a boolean (read-only)
local boolVal = cert_chain.reaches_root
Introduced in version 9.5.0.44

true if the chain extends to a terminal root certificate rather than stopping at an intermediate certificate. When this property is true, the certificate at index NumCerts - 1 is the root-end certificate of the chain.

Root presence is not root trust: An included root is commonly self-signed, but its presence does not make it a trusted anchor. Use IsRootTrusted with the application's approved TrustedRoots collection to test trust-anchor membership.

A value of false means the chain is incomplete at the root end; the final certificate may be an intermediate CA for which no issuer certificate was available.

top
UncommonOptions
-- strVal is a string
local strVal = cert_chain.uncommon_options
cert_chain.uncommon_options = strVal
Introduced in version 9.5.0.98

Specifies uncommon behavior as a comma-separated list of keywords. The default is the empty string, which enables the standard behavior.

Keyword Effect
PkiPathV1.ExcludeRoot Omits the root certificate from the value returned by X509PKIPathv1. This option affects that method only.
Interoperability note: Some protocols transport the trust anchor with the path, while others expect the recipient to obtain or trust it independently. Use this option according to the format required by the receiving system.

top
VerboseLogging
-- boolVal is a boolean
local boolVal = cert_chain.verbose_logging
cert_chain.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 = cert_chain.version

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

More Information and Examples
top

Methods

CertAt
-- index is an integer
-- cert is a chilkat.Cert
-- ok is true, or nil and err on failure
local ok, err = cert_chain:cert_at(index, cert)
Introduced in version 11.0.0

Populates cert with the certificate at the specified zero-based index. Valid indices range from 0 through NumCerts - 1.

Index Position in the chain
0 Target or end-entity certificate
1 ... NumCerts - 2 Intermediate CA certificates, when present
NumCerts - 1 Highest certificate available in the chain; a root certificate only when ReachesRoot is true

Returns true on success. Returns false if the index is invalid or the certificate cannot be returned; in that case, do not use the contents of cert as a successful result.

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
IsRootTrusted
-- trusted_roots is a chilkat.TrustedRoots
-- flag is true or false
local flag = cert_chain:is_root_trusted(trusted_roots)
Introduced in version 9.5.0.40

Tests whether the certificate at the root end of this chain is present in the TrustedRoots collection passed in trusted_roots. Returns true when a matching trusted-root certificate is found; otherwise returns false.

Trust is a local policy decision: A self-signed certificate is not trusted merely because it appears at the end of a chain. This method checks membership in the supplied trust collection; it does not, by itself, establish that the entire chain is valid for a particular application or identity.

A complete certificate-validation decision may also require checking signature linkage, certificate validity periods, revocation status, basic constraints, key usage, policies, name constraints, and application-specific identity requirements.

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

top
LoadX5C
-- jwk is a chilkat.JsonObject
-- ok is true, or nil and err on failure
local ok, err = cert_chain:load_x5_c(jwk)
Introduced in version 9.5.0.66

Loads the certificate chain from the x5c member of the JWK contained in jwk. Returns true if the chain is loaded successfully; otherwise returns false.

As defined by RFC 7517, x5c is a JSON array of DER-encoded X.509 certificates represented using standard Base64—not Base64URL. The first certificate contains the key associated with the JWK. Each following certificate should certify the certificate immediately before it, so the array order is target certificate first and rootward certificates afterward.

Security note: Loading an x5c array does not make its certificates trusted. Treat the chain as untrusted input until the required signature, validity, trust-anchor, revocation, and application-policy checks have been completed.

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
VerifyCertSignatures
-- flag is true or false
local flag = cert_chain:verify_cert_signatures()
Introduced in version 9.5.0.40

Verifies the cryptographic signature linkage of the certificates in this chain. Starting with the target certificate at index 0, each certificate's signature is checked using the public key of the next issuer certificate in the chain. Returns true when the certificate signatures are valid through the root end of the chain; otherwise returns false.

Signature verification is not full path validation: A true result confirms the issuer-signature relationships represented by the chain. It does not by itself prove that the root is trusted, that certificates are within their validity periods, that none are revoked, that CA constraints and policies are acceptable, or that the target certificate is valid for a particular hostname or usage.

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

top
X509PKIPathv1
-- str is a string, or nil and err on failure
local str, err = cert_chain:x509_pki_pathv1()
Introduced in version 9.5.0.88

Returns the Base64 representation of a DER-encoded ASN.1 PkiPath containing the certificates in this chain. This representation is commonly identified as the X509PKIPathv1 token type in WS-Security and may also be encountered in XML-signature workflows.

A PkiPath is a SEQUENCE OF Certificate ordered from the root or most-trusted end toward the target certificate. This is the reverse of the indexing used by CertAt, where index 0 is the target certificate.

Representation Certificate order
CertChain indexing Target/leaf → intermediates → root
X509PKIPathv1 Root → intermediates → target/leaf

By default, the root certificate is included when it is present in the chain. Set UncommonOptions to PkiPathV1.ExcludeRoot when the receiving protocol expects the trust anchor to be supplied separately.

Return-value note: This is a string-returning method. Check LastMethodSuccess before using the returned text, and inspect LastErrorText if the operation fails.

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