Skip to content

Commit 978e012

Browse files
committed
feature: implement ssl.verify_client()
1 parent bdcc16b commit 978e012

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/ngx/ssl.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ local ngx_lua_ffi_set_cert
3535
local ngx_lua_ffi_set_priv_key
3636
local ngx_lua_ffi_free_cert
3737
local ngx_lua_ffi_free_priv_key
38+
local ngx_lua_ffi_ssl_verify_client
3839

3940

4041
if subsystem == 'http' then
@@ -78,6 +79,9 @@ if subsystem == 'http' then
7879
void ngx_http_lua_ffi_free_cert(void *cdata);
7980

8081
void ngx_http_lua_ffi_free_priv_key(void *cdata);
82+
83+
int ngx_http_lua_ffi_ssl_verify_client(void *r,
84+
int depth, void *cdata, char **err);
8185
]]
8286

8387
ngx_lua_ffi_ssl_set_der_certificate =
@@ -97,6 +101,7 @@ if subsystem == 'http' then
97101
ngx_lua_ffi_set_priv_key = C.ngx_http_lua_ffi_set_priv_key
98102
ngx_lua_ffi_free_cert = C.ngx_http_lua_ffi_free_cert
99103
ngx_lua_ffi_free_priv_key = C.ngx_http_lua_ffi_free_priv_key
104+
ngx_lua_ffi_ssl_verify_client = C.ngx_http_lua_ffi_ssl_verify_client
100105

101106
elseif subsystem == 'stream' then
102107
ffi.cdef[[
@@ -140,6 +145,9 @@ elseif subsystem == 'stream' then
140145
void ngx_stream_lua_ffi_free_cert(void *cdata);
141146

142147
void ngx_stream_lua_ffi_free_priv_key(void *cdata);
148+
149+
int ngx_stream_lua_ffi_ssl_verify_client(void *r,
150+
int depth, void *cdata, char **err);
143151
]]
144152

145153
ngx_lua_ffi_ssl_set_der_certificate =
@@ -159,6 +167,7 @@ elseif subsystem == 'stream' then
159167
ngx_lua_ffi_set_priv_key = C.ngx_stream_lua_ffi_set_priv_key
160168
ngx_lua_ffi_free_cert = C.ngx_stream_lua_ffi_free_cert
161169
ngx_lua_ffi_free_priv_key = C.ngx_stream_lua_ffi_free_priv_key
170+
ngx_lua_ffi_ssl_verify_client = C.ngx_stream_lua_ffi_ssl_verify_client
162171
end
163172

164173

@@ -380,6 +389,21 @@ function _M.set_priv_key(priv_key)
380389
end
381390

382391

392+
function _M.verify_client(depth, ca_certs)
393+
local r = get_request()
394+
if not r then
395+
error("no request found")
396+
end
397+
398+
local rc = ngx_lua_ffi_ssl_verify_client(r, depth, ca_certs, errmsg)
399+
if rc == FFI_OK then
400+
return true
401+
end
402+
403+
return nil, ffi_str(errmsg[0])
404+
end
405+
406+
383407
do
384408
_M.SSL3_VERSION = 0x0300
385409
_M.TLS1_VERSION = 0x0301

lib/ngx/ssl.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,28 @@ This function was first added in version `0.1.7`.
475475

476476
[Back to TOC](#table-of-contents)
477477

478+
verify_client
479+
------------
480+
**syntax:** *ok, err = ssl.verify_client(depth, ca_certs)*
481+
482+
**context:** *ssl_certificate_by_lua**
483+
484+
Requires a client certificate during TLS handshake.
485+
486+
The `depth` is the verification depth in the client certificates chain.
487+
488+
The `ca_certs` is the CA certificate chain opaque pointer returned by the
489+
[parse_pem_cert](#parse_pem_cert) function for the current SSL connection.
490+
491+
Returns `true` on success, or a `nil` value and a string describing the error otherwise.
492+
493+
Note that TLS is not terminated when verification fails. You need to examine Nginx variable `$ssl_client_verify`
494+
later to determine next steps.
495+
496+
This function was first added in version `0.1.18`.
497+
498+
[Back to TOC](#table-of-contents)
499+
478500
Community
479501
=========
480502

0 commit comments

Comments
 (0)