Skip to content

Commit d677c00

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 d959374 commit d677c00

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
@@ -977,7 +977,6 @@ TODO
977977
* 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.
978978
* add automatic Lua code time slicing support by yielding and resuming the Lua VM actively via Lua's debug hooks.
979979
* add `stat` mode similar to [mod_lua](https://httpd.apache.org/docs/trunk/mod/mod_lua.html).
980-
* cosocket: add client SSL certificate support.
981980

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

@@ -3594,6 +3593,7 @@ Nginx API for Lua
35943593
* [ngx.socket.stream](#ngxsocketstream)
35953594
* [ngx.socket.tcp](#ngxsockettcp)
35963595
* [tcpsock:connect](#tcpsockconnect)
3596+
* [tcpsock:setclientcert](#tcpsocksetclientcert)
35973597
* [tcpsock:sslhandshake](#tcpsocksslhandshake)
35983598
* [tcpsock:send](#tcpsocksend)
35993599
* [tcpsock:receive](#tcpsockreceive)
@@ -7564,6 +7564,7 @@ ngx.socket.tcp
75647564
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:
75657565

75667566
* [connect](#tcpsockconnect)
7567+
* [setclientcert](#tcpsocksetclientcert)
75677568
* [sslhandshake](#tcpsocksslhandshake)
75687569
* [send](#tcpsocksend)
75697570
* [receive](#tcpsockreceive)
@@ -7723,6 +7724,31 @@ This method was first introduced in the `v0.5.0rc1` release.
77237724

77247725
[Back to TOC](#nginx-api-for-lua)
77257726

7727+
tcpsock:setclientcert
7728+
--------------------
7729+
7730+
**syntax:** *ok, err = tcpsock:setclientcert(cert, pkey)*
7731+
7732+
**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;*
7733+
7734+
Set client certificate chain and corresponding private key to the TCP socket object.
7735+
The certificate chain and private key provided will be used later by the [tcpsock:sslhandshake](#tcpsocksslhandshake) method.
7736+
7737+
* `cert` specify a client certificate chain cdata object that will be used while handshaking with
7738+
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)
7739+
function provided by lua-resty-core. Note that specifying the `cert` option requires
7740+
corresponding `pkey` be provided too. See below.
7741+
* `pkey` specify a private key corresponds to the `cert` option above.
7742+
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)
7743+
function provided by lua-resty-core.
7744+
7745+
If both of `cert` and `pkey` are `nil`, this method will clear any existing client certificate and private key
7746+
that was previously set on the cosocket object.
7747+
7748+
This method was first introduced in the `v0.10.22` release.
7749+
7750+
[Back to TOC](#nginx-api-for-lua)
7751+
77267752
tcpsock:sslhandshake
77277753
--------------------
77287754

0 commit comments

Comments
 (0)