Skip to content

Commit 6a51aa4

Browse files
committed
feature: implement ssl.verify_client()
1 parent bdcc16b commit 6a51aa4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/ngx/ssl.lua

Lines changed: 22 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,8 @@ 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, int depth, void *cdata, char **err);
8184
]]
8285

8386
ngx_lua_ffi_ssl_set_der_certificate =
@@ -97,6 +100,7 @@ if subsystem == 'http' then
97100
ngx_lua_ffi_set_priv_key = C.ngx_http_lua_ffi_set_priv_key
98101
ngx_lua_ffi_free_cert = C.ngx_http_lua_ffi_free_cert
99102
ngx_lua_ffi_free_priv_key = C.ngx_http_lua_ffi_free_priv_key
103+
ngx_lua_ffi_ssl_verify_client = C.ngx_http_lua_ffi_ssl_verify_client
100104

101105
elseif subsystem == 'stream' then
102106
ffi.cdef[[
@@ -140,6 +144,8 @@ elseif subsystem == 'stream' then
140144
void ngx_stream_lua_ffi_free_cert(void *cdata);
141145

142146
void ngx_stream_lua_ffi_free_priv_key(void *cdata);
147+
148+
int ngx_stream_lua_ffi_ssl_verify_client(void *r, int depth, void *cdata, char **err);
143149
]]
144150

145151
ngx_lua_ffi_ssl_set_der_certificate =
@@ -159,6 +165,7 @@ elseif subsystem == 'stream' then
159165
ngx_lua_ffi_set_priv_key = C.ngx_stream_lua_ffi_set_priv_key
160166
ngx_lua_ffi_free_cert = C.ngx_stream_lua_ffi_free_cert
161167
ngx_lua_ffi_free_priv_key = C.ngx_stream_lua_ffi_free_priv_key
168+
ngx_lua_ffi_ssl_verify_client = C.ngx_stream_lua_ffi_ssl_verify_client
162169
end
163170

164171

@@ -380,6 +387,21 @@ function _M.set_priv_key(priv_key)
380387
end
381388

382389

390+
function _M.verify_client(depth, ca_certs)
391+
local r = get_request()
392+
if not r then
393+
error("no request found")
394+
end
395+
396+
local rc = ngx_lua_ffi_ssl_verify_client(r, depth, ca_certs, errmsg)
397+
if rc == FFI_OK then
398+
return true
399+
end
400+
401+
return nil, ffi_str(errmsg[0])
402+
end
403+
404+
383405
do
384406
_M.SSL3_VERSION = 0x0300
385407
_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)