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 );
@@ -219,9 +218,6 @@ static char ngx_http_lua_upstream_udata_metatable_key;
219
218
static char ngx_http_lua_downstream_udata_metatable_key ;
220
219
static char ngx_http_lua_pool_udata_metatable_key ;
221
220
static char ngx_http_lua_pattern_udata_metatable_key ;
222
- #if (NGX_HTTP_SSL )
223
- static char ngx_http_lua_ssl_session_metatable_key ;
224
- #endif
225
221
226
222
227
223
#define ngx_http_lua_tcp_socket_metatable_literal_key "__tcp_cosocket_mt"
@@ -1563,13 +1559,16 @@ int
1563
1559
ngx_http_lua_ffi_socket_tcp_tlshandshake (ngx_http_request_t * r ,
1564
1560
ngx_http_lua_socket_tcp_upstream_t * u , ngx_ssl_session_t * sess ,
1565
1561
int enable_session_reuse , ngx_str_t * server_name , int verify ,
1566
- int ocsp_status_req , const char * * errmsg )
1562
+ int ocsp_status_req , STACK_OF (X509 ) * chain , EVP_PKEY * pkey ,
1563
+ const char * * errmsg )
1567
1564
{
1568
- ngx_int_t rc ;
1565
+ ngx_int_t rc , i ;
1569
1566
ngx_connection_t * c ;
1570
1567
ngx_http_lua_ctx_t * ctx ;
1571
1568
ngx_http_lua_co_ctx_t * coctx ;
1572
1569
const char * busy_rc ;
1570
+ ngx_ssl_conn_t * ssl_conn ;
1571
+ X509 * x509 ;
1573
1572
1574
1573
ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
1575
1574
"lua tcp socket tls handshake" );
@@ -1625,6 +1624,8 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
1625
1624
return NGX_ERROR ;
1626
1625
}
1627
1626
1627
+ ssl_conn = c -> ssl -> connection ;
1628
+
1628
1629
ctx = ngx_http_get_module_ctx (r , ngx_http_lua_module );
1629
1630
if (ctx == NULL ) {
1630
1631
return NGX_HTTP_LUA_FFI_NO_REQ_CTX ;
@@ -1647,6 +1648,53 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
1647
1648
u -> ssl_session_reuse = enable_session_reuse ;
1648
1649
}
1649
1650
1651
+ if (chain != NULL ) {
1652
+ ngx_http_lua_assert (pkey != NULL ); /* ensured by resty.core */
1653
+
1654
+ if (sk_X509_num (chain ) < 1 ) {
1655
+ ERR_clear_error ();
1656
+ * errmsg = "invalid client certificate chain" ;
1657
+ return NGX_ERROR ;
1658
+ }
1659
+
1660
+ x509 = sk_X509_value (chain , 0 );
1661
+ if (x509 == NULL ) {
1662
+ ERR_clear_error ();
1663
+ * errmsg = "lua tls fetch client certificate from chain failed" ;
1664
+ return NGX_ERROR ;
1665
+ }
1666
+
1667
+ if (SSL_use_certificate (ssl_conn , x509 ) == 0 ) {
1668
+ ERR_clear_error ();
1669
+ * errmsg = "lua tls set client certificate failed" ;
1670
+ return NGX_ERROR ;
1671
+ }
1672
+
1673
+ /* read rest of the chain */
1674
+
1675
+ for (i = 1 ; i < sk_X509_num (chain ); i ++ ) {
1676
+ x509 = sk_X509_value (chain , i );
1677
+ if (x509 == NULL ) {
1678
+ ERR_clear_error ();
1679
+ * errmsg = "lua tls fetch client intermediate certificate "
1680
+ "from chain failed" ;
1681
+ return NGX_ERROR ;
1682
+ }
1683
+
1684
+ if (SSL_add1_chain_cert (ssl_conn , x509 ) == 0 ) {
1685
+ ERR_clear_error ();
1686
+ * errmsg = "lua tls set client intermediate certificate failed" ;
1687
+ return NGX_ERROR ;
1688
+ }
1689
+ }
1690
+
1691
+ if (SSL_use_PrivateKey (ssl_conn , pkey ) == 0 ) {
1692
+ ERR_clear_error ();
1693
+ * errmsg = "lua ssl set client private key failed" ;
1694
+ return NGX_ERROR ;
1695
+ }
1696
+ }
1697
+
1650
1698
if (server_name != NULL && server_name -> data != NULL ) {
1651
1699
ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
1652
1700
"lua tls server name: \"%V\"" , server_name );
0 commit comments