Skip to content

Commit 0fea325

Browse files
committed
cosocket: added client certificate support with TLS handshake.
1 parent 1b73826 commit 0fea325

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

lib/resty/core/socket_tcp.lua

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,26 @@ local co_yield = coroutine._yield
1919
local table_new = require("table.new")
2020
local table_clear = require("table.clear")
2121

22-
if not pcall(ffi.typeof, "ngx_ssl_session_t") then
23-
ffi.cdef[[
24-
typedef struct SSL_SESSION ngx_ssl_session_t;
25-
]]
26-
end
27-
2822
ffi.cdef[[
2923
typedef struct ngx_http_lua_socket_tcp_upstream_s ngx_http_lua_socket_tcp_upstream_t;
3024

3125
int ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
32-
ngx_http_lua_socket_tcp_upstream_t *u, ngx_ssl_session_t *sess,
26+
ngx_http_lua_socket_tcp_upstream_t *u, void *sess,
3327
int enable_session_reuse, ngx_str_t *server_name, int verify,
34-
int ocsp_status_req, char **errmsg);
28+
int ocsp_status_req, void *chain, void *pkey,
29+
char **errmsg);
3530
int ngx_http_lua_ffi_socket_tcp_get_tlshandshake_result(ngx_http_request_t *r,
36-
ngx_http_lua_socket_tcp_upstream_t *u, ngx_ssl_session_t **sess,
31+
ngx_http_lua_socket_tcp_upstream_t *u, void **sess,
3732
char **errmsg, int *openssl_error_code);
38-
void ngx_http_lua_ffi_tls_free_session(ngx_ssl_session_t *sess);
33+
void ngx_http_lua_ffi_tls_free_session(void *sess);
3934
]]
4035

4136

4237
local SOCKET_CTX_INDEX = 1
4338

4439

4540
local errmsg = base.get_errmsg_ptr()
46-
local session_ptr = ffi.new("ngx_ssl_session_t *[1]")
41+
local session_ptr = ffi.new("void *[1]")
4742
local server_name_str = ffi.new("ngx_str_t[1]")
4843
local openssl_error_code = ffi.new("int[1]")
4944
local cached_options = table_new(0, 4)
@@ -76,6 +71,21 @@ local function tlshandshake(self, options)
7671
server_name_str[0].len = 0
7772
end
7873

74+
local client_cert = options.client_cert
75+
local client_priv_key = options.client_priv_key
76+
if client_cert then
77+
if not client_priv_key then
78+
error("client certificate supplied without "
79+
.. "corresponding private key", 2)
80+
end
81+
82+
if type(client_cert) ~= "cdata"
83+
or type(client_priv_key) ~= "cdata"
84+
then
85+
error("wrong type of client certificate or private key supplied", 2)
86+
end
87+
end
88+
7989
local rc =
8090
C.ngx_http_lua_ffi_socket_tcp_tlshandshake(r, self[SOCKET_CTX_INDEX],
8191
session_ptr[0],
@@ -84,6 +94,8 @@ local function tlshandshake(self, options)
8494
options.verify and 1 or 0,
8595
options.ocsp_status_req
8696
and 1 or 0,
97+
client_cert,
98+
client_priv_key,
8799
errmsg)
88100

89101
if rc == FFI_NO_REQ_CTX then

0 commit comments

Comments
 (0)