Skip to content

feature: add request SSL_CTX getter to the FFI API. - #2546

Merged
zhuizhuhaomeng merged 1 commit into
openresty:masterfrom
zhuizhuhaomeng:feature/request-ssl-ctx-ffi
Nov 8, 2026
Merged

zhuizhuhaomeng merged 1 commit into
openresty:masterfrom
zhuizhuhaomeng:feature/request-ssl-ctx-ffi

Conversation

@zhuizhuhaomeng

Copy link
Copy Markdown
Contributor

Export ngx_http_lua_ffi_get_req_ssl_ctx(r, err) to let Lua libraries obtain the downstream request's SSL_CTX* without lua-resty-openssl-aux-module or direct FFI access to Nginx structure layouts.

The getter returns Nginx's borrowed r->connection->ssl->session_ctx. It deliberately preserves this context when SNI switches the active context returned by SSL_get_SSL_CTX(). Missing request/connection/SSL state returns NULL with bad request; a missing session context returns NULL with bad ssl context. The getter does not allocate, free, or change ownership of the context.

Tests cover NULL requests, plain connections, valid TLS access during certificate and content phases, repeated reads, and SNI selecting a different active context. The SNI case asserts distinct verify-depth values for the original and active contexts.

Validation on the local HTTP + stream build with Nginx 1.31.8 and OpenSSL 3.5.9:

  • t/198-request-ssl-ctx.t: 24 assertions passed.
  • t/140-ssl-c-api.t: 300 assertions passed.
  • C probes passed for NULL requests, missing connections, missing SSL state, missing session contexts, and valid borrowed contexts.
  • Both new FFI symbols are visible in the executable's dynamic symbol table.
  • git diff --check passed.

This PR adds the C FFI export; a lua-resty-core wrapper can consume it separately.

Export ngx_http_lua_ffi_get_req_ssl_ctx so Lua libraries can access
Nginx's downstream session context without an auxiliary C module or
version-dependent FFI structure layouts. Return the borrowed session_ctx,
which can differ from SSL_get_SSL_CTX after SNI selection.

Return explicit errors for missing request or SSL context state. Cover
NULL and plain requests, certificate and content phases, repeated reads,
and SNI selection with distinct default and active context settings.

Validation: Nginx 1.31.8 / OpenSSL 3.5.9 build, 24 new assertions,
300 existing SSL C API assertions, and invalid-state C probes pass.
@zhuizhuhaomeng
zhuizhuhaomeng merged commit b6585f5 into openresty:master Nov 8, 2026
4 of 6 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