Skip to content

Commit 8f52c25

Browse files
committed
fix(*) add prefix to all error messages
1 parent df0c06f commit 8f52c25

33 files changed

+286
-256
lines changed

lib/resty/openssl/asn1.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,23 @@ local function asn1_to_unix(asn1)
4040
end
4141
local month = tonumber(s:sub(yyoffset+1, yyoffset+2))
4242
if month > 12 or month < 1 then
43-
return nil, "bad format " .. s
43+
return nil, "asn1.asn1_to_unix: bad format " .. s
4444
end
4545
local day = tonumber(s:sub(yyoffset+3, yyoffset+4))
4646
if day > 31 or day < 1 then
47-
return nil, "bad format " .. s
47+
return nil, "asn1.asn1_to_unix: bad format " .. s
4848
end
4949
local hour = tonumber(s:sub(yyoffset+5, yyoffset+6))
5050
if hour > 23 or hour < 0 then
51-
return nil, "bad format " .. s
51+
return nil, "asn1.asn1_to_unix: bad format " .. s
5252
end
5353
local minute = tonumber(s:sub(yyoffset+7, yyoffset+8))
5454
if minute > 59 or hour < 0 then
55-
return nil, "bad format " .. s
55+
return nil, "asn1.asn1_to_unix: bad format " .. s
5656
end
5757
local second = tonumber(s:sub(yyoffset+9, yyoffset+10))
5858
if second > 59 or second < 0 then
59-
return nil, "bad format " .. s
59+
return nil, "asn1.asn1_to_unix: bad format " .. s
6060
end
6161

6262
local tm
@@ -74,7 +74,7 @@ local function asn1_to_unix(asn1)
7474
local hh = tonumber(s:sub(yyoffset+12, yyoffset+13) or 'no')
7575
local mm = tonumber(s:sub(yyoffset+14, yyoffset+15) or 'no')
7676
if not hh or not mm then
77-
return nil, "bad format " .. s
77+
return nil, "asn1.asn1_to_unix: bad format " .. s
7878
end
7979
tm = tm + sgn * (hh * 3600 + mm * 60)
8080
end

lib/resty/openssl/aux/jwk.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ end
124124
function _M.load_jwk(txt)
125125
local tbl, err = cjson.decode(txt)
126126
if err then
127-
return nil, "error decoding jwk json: " .. err
127+
return nil, "error decoding JSON from JWK: " .. err
128+
elseif type(tbl) ~= "table" then
129+
return nil, "except input to be decoded as a table, got " .. type(tbl)
128130
end
129131

130132
local key, key_free, key_type, err
@@ -155,7 +157,7 @@ function _M.load_jwk(txt)
155157
end
156158

157159
if err then
158-
return nil, "failed to construct key from JWK: " .. err
160+
return nil, "failed to construct " .. tbl["kty"] .. " key from JWK: " .. err
159161
end
160162

161163
local ctx = C.EVP_PKEY_new()

lib/resty/openssl/bn.lua

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function _M.new(bn)
2525
return nil, format_error("bn.new")
2626
end
2727
elseif bn then
28-
return nil, "expect nil or a number at #1"
28+
return nil, "bn.new: expect nil or a number at #1"
2929
end
3030

3131
return setmetatable( { ctx = ctx }, mt), nil
@@ -37,7 +37,7 @@ end
3737

3838
function _M.dup(ctx)
3939
if not ffi.istype(bn_ptr_ct, ctx) then
40-
return nil, "expect a bn ctx at #1"
40+
return nil, "bn.dup: expect a bn ctx at #1"
4141
end
4242
local ctx = C.BN_dup(ctx)
4343
ffi_gc(ctx, C.BN_free)
@@ -64,7 +64,7 @@ end
6464

6565
function _M.from_binary(s)
6666
if type(s) ~= "string" then
67-
return nil, "expect a string at #1"
67+
return nil, "bn.from_binary: expect a string at #1"
6868
end
6969

7070
local ctx = C.BN_bin2bn(s, #s, nil)
@@ -87,7 +87,7 @@ end
8787

8888
function _M.from_hex(s)
8989
if type(s) ~= "string" then
90-
return nil, "expect a string at #1"
90+
return nil, "bn.from_hex: expect a string at #1"
9191
end
9292

9393
local p = ffi.new(bn_ptrptr_ct)
@@ -113,7 +113,7 @@ mt.__tostring = _M.to_dec
113113

114114
function _M.from_dec(s)
115115
if type(s) ~= "string" then
116-
return nil, "expect a string at #1"
116+
return nil, "bn.from_dec: expect a string at #1"
117117
end
118118

119119
local p = ffi.new(bn_ptrptr_ct)
@@ -353,10 +353,10 @@ if OPENSSL_10 then
353353
local ctx = C.BN_new()
354354
ffi_gc(ctx, C.BN_free)
355355
if ctx == nil then
356-
return nil, "BN_new() failed"
356+
return nil, "bn:is_word: BN_new() failed"
357357
end
358358
if C.BN_set_word(ctx, n) ~= 1 then
359-
return nil, "BN_set_word() failed"
359+
return nil, "bn:is_word: BN_set_word() failed"
360360
end
361361
return C.BN_cmp(self.ctx, ctx) == 0
362362
end
@@ -384,7 +384,7 @@ end
384384

385385
function _M:is_prime(nchecks)
386386
if nchecks and type(nchecks) ~= "number" then
387-
return nil, "expect a number at #1"
387+
return nil, "bn:is_prime: expect a number at #1"
388388
end
389389
-- if nchecks is not defined, set to BN_prime_checks:
390390
-- select number of iterations based on the size of the number

lib/resty/openssl/cipher.lua

+13-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ local cipher_ctx_ptr_ct = ffi.typeof('EVP_CIPHER_CTX*')
1616

1717
function _M.new(typ)
1818
if not typ then
19-
return nil, "expect type to be defined"
19+
return nil, "cipher.new: expect type to be defined"
2020
end
2121

2222
local ctx
@@ -29,12 +29,12 @@ function _M.new(typ)
2929
ffi_gc(ctx, C.EVP_CIPHER_CTX_cleanup)
3030
end
3131
if ctx == nil then
32-
return nil, "failed to create EVP_CIPHER_CTX"
32+
return nil, "cipher.new: failed to create EVP_CIPHER_CTX"
3333
end
3434

3535
local dtyp = C.EVP_get_cipherbyname(typ)
3636
if dtyp == nil then
37-
return nil, string.format("invalid cipher type \"%s\"", typ)
37+
return nil, string.format("cipher.new: invalid cipher type \"%s\"", typ)
3838
end
3939

4040
local code = C.EVP_CipherInit_ex(ctx, dtyp, nil, "", nil, -1)
@@ -58,10 +58,10 @@ end
5858
function _M:init(key, iv, opts)
5959
opts = opts or {}
6060
if not key or #key ~= self.key_size then
61-
return false, string.format("incorrect key size, expect %d", self.key_size)
61+
return false, string.format("cipher:init: incorrect key size, expect %d", self.key_size)
6262
end
6363
if not iv or #iv ~= self.iv_size then
64-
return false, string.format("incorrect iv size, expect %d", self.iv_size)
64+
return false, string.format("cipher:init: incorrect iv size, expect %d", self.iv_size)
6565
end
6666

6767
if C.EVP_CipherInit_ex(self.ctx, nil, nil, key, iv, opts.is_encrypt and 1 or 0) == 0 then
@@ -103,7 +103,7 @@ end
103103
local int_ptr = ffi.typeof("int[1]")
104104
function _M:update(...)
105105
if not self.initialized then
106-
return nil, "cipher not initalized, call cipher:init first"
106+
return nil, "cipher:update: cipher not initalized, call cipher:init first"
107107
end
108108

109109
local ret = {}
@@ -146,31 +146,31 @@ end
146146

147147
function _M:derive(key, salt, count, md)
148148
if type(key) ~= "string" then
149-
return nil, nil, "expect a string at #1"
149+
return nil, nil, "cipher:derive: expect a string at #1"
150150
elseif salt and type(salt) ~= "string" then
151-
return nil, nil, "expect a string at #2"
151+
return nil, nil, "cipher:derive: expect a string at #2"
152152
elseif count then
153153
count = tonumber(count)
154154
if not count then
155-
return nil, nil, "expect a number at #3"
155+
return nil, nil, "cipher:derive: expect a number at #3"
156156
end
157157
elseif md and type(md) ~= "string" then
158-
return nil, nil, "expect a string or nil at #4"
158+
return nil, nil, "cipher:derive: expect a string or nil at #4"
159159
end
160160

161161
if salt then
162162
if #salt > 8 then
163-
ngx.log(ngx.WARN, "salt is too long, truncate salt to 8 bytes")
163+
ngx.log(ngx.WARN, "cipher:derive: salt is too long, truncate salt to 8 bytes")
164164
salt = salt:sub(0, 8)
165165
elseif #salt < 8 then
166-
ngx.log(ngx.WARN, "salt is too short, padding with zero bytes to length")
166+
ngx.log(ngx.WARN, "cipher:derive: salt is too short, padding with zero bytes to length")
167167
salt = salt .. string.rep('\000', 8 - #salt)
168168
end
169169
end
170170

171171
local mdt = C.EVP_get_digestbyname(md or 'sha1')
172172
if mdt == nil then
173-
return nil, nil, string.format("invalid digest type \"%s\"", md)
173+
return nil, nil, string.format("cipher:derive: invalid digest type \"%s\"", md)
174174
end
175175
local cipt = C.EVP_CIPHER_CTX_cipher(self.ctx)
176176
local keyb = ffi_new('unsigned char[?]', self.key_size)

lib/resty/openssl/digest.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ function _M.new(typ)
2424
ffi_gc(ctx, C.EVP_MD_CTX_destroy)
2525
end
2626
if ctx == nil then
27-
return nil, "failed to create EVP_MD_CTX"
27+
return nil, "digest.new: failed to create EVP_MD_CTX"
2828
end
2929

3030
local dtyp = C.EVP_get_digestbyname(typ or 'sha1')
3131
if dtyp == nil then
32-
return nil, string.format("invalid digest type \"%s\"", typ)
32+
return nil, string.format("digest.new: invalid digest type \"%s\"", typ)
3333
end
3434

3535
local code = C.EVP_DigestInit_ex(ctx, dtyp, nil)

lib/resty/openssl/ec.lua

+8-19
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function _M.get_parameters(ec_key_st)
2323
if k == 'group' then
2424
local nid = C.EC_GROUP_get_curve_name(group)
2525
if nid == 0 then
26-
return nil, "EC_GROUP_get_curve_name() failed"
26+
return nil, "ec.get_parameters: EC_GROUP_get_curve_name() failed"
2727
end
2828
return nid
2929
elseif k == 'public' or k == "pub_key" then
@@ -68,25 +68,14 @@ function _M.get_parameters(ec_key_st)
6868
return nil, format_error("ec.get_parameters: EC_POINT_get_affine_coordinates")
6969
end
7070
else
71-
return nil, "unknown parameter \"" .. k .. "\" for EC key"
71+
return nil, "ec.get_parameters: unknown parameter \"" .. k .. "\" for EC key"
7272
end
7373

7474
return bn_lib.dup(bn)
7575
end
7676
}), nil
7777
end
7878

79-
function _M.ec_group_from_nid(nid)
80-
local group = C.EC_GROUP_new_by_curve_name(nid)
81-
if group == nil then
82-
return nil, "EC_GROUP_new_by_curve_name() failed"
83-
end
84-
-- # define OPENSSL_EC_NAMED_CURVE 0x001
85-
C.EC_GROUP_set_asn1_flag(group, 1)
86-
C.EC_GROUP_set_point_conversion_form(group, C.POINT_CONVERSION_UNCOMPRESSED)
87-
return group
88-
end
89-
9079
function _M.set_parameters(ec_key_st, opts)
9180
for _, k in ipairs(_M.params) do
9281
if k ~= "group" then
@@ -101,12 +90,12 @@ function _M.set_parameters(ec_key_st, opts)
10190
if group_nid then
10291
local nid, err = objects_lib.txtnid2nid(group_nid)
10392
if err then
104-
return nil, "cannot use parameter \"group\":" .. err
93+
return nil, "ec.set_parameters: cannot use parameter \"group\":" .. err
10594
end
10695

10796
group = C.EC_GROUP_new_by_curve_name(nid)
10897
if group == nil then
109-
return nil, "EC_GROUP_new_by_curve_name() failed"
98+
return nil, "ec.set_parameters: EC_GROUP_new_by_curve_name() failed"
11099
end
111100
ffi_gc(group, C.EC_GROUP_free)
112101
-- # define OPENSSL_EC_NAMED_CURVE 0x001
@@ -122,18 +111,18 @@ function _M.set_parameters(ec_key_st, opts)
122111
local y = opts["y"]
123112
local pub = opts["public"]
124113
if (x and not y) or (y and not x) then
125-
return nil, "\"x\" and \"y\" parameter must be defined at same time or both undefined"
114+
return nil, "ec.set_parameters: \"x\" and \"y\" parameter must be defined at same time or both undefined"
126115
end
127116

128117
if x and y then
129118
if pub then
130-
return nil, "cannot set \"x\" and \"y\" with \"public\" at same time to set public key"
119+
return nil, "ec.set_parameters: cannot set \"x\" and \"y\" with \"public\" at same time to set public key"
131120
end
132121
-- double check if we have set group already
133122
if group == nil then
134123
group = C.EC_KEY_get0_group(ec_key_st)
135124
if group == nil then
136-
return nil, "cannot set public key without setting \"group\""
125+
return nil, "ec.set_parameters: cannot set public key without setting \"group\""
137126
end
138127
end
139128

@@ -146,7 +135,7 @@ function _M.set_parameters(ec_key_st, opts)
146135
if group == nil then
147136
group = C.EC_KEY_get0_group(ec_key_st)
148137
if group == nil then
149-
return nil, "cannot set public key without setting \"group\""
138+
return nil, "ec.set_parameters: cannot set public key without setting \"group\""
150139
end
151140
end
152141

lib/resty/openssl/err.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ local function format_error(ctx, code)
4040
end
4141

4242
if #errors > 0 then
43-
return string.format("%s: %s", ctx, table.concat(errors, " "))
43+
return string.format("%s%s%s", (ctx or ""), (ctx and ": " or ""), table.concat(errors, " "))
4444
else
4545
return string.format("%s failed", ctx)
4646
end

lib/resty/openssl/hmac.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ function _M.new(key, typ)
2626
ffi_gc(ctx, C.HMAC_CTX_cleanup)
2727
end
2828
if ctx == nil then
29-
return nil, "failed to create HMAC_CTX"
29+
return nil, "hmac.new: failed to create HMAC_CTX"
3030
end
3131

3232
local dtyp = C.EVP_get_digestbyname(typ or 'sha1')
3333
if dtyp == nil then
34-
return nil, string.format("invalid digest type \"%s\"", typ)
34+
return nil, string.format("hmac.new: invalid digest type \"%s\"", typ)
3535
end
3636

3737
local code = C.HMAC_Init_ex(ctx, key, #key, dtyp, nil)

lib/resty/openssl/kdf.lua

+7-7
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,20 @@ local void_ptr = ffi.typeof("void *")
114114
function _M.derive(options)
115115
local typ = options.type
116116
if not typ then
117-
return nil, "\"type\" must be set"
117+
return nil, "kdf.derive: \"type\" must be set"
118118
elseif type(typ) ~= "number" then
119-
return nil, "expect a number as \"type\""
119+
return nil, "kdf.derive: expect a number as \"type\""
120120
end
121121

122122
if typ <= 0 then
123-
return nil, "kdf type " .. (type_literals[typ] or tostring(typ)) ..
123+
return nil, "kdf.derive: kdf type " .. (type_literals[typ] or tostring(typ)) ..
124124
" not supported in " .. version_text
125125
end
126126

127127
for k, v in pairs(options_schema) do
128128
local v, err = check_options(options, typ, k, unpack(v))
129129
if err then
130-
return nil, err
130+
return nil, "kdf.derive: " .. err
131131
end
132132
options[k] = v
133133
end
@@ -144,7 +144,7 @@ function _M.derive(options)
144144
local md
145145
md = C.EVP_get_digestbyname(options.md or "sha1")
146146
if md == nil then
147-
return nil, string.format("invalid digest type \"%s\"", md)
147+
return nil, string.format("kdf.derive: invalid digest type \"%s\"", md)
148148
end
149149

150150
local buf = ffi_new('unsigned char[?]', options.outlen)
@@ -171,7 +171,7 @@ function _M.derive(options)
171171
buf, options.outlen
172172
)
173173
elseif typ ~= NID_tls1_prf and typ ~= NID_hkdf then
174-
return nil, ("unknown type %d"):format(typ)
174+
return nil, string.format("kdf.derive: unknown type %d", typ)
175175
end
176176
if code then
177177
if code ~= 1 then
@@ -243,7 +243,7 @@ function _M.derive(options)
243243
end
244244
end
245245
else
246-
return nil, ("unknown type %d"):format(typ)
246+
return nil, string.format("kdf.derive: unknown type %d", typ)
247247
end
248248
code = C.EVP_PKEY_derive(ctx, buf, outlen)
249249
if code == -2 then

0 commit comments

Comments
 (0)