Skip to content

feature: add tcpsock:settrustedstore() for per-handshake trusted CAs#525

Merged
zhuizhuhaomeng merged 2 commits intoopenresty:masterfrom
findns94:feat/support-trusted-ca
May 2, 2026
Merged

feature: add tcpsock:settrustedstore() for per-handshake trusted CAs#525
zhuizhuhaomeng merged 2 commits intoopenresty:masterfrom
findns94:feat/support-trusted-ca

Conversation

@findns94
Copy link
Copy Markdown
Contributor

@findns94 findns94 commented Apr 29, 2026

Adds a Lua wrapper around the new
ngx_http_lua_ffi_socket_tcp_settrustedstore FFI in lua-nginx-module, exposed as tcpsock:settrustedstore(store). The store is an X509_STORE * cdata (e.g. from lua-resty-openssl) that overrides lua_ssl_trusted_certificate for the next sslhandshake() on this cosocket. The C side consumes the slot during the handshake, so the override does not leak across handshakes; passing nil clears it on both the lua and C sides so a previously-set store cannot dangle past a GC of the user's last reference.

This is needed for per-request mTLS upstreams where the trusted CA set is determined dynamically (per-tenant routing, dynamic CA discovery) and cannot be expressed via the static
lua_ssl_trusted_certificate directive.

The FFI symbol is looked up softly so loading lua-resty-core against an older lua-nginx-module that lacks the new symbol still works; the method is simply not attached to the cosocket metatable in that case. Only the http subsystem is wired up, matching the FFI surface in ngx_http_lua_module. Stream cosockets are unchanged.

Requires lua-nginx-module change openresty/lua-nginx-module#2495.

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-resty-core project.

Comment thread lib/resty/core/socket.lua Outdated
local SOCKET_CTX_INDEX = 1
local SOCKET_CLIENT_CERT_INDEX = 6
local SOCKET_CLIENT_PKEY_INDEX = 7
local SOCKET_TRUSTED_STORE_INDEX = 8
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SOCKET_BIND_INDEX = 8 /* only in upstream cosocket */

So you should use 10 for SOCKET_TRUSTED_STORE_INDEX

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I used local SOCKET_TRUSTED_STORE_INDEX = 10 for now.

@findns94 findns94 force-pushed the feat/support-trusted-ca branch from 875efd1 to b71f0c5 Compare April 30, 2026 03:21
Adds a Lua wrapper around the new
ngx_http_lua_ffi_socket_tcp_settrustedstore FFI in lua-nginx-module,
exposed as tcpsock:settrustedstore(store). The store is an
X509_STORE * cdata (e.g. from lua-resty-openssl) that overrides
lua_ssl_trusted_certificate for the next sslhandshake() on this
cosocket. The C side consumes the slot during the handshake, so the
override does not leak across handshakes; passing nil clears it on
both the lua and C sides so a previously-set store cannot dangle
past a GC of the user's last reference.

This is needed for per-request mTLS upstreams where the trusted CA
set is determined dynamically (per-tenant routing, dynamic CA
discovery) and cannot be expressed via the static
lua_ssl_trusted_certificate directive.

The FFI symbol is looked up softly so loading lua-resty-core against
an older lua-nginx-module that lacks the new symbol still works;
the method is simply not attached to the cosocket metatable in that
case. Only the http subsystem is wired up, matching the FFI surface
in ngx_http_lua_module. Stream cosockets are unchanged.

Requires lua-nginx-module change "feature: support custom trusted
CA store for cosocket TLS handshake".

Signed-off-by: Walker Zhao <walker.zhao@konghq.com>
@findns94 findns94 force-pushed the feat/support-trusted-ca branch from b71f0c5 to ad5ac4e Compare April 30, 2026 03:42
@zhuizhuhaomeng zhuizhuhaomeng force-pushed the feat/support-trusted-ca branch from 67a6cf8 to 1a92487 Compare May 2, 2026 14:26
@zhuizhuhaomeng zhuizhuhaomeng merged commit a103a6f into openresty:master May 2, 2026
3 of 4 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.

2 participants