Skip to content

Commit 4175a65

Browse files
willmafhzhuizhuhaomeng
authored andcommitted
feature: proxy_ssl_verify_by_lua* directives
working after receiving server certificates, allowing us to control upstream ssl handshake dynamically with Lua
1 parent 2bc2e81 commit 4175a65

11 files changed

+2909
-3
lines changed

README.markdown

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,8 @@ Directives
11741174
* [ssl_session_fetch_by_lua_file](#ssl_session_fetch_by_lua_file)
11751175
* [ssl_session_store_by_lua_block](#ssl_session_store_by_lua_block)
11761176
* [ssl_session_store_by_lua_file](#ssl_session_store_by_lua_file)
1177+
* [proxy_ssl_certificate_by_lua_block](#proxy_ssl_certificate_by_lua_block)
1178+
* [proxy_ssl_certificate_by_lua_file](#proxy_ssl_certificate_by_lua_file)
11771179
* [proxy_ssl_verify_by_lua_block](#proxy_ssl_verify_by_lua_block)
11781180
* [proxy_ssl_verify_by_lua_file](#proxy_ssl_verify_by_lua_file)
11791181
* [lua_shared_dict](#lua_shared_dict)
@@ -3236,6 +3238,96 @@ Note that: this directive is only allowed to used in **http context** from the `
32363238

32373239
[Back to TOC](#directives)
32383240

3241+
proxy_ssl_certificate_by_lua_block
3242+
----------------------------------
3243+
3244+
**syntax:** *proxy_ssl_certificate_by_lua_block { lua-script }*
3245+
3246+
**context:** *location*
3247+
3248+
**phase:** *right-after-server-certificate-request-message-was-processed*
3249+
3250+
This directive runs user Lua code when Nginx is about to post-process the SSL server certificate request message from upstream. It is particularly useful for setting the SSL certificate chain and the corresponding private key for the upstream SSL (https) connections. It is also useful to load such handshake configurations nonblockingly from the remote (for example, with the [cosocket](#ngxsockettcp) API).
3251+
3252+
The [ngx.ssl.proxysslcert](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl/proxysslcert.md) Lua module provided by the [lua-resty-core](https://github.com/openresty/lua-resty-core/#readme) library are particularly useful in this context.
3253+
3254+
Below is a trivial example using the [ngx.ssl](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl.md) module and the [ngx.ssl.proxysslcert](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl/proxysslcert.md) module at the same time:
3255+
3256+
```nginx
3257+
3258+
server {
3259+
listen 443 ssl;
3260+
server_name test.com;
3261+
ssl_certificate /path/to/cert.crt;
3262+
ssl_certificate_key /path/to/key.key;
3263+
3264+
location /t {
3265+
proxy_pass https://upstream;
3266+
3267+
proxy_ssl_certificate_by_lua_block {
3268+
local ssl = require "ngx.ssl"
3269+
local proxy_ssl_cert = require "ngx.ssl.proxysslcert"
3270+
3271+
-- NOTE: for illustration only, we don't handle error below
3272+
3273+
local f = assert(io.open("/path/to/cert.crt"))
3274+
local cert_data = f:read("*a")
3275+
f:close()
3276+
3277+
local cert, err = ssl.parse_pem_cert(cert_data)
3278+
local ok, err = proxy_ssl_cert.set_cert(cert)
3279+
3280+
local f = assert(io.open("/path/to/key.key"))
3281+
local pkey_data = f:read("*a")
3282+
f:close()
3283+
3284+
local pkey, err = ssl.parse_pem_priv_key(pkey_data)
3285+
local ok, err = proxy_ssl_cert.set_priv_key(pkey)
3286+
-- ...
3287+
}
3288+
}
3289+
...
3290+
}
3291+
```
3292+
3293+
See more information in the [ngx.ssl.proxysslcert](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl/proxysslcert.md) Lua module's official documentation.
3294+
3295+
Uncaught Lua exceptions in the user Lua code immediately abort the current SSL session, so does the
3296+
[ngx.exit](#ngxexit) call with an error code like `ngx.ERROR`.
3297+
3298+
This Lua code execution context *does* support yielding, so Lua APIs that may yield (like cosockets, sleeping, and "light threads") are enabled in this context.
3299+
3300+
Note that, unlike the relations between the [ssl_certificate](https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_certificate) and [ssl_certificate_key](https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_certificate_key) directives and [ssl_certificate_by_lua*](#ssl_certificate_by_lua_block), the [proxy_ssl_certificate](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate) and [proxy_ssl_certificate_key](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate_key) directives can be used together with [proxy_ssl_certificate_by_lua*](#proxy_ssl_certificate_by_lua_block).
3301+
3302+
* When there are only [proxy_ssl_certificate](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate) and [proxy_ssl_certificate_key](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate_key) directives, the original Nginx behavior will obviously remain the same.
3303+
3304+
* When there is only [proxy_ssl_certificate_by_lua*](#proxy_ssl_certificate_by_lua_block), Nginx will send the certificate and its related private key and chain set by Lua codes.
3305+
3306+
* When the [proxy_ssl_certificate](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate) and [proxy_ssl_certificate_key](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate_key) directives and [proxy_ssl_certificate_by_lua*](#proxy_ssl_certificate_by_lua_block) are used at the same time, then [proxy_ssl_certificate_by_lua*](#proxy_ssl_certificate_by_lua_block) will take precedence over the [proxy_ssl_certificate](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate) and [proxy_ssl_certificate_key](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate_key) directives.
3307+
3308+
Please refer to corresponding test case file and [ngx.ssl.proxysslcert](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl/proxysslcert.md) for more details.
3309+
3310+
Note also that, it has the same condition as the [proxy_ssl_certificate](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate) directive for [proxy_ssl_certificate_by_lua*](#proxy_ssl_certificate_by_lua_block) to work, that is the upstream server should enable verification of client certificates.
3311+
3312+
This directive requires OpenSSL 1.0.2e or greater.
3313+
3314+
[Back to TOC](#directives)
3315+
3316+
proxy_ssl_certificate_by_lua_file
3317+
---------------------------------
3318+
3319+
**syntax:** *proxy_ssl_certificate_by_lua_file <path-to-lua-script-file>*
3320+
3321+
**context:** *location*
3322+
3323+
**phase:** *right-after-server-certificate-request-message-was-processed*
3324+
3325+
Equivalent to [proxy_ssl_certificate_by_lua_block](#proxy_ssl_certificate_by_lua_block), except that the file specified by `<path-to-lua-script-file>` contains the Lua code, or, as from the `v0.5.0rc32` release, the [LuaJIT bytecode](#luajit-bytecode-support) to be executed.
3326+
3327+
When a relative path like `foo/bar.lua` is given, they will be turned into the absolute path relative to the `server prefix` path determined by the `-p PATH` command-line option while starting the Nginx server.
3328+
3329+
[Back to TOC](#directives)
3330+
32393331
proxy_ssl_verify_by_lua_block
32403332
-----------------------------
32413333

config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ HTTP_LUA_SRCS=" \
297297
$ngx_addon_dir/src/ngx_http_lua_ssl_session_storeby.c \
298298
$ngx_addon_dir/src/ngx_http_lua_ssl_session_fetchby.c \
299299
$ngx_addon_dir/src/ngx_http_lua_ssl.c \
300+
$ngx_addon_dir/src/ngx_http_lua_proxy_ssl_certby.c \
300301
$ngx_addon_dir/src/ngx_http_lua_proxy_ssl_verifyby.c \
301302
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.c \
302303
$ngx_addon_dir/src/ngx_http_lua_input_filters.c \
@@ -362,6 +363,7 @@ HTTP_LUA_DEPS=" \
362363
$ngx_addon_dir/src/ngx_http_lua_ssl_session_storeby.h \
363364
$ngx_addon_dir/src/ngx_http_lua_ssl_session_fetchby.h \
364365
$ngx_addon_dir/src/ngx_http_lua_ssl.h \
366+
$ngx_addon_dir/src/ngx_http_lua_proxy_ssl_certby.h \
365367
$ngx_addon_dir/src/ngx_http_lua_proxy_ssl_verifyby.h \
366368
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.h \
367369
$ngx_addon_dir/src/ngx_http_lua_input_filters.h \

src/ngx_http_lua_common.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ typedef struct {
150150
#define NGX_HTTP_LUA_CONTEXT_SERVER_REWRITE 0x00008000
151151
#define NGX_HTTP_LUA_CONTEXT_PROXY_SSL_VERIFY 0x00010000
152152
#define NGX_HTTP_LUA_CONTEXT_PRECONTENT 0x00020000
153-
153+
#define NGX_HTTP_LUA_CONTEXT_PROXY_SSL_CERT 0x00040000
154154

155155
#define NGX_HTTP_LUA_FFI_NO_REQ_CTX -100
156156
#define NGX_HTTP_LUA_FFI_BAD_CONTEXT -101
@@ -394,6 +394,12 @@ struct ngx_http_lua_loc_conf_s {
394394
#endif
395395

396396
#ifdef HAVE_PROXY_SSL_PATCH
397+
ngx_http_lua_loc_conf_handler_pt proxy_ssl_cert_handler;
398+
ngx_str_t proxy_ssl_cert_src;
399+
u_char *proxy_ssl_cert_src_key;
400+
u_char *proxy_ssl_cert_chunkname;
401+
int proxy_ssl_cert_src_ref;
402+
397403
ngx_http_lua_loc_conf_handler_pt proxy_ssl_verify_handler;
398404
ngx_str_t proxy_ssl_verify_src;
399405
u_char *proxy_ssl_verify_src_key;

src/ngx_http_lua_control.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,
388388
| NGX_HTTP_LUA_CONTEXT_HEADER_FILTER
389389
| NGX_HTTP_LUA_CONTEXT_BALANCER
390390
#ifdef HAVE_PROXY_SSL_PATCH
391+
| NGX_HTTP_LUA_CONTEXT_PROXY_SSL_CERT
391392
| NGX_HTTP_LUA_CONTEXT_PROXY_SSL_VERIFY
392393
#endif
393394
| NGX_HTTP_LUA_CONTEXT_SSL_CLIENT_HELLO
@@ -402,6 +403,7 @@ ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,
402403

403404
if (ctx->context & (NGX_HTTP_LUA_CONTEXT_SSL_CERT
404405
#ifdef HAVE_PROXY_SSL_PATCH
406+
| NGX_HTTP_LUA_CONTEXT_PROXY_SSL_CERT
405407
| NGX_HTTP_LUA_CONTEXT_PROXY_SSL_VERIFY
406408
#endif
407409
| NGX_HTTP_LUA_CONTEXT_SSL_CLIENT_HELLO

src/ngx_http_lua_module.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
#include "ngx_http_lua_ssl_certby.h"
3333
#include "ngx_http_lua_ssl_session_storeby.h"
3434
#include "ngx_http_lua_ssl_session_fetchby.h"
35+
36+
#ifdef HAVE_PROXY_SSL_PATCH
37+
#include "ngx_http_lua_proxy_ssl_certby.h"
3538
#include "ngx_http_lua_proxy_ssl_verifyby.h"
39+
#endif
3640

3741
#include "ngx_http_lua_headers.h"
3842
#include "ngx_http_lua_headers_out.h"
@@ -695,6 +699,21 @@ static ngx_command_t ngx_http_lua_cmds[] = {
695699
(void *) ngx_http_lua_ssl_sess_fetch_handler_file },
696700

697701
#if HAVE_LUA_PROXY_SSL_VERIFY
702+
/* same context as proxy_pass directive */
703+
{ ngx_string("proxy_ssl_certificate_by_lua_block"),
704+
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
705+
ngx_http_lua_proxy_ssl_cert_by_lua_block,
706+
NGX_HTTP_LOC_CONF_OFFSET,
707+
0,
708+
(void *) ngx_http_lua_proxy_ssl_cert_handler_inline },
709+
710+
{ ngx_string("proxy_ssl_certificate_by_lua_file"),
711+
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
712+
ngx_http_lua_proxy_ssl_cert_by_lua,
713+
NGX_HTTP_LOC_CONF_OFFSET,
714+
0,
715+
(void *) ngx_http_lua_proxy_ssl_cert_handler_file },
716+
698717
/* same context as proxy_pass directive */
699718
{ ngx_string("proxy_ssl_verify_by_lua_block"),
700719
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
@@ -1524,6 +1543,11 @@ ngx_http_lua_create_loc_conf(ngx_conf_t *cf)
15241543
* conf->ssl_crl = { 0, NULL };
15251544
* conf->ssl_key_log = { 0, NULL };
15261545
*
1546+
* conf->proxy_ssl_cert_handler = NULL;
1547+
* conf->proxy_ssl_cert_src = { 0, NULL };
1548+
* conf->proxy_ssl_cert_chunkname = NULL;
1549+
* conf->proxy_ssl_cert_src_key = NULL;
1550+
*
15271551
* conf->proxy_ssl_verify_handler = NULL;
15281552
* conf->proxy_ssl_verify_src = { 0, NULL };
15291553
* conf->proxy_ssl_verify_chunkname = NULL;
@@ -1563,6 +1587,7 @@ ngx_http_lua_create_loc_conf(ngx_conf_t *cf)
15631587
conf->ssl_conf_commands = NGX_CONF_UNSET_PTR;
15641588
#endif
15651589
#ifdef HAVE_PROXY_SSL_PATCH
1590+
conf->proxy_ssl_cert_src_ref = LUA_REFNIL;
15661591
conf->proxy_ssl_verify_src_ref = LUA_REFNIL;
15671592
conf->upstream_skip_openssl_default_verify = NGX_CONF_UNSET;
15681593
#endif
@@ -1669,6 +1694,20 @@ ngx_http_lua_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
16691694
#endif
16701695

16711696
#if HAVE_LUA_PROXY_SSL_VERIFY
1697+
if (conf->proxy_ssl_cert_src.len == 0) {
1698+
conf->proxy_ssl_cert_src = prev->proxy_ssl_cert_src;
1699+
conf->proxy_ssl_cert_handler = prev->proxy_ssl_cert_handler;
1700+
conf->proxy_ssl_cert_src_ref = prev->proxy_ssl_cert_src_ref;
1701+
conf->proxy_ssl_cert_src_key = prev->proxy_ssl_cert_src_key;
1702+
conf->proxy_ssl_cert_chunkname = prev->proxy_ssl_cert_chunkname;
1703+
}
1704+
1705+
if (conf->proxy_ssl_cert_src.len) {
1706+
if (ngx_http_lua_proxy_ssl_cert_set_callback(cf) != NGX_OK) {
1707+
return NGX_CONF_ERROR;
1708+
}
1709+
}
1710+
16721711
if (conf->proxy_ssl_verify_src.len == 0) {
16731712
conf->proxy_ssl_verify_src = prev->proxy_ssl_verify_src;
16741713
conf->proxy_ssl_verify_handler = prev->proxy_ssl_verify_handler;

0 commit comments

Comments
 (0)