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 );
@@ -221,9 +220,6 @@ static char ngx_http_lua_upstream_udata_metatable_key;
221
220
static char ngx_http_lua_downstream_udata_metatable_key ;
222
221
static char ngx_http_lua_pool_udata_metatable_key ;
223
222
static char ngx_http_lua_pattern_udata_metatable_key ;
224
- #if (NGX_HTTP_SSL )
225
- static char ngx_http_lua_ssl_session_metatable_key ;
226
- #endif
227
223
228
224
229
225
#define ngx_http_lua_tcp_socket_metatable_literal_key "__tcp_cosocket_mt"
@@ -1572,13 +1568,16 @@ int
1572
1568
ngx_http_lua_ffi_socket_tcp_tlshandshake (ngx_http_request_t * r ,
1573
1569
ngx_http_lua_socket_tcp_upstream_t * u , ngx_ssl_session_t * sess ,
1574
1570
int enable_session_reuse , ngx_str_t * server_name , int verify ,
1575
- int ocsp_status_req , const char * * errmsg )
1571
+ int ocsp_status_req , STACK_OF (X509 ) * chain , EVP_PKEY * pkey ,
1572
+ const char * * errmsg )
1576
1573
{
1577
- ngx_int_t rc ;
1574
+ ngx_int_t rc , i ;
1578
1575
ngx_connection_t * c ;
1579
1576
ngx_http_lua_ctx_t * ctx ;
1580
1577
ngx_http_lua_co_ctx_t * coctx ;
1581
1578
const char * busy_rc ;
1579
+ ngx_ssl_conn_t * ssl_conn ;
1580
+ X509 * x509 ;
1582
1581
1583
1582
ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
1584
1583
"lua tcp socket tls handshake" );
@@ -1634,6 +1633,8 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
1634
1633
return NGX_ERROR ;
1635
1634
}
1636
1635
1636
+ ssl_conn = c -> ssl -> connection ;
1637
+
1637
1638
ctx = ngx_http_get_module_ctx (r , ngx_http_lua_module );
1638
1639
if (ctx == NULL ) {
1639
1640
return NGX_HTTP_LUA_FFI_NO_REQ_CTX ;
@@ -1656,6 +1657,53 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
1656
1657
u -> ssl_session_reuse = enable_session_reuse ;
1657
1658
}
1658
1659
1660
+ if (chain != NULL ) {
1661
+ ngx_http_lua_assert (pkey != NULL ); /* ensured by resty.core */
1662
+
1663
+ if (sk_X509_num (chain ) < 1 ) {
1664
+ ERR_clear_error ();
1665
+ * errmsg = "invalid client certificate chain" ;
1666
+ return NGX_ERROR ;
1667
+ }
1668
+
1669
+ x509 = sk_X509_value (chain , 0 );
1670
+ if (x509 == NULL ) {
1671
+ ERR_clear_error ();
1672
+ * errmsg = "lua tls fetch client certificate from chain failed" ;
1673
+ return NGX_ERROR ;
1674
+ }
1675
+
1676
+ if (SSL_use_certificate (ssl_conn , x509 ) == 0 ) {
1677
+ ERR_clear_error ();
1678
+ * errmsg = "lua tls set client certificate failed" ;
1679
+ return NGX_ERROR ;
1680
+ }
1681
+
1682
+ /* read rest of the chain */
1683
+
1684
+ for (i = 1 ; i < sk_X509_num (chain ); i ++ ) {
1685
+ x509 = sk_X509_value (chain , i );
1686
+ if (x509 == NULL ) {
1687
+ ERR_clear_error ();
1688
+ * errmsg = "lua tls fetch client intermediate certificate "
1689
+ "from chain failed" ;
1690
+ return NGX_ERROR ;
1691
+ }
1692
+
1693
+ if (SSL_add1_chain_cert (ssl_conn , x509 ) == 0 ) {
1694
+ ERR_clear_error ();
1695
+ * errmsg = "lua tls set client intermediate certificate failed" ;
1696
+ return NGX_ERROR ;
1697
+ }
1698
+ }
1699
+
1700
+ if (SSL_use_PrivateKey (ssl_conn , pkey ) == 0 ) {
1701
+ ERR_clear_error ();
1702
+ * errmsg = "lua ssl set client private key failed" ;
1703
+ return NGX_ERROR ;
1704
+ }
1705
+ }
1706
+
1659
1707
if (server_name != NULL && server_name -> data != NULL ) {
1660
1708
ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
1661
1709
"lua tls server name: \"%V\"" , server_name );
0 commit comments