Skip to content

Commit a120ed9

Browse files
authored
feature: support tcpsock:settrustedstore() for stream subsystem.
This extends the settrustedstore() cosocket method (already available in the http subsystem) to the stream subsystem, allowing Lua code to supply a per-handshake X509_STORE that overrides lua_ssl_trusted_certificate for the upcoming sslhandshake(). This requires the corresponding ngx_stream_lua_ffi_socket_tcp_settrustedstore() FFI entry point in stream-lua-nginx-module. Signed-off-by: Walker Zhao <walker.zhao@konghq.com>
1 parent e8a4af5 commit a120ed9

2 files changed

Lines changed: 429 additions & 42 deletions

File tree

lib/resty/core/socket.lua

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ local ngx_lua_ffi_socket_tcp_setoption
4242
local ngx_lua_ffi_socket_getfd
4343
local ngx_lua_ffi_socket_getsslpointer
4444
local ngx_lua_ffi_socket_getsslctx
45+
local ngx_lua_ffi_socket_tcp_settrustedstore
4546

4647
if subsystem == 'http' then
4748
ffi.cdef[[
@@ -103,6 +104,13 @@ if pcall(function() return C.ngx_http_lua_ffi_socket_tcp_get_ssl_ctx end) then
103104
ngx_lua_ffi_socket_getsslctx = C.ngx_http_lua_ffi_socket_tcp_get_ssl_ctx
104105
end
105106

107+
if pcall(function()
108+
return C.ngx_http_lua_ffi_socket_tcp_settrustedstore
109+
end) then
110+
ngx_lua_ffi_socket_tcp_settrustedstore =
111+
C.ngx_http_lua_ffi_socket_tcp_settrustedstore
112+
end
113+
106114

107115
elseif subsystem == 'stream' then
108116

@@ -128,6 +136,10 @@ int
128136
ngx_stream_lua_ffi_socket_tcp_get_ssl_ctx(ngx_stream_lua_request_t *r,
129137
ngx_stream_lua_socket_tcp_upstream_t *u, void **pctx,
130138
char **errmsg);
139+
140+
int
141+
ngx_stream_lua_ffi_socket_tcp_settrustedstore(ngx_stream_lua_request_t *r,
142+
ngx_stream_lua_socket_tcp_upstream_t *u, void *store, char **errmsg);
131143
]]
132144

133145
ngx_lua_ffi_socket_tcp_getoption = C.ngx_stream_lua_ffi_socket_tcp_getoption
@@ -142,6 +154,13 @@ if pcall(function() return C.ngx_stream_lua_ffi_socket_tcp_get_ssl_pointer end)
142154
then
143155
ngx_lua_ffi_socket_getsslctx = C.ngx_stream_lua_ffi_socket_tcp_get_ssl_pointer
144156
end
157+
158+
if pcall(function()
159+
return C.ngx_stream_lua_ffi_socket_tcp_settrustedstore
160+
end) then
161+
ngx_lua_ffi_socket_tcp_settrustedstore =
162+
C.ngx_stream_lua_ffi_socket_tcp_settrustedstore
163+
end
145164
end
146165

147166

@@ -298,6 +317,38 @@ local function getsslctx(cosocket)
298317
end
299318

300319

320+
local NULL_STORE = ffi_new("void *", nil)
321+
322+
323+
local function settrustedstore(cosocket, store)
324+
if not ngx_lua_ffi_socket_tcp_settrustedstore then
325+
return nil, "tcpsock:settrustedstore is not supported by "
326+
.. "the current nginx module"
327+
end
328+
329+
if store ~= nil and type(store) ~= "cdata" then
330+
return nil, "bad store arg: cdata expected, got " .. type(store)
331+
end
332+
333+
local r = get_request()
334+
if not r then
335+
error("no request found", 2)
336+
end
337+
338+
local u = get_tcp_socket(cosocket)
339+
340+
local rc = ngx_lua_ffi_socket_tcp_settrustedstore(r, u,
341+
store or NULL_STORE,
342+
errmsg)
343+
if rc ~= FFI_OK then
344+
return nil, ffi_str(errmsg[0])
345+
end
346+
347+
cosocket[SOCKET_TRUSTED_STORE_INDEX] = store
348+
349+
return true
350+
end
351+
301352

302353
if subsystem == 'http' then
303354
local server_name_str = ffi_new("ngx_str_t[1]")
@@ -332,48 +383,6 @@ local function setclientcert(cosocket, cert, pkey)
332383
end
333384

334385

335-
local ngx_lua_ffi_socket_tcp_settrustedstore
336-
if pcall(function()
337-
return C.ngx_http_lua_ffi_socket_tcp_settrustedstore
338-
end) then
339-
ngx_lua_ffi_socket_tcp_settrustedstore =
340-
C.ngx_http_lua_ffi_socket_tcp_settrustedstore
341-
end
342-
343-
344-
local NULL_STORE = ffi_new("void *", nil)
345-
346-
347-
local function settrustedstore(cosocket, store)
348-
if not ngx_lua_ffi_socket_tcp_settrustedstore then
349-
return nil, "tcpsock:settrustedstore is not supported by " ..
350-
"the current lua-nginx-module"
351-
end
352-
353-
if store ~= nil and type(store) ~= "cdata" then
354-
return nil, "bad store arg: cdata expected, got " .. type(store)
355-
end
356-
357-
local r = get_request()
358-
if not r then
359-
error("no request found", 2)
360-
end
361-
362-
local u = get_tcp_socket(cosocket)
363-
364-
local rc = ngx_lua_ffi_socket_tcp_settrustedstore(r, u,
365-
store or NULL_STORE,
366-
errmsg)
367-
if rc ~= FFI_OK then
368-
return nil, ffi_str(errmsg[0])
369-
end
370-
371-
cosocket[SOCKET_TRUSTED_STORE_INDEX] = store
372-
373-
return true
374-
end
375-
376-
377386
local function sslhandshake(cosocket, reused_session, server_name, ssl_verify,
378387
send_status_req, ...)
379388

@@ -520,6 +529,9 @@ do
520529
method_table.getfd = getfd
521530
method_table.getsslpointer = getsslpointer
522531
method_table.getsslctx = getsslctx
532+
if ngx_lua_ffi_socket_tcp_settrustedstore then
533+
method_table.settrustedstore = settrustedstore
534+
end
523535

524536
method_table = registry.__tcp_raw_req_cosocket_mt
525537
method_table.getfd = getfd

0 commit comments

Comments
 (0)