Skip to content

Commit beb8255

Browse files
authored
feature: support custom trusted CA store for cosocket TLS handshake. (#401)
Adds the cosocket-level plumbing for a new tcpsock:settrustedstore(store) method, allowing Lua code to supply a per-handshake X509_STORE that overrides lua_ssl_trusted_certificate for the upcoming sslhandshake(). This is needed for use cases where the set of trusted CAs is determined at request time (e.g. per-tenant mTLS upstreams). This is the stream-module counterpart of the same feature merged into lua-nginx-module. The lua-resty-core binding will be added separately. NULL store is allowed to clear a previously set trusted store on the cosocket object. Signed-off-by: Walker Zhao <walker.zhao@konghq.com>
1 parent e50520b commit beb8255

3 files changed

Lines changed: 617 additions & 0 deletions

File tree

src/ngx_stream_lua_socket_tcp.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,21 @@ ngx_stream_lua_socket_tcp_sslhandshake(lua_State *L)
20662066
#endif
20672067
#endif
20682068

2069+
if (u->ssl_trusted_store) {
2070+
if (SSL_set1_verify_cert_store(c->ssl->connection,
2071+
u->ssl_trusted_store)
2072+
== 0)
2073+
{
2074+
ERR_clear_error();
2075+
2076+
lua_pushnil(L);
2077+
lua_pushliteral(L, "SSL_set1_verify_cert_store() failed");
2078+
return 2;
2079+
}
2080+
2081+
u->ssl_trusted_store = NULL;
2082+
}
2083+
20692084
rc = ngx_ssl_handshake(c);
20702085

20712086
dd("ngx_ssl_handshake returned %d", (int) rc);
@@ -2505,6 +2520,31 @@ ngx_stream_lua_server_ssl_handshake_retval_handler(ngx_stream_lua_request_t *r,
25052520
return 1;
25062521
}
25072522

2523+
2524+
int
2525+
ngx_stream_lua_ffi_socket_tcp_settrustedstore(ngx_stream_lua_request_t *r,
2526+
ngx_stream_lua_socket_tcp_upstream_t *u, void *store, char **errmsg)
2527+
{
2528+
if (u == NULL
2529+
|| u->peer.connection == NULL
2530+
|| u->read_closed
2531+
|| u->write_closed)
2532+
{
2533+
*errmsg = "closed";
2534+
return NGX_ERROR;
2535+
}
2536+
2537+
if (u->request != r) {
2538+
*errmsg = "bad request";
2539+
return NGX_ERROR;
2540+
}
2541+
2542+
u->ssl_trusted_store = store;
2543+
2544+
return NGX_OK;
2545+
}
2546+
2547+
25082548
#endif /* NGX_STREAM_SSL */
25092549

25102550

src/ngx_stream_lua_socket_tcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ struct ngx_stream_lua_socket_tcp_upstream_s {
138138
char host[COSOCKET_HOST_LEN];
139139
#if (NGX_STREAM_SSL)
140140
ngx_str_t ssl_name;
141+
X509_STORE *ssl_trusted_store;
141142
#endif
142143

143144
unsigned ft_type:16;

0 commit comments

Comments
 (0)