2424
2525static void ngx_stream_lua_proxy_ssl_verify_done (void * data );
2626static void ngx_stream_lua_proxy_ssl_verify_aborted (void * data );
27- static u_char * ngx_stream_lua_log_proxy_ssl_verify_error (ngx_log_t * log ,
28- u_char * buf , size_t len );
2927static ngx_int_t ngx_stream_lua_proxy_ssl_verify_by_chunk (lua_State * L ,
3028 ngx_stream_lua_request_t * r );
3129
@@ -254,20 +252,20 @@ ngx_stream_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
254252{
255253 lua_State * L ;
256254 ngx_int_t rc ;
257- ngx_connection_t * c , * fc ;
255+ ngx_connection_t * c ;
258256 ngx_stream_lua_request_t * r = NULL ;
259257 ngx_pool_cleanup_t * cln ;
260258 ngx_stream_lua_srv_conf_t * lscf ;
261259 ngx_stream_lua_ctx_t * ctx ;
262260 ngx_stream_lua_ssl_ctx_t * cctx ;
263261 ngx_stream_core_srv_conf_t * cscf ;
264- ngx_stream_session_t * s , * fs ;
262+ ngx_stream_session_t * s ;
265263 ngx_ssl_conn_t * ssl_conn ;
266264
267265 ssl_conn = X509_STORE_CTX_get_ex_data (x509_store ,
268266 SSL_get_ex_data_X509_STORE_CTX_idx ());
269267
270- c = ngx_ssl_get_connection (ssl_conn ); /* connection to upstream */
268+ c = ngx_ssl_get_connection (ssl_conn ); /* upstream connection */
271269
272270 ngx_log_debug1 (NGX_LOG_DEBUG_STREAM , c -> log , 0 ,
273271 "proxy ssl verify: connection reusable: %ud" , c -> reusable );
@@ -298,51 +296,11 @@ ngx_stream_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
298296
299297 s = c -> data ;
300298
301- fc = ngx_stream_lua_create_fake_connection (NULL );
302- if (fc == NULL ) {
303- goto failed ;
304- }
305-
306- fc -> log -> handler = ngx_stream_lua_log_proxy_ssl_verify_error ;
307- fc -> log -> data = fc ;
308-
309- fc -> addr_text = c -> addr_text ;
310- fc -> listening = c -> listening ;
311-
312- fs = ngx_stream_lua_create_fake_session (fc );
313- if (fs == NULL ) {
314- goto failed ;
315- }
316-
317- fs -> main_conf = s -> main_conf ;
318- fs -> srv_conf = s -> srv_conf ;
319- /*
320- * so that we can use ngx.ctx to pass data from downstream phases to
321- * upstream phases if there is any
322- */
323- ctx = ngx_stream_get_module_ctx (s , ngx_stream_lua_module );
324- if (ctx ) {
325- ngx_stream_set_ctx (fs , ctx , ngx_stream_lua_module );
326- }
327-
328- r = ngx_stream_lua_create_fake_request (fs );
299+ r = ngx_stream_lua_create_fake_request (s );
329300 if (r == NULL ) {
330301 goto failed ;
331302 }
332303
333- fc -> log -> file = c -> log -> file ;
334- fc -> log -> log_level = c -> log -> log_level ;
335- fc -> ssl = c -> ssl ;
336-
337- cscf = ngx_stream_get_module_srv_conf (fs , ngx_stream_core_module );
338-
339- #if defined(nginx_version ) && nginx_version >= 1027001
340- ngx_set_connection_log (fc , cscf -> error_log );
341-
342- #else
343- #error "stream proxy_ssl_verify_by_lua only supports nginx >= 1.27.1"
344- #endif
345-
346304 if (cctx == NULL ) {
347305 cctx = ngx_pcalloc (c -> pool , sizeof (ngx_stream_lua_ssl_ctx_t ));
348306 if (cctx == NULL ) {
@@ -352,12 +310,16 @@ ngx_stream_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
352310 cctx -> ctx_ref = LUA_NOREF ;
353311 }
354312
355- cctx -> exit_code = 1 ; /* successful by default */
356- cctx -> x509_store = x509_store ;
357313 cctx -> connection = c ;
358314 cctx -> request = r ;
359- cctx -> entered_proxy_ssl_verify_handler = 1 ;
315+ cctx -> x509_store = x509_store ;
316+ cctx -> exit_code = 1 ; /* successful by default */
360317 cctx -> done = 0 ;
318+ cctx -> entered_proxy_ssl_verify_handler = 1 ;
319+ cctx -> pool = ngx_create_pool (128 , c -> log );
320+ if (cctx -> pool == NULL ) {
321+ goto failed ;
322+ }
361323
362324 dd ("setting cctx" );
363325
@@ -386,6 +348,8 @@ ngx_stream_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
386348 c -> log -> action = "loading proxy ssl verify by lua" ;
387349
388350 if (lscf -> ups .proxy_ssl_verify_handler == NULL ) {
351+ cscf = ngx_stream_get_module_srv_conf (s , ngx_stream_core_module );
352+
389353 ngx_log_error (NGX_LOG_ALERT , c -> log , 0 ,
390354 "no proxy_ssl_verify_by_lua* defined in "
391355 "server %s:%ui" , & cscf -> file_name , & cscf -> line );
@@ -412,7 +376,7 @@ ngx_stream_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
412376
413377 /* rc == NGX_DONE */
414378
415- cln = ngx_pool_cleanup_add (fc -> pool , 0 );
379+ cln = ngx_pool_cleanup_add (cctx -> pool , 0 );
416380 if (cln == NULL ) {
417381 goto failed ;
418382 }
@@ -437,12 +401,8 @@ ngx_stream_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
437401#if 1
438402failed :
439403
440- if (r && r -> pool ) {
441- ngx_stream_lua_free_fake_request (r );
442- }
443-
444- if (fc ) {
445- ngx_stream_lua_close_fake_connection (fc );
404+ if (cctx && cctx -> pool ) {
405+ ngx_destroy_pool (cctx -> pool );
446406 }
447407
448408 return 0 ; /* verify failure or error */
@@ -472,6 +432,14 @@ ngx_stream_lua_proxy_ssl_verify_done(void *data)
472432
473433 c = cctx -> connection ;
474434
435+ if (c -> read -> timer_set ) {
436+ ngx_del_timer (c -> read );
437+ }
438+
439+ if (c -> write -> timer_set ) {
440+ ngx_del_timer (c -> write );
441+ }
442+
475443 c -> log -> action = "proxy pass SSL handshaking" ;
476444
477445 ngx_post_event (c -> write , & ngx_posted_events );
@@ -493,45 +461,10 @@ ngx_stream_lua_proxy_ssl_verify_aborted(void *data)
493461 ngx_log_debug0 (NGX_LOG_DEBUG_STREAM , cctx -> connection -> log , 0 ,
494462 "proxy_ssl_verify_by_lua: cert verify callback aborted" );
495463
496- cctx -> aborted = 1 ;
497- cctx -> request -> connection -> ssl = NULL ;
498-
499- ngx_stream_lua_finalize_fake_request (cctx -> request , NGX_ERROR );
500- }
501-
502-
503- static u_char *
504- ngx_stream_lua_log_proxy_ssl_verify_error (ngx_log_t * log , u_char * buf ,
505- size_t len )
506- {
507- u_char * p ;
508- ngx_connection_t * c ;
509-
510- if (log -> action ) {
511- p = ngx_snprintf (buf , len , " while %s" , log -> action );
512- len -= p - buf ;
513- buf = p ;
514- }
515-
516- p = ngx_snprintf (buf , len , ", context: proxy_ssl_verify_by_lua*" );
517- len -= p - buf ;
518- buf = p ;
519-
520- c = log -> data ;
521-
522- if (c && c -> addr_text .len ) {
523- p = ngx_snprintf (buf , len , ", client: %V" , & c -> addr_text );
524- len -= p - buf ;
525- buf = p ;
526- }
527-
528- if (c && c -> listening && c -> listening -> addr_text .len ) {
529- p = ngx_snprintf (buf , len , ", server: %V" , & c -> listening -> addr_text );
530- /* len -= p - buf; */
531- buf = p ;
532- }
464+ ngx_stream_lua_finalize_request (cctx -> request , NGX_ERROR );
533465
534- return buf ;
466+ cctx -> aborted = 1 ;
467+ cctx -> connection -> ssl = NULL ;
535468}
536469
537470
@@ -544,6 +477,9 @@ ngx_stream_lua_proxy_ssl_verify_by_chunk(lua_State *L,
544477 lua_State * co ;
545478 ngx_stream_lua_ctx_t * ctx ;
546479 ngx_stream_lua_cleanup_t * cln ;
480+ ngx_stream_upstream_t * u ;
481+ ngx_connection_t * c ;
482+ ngx_stream_lua_ssl_ctx_t * cctx ;
547483
548484 ctx = ngx_stream_lua_get_module_ctx (r , ngx_stream_lua_module );
549485
@@ -598,7 +534,11 @@ ngx_stream_lua_proxy_ssl_verify_by_chunk(lua_State *L,
598534
599535 /* register request cleanup hooks */
600536 if (ctx -> cleanup == NULL ) {
601- cln = ngx_stream_lua_cleanup_add (r , 0 );
537+ u = r -> session -> upstream ;
538+ c = u -> peer .connection ;
539+ cctx = ngx_stream_lua_ssl_get_ctx (c -> ssl -> connection );
540+
541+ cln = ngx_pool_cleanup_add (cctx -> pool , 0 );
602542 if (cln == NULL ) {
603543 rc = NGX_ERROR ;
604544 ngx_stream_lua_finalize_request (r , rc );
@@ -643,17 +583,25 @@ ngx_stream_lua_ffi_ssl_set_verify_result(ngx_stream_lua_request_t *r,
643583 int verify_result , char * * err )
644584{
645585#ifdef SSL_ERROR_WANT_RETRY_VERIFY
586+ ngx_stream_upstream_t * u ;
646587 ngx_ssl_conn_t * ssl_conn ;
647588 ngx_connection_t * c ;
648589 ngx_stream_lua_ssl_ctx_t * cctx ;
649590 X509_STORE_CTX * x509_store ;
650591
651- if (r -> connection == NULL || r -> connection -> ssl == NULL ) {
592+ u = r -> session -> upstream ;
593+ if (u == NULL ) {
652594 * err = "bad request" ;
653595 return NGX_ERROR ;
654596 }
655597
656- ssl_conn = r -> connection -> ssl -> connection ;
598+ c = u -> peer .connection ;
599+ if (c == NULL || c -> ssl == NULL ) {
600+ * err = "bad upstream connection" ;
601+ return NGX_ERROR ;
602+ }
603+
604+ ssl_conn = c -> ssl -> connection ;
657605 if (ssl_conn == NULL ) {
658606 * err = "bad ssl conn" ;
659607 return NGX_ERROR ;
@@ -687,17 +635,25 @@ ngx_stream_lua_ffi_ssl_get_verify_result(ngx_stream_lua_request_t *r,
687635 char * * err )
688636{
689637#ifdef SSL_ERROR_WANT_RETRY_VERIFY
638+ ngx_stream_upstream_t * u ;
690639 ngx_ssl_conn_t * ssl_conn ;
691640 ngx_connection_t * c ;
692641 ngx_stream_lua_ssl_ctx_t * cctx ;
693642 X509_STORE_CTX * x509_store ;
694643
695- if (r -> connection == NULL || r -> connection -> ssl == NULL ) {
644+ u = r -> session -> upstream ;
645+ if (u == NULL ) {
696646 * err = "bad request" ;
697647 return NGX_ERROR ;
698648 }
699649
700- ssl_conn = r -> connection -> ssl -> connection ;
650+ c = u -> peer .connection ;
651+ if (c == NULL || c -> ssl == NULL ) {
652+ * err = "bad upstream connection" ;
653+ return NGX_ERROR ;
654+ }
655+
656+ ssl_conn = c -> ssl -> connection ;
701657 if (ssl_conn == NULL ) {
702658 * err = "bad ssl conn" ;
703659 return NGX_ERROR ;
@@ -737,18 +693,26 @@ void *
737693ngx_stream_lua_ffi_ssl_get_verify_cert (ngx_stream_lua_request_t * r , char * * err )
738694{
739695#ifdef SSL_ERROR_WANT_RETRY_VERIFY
696+ ngx_stream_upstream_t * u ;
740697 ngx_ssl_conn_t * ssl_conn ;
741698 ngx_connection_t * c ;
742699 ngx_stream_lua_ssl_ctx_t * cctx ;
743700 X509_STORE_CTX * x509_store ;
744701 X509 * x509 ;
745702
746- if (r -> connection == NULL || r -> connection -> ssl == NULL ) {
703+ u = r -> session -> upstream ;
704+ if (u == NULL ) {
747705 * err = "bad request" ;
748706 return NULL ;
749707 }
750708
751- ssl_conn = r -> connection -> ssl -> connection ;
709+ c = u -> peer .connection ;
710+ if (c == NULL || c -> ssl == NULL ) {
711+ * err = "bad upstream connection" ;
712+ return NULL ;
713+ }
714+
715+ ssl_conn = c -> ssl -> connection ;
752716 if (ssl_conn == NULL ) {
753717 * err = "bad ssl conn" ;
754718 return NULL ;
0 commit comments