Skip to content

Commit

Permalink
udpate test case and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinsRan committed Jul 28, 2023
1 parent be8a5ba commit 5d385be
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 15 deletions.
20 changes: 11 additions & 9 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,20 @@ function _M.http_ssl_phase()
end

function _M.http_ssl_protocols_phase()
local ssl_clt = require "ngx.ssl.clienthello"
local host, err = ssl_clt.get_client_hello_server_name()

if err then
core.log.error("failed to get the SNI name: ", err)
local sni, err = apisix_ssl.server_name(true)
if not sni or type(sni) ~= "string" then
local advise = "please check if the client requests via IP or uses an outdated " ..
"protocol. If you need to report an issue, " ..
"provide a packet capture file of the TLS handshake."
core.log.error("failed to find SNI: " .. (err or advise))
ngx_exit(-1)
end

local ngx_ctx = ngx.ctx
local api_ctx = core.tablepool.fetch("api_ctx", 0, 32)
ngx_ctx.api_ctx = api_ctx

local ok, err = router.router_ssl.match_and_set(api_ctx, true, host)
local ok, err = router.router_ssl.match_and_set(api_ctx, true, sni)

ngx_ctx.matched_ssl = api_ctx.matched_ssl
core.tablepool.release("api_ctx", api_ctx)
Expand All @@ -213,9 +214,10 @@ function _M.http_ssl_protocols_phase()
ngx_exit(-1)
end

local ssl_protocols = ngx_ctx.matched_ssl.value.ssl_protocols
if ssl_protocols then
ssl_clt.set_protocols(ssl_protocols)
ok ,err = apisix_ssl.set_protocols_by_clienthello(ngx_ctx.matched_ssl.value.ssl_protocols)
if not ok then
core.log.error("failed to set ssl protocols: ", err)
ngx_exit(-1)
end
end

Expand Down
23 changes: 18 additions & 5 deletions apisix/ssl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local core = require("apisix.core")
local ngx_ssl = require("ngx.ssl")
local secret = require("apisix.secret")
local core = require("apisix.core")
local ngx_ssl = require("ngx.ssl")
local secret = require("apisix.secret")
local ngx_ssl_client = require("ngx.ssl.clienthello")
local ngx_encode_base64 = ngx.encode_base64
local ngx_decode_base64 = ngx.decode_base64
local aes = require("resty.aes")
Expand All @@ -38,8 +39,14 @@ local pkey_cache = core.lrucache.new {
local _M = {}


function _M.server_name()
local sni, err = ngx_ssl.server_name()
function _M.server_name(clienthello)
local sni, err
if clienthello then
sni, err = ngx_ssl_client.get_client_hello_server_name()
else
sni, err = ngx_ssl.server_name()

end
if err then
return nil, err
end
Expand All @@ -56,6 +63,12 @@ function _M.server_name()
return sni
end

function _M.set_protocols_by_clienthello(ssl_protocols)
if ssl_protocols then
return ngx_ssl_client.set_protocols(ssl_protocols)
end
return true
end

local function init_iv_tbl(ivs)
local _aes_128_cbc_with_iv_tbl = core.table.new(2, 0)
Expand Down
4 changes: 4 additions & 0 deletions docs/en/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@
{
"type": "doc",
"id": "profile"
},
{
"type": "doc",
"id": "ssl-protocol"
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions docs/zh/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@
{
"type": "doc",
"id": "profile"
},
{
"type": "doc",
"id": "ssl-protocol"
}
]
},
Expand Down
45 changes: 45 additions & 0 deletions t/node/ssl-protocols.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ log_level('info');
no_root_location();
no_shuffle();

$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();

add_block_preprocessor(sub {
my ($block) = @_;

Expand Down Expand Up @@ -286,3 +288,46 @@ qr/TLSv1\.1 \(IN\), TLS handshake, Server hello(?s).*hello world/
curl -k -v --tls-max 1.3 --tlsv1.3 --resolve "test.com:1994:127.0.0.1" https://test.com:1994/hello 2>&1 | cat
--- response_body eval
qr/TLSv1\.3 \(IN\), TLS alert/
=== TEST 15: hello
--- config
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
location /t {
content_by_lua_block {
do
local sock = ngx.socket.tcp()
local ssl = require "ssl"
sock:settimeout(2000)
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
if not ok then
ngx.say("failed to connect: ", err)
return
end
local params = {
mode = "client",
protocol = "tlsv1_3",
verify = "none",
options = "all",
}
local sec_sock = ssl.wrap(sock, params)
local sess, err = sec_sock:dohandshake()
if not sess then
ngx.say("failed to do SSL handshake: ", err)
return
end
ngx.say("ssl handshake: ", sess ~= nil)
end -- do
-- collectgarbage()
}
}
--- request
GET /t
--- response_body
ssl handshake: true
2 changes: 1 addition & 1 deletion t/router/radixtree-sni2.t
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ location /t {
--- response_body
failed to do SSL handshake: handshake failed
--- error_log
failed to fetch ssl config: failed to find SNI: please check if the client requests via IP or uses an outdated protocol
failed to find SNI: please check if the client requests via IP or uses an outdated protocol
--- no_error_log
[alert]
Expand Down

0 comments on commit 5d385be

Please sign in to comment.