Skip to content

Commit

Permalink
fix: check decrypt key to prevent lua thread aborted (#2815)
Browse files Browse the repository at this point in the history
Fix #2791
  • Loading branch information
starsz authored Nov 24, 2020
1 parent 39840f9 commit 95226d9
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 6 deletions.
14 changes: 10 additions & 4 deletions apisix/ssl/router/radixtree_sni.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ local function decrypt_priv_pkey(iv, key)
return key
end

local decrypted = iv:decrypt(ngx_decode_base64(key))
if decrypted then
return decrypted
local decoded_key = ngx_decode_base64(key)
if not decoded_key then
core.log.error("base64 decode ssl key failed and skipped. key[", key, "] ")
return
end

core.log.error("decrypt ssl key failed. key[", key, "] ")
local decrypted = iv:decrypt(decoded_key)
if not decrypted then
core.log.error("decrypt ssl key failed and skipped. key[", key, "] ")
end

return decrypted
end


Expand Down
12 changes: 12 additions & 0 deletions t/certs/incorrect.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
test not base64 encoded crt
12 changes: 12 additions & 0 deletions t/certs/incorrect.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
test not base64 encoded key
83 changes: 81 additions & 2 deletions t/router/radixtree-sni.t
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ GET /t
connected: 1
failed to do SSL handshake: handshake failed
--- error_log
decrypt ssl key failed.
decrypt ssl key failed and skipped.



Expand Down Expand Up @@ -1253,4 +1253,83 @@ GET /t
connected: 1
failed to do SSL handshake: handshake failed
--- error_log
decrypt ssl key failed.
decrypt ssl key failed and skipped.



=== TEST 28: set miss_head ssl certificate
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local t = require("lib.test_admin")

--TODO: check the ssl certificate in admin ssl API
local ssl_cert = t.read_file("t/certs/incorrect.crt")
local ssl_key = t.read_file("t/certs/incorrect.key")
local data = {cert = ssl_cert, key = ssl_key, sni = "www.test.com"}

local code, body = t.test('/apisix/admin/ssl/1',
ngx.HTTP_PUT,
core.json.encode(data),
[[{
"node": {
"value": {
"sni": "www.test.com"
},
"key": "/apisix/ssl/1"
},
"action": "set"
}]]
)

ngx.status = code
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 29: test illegal ssl certificate
--- config
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
location /t {
content_by_lua_block {
-- etcd sync
ngx.sleep(0.2)

do
local sock = ngx.socket.tcp()

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

ngx.say("connected: ", ok)

local sess, err = sock:sslhandshake(nil, "www.test.com", true)
if not sess then
ngx.say("failed to do SSL handshake: ", err)
return
end
end -- do
-- collectgarbage()
}
}
--- request
GET /t
--- response_body
connected: 1
failed to do SSL handshake: handshake failed
--- error_log
base64 decode ssl key failed and skipped.

0 comments on commit 95226d9

Please sign in to comment.