Skip to content

Commit e840027

Browse files
committed
Get all errs in openssl err helper
Openssl maintains an error queue. A given function may add multiple errors to that queue, and only printing the last one is worse than printing the whole thing. In fact, as it was implemented before, the output was not consistent; e.g. the same error could produce different messages.
1 parent d307a14 commit e840027

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

lib/resty/evp.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,18 @@ int X509_digest(const X509 *data,const EVP_MD *type,
111111

112112

113113
local function _err(ret)
114+
-- The openssl error queue can have multiple items, print them all separated by ': '
115+
local errs = {}
114116
local code = _C.ERR_get_error()
115-
if code == 0 then
117+
while code ~= 0 do
118+
table.insert(errs, 1, ffi.string(_C.ERR_reason_error_string(code)))
119+
code = _C.ERR_get_error()
120+
end
121+
122+
if #errs == 0 then
116123
return ret, "Zero error code (null arguments?)"
117124
end
118-
return ret, ffi.string(_C.ERR_reason_error_string(code))
125+
return ret, table.concat(errs, ": ")
119126
end
120127

121128

@@ -134,6 +141,9 @@ function RSASigner.new(self, pem_private_key)
134141

135142
-- TODO might want to support password protected private keys...
136143
local rsa = _C.PEM_read_bio_RSAPrivateKey(bio, nil, nil, nil)
144+
if rsa == nil then
145+
return _err()
146+
end
137147
ffi.gc(rsa, _C.RSA_free)
138148

139149
local evp_pkey = _C.EVP_PKEY_new()

0 commit comments

Comments
 (0)