22
22
static int ngx_http_lua_socket_tcp (lua_State * L );
23
23
static int ngx_http_lua_socket_tcp_connect (lua_State * L );
24
24
#if (NGX_HTTP_SSL )
25
- static int ngx_http_lua_socket_tcp_sslhandshake (lua_State * L );
26
25
static void ngx_http_lua_tls_handshake_handler (ngx_connection_t * c );
27
26
static int ngx_http_lua_tls_handshake_retval_handler (ngx_http_request_t * r ,
28
27
ngx_http_lua_socket_tcp_upstream_t * u , lua_State * L );
@@ -213,9 +212,6 @@ static char ngx_http_lua_upstream_udata_metatable_key;
213
212
static char ngx_http_lua_downstream_udata_metatable_key ;
214
213
static char ngx_http_lua_pool_udata_metatable_key ;
215
214
static char ngx_http_lua_pattern_udata_metatable_key ;
216
- #if (NGX_HTTP_SSL )
217
- static char ngx_http_lua_ssl_session_metatable_key ;
218
- #endif
219
215
220
216
221
217
void
@@ -1535,13 +1531,16 @@ int
1535
1531
ngx_http_lua_ffi_socket_tcp_tlshandshake (ngx_http_request_t * r ,
1536
1532
ngx_http_lua_socket_tcp_upstream_t * u , ngx_ssl_session_t * sess ,
1537
1533
int enable_session_reuse , ngx_str_t * server_name , int verify ,
1538
- int ocsp_status_req , const char * * errmsg )
1534
+ int ocsp_status_req , STACK_OF (X509 ) * chain , EVP_PKEY * pkey ,
1535
+ const char * * errmsg )
1539
1536
{
1540
- ngx_int_t rc ;
1537
+ ngx_int_t rc , i ;
1541
1538
ngx_connection_t * c ;
1542
1539
ngx_http_lua_ctx_t * ctx ;
1543
1540
ngx_http_lua_co_ctx_t * coctx ;
1544
1541
const char * busy_rc ;
1542
+ ngx_ssl_conn_t * ssl_conn ;
1543
+ X509 * x509 ;
1545
1544
1546
1545
ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
1547
1546
"lua tcp socket tls handshake" );
@@ -1597,6 +1596,8 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
1597
1596
return NGX_ERROR ;
1598
1597
}
1599
1598
1599
+ ssl_conn = c -> ssl -> connection ;
1600
+
1600
1601
ctx = ngx_http_get_module_ctx (r , ngx_http_lua_module );
1601
1602
if (ctx == NULL ) {
1602
1603
return NGX_HTTP_LUA_FFI_NO_REQ_CTX ;
@@ -1619,6 +1620,53 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
1619
1620
u -> ssl_session_reuse = enable_session_reuse ;
1620
1621
}
1621
1622
1623
+ if (chain != NULL ) {
1624
+ ngx_http_lua_assert (pkey != NULL ); /* ensured by resty.core */
1625
+
1626
+ if (sk_X509_num (chain ) < 1 ) {
1627
+ ERR_clear_error ();
1628
+ * errmsg = "invalid client certificate chain" ;
1629
+ return NGX_ERROR ;
1630
+ }
1631
+
1632
+ x509 = sk_X509_value (chain , 0 );
1633
+ if (x509 == NULL ) {
1634
+ ERR_clear_error ();
1635
+ * errmsg = "lua tls fetch client certificate from chain failed" ;
1636
+ return NGX_ERROR ;
1637
+ }
1638
+
1639
+ if (SSL_use_certificate (ssl_conn , x509 ) == 0 ) {
1640
+ ERR_clear_error ();
1641
+ * errmsg = "lua tls set client certificate failed" ;
1642
+ return NGX_ERROR ;
1643
+ }
1644
+
1645
+ /* read rest of the chain */
1646
+
1647
+ for (i = 1 ; i < sk_X509_num (chain ); i ++ ) {
1648
+ x509 = sk_X509_value (chain , i );
1649
+ if (x509 == NULL ) {
1650
+ ERR_clear_error ();
1651
+ * errmsg = "lua tls fetch client intermediate certificate "
1652
+ "from chain failed" ;
1653
+ return NGX_ERROR ;
1654
+ }
1655
+
1656
+ if (SSL_add1_chain_cert (ssl_conn , x509 ) == 0 ) {
1657
+ ERR_clear_error ();
1658
+ * errmsg = "lua tls set client intermediate certificate failed" ;
1659
+ return NGX_ERROR ;
1660
+ }
1661
+ }
1662
+
1663
+ if (SSL_use_PrivateKey (ssl_conn , pkey ) == 0 ) {
1664
+ ERR_clear_error ();
1665
+ * errmsg = "lua ssl set client private key failed" ;
1666
+ return NGX_ERROR ;
1667
+ }
1668
+ }
1669
+
1622
1670
if (server_name != NULL && server_name -> data != NULL ) {
1623
1671
ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
1624
1672
"lua tls server name: \"%V\"" , server_name );
0 commit comments