Skip to content

Commit 54c8d1c

Browse files
authored
feat: reject bad client TLS request in handshake (#50)
Signed-off-by: spacewander <[email protected]>
1 parent 2dbfdfe commit 54c8d1c

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

lib/resty/apisix/patch.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- This module stores the flags which mark the difference between
2+
-- APISIX's OpenResty and vanilla OpenResty.
3+
-- We use flag to distinguish the difference when it is impossible
4+
-- to distinguish APISIX's OpenResty via additional module methods.
5+
local _M = {
6+
client_cert_verified_in_handshake = true
7+
}
8+
9+
10+
return _M
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
diff --git lib/ngx/ssl.lua lib/ngx/ssl.lua
2+
index b769fd8..89ccabe 100644
3+
--- lib/ngx/ssl.lua
4+
+++ lib/ngx/ssl.lua
5+
@@ -85,7 +85,7 @@ if subsystem == 'http' then
6+
void ngx_http_lua_ffi_free_priv_key(void *cdata);
7+
8+
int ngx_http_lua_ffi_ssl_verify_client(void *r,
9+
- void *cdata, int depth, char **err);
10+
+ void *cdata, int depth, int reject_in_handshake, char **err);
11+
]]
12+
13+
ngx_lua_ffi_ssl_set_der_certificate =
14+
@@ -155,7 +155,7 @@ elseif subsystem == 'stream' then
15+
void ngx_stream_lua_ffi_free_priv_key(void *cdata);
16+
17+
int ngx_stream_lua_ffi_ssl_verify_client(void *r,
18+
- void *cdata, int depth, char **err);
19+
+ void *cdata, int depth, int reject_in_handshake, char **err);
20+
]]
21+
22+
ngx_lua_ffi_ssl_set_der_certificate =
23+
@@ -414,7 +414,7 @@ function _M.set_priv_key(priv_key)
24+
end
25+
26+
27+
-function _M.verify_client(ca_certs, depth)
28+
+function _M.verify_client(ca_certs, depth, reject_in_handshake)
29+
local r = get_request()
30+
if not r then
31+
error("no request found")
32+
@@ -424,7 +424,15 @@ function _M.verify_client(ca_certs, depth)
33+
depth = -1
34+
end
35+
36+
- local rc = ngx_lua_ffi_ssl_verify_client(r, ca_certs, depth, errmsg)
37+
+ if reject_in_handshake == nil then
38+
+ -- reject by default so we can migrate to the new behavior
39+
+ -- without modifying Lua code
40+
+ reject_in_handshake = true
41+
+ end
42+
+
43+
+ local reject_in_handshake_int = reject_in_handshake and 1 or 0
44+
+ local rc = ngx_lua_ffi_ssl_verify_client(r, ca_certs, depth,
45+
+ reject_in_handshake_int, errmsg)
46+
if rc == FFI_OK then
47+
return true
48+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
diff --git src/ngx_http_lua_ssl_certby.c src/ngx_http_lua_ssl_certby.c
2+
index 6ed2f3f..c46cc91 100644
3+
--- src/ngx_http_lua_ssl_certby.c
4+
+++ src/ngx_http_lua_ssl_certby.c
5+
@@ -1346,9 +1346,16 @@ ngx_http_lua_ssl_verify_callback(int ok, X509_STORE_CTX *x509_store)
6+
}
7+
8+
9+
+static int
10+
+ngx_http_lua_ssl_verify_reject_in_handshake_callback(int ok, X509_STORE_CTX *x509_store)
11+
+{
12+
+ return ok;
13+
+}
14+
+
15+
+
16+
int
17+
ngx_http_lua_ffi_ssl_verify_client(ngx_http_request_t *r, void *ca_certs,
18+
- int depth, char **err)
19+
+ int depth, int reject_in_handshake, char **err)
20+
{
21+
ngx_http_lua_ctx_t *ctx;
22+
ngx_ssl_conn_t *ssl_conn;
23+
@@ -1388,7 +1395,14 @@ ngx_http_lua_ffi_ssl_verify_client(ngx_http_request_t *r, void *ca_certs,
24+
25+
/* enable verify */
26+
27+
- SSL_set_verify(ssl_conn, SSL_VERIFY_PEER, ngx_http_lua_ssl_verify_callback);
28+
+ if (reject_in_handshake) {
29+
+ SSL_set_verify(ssl_conn,
30+
+ SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT ,
31+
+ ngx_http_lua_ssl_verify_reject_in_handshake_callback);
32+
+
33+
+ } else {
34+
+ SSL_set_verify(ssl_conn, SSL_VERIFY_PEER, ngx_http_lua_ssl_verify_callback);
35+
+ }
36+
37+
/* set depth */
38+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
diff --git src/ngx_stream_lua_ssl_certby.c src/ngx_stream_lua_ssl_certby.c
2+
index bc1b156..49fa0d3 100644
3+
--- src/ngx_stream_lua_ssl_certby.c
4+
+++ src/ngx_stream_lua_ssl_certby.c
5+
@@ -1365,9 +1365,16 @@ ngx_stream_lua_ssl_verify_callback(int ok, X509_STORE_CTX *x509_store)
6+
}
7+
8+
9+
+static int
10+
+ngx_stream_lua_ssl_verify_reject_in_handshake_callback(int ok, X509_STORE_CTX *x509_store)
11+
+{
12+
+ return ok;
13+
+}
14+
+
15+
+
16+
int
17+
ngx_stream_lua_ffi_ssl_verify_client(ngx_stream_lua_request_t *r,
18+
- void *ca_certs, int depth, char **err)
19+
+ void *ca_certs, int depth, int reject_in_handshake, char **err)
20+
{
21+
ngx_stream_lua_ctx_t *ctx;
22+
ngx_ssl_conn_t *ssl_conn;
23+
@@ -1407,8 +1414,15 @@ ngx_stream_lua_ffi_ssl_verify_client(ngx_stream_lua_request_t *r,
24+
25+
/* enable verify */
26+
27+
- SSL_set_verify(ssl_conn, SSL_VERIFY_PEER,
28+
- ngx_stream_lua_ssl_verify_callback);
29+
+ if (reject_in_handshake) {
30+
+ SSL_set_verify(ssl_conn,
31+
+ SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT ,
32+
+ ngx_stream_lua_ssl_verify_reject_in_handshake_callback);
33+
+
34+
+ } else {
35+
+ SSL_set_verify(ssl_conn, SSL_VERIFY_PEER,
36+
+ ngx_stream_lua_ssl_verify_callback);
37+
+ }
38+
39+
/* set depth */
40+

0 commit comments

Comments
 (0)