Skip to content

feature: ssl: support for TLS-PSK #1167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7fc0fac
feature: TLS-PSK handshake control.
Jul 28, 2017
26bd228
tests: added a test case for TLS-PSK upstream and downstream.
Jul 31, 2017
7f5808a
travis-ci: use ssl-psk branch of lua-resty-core.
Jul 31, 2017
78e1aed
config: fixed a typo, include ngx_http_lua_ssl_pskby.h in HTTP_LUA_DEPS.
Jul 31, 2017
a22ed55
bugfix: TLS-PSK requires at least OpenSSL 1.0.0, not OpenSSL 1.0.1.
Oct 4, 2017
a250455
tests: fixed a typo in TLS-PSK test case.
Oct 4, 2017
88737ed
doc: documented ssl_psk_by_lua_block, ssl_psk_by_lua_file, ssl_psk_id…
Oct 4, 2017
8b308df
style: fixed oversized lines.
Oct 4, 2017
1b9827f
style: fixed one oversized line.
Oct 4, 2017
e2c532e
tests: Use TLSv1 in TLS-PSK test case.
Oct 4, 2017
121534b
tests: Fixed a typo, use TLSv1 in TLS-PSK test case.
Oct 4, 2017
92ebec5
bugfix: use correct memory pool for ngx_palloc in ngx_http_lua_ffi_ss…
Oct 5, 2017
a28db8b
tests: trigger travis-ci run again
Oct 5, 2017
1a03ba2
bugfix: get return value of ssl_psk_by_lua_* pcall.
Oct 5, 2017
aaeee58
tests: trigger travis-ci run again.
Oct 5, 2017
7766377
tests: trigger travis-ci run again.
Oct 5, 2017
2e2c5d9
doc: generated a new markdown version.
Oct 5, 2017
375a933
doc: updated the documentation to point to a related branch of lua-re…
Oct 5, 2017
7d319c9
doc: updated the documentation to point to a related branch of lua-re…
Oct 5, 2017
b234464
doc: updated the documentation to contain a better example of using s…
Oct 5, 2017
81a1273
bugfix: return NGX_OK in ngx_http_lua_ffi_exit when in NGX_HTTP_LUA_C…
Oct 6, 2017
dda90bc
Removed trailing white spaces.
Oct 7, 2017
c1f255b
Changed ngx_http_lua_ssl_psk_server_handler to use ssl_certificate_by…
Oct 8, 2017
2d2e9a4
Removed references to ssl_psk_by_lua*.
Oct 9, 2017
8fbbe24
tests: Changed t/140-ssl-c-api.t TLS-PSK test to use ssl_certificate_…
Oct 9, 2017
7fd69c3
doc: Removed references to ssl_psk_by_lua*.
Oct 9, 2017
2ca05ce
bugfix: copy ctx->exit_code to cctx->exit_code in ngx_http_lua_ssl_ce…
Oct 9, 2017
7fff2b2
This reverts commit 01055ca82044dd452b2a10b46f67850f0526473b
Oct 9, 2017
3353c6d
tests: Updated expected ciphers to match ones used by openresty.org i…
Oct 14, 2017
f1844c8
tests: increased a timeout value of test 25 by one second in t/129-ss…
Oct 14, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ install:
- git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module
- git clone https://github.com/openresty/srcache-nginx-module.git ../srcache-nginx-module
- git clone https://github.com/openresty/redis2-nginx-module.git ../redis2-nginx-module
- git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
- git clone -b ssl-psk https://github.com/vartiait/lua-resty-core.git ../lua-resty-core
- git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
- git clone https://github.com/openresty/lua-resty-mysql.git ../lua-resty-mysql
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git
Expand Down
49 changes: 49 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ Directives
* [lua_need_request_body](#lua_need_request_body)
* [ssl_certificate_by_lua_block](#ssl_certificate_by_lua_block)
* [ssl_certificate_by_lua_file](#ssl_certificate_by_lua_file)
* [ssl_psk_identity_hint](#ssl_psk_identity_hint)
* [ssl_session_fetch_by_lua_block](#ssl_session_fetch_by_lua_block)
* [ssl_session_fetch_by_lua_file](#ssl_session_fetch_by_lua_file)
* [ssl_session_store_by_lua_block](#ssl_session_store_by_lua_block)
Expand All @@ -1094,6 +1095,8 @@ Directives
* [lua_ssl_protocols](#lua_ssl_protocols)
* [lua_ssl_trusted_certificate](#lua_ssl_trusted_certificate)
* [lua_ssl_verify_depth](#lua_ssl_verify_depth)
* [lua_ssl_psk_identity](#lua_ssl_psk_identity)
* [lua_ssl_psk_key](#lua_ssl_psk_key)
* [lua_http10_buffering](#lua_http10_buffering)
* [rewrite_by_lua_no_postpone](#rewrite_by_lua_no_postpone)
* [access_by_lua_no_postpone](#access_by_lua_no_postpone)
Expand Down Expand Up @@ -2564,6 +2567,22 @@ This directive was first introduced in the `v0.10.0` release.

[Back to TOC](#directives)

ssl_psk_identity_hint
---------------------

**syntax:** *ssl_psk_identity_hint <tls_psk_identity_hint>*

**default:** *no*

**context:** *http, server*

Specifies the TLS-PSK identity hint string which NGINX will send to a client during
the SSL handshake for the downstream SSL (https) connections.

This directive was first introduced in the `v0.XX.YY` release.

[Back to TOC](#directives)

ssl_session_fetch_by_lua_block
------------------------------

Expand Down Expand Up @@ -2958,6 +2977,36 @@ See also [lua_ssl_trusted_certificate](#lua_ssl_trusted_certificate).

[Back to TOC](#directives)

lua_ssl_psk_identity
--------------------

**syntax:** *lua_ssl_psk_identity <tls_psk_identity>*

**default:** *no*

**context:** *http, server, location*

Specifies the TLS-PSK identity string which NGINX will send to a SSL/TLS server in the [tcpsock:sslhandshake](#tcpsocksslhandshake) method.

This directive was first introduced in the `v0.XX.YY` release.

[Back to TOC](#directives)

lua_ssl_psk_key
---------------

**syntax:** *lua_ssl_psk_key <tls_psk_key>*

**default:** *no*

**context:** *http, server, location*

Specifies the TLS-PSK key string which NGINX will try use with a SSL/TLS server in the [tcpsock:sslhandshake](#tcpsocksslhandshake) method.

This directive was first introduced in the `v0.XX.YY` release.

[Back to TOC](#directives)

lua_http10_buffering
--------------------

Expand Down
2 changes: 2 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ HTTP_LUA_SRCS=" \
$ngx_addon_dir/src/ngx_http_lua_balancer.c \
$ngx_addon_dir/src/ngx_http_lua_ssl_session_storeby.c \
$ngx_addon_dir/src/ngx_http_lua_ssl_session_fetchby.c \
$ngx_addon_dir/src/ngx_http_lua_ssl_pskby.c \
$ngx_addon_dir/src/ngx_http_lua_ssl.c \
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.c \
"
Expand Down Expand Up @@ -420,6 +421,7 @@ HTTP_LUA_DEPS=" \
$ngx_addon_dir/src/ngx_http_lua_balancer.h \
$ngx_addon_dir/src/ngx_http_lua_ssl_session_storeby.h \
$ngx_addon_dir/src/ngx_http_lua_ssl_session_fetchby.h \
$ngx_addon_dir/src/ngx_http_lua_ssl_pskby.h \
$ngx_addon_dir/src/ngx_http_lua_ssl.h \
$ngx_addon_dir/src/ngx_http_lua_log_ringbuf.h \
"
Expand Down
37 changes: 37 additions & 0 deletions doc/HttpLuaModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,19 @@ When a relative path like <code>foo/bar.lua</code> is given, they will be turned

This directive was first introduced in the <code>v0.10.0</code> release.

== ssl_psk_identity_hint ==

'''syntax:''' ''ssl_psk_identity_hint <tls_psk_identity_hint>''

'''default:''' ''no''

'''context:''' ''http, server''

Specifies the TLS-PSK identity hint string which NGINX will send to a client during
the SSL handshake for the downstream SSL (https) connections.

This directive was first introduced in the <code>v0.XX.YY</code> release.

== ssl_session_fetch_by_lua_block ==

'''syntax:''' ''ssl_session_fetch_by_lua_block { lua-script }''
Expand Down Expand Up @@ -2498,6 +2511,30 @@ This directive was first introduced in the <code>v0.9.11</code> release.

See also [[#lua_ssl_trusted_certificate|lua_ssl_trusted_certificate]].

== lua_ssl_psk_identity ==

'''syntax:''' ''lua_ssl_psk_identity <tls_psk_identity>''

'''default:''' ''no''

'''context:''' ''http, server, location''

Specifies the TLS-PSK identity string which NGINX will send to a SSL/TLS server in the [[#tcpsock:sslhandshake|tcpsock:sslhandshake]] method.

This directive was first introduced in the <code>v0.XX.YY</code> release.

== lua_ssl_psk_key ==

'''syntax:''' ''lua_ssl_psk_key <tls_psk_key>''

'''default:''' ''no''

'''context:''' ''http, server, location''

Specifies the TLS-PSK key string which NGINX will try use with a SSL/TLS server in the [[#tcpsock:sslhandshake|tcpsock:sslhandshake]] method.

This directive was first introduced in the <code>v0.XX.YY</code> release.

== lua_http10_buffering ==

'''syntax:''' ''lua_http10_buffering on|off''
Expand Down
5 changes: 5 additions & 0 deletions src/ngx_http_lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ typedef struct {
#define NGX_HTTP_LUA_CONTEXT_SSL_CERT 0x0400
#define NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE 0x0800
#define NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH 0x1000
#define NGX_HTTP_LUA_CONTEXT_SSL_PSK 0x2000


#ifndef NGX_LUA_NO_FFI_API
Expand Down Expand Up @@ -248,6 +249,8 @@ union ngx_http_lua_srv_conf_u {
ngx_http_lua_srv_conf_handler_pt ssl_sess_fetch_handler;
ngx_str_t ssl_sess_fetch_src;
u_char *ssl_sess_fetch_src_key;

ngx_str_t ssl_psk_identity_hint;
} srv;
#endif

Expand All @@ -268,6 +271,8 @@ typedef struct {
ngx_uint_t ssl_verify_depth;
ngx_str_t ssl_trusted_certificate;
ngx_str_t ssl_crl;
ngx_str_t ssl_psk_identity;
ngx_str_t ssl_psk_key;
#endif

ngx_flag_t force_read_body; /* whether force request body to
Expand Down
15 changes: 10 additions & 5 deletions src/ngx_http_lua_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,15 @@ ngx_http_lua_ngx_exit(lua_State *L)
| NGX_HTTP_LUA_CONTEXT_BALANCER
| NGX_HTTP_LUA_CONTEXT_SSL_CERT
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH);
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH
| NGX_HTTP_LUA_CONTEXT_SSL_PSK);

rc = (ngx_int_t) luaL_checkinteger(L, 1);

if (ctx->context & (NGX_HTTP_LUA_CONTEXT_SSL_CERT
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH))
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH
| NGX_HTTP_LUA_CONTEXT_SSL_PSK))
{

#if (NGX_HTTP_SSL)
Expand All @@ -339,7 +341,8 @@ ngx_http_lua_ngx_exit(lua_State *L)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua exit with code %i", rc);

if (ctx->context == NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE) {
if (ctx->context == NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
|| ctx->context == NGX_HTTP_LUA_CONTEXT_SSL_PSK) {
return 0;
}

Expand Down Expand Up @@ -473,7 +476,8 @@ ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,
| NGX_HTTP_LUA_CONTEXT_BALANCER
| NGX_HTTP_LUA_CONTEXT_SSL_CERT
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH,
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH
| NGX_HTTP_LUA_CONTEXT_SSL_PSK,
err, errlen)
!= NGX_OK)
{
Expand All @@ -482,7 +486,8 @@ ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,

if (ctx->context & (NGX_HTTP_LUA_CONTEXT_SSL_CERT
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH))
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH
| NGX_HTTP_LUA_CONTEXT_SSL_PSK))
{

#if (NGX_HTTP_SSL)
Expand Down
89 changes: 89 additions & 0 deletions src/ngx_http_lua_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "ngx_http_lua_ssl_certby.h"
#include "ngx_http_lua_ssl_session_storeby.h"
#include "ngx_http_lua_ssl_session_fetchby.h"
#include "ngx_http_lua_ssl_pskby.h"
#include "ngx_http_lua_headers.h"


Expand Down Expand Up @@ -557,6 +558,27 @@ static ngx_command_t ngx_http_lua_cmds[] = {
0,
(void *) ngx_http_lua_ssl_sess_fetch_handler_file },

{ ngx_string("ssl_psk_identity_hint"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_lua_srv_conf_t, srv.ssl_psk_identity_hint),
NULL },

{ ngx_string("lua_ssl_psk_identity"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_lua_loc_conf_t, ssl_psk_identity),
NULL },

{ ngx_string("lua_ssl_psk_key"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_lua_loc_conf_t, ssl_psk_key),
NULL },

{ ngx_string("lua_ssl_verify_depth"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
Expand Down Expand Up @@ -922,6 +944,8 @@ ngx_http_lua_create_srv_conf(ngx_conf_t *cf)
* lscf->srv.ssl_session_fetch_src = { 0, NULL };
* lscf->srv.ssl_session_fetch_src_key = NULL;
*
* lscf->srv.ssl_psk_identity_hint = { 0, NULL };
*
* lscf->balancer.handler = NULL;
* lscf->balancer.src = { 0, NULL };
* lscf->balancer.src_key = NULL;
Expand Down Expand Up @@ -969,6 +993,8 @@ ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)

SSL_CTX_set_cert_cb(sscf->ssl.ctx, ngx_http_lua_ssl_cert_handler, NULL);

SSL_CTX_set_psk_server_callback(sscf->ssl.ctx,
ngx_http_lua_ssl_psk_server_handler);
# else

ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
Expand Down Expand Up @@ -1024,6 +1050,42 @@ ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
}
}

if (conf->srv.ssl_psk_identity_hint.len == 0) {
conf->srv.ssl_psk_identity_hint = prev->srv.ssl_psk_identity_hint;
}

if (conf->srv.ssl_psk_identity_hint.len) {
dd("ssl psk identity hint: %.*s",
(int) conf->srv.ssl_psk_identity_hint.len,
conf->srv.ssl_psk_identity_hint.data);

# if OPENSSL_VERSION_NUMBER >= 0x1000000fL

sscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_ssl_module);
if (sscf == NULL || sscf->ssl.ctx == NULL) {
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"no ssl configured for the server");

return NGX_CONF_ERROR;
}

if (SSL_CTX_use_psk_identity_hint(sscf->ssl.ctx,
(const char *) conf->srv.ssl_psk_identity_hint.data) == 0) {
ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
"SSL_CTX_use_psk_identity_hint(\"%V\") failed",
&conf->srv.ssl_psk_identity_hint);
return NGX_CONF_ERROR;
}

# else

ngx_log_error(NGX_LOG_CRIT, cf->log, 0,
"OpenSSL too old to support ssl_psk_identity_hint");
return NGX_CONF_ERROR;

# endif
}

#endif /* NGX_HTTP_SSL */
return NGX_CONF_OK;
}
Expand Down Expand Up @@ -1067,6 +1129,8 @@ ngx_http_lua_create_loc_conf(ngx_conf_t *cf)
* conf->ssl_ciphers = { 0, NULL };
* conf->ssl_trusted_certificate = { 0, NULL };
* conf->ssl_crl = { 0, NULL };
* conf->ssl_psk_identity = { 0, NULL };
* conf->ssl_psk_key = {0, NULL };
*/

conf->force_read_body = NGX_CONF_UNSET;
Expand Down Expand Up @@ -1160,6 +1224,11 @@ ngx_http_lua_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
prev->ssl_trusted_certificate, "");
ngx_conf_merge_str_value(conf->ssl_crl, prev->ssl_crl, "");

ngx_conf_merge_str_value(conf->ssl_psk_identity,
prev->ssl_psk_identity, "");
ngx_conf_merge_str_value(conf->ssl_psk_key,
prev->ssl_psk_key, "");

if (ngx_http_lua_set_ssl(cf, conf) != NGX_OK) {
return NGX_CONF_ERROR;
}
Expand Down Expand Up @@ -1266,6 +1335,26 @@ ngx_http_lua_set_ssl(ngx_conf_t *cf, ngx_http_lua_loc_conf_t *llcf)
return NGX_ERROR;
}

if (llcf->ssl_psk_identity.len && llcf->ssl_psk_key.len) {
dd("ssl psk identity: %.*s", (int) llcf->ssl_psk_identity.len,
llcf->ssl_psk_identity.data);
dd("ssl psk key: %.*s", (int) llcf->ssl_psk_key.len,
llcf->ssl_psk_key.data);

# if OPENSSL_VERSION_NUMBER >= 0x1000000fL

SSL_CTX_set_psk_client_callback(llcf->ssl->ctx,
ngx_http_lua_ssl_psk_client_handler);

# else

ngx_log_error(NGX_LOG_CRIT, cf->log, 0,
"OpenSSL too old to support ssl_psk_identity");
return NGX_ERROR;

# endif
}

return NGX_OK;
}

Expand Down
4 changes: 4 additions & 0 deletions src/ngx_http_lua_phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ ngx_http_lua_ngx_get_phase(lua_State *L)
lua_pushliteral(L, "balancer");
break;

case NGX_HTTP_LUA_CONTEXT_SSL_PSK:
lua_pushliteral(L, "ssl_psk");
break;

case NGX_HTTP_LUA_CONTEXT_SSL_CERT:
lua_pushliteral(L, "ssl_cert");
break;
Expand Down
Loading