Skip to content

Commit 63ae019

Browse files
author
mengxiangwei
committed
feat: add server random and master key fetch api
1 parent 8dc57c6 commit 63ae019

5 files changed

Lines changed: 67 additions & 47 deletions

File tree

‎src/api/ngx_http_lua_api.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/* Public API for other Nginx modules */
2020

2121

22-
#define ngx_http_lua_version 10029
22+
#define ngx_http_lua_version 10030
2323
#define NGX_HTTP_LUA_EXPORT_CO_CTX_CLEANUP 1
2424

2525

‎src/ngx_http_lua_initworkerby.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
115115

116116
ngx_memcpy(fake_cycle, cycle, sizeof(ngx_cycle_t));
117117

118+
/*
119+
* nginx clears cycle->old_cycle after ngx_init_cycle() completes.
120+
* Since nginx 1.29.2, ngx_ssl_cache_fetch() accesses old_cycle->conf_ctx
121+
* without a NULL guard, so we must ensure old_cycle is non-NULL.
122+
* This avoids a NULL dereference when merge_loc_conf triggers
123+
* ngx_ssl_trusted_certificate.
124+
* Pointing to the current cycle is safe: the SSL cache is shared via
125+
* conf_ctx, so cert lookups still find previously loaded entries.
126+
*/
127+
if (fake_cycle->old_cycle == NULL) {
128+
fake_cycle->old_cycle = cycle;
129+
}
130+
118131
ngx_queue_init(&fake_cycle->reusable_connections_queue);
119132

120133
if (ngx_array_init(&fake_cycle->listening, cycle->pool,

‎t/138-balancer.t‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ hello world: HTTP/2.0
698698
--- no_error_log
699699
[error]
700700
[crit]
701+
--- skip_nginx: 4: < 1.29.4
701702
702703
703704
@@ -736,3 +737,4 @@ qr/502 Bad Gateway/
736737
[error]
737738
--- error_log eval
738739
qr/SSL_do_handshake.*(?:no application protocol|NO_APPLICATION_PROTOCOL).*SSL alert number 120\) while SSL handshaking to upstream/
740+
--- skip_nginx: 4: < 1.29.4

‎t/140-ssl-c-api.t‎

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,61 +2089,66 @@ TLSv1.2, cipher: "ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 Kx=ECDH Au=RSA Enc=AESGCM(
20892089
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
20902090
server_name test.com;
20912091
2092-
ssl_certificate_by_lua_block {
2093-
collectgarbage()
2092+
ssl_certificate ../../cert/test.crt;
2093+
ssl_certificate_key ../../cert/test.key;
20942094
2095-
local ffi = require "ffi"
2096-
require "defines"
2095+
server_tokens off;
2096+
location /foo {
2097+
default_type 'text/plain';
2098+
content_by_lua_block {
2099+
collectgarbage()
20972100
2098-
local errmsg = ffi.new("char *[1]")
2101+
local ffi = require "ffi"
2102+
require "defines"
20992103
2100-
local r = require "resty.core.base" .get_request()
2101-
if r == nil then
2102-
ngx.log(ngx.ERR, "no request found")
2103-
return
2104-
end
2104+
local errmsg = ffi.new("char *[1]")
21052105
2106-
-- test server random length
2107-
local out = ffi.new("unsigned char[?]", 0)
2108-
local sizep = ffi.new("size_t[1]", 0)
2109-
2110-
local rc = ffi.C.ngx_http_lua_ffi_ssl_server_random(r, out, sizep, errmsg)
2111-
if rc ~= 0 then
2112-
ngx.log(ngx.ERR, "failed to get server random length: ",
2113-
ffi.string(errmsg[0]))
2114-
return
2115-
end
2106+
local r = require "resty.core.base" .get_request()
2107+
if r == nil then
2108+
ngx.log(ngx.ERR, "no request found")
2109+
ngx.status = 201 ngx.say("foo") ngx.exit(201)
2110+
return
2111+
end
21162112
2117-
if tonumber(sizep[0]) ~= 32 then
2118-
ngx.log(ngx.ERR, "server random length does not equal 32")
2119-
return
2120-
end
2113+
-- test server random length
2114+
local out = ffi.new("unsigned char[?]", 0)
2115+
local sizep = ffi.new("size_t[1]", 0)
21212116
2122-
-- test server random value
2123-
out = ffi.new("unsigned char[?]", 50)
2124-
sizep = ffi.new("size_t[1]", 50)
2117+
local rc = ffi.C.ngx_http_lua_ffi_ssl_server_random(r, out, sizep, errmsg)
2118+
if rc ~= 0 then
2119+
ngx.log(ngx.ERR, "failed to get server random length: ",
2120+
ffi.string(errmsg[0]))
2121+
ngx.status = 201 ngx.say("foo") ngx.exit(201)
2122+
return
2123+
end
21252124
2126-
rc = ffi.C.ngx_http_lua_ffi_ssl_server_random(r, out, sizep, errmsg)
2127-
if rc ~= 0 then
2128-
ngx.log(ngx.ERR, "failed to get server random: ",
2129-
ffi.string(errmsg[0]))
2130-
return
2131-
end
2125+
if tonumber(sizep[0]) ~= 32 then
2126+
ngx.log(ngx.ERR, "server random length does not equal 32")
2127+
ngx.status = 201 ngx.say("foo") ngx.exit(201)
2128+
return
2129+
end
21322130
2133-
local init_v = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
2134-
if ffi.string(out, sizep[0]) == init_v then
2135-
ngx.log(ngx.ERR, "maybe the server random value is incorrect")
2136-
return
2137-
end
2138-
}
2131+
-- test server random value
2132+
out = ffi.new("unsigned char[?]", 50)
2133+
sizep = ffi.new("size_t[1]", 50)
21392134
2140-
ssl_certificate ../../cert/test.crt;
2141-
ssl_certificate_key ../../cert/test.key;
2135+
rc = ffi.C.ngx_http_lua_ffi_ssl_server_random(r, out, sizep, errmsg)
2136+
if rc ~= 0 then
2137+
ngx.log(ngx.ERR, "failed to get server random: ",
2138+
ffi.string(errmsg[0]))
2139+
ngx.status = 201 ngx.say("foo") ngx.exit(201)
2140+
return
2141+
end
21422142
2143-
server_tokens off;
2144-
location /foo {
2145-
default_type 'text/plain';
2146-
content_by_lua_block { ngx.status = 201 ngx.say("foo") ngx.exit(201) }
2143+
local init_v = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
2144+
if ffi.string(out, sizep[0]) == init_v then
2145+
ngx.log(ngx.ERR, "maybe the server random value is incorrect")
2146+
ngx.status = 201 ngx.say("foo") ngx.exit(201)
2147+
return
2148+
end
2149+
2150+
ngx.status = 201 ngx.say("foo") ngx.exit(201)
2151+
}
21472152
more_clear_headers Date;
21482153
}
21492154
}

‎util/build.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ time ngx-build $force $version \
3737
--with-http_ssl_module \
3838
--add-module=$root/../ndk-nginx-module \
3939
--add-module=$root/../set-misc-nginx-module \
40-
--with-ld-opt="-L$PCRE2_LIB -L$OPENSSL_LIB -Wl,-rpath,$PCRE2_LIB:$LIBDRIZZLE_LIB:$OPENSSL_LIB" \
40+
--with-ld-opt="-L$PCRE2_LIB -L$OPENSSL_LIB -L$LUAJIT_LIB -Wl,-rpath,$PCRE2_LIB:$LIBDRIZZLE_LIB:$OPENSSL_LIB:$LUAJIT_LIB" \
4141
--without-mail_pop3_module \
4242
--without-mail_imap_module \
4343
--with-http_image_filter_module \

0 commit comments

Comments
 (0)