aboutsummaryrefslogtreecommitdiffstats
path: root/server/resty/openssl/ctx.lua
diff options
context:
space:
mode:
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>2023-04-14 11:59:32 +0000
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>2023-04-14 11:59:32 +0000
commitd68841d9f75636575cd778838a8ceea5fd5aada3 (patch)
tree778c84203ed9bfa4dc1c8234e4e2cf60da6ebd8c /server/resty/openssl/ctx.lua
parent42af09588f1f839b9ab36356f02f34c89559bcfa (diff)
Upload ui
Issue-ID: PORTAL-1084 Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de> Change-Id: Id0c94859a775094e67b0bb9c91ca5e776a08c068
Diffstat (limited to 'server/resty/openssl/ctx.lua')
-rw-r--r--server/resty/openssl/ctx.lua78
1 files changed, 78 insertions, 0 deletions
diff --git a/server/resty/openssl/ctx.lua b/server/resty/openssl/ctx.lua
new file mode 100644
index 0000000..eaec396
--- /dev/null
+++ b/server/resty/openssl/ctx.lua
@@ -0,0 +1,78 @@
+local ffi = require "ffi"
+local C = ffi.C
+local ffi_gc = ffi.gc
+
+require "resty.openssl.include.ossl_typ"
+local format_error = require("resty.openssl.err").format_error
+local OPENSSL_3X = require("resty.openssl.version").OPENSSL_3X
+
+ffi.cdef [[
+ OSSL_LIB_CTX *OSSL_LIB_CTX_new(void);
+ int OSSL_LIB_CTX_load_config(OSSL_LIB_CTX *ctx, const char *config_file);
+ void OSSL_LIB_CTX_free(OSSL_LIB_CTX *ctx);
+]]
+
+local ossl_lib_ctx
+
+local function new(request_context_only, conf_file)
+ if not OPENSSL_3X then
+ return false, "ctx is only supported from OpenSSL 3.0"
+ end
+
+ local ctx = C.OSSL_LIB_CTX_new()
+ ffi_gc(ctx, C.OSSL_LIB_CTX_free)
+
+ if conf_file and C.OSSL_LIB_CTX_load_config(ctx, conf_file) ~= 1 then
+ return false, format_error("ctx.new")
+ end
+
+ if request_context_only then
+ ngx.ctx.ossl_lib_ctx = ctx
+ else
+ ossl_lib_ctx = ctx
+ end
+
+ return true
+end
+
+local function free(request_context_only)
+ if not OPENSSL_3X then
+ return false, "ctx is only supported from OpenSSL 3.0"
+ end
+
+ if request_context_only then
+ ngx.ctx.ossl_lib_ctx = nil
+ else
+ ossl_lib_ctx = nil
+ end
+
+ return true
+end
+
+local test_request
+
+do
+
+ local ok, exdata = pcall(require, "thread.exdata")
+ if ok and exdata then
+ test_request = function()
+ local r = exdata()
+ if r ~= nil then
+ return not not r
+ end
+ end
+
+ else
+ local getfenv = getfenv
+
+ function test_request()
+ return not not getfenv(0).__ngx_req
+ end
+ end
+end
+
+return {
+ new = new,
+ free = free,
+ get_libctx = function() return test_request() and ngx.ctx.ossl_lib_ctx or ossl_lib_ctx end,
+} \ No newline at end of file