Skip to content

Commit 8d22be2

Browse files
committed
feature: TCP cosocket client certificate support. closes openresty#534
1 parent 03f7b3a commit 8d22be2

File tree

8 files changed

+732
-8
lines changed

8 files changed

+732
-8
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 54 additions & 6 deletions
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);
@@ -221,9 +220,6 @@ static char ngx_http_lua_upstream_udata_metatable_key;
221220
static char ngx_http_lua_downstream_udata_metatable_key;
222221
static char ngx_http_lua_pool_udata_metatable_key;
223222
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
227223

228224

229225
#define ngx_http_lua_tcp_socket_metatable_literal_key "__tcp_cosocket_mt"
@@ -1572,13 +1568,16 @@ int
15721568
ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
15731569
ngx_http_lua_socket_tcp_upstream_t *u, ngx_ssl_session_t *sess,
15741570
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)
15761573
{
1577-
ngx_int_t rc;
1574+
ngx_int_t rc, i;
15781575
ngx_connection_t *c;
15791576
ngx_http_lua_ctx_t *ctx;
15801577
ngx_http_lua_co_ctx_t *coctx;
15811578
const char *busy_rc;
1579+
ngx_ssl_conn_t *ssl_conn;
1580+
X509 *x509;
15821581

15831582
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
15841583
"lua tcp socket tls handshake");
@@ -1634,6 +1633,8 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
16341633
return NGX_ERROR;
16351634
}
16361635

1636+
ssl_conn = c->ssl->connection;
1637+
16371638
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
16381639
if (ctx == NULL) {
16391640
return NGX_HTTP_LUA_FFI_NO_REQ_CTX;
@@ -1656,6 +1657,53 @@ ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
16561657
u->ssl_session_reuse = enable_session_reuse;
16571658
}
16581659

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

0 commit comments

Comments
 (0)