Skip to content

Commit 1855461

Browse files
committed
feature: proxy_ssl_certificate_by_lua directives
1 parent 991e708 commit 1855461

File tree

7 files changed

+950
-0
lines changed

7 files changed

+950
-0
lines changed

lib/ngx/proxyssl.lua

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
-- Copyright (C) Yichun Zhang (agentzh)
2+
3+
4+
local base = require "resty.core.base"
5+
base.allows_subsystem('http', 'stream')
6+
7+
8+
local ffi = require "ffi"
9+
local C = ffi.C
10+
local ffi_str = ffi.string
11+
local get_request = base.get_request
12+
local error = error
13+
local tonumber = tonumber
14+
local errmsg = base.get_errmsg_ptr()
15+
local subsystem = ngx.config.subsystem
16+
17+
18+
local ngx_lua_ffi_proxy_ssl_get_tls1_version
19+
20+
21+
if subsystem == 'http' then
22+
ffi.cdef[[
23+
int ngx_http_lua_ffi_proxy_ssl_get_tls1_version(ngx_http_request_t *r,
24+
char **err);
25+
]]
26+
27+
ngx_lua_ffi_proxy_ssl_get_tls1_version =
28+
C.ngx_http_lua_ffi_proxy_ssl_get_tls1_version
29+
30+
elseif subsystem == 'stream' then
31+
ffi.cdef[[
32+
int ngx_stream_lua_ffi_proxy_ssl_get_tls1_version(
33+
ngx_stream_lua_request_t *r, char **err);
34+
]]
35+
36+
ngx_lua_ffi_proxy_ssl_get_tls1_version =
37+
C.ngx_stream_lua_ffi_proxy_ssl_get_tls1_version
38+
end
39+
40+
41+
local _M = { version = base.version }
42+
43+
44+
local function get_tls1_version()
45+
46+
local r = get_request()
47+
if not r then
48+
error("no request found")
49+
end
50+
51+
local ver = ngx_lua_ffi_proxy_ssl_get_tls1_version(r, errmsg)
52+
53+
ver = tonumber(ver)
54+
55+
if ver >= 0 then
56+
return ver
57+
end
58+
59+
-- rc == FFI_ERROR
60+
61+
return nil, ffi_str(errmsg[0])
62+
end
63+
_M.get_tls1_version = get_tls1_version
64+
65+
66+
do
67+
_M.SSL3_VERSION = 0x0300
68+
_M.TLS1_VERSION = 0x0301
69+
_M.TLS1_1_VERSION = 0x0302
70+
_M.TLS1_2_VERSION = 0x0303
71+
_M.TLS1_3_VERSION = 0x0304
72+
73+
local map = {
74+
[_M.SSL3_VERSION] = "SSLv3",
75+
[_M.TLS1_VERSION] = "TLSv1",
76+
[_M.TLS1_1_VERSION] = "TLSv1.1",
77+
[_M.TLS1_2_VERSION] = "TLSv1.2",
78+
[_M.TLS1_3_VERSION] = "TLSv1.3",
79+
}
80+
81+
function _M.get_tls1_version_str()
82+
local ver, err = get_tls1_version()
83+
if not ver then
84+
return nil, err
85+
end
86+
87+
local ver_str = map[ver]
88+
if not ver_str then
89+
return nil, "unknown version"
90+
end
91+
92+
return ver_str
93+
end
94+
end
95+
96+
97+
return _M

lib/ngx/proxyssl.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
Name
2+
====
3+
4+
ngx.proxyssl - Lua API for controlling NGINX upstream SSL handshakes
5+
6+
Table of Contents
7+
=================
8+
9+
* [Name](#name)
10+
* [Status](#status)
11+
* [Synopsis](#synopsis)
12+
* [Description](#description)
13+
* [Methods](#methods)
14+
* [get_tls1_version](#get_tls1_version)
15+
* [get_tls1_version_str](#get_tls1_version_str)
16+
* [Community](#community)
17+
* [English Mailing List](#english-mailing-list)
18+
* [Chinese Mailing List](#chinese-mailing-list)
19+
* [Bugs and Patches](#bugs-and-patches)
20+
* [Author](#author)
21+
* [Copyright and License](#copyright-and-license)
22+
* [See Also](#see-also)
23+
24+
Status
25+
======
26+
27+
This Lua module is production ready.
28+
29+
Synopsis
30+
========
31+
32+
```nginx
33+
server {
34+
listen 443 ssl;
35+
server_name test.com;
36+
37+
proxy_ssl_certificate_by_lua_block {
38+
local proxy_ssl = require "ngx.proxyssl"
39+
40+
local ver, err = proxy_ssl.get_tls1_version_str()
41+
if not ver then
42+
ngx.log(ngx.ERR, "failed to get TLS1 version: ", err)
43+
return
44+
end
45+
ngx.log(ngx.INFO, "got TLS1 version: ", ver)
46+
}
47+
48+
location / {
49+
root html;
50+
}
51+
}
52+
```
53+
54+
Description
55+
===========
56+
57+
This Lua module provides API functions to control the SSL handshake process in contexts like [proxy_ssl_certificate_by_lua*](https://github.com/openresty/lua-nginx-module/#proxy_ssl_certificate_by_lua_block) and [proxy_ssl_verify_by_lua*](https://github.com/openresty/lua-nginx-module/#proxy_ssl_verify_by_lua_block) (of the [ngx_lua](https://github.com/openresty/lua-nginx-module#readme) module).
58+
59+
This Lua module only provides API functions that can be used in any upstream SSL context, so here we make it an independent module.
60+
61+
To load the `ngx.proxyssl` module in Lua, just write
62+
63+
```lua
64+
local proxy_ssl = require "ngx.proxyssl"
65+
```
66+
67+
[Back to TOC](#table-of-contents)
68+
69+
Methods
70+
=======
71+
72+
get_tls1_version
73+
----------------
74+
**syntax:** *ver, err = proxy_ssl.get_tls1_version()*
75+
76+
**context:** *any*
77+
78+
It's the same as [ngx.ssl.get_tls1_version](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl.md#get_tls1_version), but for the current upstream SSL connection.
79+
80+
This function can be called in any context where upstream https is used.
81+
82+
[Back to TOC](#table-of-contents)
83+
84+
get_tls1_version_str
85+
--------------------
86+
**syntax:** *ver, err = proxy_ssl.get_tls1_version_str()*
87+
88+
**context:** *any*
89+
90+
It's the same as [ngx.ssl.get_tls1_version_str](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl.md#get_tls1_version_str), but for the current upstream SSL connection.
91+
92+
This function can be called in any context where upstream https is used.
93+
94+
[Back to TOC](#table-of-contents)
95+
96+
Community
97+
=========
98+
99+
[Back to TOC](#table-of-contents)
100+
101+
English Mailing List
102+
--------------------
103+
104+
The [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers.
105+
106+
[Back to TOC](#table-of-contents)
107+
108+
Chinese Mailing List
109+
--------------------
110+
111+
The [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers.
112+
113+
[Back to TOC](#table-of-contents)
114+
115+
Bugs and Patches
116+
================
117+
118+
Please report bugs or submit patches by
119+
120+
1. creating a ticket on the [GitHub Issue Tracker](https://github.com/openresty/lua-resty-core/issues),
121+
2. or posting to the [OpenResty community](#community).
122+
123+
[Back to TOC](#table-of-contents)
124+
125+
Author
126+
======
127+
128+
Fuhong Ma <willmafh@hotmail.com> (willmafh)
129+
130+
[Back to TOC](#table-of-contents)
131+
132+
Copyright and License
133+
=====================
134+
135+
This module is licensed under the BSD license.
136+
137+
Copyright (C) 2015-2017, by Yichun "agentzh" Zhang, OpenResty Inc.
138+
139+
All rights reserved.
140+
141+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
142+
143+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
144+
145+
* 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.
146+
147+
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.
148+
149+
[Back to TOC](#table-of-contents)
150+
151+
See Also
152+
========
153+
* the ngx_lua module: https://github.com/openresty/lua-nginx-module
154+
* the [proxy_ssl_certificate_by_lua*](https://github.com/openresty/lua-nginx-module/#proxy_ssl_certificate_by_lua_block) directive.
155+
* the [proxy_ssl_verify_by_lua*](https://github.com/openresty/lua-nginx-module/#proxy_ssl_verify_by_lua_block) directive.
156+
* the [lua-resty-core](https://github.com/openresty/lua-resty-core) library.
157+
* OpenResty: https://openresty.org
158+
159+
[Back to TOC](#table-of-contents)

0 commit comments

Comments
 (0)