23
23
24
24
static void ngx_http_lua_proxy_ssl_verify_done (void * data );
25
25
static void ngx_http_lua_proxy_ssl_verify_aborted (void * data );
26
- static u_char * ngx_http_lua_log_proxy_ssl_verify_error (ngx_log_t * log ,
27
- u_char * buf , size_t len );
28
26
static ngx_int_t ngx_http_lua_proxy_ssl_verify_by_chunk (lua_State * L ,
29
27
ngx_http_request_t * r );
30
28
@@ -239,10 +237,9 @@ ngx_http_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
239
237
{
240
238
lua_State * L ;
241
239
ngx_int_t rc ;
242
- ngx_connection_t * c , * fc ;
243
- ngx_http_request_t * r = NULL , * fr = NULL ;
240
+ ngx_connection_t * c ;
241
+ ngx_http_request_t * r = NULL ;
244
242
ngx_pool_cleanup_t * cln ;
245
- ngx_http_core_loc_conf_t * clcf ;
246
243
ngx_http_lua_loc_conf_t * llcf ;
247
244
ngx_http_lua_ctx_t * ctx ;
248
245
ngx_http_lua_ssl_ctx_t * cctx ;
@@ -252,7 +249,7 @@ ngx_http_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
252
249
ssl_conn = X509_STORE_CTX_get_ex_data (x509_store ,
253
250
SSL_get_ex_data_X509_STORE_CTX_idx ());
254
251
255
- c = ngx_ssl_get_connection (ssl_conn ); /* connection to upstream */
252
+ c = ngx_ssl_get_connection (ssl_conn ); /* upstream connection */
256
253
257
254
ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , c -> log , 0 ,
258
255
"proxy ssl verify: connection reusable: %ud" , c -> reusable );
@@ -285,51 +282,6 @@ ngx_http_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
285
282
286
283
r = c -> data ;
287
284
288
- fc = ngx_http_lua_create_fake_connection (NULL );
289
- if (fc == NULL ) {
290
- goto failed ;
291
- }
292
-
293
- fc -> log -> handler = ngx_http_lua_log_proxy_ssl_verify_error ;
294
- fc -> log -> data = fc ;
295
-
296
- fc -> addr_text = c -> addr_text ;
297
- fc -> listening = c -> listening ;
298
-
299
- fr = ngx_http_lua_create_fake_request (fc );
300
- if (fr == NULL ) {
301
- goto failed ;
302
- }
303
-
304
- cscf = ngx_http_get_module_srv_conf (r , ngx_http_core_module );
305
- fr -> main_conf = cscf -> ctx -> main_conf ;
306
- fr -> srv_conf = cscf -> ctx -> srv_conf ;
307
- /*
308
- * the hook is running after find config phase, and r->loc_conf may
309
- * already been changed, we need to get correct location configs
310
- */
311
- fr -> loc_conf = r -> loc_conf ;
312
- /*
313
- * so that we can use ngx.ctx to pass data from downstream phases to
314
- * upstream phases if there is any
315
- */
316
- ctx = ngx_http_get_module_ctx (r , ngx_http_lua_module );
317
- if (ctx ) {
318
- ngx_http_set_ctx (fr , ctx , ngx_http_lua_module );
319
- }
320
-
321
- fc -> log -> file = c -> log -> file ;
322
- fc -> log -> log_level = c -> log -> log_level ;
323
- fc -> ssl = c -> ssl ;
324
-
325
- clcf = ngx_http_get_module_loc_conf (fr , ngx_http_core_module );
326
-
327
- #if nginx_version >= 1009000
328
- ngx_set_connection_log (fc , clcf -> error_log );
329
- #else
330
- ngx_http_set_connection_log (fc , clcf -> error_log );
331
- #endif
332
-
333
285
if (cctx == NULL ) {
334
286
cctx = ngx_pcalloc (c -> pool , sizeof (ngx_http_lua_ssl_ctx_t ));
335
287
if (cctx == NULL ) {
@@ -339,12 +291,17 @@ ngx_http_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
339
291
cctx -> ctx_ref = LUA_NOREF ;
340
292
}
341
293
342
- cctx -> exit_code = 1 ; /* successful by default */
343
- cctx -> x509_store = x509_store ;
344
294
cctx -> connection = c ;
345
- cctx -> request = fr ;
346
- cctx -> entered_proxy_ssl_verify_handler = 1 ;
295
+ cctx -> request = r ;
296
+ cctx -> x509_store = x509_store ;
297
+ cctx -> exit_code = 1 ; /* successful by default */
298
+ cctx -> original_request_count = r -> main -> count ;
347
299
cctx -> done = 0 ;
300
+ cctx -> entered_proxy_ssl_verify_handler = 1 ;
301
+ cctx -> pool = ngx_create_pool (128 , c -> log );
302
+ if (cctx -> pool == NULL ) {
303
+ goto failed ;
304
+ }
348
305
349
306
dd ("setting cctx" );
350
307
@@ -355,7 +312,7 @@ ngx_http_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
355
312
goto failed ;
356
313
}
357
314
358
- llcf = ngx_http_get_module_loc_conf (fr , ngx_http_lua_module );
315
+ llcf = ngx_http_get_module_loc_conf (r , ngx_http_lua_module );
359
316
if (llcf -> upstream_skip_openssl_default_verify == 0 ) {
360
317
ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , c -> log , 0 ,
361
318
"proxy_ssl_verify_by_lua: openssl default verify" );
@@ -367,19 +324,20 @@ ngx_http_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
367
324
}
368
325
369
326
/* TODO honor lua_code_cache off */
370
- L = ngx_http_lua_get_lua_vm (fr , NULL );
327
+ L = ngx_http_lua_get_lua_vm (r , NULL );
371
328
372
329
c -> log -> action = "loading proxy ssl verify by lua" ;
373
330
374
331
if (llcf -> proxy_ssl_verify_handler == NULL ) {
332
+ cscf = ngx_http_get_module_srv_conf (r , ngx_http_core_module );
375
333
ngx_log_error (NGX_LOG_ALERT , c -> log , 0 ,
376
334
"no proxy_ssl_verify_by_lua* defined in "
377
335
"server %V" , & cscf -> server_name );
378
336
379
337
goto failed ;
380
338
}
381
339
382
- rc = llcf -> proxy_ssl_verify_handler (fr , llcf , L );
340
+ rc = llcf -> proxy_ssl_verify_handler (r , llcf , L );
383
341
384
342
if (rc >= NGX_OK || rc == NGX_ERROR ) {
385
343
cctx -> done = 1 ;
@@ -398,7 +356,7 @@ ngx_http_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
398
356
399
357
/* rc == NGX_DONE */
400
358
401
- cln = ngx_pool_cleanup_add (fc -> pool , 0 );
359
+ cln = ngx_pool_cleanup_add (cctx -> pool , 0 );
402
360
if (cln == NULL ) {
403
361
goto failed ;
404
362
}
@@ -421,13 +379,8 @@ ngx_http_lua_proxy_ssl_verify_handler(X509_STORE_CTX *x509_store, void *arg)
421
379
return SSL_set_retry_verify (ssl_conn );
422
380
423
381
failed :
424
-
425
- if (fr && fr -> pool ) {
426
- ngx_http_lua_free_fake_request (fr );
427
- }
428
-
429
- if (fc ) {
430
- ngx_http_lua_close_fake_connection (fc );
382
+ if (cctx && cctx -> pool ) {
383
+ ngx_destroy_pool (cctx -> pool );
431
384
}
432
385
433
386
return 0 ; /* verify failure or error */
@@ -456,6 +409,14 @@ ngx_http_lua_proxy_ssl_verify_done(void *data)
456
409
457
410
c = cctx -> connection ;
458
411
412
+ if (c -> read -> timer_set ) {
413
+ ngx_del_timer (c -> read );
414
+ }
415
+
416
+ if (c -> write -> timer_set ) {
417
+ ngx_del_timer (c -> write );
418
+ }
419
+
459
420
c -> log -> action = "proxy pass SSL handshaking" ;
460
421
461
422
ngx_post_event (c -> write , & ngx_posted_events );
@@ -477,45 +438,10 @@ ngx_http_lua_proxy_ssl_verify_aborted(void *data)
477
438
ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , cctx -> connection -> log , 0 ,
478
439
"proxy_ssl_verify_by_lua: cert verify callback aborted" );
479
440
480
- cctx -> aborted = 1 ;
481
- cctx -> request -> connection -> ssl = NULL ;
482
-
483
- ngx_http_lua_finalize_fake_request (cctx -> request , NGX_ERROR );
484
- }
485
-
486
-
487
- static u_char *
488
- ngx_http_lua_log_proxy_ssl_verify_error (ngx_log_t * log ,
489
- u_char * buf , size_t len )
490
- {
491
- u_char * p ;
492
- ngx_connection_t * c ;
493
-
494
- if (log -> action ) {
495
- p = ngx_snprintf (buf , len , " while %s" , log -> action );
496
- len -= p - buf ;
497
- buf = p ;
498
- }
499
-
500
- p = ngx_snprintf (buf , len , ", context: proxy_ssl_verify_by_lua*" );
501
- len -= p - buf ;
502
- buf = p ;
441
+ ngx_http_lua_finalize_request (cctx -> request , NGX_ERROR );
503
442
504
- c = log -> data ;
505
-
506
- if (c && c -> addr_text .len ) {
507
- p = ngx_snprintf (buf , len , ", client: %V" , & c -> addr_text );
508
- len -= p - buf ;
509
- buf = p ;
510
- }
511
-
512
- if (c && c -> listening && c -> listening -> addr_text .len ) {
513
- p = ngx_snprintf (buf , len , ", server: %V" , & c -> listening -> addr_text );
514
- /* len -= p - buf; */
515
- buf = p ;
516
- }
517
-
518
- return buf ;
443
+ cctx -> aborted = 1 ;
444
+ cctx -> connection -> ssl = NULL ;
519
445
}
520
446
521
447
@@ -527,6 +453,9 @@ ngx_http_lua_proxy_ssl_verify_by_chunk(lua_State *L, ngx_http_request_t *r)
527
453
lua_State * co ;
528
454
ngx_http_lua_ctx_t * ctx ;
529
455
ngx_pool_cleanup_t * cln ;
456
+ ngx_http_upstream_t * u ;
457
+ ngx_connection_t * c ;
458
+ ngx_http_lua_ssl_ctx_t * cctx ;
530
459
531
460
ctx = ngx_http_get_module_ctx (r , ngx_http_lua_module );
532
461
@@ -580,7 +509,11 @@ ngx_http_lua_proxy_ssl_verify_by_chunk(lua_State *L, ngx_http_request_t *r)
580
509
581
510
/* register request cleanup hooks */
582
511
if (ctx -> cleanup == NULL ) {
583
- cln = ngx_pool_cleanup_add (r -> pool , 0 );
512
+ u = r -> upstream ;
513
+ c = u -> peer .connection ;
514
+ cctx = ngx_http_lua_ssl_get_ctx (c -> ssl -> connection );
515
+
516
+ cln = ngx_pool_cleanup_add (cctx -> pool , 0 );
584
517
if (cln == NULL ) {
585
518
rc = NGX_ERROR ;
586
519
ngx_http_lua_finalize_request (r , rc );
@@ -625,17 +558,25 @@ ngx_http_lua_ffi_ssl_set_verify_result(ngx_http_request_t *r,
625
558
int verify_result , char * * err )
626
559
{
627
560
#ifdef SSL_ERROR_WANT_RETRY_VERIFY
561
+ ngx_http_upstream_t * u ;
628
562
ngx_ssl_conn_t * ssl_conn ;
629
563
ngx_connection_t * c ;
630
564
ngx_http_lua_ssl_ctx_t * cctx ;
631
565
X509_STORE_CTX * x509_store ;
632
566
633
- if (r -> connection == NULL || r -> connection -> ssl == NULL ) {
567
+ u = r -> upstream ;
568
+ if (u == NULL ) {
634
569
* err = "bad request" ;
635
570
return NGX_ERROR ;
636
571
}
637
572
638
- ssl_conn = r -> connection -> ssl -> connection ;
573
+ c = u -> peer .connection ;
574
+ if (c == NULL || c -> ssl == NULL ) {
575
+ * err = "bad upstream connection" ;
576
+ return NGX_ERROR ;
577
+ }
578
+
579
+ ssl_conn = c -> ssl -> connection ;
639
580
if (ssl_conn == NULL ) {
640
581
* err = "bad ssl conn" ;
641
582
return NGX_ERROR ;
@@ -668,17 +609,25 @@ int
668
609
ngx_http_lua_ffi_ssl_get_verify_result (ngx_http_request_t * r , char * * err )
669
610
{
670
611
#ifdef SSL_ERROR_WANT_RETRY_VERIFY
612
+ ngx_http_upstream_t * u ;
671
613
ngx_ssl_conn_t * ssl_conn ;
672
614
ngx_connection_t * c ;
673
615
ngx_http_lua_ssl_ctx_t * cctx ;
674
616
X509_STORE_CTX * x509_store ;
675
617
676
- if (r -> connection == NULL || r -> connection -> ssl == NULL ) {
618
+ u = r -> upstream ;
619
+ if (u == NULL ) {
677
620
* err = "bad request" ;
678
621
return NGX_ERROR ;
679
622
}
680
623
681
- ssl_conn = r -> connection -> ssl -> connection ;
624
+ c = u -> peer .connection ;
625
+ if (c == NULL || c -> ssl == NULL ) {
626
+ * err = "bad upstream connection" ;
627
+ return NGX_ERROR ;
628
+ }
629
+
630
+ ssl_conn = c -> ssl -> connection ;
682
631
if (ssl_conn == NULL ) {
683
632
* err = "bad ssl conn" ;
684
633
return NGX_ERROR ;
@@ -718,18 +667,26 @@ void *
718
667
ngx_http_lua_ffi_ssl_get_verify_cert (ngx_http_request_t * r , char * * err )
719
668
{
720
669
#ifdef SSL_ERROR_WANT_RETRY_VERIFY
670
+ ngx_http_upstream_t * u ;
721
671
ngx_ssl_conn_t * ssl_conn ;
722
672
ngx_connection_t * c ;
723
673
ngx_http_lua_ssl_ctx_t * cctx ;
724
674
X509_STORE_CTX * x509_store ;
725
675
X509 * x509 ;
726
676
727
- if (r -> connection == NULL || r -> connection -> ssl == NULL ) {
677
+ u = r -> upstream ;
678
+ if (u == NULL ) {
728
679
* err = "bad request" ;
729
680
return NULL ;
730
681
}
731
682
732
- ssl_conn = r -> connection -> ssl -> connection ;
683
+ c = u -> peer .connection ;
684
+ if (c == NULL || c -> ssl == NULL ) {
685
+ * err = "bad upstream connection" ;
686
+ return NULL ;
687
+ }
688
+
689
+ ssl_conn = c -> ssl -> connection ;
733
690
if (ssl_conn == NULL ) {
734
691
* err = "bad ssl conn" ;
735
692
return NULL ;
0 commit comments