Skip to content

Commit 6d16356

Browse files
author
mengxiangwei
committed
feat: add server random and master key fetch api
1 parent cac9cbc commit 6d16356

6 files changed

Lines changed: 370 additions & 4 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,

‎src/ngx_http_lua_ssl_certby.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,4 +1754,54 @@ ngx_http_lua_ffi_ssl_client_random(ngx_http_request_t *r,
17541754
}
17551755

17561756

1757+
int
1758+
ngx_http_lua_ffi_ssl_server_random(ngx_http_request_t *r,
1759+
unsigned char *out, size_t *outlen, char **err)
1760+
{
1761+
ngx_ssl_conn_t *ssl_conn;
1762+
1763+
if (r->connection == NULL || r->connection->ssl == NULL) {
1764+
*err = "bad request";
1765+
return NGX_ERROR;
1766+
}
1767+
1768+
ssl_conn = r->connection->ssl->connection;
1769+
if (ssl_conn == NULL) {
1770+
*err = "bad ssl conn";
1771+
return NGX_ERROR;
1772+
}
1773+
1774+
*outlen = SSL_get_server_random(ssl_conn, out, *outlen);
1775+
1776+
return NGX_OK;
1777+
}
1778+
1779+
1780+
int
1781+
ngx_http_lua_ffi_ssl_session_master_key(ngx_http_request_t *r,
1782+
unsigned char *out, size_t *outlen, char **err)
1783+
{
1784+
ngx_ssl_conn_t *ssl_conn;
1785+
SSL_SESSION *sess;
1786+
1787+
if (r->connection == NULL || r->connection->ssl == NULL) {
1788+
*err = "bad request";
1789+
return NGX_ERROR;
1790+
}
1791+
1792+
ssl_conn = r->connection->ssl->connection;
1793+
if (ssl_conn == NULL) {
1794+
*err = "bad ssl conn";
1795+
return NGX_ERROR;
1796+
}
1797+
1798+
sess = SSL_get0_session(ssl_conn);
1799+
1800+
1801+
*outlen = SSL_SESSION_get_master_key(sess, out, *outlen);
1802+
1803+
return NGX_OK;
1804+
}
1805+
1806+
17571807
#endif /* NGX_HTTP_SSL */

‎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

0 commit comments

Comments
 (0)