Skip to content

Commit d6ce64d

Browse files
authored
chore(tls): remove request_client_certificate and set_client_ca_list (#54)
OpenResty has supported [ssl.verify_client](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl.md#verify_client), which is able to replace our usage of `kong.tls.request_client_certificate` and `kong.tls.set_client_ca_list`. Thus we don't need to maintain these two APIs ourselves anymore. FT-3584
1 parent 602ae56 commit d6ce64d

File tree

4 files changed

+182
-856
lines changed

4 files changed

+182
-856
lines changed

README.md

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ Table of Contents
1313
* [lua\_kong\_load\_var\_index](#lua_kong_load_var_index)
1414
* [lua\_kong\_set\_static\_tag](#lua_kong_set_static_tag)
1515
* [Methods](#methods)
16-
* [resty.kong.tls.request\_client\_certificate](#restykongtlsrequest_client_certificate)
1716
* [resty.kong.tls.disable\_session\_reuse](#restykongtlsdisable_session_reuse)
18-
* [resty.kong.tls.set\_client\_ca\_list](#restykongtlsset_client_ca_list)
1917
* [resty.kong.tls.get\_full\_client\_certificate\_chain](#restykongtlsget_full_client_certificate_chain)
2018
* [resty.kong.tls.set\_upstream\_cert\_and\_key](#restykongtlsset_upstream_cert_and_key)
2119
* [resty.kong.tls.set\_upstream\_ssl\_trusted\_store](#restykongtlsset_upstream_ssl_trusted_store)
@@ -144,29 +142,6 @@ you will always get the value where your Lua code runs in but not others.
144142
Methods
145143
=======
146144

147-
resty.kong.tls.request\_client\_certificate
148-
-------------------------------------------
149-
**syntax:** *succ, err = resty.kong.tls.request\_client\_certificate()*
150-
151-
**context:** *ssl_certificate_by_lua**
152-
153-
**subsystems:** *http*
154-
155-
Requests client to present its client-side certificate to initiate mutual TLS
156-
authentication between server and client.
157-
158-
This function only *requests*, but does not *require* the client to start the mTLS
159-
process. Even if the client did not present a client certificate the TLS handshake
160-
will still complete (obviously not being mTLS in that case).
161-
Whether the client honored the request can be determined using
162-
[get\_full\_client\_certificate\_chain](#restykongtlsget_full_client_certificate_chain)
163-
in later phases.
164-
165-
This function returns `true` when the call is successful. Otherwise it returns
166-
`nil` and a string describing the error.
167-
168-
[Back to TOC](#table-of-contents)
169-
170145
resty.kong.tls.disable\_session\_reuse
171146
--------------------------------------
172147
**syntax:** *succ, err = resty.kong.tls.disable\_session\_reuse()*
@@ -183,59 +158,6 @@ This function returns `true` when the call is successful. Otherwise it returns
183158

184159
[Back to TOC](#table-of-contents)
185160

186-
resty.kong.tls.set\_client\_ca\_list
187-
-------------------------------------------
188-
**syntax:** *succ, err = resty.kong.tls.set\_client\_ca\_list(ca_list)*
189-
190-
**context:** *ssl_certificate_by_lua**
191-
192-
**subsystems:** *http*
193-
194-
Set the CA DN list to the underlying SSL structure, which will be sent in the
195-
Certificate Request Message of downstram TLS handshake.
196-
197-
The downstream client then can use this DN information to filter certificates,
198-
and chooses an appropriate certificate issued by a CA in the list.
199-
200-
`ca_list` is of type `STACK_OF(X509) *` which can be created by using the API
201-
of `resty.openssl.x509.chain` as follows:
202-
203-
```lua
204-
local tls_lib = require "resty.kong.tls"
205-
local x509_lib = require "resty.openssl.x509"
206-
local chain_lib = require "resty.openssl.x509.chain"
207-
208-
local suc, err
209-
local chain = chain_lib.new()
210-
-- err check
211-
local x509, err = x509_lib.new(pem_cert, "PEM")
212-
-- err check
213-
suc, err = chain:add(x509)
214-
-- err check
215-
216-
-- `chain.ctx` is the raw data of the chain, i.e. `STACK_OF(X509) *`
217-
suc, err = tls_lib.set_client_ca_list(chain.ctx)
218-
-- err check
219-
```
220-
221-
Or by using `ngx.ssl` as follows:
222-
223-
```lua
224-
local ssl = require "ngx.ssl"
225-
226-
local chain, err = ssl.parse_pem_cert(pem_cert_chain)
227-
-- note the `chain` returned by `ssl.parse_pem_cert` is already a raw `STACK_OF(X509) *`
228-
-- err check
229-
230-
suc, err = tls_lib.set_client_ca_list(chain)
231-
-- err check
232-
```
233-
234-
This function returns `true` when the call is successful. Otherwise it returns
235-
`nil` and a string describing the error.
236-
237-
[Back to TOC](#table-of-contents)
238-
239161
resty.kong.tls.get\_full\_client\_certificate\_chain
240162
----------------------------------------------------
241163
**syntax:** *pem_chain, err = resty.kong.tls.get\_full\_client\_certificate\_chain()*

lualib/resty/kong/tls.lua

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ base.allows_subsystem('http', 'stream')
2323

2424
if ngx.config.subsystem == "http" then
2525
ffi.cdef([[
26-
const char *ngx_http_lua_kong_ffi_request_client_certificate(ngx_http_request_t *r);
2726
int ngx_http_lua_kong_ffi_get_full_client_certificate_chain(
2827
ngx_http_request_t *r, char *buf, size_t *buf_len);
2928
const char *ngx_http_lua_kong_ffi_disable_session_reuse(ngx_http_request_t *r);
30-
const char *ngx_http_lua_kong_ffi_set_client_ca_list(ngx_http_request_t *r,
31-
void *ca_list);
3229
int ngx_http_lua_kong_ffi_set_upstream_client_cert_and_key(ngx_http_request_t *r,
3330
void *_chain, void *_key);
3431
int ngx_http_lua_kong_ffi_set_upstream_ssl_trusted_store(ngx_http_request_t *r,
@@ -77,22 +74,6 @@ local function get_request()
7774
end
7875

7976
if ngx.config.subsystem == "http" then
80-
function _M.request_client_certificate(no_session_reuse)
81-
if get_phase() ~= 'ssl_cert' then
82-
error("API disabled in the current context")
83-
end
84-
85-
local r = get_request()
86-
87-
local errmsg = C.ngx_http_lua_kong_ffi_request_client_certificate(r)
88-
if errmsg == nil then
89-
return true
90-
end
91-
92-
return nil, ffi_string(errmsg)
93-
end
94-
95-
9677
function _M.disable_session_reuse()
9778
if get_phase() ~= 'ssl_cert' then
9879
error("API disabled in the current context")
@@ -109,25 +90,6 @@ if ngx.config.subsystem == "http" then
10990
end
11091

11192

112-
function _M.set_client_ca_list(ca_list)
113-
if get_phase() ~= 'ssl_cert' then
114-
error("API disabled in the current context")
115-
end
116-
117-
local r = get_request()
118-
if not r then
119-
error("no request found")
120-
end
121-
122-
local errmsg = C.ngx_http_lua_kong_ffi_set_client_ca_list(r, ca_list)
123-
if errmsg == nil then
124-
return true
125-
end
126-
127-
return nil, ffi_string(errmsg)
128-
end
129-
130-
13193
do
13294
local ALLOWED_PHASES = {
13395
['rewrite'] = true,

src/ngx_http_lua_kong_ssl.c

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ ngx_http_lua_kong_ssl_init(ngx_conf_t *cf)
5555
}
5656

5757
#if (NGX_SSL)
58-
static int
59-
ngx_http_lua_kong_verify_callback(int ok, X509_STORE_CTX *x509_store)
60-
{
61-
/* similar to ngx_ssl_verify_callback, always allow handshake
62-
* to conclude before deciding the validity of client certificate */
63-
return 1;
64-
}
65-
66-
6758
static int
6859
ngx_http_lua_kong_new_session(ngx_ssl_conn_t *ssl_conn, ngx_ssl_session_t *sess)
6960
{
@@ -146,83 +137,6 @@ ngx_http_lua_kong_ffi_disable_session_reuse(ngx_http_request_t *r)
146137
}
147138

148139

149-
/*
150-
* request downstream to present a client certificate during TLS handshake,
151-
* but does not validate it
152-
*
153-
* this is roughly equivalent to setting ssl_verify_client to optional_no_ca
154-
*
155-
* on success, NULL is returned, otherwise a static string indicating the
156-
* failure reason is returned
157-
*/
158-
159-
const char *
160-
ngx_http_lua_kong_ffi_request_client_certificate(ngx_http_request_t *r)
161-
{
162-
#if (NGX_SSL)
163-
ngx_connection_t *c = r->connection;
164-
ngx_ssl_conn_t *sc;
165-
166-
if (c->ssl == NULL) {
167-
return "server does not have TLS enabled";
168-
}
169-
170-
sc = c->ssl->connection;
171-
172-
SSL_set_verify(sc, SSL_VERIFY_PEER, ngx_http_lua_kong_verify_callback);
173-
174-
return NULL;
175-
176-
#else
177-
return "TLS support is not enabled in Nginx build";
178-
#endif
179-
}
180-
181-
182-
/*
183-
* Set the CA DN list to the underlying SSL structure, which will be sent in the
184-
* Certificate Request Message of downstram TLS handshake.
185-
*
186-
* The downstream client can use this DN information to filter certificates,
187-
* and chooses an appropriate certificate issued by a CA in the list.
188-
*
189-
* on success, NULL is returned, otherwise a static string indicating the
190-
* failure reason is returned.
191-
*/
192-
193-
const char *
194-
ngx_http_lua_kong_ffi_set_client_ca_list(ngx_http_request_t *r,
195-
const STACK_OF(X509) *ca_list)
196-
{
197-
#if (NGX_SSL)
198-
ngx_connection_t *c = r->connection;
199-
ngx_ssl_conn_t *sc;
200-
X509 *ca;
201-
int i;
202-
203-
if (c->ssl == NULL) {
204-
return "server does not have TLS enabled";
205-
}
206-
207-
sc = c->ssl->connection;
208-
209-
for (i = 0; i < sk_X509_num(ca_list); i++) {
210-
ca = sk_X509_value(ca_list, i);
211-
212-
/* will call X509_NAME_dup() internally */
213-
if (SSL_add_client_CA(sc, ca) == 0) {
214-
return "unable to add the CA name to the list";
215-
}
216-
}
217-
218-
return NULL;
219-
220-
#else
221-
return "TLS support is not enabled in Nginx build";
222-
#endif
223-
}
224-
225-
226140
int
227141
ngx_http_lua_kong_ffi_get_full_client_certificate_chain(ngx_http_request_t *r,
228142
char *buf, size_t *buf_len)

0 commit comments

Comments
 (0)