21
21
#include "ngx_http_lua_ssl.h"
22
22
23
23
24
+ #ifdef OPENSSL_IS_BORINGSSL
25
+ #define NGX_HTTP_LUA_CLIENT_HELLO_PENDING_STATUS ssl_select_cert_retry
26
+ #else
27
+ #define NGX_HTTP_LUA_CLIENT_HELLO_PENDING_STATUS -1
28
+ #endif
29
+
30
+
24
31
static void ngx_http_lua_ssl_client_hello_done (void * data );
25
32
static void ngx_http_lua_ssl_client_hello_aborted (void * data );
26
33
static u_char * ngx_http_lua_log_ssl_client_hello_error (ngx_log_t * log ,
@@ -96,7 +103,7 @@ char *
96
103
ngx_http_lua_ssl_client_hello_by_lua (ngx_conf_t * cf , ngx_command_t * cmd ,
97
104
void * conf )
98
105
{
99
- #ifndef SSL_ERROR_WANT_CLIENT_HELLO_CB
106
+ #if !defined( SSL_ERROR_WANT_CLIENT_HELLO_CB ) && !defined( OPENSSL_IS_BORINGSSL )
100
107
101
108
ngx_log_error (NGX_LOG_EMERG , cf -> log , 0 ,
102
109
"at least OpenSSL 1.1.1 required but found "
@@ -178,9 +185,14 @@ ngx_http_lua_ssl_client_hello_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
178
185
}
179
186
180
187
188
+ #ifdef OPENSSL_IS_BORINGSSL
189
+ int
190
+ ngx_http_lua_ssl_client_hello_handler (const SSL_CLIENT_HELLO * client_hello )
191
+ #else
181
192
int
182
193
ngx_http_lua_ssl_client_hello_handler (ngx_ssl_conn_t * ssl_conn ,
183
194
int * al , void * arg )
195
+ #endif
184
196
{
185
197
lua_State * L ;
186
198
ngx_int_t rc ;
@@ -193,7 +205,11 @@ ngx_http_lua_ssl_client_hello_handler(ngx_ssl_conn_t *ssl_conn,
193
205
ngx_http_lua_ssl_ctx_t * cctx ;
194
206
ngx_http_core_srv_conf_t * cscf ;
195
207
208
+ #ifdef OPENSSL_IS_BORINGSSL
209
+ c = ngx_ssl_get_connection (client_hello -> ssl );
210
+ #else
196
211
c = ngx_ssl_get_connection (ssl_conn );
212
+ #endif
197
213
198
214
ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , c -> log , 0 ,
199
215
"ssl client hello: connection reusable: %ud" , c -> reusable );
@@ -215,7 +231,7 @@ ngx_http_lua_ssl_client_hello_handler(ngx_ssl_conn_t *ssl_conn,
215
231
return cctx -> exit_code ;
216
232
}
217
233
218
- return -1 ;
234
+ return NGX_HTTP_LUA_CLIENT_HELLO_PENDING_STATUS ;
219
235
}
220
236
221
237
dd ("first time" );
@@ -274,6 +290,10 @@ ngx_http_lua_ssl_client_hello_handler(ngx_ssl_conn_t *ssl_conn,
274
290
cctx -> entered_client_hello_handler = 1 ;
275
291
cctx -> done = 0 ;
276
292
293
+ #ifdef OPENSSL_IS_BORINGSSL
294
+ cctx -> client_hello = client_hello ;
295
+ #endif
296
+
277
297
dd ("setting cctx" );
278
298
279
299
if (SSL_set_ex_data (c -> ssl -> connection , ngx_http_lua_ssl_ctx_index , cctx )
@@ -339,7 +359,7 @@ ngx_http_lua_ssl_client_hello_handler(ngx_ssl_conn_t *ssl_conn,
339
359
340
360
* cctx -> cleanup = ngx_http_lua_ssl_client_hello_aborted ;
341
361
342
- return -1 ;
362
+ return NGX_HTTP_LUA_CLIENT_HELLO_PENDING_STATUS ;
343
363
344
364
#if 1
345
365
failed :
@@ -537,15 +557,67 @@ ngx_http_lua_ssl_client_hello_by_chunk(lua_State *L, ngx_http_request_t *r)
537
557
}
538
558
539
559
560
+ static int
561
+ ngx_http_lua_ssl_client_hello_get_ext (const uint8_t * exts , ngx_int_t exts_size ,
562
+ ngx_int_t target_type , const unsigned char * * out , size_t * out_len ,
563
+ char * * err )
564
+ {
565
+ uint8_t * p , * last ;
566
+ ngx_int_t ext_len , ext_type ;
567
+
568
+ if (err == NULL ) {
569
+ return NGX_ERROR ;
570
+ }
571
+
572
+ if (exts == NULL ) {
573
+ * err = "bad boringssl exts" ;
574
+ return NGX_ERROR ;
575
+ }
576
+
577
+ if (out == NULL || out_len == NULL ) {
578
+ * err = "invalid args" ;
579
+ return NGX_ERROR ;
580
+ }
581
+
582
+ p = (uint8_t * ) exts ;
583
+ last = (uint8_t * ) exts + exts_size ;
584
+
585
+ while (p < last ) {
586
+ ext_type = * p ++ ;
587
+ ext_type = (ext_type << 8 ) + * p ++ ;
588
+ ext_len = * p ++ ;
589
+ ext_len = (ext_len << 8 ) + * p ++ ;
590
+ if (p + ext_len > last ) {
591
+ * err = "invalid boringssl exts" ;
592
+ return NGX_ERROR ;
593
+ }
594
+
595
+ if (ext_type == target_type ) {
596
+ * out = p ;
597
+ * out_len = ext_len ;
598
+ return NGX_OK ;
599
+ }
600
+
601
+ p += ext_len ;
602
+ }
603
+
604
+ /* found nothing */
605
+ return NGX_DECLINED ;
606
+ }
607
+
608
+
540
609
int
541
610
ngx_http_lua_ffi_ssl_get_client_hello_server_name (ngx_http_request_t * r ,
542
611
const char * * name , size_t * namelen , char * * err )
543
612
{
544
- ngx_ssl_conn_t * ssl_conn ;
545
613
#ifdef SSL_ERROR_WANT_CLIENT_HELLO_CB
546
- const unsigned char * p ;
547
- size_t remaining , len ;
614
+ size_t remaining , len ;
615
+ const unsigned char * p ;
616
+ #elif defined(OPENSSL_IS_BORINGSSL )
617
+ size_t remaining ;
618
+ const char * p ;
548
619
#endif
620
+ ngx_ssl_conn_t * ssl_conn ;
549
621
550
622
if (r -> connection == NULL || r -> connection -> ssl == NULL ) {
551
623
* err = "bad request" ;
@@ -560,17 +632,27 @@ ngx_http_lua_ffi_ssl_get_client_hello_server_name(ngx_http_request_t *r,
560
632
561
633
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
562
634
563
- #ifdef SSL_ERROR_WANT_CLIENT_HELLO_CB
635
+ #if defined( SSL_ERROR_WANT_CLIENT_HELLO_CB ) || defined( OPENSSL_IS_BORINGSSL )
564
636
remaining = 0 ;
565
637
638
+ #ifdef OPENSSL_IS_BORINGSSL
639
+ p = SSL_get_servername (ssl_conn , TLSEXT_NAMETYPE_host_name );
640
+ if (p == NULL ) {
641
+ return NGX_DECLINED ;
642
+ }
643
+ remaining = ngx_strlen (p );
644
+
645
+ #else
566
646
/* This code block is taken from OpenSSL's client_hello_select_server_ctx()
567
647
* */
568
648
if (!SSL_client_hello_get0_ext (ssl_conn , TLSEXT_TYPE_server_name , & p ,
569
649
& remaining ))
570
650
{
571
651
return NGX_DECLINED ;
572
652
}
653
+ #endif
573
654
655
+ #ifndef OPENSSL_IS_BORINGSSL
574
656
if (remaining <= 2 ) {
575
657
* err = "Bad SSL Client Hello Extension" ;
576
658
return NGX_ERROR ;
@@ -603,8 +685,10 @@ ngx_http_lua_ffi_ssl_get_client_hello_server_name(ngx_http_request_t *r,
603
685
}
604
686
605
687
remaining = len ;
688
+ #endif
689
+
606
690
* name = (const char * ) p ;
607
- * namelen = len ;
691
+ * namelen = remaining ;
608
692
609
693
return NGX_OK ;
610
694
@@ -627,6 +711,11 @@ ngx_http_lua_ffi_ssl_get_client_hello_ext(ngx_http_request_t *r,
627
711
unsigned int type , const unsigned char * * out , size_t * outlen , char * * err )
628
712
{
629
713
ngx_ssl_conn_t * ssl_conn ;
714
+ #ifdef OPENSSL_IS_BORINGSSL
715
+ ngx_int_t rc ;
716
+ const SSL_CLIENT_HELLO * client_hello ;
717
+ ngx_http_lua_ssl_ctx_t * cctx ;
718
+ #endif
630
719
631
720
if (r -> connection == NULL || r -> connection -> ssl == NULL ) {
632
721
* err = "bad request" ;
@@ -645,6 +734,23 @@ ngx_http_lua_ffi_ssl_get_client_hello_ext(ngx_http_request_t *r,
645
734
}
646
735
647
736
return NGX_OK ;
737
+ #elif defined(OPENSSL_IS_BORINGSSL )
738
+ cctx = ngx_http_lua_ssl_get_ctx (r -> connection -> ssl -> connection );
739
+ if (cctx == NULL ) {
740
+ * err = "bad lua ssl ctx" ;
741
+ return NGX_ERROR ;
742
+ }
743
+
744
+ if (cctx -> client_hello == NULL ) {
745
+ * err = "bad boringssl client hello ctx" ;
746
+ return NGX_ERROR ;
747
+ }
748
+
749
+ client_hello = cctx -> client_hello ;
750
+ rc = ngx_http_lua_ssl_client_hello_get_ext (client_hello -> extensions ,
751
+ client_hello -> extensions_len ,
752
+ type , out , outlen , err );
753
+ return rc ;
648
754
#else
649
755
* err = "OpenSSL too old to support this function" ;
650
756
return NGX_ERROR ;
0 commit comments