Skip to content

Commit e81c5f0

Browse files
committed
feature: TCP cosocket client certificate support. closes openresty#534
1 parent c4c9e51 commit e81c5f0

8 files changed

+732
-8
lines changed

src/ngx_http_lua_socket_tcp.c

+54-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
static int ngx_http_lua_socket_tcp(lua_State *L);
2323
static int ngx_http_lua_socket_tcp_connect(lua_State *L);
2424
#if (NGX_HTTP_SSL)
25-
static int ngx_http_lua_socket_tcp_sslhandshake(lua_State *L);
2625
static void ngx_http_lua_tls_handshake_handler(ngx_connection_t *c);
2726
static int ngx_http_lua_tls_handshake_retval_handler(ngx_http_request_t *r,
2827
ngx_http_lua_socket_tcp_upstream_t *u, lua_State *L);
@@ -219,9 +218,6 @@ static char ngx_http_lua_upstream_udata_metatable_key;
219218
static char ngx_http_lua_downstream_udata_metatable_key;
220219
static char ngx_http_lua_pool_udata_metatable_key;
221220
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
225221

226222

227223
#define ngx_http_lua_tcp_socket_metatable_literal_key "__tcp_cosocket_mt"
@@ -1563,13 +1559,16 @@ int
15631559
ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
15641560
ngx_http_lua_socket_tcp_upstream_t *u, ngx_ssl_session_t *sess,
15651561
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)
15671564
{
1568-
ngx_int_t rc;
1565+
ngx_int_t rc, i;
15691566
ngx_connection_t *c;
15701567
ngx_http_lua_ctx_t *ctx;
15711568
ngx_http_lua_co_ctx_t *coctx;
15721569
const char *busy_rc;
1570+
ngx_ssl_conn_t *ssl_conn;
1571+
X509 *x509;
15731572

15741573
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
15751574
"lua tcp socket tls handshake");
@@ -1625,6 +1624,8 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
16251624
return NGX_ERROR;
16261625
}
16271626

1627+
ssl_conn = c->ssl->connection;
1628+
16281629
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
16291630
if (ctx == NULL) {
16301631
return NGX_HTTP_LUA_FFI_NO_REQ_CTX;
@@ -1647,6 +1648,53 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
16471648
u->ssl_session_reuse = enable_session_reuse;
16481649
}
16491650

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+
16501698
if (server_name != NULL && server_name->data != NULL) {
16511699
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
16521700
"lua tls server name: \"%V\"", server_name);

0 commit comments

Comments
 (0)