Skip to content

Commit a38d906

Browse files
spacewanderthibaultcha
authored andcommitted
feature: added OpenSSL 1.1.1 support to ssl_session_fetch_by_lua*.
See openresty/openresty#456. Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>
1 parent 3a58729 commit a38d906

File tree

6 files changed

+162
-46
lines changed

6 files changed

+162
-46
lines changed

.travis.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ env:
4848
- DRIZZLE_VER=2011.07.21
4949
- TEST_NGINX_SLEEP=0.006
5050
matrix:
51-
- NGINX_VERSION=1.15.8 OPENSSL_VER=1.0.2s OPENSSL_OPT="" OPENSSL_PATCH_VER=1.0.2h
52-
- NGINX_VERSION=1.15.8 OPENSSL_VER=1.1.0k OPENSSL_OPT="" OPENSSL_PATCH_VER=1.1.0d
53-
# TODO: when adding an OpenSSL version >= 1.1.1, please add "enable-tls1_3"
54-
# to $OPENSSL_OPT.
51+
- NGINX_VERSION=1.15.8 OPENSSL_VER=1.0.2s OPENSSL_PATCH_VER=1.0.2h
52+
- NGINX_VERSION=1.15.8 OPENSSL_VER=1.1.0k OPENSSL_PATCH_VER=1.1.0d
53+
- NGINX_VERSION=1.15.8 OPENSSL_VER=1.1.1c OPENSSL_PATCH_VER=""
5554

5655
services:
5756
- memcache
@@ -116,8 +115,8 @@ script:
116115
- cd ..
117116
- tar zxf download-cache/openssl-$OPENSSL_VER.tar.gz
118117
- cd openssl-$OPENSSL_VER/
119-
- patch -p1 < ../../openresty/patches/openssl-$OPENSSL_PATCH_VER-sess_set_get_cb_yield.patch
120-
- ./config no-threads shared enable-ssl3 enable-ssl3-method $OPENSSL_OPT -g --prefix=$OPENSSL_PREFIX -DPURIFY > build.log 2>&1 || (cat build.log && exit 1)
118+
- if [ -n "$OPENSSL_PATCH_VER" ]; then patch -p1 < ../../openresty/patches/openssl-$OPENSSL_PATCH_VER-sess_set_get_cb_yield.patch; fi
119+
- ./config no-threads shared enable-ssl3 enable-ssl3-method -g --prefix=$OPENSSL_PREFIX -DPURIFY > build.log 2>&1 || (cat build.log && exit 1)
121120
- make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
122121
- sudo make PATH=$PATH install_sw > build.log 2>&1 || (cat build.log && exit 1)
123122
- cd ..

README.markdown

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,15 +2623,18 @@ But do not forget to comment this line out before publishing your site to the wo
26232623
If you are using the [official pre-built packages](http://openresty.org/en/linux-packages.html) for [OpenResty](https://openresty.org/)
26242624
1.11.2.1 or later, then everything should work out of the box.
26252625

2626-
If you are using OpenSSL libraries not provided by [OpenResty](https://openresty.org),
2627-
then you need to apply the following patch for OpenSSL 1.0.2h or later:
2626+
If you are not using one of the [OpenSSL
2627+
packages](https://openresty.org/en/linux-packages.html) provided by
2628+
[OpenResty](https://openresty.org), you will need to apply patches to OpenSSL
2629+
1.0.2, up to (and including) 1.1.0:
26282630

2629-
<https://github.com/openresty/openresty/blob/master/patches/openssl-1.0.2h-sess_set_get_cb_yield.patch>
2631+
<https://openresty.org/en/openssl-patches.html>
26302632

2631-
If you are not using the NGINX core shipped with [OpenResty](https://openresty.org) 1.11.2.1 or later, then you need to
2632-
apply the following patch to the standard NGINX core 1.11.2 or later:
2633+
Similarly, if you are not using the NGINX core shipped with
2634+
[OpenResty](https://openresty.org) 1.11.2.1 or later, you will need to apply
2635+
patches to the standard NGINX core:
26332636

2634-
<http://openresty.org/download/nginx-1.11.2-nonblocking_ssl_handshake_hooks.patch>
2637+
<https://openresty.org/en/nginx-ssl-patches.html>
26352638

26362639
This directive was first introduced in the `v0.10.6` release.
26372640

doc/HttpLuaModule.wiki

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,15 +2219,18 @@ But do not forget to comment this line out before publishing your site to the wo
22192219
If you are using the [official pre-built packages](http://openresty.org/en/linux-packages.html) for [OpenResty](https://openresty.org/)
22202220
1.11.2.1 or later, then everything should work out of the box.
22212221
2222-
If you are using OpenSSL libraries not provided by [OpenResty](https://openresty.org),
2223-
then you need to apply the following patch for OpenSSL 1.0.2h or later:
2222+
If you are not using one of the [OpenSSL
2223+
packages](https://openresty.org/en/linux-packages.html) provided by
2224+
[OpenResty](https://openresty.org), you will need to apply patches to OpenSSL
2225+
1.0.2, up to (and including) 1.1.0:
22242226
2225-
https://github.com/openresty/openresty/blob/master/patches/openssl-1.0.2h-sess_set_get_cb_yield.patch
2227+
https://openresty.org/en/openssl-patches.html
22262228
2227-
If you are not using the NGINX core shipped with [OpenResty](https://openresty.org) 1.11.2.1 or later, then you need to
2228-
apply the following patch to the standard NGINX core 1.11.2 or later:
2229+
Similarly, if you are not using the NGINX core shipped with
2230+
[OpenResty](https://openresty.org) 1.11.2.1 or later, you will need to apply
2231+
patches to the standard NGINX core:
22292232
2230-
http://openresty.org/download/nginx-1.11.2-nonblocking_ssl_handshake_hooks.patch
2233+
https://openresty.org/en/nginx-ssl-patches.html
22312234
22322235
This directive was first introduced in the <code>v0.10.6</code> release.
22332236

src/ngx_http_lua_module.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,12 @@ ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
10771077

10781078
return NGX_CONF_ERROR;
10791079
#else
1080+
# ifdef HAVE_SSL_CLIENT_HELLO_CB_SUPPORT
1081+
SSL_CTX_set_client_hello_cb(sscf->ssl.ctx,
1082+
ngx_http_lua_ssl_client_hello_handler,
1083+
NULL);
1084+
# endif
1085+
10801086
SSL_CTX_sess_set_get_cb(sscf->ssl.ctx,
10811087
ngx_http_lua_ssl_sess_fetch_handler);
10821088
#endif

src/ngx_http_lua_ssl_session_fetchby.c

Lines changed: 128 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,12 @@ ngx_http_lua_ssl_sess_fetch_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
173173
}
174174

175175

176-
/* cached session fetching callback to be set with SSL_CTX_sess_set_get_cb */
177-
ngx_ssl_session_t *
178-
ngx_http_lua_ssl_sess_fetch_handler(ngx_ssl_conn_t *ssl_conn,
179-
#if OPENSSL_VERSION_NUMBER >= 0x10100003L
180-
const
181-
#endif
182-
u_char *id, int len, int *copy)
176+
static ngx_int_t
177+
ngx_http_lua_ssl_sess_fetch_helper(ngx_ssl_conn_t *ssl_conn,
178+
const u_char *id, int len)
183179
{
184180
lua_State *L;
185-
ngx_int_t rc;
181+
ngx_int_t rc, res = NGX_ERROR;
186182
ngx_connection_t *c, *fc = NULL;
187183
ngx_http_request_t *r = NULL;
188184
ngx_pool_cleanup_t *cln;
@@ -191,11 +187,6 @@ ngx_http_lua_ssl_sess_fetch_handler(ngx_ssl_conn_t *ssl_conn,
191187
ngx_http_lua_srv_conf_t *lscf;
192188
ngx_http_core_loc_conf_t *clcf;
193189

194-
/* set copy to 0 as we expect OpenSSL to handle
195-
* the memory of returned session */
196-
197-
*copy = 0;
198-
199190
c = ngx_ssl_get_connection(ssl_conn);
200191

201192
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
@@ -217,17 +208,10 @@ ngx_http_lua_ssl_sess_fetch_handler(ngx_ssl_conn_t *ssl_conn,
217208
cctx->exit_code);
218209

219210
dd("lua ssl sess_fetch done, finally");
220-
return cctx->session;
211+
return NGX_OK;
221212
}
222213

223-
#ifdef SSL_ERROR_PENDING_SESSION
224-
return SSL_magic_pending_session_ptr();
225-
#else
226-
ngx_log_error(NGX_LOG_CRIT, c->log, 0,
227-
"lua: cannot yield in sess get cb: "
228-
"missing async sess get cb support in OpenSSL");
229-
return NULL;
230-
#endif
214+
return NGX_AGAIN;
231215
}
232216

233217
dd("first time");
@@ -329,7 +313,7 @@ ngx_http_lua_ssl_sess_fetch_handler(ngx_ssl_conn_t *ssl_conn,
329313
"sess get cb exit code: %d", rc, cctx->exit_code);
330314

331315
c->log->action = "SSL handshaking";
332-
return cctx->session;
316+
return NGX_OK;
333317
}
334318

335319
/* rc == NGX_DONE */
@@ -356,12 +340,13 @@ ngx_http_lua_ssl_sess_fetch_handler(ngx_ssl_conn_t *ssl_conn,
356340

357341
*cctx->cleanup = ngx_http_lua_ssl_sess_fetch_aborted;
358342

359-
#ifdef SSL_ERROR_PENDING_SESSION
360-
return SSL_magic_pending_session_ptr();
343+
#if defined(SSL_ERROR_PENDING_SESSION) \
344+
|| defined(HAVE_SSL_CLIENT_HELLO_CB_SUPPORT)
345+
346+
return NGX_AGAIN;
347+
361348
#else
362-
ngx_log_error(NGX_LOG_CRIT, c->log, 0,
363-
"lua: cannot yield in sess get cb: "
364-
"missing async sess get cb support in OpenSSL");
349+
res = NGX_AGAIN;
365350

366351
/* fall through to the "failed" label below */
367352
#endif
@@ -376,10 +361,125 @@ ngx_http_lua_ssl_sess_fetch_handler(ngx_ssl_conn_t *ssl_conn,
376361
ngx_http_lua_close_fake_connection(fc);
377362
}
378363

364+
return res;
365+
}
366+
367+
368+
#ifdef HAVE_SSL_CLIENT_HELLO_CB_SUPPORT
369+
int
370+
ngx_http_lua_ssl_client_hello_handler(ngx_ssl_conn_t *ssl_conn,
371+
int *al, void *arg)
372+
{
373+
int len;
374+
ngx_int_t rc;
375+
const u_char *id;
376+
377+
len = SSL_client_hello_get0_session_id(ssl_conn, &id);
378+
379+
if (len <= 0) {
380+
return SSL_CLIENT_HELLO_SUCCESS;
381+
}
382+
383+
rc = ngx_http_lua_ssl_sess_fetch_helper(ssl_conn, id, len);
384+
385+
if (rc == NGX_AGAIN) {
386+
return SSL_CLIENT_HELLO_RETRY;
387+
}
388+
389+
return SSL_CLIENT_HELLO_SUCCESS;
390+
}
391+
392+
393+
ngx_ssl_session_t *
394+
ngx_http_lua_ssl_sess_fetch_handler(ngx_ssl_conn_t *ssl_conn,
395+
const u_char *id, int len, int *copy)
396+
{
397+
ngx_connection_t *c;
398+
ngx_http_lua_ssl_ctx_t *cctx;
399+
400+
/* set copy to 0 as we expect OpenSSL to handle
401+
* the memory of returned session */
402+
403+
*copy = 0;
404+
405+
c = ngx_ssl_get_connection(ssl_conn);
406+
407+
cctx = ngx_http_lua_ssl_get_ctx(c->ssl->connection);
408+
409+
if (cctx && cctx->done) {
410+
return cctx->session;
411+
}
412+
379413
return NULL;
380414
}
381415

382416

417+
#else
418+
419+
/* cached session fetching callback to be set with SSL_CTX_sess_set_get_cb */
420+
ngx_ssl_session_t *
421+
ngx_http_lua_ssl_sess_fetch_handler(ngx_ssl_conn_t *ssl_conn,
422+
#if OPENSSL_VERSION_NUMBER >= 0x10100003L
423+
const
424+
#endif
425+
u_char *id, int len, int *copy)
426+
{
427+
ngx_int_t rc;
428+
ngx_connection_t *c;
429+
ngx_http_lua_ssl_ctx_t *cctx;
430+
431+
/* set copy to 0 as we expect OpenSSL to handle
432+
* the memory of returned session */
433+
434+
*copy = 0;
435+
436+
c = ngx_ssl_get_connection(ssl_conn);
437+
438+
rc = ngx_http_lua_ssl_sess_fetch_helper(ssl_conn, id, len);
439+
440+
if (rc == NGX_AGAIN) {
441+
442+
#ifdef SSL_ERROR_PENDING_SESSION
443+
444+
return SSL_magic_pending_session_ptr();
445+
446+
#else
447+
448+
ngx_log_error(NGX_LOG_CRIT, c->log, 0,
449+
"lua: cannot yield in sess get cb: "
450+
# if OPENSSL_VERSION_NUMBER >= 0x1010100fL
451+
"missing support for yielding during SSL handshake in "
452+
"the nginx core; consider using the OpenResty releases "
453+
"from https://openresty.org/en/download.html or apply "
454+
"the nginx core patches yourself (see "
455+
"https://openresty.org/en/nginx-ssl-patches.html)");
456+
457+
# else
458+
"missing support for yielding during SSL handshake in "
459+
"linked " OPENSSL_VERSION_TEXT "; consider using the "
460+
"OpenResty releases from "
461+
"https://openresty.org/en/download.html or apply "
462+
"the OpenSSL patches yourself (see "
463+
"https://openresty.org/en/openssl-patches.html)");
464+
# endif
465+
466+
return NULL;
467+
468+
#endif
469+
}
470+
471+
if (rc == NGX_ERROR) {
472+
return NULL;
473+
}
474+
475+
/* rc == NGX_OK */
476+
477+
cctx = ngx_http_lua_ssl_get_ctx(c->ssl->connection);
478+
return cctx->session;
479+
}
480+
#endif
481+
482+
383483
static void
384484
ngx_http_lua_ssl_sess_fetch_done(void *data)
385485
{

src/ngx_http_lua_ssl_session_fetchby.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ ngx_ssl_session_t *ngx_http_lua_ssl_sess_fetch_handler(
3030
const
3131
#endif
3232
u_char *id, int len, int *copy);
33+
34+
#ifdef HAVE_SSL_CLIENT_HELLO_CB_SUPPORT
35+
int ngx_http_lua_ssl_client_hello_handler(ngx_ssl_conn_t *ssl_conn,
36+
int *al, void *arg);
3337
#endif
3438

39+
#endif /* NGX_HTTP_SSL */
3540

3641
#endif /* _NGX_HTTP_LUA_SSL_SESSION_FETCHBY_H_INCLUDED_ */
3742

0 commit comments

Comments
 (0)