1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
Name
====
ngx.ssl.clienthello - Lua API for post-processing SSL client hello message for NGINX downstream SSL connections.
Table of Contents
=================
* [Name](#name)
* [Status](#status)
* [Synopsis](#synopsis)
* [Description](#description)
* [Methods](#methods)
* [get_client_hello_server_name](#get_client_hello_server_name)
* [get_supported_versions](#get_supported_versions)
* [get_client_hello_ciphers](#get_client_hello_ciphers)
* [get_client_hello_ext_present](#get_client_hello_ext_present)
* [get_client_hello_ext](#get_client_hello_ext)
* [set_protocols](#set_protocols)
* [Community](#community)
* [English Mailing List](#english-mailing-list)
* [Chinese Mailing List](#chinese-mailing-list)
* [Bugs and Patches](#bugs-and-patches)
* [Author](#author)
* [Copyright and License](#copyright-and-license)
* [See Also](#see-also)
Status
======
This Lua module is production ready.
Synopsis
========
```nginx
# nginx.conf
server {
listen 443 ssl;
server_name test.com;
ssl_certificate /path/to/cert.crt;
ssl_certificate_key /path/to/key.key;
ssl_client_hello_by_lua_block {
local ssl_clt = require "ngx.ssl.clienthello"
local host, err = ssl_clt.get_client_hello_server_name()
if host == "test.com" then
ssl_clt.set_protocols({"TLSv1", "TLSv1.1"})
elseif host == "test2.com" then
ssl_clt.set_protocols({"TLSv1.2", "TLSv1.3"})
elseif not host then
ngx.log(ngx.ERR, "failed to get the SNI name: ", err)
ngx.exit(ngx.ERROR)
else
ngx.log(ngx.ERR, "unknown SNI name: ", host)
ngx.exit(ngx.ERROR)
end
}
...
}
server {
listen 443 ssl;
server_name test2.com;
ssl_certificate /path/to/cert.crt;
ssl_certificate_key /path/to/key.key;
...
}
```
Description
===========
This Lua module provides API functions for post-processing SSL client hello message for NGINX downstream connections.
It must be used in the context [ssl_client_hello_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_client_hello_by_lua_block).
This Lua API is particularly useful for dynamically setting the SSL protocols according to the SNI.
It is also useful to do some custom operations according to the per-connection information in the client hello message.
For example, one can parse the custom client hello extension and do the corresponding handling in pure Lua.
To load the `ngx.ssl.clienthello` module in Lua, just write
```lua
local ssl_clt = require "ngx.ssl.clienthello"
```
[Back to TOC](#table-of-contents)
Methods
=======
get_client_hello_server_name
--------------
**syntax:** *host, err = ssl_clt.get_client_hello_server_name()*
**context:** *ssl_client_hello_by_lua**
Returns the TLS SNI (Server Name Indication) name set by the client.
Return `nil` when then the extension does not exist.
In case of errors, it returns `nil` and a string describing the error.
Note that the SNI name is gotten from the raw extensions of the client hello message associated with the current downstream SSL connection.
So this function can only be called in the context of [ssl_client_hello_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_client_hello_by_lua_block).
[Back to TOC](#table-of-contents)
get_supported_versions
--------------
**syntax:** *types, err = ssl_clt.get_supported_versions()*
**context:** *ssl_client_hello_by_lua**
Returns the table of ssl hello supported versions set by the client.
Return `nil` when then the extension does not exist.
In case of errors, it returns `nil` and a string describing the error.
Note that the types is gotten from the raw extensions of the client hello message associated with the current downstream SSL connection.
So this function can only be called in the context of [ssl_client_hello_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_client_hello_by_lua_block).
[Back to TOC](#table-of-contents)
get_client_hello_ciphers
----------------------------
**syntax:** *ciphers, err = ssl_clt.get_client_hello_ciphers()*
**context:** *ssl_client_hello_by_lua**
Returns a Lua table containing the decimal representations of the ciphers sent by the client on success.
GREASE ciphers are also returned by the underlying OPENSSL function (SSL_client_hello_get0_ciphers) but excluded by the lua implementation of get_client_hello_ciphers().
In case of errors, `nil` and a string describing the error are returned.
This function can only be called in the context of [ssl_client_hello_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_client_hello_by_lua_block).
Example:
```nginx
# nginx.conf
server {
listen 443 ssl;
server_name test.com;
ssl_client_hello_by_lua_block {
local ssl_clt = require "ngx.ssl.clienthello"
local ciphers, err = ssl_clt.get_client_hello_ciphers()
if not ciphers then
ngx.log(ngx.ERR, "failed to get_client_hello_ciphers()")
ngx.exit(ngx.ERROR)
end
for i, cipher in ipairs(ciphers) do
ngx.log(ngx.INFO, "ciphers ", cipher)
end
}
ssl_certificate test.crt;
ssl_certificate_key test.key;
}
```
get_client_hello_ext_present
----------------------------
**syntax:** *ext, err = ssl_clt.get_client_hello_ext_present()*
**context:** *ssl_client_hello_by_lua**
Returns a Lua table contains the extension types on success.
In case of errors, it returns `nil` and a string describing the error.
Note that the ext is gotten from the raw extensions of the client hello message associated with the current downstream SSL connection.
So this function can only be called in the context of [ssl_client_hello_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_client_hello_by_lua_block).
GREASE extensions are excluded by the underlying OPENSSL function (SSL_client_hello_get1_extensions_present)
Most modern browsers will randomize the order of the extensions so you may want to sort the table before working with it.
Example:
```nginx
# nginx.conf
server {
listen 443 ssl;
server_name test.com;
ssl_client_hello_by_lua_block {
local ssl_clt = require "ngx.ssl.clienthello"
local exts = ssl_clt.get_client_hello_ext_present()
if not exts then
ngx.log(ngx.ERR, "failed to get_client_hello_ext_present()")
ngx.exit(ngx.ERROR)
end
for i, ext in ipairs(exts) do
ngx.log(ngx.INFO, "extension ", ext)
end
}
ssl_certificate test.crt;
ssl_certificate_key test.key;
}
```
[Back to TOC](#table-of-contents)
get_client_hello_ext
--------------------
**syntax:** *ext, err = ssl_clt.get_client_hello_ext(ext_type)*
**context:** *ssl_client_hello_by_lua**
Returns raw data of arbitrary SSL client hello extension including custom extensions.
Returns `nil` if the specified extension type does not exist.
In case of errors, it returns `nil` and a string describing the error.
Note that the ext is gotten from the raw extensions of the client hello message associated with the current downstream SSL connection.
So this function can only be called in the context of [ssl_client_hello_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_client_hello_by_lua_block).
Example:
Gets server name from raw extension data. The `0` in `ssl_clt.get_client_hello_ext(0)` denotes `TLSEXT_TYPE_server_name`, and the `0` in `byte(ext, 3) ~= 0` denotes `TLSEXT_NAMETYPE_host_name`.
```nginx
# nginx.conf
server {
listen 443 ssl;
server_name test.com;
ssl_client_hello_by_lua_block {
local ssl_clt = require "ngx.ssl.clienthello"
local byte = string.byte
local ext = ssl_clt.get_client_hello_ext(0)
if not ext then
print("failed to get_client_hello_ext(0)")
ngx.exit(ngx.ERROR)
end
local total_len = string.len(ext)
if total_len <= 2 then
print("bad SSL Client Hello Extension")
ngx.exit(ngx.ERROR)
end
local len = byte(ext, 1) * 256 + byte(ext, 2)
if len + 2 ~= total_len then
print("bad SSL Client Hello Extension")
ngx.exit(ngx.ERROR)
end
if byte(ext, 3) ~= 0 then
print("bad SSL Client Hello Extension")
ngx.exit(ngx.ERROR)
end
if total_len <= 5 then
print("bad SSL Client Hello Extension")
ngx.exit(ngx.ERROR)
end
len = byte(ext, 4) * 256 + byte(ext, 5)
if len + 5 > total_len then
print("bad SSL Client Hello Extension")
ngx.exit(ngx.ERROR)
end
local name = string.sub(ext, 6, 6 + len -1)
print("read SNI name from Lua: ", name)
}
ssl_certificate test.crt;
ssl_certificate_key test.key;
}
```
[Back to TOC](#table-of-contents)
set_protocols
----------------------
**syntax:** *ok, err = ssl_clt.set_protocols(protocols)*
**context:** *ssl_client_hello_by_lua**
Sets the SSL protocols supported by the current downstream SSL connection.
Returns `true` on success, or a `nil` value and a string describing the error otherwise.
Considering it is meaningless to set ssl protocols after the protocol is determined,
so this function may only be called in the context of [ssl_client_hello_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_client_hello_by_lua_block).
Example: `ssl_clt.set_protocols({"TLSv1.1", "TLSv1.2", "TLSv1.3"})`
[Back to TOC](#table-of-contents)
Community
=========
[Back to TOC](#table-of-contents)
English Mailing List
--------------------
The [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers.
[Back to TOC](#table-of-contents)
Chinese Mailing List
--------------------
The [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers.
[Back to TOC](#table-of-contents)
Bugs and Patches
================
Please report bugs or submit patches by
1. creating a ticket on the [GitHub Issue Tracker](https://github.com/openresty/lua-resty-core/issues),
1. or posting to the [OpenResty community](#community).
[Back to TOC](#table-of-contents)
Author
======
Zhefeng Chen <chzf\_zju@163.com> (catbro666)
[Back to TOC](#table-of-contents)
Copyright and License
=====================
This module is licensed under the BSD license.
Copyright (C) 2016-2017, by Yichun "agentzh" Zhang, OpenResty Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 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.
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.
[Back to TOC](#table-of-contents)
See Also
========
* the ngx_lua module: https://github.com/openresty/lua-nginx-module
* the [ssl_client_hello_by_lua*](https://github.com/openresty/lua-nginx-module/#ssl_client_hello_by_lua_block) directive.
* the [lua-resty-core](https://github.com/openresty/lua-resty-core) library.
* OpenResty: https://openresty.org
[Back to TOC](#table-of-contents)
|