项目作者: fffonion

项目描述 :
FFI-based OpenSSL binding for OpenResty
高级语言: Lua
项目地址: git://github.com/fffonion/lua-resty-openssl.git
创建时间: 2019-09-25T22:21:08Z
项目社区:https://github.com/fffonion/lua-resty-openssl

开源协议:BSD 2-Clause "Simplified" License

下载


lua-resty-openssl

FFI-based OpenSSL binding for LuaJIT, supporting OpenSSL 3.x, 1.1 series.

OpenSSL 1.1.0, 1.0.2 and BoringSSL support has been dropped, but are still available at the 0.x branch.

Build Status luarocks opm

Table of Contents

Description

lua-resty-openssl is a FFI-based OpenSSL binding library, currently
supports OpenSSL 3.x and 1.1.1 series.

Back to TOC

Status

Production.

Synopsis

This library is greatly inspired by luaossl, while uses the
naming conversion closer to original OpenSSL API.
For example, a function called X509_set_pubkey in OpenSSL C API will expect to exist
as resty.openssl.x509:set_pubkey.
CamelCases are replaced to underscore_cases, for exmaple X509_set_serialNumber becomes
resty.openssl.x509:set_serial_number. Another difference than luaossl is that errors are never thrown
using error() but instead return as last parameter.

Each Lua table returned by new() contains a cdata object ctx. User are not supposed to manully setting
ffi.gc or calling corresponding destructor of the ctx struct (like *_free functions).

Back to TOC

resty.openssl

This meta module provides a version sanity check against linked OpenSSL library.

Back to TOC

openssl.load_library

syntax: name, err = openssl.load_library()

Try to load OpenSSL shared libraries. This function tries couple of known patterns
the library could be named and return the name of crypto library if it’s being
successfully loaded and error if any.

When running inside resty CLI or OpenResty with SSL enabled, calling this function
is not necessary.

Back to TOC

openssl.load_modules

syntax: openssl.load_modules()

Load all available sub modules into current module:

  1. bn = require("resty.openssl.bn"),
  2. cipher = require("resty.openssl.cipher"),
  3. digest = require("resty.openssl.digest"),
  4. hmac = require("resty.openssl.hmac"),
  5. kdf = require("resty.openssl.kdf"),
  6. pkey = require("resty.openssl.pkey"),
  7. objects = require("resty.openssl.objects"),
  8. rand = require("resty.openssl.rand"),
  9. version = require("resty.openssl.version"),
  10. x509 = require("resty.openssl.x509"),
  11. altname = require("resty.openssl.x509.altname"),
  12. chain = require("resty.openssl.x509.chain"),
  13. csr = require("resty.openssl.x509.csr"),
  14. crl = require("resty.openssl.x509.crl"),
  15. extension = require("resty.openssl.x509.extension"),
  16. extensions = require("resty.openssl.x509.extensions"),
  17. name = require("resty.openssl.x509.name"),
  18. store = require("resty.openssl.x509.store"),
  19. ssl = require("resty.openssl.ssl"),
  20. ssl_ctx = require("resty.openssl.ssl_ctx"),

Starting OpenSSL 3.0, provider and mac
ctx
is also available.

Back to TOC

openssl.luaossl_compat

syntax: openssl.luaossl_compat()

Provides luaossl flavored API which uses camelCase naming; user can expect drop in replacement.

For example, pkey:get_parameters is mapped to pkey:getParameters.

Note that not all luaossl API has been implemented, please check readme for source of truth.

Back to TOC

openssl.get_fips_mode

syntax: enabled = openssl.get_fips_mode()

Returns a boolean indicating if FIPS mode is enabled.

Back to TOC

openssl.set_fips_mode

syntax: ok, err = openssl.set_fips_mode(enabled)

Toggle FIPS mode on or off.

lua-resty-openssl supports following modes:

OpenSSL 1.0.2 series with fips 2.0 module

Compile the module per security policy,

OpenSSL 3.0.0 fips provider

Refer to https://wiki.openssl.org/index.php/OpenSSL_3.0 Section 7
Compile the provider per guide, install the fipsmodule.cnf that
matches hash of FIPS provider fips.so.

On OpenSSL 3.0 or later, this function also turns on and off default
properties for EVP functions. When turned on, all applications using
EVP_* API will be redirected to FIPS-compliant implementations and
have no access to non-FIPS-compliant algorithms.

Calling this function is equivalent of loading fips provider and
call openssl.set_default_properties(“fips=yes”).

If fips provider is loaded but default properties are not set, use following
to explictly fetch FIPS implementation.

  1. local provider = require "resty.openssl.provider"
  2. assert(provider.load("fips"))
  3. local cipher = require "resty.openssl.cipher"
  4. local c = assert(cipher.new("aes256"))
  5. print(c:get_provider_name()) -- prints "default"
  6. local c = assert(cipher.new("aes256", "fips=yes"))
  7. print(c:get_provider_name()) -- prints "fips"

Back to TOC

openssl.get_fips_version_text

syntax: text, err = openssl.get_fips_version_text()

Returns the version text of the FIPS module, only available on OpenSSL 3.x.

Back to TOC

openssl.set_default_properties

syntax: ok, err = openssl.set_default_properties(props)

Sets the default properties for all future EVP algorithm fetches, implicit as well as explicit. See “ALGORITHM FETCHING” in crypto(7) for information about implicit and explicit fetching.

Back to TOC

openssl.list_cipher_algorithms

syntax: ret = openssl.list_cipher_algorithms(hide_provider?)

Return available cipher algorithms in an array. Set hide_provider to true to
hide provider name from the result.

Back to TOC

openssl.list_digest_algorithms

syntax: ret = openssl.list_digest_algorithms(hide_provider?)

Return available digest algorithms in an array. Set hide_provider to true to
hide provider name from the result.

Back to TOC

openssl.list_mac_algorithms

syntax: ret = openssl.list_mac_algorithms(hide_provider?)

Return available MAC algorithms in an array. Set hide_provider to true to
hide provider name from the result.

Back to TOC

openssl.list_kdf_algorithms

syntax: ret = openssl.list_kdf_algorithms(hide_provider?)

Return available KDF algorithms in an array. Set hide_provider to true to
hide provider name from the result.

Back to TOC

openssl.list_ssl_ciphers

syntax: cipher_string, err = openssl.list_ssl_ciphers(cipher_list?, ciphersuites?, protocol?)

Return default SSL ciphers as a string. cipher_list (prior TLSv1.3) and
ciphersuites (TLSv1.3) can be used to expand the cipher settings matches
protocol.

  1. openssl.list_ssl_ciphers()
  2. openssl.list_ssl_ciphers("ECDHE-ECDSA-AES128-SHA")
  3. openssl.list_ssl_ciphers("ECDHE-ECDSA-AES128-SHA", nil, "TLSv1.2")
  4. openssl.list_ssl_ciphers("ECDHE-ECDSA-AES128-SHA", "TLS_CHACHA20_POLY1305_SHA256", "TLSv1.3")

Back to TOC

resty.openssl.ctx

A module to provide OSSL_LIB_CTX context switches.

OSSL_LIB_CTX is an internal OpenSSL library context type. Applications may allocate their own, but may also use NULL to use a default context with functions that take an OSSL_LIB_CTX argument.

See OSSL_LIB_CTX.3 for deeper
reading.

The context is currently effective following modules:

This module is only available on OpenSSL 3.0 or later.

Back to TOC

ctx.new

syntax: ok, err = ctx.new(request_context_only?, conf_file?)

Create a new context and use as default context for this module. When
request_context_only is set to true, the context is only used inside current
request’s context. conf_file can optionally specify an OpenSSL conf file
to create the context.

The created context is automatically freed with its given lifecycle.

  1. -- initialize a AES cipher instance from given provider implementation only
  2. -- for current request, without interfering other part of code
  3. -- or future requests from using the same algorithm.
  4. assert(require("resty.openssl.ctx").new(true))
  5. local p = assert(require("resty.openssl.provider").load("myprovider"))
  6. local c = require("resty.openssl.cipher").new("aes256")
  7. print(c:encrypt(string.rep("0", 32), string.rep("0", 16), "🦢"))
  8. -- don't need to release provider and ctx, they are GC'ed automatically

Back to TOC

ctx.free

syntax: ctx.free(request_context_only?)

Free the context that was previously created by ctx.new.

Back to TOC

resty.openssl.err

A module to provide error messages.

Back to TOC

err.format_error

syntax: msg = err.format_error(ctx_msg?, return_code?, all_errors?)

syntax: msg = err.format_all_errors(ctx_msg?, return_code?)

Return the latest error message from the last error code. Errors are formatted as:

  1. [ctx_msg]: code: [return_code]: error:[error code]:[library name]:[func name]:[reason string]:[file name]:[line number]:

On OpenSSL prior to 3.x, errors are formatted as:

  1. [ctx_msg]: code: [return_code]: [file name]:[line number]:error:[error code]:[library name]:[func name]:[reason string]:

If all_errors is set to true, all errors no just the latest one will be returned in a single string. All errors thrown
by this library internally only thrown the latest error.

For example:

  1. local f = io.open("t/fixtures/ec_key_encrypted.pem"):read("*a")
  2. local privkey, err = require("resty.openssl.pkey").new(f, {
  3. format = "PEM",
  4. type = "pr",
  5. passphrase = "wrongpasswrod",
  6. })
  7. ngx.say(err)
  8. -- pkey.new:load_key: error:4800065:PEM routines:PEM_do_header:bad decrypt:crypto/pem/pem_lib.c:467:

Back to TOC

err.get_last_error_code

syntax: code = err.get_last_error_code()

Return the last error code.

Back to TOC

err.get_lib_error_string

syntax: lib_error_message = err.get_lib_error_string(code?)

Return the library name of the last error code as string. If code is set, return the library name
corresponding to provided error code instead.

Back to TOC

err.get_reason_error_string

syntax: reason_error_message = err.get_reason_error_string(code?)

Return the reason of the last error code as string. If code is set, return the reason
corresponding to provided error code instead.

Back to TOC

resty.openssl.version

A module to provide version info.

Back to TOC

version_num

The OpenSSL version number.

Back to TOC

version_text

The OpenSSL version text.

Back to TOC

version.version

syntax: text = version.version(types)

Returns various OpenSSL version information. Available values for types are:

  1. VERSION
  2. CFLAGS
  3. BUILT_ON
  4. PLATFORM
  5. DIR
  6. ENGINES_DIR
  7. VERSION_STRING
  8. FULL_VERSION_STRING
  9. MODULES_DIR
  10. CPU_INFO
  1. local version = require("resty.openssl.version")
  2. ngx.say(string.format("%x", version.version_num))
  3. -- outputs "101000bf"
  4. ngx.say(version.version_text)
  5. -- outputs "OpenSSL 1.1.0k 28 May 2019"
  6. ngx.say(version.version(version.PLATFORM))
  7. -- outputs "darwin64-x86_64-cc"

Back to TOC

version.info

syntax: text = version.info(types)

Returns various OpenSSL information. Available values for types are:

  1. INFO_ENGINES_DIR
  2. INFO_DSO_EXTENSION
  3. INFO_CPU_SETTINGS
  4. INFO_LIST_SEPARATOR
  5. INFO_DIR_FILENAME_SEPARATOR
  6. INFO_CONFIG_DIR
  7. INFO_SEED_SOURCE
  8. INFO_MODULES_DIR

This function is only available on OpenSSL 3.0.
Please refer to
OPENSSL_VERSION_NUMBER(3)
for explanation of each type.

  1. local version = require("resty.openssl.version")
  2. ngx.say(version.info(version.INFO_DSO_EXTENSION))
  3. -- outputs ".so"

Back to TOC

version.OPENSSL_3X

A boolean indicates whether the linked OpenSSL is 3.x series.

Back to TOC

version.OPENSSL_111

A boolean indicates whether the linked OpenSSL is 1.1.1 series.

Back to TOC

resty.openssl.provider

Module to interact with providers. This module only work on OpenSSL >= 3.0.0.

Back to TOC

provider.load

syntax: pro, err = provider.load(name, try?)

Load provider with name. If try is set to true, OpenSSL will not disable the
fall-back providers if the provider cannot be loaded and initialized. If the provider
loads successfully, however, the fall-back providers are disabled.

By default this functions loads provider into the default context, meaning it will affect
other applications in the same process using the default context as well. If such behaviour
is not desired, consider using ctx to load
provider only to limited scope.

Back to TOC

provider.istype

syntax: ok = pkey.provider(table)

Returns true if table is an instance of provider. Returns false otherwise.

Back to TOC

provider.is_available

syntax: ok, err = provider.is_available(name)

Checks if a named provider is available for use.

Back to TOC

provider.set_default_search_path

syntax: ok, err = provider.set_default_search_path(name)

Specifies the default search path that is to be used for looking for providers.

Back to TOC

provider:unload

syntax: ok, err = pro:unload(name)

Unload a provider that is previously loaded by provider.load.

Back to TOC

provider:self_test

syntax: ok, err = pro:self_test(name)

Runs a provider’s self tests on demand. If the self tests fail then the provider
will fail to provide any further services and algorithms.

Back to TOC

provider:get_params

syntax: ok, err = pro:get_params(key1, key2?…)

Returns one or more provider parameter values.

  1. local pro = require "resty.openssl.provider"
  2. local p = pro.load("default")
  3. local name = assert(p:get_params("name"))
  4. print(name)
  5. -- outputs "OpenSSL Default Provider"
  6. local result = assert(p:get_params("name", "version", "buildinfo", "status"))
  7. print(require("cjson").encode(result))
  8. -- outputs '{"buildinfo":"3.0.0-alpha7","name":"OpenSSL Default Provider","status":1,"version":"3.0.0"}'

Back to TOC

resty.openssl.pkey

Module to interact with private keys and public keys (EVP_PKEY).

Each key type may only support part of operations:

Key Type Load existing key Key generation Encrypt/Decrypt Sign/Verify Key Exchange
RSA Y Y Y Y
DH Y Y Y
EC Y Y Y (ECDSA) Y (ECDH)
Ed25519 Y Y Y (PureEdDSA)
X25519 Y Y Y (ECDH)
Ed448 Y Y Y (PureEdDSA)
X448 Y Y Y (ECDH)

Direct support of encryption and decryption for EC and ECX does not exist, but
processes like ECIES is possible with pkey:derive,
kdf and cipher

Back to TOC

pkey.new

Load existing key

syntax: pk, err = pkey.new(string, opts?)

Supports loading a private or public key in PEM, DER or JWK format passed as first argument string.

The second parameter opts accepts an optional table to constraint the behaviour of key loading.

  • opts.format: set explictly to "PEM", "DER", "JWK" to load specific format or set to "*" for auto detect
  • opts.type: set explictly to "pr" for privatekey, "pu" for public key; set to "*" for auto detect

When loading a PEM encoded RSA key, it can either be a PKCS#8 encoded
SubjectPublicKeyInfo/PrivateKeyInfo or a PKCS#1 encoded RSAPublicKey/RSAPrivateKey.

When loading a encrypted PEM encoded key, the passphrase to decrypt it can either be set
in opts.passphrase or opts.passphrase_cb:

  1. pkey.new(pem_or_der_text, {
  2. format = "*", -- choice of "PEM", "DER", "JWK" or "*" for auto detect
  3. type = "*", -- choice of "pr" for privatekey, "pu" for public key and "*" for auto detect
  4. passphrase = "secret password", -- the PEM encryption passphrase
  5. passphrase_cb = function()
  6. return "secret password"
  7. end, -- the PEM encryption passphrase callback function
  8. }

When loading JWK, there are couple of caveats:

  • Make sure the encoded JSON text is passed in, it must have been base64 decoded.
  • Constraint type on JWK key is not supported, the parameters
    in provided JSON will decide if a private or public key is loaded.
  • Only key type of RSA, P-256, P-384 and P-512 EC,
    Ed25519, X25519, Ed448 and X448 OKP keys are supported.
  • Public key part for OKP keys (the x parameter) is always not honored and derived
    from private key part (the d parameter) if it’s specified.
  • Signatures and verification must use ecdsa_use_raw option to work with JWS standards
    for EC keys. See pkey:sign and pkey.verify for detail.
  • When running outside of OpenResty, needs to install a JSON library (cjson or dkjson)
    and basexx.

Back to TOC

Key generation

syntax: pk, err = pkey.new(config?)

Generate a new public key or private key.

To generate RSA key, config table can have bits and exp field to control key generation.
When config is emitted, this function generates a 2048 bit RSA key with exponent of 65537,
which is equivalent to:

  1. local key, err = pkey.new({
  2. type = 'RSA',
  3. bits = 2048,
  4. exp = 65537
  5. })

To generate EC or DH key, please refer to pkey.paramgen for possible values of
config table. For example:

  1. local key, err = pkey.new({
  2. type = 'EC',
  3. curve = 'prime256v1',
  4. })

It’s also possible to pass a PEM-encoded EC or DH parameters to config.param for key generation:

  1. local dhparam = pkey.paramgen({
  2. type = 'DH',
  3. group = 'dh_1024_160'
  4. })
  5. -- OR
  6. -- local dhparam = io.read("dhparams.pem"):read("*a")
  7. local key, err = pkey.new({
  8. type = 'DH',
  9. param = dhparam,
  10. })

It’s also possible to pass raw pkeyopt control strings in config table as used in the genpkey CLI program.
See openssl-genpkey(1) for a list of options.

For example:

  1. pkey.new({
  2. type = 'RSA',
  3. bits = 2048,
  4. exp = 65537,
  5. })
  6. -- is same as
  7. pkey.new({
  8. type = 'RSA',
  9. exp = 65537,
  10. "rsa_keygen_bits:4096",
  11. })

Back to TOC

Key composition

syntax: pk, err = pkey.new(config?)

Compose a public or private key using existing parameters. To see
list of parameters for each key, refer to pkey:set_parameters.

Only type and params should exist in config table, all other keys will be ignored.

  1. local private_bn = require "resty.openssl.bn".new("7F48282CCA4C1A65D589C06DBE9C42AE50FBFFDF3A18CBB48498E1DE47F11BE1A3486CD8FA950D68F111970F922279D8", 16)
  2. local p_384, err = assert(require("resty.openssl.pkey").new({
  3. type = "EC",
  4. params = {
  5. private = private_bn,
  6. group = "secp384r1",
  7. }
  8. }))

Back to TOC

pkey.istype

syntax: ok = pkey.istype(table)

Returns true if table is an instance of pkey. Returns false otherwise.

Back to TOC

pkey.paramgen

syntax: pem_txt, err = pk.paramgen(config)

Generate parameters for EC or DH key and output as PEM-encoded text.

For EC key:

Parameter Description
type "EC"
curve EC curves. If omitted, default to "prime192v1". To see list of supported EC curves, use openssl ecparam -list_curves.

For DH key:

Parameter Description
type "DH"
bits Generate a new DH parameter with bits long prime. If omitted, default to 2048. Starting OpenSSL 3.0, only bits equal to 2048 is allowed.
group Use predefined groups instead of generating new one. bit will be ignored if group is set.

Possible values for group are:

  • RFC7919 "ffdhe2048", "ffdhe3072",
    "ffdhe4096", "ffdhe6144", "ffdhe8192"
  • RFC5114 "dh_1024_160", "dh_2048_224", "dh_2048_256"
  • RFC3526 "modp_1536", "modp_2048",
    "modp_3072", "modp_4096", "modp_6144", "modp_8192"
  1. local pem, err = pkey.paramgen({
  2. type = 'EC',
  3. curve = 'prime192v1',
  4. })
  5. local pem, err = pkey.paramgen({
  6. type = 'DH',
  7. group = 'ffdhe4096',
  8. })

It’s also possible to pass raw pkeyopt control strings in config table as used in the genpkey CLI program.
See openssl-genpkey(1) for a list of options.

Back to TOC

pkey:get_provider_name

syntax: name = pkey:get_provider_name()

Returns the provider name of pkey.

This function is available since OpenSSL 3.0.

Back to TOC

pkey:gettable_params, pkey:settable_params, pkey:get_param, pkey:set_params

Query settable or gettable params and set or get params.
See Generic EVP parameter getter/setter.

Back to TOC

pkey:get_parameters

syntax: parameters, err = pk:get_parameters()

Returns a table containing the parameters of pkey instance.

Back to TOC

pkey:set_parameters

syntax: ok, err = pk:set_parameters(params)

Set the parameters of the pkey from a table params.
If the parameter is not set in the params table,
it remains untouched in the pkey instance.

  1. local pk, err = require("resty.openssl.pkey").new()
  2. local parameters, err = pk:get_parameters()
  3. local e = parameters.e
  4. ngx.say(e:to_number())
  5. -- outputs 65537
  6. local ok, err = pk:set_parameters({
  7. e = require("resty.openssl.bn").from_hex("100001")
  8. })
  9. local ok, err = pk:set_parameters(parameters)

Parameters for RSA key:

Parameter Description Type
n modulus common to both public and private key bn
e public exponent bn
d private exponent bn
p first factor of n bn
q second factor of n bn
dmp1 d mod (p - 1), exponent1 bn
dmq1 d mod (q - 1), exponent2 bn
iqmp (InverseQ)(q) = 1 mod p, coefficient bn

Parameters for EC key:

Parameter Description Type
private private key bn
public public key bn
x x coordinate of the public key bn
y y coordinate of the public key bn
group the named curve group NID as a number, when passed in as set_parameters(), it’s also possible to use the text representation. This is different from luaossl where a EC_GROUP instance is returned.

It’s not possible to set x, y with public at same time as x and y is basically another representation
of public. Also currently it’s only possible to set x and y at same time.

Parameters for DH key:

Parameter Description Type
private private key bn
public public key bn
p prime modulus bn
q reference position bn
p base generator bn

Parameters for Curve25519 and Curve448 keys:

Parameter Description Type
private raw private key represented as bytes string
public raw public key represented as bytes string

Back to TOC

pkey:is_private

syntax: ok = pk:is_private()

Checks whether pk is a private key. Returns true if it’s a private key, returns false if
it’s a public key.

Back to TOC

pkey:get_key_type

syntax: obj, err = pk:get_key_type()

Returns a ASN1_OBJECT of key type of the private key as a table.

  1. local pkey, err = require("resty.openssl.pkey").new({type="X448"})
  2. ngx.say(require("cjson").encode(pkey:get_key_type()))
  3. -- outputs '{"ln":"X448","nid":1035,"sn":"X448","id":"1.3.101.111"}'

Back to TOC

pkey:get_default_digest_type

syntax: obj, err = pk:get_default_digest_type()

Returns a ASN1_OBJECT of key type of the private key as a table. An additional field mandatory is also
returned in the table, if mandatory is true then other digests can not be used.

  1. local pkey, err = require("resty.openssl.pkey").new()
  2. ngx.say(require("cjson").encode(pkey:get_default_digest_type()))
  3. -- outputs '{"ln":"sha256","nid":672,"id":"2.16.840.1.101.3.4.2.1","mandatory":false,"sn":"SHA256"}'

Back to TOC

pkey:sign

syntax: signature, err = pk:sign(digest)

syntax: signature, err = pk:sign(message, md_alg?, padding?, opts?)

Perform a digest signing using the private key defined in pkey
instance. The first parameter must be a resty.openssl.digest
instance or a string. Returns the signed text and error if any.

When passing a digest instance as first parameter, it should not
have been called final(), user should only use update().
This mode only supports RSA and EC keys.

When passing a string as first parameter, md_alg parameter will specify the name
to use when signing. When md_alg is undefined, for RSA and EC keys, this function does SHA256
by default. For Ed25519 or Ed448 keys, this function does a PureEdDSA signing,
no message digest should be specified and will not be used.

For RSA key, it’s also possible to specify padding scheme with following choices:

  1. pkey.PADDINGS = {
  2. RSA_PKCS1_PADDING = 1,
  3. RSA_SSLV23_PADDING = 2,
  4. RSA_NO_PADDING = 3,
  5. RSA_PKCS1_OAEP_PADDING = 4,
  6. RSA_X931_PADDING = 5, -- sign only
  7. RSA_PKCS1_PSS_PADDING = 6, -- sign and verify only
  8. }

When padding is RSA_PKCS1_PSS_PADDING, it’s
possible to specify PSS salt length by setting opts.pss_saltlen.

For EC key, this function does a ECDSA signing.
Note that OpenSSL does not support EC digital signature (ECDSA) with the
obsolete MD5 hash algorithm and will return error on this combination. See
EVP_DigestSign(3)
for a list of algorithms and associated public key algorithms. Normally, the ECDSA signature
is encoded in ASN.1 DER format. If the opts table contains a ecdsa_use_raw field with
a true value, a binary with just the concatenation of binary representation pr and ps is returned.
This is useful for example to send the signature as JWS.

opts is a table that accepts additional parameters with following choices:

  1. {
  2. pss_saltlen, -- For PSS mode only this option specifies the salt length.
  3. mgf1_md, -- For PSS and OAEP padding sets the MGF1 digest. If the MGF1 digest is not explicitly set in PSS mode then the signing digest is used.
  4. oaep_md, -- The digest used for the OAEP hash function. If not explicitly set then SHA1 is used.
  5. }

It’s also possible to pass raw pkeyopt control strings as used in the pkeyutl CLI program. This lets user pass in options that
are not explictly supported as parameters above.
See openssl-pkeyutl(1) for a list of options.

  1. pk:sign(message, nil, pk.PADDINGS.RSA_PKCS1_OAEP_PADDING, {
  2. oaep_md = "sha256",
  3. })
  4. -- is same as
  5. pk:sign(message, nil, nil, {
  6. "rsa_padding_mode:oaep",
  7. "rsa_oaep_md:sha256",
  8. })
  9. -- in pkeyutl CLI the above is equivalent to: `openssl pkeyutl -sign -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha256

To sign a message without doing message digest, please check pkey:sign_raw.

Back to TOC

pkey:verify

syntax: ok, err = pk:verify(signature, digest)

syntax: ok, err = pk:verify(signature, message, md_alg?, padding?, opts?)

Verify a signture (which can be the string returned by pkey:sign). The second
argument must be a resty.openssl.digest instance that uses
the same digest algorithm as used in sign or a string. ok returns true if verficiation is
successful and false otherwise. Note when verfication failed err will not be set when used
with OpenSSL 1.1.1 or lower.

When passing digest instances as second parameter, it should not
have been called final(), user should only use update().
This mode only supports RSA and EC keys.

When passing a string as second parameter, md_alg parameter will specify the name
to use when verifying. When md_alg is undefined, for RSA and EC keys, this function does SHA256
by default. For Ed25519 or Ed448 keys, this function does a PureEdDSA verification,
no message digest should be specified and will not be used.

When key is a RSA key, the function accepts an optional argument padding which choices
of values are same as those in pkey:sign. When padding is RSA_PKCS1_PSS_PADDING, it’s
possible to specify PSS salt length by setting opts.pss_saltlen.

For EC key, this function does a ECDSA verification. Normally, the ECDSA signature
should be encoded in ASN.1 DER format. If the opts table contains a ecdsa_use_raw field with
a true value, this library treat signature as concatenation of binary representation pr and ps.
This is useful for example to verify the signature as JWS.

opts is a table that accepts additional parameters which choices
of values are same as those in pkey:sign.

  1. -- RSA and EC keys
  2. local pk, err = require("resty.openssl.pkey").new()
  3. local digest, err = require("resty.openssl.digest").new("SHA256")
  4. digest:update("dog")
  5. -- WRONG:
  6. -- digest:final("dog")
  7. local signature, err = pk:sign(digest)
  8. -- uses SHA256 by default
  9. local signature, err = pk:sign("dog")
  10. ngx.say(ngx.encode_base64(signature))
  11. -- uses SHA256 and PSS padding
  12. local signature_pss, err = pk:sign("dog", "sha256", pk.PADDINGS.RSA_PKCS1_PSS_PADDING)
  13. digest, err = require("resty.openssl.digest").new("SHA256")
  14. digest:update("dog")
  15. local ok, err = pk:verify(signature, digest)
  16. -- uses SHA256 by default
  17. local ok, err = pk:verify(signature, "dog")
  18. -- uses SHA256 and PSS padding
  19. local ok, err = pk:verify(signature_pss, "dog", "sha256", pk.PADDINGS.RSA_PKCS1_PSS_PADDING)
  20. -- Ed25519 and Ed448 keys
  21. local pk, err = require("resty.openssl.pkey").new({
  22. type = "Ed25519",
  23. })
  24. local signature, err = pk:sign("23333")
  25. ngx.say(ngx.encode_base64(signature))

To verify a message without doing message digest, please check pkey:verify_raw and pkey:verify_recover.

Back to TOC

pkey:encrypt

syntax: cipher_txt, err = pk:encrypt(txt, padding?, opts?)

Encrypts plain text txt with pkey instance, which must loaded a public key.

The optional second argument padding has same meaning as in pkey:sign.
If omitted, padding is default to pkey.PADDINGS.RSA_PKCS1_PADDING.

The third optional argument opts has same meaning as in pkey:sign.

Back to TOC

pkey:decrypt

syntax: txt, err = pk:decrypt(cipher_txt, padding?, opts?)

Decrypts cipher text cipher_txt with pkey instance, which must loaded a private key.

The optional second argument padding has same meaning as in pkey:sign.
If omitted, padding is default to pkey.PADDINGS.RSA_PKCS1_PADDING.

The third optional argument opts has same meaning as in pkey:sign.

  1. local pkey = require("resty.openssl.pkey")
  2. local privkey, err = pkey.new()
  3. local pub_pem = privkey:to_PEM("public")
  4. local pubkey, err = pkey.new(pub_pem)
  5. local s, err = pubkey:encrypt("🦢", pkey.PADDINGS.RSA_PKCS1_PADDING)
  6. ngx.say(#s)
  7. -- outputs 256
  8. local decrypted, err = privkey:decrypt(s)
  9. ngx.say(decrypted)
  10. -- outputs "🦢"

Back to TOC

pkey:sign_raw

syntax: signature, err = pk:sign_raw(txt, padding?, opts?)

Signs the cipher text cipher_txt with pkey instance, which must loaded a private key.

The optional second argument padding has same meaning as in pkey:sign.
If omitted, padding is default to pkey.PADDINGS.RSA_PKCS1_PADDING.

The third optional argument opts has same meaning as in pkey:sign.

This function may also be called “private encrypt” in some implementations like NodeJS or PHP.
Do note as the function names suggested, this function is not secure to be regarded as an encryption.
When developing new applications, user should use pkey:sign for signing with digest, or
pkey:encrypt for encryption.

See examples/raw-sign-and-recover.lua
for an example.

Back to TOC

pkey:verify_raw

syntax: ok, err = pk:verify_raw(signature, data, md_alg, padding?, opts?)

Verify the cipher text signature with the message data with pkey instance, which must loaded a public key. Set the message digest to md_alg but doesn’t do message digest
automatically, in other words, this function assumes data has already been hashed with md_alg.

When md_alg is undefined, for RSA and EC keys, this function does SHA256 by default. For Ed25519 or Ed448 keys, no default value is set.

The optinal fourth argument padding has same meaning as in pkey:sign.
If omitted, padding is default to pkey.PADDINGS.RSA_PKCS1_PADDING.

The fifth optional argument opts has same meaning as in pkey:sign.

See examples/raw-sign-and-recover.lua
for an example.

Back to TOC

pkey:verify_recover

syntax: txt, err = pk:verify_recover(signature, padding?, opts?)

Verify the cipher text signature with pkey instance, which must loaded a public key, and also
returns the original text being signed. This operation is only supported by RSA key.

The optional second argument padding has same meaning as in pkey:sign.
If omitted, padding is default to pkey.PADDINGS.RSA_PKCS1_PADDING.

The third optional argument opts has same meaning as in pkey:sign.

This function may also be called “public decrypt” in some implementations like NodeJS or PHP.

See examples/raw-sign-and-recover.lua
for an example.

Back to TOC

pkey:derive

syntax: txt, err = pk:derive(peer_key)

Derive public key algorithm shared secret peer_key, which must be a pkey
instance.

See examples/x25519-dh.lua
for an example on how key exchange works for X25519 keys with DH algorithm.

Back to TOC

pkey:tostring

syntax: txt, err = pk:tostring(private_or_public?, fmt?, is_pkcs1?)

Outputs private key or public key of pkey instance in PEM-formatted text.
The first argument must be a choice of public, PublicKey, private, PrivateKey or nil.

The second argument fmt can be PEM, DER, JWK or nil.
If both arguments are omitted, this functions returns the PEM representation of public key.

If is_pkcs1 is set to true, the output is encoded using a PKCS#1 RSAPublicKey structure;
PKCS#1 encoding is currently supported for RSA key in PEM format. Writing out a PKCS#1
encoded RSA key is currently not supported when using with OpenSSL 3.0.

Back to TOC

pkey:to_PEM

syntax: pem, err = pk:to_PEM(private_or_public?, is_pkcs1?)

Equivalent to pkey:tostring(private_or_public, "PEM", is_pkcs1).

Back to TOC

resty.openssl.bn

Module to expose BIGNUM structure. Note bignum is a big integer, no float operations
(like square root) are supported.

Back to TOC

bn.new

syntax: b, err = bn.new(number?)

syntax: b, err = bn.new(string?, base?)

Creates a bn instance. The first argument can be:

  • nil to creates an empty bn instance.
  • A Lua number to initialize the bn instance.
  • A string to initialize the bn instance. The second argument base specifies the base of the string,
    and can take value from (compatible with Ruby OpenSSL.BN API):
    • 10 or omitted, for decimal string ("23333")
    • 16, for hex encoded string ("5b25")
    • 2, for binary string ("\x5b\x25")
    • 0, for MPI formated string ("\x00\x00\x00\x02\x5b\x25")

MPI is a format that consists of the number’s length in bytes represented as a 4-byte big-endian number, and the number itself in big-endian format, where the most significant bit signals a negative number (the representation of numbers with the MSB set is prefixed with null byte).

Back to TOC

bn.dup

syntax: b, err = bn.dup(bn_ptr_cdata)

Duplicates a BIGNUM* to create a new bn instance.

Back to TOC

bn.istype

syntax: ok = bn.istype(table)

Returns true if table is an instance of bn. Returns false otherwise.

Back to TOC

bn.set

syntax: b, err = bn:set(number)

syntax: b, err = bn:set(string, base?)

Reuse the existing bn instance and reset its value with given number or string.
Refer to bn.new for the type of arguments supported.

Back to TOC

bn.from_binary, bn:to_binary

syntax: bn, err = bn.from_binary(bin)

syntax: bin, err = bn:to_binary(padto?)

Creates a bn instance from binary string.

Exports the BIGNUM value in binary string.

bn:to_binary accepts an optional number argument padto that can be
used to pad leading zeros to the output to a specific length.

  1. local to_hex = require "resty.string".to_hex
  2. local b, err = require("resty.openssl.bn").from_binary("\x5b\x25")
  3. local bin, err = b:to_binary()
  4. ngx.say(to_hex(bin))
  5. -- outputs "5b25

Back to TOC

bn.from_mpi, bn:to_mpi

syntax: bn, err = bn.from_mpi(bin)

syntax: bin, err = bn:to_mpi()

Creates a bn instance from MPI formatted binary string.

Exports the BIGNUM value in MPI formatted binary string.

  1. local to_hex = require "resty.string".to_hex
  2. local b, err = require("resty.openssl.bn").from_mpi("\x00\x00\x00\x02\x5b\x25")
  3. local bin, err = b:to_mpi()
  4. ngx.say(to_hex(bin))
  5. -- outputs "000000025b25

Back to TOC

bn.from_hex, bn:to_hex

syntax: bn, err = bn.from_hex(hex)

syntax: hex, err = bn:to_hex()

Creates a bn instance from hex encoded string. Note that the leading 0x should not be
included. A leading - indicating the sign may be included.

Exports the bn instance to hex encoded string.

  1. local bn = require("resty.openssl.bn")
  2. local b = bn.from_hex("5B25")
  3. local hex, err = b:to_hex()
  4. ngx.say(hex)
  5. -- outputs "5B25"

Back to TOC

bn.from_dec, bn:to_dec

syntax: bn, err = bn.from_dec(dec)

syntax: dec, err = bn:to_dec()

Creates a bn instance from decimal string. A leading - indicating the sign may be included.

Exports the bn instance to decimal string.

  1. local bn = require("resty.openssl.bn")
  2. local b = bn.from_dec("23333")
  3. local dec, err = b:to_dec()
  4. ngx.say(dec)
  5. -- outputs "23333"

Back to TOC

bn:to_number

syntax: n, err = bn:to_number()

syntax: n, err = bn:tonumber()

Export the lowest 32 bits or 64 bits part (based on the ABI) of bn instance
to a number. This is useful when user wants to perform bitwise operations.

  1. local bn = require("resty.openssl.bn")
  2. local b = bn.from_dec("23333")
  3. local n, err = b:to_number()
  4. ngx.say(n)
  5. -- outputs 23333
  6. ngx.say(type(n))
  7. -- outputs "number"

Back to TOC

bn.generate_prime

syntax: bn, err = bn.generate_prime(bits, safe)

Generates a pseudo-random prime number of bit length bits.

If safe is true, it will be a safe prime (i.e. a prime p so that (p-1)/2 is also prime).

The PRNG must be seeded prior to calling BN_generate_prime_ex().
The prime number generation has a negligible error probability.

Back to TOC

bn:__metamethods

Various mathematical operations can be performed as if it’s a number.

  1. local bn = require("resty.openssl.bn")
  2. local a = bn.new(123456)
  3. local b = bn.new(222)
  4. -- the following returns a bn
  5. local r
  6. r = -a
  7. r = a + b
  8. r = a - b
  9. r = a * b
  10. r = a / b -- equal to bn:idiv, returns floor division
  11. r = a % b
  12. -- all operations can be performed between number and bignum
  13. r = a + 222
  14. r = 222 + a
  15. -- the following returns a bool
  16. local bool
  17. bool = a < b
  18. bool = a >= b
  19. -- compare between number will not work
  20. -- WRONG: bool = a < 222

Back to TOC

bn:add, bn:sub, bn:mul, bn:div, bn:exp, bn:mod, bn:gcd

syntax: r = a:op(b)

syntax: r = bn.op(a, b)

Perform mathematical operations op.

  • add: add
  • sub: subtract
  • mul: multiply
  • div, idiv: floor division (division with rounding down to nearest integer)
  • exp, pow: the b-th power of a, this function is faster than repeated a * a * ....
  • mod: modulo
  • gcd: the greatest common divider of a and b.

Note that add, sub, mul, div, mod is also available with +, -, *, /, % operaters.
See above section for examples.

  1. local bn = require("resty.openssl.bn")
  2. local a = bn.new(123456)
  3. local b = bn.new(9876)
  4. local r
  5. -- the followings are equal
  6. r = a:add(b)
  7. r = bn.add(a, b)
  8. r = a:add(9876)
  9. r = bn.add(a, 9876)
  10. r = bn.add(123456, b)
  11. r = bn.add(123456, 9876)

Back to TOC

bn:sqr

syntax: r = a:sqr()

syntax: r = bn.sqr(a)

Computes the 2-th power of a. This function is faster than r = a * a.

Back to TOC

bn:mod_add, bn:mod_sub, bn:mod_mul, bn:mod_exp

syntax: r = a:op(b, m)

syntax: r = bn.op(a, b, m)

Perform modulo mathematical operations op.

  • mod_add: adds a to b modulo m
  • mod_sub: substracts b from a modulo m
  • mod_mul: multiplies a by b and finds the non-negative remainder respective to modulus m
  • mod_exp, mod_pow: computes a to the b-th power modulo m (r=a^b % m). This function uses less
    time and space than exp. Do not call this function when m is even and any of the parameters
    have the BN_FLG_CONSTTIME flag set.
  1. local bn = require("resty.openssl.bn")
  2. local a = bn.new(123456)
  3. local b = bn.new(9876)
  4. local r
  5. -- the followings are equal
  6. r = a:mod_add(b, 3)
  7. r = bn.mod_add(a, b, 3)
  8. r = a:mod_add(9876, 3)
  9. r = bn.mod_add(a, 9876, 3)
  10. r = bn.mod_add(123456, b, 3)
  11. r = bn.mod_add(123456, 9876, 3)

Back to TOC

bn:mod_sqr

syntax: r = a:mod_sqr(m)

syntax: r = bn.mod_sqr(a, m)

Takes the square of a modulo m.

Back to TOC

bn:lshift, bn:rshift

syntax: r = bn:lshift(bit)

syntax: r = bn.lshift(a, bit)

syntax: r = bn:rshift(bit)

syntax: r = bn.rshift(a, bit)

Bit shift a to bit bits.

Back to TOC

bn:is_zero, bn:is_one, bn:is_odd, bn:is_word

syntax: ok = bn:is_zero()

syntax: ok = bn:is_one()

syntax: ok = bn:is_odd()

syntax: ok, err = bn:is_word(n)

Checks if bn is 0, 1, and odd number or a number n respectively.

Back to TOC

bn:is_prime

syntax: ok, err = bn:is_prime(nchecks?)

Checks if bn is a prime number. Returns true if it is prime with an
error probability of less than 0.25^nchecks and error if any. If omitted,
nchecks is set to 0 which means to select number of iterations basedon the
size of the number

This function perform a Miller-Rabin probabilistic primality test with nchecks iterations. If nchecks == BN_prime_checks (0), a number of iterations is used that yields a false positive rate of at most 2^-64 for random input. The error rate depends on the size of the prime and goes down for bigger primes. The rate is 2^-80 starting at 308 bits, 2^-112 at 852 bits, 2^-128 at 1080 bits, 2^-192 at 3747 bits and 2^-256 at 6394 bits.

When the source of the prime is not random or not trusted, the number of checks needs to be much higher to reach the same level of assurance: It should equal half of the targeted security level in bits (rounded up to the next integer if necessary). For instance, to reach the 128 bit security level, nchecks should be set to 64.

See also BN_is_prime(3).

Back to TOC

resty.openssl.cipher

Module to interact with symmetric cryptography (EVP_CIPHER).

Back to TOC

cipher.new

syntax: d, err = cipher.new(cipher_name, properties?)

Creates a cipher instance. cipher_name is a case-insensitive string of cipher algorithm name.
To view a list of cipher algorithms implemented, use
openssl.list_cipher_algorithms
or openssl list -cipher-algorithms

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

cipher.istype

syntax: ok = cipher.istype(table)

Returns true if table is an instance of cipher. Returns false otherwise.

Back to TOC

cipher.set_buffer_size

syntax: ok = cipher.set_buffer_size(sz)

Resize the internal buffer size used by all cipher instance. The default buffer size is 1024 bytes.

If you are expecting to pass input text larger than 1024 bytes at one time to update(), encrypt()
or decrypt(), setting the buffer to larger than the expected input size will improve performance
by let more code to be JIT-able.

Avoid call this function at hotpath, as this re-allocate the buffer every time it’s called.

Back to TOC

cipher:get_provider_name

syntax: name = cipher:get_provider_name()

Returns the provider name of cipher.

This function is available since OpenSSL 3.0.

Back to TOC

cipher:gettable_params, cipher:settable_params, cipher:get_param, cipher:set_params

Query settable or gettable params and set or get params.
See Generic EVP parameter getter/setter.

Back to TOC

cipher:encrypt

syntax: s, err = cipher:encrypt(key, iv?, s, no_padding?, aead_aad?)

Encrypt the text s with key key and IV iv. Returns the encrypted text in raw binary string
and error if any.
Optionally accepts a boolean no_padding which tells the cipher to enable or disable padding and default
to false (enable padding). If no_padding is true, the length of s must then be a multiple of the
block size or an error will occur.

When using GCM or CCM mode or chacha20-poly1305 cipher, it’s also possible to pass
the Additional Authenticated Data (AAD) as the fifth argument.

This function is a shorthand of cipher:init, cipher:set_aead_aad (if appliable) then cipher:final.

Back to TOC

cipher:decrypt

syntax: s, err = cipher:decrypt(key, iv?, s, no_padding?, aead_aad?, aead_tag?)

Decrypt the text s with key key and IV iv. Returns the decrypted text in raw binary string
and error if any.
Optionally accepts a boolean no_padding which tells the cipher to enable or disable padding and default
to false (enable padding). If no_padding is true, the length of s must then be a multiple of the
block size or an error will occur; also, padding in the decrypted text will not be removed.

When using GCM or CCM mode or chacha20-poly1305 cipher, it’s also possible to pas
the Additional Authenticated Data (AAD) as the fifth argument and authentication tag
as the sixth argument.

This function is a shorthand of cipher:init, cipher:set_aead_aad (if appliable),
cipher:set_aead_tag (if appliable) then cipher:final.

Back to TOC

cipher:init

syntax: ok, err = cipher:init(key, iv?, opts?)

Initialize the cipher with key key and IV iv. The optional third argument is a table consists of:

  1. {
  2. is_encrypt = false,
  3. no_padding = false,
  4. }

Calling function is needed before cipher:update and
cipher:final if the cipher is not being initialized already. But not
cipher:encrypt and cipher:decrypt.

If you wish to reuse cipher instance multiple times, calling this function is necessary
to clear the internal state of the cipher. The shorthand functions
cipher:encrypt and cipher:decrypt
already take care of initialization and reset.

Back to TOC

cipher:update

syntax: s, err = cipher:update(partial, …)

Updates the cipher with one or more strings. If the cipher has larger than block size of data to flush,
the function will return a non-empty string as first argument. This function can be used in a streaming
fashion to encrypt or decrypt continous data stream.

Back to TOC

cipher:update_aead_aad

syntax: ok, err = cipher:update_aead_aad(aad)

Provides AAD data to the cipher, this function can be called more than one times.

Back to TOC

cipher:get_aead_tag

syntax: tag, err = cipher:get_aead_tag(size?)

Gets the authentication tag from cipher with length specified as size. If omitted, a tag with length
of half of the block size will be returned. The size cannot exceed block size.

This function can only be called after encryption is finished.

Back to TOC

cipher:set_aead_tag

syntax: ok, err = cipher:set_aead_tag(tag)

Set the authentication tag of cipher with tag.

This function can only be called before decryption starts.

Back to TOC

cipher:final

syntax: s, err = cipher:final(partial?)

Returns the encrypted or decrypted text in raw binary string, optionally accept one string to encrypt or decrypt.

  1. -- encryption
  2. local c, err = require("resty.openssl.cipher").new("aes256")
  3. c:init(string.rep("0", 32), string.rep("0", 16), {
  4. is_encrypt = true,
  5. })
  6. c:update("🦢")
  7. local cipher, err = c:final()
  8. ngx.say(ngx.encode_base64(cipher))
  9. -- outputs "vGJRHufPYrbbnYYC0+BnwQ=="
  10. -- OR:
  11. local c, err = require("resty.openssl.cipher").new("aes256")
  12. local cipher, err = c:encrypt(string.rep("0", 32), string.rep("0", 16), "🦢")
  13. ngx.say(ngx.encode_base64(cipher))
  14. -- outputs "vGJRHufPYrbbnYYC0+BnwQ=="
  15. -- decryption
  16. local encrypted = ngx.decode_base64("vGJRHufPYrbbnYYC0+BnwQ==")
  17. local c, err = require("resty.openssl.cipher").new("aes256")
  18. c:init(string.rep("0", 32), string.rep("0", 16), {
  19. is_encrypt = false,
  20. })
  21. c:update(encrypted)
  22. local cipher, err = c:final()
  23. ngx.say(cipher)
  24. -- outputs "🦢"
  25. -- OR:
  26. local c, err = require("resty.openssl.cipher").new("aes256")
  27. local cipher, err = c:decrypt(string.rep("0", 32), string.rep("0", 16), encrypted)
  28. ngx.say(cipher)
  29. -- outputs "🦢"

Note: in some implementations like libsodium or Java, AEAD ciphers append the tag (or MAC)
at the end of encrypted ciphertext. In such case, user will need to manually cut off the tag
with correct size(usually 16 bytes) and pass in the ciphertext and tag seperately.

See examples/aes-gcm-aead.lua
for an example to use AEAD modes with authentication.

Back to TOC

cipher:derive

syntax: key, iv, err = cipher:derive(key, salt?, count?, md?)

Derive a key and IV (if appliable) from given material that can be used in current cipher. This function
is useful mainly to work with keys that were already derived from same algorithm. Newer applications should
use a more modern algorithm such as PBKDF2 provided by kdf.derive.

count is the iteration count to perform. If it’s omitted, it’s set to 1. Note the recent version of
openssl enc cli tool automatically use PBKDF2 if -iter is set to larger than 1,
while this function will not. To use PBKDF2 to derive a key, please refer to kdf.derive.

md is the message digest name to use, it can take one of the values md2, md5, sha or sha1.
If it’s omitted, it’s default to sha1.

  1. local cipher = require("resty.openssl.cipher").new("aes-128-cfb")
  2. local key, iv, err = cipher:derive("x")
  3. -- equivalent to `openssl enc -aes-128-cfb -pass pass:x -nosalt -P -md sha1`

Back to TOC

resty.openssl.digest

Module to interact with message digest (EVP_MD_CTX).

Back to TOC

digest.new

syntax: d, err = digest.new(digest_name?, properties?)

Creates a digest instance. digest_name is a case-insensitive string of digest algorithm name.
To view a list of digest algorithms implemented, use
openssl.list_digest_algorithms or
openssl list -digest-algorithms.

If digest_name is omitted, it’s default to sha1. Specially, the digest_name "null"
represents a “null” message digest that does nothing: i.e. the hash it returns is of zero length.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

digest.istype

syntax: ok = digest.istype(table)

Returns true if table is an instance of digest. Returns false otherwise.

Back to TOC

digest:get_provider_name

syntax: name = digest:get_provider_name()

Returns the provider name of digest.

This function is available since OpenSSL 3.0.

Back to TOC

digest:gettable_params, digest:settable_params, digest:get_param, digest:set_params

Query settable or gettable params and set or get params.
See Generic EVP parameter getter/setter.

Back to TOC

digest:update

syntax: ok, err = digest:update(partial, …)

Updates the digest with one or more strings.

Back to TOC

digest:final

syntax: str, err = digest:final(partial?)

Returns the digest in raw binary string, optionally accept one string to digest.

  1. local d, err = require("resty.openssl.digest").new("sha256")
  2. d:update("🦢")
  3. local digest, err = d:final()
  4. ngx.say(ngx.encode_base64(digest))
  5. -- outputs "tWW/2P/uOa/yIV1gRJySJLsHq1xwg0E1RWCvEUDlla0="
  6. -- OR:
  7. local d, err = require("resty.openssl.digest").new("sha256")
  8. local digest, err = d:final("🦢")
  9. ngx.say(ngx.encode_base64(digest))
  10. -- outputs "tWW/2P/uOa/yIV1gRJySJLsHq1xwg0E1RWCvEUDlla0="

Back to TOC

digest:reset

syntax: ok, err = digest:reset()

Reset the internal state of digest instance as it’s just created by digest.new.
It calls EVP_DigestInit_ex under
the hood.

User must call this before reusing the same digest instance.

Back to TOC

resty.openssl.hmac

Module to interact with hash-based message authentication code (HMAC_CTX).

Use of this module is deprecated since OpenSSL 3.0, please use resty.openssl.mac
instead.

Back to TOC

hmac.new

syntax: h, err = hmac.new(key, digest_name?)

Creates a hmac instance. digest_name is a case-insensitive string of digest algorithm name.
To view a list of digest algorithms implemented, use
openssl.list_digest_algorithms or
openssl list -digest-algorithms.

If digest_name is omitted, it’s default to sha1.

Back to TOC

hmac.istype

syntax: ok = hmac.istype(table)

Returns true if table is an instance of hmac. Returns false otherwise.

Back to TOC

hmac:update

syntax: ok, err = hmac:update(partial, …)

Updates the HMAC with one or more strings.

Back to TOC

hmac:final

syntax: str, err = hmac:final(partial?)

Returns the HMAC in raw binary string, optionally accept one string to digest.

  1. local d, err = require("resty.openssl.hmac").new("goose", "sha256")
  2. d:update("🦢")
  3. local hmac, err = d:final()
  4. ngx.say(ngx.encode_base64(hmac))
  5. -- outputs "k2UcrRp25tj1Spff89mJF3fAVQ0lodq/tJT53EYXp0c="
  6. -- OR:
  7. local d, err = require("resty.openssl.hmac").new("goose", "sha256")
  8. local hmac, err = d:final("🦢")
  9. ngx.say(ngx.encode_base64(hmac))
  10. -- outputs "k2UcrRp25tj1Spff89mJF3fAVQ0lodq/tJT53EYXp0c="

Back to TOC

hmac:reset

syntax: ok, err = hmac:reset()

Reset the internal state of hmac instance as it’s just created by hmac.new.
It calls HMAC_Init_ex under
the hood.

User must call this before reusing the same hmac instance.

Back to TOC

resty.openssl.mac

Module to interact with message authentication code (EVP_MAC).

Back to TOC

mac.new

syntax: h, err = mac.new(key, mac, cipher?, digest?, properties?)

Creates a mac instance. mac is a case-insensitive string of MAC algorithm name.
To view a list of digest algorithms implemented, use
openssl.list_mac_algorithms or
openssl list -mac-algorithms.

At least one of cipher or digest must be specified.

cipher is a case-insensitive string of digest algorithm name.
To view a list of digest algorithms implemented, use
openssl.list_cipher_algorithms or
openssl list -cipher-algorithms.
digest is a case-insensitive string of digest algorithm name.
To view a list of digest algorithms implemented, use
openssl.list_digest_algorithms or
openssl list -digest-algorithms.
properties parameter can be used to explictly select provider to fetch algorithms.

Back to TOC

mac.istype

syntax: ok = mac.istype(table)

Returns true if table is an instance of mac. Returns false otherwise.

Back to TOC

mac:get_provider_name

syntax: name = mac:get_provider_name()

Returns the provider name of mac.

This function is available since OpenSSL 3.0.

Back to TOC

mac:gettable_params, mac:settable_params, mac:get_param, mac:set_params

Query settable or gettable params and set or get params.
See Generic EVP parameter getter/setter.

Back to TOC

mac:update

syntax: ok, err = mac:update(partial, …)

Updates the MAC with one or more strings.

Back to TOC

mac:final

syntax: str, err = mac:final(partial?)

Returns the MAC in raw binary string, optionally accept one string to digest.

  1. local d, err = require("resty.openssl.mac").new("goose", "HMAC", nil, "sha256")
  2. d:update("🦢")
  3. local mac, err = d:final()
  4. ngx.say(ngx.encode_base64(mac))
  5. -- outputs "k2UcrRp25tj1Spff89mJF3fAVQ0lodq/tJT53EYXp0c="
  6. -- OR:
  7. local d, err = require("resty.openssl.mac").new("goose", "HMAC", nil, "sha256")
  8. local hmac, err = d:final("🦢")
  9. ngx.say(ngx.encode_base64(mac))
  10. -- outputs "k2UcrRp25tj1Spff89mJF3fAVQ0lodq/tJT53EYXp0c="

Back to TOC

mac:reset

syntax: ok, err = mac:reset()

Reset the internal state of mac instance as it’s just created by mac.new.
It calls EVP_MAC_Init under
the hood.

User must call this before reusing the same mac instance.

resty.openssl.kdf

Module to interact with KDF (key derivation function).

Back to TOC

kdf.derive (legacy)

syntax: key, err = kdf.derive(options)

Use of this module is deprecated since OpenSSL 3.0, please use kdf.new
instead.

Derive a key from given material. Various KDFs are supported based on OpenSSL version:

PBKDF2(RFC 2898, NIST SP 800-132), HKDF(RFC 5869), TLS1-PRF(RFC 2246, RFC 5246 and NIST SP 800-135 r1) and scrypt(RFC 7914) is available.

options is a table that contains:

Key Type Description Required or default
type number Type of KDF function to use, one of kdf.PBKDF2, kdf.SCRYPT, kdf.TLS1_PRF or kdf.HKDF required
outlen number Desired key length to derive required
pass string Initial key material to derive from (empty string)
salt string Add some salt (empty string)
md string Message digest method name to use, not effective for scrypt type "sha1"
properties string Staring from OpenSSL 3.0, this function accepts an optional properties parameter

to explictly select provider to fetch algorithms. | |
| pbkdf2_iter | number | PBKDF2 iteration count. RFC 2898 suggests an iteration count of at least 1000. Any value less than 1 is treated as a single iteration. | 1 |
| hkdf_key | string | HKDF key | required |
| hkdf_mode | number | HKDF mode to use, one of kdf.HKDEF_MODE_EXTRACT_AND_EXPAND, kdf.HKDEF_MODE_EXTRACT_ONLY or kdf.HKDEF_MODE_EXPAND_ONLY. To learn about mode, please refer to EVP_PKEY_CTX_set1_hkdf_key(3). Note with kdf.HKDEF_MODE_EXTRACT_ONLY, outlen is ignored and the output will be fixed size of HMAC-<md>. | kdf.HKDEF_MODE_EXTRACT_AND_EXPAND|
| hkdf_info | string | HKDF info value | (empty string) |
| tls1_prf_secret | string | TLS1-PRF secret | required |
| tls1_prf_seed | string | TLS1-PRF seed | required |
| scrypt_maxmem | number | Scrypt maximum memory usage in bytes |32 * 1024 * 1024 |
| scrypt_N | number | Scrypt CPU/memory cost parameter, must be a power of 2 | required |
| scrypt_r | number | Scrypt blocksize parameter (8 is commonly used) | required |
| scrypt_p | number | Scrypt parallelization parameter | required |

  1. local kdf = require("resty.openssl.kdf")
  2. local key, err = kdf.derive({
  3. type = kdf.PBKDF2,
  4. outlen = 16,
  5. pass = "1234567",
  6. md = "md5",
  7. pbkdf2_iter = 1000,
  8. })
  9. ngx.say(ngx.encode_base64(key))
  10. -- outputs "cDRFLQ7NWt+AP4i0TdBzog=="
  11. key, err = kdf.derive({
  12. type = kdf.SCRYPT,
  13. outlen = 16,
  14. pass = "1234567",
  15. scrypt_N = 1024,
  16. scrypt_r = 8,
  17. scrypt_p = 16,
  18. })
  19. ngx.say(ngx.encode_base64(key))
  20. -- outputs "9giFtxace5sESmRb8qxuOw=="

Back to TOC

kdf.new

syntax: k, err = kdf.new(kdf_name?, properties?)

Creates a kdf instance. kdf_name is a case-insensitive string of kdf algorithm name.
To view a list of kdf algorithms implemented, use
openssl.list_kdf_algorithms or
openssl list -kdf-algorithms.

This function is available since OpenSSL 3.0.

This function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

kdf.istype

syntax: ok = kdf.istype(table)

Returns true if table is an instance of kdf. Returns false otherwise.

This function is available since OpenSSL 3.0.

Back to TOC

kdf:get_provider_name

syntax: name = kdf:get_provider_name()

Returns the provider name of kdf.

This function is available since OpenSSL 3.0.

Back to TOC

kdf:gettable_params, kdf:settable_params, kdf:get_param, kdf:set_params

Query settable or gettable params and set or get params.
See Generic EVP parameter getter/setter.

This function is available since OpenSSL 3.0.

Back to TOC

kdf:derive

syntax: ok, err = kdf:derive(outlen, options?, options_count?)

Derive a key with length of outlen with options. Certain algorithms output fixed
length of key where outlen should be unset.

options is a table map holding parameters passing to kdf. To view the list of parameters
acceptable by selecter algorithm and provider, use kdf:settable_params.

Optionally, if length of options is known, user can provide its length through options_count
to gain better performance where options table is relatively large.

  1. local k = assert(kdf.new("PBKDF2"))
  2. key = assert(k:derive(16, {
  3. pass = "1234567",
  4. iter = 1000,
  5. digest = "md5",
  6. salt = "",
  7. }))
  8. ngx.say(ngx.encode_base64(key))
  9. -- outputs "cDRFLQ7NWt+AP4i0TdBzog=="
  10. assert(k:reset())
  11. -- kdf instance is reusable, user can set common parameters
  12. -- through set_params and don't need to repeat in derive()
  13. assert(k:set_params({
  14. iter = 1000,
  15. digest = "md5",
  16. salt = "",
  17. }))
  18. key = assert(k:derive(16, {
  19. pass = "1234567",
  20. }))
  21. ngx.say(ngx.encode_base64(key))
  22. -- outputs "cDRFLQ7NWt+AP4i0TdBzog=="
  23. local k = assert(kdf.new("HKDF"))
  24. key = assert(k:derive(16, {
  25. digest = "md5",
  26. key = "secret",
  27. salt = "salt",
  28. info = "some info",
  29. mode = kdf.HKDEF_MODE_EXPAND_ONLY,
  30. -- as HKDF also accepts mode as string, use the literal below also works
  31. -- mode = "EXPAND_ONLY"
  32. }))

This function is available since OpenSSL 3.0.

Back to TOC

kdf:reset

syntax: ok, err = kdf:reset()

Reset the internal state of kdf instance as it’s just created by kdf.new.

User must call this before reusing the same kdf instance.

Back to TOC

resty.openssl.objects

Helpfer module on ASN1_OBJECT.

Back to TOC

objects.obj2table

syntax: tbl = objects.bytes(asn1_obj)

Convert a ASN1_OBJECT pointer to a Lua table where

  1. {
  2. id: OID of the object,
  3. nid: NID of the object,
  4. sn: short name of the object,
  5. ln: long name of the object,
  6. }

Back to TOC

objects.nid2table

syntax: tbl, err = objects.nid2table(nid)

Convert a NID to a Lua table, returns the same format as
objects.obj2table

Back to TOC

objects.txt2nid

syntax: nid, err = objects.txt2nid(txt)

Convert a text representation to NID.

Back to TOC

resty.openssl.pkcs12

Module to interact with PKCS#12 format.

Back to TOC

pkcs12.encode

syntax: der, err = pkcs12.encode(data, passphrase?, properties?)

Encode data in data to a PKCS#12 text.

data is a table that contains:

Key Type Description Required or default
key pkey Private key required
cert x509 Certificate required
cacerts A list of x509 as Lua table Additional certificates []
friendly_name string The name used for the supplied certificate and key ""
nid_key number or string The NID or text to specify algorithm to encrypt key "PBE-SHA1-RC2-4" if compiled with RC2, otherwise "PBE-SHA1-3DES"; on OpenSSL 3.0 and later PBES2 with PBKDF2 and AES-256-CBC.
nid_cert number or string The NID or text to specify algorithm to encrypt cert "PBE-SHA1-3DES"; on OpenSSL 3.0 and later PBES2 with PBKDF2 and AES-256-CBC
iter number Key iterration count PKCS12_DEFAULT_ITER (2048)
mac_iter number MAC iterration count 1

passphrase is the string for encryption. If omitted, an empty string will be used.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Note in OpenSSL 3.0 RC2 has been moved to legacy provider. In order to encode p12 data with RC2
encryption, you need to load the legacy provider first.

  1. local pro = require "resty.openssl.provider"
  2. local legacy_provider = assert(pro.load("legacy"))
  3. local p12, err = pkcs12.encode({ key = key, cert = cert})
  4. assert(legacy_provider:unload())

Back to TOC

pkcs12.decode

syntax: data, err = pkcs12.decode(p12, passphrase?)

Decode a PKCS#12 text to Lua table data. Similar to the data table passed to pkcs12.encode,
but onle cert, key, cacerts and friendly_name are returned.

passphrase is the string for encryption. If omitted, an empty string will be used.

Note in OpenSSL 3.0 RC2 has been moved to legacy provider. In order to decode p12 data with RC2
encryption, you need to load the legacy provider first.

Back to TOC

resty.openssl.rand

Module to interact with random number generator.

Back to TOC

rand.bytes

syntax: str, err = rand.bytes(length, private?, strength?)

Generate random bytes with length of length. If private is set to true, a
private PRNG instance is used so that a compromise of the “public” PRNG instance
will not affect the secrecy of these private values.

The bytes generated will have a security strength of at least strength bits.

Back to TOC

resty.openssl.x509

Module to interact with X.509 certificates.

Back to TOC

x509.new

syntax: crt, err = x509.new(txt?, fmt?, properties?)

Creates a x509 instance. txt can be PEM or DER formatted text;
fmt is a choice of PEM, DER to load specific format, or * for auto detect.

When txt is omitted, new() creates an empty x509 instance.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

x509.dup

syntax: x509, err = x509.dup(x509_ptr_cdata)

Duplicates a X509* to create a new x509 instance.

Back to TOC

x509.istype

syntax: ok = x509.istype(table)

Returns true if table is an instance of x509. Returns false otherwise.

Back to TOC

x509:digest

syntax: d, err = x509:digest(digest_name?, properties?)

Returns a digest of the DER representation of the X509 certificate object in raw binary text.

digest_name is a case-insensitive string of digest algorithm name.
To view a list of digest algorithms implemented, use
openssl.list_digest_algorithms or
openssl list -digest-algorithms.

If digest_name is omitted, it’s default to sha1.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

x509:pubkey_digest

syntax: d, err = x509:pubkey_digest(digest_name?, properties?)

Returns a digest of the DER representation of the pubkey in the X509 object in raw binary text.

digest_name is a case-insensitive string of digest algorithm name.
To view a list of digest algorithms implemented, use
openssl.list_digest_algorithms or
openssl list -digest-algorithms.

If digest_name is omitted, it’s default to sha1.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

x509:check_private_key

syntax: match, err = x509:check_private_key(pkey)

Checks the consistency of private key pkey with the public key in current X509 object.

Returns a boolean indicating if it’s a match and err describing the reason.

Note this function also checks if k itself is indeed a private key or not.

Back to TOC

x509:get*, x509:set*

syntax: ok, err = x509:set_attribute(instance)

syntax: instance, err = x509:get_attribute()

Setters and getters for x509 attributes share the same syntax.

Attribute name Type Description
issuer_name x509.name Issuer of the certificate
not_before number Unix timestamp when certificate is not valid before
not_after number Unix timestamp when certificate is not valid after
pubkey pkey Public key of the certificate
serial_number bn Serial number of the certficate
subject_name x509.name Subject of the certificate
version number Version of the certificate, value is one less than version. For example, 2 represents version 3

Additionally, getters and setters for extensions are also available:

Extension name Type Description
subject_alt_name x509.altname Subject Alternative Name of the certificate, SANs are usually used to define “additional Common Names”
issuer_alt_name x509.altname Issuer Alternative Name of the certificate
basic_constraints table, { ca = bool, pathlen = int} Basic Constriants of the certificate
info_access x509.extension.info_access Authority Information Access of the certificate, contains information like OCSP reponder URL.
crl_distribution_points x509.extension.dist_points CRL Distribution Points of the certificate, contains information like Certificate Revocation List(CRL) URLs.

For all extensions, get_{extension}_critical and set_{extension}_critical is also supported to
access the critical flag of the extension.

If the attribute is not found, getter will return nil, nil.

  1. local x509, err = require("resty.openssl.x509").new()
  2. err = x509:set_not_before(ngx.time())
  3. local not_before, err = x509:get_not_before()
  4. ngx.say(not_before)
  5. -- outputs 1571875065
  6. err = x509:set_basic_constraints_critical(true)

If type is a table, setter requires a table with case-insensitive keys to set;
getter returns the value of the given case-insensitive key or a table of all keys if no key provided.

  1. local x509, err = require("resty.openssl.x509").new()
  2. err = x509:set_basic_constraints({
  3. cA = false,
  4. pathlen = 0,
  5. })
  6. ngx.say(x509:get_basic_constraints("pathlen"))
  7. -- outputs 0
  8. ngx.say(x509:get_basic_constraints())
  9. -- outputs '{"ca":false,"pathlen":0}'

Note that user may also access the certain extension by x509:get_extension and
x509:set_extension, while the later two function returns or requires
extension instead. User may use getter and setters listed here if modification
of current extensions is needed; use x509:get_extension or
x509:set_extension if user are adding or replacing the whole extension or
getters/setters are not implemented. If the getter returned a type of x509.* instance, it can be
converted to a extension instance by extension:from_data,
and thus used by x509:get_extension and x509:set_extension

Back to TOC

x509:get_lifetime

syntax: not_before, not_after, err = x509:get_lifetime()

A shortcut of x509:get_not_before plus x509:get_not_after

Back to TOC

x509:set_lifetime

syntax: ok, err = x509:set_lifetime(not_before, not_after)

A shortcut of x509:set_not_before
plus x509:set_not_after.

Back to TOC

x509:get_signature_name, x509:get_signature_nid, x509:get_signature_digest_name

syntax: sn, err = x509:get_signature_name()

syntax: nid, err = x509:get_signature_nid()

syntax: sn, err = x509:get_signature_digest_name()

Return the NID or the short name (SN) of the signature of the certificate.

x509:get_signature_digest_name returns the short name of the digest algorithm
used to sign the certificate.

Back to TOC

x509:get_extension

syntax: extension, pos, err = x509:get_extension(nid_or_txt, last_pos?)

Get X.509 extension matching the given NID to certificate, returns a
resty.openssl.x509.extension instance and the found position.

If last_pos is defined, the function searchs from that position; otherwise it
finds from beginning. Index is 1-based.

  1. local ext, pos, err = x509:get_extension("keyUsage")
  2. ngx.say(ext:text())
  3. -- outputs "Digital Signature, Key Encipherment"
  4. local ext, pos, err = x509:get_extension("subjectKeyIdentifier")
  5. ngx.say(ext:text())
  6. -- outputs "3D:42:13:57:8F:79:BE:30:7D:86:A9:AC:67:50:E5:56:3E:0E:AF:4F"

Back to TOC

x509:add_extension

syntax: ok, err = x509:add_extension(extension)

Adds an X.509 extension to certificate, the first argument must be a
resty.openssl.x509.extension instance.

  1. local extension, err = require("resty.openssl.x509.extension").new(
  2. "keyUsage", "critical,keyCertSign,cRLSign"
  3. )
  4. local x509, err = require("resty.openssl.x509").new()
  5. local ok, err = x509:add_extension(extension)

Back to TOC

x509:set_extension

syntax: ok, err = x509:set_extension(extension, last_pos?)

Adds an X.509 extension to certificate, the first argument must be a
resty.openssl.x509.extension instance.
The difference from x509:add_extension is that
in this function if a extension with same type already exists,
the old extension will be replaced.

If last_pos is defined, the function replaces the same extension from that position;
otherwise it finds from beginning. Index is 1-based. Returns nil, nil if not found.

Note this function is not thread-safe.

Back to TOC

x509:get_extension_critical

syntax: ok, err = x509:get_extension_critical(nid_or_txt)

Get critical flag of the X.509 extension matching the given NID from certificate.

Back to TOC

x509:set_extension_critical

syntax: ok, err = x509:set_extension_critical(nid_or_txt, crit?)

Set critical flag of the X.509 extension matching the given NID to certificate.

Back to TOC

x509:get_ocsp_url

syntax: url_or_urls, err = x509:get_ocsp_url(return_all?)

Get OCSP URL(s) of the X.509 object. If return_all is set to true, returns a table
containing all OCSP URLs; otherwise returns a string with first OCSP URL found.
Returns nil if the extension is not found.

Back to TOC

x509:get_crl_url

syntax: url_or_urls, err = x509:get_crl_url(return_all?)

Get CRL URL(s) of the X.509 object. If return_all is set to true, returns a table
containing all CRL URLs; otherwise returns a string with first CRL URL found.
Returns nil if the extension is not found.

Back to TOC

x509:sign

syntax: ok, err = x509:sign(pkey, digest?)

Sign the certificate using the private key specified by pkey, which must be a
resty.openssl.pkey that stores private key. Optionally accept digest
parameter to set digest method, whichmust be a resty.openssl.digest instance.
Returns a boolean indicating if signing is successful and error if any.

Back to TOC

x509:verify

syntax: ok, err = x509:verify(pkey)

Verify the certificate signature using the public key specified by pkey, which
must be a resty.openssl.pkey. Returns a boolean indicating if
verification is successful and error if any.

Back to TOC

x509:tostring

syntax: str, err = x509:tostring(fmt?)

Outputs certificate in PEM-formatted text or DER-formatted binary.
The first argument can be a choice of PEM or DER; when omitted, this function outputs PEM by default.

Back to TOC

x509:to_PEM

syntax: pem, err = x509:to_PEM()

Outputs the certificate in PEM-formatted text.

Back to TOC

resty.openssl.x509.csr

Module to interact with certificate signing request (X509_REQ).

See examples/csr.lua
for an example to generate CSR.

Back to TOC

csr.new

syntax: csr, err = csr.new(txt?, fmt?, properties?)

Create an empty csr instance. txt can be PEM or DER formatted text;
fmt is a choice of PEM, DER to load specific format, or * for auto detect.

When txt is omitted, new() creates an empty csr instance.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

csr.istype

syntax: ok = csr.istype(table)

Returns true if table is an instance of csr. Returns false otherwise.

Back to TOC

csr:check_private_key

syntax: match, err = csr:check_private_key(pkey)

Checks the consistency of private key pkey with the public key in current CSR object.

Returns a boolean indicating if it’s a match and err describing the reason.

Note this function also checks if k itself is indeed a private key or not.

Back to TOC

csr:get*, csr:set*

syntax: ok, err = csr:set_attribute(instance)

syntax: instance, err = csr:get_attribute()

Setters and getters for x509 attributes share the same syntax.

Attribute name Type Description
pubkey pkey Public key of the certificate request
subject_name x509.name Subject of the certificate request
version number Version of the certificate request, value is one less than version. For example, 2 represents version 3

Additionally, getters and setters for extensions are also available:

Extension name Type Description
subject_alt_name x509.altname Subject Alternative Name of the certificate request, SANs are usually used to define “additional Common Names”

For all extensions, get_{extension}_critical and set_{extension}_critical is also supported to
access the critical flag of the extension.

If the attribute is not found, getter will return nil, nil.

  1. local csr, err = require("resty.openssl.csr").new()
  2. err = csr:set_version(3)
  3. local version, err = csr:get_version()
  4. ngx.say(version)
  5. -- outputs 3

Note that user may also access the certain extension by csr:get_extension and
csr:set_extension, while the later two function returns or requires
extension instead. User may use getter and setters listed here if modification
of current extensions is needed; use csr:get_extension or
csr:set_extension if user are adding or replacing the whole extension or
getters/setters are not implemented. If the getter returned a type of x509.* instance, it can be
converted to a extension instance by extension:from_data,
and thus used by csr:get_extension and csr:set_extension

Back to TOC

csr:set_subject_alt

Same as csr:set_subject_alt_name, this function is deprecated to align
with naming convension with other functions.

Back to TOC

csr:get_signature_name, csr:get_signature_nid, csr:get_signature_digest_name

syntax: sn, err = csr:get_signature_name()

syntax: nid, err = csr:get_signature_nid()

syntax: sn, err = csr:get_signature_digest_name()

Return the NID or the short name (SN) of the signature of the certificate request.

csr:get_signature_digest_name returns the short name of the digest algorithm
used to sign the certificate.

Back to TOC

csr:get_extension

syntax: extension, pos, err = csr:get_extension(nid_or_txt, pos?)

Get X.509 extension matching the given NID to certificate, returns a
resty.openssl.x509.extension instance and the found position.

If last_pos is defined, the function searchs from that position; otherwise it
finds from beginning. Index is 1-based.

  1. local ext, pos, err = csr:get_extension("basicConstraints")

Back to TOC

csr:get_extensions

syntax: extensions, err = csr:get_extensions()

Return all extensions as a resty.openssl.x509.extensions instance.

Back to TOC

csr:add_extension

syntax: ok, err = csr:add_extension(extension)

Adds an X.509 extension to csr, the first argument must be a
resty.openssl.x509.extension instance.

Back to TOC

csr:set_extension

syntax: ok, err = csr:set_extension(extension)

Adds an X.509 extension to csr, the first argument must be a
resty.openssl.x509.extension instance.
The difference from csr:add_extension is that
in this function if a extension with same type already exists,
the old extension will be replaced.

Note this function is not thread-safe.

Back to TOC

csr:get_extension_critical

syntax: ok, err = csr:get_extension_critical(nid_or_txt)

Get critical flag of the X.509 extension matching the given NID from csr.

Back to TOC

csr:set_extension_critical

syntax: ok, err = csr:set_extension_critical(nid_or_txt, crit?)

Set critical flag of the X.509 extension matching the given NID to csr.

Back to TOC

csr:sign

syntax: ok, err = csr:sign(pkey, digest?)

Sign the certificate request using the private key specified by pkey, which must be a
resty.openssl.pkey that stores private key. Optionally accept digest
parameter to set digest method, whichmust be a resty.openssl.digest instance.
Returns a boolean indicating if signing is successful and error if any.

Back to TOC

csr:verify

syntax: ok, err = csr:verify(pkey)

Verify the CSR signature using the public key specified by pkey, which
must be a resty.openssl.pkey. Returns a boolean indicating if
verification is successful and error if any.

Back to TOC

csr:tostring

syntax: str, err = csr:tostring(fmt?)

Outputs certificate request in PEM-formatted text or DER-formatted binary.
The first argument can be a choice of PEM or DER; when omitted, this function outputs PEM by default.

Back to TOC

csr:to_PEM

syntax: pem, err = csr:to_PEM(?)

Outputs CSR in PEM-formatted text.

Back to TOC

resty.openssl.x509.crl

Module to interact with X509_CRL(certificate revocation list).

Back to TOC

crl.new

syntax: crt, err = crl.new(txt?, fmt?, properties?)

Creates a crl instance. txt can be PEM or DER formatted text;
fmt is a choice of PEM, DER to load specific format, or * for auto detect.

When txt is omitted, new() creates an empty crl instance.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

crl.dup

syntax: crl, err = crl.dup(crl_ptr_cdata)

Duplicates a X509_CRL* to create a new crl instance.

Back to TOC

crl.istype

syntax: ok = crl.istype(table)

Returns true if table is an instance of crl. Returns false otherwise.

Back to TOC

crl:get*, crl:set*

syntax: ok, err = crl:set_attribute(instance)

syntax: instance, err = crl:get_attribute()

Setters and getters for crl attributes share the same syntax.

Attribute name Type Description
issuer_name x509.name Issuer of the CRL
last_update number Unix timestamp when CRL is not valid before
next_update number Unix timestamp when CRL is not valid after
version number Version of the certificate, value is one less than version. For example, 2 represents version 3

Additionally, getters and setters for extensions are also available:

Extension name Type Description

For all extensions, get_{extension}_critical and set_{extension}_critical is also supported to
access the critical flag of the extension.

If the attribute is not found, getter will return nil, nil.

  1. local crl, err = require("resty.openssl.crl").new()
  2. err = crl:set_next_update(ngx.time())
  3. local not_before, err = crl:get_next_update()
  4. ngx.say(not_before)
  5. -- outputs 1571875065

Note that user may also access the certain extension by crl:get_extension and
crl:set_extension, while the later two function returns or requires
extension instead. User may use getter and setters listed here if modification
of current extensions is needed; use crl:get_extension or
crl:set_extension if user are adding or replacing the whole extension or
getters/setters are not implemented. If the getter returned a type of crl.* instance, it can be
converted to a extension instance by extension:from_data,
and thus used by crl:get_extension and crl:set_extension

Back to TOC

crl:get_signature_name, crl:get_signature_nid, crl:get_signature_digest_name

syntax: sn, err = crl:get_signature_name()

syntax: nid, err = crl:get_signature_nid()

syntax: sn, err = crl:get_signature_digest_name()

Return the NID or the short name (SN) of the signature of the CRL.

crl:get_signature_digest_name returns the short name of the digest algorithm
used to sign the certificate.

Back to TOC

crl:get_by_serial

syntax: found_revoked, err = crl:get_by_serial(serial)

Find if given serial is in the CRL, serial can be bn instance, or
a hexadecimal string. Returns a table if found where:

  1. {
  2. serial_number: serial number of the revoked cert in hexadecimal string,
  3. revoked_date: revoked date of the cert as unix timestamp
  4. }

Returns false if not found; specially if a serial number is removed from CRL, then
false, "not revoked (removeFromCRL)" is returned.

Back to TOC

crl:get_extension

syntax: extension, pos, err = crl:get_extension(nid_or_txt, last_pos?)

Get X.509 extension matching the given NID to CRL, returns a
resty.openssl.x509.extension instance and the found position.

If last_pos is defined, the function searchs from that position; otherwise it
finds from beginning. Index is 1-based.

Back to TOC

crl:add_extension

syntax: ok, err = crl:add_extension(extension)

Adds an X.509 extension to CRL, the first argument must be a
resty.openssl.x509.extension instance.

Back to TOC

crl:set_extension

syntax: ok, err = crl:set_extension(extension, last_pos?)

Adds an X.509 extension to CRL, the first argument must be a
resty.openssl.x509.extension instance.
The difference from crl:add_extension is that
in this function if a extension with same type already exists,
the old extension will be replaced.

If last_pos is defined, the function replaces the same extension from that position;
otherwise it finds from beginning. Index is 1-based. Returns nil, nil if not found.

Note this function is not thread-safe.

Back to TOC

crl:get_extension_critical

syntax: ok, err = crl:get_extension_critical(nid_or_txt)

Get critical flag of the X.509 extension matching the given NID from CRL.

Back to TOC

crl:set_extension_critical

syntax: ok, err = crl:set_extension_critical(nid_or_txt, crit?)

Set critical flag of the X.509 extension matching the given NID to CRL.

Back to TOC

crl:add_revoked

syntax: ok, err = crl:add_revoked(revoked)

Adds a resty.openssl.x509.revoked instance to the CRL.

Back to TOC

crl:sign

syntax: ok, err = crl:sign(pkey, digest?)

Sign the CRL using the private key specified by pkey, which must be a
resty.openssl.pkey that stores private key. Optionally accept digest
parameter to set digest method, whichmust be a resty.openssl.digest instance.
Returns a boolean indicating if signing is successful and error if any.

Back to TOC

crl:verify

syntax: ok, err = crl:verify(pkey)

Verify the CRL signature using the public key specified by pkey, which
must be a resty.openssl.pkey. Returns a boolean indicating if
verification is successful and error if any.

Back to TOC

crl:tostring

syntax: str, err = crl:tostring(fmt?)

Outputs CRL in PEM-formatted text or DER-formatted binary.
The first argument can be a choice of PEM or DER; when omitted, this function outputs PEM by default.

Back to TOC

crl:text

syntax: str, err = crl:text()

Outputs CRL in a human-readable format.

Back to TOC

crl:to_PEM

syntax: pem, err = crl:to_PEM()

Outputs the CRL in PEM-formatted text.

Back to TOC

crl:__metamethods

syntax: for i, revoked in ipairs(crl)

syntax: len = #crl

syntax: revoked = crl[i]

Access the revoked list as it’s a Lua table. Make sure your LuaJIT compiled
with -DLUAJIT_ENABLE_LUA52COMPAT flag; otherwise use all, each, index and count
instead.

See also functions for stack-like objects.

Each returned object is a table where:

  1. {
  2. serial_number: serial number of the revoked cert in hexadecimal string,
  3. revoked_date: revoked date of the cert as unix timestamp
  4. }
  1. local f = io.open("t/fixtures/TrustAsiaEVTLSProCAG2.crl"):read("*a")
  2. local crl = assert(require("resty.openssl.x509.crl").new(f))
  3. for _, obj in ipairs(crl) do
  4. ngx.say(require("cjson").encode(obj))
  5. end
  6. -- outputs '{"revocation_date":1577753344,"serial_number":"09159859CAC0C90203BB34C5A012C2A3"}'

Back to TOC

resty.openssl.x509.name

Module to interact with X.509 names.

Back to TOC

name.new

syntax: name, err = name.new()

Creates an empty name instance.

Back to TOC

name.dup

syntax: name, err = name.dup(name_ptr_cdata)

Duplicates a X509_NAME* to create a new name instance.

Back to TOC

name.istype

syntax: ok = name.istype(table)

Returns true if table is an instance of name. Returns false otherwise.

Back to TOC

name:add

syntax: name, err = name:add(nid_text, txt)

Adds an ASN.1 object to name. First arguments in the text representation of NID.
Second argument is the plain text value for the ASN.1 object.

Returns the name instance itself on success, or nil and an error on failure.

This function can be called multiple times in a chained fashion.

  1. local name, err = require("resty.openssl.x509.name").new()
  2. local _, err = name:add("CN", "example.com")
  3. _, err = name
  4. :add("C", "US")
  5. :add("ST", "California")
  6. :add("L", "San Francisco")

Back to TOC

name:find

syntax: obj, pos, err = name:find(nid_text, last_pos?)

Finds the ASN.1 object with the given text representation of NID from the
postition of last_pos. By omitting the last_pos parameter, find finds from the beginning.

Returns the object in a table as same format as decribed here, the position
of the found object and error if any. Index is 1-based. Returns nil, nil if not found.

  1. local name, err = require("resty.openssl.x509.name").new()
  2. local _, err = name:add("CN", "example.com")
  3. :add("CN", "example2.com")
  4. local obj, pos, err = name:find("CN")
  5. ngx.say(obj.blob, " at ", pos)
  6. -- outputs "example.com at 1"
  7. local obj, pos, err = name:find("2.5.4.3", 1)
  8. ngx.say(obj.blob, " at ", pos)
  9. -- outputs "example2.com at 2"

Back to TOC

name:tostring

syntax: txt = name:tostring()

Outputs name in a text representation.

  1. local name, err = require("resty.openssl.x509.name").new()
  2. local _, err = name:add("CN", "example.com")
  3. :add("CN", "example2.com")
  4. ngx.say(name:tostring())
  5. -- outputs "CN=example.com/CN=example2.com"

Back to TOC

name:__metamethods

syntax: for k, obj in pairs(name)

syntax: len = #name

syntax: k, v = name[i]

Access the underlying objects as it’s a Lua table. Make sure your LuaJIT compiled
with -DLUAJIT_ENABLE_LUA52COMPAT flag; otherwise use all, each, index and count
instead.

See also functions for stack-like objects.

Each returned object is a table where:

  1. {
  2. id: OID of the object,
  3. nid: NID of the object,
  4. sn: short name of the object,
  5. ln: long name of the object,
  6. blob: value of the object,
  7. }
  1. local name, err = require("resty.openssl.x509.name").new()
  2. local _, err = name:add("CN", "example.com")
  3. for k, obj in pairs(name) do
  4. ngx.say(k, ":", require("cjson").encode(obj))
  5. end
  6. -- outputs 'CN: {"sn":"CN","id":"2.5.4.3","nid":13,"blob":"3.example.com","ln":"commonName"}'

Back to TOC

resty.openssl.x509.altname

Module to interact with GENERAL_NAMES, an extension to X.509 names.

Back to TOC

altname.new

syntax: altname, err = altname.new()

Creates an empty altname instance.

Back to TOC

altname.dup

syntax: altname, err = altname.dup(altname_ptr_cdata)

Duplicates a STACK_OF(GENERAL_NAMES) to create a new altname instance. The function creates a new
stack but won’t duplicates elements in the stack.

Back to TOC

altname.istype

syntax: altname = digest.istype(table)

Returns true if table is an instance of altname. Returns false otherwise.

Back to TOC

altname:add

syntax: altname, err = altname:add(key, value)

Adds a name to altname stack, first argument is case-insensitive and can be one of

  1. RFC822Name
  2. RFC822
  3. Email
  4. UniformResourceIdentifier
  5. URI
  6. DNSName
  7. DNS
  8. IP
  9. IPAddress

This function can be called multiple times in a chained fashion.

Back to TOC

altname:tostring

syntax: txt = altname:tostring()

Outputs altname in a text representation.

  1. local altname, err = require("resty.openssl.x509.altname").new()
  2. _, err = altname
  3. :add("DNS", "2.example.com")
  4. :add("DnS", "3.example.com")
  5. ngx.say(altname:tostring())
  6. -- outputs "DNS=2.example.com/DNS=3.example.com"

Back to TOC

altname:__metamethods

syntax: for k, obj in pairs(altname)

syntax: len = #altname

syntax: k, v = altname[i]

Access the underlying objects as it’s a Lua table. Make sure your LuaJIT compiled
with -DLUAJIT_ENABLE_LUA52COMPAT flag; otherwise use all, each, index and count
instead.

See also functions for stack-like objects.

Only the following types are decoded, other types are decoded as "TYPE:<unsupported>":

  1. RFC822Name / Email
  2. URI
  3. DNS
  4. DirName

Back to TOC

resty.openssl.x509.extension

Module to interact with every X.509 extensions.

This module is particular useful to create extensions not supported by
x509.* modules or custom extensions.

Back to TOC

extension.new

syntax: ext, err = extension.new(name, value, data?)

Creates a new extension instance. name and value are strings in OpenSSL
arbitrary extension format.

data can be a table, string or nil. Where data is a table, the following key will be looked up:

  1. data = {
  2. issuer = resty.openssl.x509 instance,
  3. subject = resty.openssl.x509 instance,
  4. request = resty.openssl.x509.csr instance,
  5. crl = resty.openssl.x509.crl instance,
  6. issuer_pkey = resty.openssl.pkey instance, -- >= OpenSSL 3.0
  7. }

From OpenSSL 3.0, issuer_pkey can be specified as a fallback source for
generating the authority key identifier extension when issuer is same as subject.

When data is a string, it’s the full nconf string. Using section lookup from value to
data is also supported.


Example usages:

lua local extension = require("resty.openssl.x509.extension") -- extendedKeyUsage=serverAuth,clientAuth local ext, err = extension.new("extendedKeyUsage", "serverAuth,clientAuth") -- crlDistributionPoints=URI:http://myhost.com/myca.crl ext, err = extension.new("crlDistributionPoints", "URI:http://myhost.com/myca.crl") -- with section lookup ext, err = extension.new( "crlDistributionPoints", "crldp1_section", [[ [crldp1_section] fullname=URI:http://myhost.com/myca.crl CRLissuer=dirName:issuer_sect reasons=keyCompromise, CACompromise [issuer_sect] C=UK O=Organisation CN=Some Name ]] ) -- combine section lookup with other value ext, err = extension.new( "certificatePolicies", "ia5org,1.2.3.4,1.5.6.7.8,@polsect", [[ [polsect] policyIdentifier = 1.3.5.8 CPS.1="http://my.host.name/" CPS.2="http://my.your.name/" userNotice.1=@notice [notice] explicitText="Explicit Text Here" organization="Organisation Name" noticeNumbers=1,2,3,4 ]] )) -- subjectKeyIdentifier=hash local x509, err = require("resty.openssl.x509").new() ext, err = extension.new("subjectKeyIdentifier", "hash", { subject = x509 })

See examples/tls-alpn-01.lua
for an example to create extension with an unknown nid.

Back to TOC

extension.dup

syntax: ext, err = extension.dup(extension_ptr_cdata)

Creates a new extension instance from X509_EXTENSION* pointer.

Back to TOC

extension.from_der

syntax: ext, ok = extension.from_der(der, nid_or_txt, crit?)

Creates a new extension instance. der is the ASN.1 encoded string to be
set for the extension.

nid_or_txt is a number or text representation of NID and
crit is the critical flag of the extension.

See examples/tls-alpn-01.lua
for an example to create extension with an unknown nid.

Back to TOC

extension:to_der

syntax: der, ok = extension:to_der()

Returns the ASN.1 encoded (DER) value of the extension.

nid_or_txt is a number or text representation of NID. Note DER is a binary
encoding format. Consider using extension:text for human readable
or printable output.

Back to TOC

extension.from_data

syntax: ext, ok = extension.from_data(table, nid_or_txt, crit?)

Creates a new extension instance. table can be instance of:

nid_or_txt is a number or text representation of NID and
crit is the critical flag of the extension.

Back to TOC

extension.to_data

syntax: ext, ok = extension:to_data(nid_or_txt)

Convert the extension to another wrapper instance. Currently supported following:

nid_or_txt is a number or text representation of NID.

Back to TOC

extension.istype

syntax: ok = extension.istype(table)

Returns true if table is an instance of extension. Returns false otherwise.

Back to TOC

extension:get_extension_critical

syntax: crit, err = extension:get_extension_critical()

Returns true if extension is critical. Returns false otherwise.

Back to TOC

extension:set_extension_critical

syntax: ok, err = extension:set_extension_critical(crit)

Set the critical flag of the extension.

Back to TOC

extension:get_object

syntax: obj = extension:get_object()

Returns the name of extension as ASN.1 Object. User can further use helper functions in
resty.openssl.objects to print human readable texts.

Back to TOC

extension:text

syntax: txt, err = extension:text()

Returns the text representation of extension. This functions calls X509V3_EXT_print under the hood,
and fallback to ASN1_STRING_print if the former failed. It thus has exact same output with that
of openssl x509 -text.

  1. local objects = require "resty.openssl.objects"
  2. ngx.say(cjson.encode(objects.obj2table(extension:get_object())))
  3. -- outputs '{"ln":"X509v3 Subject Key Identifier","nid":82,"sn":"subjectKeyIdentifier","id":"2.5.29.14"}'
  4. ngx.say(extension:text())
  5. -- outputs "C9:C2:53:61:66:9D:5F:AB:25:F4:26:CD:0F:38:9A:A8:49:EA:48:A9"

Back to TOC

extension:tostring

syntax: txt, err = extension:tostring()

Same as extension:text.

Back to TOC

resty.openssl.x509.extension.dist_points

Module to interact with CRL Distribution Points(DIST_POINT stack).

Back to TOC

dist_points.new

syntax: dp, err = dist_points.new()

Creates a new dist_points instance.

Back to TOC

dist_points.dup

syntax: dp, err = dist_points.dup(dist_points_ptr_cdata)

Duplicates a STACK_OF(DIST_POINT) to create a new dist_points instance. The function creates a new
stack but won’t duplicates elements in the stack.

Back to TOC

dist_points.istype

syntax: ok = dist_points.istype(table)

Returns true if table is an instance of dist_points. Returns false otherwise.

Back to TOC

dist_points:__metamethods

syntax: for i, obj in ipairs(dist_points)

syntax: len = #dist_points

syntax: obj = dist_points[i]

Access the underlying objects as it’s a Lua table. Make sure your LuaJIT compiled
with -DLUAJIT_ENABLE_LUA52COMPAT flag; otherwise use all, each, index and count
instead.

See also functions for stack-like objects.

Each object returned when iterrating dist_points instance is a x509.altname
instance.

  1. local x = x509.new(io.open("/path/to/a_cert_has_dist_points.crt"):read("*a"))
  2. local cdp = x:get_crl_distribution_points()
  3. local an = cdp[1]
  4. ngx.say(an:tostring())
  5. -- or any other function for resty.openssl.x509.altname
  6. for _, an in ipairs(cdp) do
  7. ngx.say(an:tostring())
  8. end

Back to TOC

resty.openssl.x509.extension.info_access

Module to interact with Authority Information Access data (AUTHORITY_INFO_ACCESS, ACCESS_DESCRIPTION stack).

Back to TOC

info_access.new

syntax: aia, err = info_access.new()

Creates a new info_access instance.

Back to TOC

info_access.dup

syntax: aia, err = info_access.dup(info_access_ptr_cdata)

Duplicates a AUTHORITY_INFO_ACCESS to create a new info_access instance. The function creates a new
stack but won’t duplicates elements in the stack.

Back to TOC

info_access.istype

syntax: ok = info_access.istype(table)

Returns true if table is an instance of info_access. Returns false otherwise.

Back to TOC

info_access:add

syntax: ok, err = info_access:add(x509)

Add a x509 object to the info_access. The first argument must be a
resty.openssl.x509 instance.

Back to TOC

info_access:__metamethods

syntax: for i, obj in ipairs(info_access)

syntax: len = #info_access

syntax: obj = info_access[i]

Access the underlying objects as it’s a Lua table. Make sure your LuaJIT compiled
with -DLUAJIT_ENABLE_LUA52COMPAT flag; otherwise use all, each, index and count
instead.

See also functions for stack-like objects.

Each object returned when iterrating dist_points instance is a table of NID type and values.

  1. local cjson = require("cjosn")
  2. local x509 = require("resty.openssl.x509")
  3. local crt = x509.new(io.open("/path/to/a_cert_has_info_access.crt"):read("*a"))
  4. local aia = crt:get_info_access()
  5. ngx.say(cjson.encode(aia[1]))
  6. -- outputs '[178,"URI","http:\/\/ocsp.starfieldtech.com\/"]'
  7. for _, a in ipairs(aia) do
  8. ngx.say(cjson.encode(a))
  9. end

Back to TOC

resty.openssl.x509.extensions

Module to interact with X.509 Extension stack.

Back to TOC

extensions.new

syntax: ch, err = extensions.new()

Creates a new extensions instance.

Back to TOC

extensions.dup

syntax: ch, err = extensions.dup(extensions_ptr_cdata)

Duplicates a STACK_OF(X509_EXTENSION) to create a new extensions instance. The function creates a new
stack but won’t duplicates elements in the stack.

Back to TOC

extensions.istype

syntax: ok = extensions.istype(table)

Returns true if table is an instance of extensions. Returns false otherwise.

Back to TOC

extensions:add

syntax: ok, err = extensions:add(x509)

Add a x509 object to the extensions. The first argument must be a
resty.openssl.x509 instance.

Back to TOC

extensions:__metamethods

syntax: for i, obj in ipairs(extensions)

syntax: len = #extensions

syntax: obj = extensions[i]

Access the underlying objects as it’s a Lua table. Make sure your LuaJIT compiled
with -DLUAJIT_ENABLE_LUA52COMPAT flag; otherwise use all, each, index and count
instead.

See also functions for stack-like objects.

Back to TOC

resty.openssl.x509.chain

Module to interact with X.509 stack.

Back to TOC

chain.new

syntax: ch, err = chain.new()

Creates a new chain instance.

Back to TOC

chain.dup

syntax: ch, err = chain.dup(chain_ptr_cdata)

Duplicates a STACK_OF(X509) to create a new chain instance. The function creates a new
stack and increases reference count for all elements by 1. But it won’t duplicate the elements
themselves.

Back to TOC

chain.istype

syntax: ok = chain.istype(table)

Returns true if table is an instance of chain. Returns false otherwise.

Back to TOC

chain:add

syntax: ok, err = chain:add(x509)

Add a x509 object to the chain. The first argument must be a
resty.openssl.x509 instance.

Back to TOC

chain:__metamethods

syntax: for i, obj in ipairs(chain)

syntax: len = #chain

syntax: obj = chain[i]

Access the underlying objects as it’s a Lua table. Make sure your LuaJIT compiled
with -DLUAJIT_ENABLE_LUA52COMPAT flag; otherwise use all, each, index and count
instead.

See also functions for stack-like objects.

Back to TOC

resty.openssl.x509.store

Module to interact with X.509 certificate store (X509_STORE).

Back to TOC

store.new

syntax: st, err = store.new(properties?)

Creates a new store instance.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

store.istype

syntax: ok = store.istype(table)

Returns true if table is an instance of store. Returns false otherwise.

Back to TOC

store:use_default

syntax: ok, err = store:use_default(properties?)

Loads certificates into the X509_STORE from the hardcoded default paths.

Note that to load “default” CAs correctly, usually you need to install a CA
certificates bundle. For example, the package in Debian/Ubuntu is called ca-certificates.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

store:add

syntax: ok, err = store:add(x509_or_crl, skip_set_crl_check_flags?)

Adds a X.509 or a CRL object into store.
The argument must be a resty.openssl.x509 instance or a
resty.openssl.x509.crl instance.

By default, adding a CRL object will automatically set the flag to store
X509_V_FLAG_CRL_CHECK. Setting the second optional argument to true will
skip settting the flags.

Back to TOC

store:load_file

syntax: ok, err = store:load_file(path, properties?)

Loads a X.509 certificate on file system into store.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

store:load_directory

syntax: ok, err = store:load_directory(path, properties?)

Loads a directory of X.509 certificates on file system into store. The certificates in the directory
must be in hashed form, as documented in
X509_LOOKUP_hash_dir(3).

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Back to TOC

store:set_purpose

syntax: ok, err = store:set_purpose(purpose)

Set the X509_STORE to match Key Usage and Extendend Key Usage when verifying the cert.
Possible values are:

  1. sslclient SSL client
  2. sslserver SSL server
  3. nssslserver Netscape SSL server
  4. smimesign S/MIME signing
  5. smimeencrypt S/MIME encryption
  6. crlsign CRL signing
  7. any Any Purpose
  8. ocsphelper OCSP helper
  9. timestampsign Time Stamp signing

Normally user should use verify_method parameter of store:verify unless the purpose
is not included in the default verify methods.

Back to TOC

store:set_depth

syntax: ok, err = store:set_depth(depth)

Set the verify depth.

store:set_flags

syntax: ok, err = store:set_flags(flag1, flag2, …)

Set the verify flags, available via store.verify_flags table:

  1. X509_V_FLAG_CB_ISSUER_CHECK = 0x0, -- Deprecated
  2. X509_V_FLAG_USE_CHECK_TIME = 0x2,
  3. X509_V_FLAG_CRL_CHECK = 0x4,
  4. X509_V_FLAG_CRL_CHECK_ALL = 0x8,
  5. X509_V_FLAG_IGNORE_CRITICAL = 0x10,
  6. X509_V_FLAG_X509_STRICT = 0x20,
  7. X509_V_FLAG_ALLOW_PROXY_CERTS = 0x40,
  8. X509_V_FLAG_POLICY_CHECK = 0x80,
  9. X509_V_FLAG_EXPLICIT_POLICY = 0x100,
  10. X509_V_FLAG_INHIBIT_ANY = 0x200,
  11. X509_V_FLAG_INHIBIT_MAP = 0x400,
  12. X509_V_FLAG_NOTIFY_POLICY = 0x800,
  13. X509_V_FLAG_EXTENDED_CRL_SUPPORT = 0x1000,
  14. X509_V_FLAG_USE_DELTAS = 0x2000,
  15. X509_V_FLAG_CHECK_SS_SIGNATURE = 0x4000,
  16. X509_V_FLAG_TRUSTED_FIRST = 0x8000,
  17. X509_V_FLAG_SUITEB_128_LOS_ONLY = 0x10000,
  18. X509_V_FLAG_SUITEB_192_LOS = 0x20000,
  19. X509_V_FLAG_SUITEB_128_LOS = 0x30000,
  20. X509_V_FLAG_PARTIAL_CHAIN = 0x80000,
  21. X509_V_FLAG_NO_ALT_CHAINS = 0x100000,
  22. X509_V_FLAG_NO_CHECK_TIME = 0x200000,
  1. store:set_flags(store.verify_flags.X509_V_FLAG_PARTIAL_CHAIN)
  2. store:set_flags(store.verify_flags.X509_V_FLAG_PARTIAL_CHAIN,
  3. store.verify_flags.X509_V_FLAG_NO_CHECK_TIME)
  4. store:set_flags(store.verify_flags.X509_V_FLAG_PARTIAL_CHAIN +
  5. store.verify_flags.X509_V_FLAG_NO_CHECK_TIME)

See X509_VERIFY_PARAM_set_flags(3)
for explanation of each flag.

Back to TOC

store:verify

syntax: chain, err = store:verify(x509, chain?, return_chain?, properties?, verify_method?, verify_flags?)

Verifies a X.509 object with the store. The first argument must be
resty.openssl.x509 instance. Optionally accept a validation chain as second
argument, which must be a resty.openssl.x509.chain instance.

If verification succeed, and return_chain is set to true, returns the proof of validation as a
resty.openssl.x509.chain; otherwise
returns true only. If verification failed, returns nil and error explaining the reason.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

verify_method can be set to use predefined verify parameters such as "default", "pkcs7",
"smime_sign", "ssl_client" and "ssl_server". This set corresponding purpose, trust and
couple of other defaults but does not override the parameters set from
store:set_purpose.

verify_flags paramter is the additional verify flags to be set. See store:set_flags
for all available flags.

Back to TOC

store:check_revocation

syntax: ok, err = store:check_revocation(verified_chain, properties?)

Only does the revocation check. The first argument verified_chain must be a
resty.openssl.x509.chain instance which could be returned from
store_ctx:verify or be built by yourself. Note the first cert needs to be the end entity
certificate you want to check and the second cert needs to be its issuer.

Staring from OpenSSL 3.0, this function accepts an optional properties parameter
to explictly select provider to fetch algorithms.

Returns true when the certificate isn’t revoked,
otherwise returns nil and error explaining the reason.

Back to TOC

resty.openssl.x509.revoked

Module to interact with X509_REVOKED.

Back to TOC

revoked.new

syntax: ch, err = revoked.new(serial_number, time, reason)

Creates a new revoked instance. serial_number can be either a resty.openssl.bn
instance or a number. time and reason must be numbers.

Back to TOC

revoked.istype

syntax: ok = revoked.istype(table)

Returns true if table is an instance of revoked. Returns false otherwise.

Back to TOC

resty.openssl.ssl

Module to interact with SSL connection.

This module is currently considered experimental.

Note: to use this module in production, user is encouraged to compile lua-resty-openssl-aux-module.

Back to TOC

ssl.from_request

syntax: sess, err = ssl.from_request()

Wraps the SSL* instance from current downstream request.

Back to TOC

ssl.from_socket

syntax: sess, err = ssl.from_socket(sock)

Wraps the SSL* instance from a TCP cosocket, the cosocket must have already
been called sslhandshake.

Back to TOC

ssl:get_peer_certificate

syntax: x509, err = ssl:get_peer_certificate()

Return the peer certificate as a x509 instance. Depending on the type
of ssl, peer certificate means the server certificate on client side, or the client certificate
on server side.

This function should be called after SSL handshake.

Back to TOC

ssl:get_peer_cert_chain

syntax: chain, err = ssl:get_peer_certificate()

Return the whole peer certificate chain as a x509.chain instance.
Depending on the type of ssl, peer certificate means the server certificate on client side,
or the client certificate on server side.

This function should be called after SSL handshake.

Back to TOC

ssl:set_ciphersuites, ssl:set_cipher_list

syntax: ok, err = ssl:set_ciphersuites(cipher_suite)
syntax: ok, err = ssl:set_cipher_list(cipher_list)

Set the cipher suites string used in handshake. Use ssl:set_ciphersuites for TLSv1.3 andssl:set_cipher_list` for lower.

This function should be called before SSL handshake; for server this means this function
is only effective in ssl_certificate_by or ssl_client_hello_by phases.

Back to TOC

ssl:get_ciphers

syntax: ciphers, err = ssl:get_ciphers()

Get the cipher list string used in handshake as a string.

Back to TOC

ssl:get_cipher_name

syntax: cipher_name, err = ssl:get_cipher_name()

Get the negotiated cipher name as a string.

This function should be called after SSL handshake.

Back to TOC

ssl:set_timeout

syntax: ok, err = ssl:set_timeout(tm)

Set the timeout value for current session in seconds tm.

Back to TOC

ssl:get_timeout

syntax: tm, err = ssl:set_timeout(tm)

Get the timeout value for current session in seconds.

Back to TOC

ssl:set_verify

syntax: ok, err = ssl:set_verify(mode)

Set the verify mode in current session. Available modes are:

  1. ssl.SSL_VERIFY_NONE = 0x00,
  2. ssl.SSL_VERIFY_PEER = 0x01,
  3. ssl.SSL_VERIFY_FAIL_IF_NO_PEER_CERT = 0x02,
  4. ssl.SSL_VERIFY_CLIENT_ONCE = 0x04,
  5. ssl.SSL_VERIFY_POST_HANDSHAKE = 0x08,

This function should be called before SSL handshake; for server this means this function
is only effective in ssl_certificate_by or ssl_client_hello_by phases.

Back to TOC

ssl:add_client_ca

syntax: ok, err = ssl:add_client_ca(x509)

Add the CA name extracted from x509 to the list of CAs sent to the client
when requesting a client certificate. x509 is a x509
instance. This function doesn’t affect the verification result of client certificate.

This function should be called before SSL handshake; for server this means this function
is only effective in ssl_certificate_by or ssl_client_hello_by phases.

Back to TOC

ssl:set_options

syntax: bitmask, err = ssl:set_options(option, …)

Set one or more options in current session. Available options are:


SSL options

ssl.SSL_OP_NO_EXTENDED_MASTER_SECRET = 0x00000001, ssl.SSL_OP_CLEANSE_PLAINTEXT = 0x00000002, ssl.SSL_OP_LEGACY_SERVER_CONNECT = 0x00000004, ssl.SSL_OP_TLSEXT_PADDING = 0x00000010, ssl.SSL_OP_SAFARI_ECDHE_ECDSA_BUG = 0x00000040, ssl.SSL_OP_IGNORE_UNEXPECTED_EOF = 0x00000080, ssl.SSL_OP_DISABLE_TLSEXT_CA_NAMES = 0x00000200, ssl.SSL_OP_ALLOW_NO_DHE_KEX = 0x00000400, ssl.SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS = 0x00000800, ssl.SSL_OP_NO_QUERY_MTU = 0x00001000, ssl.SSL_OP_COOKIE_EXCHANGE = 0x00002000, ssl.SSL_OP_NO_TICKET = 0x00004000, ssl.SSL_OP_CISCO_ANYCONNECT = 0x00008000, ssl.SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION = 0x00010000, ssl.SSL_OP_NO_COMPRESSION = 0x00020000, ssl.SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION = 0x00040000, ssl.SSL_OP_NO_ENCRYPT_THEN_MAC = 0x00080000, ssl.SSL_OP_ENABLE_MIDDLEBOX_COMPAT = 0x00100000, ssl.SSL_OP_PRIORITIZE_CHACHA = 0x00200000, ssl.SSL_OP_CIPHER_SERVER_PREFERENCE = 0x00400000, ssl.SSL_OP_TLS_ROLLBACK_BUG = 0x00800000, ssl.SSL_OP_NO_ANTI_REPLAY = 0x01000000, ssl.SSL_OP_NO_SSLv3 = 0x02000000, ssl.SSL_OP_NO_TLSv1 = 0x04000000, ssl.SSL_OP_NO_TLSv1_2 = 0x08000000, ssl.SSL_OP_NO_TLSv1_1 = 0x10000000, ssl.SSL_OP_NO_TLSv1_3 = 0x20000000, ssl.SSL_OP_NO_DTLSv1 = 0x04000000, ssl.SSL_OP_NO_DTLSv1_2 = 0x08000000, ssl.SSL_OP_NO_RENEGOTIATION = 0x40000000, ssl.SSL_OP_CRYPTOPRO_TLSEXT_BUG = 0x80000000,

Multiple options can be passed in seperatedly, or in a bitwise or bitmask.

  1. assert(ssl:set_options(bit.bor(ssl.SSL_OP_NO_TLSv1_1, ssl.SSL_OP_NO_TLSv1_2)))
  2. -- same as
  3. assert(ssl:set_options(ssl.SSL_OP_NO_TLSv1_1, ssl.SSL_OP_NO_TLSv1_2))

Returns the options of current session in bitmask.

This function should be called before SSL handshake; for server this means this function
is only effective in ssl_client_hello_by phase.

Back to TOC

ssl:get_options

syntax: bitmask, err = ssl:get_options(readable?)

Get the options for current session. If readable is not set or set to false, the function
return the bit mask for all optinos; if readable is set to true, the function returns
a sorted Lua table containing literals for all options.

Back to TOC

ssl:clear_options

syntax: bitmask, err = ssl:clear_options(option, …)

Clear one or more options in current session.
Available options are same as that in ssl:set_options.

Multiple options can be passed in seperatedly, or in a bitwise or bitmask.

  1. assert(ssl:clear_options(bit.bor(ssl.SSL_OP_NO_TLSv1_1, ssl.SSL_OP_NO_TLSv1_2)))
  2. -- same as
  3. assert(ssl:clear_options(ssl.SSL_OP_NO_TLSv1_1, ssl.SSL_OP_NO_TLSv1_2))

Returns the options of current session in bitmask.

This function should be called before SSL handshake; for server this means this function
is only effective in ssl_client_hello_by phase.

Back to TOC

ssl:set_protocols

syntax: bitmask, err = ssl:set_protocols(protocol, …)

Set avaialable protocols for handshake, this is a convenient function that
calls ssl:set_options and ssl:clear_options to
set appropriate options.

Returns the options of current session in bitmask.

This function should be called before SSL handshake; for server this means this function
is only effective in ssl_client_hello_by phase.

  1. assert(ssl:set_protocols("TLSv1.1", "TLSv1.2"))
  2. -- same as
  3. assert(ssl:set_options(ssl.SSL_OP_NO_SSL_MASK))
  4. assert(ssl:clear_options(ssl.SSL_OP_NO_TLSv1_1, ssl.SSL_OP_NO_TLSv1_2))

Back to TOC

resty.openssl.ssl_ctx

Module to interact with SSL_CTX context.

This module is currently considered experimental.

Note: to use this module in production, user is encouraged to compile lua-resty-openssl-aux-module.

Back to TOC

ssl_ctx.from_request

syntax: ctx, err = ssl_ctx.from_request()

Wraps the SSL_CTX* instance from current downstream request.

Back to TOC

ssl_ctx.from_socket

syntax: sess, err = ssl_ctx.from_socket(sock)

Wraps the SSL_CTX* instance from a TCP cosocket, the cosocket must have already
been called sslhandshake.

Back to TOC

ssl_ctx:set_alpns

syntax: ok, err = ssl_ctx:set_alpns(alpn, …)

Set the ALPN list to be negotiated with peer. Each alpn is the plaintext
literal for the protocol, like "h2".

Back to TOC

Functions for stack-like objects

Back to TOC

metamethods

syntax: for k, obj in pairs(x)

syntax: for k, obj in ipairs(x)

syntax: len = #x

syntax: obj = x[i]

Access the underlying objects as it’s a Lua table. Make sure your LuaJIT compiled
with -DLUAJIT_ENABLE_LUA52COMPAT flag.

Each object may only support either pairs or ipairs. Index is 1-based.

  1. local name, err = require("resty.openssl.x509.name").new()
  2. local _, err = name:add("CN", "example.com")
  3. for k, obj in pairs(name) do
  4. ngx.say(k, ":", require("cjson").encode(obj))
  5. end
  6. -- outputs 'CN: {"sn":"CN","id":"2.5.4.3","nid":13,"blob":"3.example.com","ln":"commonName"}'

Back to TOC

each

syntax: iter = x:each()

Return an iterator to traverse objects. Use this while LUAJIT_ENABLE_LUA52COMPAT is not enabled.

  1. local name, err = require("resty.openssl.x509.name").new()
  2. local _, err = name:add("CN", "example.com")
  3. local iter = name:each()
  4. while true do
  5. local k, obj = iter()
  6. if not k then
  7. break
  8. end
  9. end

Back to TOC

all

syntax: objs, err = x:all()

Returns all objects in the table. Use this while LUAJIT_ENABLE_LUA52COMPAT is not enabled.

Back to TOC

count

syntax: len = x:count()

Returns count of objects of the table. Use this while LUAJIT_ENABLE_LUA52COMPAT is not enabled.

Back to TOC

index

syntax: obj = x:index(i)

Returns objects at index of i of the table, index is 1-based. If index is out of bound, nil is returned.

Back to TOC

General rules on garbage collection

  • When a type is added or returned to another type, it’s internal cdata is always copied.
    ``lua local name = require("resty.openssl.x509.name"):add("CN", "example.com") local x509 = require("resty.openssl.x509").new() --name` is copied when added to x509
    x509:set_subject_name(name)

name:add(“L”, “Mars”)
— subject_name in x509 will not be modified

  1. - The creator set the GC handler; the user must not free it.
  2. - For a stack:
  3. - If it's created by `new()`: set GC handler to sk_TYPE_pop_free
  4. - The gc handler for elements being added to stack should not be set. Instead, rely on the gc
  5. handler of the stack to free each individual elements.
  6. - If it's created by `dup()` (shallow copy):
  7. - If elements support reference counter (like X509): increase ref count for all elements by 1;
  8. set GC handler to sk_TYPE_pop_free.
  9. - If not, set GC handler to sk_free
  10. - Additionally, the stack duplicates the element when it's added to stack, a GC handler for the duplicate
  11. must be set. But a reference should be kept in Lua land to prevent premature
  12. gc of individual elements. (See x509.altname).
  13. - Shallow copy for stack is fine because in current design user can't modify the element in the
  14. stack directly. Each elemente is duplicated when added to stack and when returned.
  15. [Back to TOC](#table-of-contents)
  16. ## Generic EVP parameter getter/setter
  17. Starting from OpenSSL 3.0, this library provides a genric interface to get and set abitrary parameters
  18. from underlying implementation. This works for [cipher](#resty.openssl.cipher),
  19. [pkey](#resty.openssl.pkey), [digest](#resty.openssl.digest), [mac](#resty.openssl.mac) and
  20. [kdf](#resty.openssl.kdf).
  21. Some can be used to provide equal results with existing functions, for example the following
  22. code produces same result.
  23. ```lua
  24. local pkey = require("resty.openssl.pkey").new({ type = "EC" })
  25. pkey:get_param("priv", nil, "bn") == pkey:get_parameters().private
  26. local cipher = require("resty.openssl.cipher").new("aes-256-gcm")
  27. cipher:encrypt(string.rep("0", 32), string.rep("0", "12"), "secret", false, "aad")
  28. cipher:get_param("tag", 16) == cipher:get_aead_tag()

Back to TOC

gettable_params

syntax: schema, err = x:gettable_params(raw?)

Returns the readable schema as a Lua table for all gettable params.
When raw is set to true, the function returns the raw schema instead.

Back to TOC

settable_params

syntax: schema, err = x:settable_params(raw?)

Returns the readable schema as a Lua table for all settable params.
When raw is set to true, the function returns the raw schema instead.

  1. local c = require("resty.openssl.cipher").new("aes-256-gcm")
  2. print(cjson.encode(c:settable_params()))
  3. -- outputs [["ivlen","unsigned integer (max 8 bytes large)"],["tag","octet string (arbitrary size)"],["tlsaad","octet string (arbitrary size)"],["tlsivfixed","octet string (arbitrary size)"],["tlsivinv","octet string (arbitrary size)"]]
  4. print(cjson.encode(c:gettable_params()))
  5. -- outputs [["keylen","unsigned integer (max 8 bytes large)"],["ivlen","unsigned integer (max 8 bytes large)"],["taglen","unsigned integer (max 8 bytes large)"],["iv","octet string (arbitrary size)"],["updated-iv","octet string (arbitrary size)"],["tag","octet string (arbitrary size)"],["tlsaadpad","unsigned integer (max 8 bytes large)"],["tlsivgen","octet string (arbitrary size)"]]

Back to TOC

get_param

syntax: value, err = x:get_param(key, want_size?, want_type?)

Read the param key and return its value. The return value is a Lua number
or a string.
Certain params requires exact size to be set, in such case,
want_size should be specified; if want_size is not specified and, the
library use a buffer of 100 bytes to hold the return value.
Certain params returns a special type, user should explictly set want_type
as a string to correctly decode them. Currently want_type can only be
"bn" or unset. Note it may also be necessary to increase temporary buffer
size want_size when want_type is "bn".

  1. local c = require("resty.openssl.cipher").new("aes-256-gcm")
  2. print(c:get_param("taglen"))
  3. -- outputs 16
  4. print(c:get_param("tag"))
  5. -- returns error, tag must have a explict size
  6. print(c:get_param("tag", 16))
  7. -- outputs the tag
  8. local p = require("resty.openssl.pkey").new())
  9. print(p:get_param("d"):to_hex())
  10. -- returns error, d (private exponent) is a BIGNUM
  11. print(p:get_param("d", 256, "bn"):to_hex())
  12. -- returns d as resty.openssl.bn instance

Back to TOC

set_params

syntax: ok, err = x:set_params(params)

Set params passed in with Lua table params. The library does limited type check, user
is responsible for validity of input.

  1. local k = require("resty.openssl.kdf").new("HKDF")
  2. k:set_params({
  3. digest = "md5",
  4. salt = "salt",
  5. info = "some info",
  6. mode = kdf.HKDEF_MODE_EXPAND_ONLY,
  7. -- as HKDF also accepts mode as string, use the literal below also works
  8. -- mode = "EXPAND_ONLY"
  9. }))

Back to TOC

Code generation

Lots of functions and tests for X509, CSR and CRL are generated from templates under
scripts
directory. Those functions and tests are either commented with AUTO GENERATED or AUTOGEN.

When making changes to them, please update the template under scripts/templates instead. Then
regenerate them again.

  1. cd scripts
  2. pip3 install -r requirements.txt
  3. python3 ./x509_autogen.py

Back to TOC

Compatibility

Although only a small combinations of CPU arch and OpenSSL version are tested, the library
should function well as long as the linked OpenSSL library is API compatible. This means
the same name of functions are exported with same argument types.

For OpenSSL 1.0.2 series however, binary/ABI compatibility must be ensured as some struct members
are accessed directly. They are accessed by memory offset in assembly.

OpenSSL keeps ABI/binary compatibility
with minor releases or letter releases. So all structs offsets and macro constants are kept
same.

If you plan to use this library on an untested version of OpenSSL (like custom builds or pre releases),
this can be a good source to consult.

Back to TOC

Credits

This project receives contribution from following developers:

Back to TOC

Copyright and License

This module is licensed under the BSD license.

Copyright (C) 2019-2023, by fffonion fffonion@gmail.com.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Back to TOC

See Also

Back to TOC