Skip to content

Commit 3a20227

Browse files
feature: add sock:getsslpointer() and sock:getsslctx().
1 parent 77621ff commit 3a20227

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

lib/resty/core/socket.lua

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ local option_index = {
4040
local ngx_lua_ffi_socket_tcp_getoption
4141
local ngx_lua_ffi_socket_tcp_setoption
4242
local ngx_lua_ffi_socket_getfd
43+
local ngx_lua_ffi_socket_getsslpointer
44+
local ngx_lua_ffi_socket_getsslctx
4345

4446
if subsystem == 'http' then
4547
ffi.cdef[[
@@ -75,11 +77,21 @@ int
7577
ngx_http_lua_socket_tcp_get_ssl_session(ngx_http_request_t *r,
7678
ngx_http_lua_socket_tcp_upstream_t *u, void **sess,
7779
char **errmsg);
80+
int
81+
ngx_http_lua_ffi_socket_tcp_get_ssl_pointer(ngx_http_request_t *r,
82+
ngx_http_lua_socket_tcp_upstream_t *u, void **pssl,
83+
char **errmsg);
84+
int
85+
ngx_http_lua_ffi_socket_tcp_get_ssl_ctx(ngx_http_request_t *r,
86+
ngx_http_lua_socket_tcp_upstream_t *u, void **pctx,
87+
char **errmsg);
7888
]]
7989

8090
ngx_lua_ffi_socket_tcp_getoption = C.ngx_http_lua_ffi_socket_tcp_getoption
8191
ngx_lua_ffi_socket_tcp_setoption = C.ngx_http_lua_ffi_socket_tcp_setoption
8292
ngx_lua_ffi_socket_getfd = C.ngx_http_lua_ffi_socket_tcp_getfd
93+
ngx_lua_ffi_socket_getsslpointer = C.ngx_http_lua_ffi_socket_tcp_get_ssl_pointer
94+
ngx_lua_ffi_socket_getsslctx = C.ngx_http_lua_ffi_socket_tcp_get_ssl_ctx
8395

8496
elseif subsystem == 'stream' then
8597

@@ -97,11 +109,21 @@ int
97109
ngx_stream_lua_ffi_socket_tcp_getfd(ngx_stream_lua_request_t *r,
98110
ngx_stream_lua_socket_tcp_upstream_t *u,
99111
char **errmsg);
112+
int
113+
ngx_stream_lua_ffi_socket_tcp_get_ssl_pointer(ngx_stream_lua_request_t *r,
114+
ngx_stream_lua_socket_tcp_upstream_t *u, void **pssl,
115+
char **errmsg);
116+
int
117+
ngx_stream_lua_ffi_socket_tcp_get_ssl_ctx(ngx_stream_lua_request_t *r,
118+
ngx_stream_lua_socket_tcp_upstream_t *u, void **pctx,
119+
char **errmsg);
100120
]]
101121

102122
ngx_lua_ffi_socket_tcp_getoption = C.ngx_stream_lua_ffi_socket_tcp_getoption
103123
ngx_lua_ffi_socket_tcp_setoption = C.ngx_stream_lua_ffi_socket_tcp_setoption
104124
ngx_lua_ffi_socket_getfd = C.ngx_stream_lua_ffi_socket_tcp_getfd
125+
ngx_lua_ffi_socket_getsslpointer = C.ngx_stream_lua_ffi_socket_tcp_get_ssl_pointer
126+
ngx_lua_ffi_socket_getsslctx = C.ngx_stream_lua_ffi_socket_tcp_get_ssl_ctx
105127
end
106128

107129

@@ -215,6 +237,49 @@ local function getfd(cosocket)
215237
end
216238

217239

240+
local function getsslpointer(cosocket)
241+
if not cosocket then
242+
error("ngx.socket getfd: expecting the cosocket object, but seen none")
243+
end
244+
245+
local r = get_request()
246+
if not r then
247+
error("no request found")
248+
end
249+
250+
local u = get_tcp_socket(cosocket)
251+
local rc = ngx_lua_ffi_socket_getsslpointer(r, u,
252+
session_ptr, errmsg)
253+
if rc == FFI_ERROR then
254+
return nil, ffi_str(errmsg[0])
255+
end
256+
257+
return session_ptr[0]
258+
end
259+
260+
261+
local function getsslctx(cosocket)
262+
if not cosocket then
263+
error("ngx.socket getfd: expecting the cosocket object, but seen none")
264+
end
265+
266+
local r = get_request()
267+
if not r then
268+
error("no request found")
269+
end
270+
271+
local u = get_tcp_socket(cosocket)
272+
local rc = ngx_lua_ffi_socket_getsslctx(r, u,
273+
session_ptr, errmsg)
274+
if rc == FFI_ERROR then
275+
return nil, ffi_str(errmsg[0])
276+
end
277+
278+
return session_ptr[0]
279+
end
280+
281+
282+
218283
if subsystem == 'http' then
219284
local server_name_str = ffi_new("ngx_str_t[1]")
220285
local openssl_error_code = ffi_new("int[1]")
@@ -358,6 +423,7 @@ local function getsslsession(cosocket)
358423
return ffi_gc(session_ptr[0], C.ngx_http_lua_ffi_ssl_free_session)
359424
end
360425

426+
361427
do
362428
local method_table = registry.__tcp_cosocket_mt
363429
method_table.getoption = getoption
@@ -368,6 +434,8 @@ do
368434
method_table.getoption = getoption
369435
method_table.setoption = setoption
370436
method_table.getsslsession = getsslsession
437+
method_table.getsslpointer = getsslpointer
438+
method_table.getsslctx = getsslctx
371439

372440
method_table = registry.__tcp_req_cosocket_mt
373441
method_table.getfd = getfd
@@ -386,6 +454,8 @@ do
386454
method_table.getoption = getoption
387455
method_table.setoption = setoption
388456
method_table.getfd = getfd
457+
method_table.getsslpointer = getsslpointer
458+
method_table.getsslctx = getsslctx
389459

390460
method_table = registry.__tcp_raw_req_cosocket_mt
391461
method_table.getfd = getfd

t/ssl.t

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3545,3 +3545,100 @@ qr/upstream ssl state: (false|true)/
35453545
[error]
35463546
[emerg]
35473547
[crit]
3548+
3549+
3550+
3551+
=== TEST 37: get cosocket SSL pointer
3552+
--- http_config
3553+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
3554+
3555+
server {
3556+
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
3557+
server_name test.com;
3558+
ssl_certificate ../../cert/test.crt;
3559+
ssl_certificate_key ../../cert/test.key;
3560+
3561+
server_tokens off;
3562+
location /foo {
3563+
default_type 'text/plain';
3564+
content_by_lua_block {ngx.status = 201 ngx.say("foo") ngx.exit(201)}
3565+
more_clear_headers Date;
3566+
}
3567+
}
3568+
--- config
3569+
server_tokens off;
3570+
lua_ssl_trusted_certificate ../../cert/test.crt;
3571+
3572+
location /t {
3573+
content_by_lua_block {
3574+
do
3575+
local sock = ngx.socket.tcp()
3576+
3577+
sock:settimeout(3000)
3578+
3579+
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
3580+
if not ok then
3581+
ngx.say("failed to connect: ", err)
3582+
return
3583+
end
3584+
3585+
ngx.say("connected: ", ok)
3586+
3587+
local sess, err = sock:sslhandshake(nil, "test.com", true)
3588+
if not sess then
3589+
ngx.say("failed to do SSL handshake: ", err)
3590+
return
3591+
end
3592+
3593+
ngx.say("ssl handshake: ", type(sess))
3594+
ngx.say("ssl pointer: ", type(sock:getsslpointer()))
3595+
ngx.say("ssl ctx: ", type(sock:getsslctx()))
3596+
3597+
local req = "GET /foo HTTP/1.0\r\nHost: test.com\r\nConnection: close\r\n\r\n"
3598+
local bytes, err = sock:send(req)
3599+
if not bytes then
3600+
ngx.say("failed to send http request: ", err)
3601+
return
3602+
end
3603+
3604+
ngx.say("sent http request: ", bytes, " bytes.")
3605+
3606+
while true do
3607+
local line, err = sock:receive()
3608+
if not line then
3609+
-- ngx.say("failed to receive response status line: ", err)
3610+
break
3611+
end
3612+
3613+
ngx.say("received: ", line)
3614+
end
3615+
3616+
local ok, err = sock:close()
3617+
ngx.say("close: ", ok, " ", err)
3618+
end -- do
3619+
-- collectgarbage()
3620+
}
3621+
}
3622+
3623+
--- request
3624+
GET /t
3625+
--- response_body
3626+
connected: 1
3627+
ssl handshake: cdata
3628+
ssl pointer: cdata
3629+
ssl ctx: cdata
3630+
sent http request: 56 bytes.
3631+
received: HTTP/1.1 201 Created
3632+
received: Server: nginx
3633+
received: Content-Type: text/plain
3634+
received: Content-Length: 4
3635+
received: Connection: close
3636+
received:
3637+
received: foo
3638+
close: 1 nil
3639+
--- error_log
3640+
lua ssl server name: "test.com"
3641+
--- no_error_log
3642+
[error]
3643+
[emerg]
3644+
[crit]

t/stream/ssl.t

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,3 +2552,78 @@ qr/session reused: false/
25522552
[alert]
25532553
[crit]
25542554
[error]
2555+
2556+
2557+
2558+
=== TEST 32: get cosocet SSL pointer
2559+
--- stream_config
2560+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
2561+
2562+
server {
2563+
listen 127.0.0.1:$TEST_NGINX_RAND_PORT_1 ssl;
2564+
ssl_protocols TLSv1.2;
2565+
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384;
2566+
ssl_certificate ../../cert/test.crt;
2567+
ssl_certificate_key ../../cert/test.key;
2568+
2569+
return 'it works!\n';
2570+
}
2571+
--- stream_server_config
2572+
lua_ssl_trusted_certificate ../../cert/test.crt;
2573+
lua_ssl_protocols TLSv1.2;
2574+
lua_ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256;
2575+
2576+
content_by_lua_block {
2577+
do
2578+
local sock = ngx.socket.tcp()
2579+
2580+
sock:settimeout(3000)
2581+
2582+
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_RAND_PORT_1)
2583+
if not ok then
2584+
ngx.say("failed to connect: ", err)
2585+
return
2586+
end
2587+
2588+
ngx.say("connected: ", ok)
2589+
2590+
local sess, err = sock:sslhandshake(nil, nil, true)
2591+
if not sess then
2592+
ngx.say("failed to do SSL handshake: ", err)
2593+
return
2594+
end
2595+
2596+
ngx.say("ssl session: ", type(sess))
2597+
ngx.say("ssl pointer: ", type(sock:getsslpointer()))
2598+
ngx.say("ssl ctx: ", type(sock:getsslctx()))
2599+
ngx.log(ngx.INFO, "ssl pointer: ", tostring(sock:getsslpointer()))
2600+
2601+
while true do
2602+
local line, err = sock:receive()
2603+
if not line then
2604+
-- ngx.say("failed to receive response status line: ", err)
2605+
break
2606+
end
2607+
2608+
ngx.say("received: ", line)
2609+
end
2610+
2611+
local ok, err = sock:close()
2612+
ngx.say("close: ", ok, " ", err)
2613+
end -- do
2614+
-- collectgarbage()
2615+
}
2616+
2617+
--- stream_response
2618+
connected: 1
2619+
ssl session: userdata
2620+
ssl pointer: cdata
2621+
ssl ctx: cdata
2622+
received: it works!
2623+
close: 1 nil
2624+
--- error_log eval
2625+
qr/ssl pointer: cdata<void \*>: 0x[0-9a-f]+,/
2626+
--- no_error_log
2627+
[alert]
2628+
[crit]
2629+
[error]

0 commit comments

Comments
 (0)