Skip to content

Commit e6077b5

Browse files
committed
feature: added the ngx.ssl.session module for the contexts ssl_session_fetch_by_lua* and ssl_session_store_by_lua*.
thanks Zi Lin for the patches.
1 parent 93f9e68 commit e6077b5

File tree

7 files changed

+954
-1
lines changed

7 files changed

+954
-1
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ install:
4141
- git clone https://github.com/openresty/openresty.git ../openresty
4242
- git clone https://github.com/openresty/nginx-devel-utils.git
4343
- git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
44-
- git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
44+
- git clone -b ssl-session https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
4545
- git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
4646
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
4747
- git clone https://github.com/openresty/lua-resty-lrucache.git
@@ -57,6 +57,8 @@ script:
5757
- cd lua-resty-lrucache && sudo make DESTDIR=$LUAJIT_PREFIX LUA_LIB_DIR=/share/lua/5.1 install && cd ..
5858
- tar zxf download-cache/openssl-$OPENSSL_VER.tar.gz
5959
- cd openssl-$OPENSSL_VER/
60+
- wget https://raw.githubusercontent.com/openresty/openresty/master/patches/openssl-$OPENSSL_VER-sess_set_get_cb_yield.patch
61+
- patch -p1 < openssl-$OPENSSL_VER-sess_set_get_cb_yield.patch
6062
- ./config shared --prefix=$OPENSSL_PREFIX -DPURIFY > build.log 2>&1 || (cat build.log && exit 1)
6163
- make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
6264
- sudo make PATH=$PATH install_sw > build.log 2>&1 || (cat build.log && exit 1)

README.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Table of Contents
2828
* [ngx.semaphore](#ngxsemaphore)
2929
* [ngx.balancer](#ngxbalancer)
3030
* [ngx.ssl](#ngxssl)
31+
* [ngx.ssl.session](#ngxsslsession)
3132
* [Caveat](#caveat)
3233
* [TODO](#todo)
3334
* [Author](#author)
@@ -225,6 +226,15 @@ See the [documentation](./lib/ngx/ssl.md) for this Lua module for more details.
225226

226227
[Back to TOC](#table-of-contents)
227228

229+
## ngx.ssl.session
230+
231+
This Lua module provides a Lua API for manipulating SSL session data and IDs
232+
for NGINX downstream SSL connections.
233+
234+
See the [documentation](./lib/ngx/ssl/session.md) for this Lua module for more details.
235+
236+
[Back to TOC](#table-of-contents)
237+
228238
Caveat
229239
======
230240

lib/ngx/ssl/session.lua

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
local _M = {}
2+
3+
4+
local ffi = require "ffi"
5+
local base = require "resty.core.base"
6+
7+
8+
local C = ffi.C
9+
local ffi_str = ffi.string
10+
local getfenv = getfenv
11+
local error = error
12+
local errmsg = base.get_errmsg_ptr()
13+
local get_string_buf = base.get_string_buf
14+
15+
16+
ffi.cdef[[
17+
int ngx_http_lua_ffi_ssl_set_serialized_session(ngx_http_request_t *r,
18+
const unsigned char *buf, int len, char **err);
19+
20+
int ngx_http_lua_ffi_ssl_get_serialized_session(ngx_http_request_t *r,
21+
char *buf, char **err);
22+
23+
int ngx_http_lua_ffi_ssl_get_session_id(ngx_http_request_t *r,
24+
char *buf, char **err);
25+
26+
int ngx_http_lua_ffi_ssl_get_serialized_session_size(ngx_http_request_t *r,
27+
char **err);
28+
29+
int ngx_http_lua_ffi_ssl_get_session_id_size(ngx_http_request_t *r,
30+
char **err);
31+
]]
32+
33+
34+
-- return session, err
35+
function _M.get_serialized_session()
36+
local r = getfenv(0).__ngx_req
37+
if not r then
38+
return error("no request found")
39+
end
40+
41+
local len = C.ngx_http_lua_ffi_ssl_get_serialized_session_size(r, errmsg)
42+
43+
if len < 0 then
44+
return nil, ffi_str(errmsg[0])
45+
end
46+
47+
if len > 4096 then
48+
return nil, "session too big to serialize"
49+
end
50+
local buf = get_string_buf(len)
51+
52+
local rc = C.ngx_http_lua_ffi_ssl_get_serialized_session(r, buf, errmsg)
53+
54+
if rc < 0 then
55+
return nil, ffi_str(errmsg[0])
56+
end
57+
58+
return ffi_str(buf, len)
59+
end
60+
61+
62+
-- return session_id, err
63+
function _M.get_session_id()
64+
local r = getfenv(0).__ngx_req
65+
if not r then
66+
return error("no request found")
67+
end
68+
69+
local len = C.ngx_http_lua_ffi_ssl_get_session_id_size(r, errmsg)
70+
71+
if len < 0 then
72+
return nil, ffi_str(errmsg[0])
73+
end
74+
75+
local buf = get_string_buf(len)
76+
77+
local rc = C.ngx_http_lua_ffi_ssl_get_session_id(r, buf, errmsg)
78+
79+
if rc < 0 then
80+
return nil, ffi_str(errmsg[0])
81+
end
82+
83+
return ffi_str(buf, len)
84+
end
85+
86+
87+
-- return ok, err
88+
function _M.set_serialized_session(sess)
89+
local r = getfenv(0).__ngx_req
90+
if not r then
91+
return error("no request found")
92+
end
93+
94+
local rc = C.ngx_http_lua_ffi_ssl_set_serialized_session(r, sess, #sess,
95+
errmsg)
96+
if rc < 0 then
97+
return nil, ffi_str(errmsg[0])
98+
end
99+
100+
return true
101+
end
102+
103+
104+
return _M

lib/ngx/ssl/session.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
Name
2+
====
3+
4+
ngx.ssl.session - Lua API for manipulating SSL session data and IDs for NGINX downstream SSL connections.
5+
6+
Status
7+
======
8+
9+
This Lua module is currently considered experimental.
10+
11+
Description
12+
===========
13+
14+
This Lua module provides API functions for manipulating SSL session data and IDs for NGINX
15+
downstream connections. It is mostly for the contexts [ssl_session_fetch_by_lua*](https://github.com/openresty/lua-nginx-module/tree/ssl-session#ssl_session_fetch_by_lua_block)
16+
and [ssl_session_store_by_lua*](https://github.com/openresty/lua-nginx-module/tree/ssl-session#ssl_session_store_by_lua_block).
17+
18+
This Lua API can be used to implement distributed SSL session caching for downstream SSL connections, thus saving a lot of full SSL handshakes which are very expensive.
19+
20+
To load the `ngx.ssl.session` module in Lua, just write
21+
22+
```lua
23+
local ssl_sess = require "ngx.ssl.session"
24+
```
25+
26+
Methods
27+
=======
28+
29+
get_session_id
30+
--------------
31+
**syntax:** *id, err = ssl_sess.get_session_id()*
32+
33+
**context:** *ssl_session_fetch_by_lua&#42;, ssl_session_store_by_lua&#42;*
34+
35+
Fetches the SSL session ID associated with the current downstream SSL connection.
36+
The ID is returned as a Lua string.
37+
38+
In case of errors, it returns `nil` and a string describing the error.
39+
40+
This API function is usually called in the contexts of
41+
[ssl_session_store_by_lua*](https://github.com/openresty/lua-nginx-module/tree/ssl-session#ssl_session_store_by_lua_block)
42+
and [ssl_session_fetch_by_lua*](https://github.com/openresty/lua-nginx-module/tree/ssl-session#ssl_session_fetch_by_lua_block).
43+
44+
get_serialized_session
45+
----------------------
46+
**syntax:** *session, err = ssl_sess.get_serialized_session()*
47+
48+
**context:** *ssl_session_store_by_lua&#42;*
49+
50+
Returns the serialized form of the SSL sesson data of the current SSL connection, in a Lua string.
51+
52+
This session can be cached in [lua-resty-lrucache](https://github.com/openresty/lua-resty-lrucache), [lua_shared_dict](https://github.com/openresty/lua-nginx-module#lua_shared_dict),
53+
and/or external data storage services like `memcached` and `redis`. The SSL session ID returned
54+
by the [get_session_id](#get_session_id) function is usually used as the cache key.
55+
56+
The returned SSL session data can later be loaded into other SSL connections using the same
57+
session ID via the [set_serialized_session](#set_serialized_session) function.
58+
59+
In case of errors, it returns `nil` and a string describing the error.
60+
61+
This API function is usually called in the context of
62+
[ssl_session_store_by_lua*](https://github.com/openresty/lua-nginx-module/tree/ssl-session#ssl_session_store_by_lua_block)
63+
where the SSL handshake has just completed.
64+
65+
set_serialized_session
66+
----------------------
67+
**syntax:** *ok, err = ssl_sess.set_serialized_session(session)*
68+
69+
**context:** *ssl_session_fetch_by_lua&#42;*
70+
71+
Sets the serialized SSL session provided as the argument to the current SSL connection.
72+
If the SSL session is successfully set, the current SSL connection can resume the session
73+
directly without going through the full SSL handshake process (which is very expensive in terms of CPU time).
74+
75+
This API is usually used in the context of [ssl_session_fetch_by_lua*](https://github.com/openresty/lua-nginx-module/tree/ssl-session#ssl_session_fetch_by_lua_block)
76+
when a cache hit is found with the current SSL session ID.
77+
78+
The serialized SSL session used as the argument should be originally returned by the
79+
[get_serialized_session](#get_serialized_session) function.
80+
81+
Community
82+
=========
83+
84+
[Back to TOC](#table-of-contents)
85+
86+
English Mailing List
87+
--------------------
88+
89+
The [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers.
90+
91+
[Back to TOC](#table-of-contents)
92+
93+
Chinese Mailing List
94+
--------------------
95+
96+
The [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers.
97+
98+
[Back to TOC](#table-of-contents)
99+
100+
Bugs and Patches
101+
================
102+
103+
Please report bugs or submit patches by
104+
105+
1. creating a ticket on the [GitHub Issue Tracker](https://github.com/openresty/lua-resty-core/issues),
106+
1. or posting to the [OpenResty community](#community).
107+
108+
[Back to TOC](#table-of-contents)
109+
110+
Author
111+
======
112+
113+
Yichun Zhang &lt;[email protected]&gt; (agentzh), CloudFlare Inc.
114+
115+
[Back to TOC](#table-of-contents)
116+
117+
Copyright and License
118+
=====================
119+
120+
This module is licensed under the BSD license.
121+
122+
Copyright (C) 2016, by Yichun "agentzh" Zhang, CloudFlare Inc.
123+
124+
All rights reserved.
125+
126+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
127+
128+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
129+
130+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
131+
132+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
133+
134+
[Back to TOC](#table-of-contents)
135+
136+
See Also
137+
========
138+
* the ngx_lua module: https://github.com/openresty/lua-nginx-module
139+
* the [ssl_session_fetch_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_session_fetch_by_lua_block) directive.
140+
* the [ssl_session_store_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_session_store_by_lua_block) directive.
141+
* library [lua-resty-core](https://github.com/openresty/lua-resty-core)
142+
* OpenResty: https://openresty.org
143+
144+
[Back to TOC](#table-of-contents)

0 commit comments

Comments
 (0)