@@ -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+
383483static void
384484ngx_http_lua_ssl_sess_fetch_done (void * data )
385485{
0 commit comments