Skip to content

Commit a4ee237

Browse files
committed
fix(x509.store) return all error on load_file or add failure
1 parent ac51fb1 commit a4ee237

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lib/resty/openssl/err.lua

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local ctypes = require "resty.openssl.aux.ctypes"
88
ffi.cdef [[
99
unsigned long ERR_peek_error(void);
1010
unsigned long ERR_peek_last_error_line(const char **file, int *line);
11+
unsigned long ERR_get_error_line(const char **file, int *line);
1112
void ERR_clear_error(void);
1213
void ERR_error_string_n(unsigned long e, char *buf, size_t len);
1314
]]
@@ -16,16 +17,21 @@ local constchar_ptrptr = ffi.typeof("const char*[1]")
1617

1718
local buf = ffi.new('char[256]')
1819

19-
local function format_error(ctx, code)
20+
local function format_error(ctx, code, all_errors)
2021
local errors = {}
2122
if code then
2223
table.insert(errors, string.format("code: %d", code or 0))
2324
end
2425
-- get the OpenSSL errors
25-
if C.ERR_peek_error() ~= 0 then
26+
while C.ERR_peek_error() ~= 0 do
2627
local line = ctypes.ptr_of_int()
2728
local path = constchar_ptrptr()
28-
local code = C.ERR_peek_last_error_line(path, line)
29+
local code
30+
if all_errors then
31+
code = C.ERR_get_error_line(path, line)
32+
else
33+
code = C.ERR_peek_last_error_line(path, line)
34+
end
2935

3036
local abs_path = ffi_str(path[0])
3137
-- ../crypto/asn1/a_d2i_fp.c => crypto/asn1/a_d2i_fp.c
@@ -34,20 +40,30 @@ local function format_error(ctx, code)
3440
abs_path = abs_path:sub(start+1)
3541
end
3642

37-
C.ERR_clear_error()
3843
C.ERR_error_string_n(code, buf, ffi_sizeof(buf))
3944
table.insert(errors, string.format("%s:%d:%s",
4045
abs_path, line[0], ffi_str(buf))
4146
)
47+
48+
if not all_errors then
49+
break
50+
end
4251
end
4352

53+
C.ERR_clear_error()
54+
4455
if #errors > 0 then
4556
return string.format("%s%s%s", (ctx or ""), (ctx and ": " or ""), table.concat(errors, " "))
4657
else
4758
return string.format("%s failed", ctx)
4859
end
4960
end
5061

62+
local function format_all_error(ctx, code)
63+
return format_error(ctx, code, true)
64+
end
65+
5166
return {
5267
format_error = format_error,
68+
format_all_error = format_all_error,
5369
}

lib/resty/openssl/x509/store.lua

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ local x509_vfy_macro = require "resty.openssl.include.x509_vfy"
77
local x509_lib = require "resty.openssl.x509"
88
local chain_lib = require "resty.openssl.x509.chain"
99
local crl_lib = require "resty.openssl.x509.crl"
10-
local format_error = require("resty.openssl.err").format_error
10+
local format_error = require("resty.openssl.err").format_all_error
11+
local format_all_error = require("resty.openssl.err").format_error
1112

1213
local _M = {}
1314
local mt = { __index = _M }
@@ -36,7 +37,7 @@ end
3637

3738
function _M:use_default()
3839
if C.X509_STORE_set_default_paths(self.ctx) ~= 1 then
39-
return false, format_error("x509.store:use_default")
40+
return false, format_all_error("x509.store:use_default")
4041
end
4142
return true
4243
end
@@ -51,7 +52,7 @@ function _M:add(item)
5152
end
5253
-- ref counter of dup is increased by 1
5354
if C.X509_STORE_add_cert(self.ctx, dup) ~= 1 then
54-
err = format_error("x509.store:add: X509_STORE_add_cert")
55+
err = format_all_error("x509.store:add: X509_STORE_add_cert")
5556
end
5657
-- decrease the dup ctx ref count immediately to make leak test happy
5758
C.X509_free(dup)
@@ -62,7 +63,7 @@ function _M:add(item)
6263
end
6364
-- ref counter of dup is increased by 1
6465
if C.X509_STORE_add_crl(self.ctx, dup) ~= 1 then
65-
err = format_error("x509.store:add: X509_STORE_add_crl")
66+
err = format_all_error("x509.store:add: X509_STORE_add_crl")
6667
end
6768

6869
-- define X509_V_FLAG_CRL_CHECK 0x4
@@ -92,7 +93,7 @@ function _M:load_file(path)
9293
return false, "x509.store:load_file: expect a string at #1"
9394
else
9495
if C.X509_STORE_load_locations(self.ctx, path, nil) ~= 1 then
95-
return false, format_error("x509.store:load_file")
96+
return false, format_all_error("x509.store:load_file")
9697
end
9798
end
9899

@@ -104,7 +105,7 @@ function _M:load_directory(path)
104105
return false, "x509.store:load_directory expect a string at #1"
105106
else
106107
if C.X509_STORE_load_locations(self.ctx, nil, path) ~= 1 then
107-
return false, format_error("x509.store:load_directory")
108+
return false, format_all_error("x509.store:load_directory")
108109
end
109110
end
110111

0 commit comments

Comments
 (0)