Skip to content

Commit 51e897f

Browse files
dndxchronolaw
andcommitted
cosocket: add function tcpsock:setclientcert, reimplemented tcpsock:sslhandshake with FFI
This adds support for setting client certificate/private key that will be used later for mutual TLS handshake with a server. Also, the `tcpsock:sslhandshake` implementation has been rewritten to use FFI C API to be more performant and easier to maintain. Also see: openresty/lua-resty-core#278 Co-authored-by: Chrono Law <[email protected]>
1 parent 8cf9c63 commit 51e897f

19 files changed

+1079
-309
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ install:
8888
- git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module
8989
- git clone https://github.com/openresty/srcache-nginx-module.git ../srcache-nginx-module
9090
- git clone https://github.com/openresty/redis2-nginx-module.git ../redis2-nginx-module
91-
- git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
91+
- git clone -b feat/cosocket_tlshandshake https://github.com/dndx/lua-resty-core.git ../lua-resty-core
9292
- git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
9393
- git clone https://github.com/openresty/lua-resty-mysql.git ../lua-resty-mysql
9494
- git clone https://github.com/openresty/lua-resty-string.git ../lua-resty-string

README.markdown

+27-1
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,6 @@ TODO
978978
* add `ignore_resp_headers`, `ignore_resp_body`, and `ignore_resp` options to [ngx.location.capture](#ngxlocationcapture) and [ngx.location.capture_multi](#ngxlocationcapture_multi) methods, to allow micro performance tuning on the user side.
979979
* add automatic Lua code time slicing support by yielding and resuming the Lua VM actively via Lua's debug hooks.
980980
* add `stat` mode similar to [mod_lua](https://httpd.apache.org/docs/trunk/mod/mod_lua.html).
981-
* cosocket: add client SSL certificate support.
982981

983982
[Back to TOC](#table-of-contents)
984983

@@ -3609,6 +3608,7 @@ Nginx API for Lua
36093608
* [ngx.socket.stream](#ngxsocketstream)
36103609
* [ngx.socket.tcp](#ngxsockettcp)
36113610
* [tcpsock:connect](#tcpsockconnect)
3611+
* [tcpsock:setclientcert](#tcpsocksetclientcert)
36123612
* [tcpsock:sslhandshake](#tcpsocksslhandshake)
36133613
* [tcpsock:send](#tcpsocksend)
36143614
* [tcpsock:receive](#tcpsockreceive)
@@ -7579,6 +7579,7 @@ ngx.socket.tcp
75797579
Creates and returns a TCP or stream-oriented unix domain socket object (also known as one type of the "cosocket" objects). The following methods are supported on this object:
75807580

75817581
* [connect](#tcpsockconnect)
7582+
* [setclientcert](#tcpsocksetclientcert)
75827583
* [sslhandshake](#tcpsocksslhandshake)
75837584
* [send](#tcpsocksend)
75847585
* [receive](#tcpsockreceive)
@@ -7738,6 +7739,31 @@ This method was first introduced in the `v0.5.0rc1` release.
77387739

77397740
[Back to TOC](#nginx-api-for-lua)
77407741

7742+
tcpsock:setclientcert
7743+
--------------------
7744+
7745+
**syntax:** *ok, err = tcpsock:setclientcert(cert, pkey)*
7746+
7747+
**context:** *rewrite_by_lua&#42;, access_by_lua&#42;, content_by_lua&#42;, ngx.timer.&#42;, ssl_certificate_by_lua&#42;, ssl_session_fetch_by_lua&#42;, ssl_client_hello_by_lua&#42;*
7748+
7749+
Set client certificate chain and corresponding private key to the TCP socket object.
7750+
The certificate chain and private key provided will be used later by the [tcpsock:sslhandshake](#tcpsocksslhandshake) method.
7751+
7752+
* `cert` specify a client certificate chain cdata object that will be used while handshaking with
7753+
remote server. These objects can be created using [ngx.ssl.parse\_pem\_cert](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl.md#parse_pem_cert)
7754+
function provided by lua-resty-core. Note that specifying the `cert` option requires
7755+
corresponding `pkey` be provided too. See below.
7756+
* `pkey` specify a private key corresponds to the `cert` option above.
7757+
These objects can be created using [ngx.ssl.parse\_pem\_priv\_key](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl.md#parse_pem_priv_key)
7758+
function provided by lua-resty-core.
7759+
7760+
If both of `cert` and `pkey` are `nil`, this method will clear any existing client certificate and private key
7761+
that was previously set on the cosocket object.
7762+
7763+
This method was first introduced in the `v0.10.22` release.
7764+
7765+
[Back to TOC](#nginx-api-for-lua)
7766+
77417767
tcpsock:sslhandshake
77427768
--------------------
77437769

0 commit comments

Comments
 (0)