Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/resty/evp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ local function _new_key(self, opts)
ffi_gc(key, _C.EC_KEY_free)
end

if not key then
if key == nil then
return _err()
end

Expand Down Expand Up @@ -553,7 +553,13 @@ function ECVerifier.get_der_sig(self, signature)
end
-- inspired from https://bit.ly/2yZxzxJ
local ec = _C.EVP_PKEY_get0_EC_KEY(self.evp_pkey)
if ec == nil then
return nil, "key is not an EC key"
end
local ecgroup = _C.EC_KEY_get0_group(ec)
if ecgroup == nil then
return nil, "EC key has no group"
end

local order = _C.BN_new()
ffi_gc(order, _C.BN_free)
Expand Down
62 changes: 61 additions & 1 deletion t/sign-verify.t
Original file line number Diff line number Diff line change
Expand Up @@ -873,4 +873,64 @@ true
everything is awesome~ :p
bar
--- no_error_log
[error]
[error]

=== TEST 27: RS256 malformed private key returns error not crash
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local jwt = require "resty.jwt"
local ok, ret = pcall(function()
return jwt:sign(
"-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEAgarbage\n-----END RSA PRIVATE KEY-----\n",
{ header = { typ = "JWT", alg = "RS256" }, payload = { foo = "bar" } }
)
end)
if ok then
ngx.say("FAIL: expected error, got token")
else
ngx.say("OK: " .. tostring(ret.reason or ret))
end
}
}
--- request
GET /t
--- response_body_like: ^OK: .*
--- no_error_log
[error]


=== TEST 28: ES256 JWT verified with RSA public key returns error not crash
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local jwt = require "resty.jwt"
local function get_testcert(name)
local f = io.open("/lua-resty-jwt/testcerts/" .. name)
local contents = f:read("*all")
f:close()
return contents
end
-- craft a minimal ES256 token (signature bytes do not matter;
-- the crash happens before signature verification)
local function b64url(s)
return ngx.encode_base64(s):gsub('+','-'):gsub('/','_'):gsub('=','')
end
local token = b64url('{"typ":"JWT","alg":"ES256"}') ..
"." .. b64url('{"sub":"test"}') ..
"." .. b64url(string.rep("A", 64))
local jwt_obj = jwt:verify(get_testcert("pubkey.pem"), token)
if jwt_obj.verified then
ngx.say("FAIL: should not be verified")
else
ngx.say("OK: " .. jwt_obj.reason)
end
}
}
--- request
GET /t
--- response_body_like: ^OK: .*
--- no_error_log
[error]
Loading