Skip to content

feature: add ngx.ssl.get_req_ssl_ctx(). - #548

Merged
zhuizhuhaomeng merged 2 commits into
openresty:masterfrom
zhuizhuhaomeng:feature/request-ssl-ctx-api
Nov 8, 2026
Merged

zhuizhuhaomeng merged 2 commits into
openresty:masterfrom
zhuizhuhaomeng:feature/request-ssl-ctx-api

Conversation

@zhuizhuhaomeng

@zhuizhuhaomeng zhuizhuhaomeng commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Add ngx.ssl.get_req_ssl_ctx() for HTTP and stream, returning the borrowed downstream session_ctx pointer as void * cdata. Lua libraries can use this API to obtain an SSL_CTX* without an auxiliary module or direct reads of Nginx structure layouts.

The getter preserves Nginx's original session context when SNI changes the active context returned by SSL_get_SSL_CTX(). It raises no request found without a request and returns nil, err for unavailable SSL state. The Lua API binds directly to the C export and requires the corresponding module update.

C exports are provided by lua-nginx-module #2546 and stream-lua-nginx-module #420. The consumer change is in lua-resty-openssl #243.

Validation with the combined Nginx 1.31.8 / OpenSSL 3.5.9 build:

  • t/ssl-ctx.t and t/stream/ssl-ctx.t: 48 assertions passed, covering errors, certificate/content phases, repeated reads, and SNI.
  • Existing t/ssl.t and t/stream/ssl.t: 856 assertions passed.
  • lua-resty-openssl SSL suite against the updated core: 176 assertions passed; real HTTP and stream TLS probes confirm ssl_ctx.from_request() works with legacy loading forbidden.
  • Luacheck and git diff --check pass.

Expose the downstream session SSL_CTX pointer through the HTTP and stream
FFI exports. Preserve Nginx session_ctx semantics after SNI selection and
return a borrowed void pointer with the existing ngx.ssl error convention.

Only export the Lua getter when its C symbol is available so older
binaries can still load ngx.ssl and callers can select a fallback.
Document ownership and availability, and cover request errors, TLS
phases, repeated reads, SNI, and missing-symbol compatibility.

Validation: 60 new and 856 existing SSL assertions pass on Nginx 1.31.8
with OpenSSL 3.5.9. A real older binary without the symbol still loads
ngx.ssl. Luacheck and git diff --check pass.
Require the HTTP and stream C getters when loading ngx.ssl and export
the Lua getter unconditionally. Remove the missing-symbol compatibility
tests and document the corresponding module requirement.

Validation: the request SSL_CTX and existing SSL suites pass for HTTP
and stream, along with the lua-resty-openssl SSL suite. Luacheck and
git diff --check pass.
@zhuizhuhaomeng
zhuizhuhaomeng merged commit 70f9925 into openresty:master Nov 8, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant