Skip to content

Commit 961bd7b

Browse files
perfgaoclaude
andauthored
fix(*) correct error message from bn:mod_sqr and pkey (#233)
- pkey.lua: use builtin tostring (not the file-local __tostring serializer) and preserve the original padding value in the "invalid padding" error message. - bn.lua: fix copy-paste mistakes in error reporting — mod_sqr now tags check_args as "mod_sqr" (was "mod_sub"), and __shr reports "BN_rshift() failed" (was "BN_lshift() failed"). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 01e08f6 commit 961bd7b

4 files changed

Lines changed: 50 additions & 5 deletions

File tree

lib/resty/openssl/bn.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ for _, op in ipairs({ "add", "sub" , "mul", "exp" }) do
371371
end
372372

373373
function _M.mod_sqr(...)
374-
local r, a, m = check_args("mod_sub", ...)
374+
local r, a, m = check_args("mod_sqr", ...)
375375
if C.BN_mod_sqr(r.ctx, a, m, bn_ctx_tmp) == 0 then
376376
error("BN_mod_sqr() failed")
377377
end
@@ -401,7 +401,7 @@ _M.lshift = mt.__shl
401401
function mt.__shr(a, b)
402402
local r, a = check_args("rshift", a)
403403
if C.BN_rshift(r.ctx, a, b) == 0 then
404-
error("BN_lshift() failed")
404+
error("BN_rshift() failed")
405405
end
406406
return r
407407
end

lib/resty/openssl/pkey.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,11 @@ local function asymmetric_routine(self, s, op, padding, opts)
738738

739739
if self.key_type == evp_macro.EVP_PKEY_RSA then
740740
if padding then
741-
padding = tonumber(padding)
742-
if not padding then
743-
return nil, "invalid padding: " .. __tostring(padding)
741+
local p = tonumber(padding)
742+
if not p then
743+
return nil, "invalid padding: " .. tostring(padding)
744744
end
745+
padding = p
745746
else
746747
padding = rsa_macro.paddings.RSA_PKCS1_PADDING
747748
end

t/openssl/bn.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,23 @@ true
592592
"
593593
--- no_error_log
594594
[error]
595+
596+
597+
598+
=== TEST: mod_sqr: error message names the correct op
599+
--- http_config eval: $::HttpConfig
600+
--- config
601+
location =/t {
602+
content_by_lua_block {
603+
local bn = require("resty.openssl.bn")
604+
local a = myassert(bn.new(3))
605+
local ok, err = pcall(function() return bn.mod_sqr(a, "not-a-bn", 5) end)
606+
ngx.say(ok)
607+
ngx.say(err)
608+
}
609+
}
610+
--- request
611+
GET /t
612+
--- response_body_like: mod_sqr
613+
--- no_error_log
614+
[error]

t/openssl/pkey.t

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,3 +1493,27 @@ default
14931493
"
14941494
--- no_error_log
14951495
[error]
1496+
1497+
1498+
1499+
=== TEST: encryption: invalid padding error message preserves user input
1500+
--- http_config eval: $::HttpConfig
1501+
--- config
1502+
location =/t {
1503+
content_by_lua_block {
1504+
local privkey = myassert(require("resty.openssl.pkey").new())
1505+
local pubkey = myassert(require("resty.openssl.pkey").new(assert(privkey:to_PEM("public"))))
1506+
1507+
local ok, err = pubkey:encrypt("23333", "bad_pad")
1508+
ngx.say(ok)
1509+
ngx.say(err)
1510+
}
1511+
}
1512+
--- request
1513+
GET /t
1514+
--- response_body eval
1515+
"nil
1516+
invalid padding: bad_pad
1517+
"
1518+
--- no_error_log
1519+
[error]

0 commit comments

Comments
 (0)