Skip to content

Commit 896f36e

Browse files
committed
refactor: caller allocate error message buffer
1 parent 1099992 commit 896f36e

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

lib/ngx/ssl.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local error = error
1414
local tonumber = tonumber
1515
local errmsg = base.get_errmsg_ptr()
1616
local get_string_buf = base.get_string_buf
17+
local get_string_buf_size = base.get_string_buf_size
1718
local get_size_ptr = base.get_size_ptr
1819
local FFI_DECLINED = base.FFI_DECLINED
1920
local FFI_OK = base.FFI_OK
@@ -66,10 +67,10 @@ void *ngx_http_lua_ffi_ssl_ctx_init(unsigned int protocols, char **err);
6667
void ngx_http_lua_ffi_ssl_ctx_free(void *cdata);
6768

6869
int ngx_http_lua_ffi_ssl_ctx_set_priv_key(void *cdata_ctx,
69-
void *cdata_key, char **err);
70+
void *cdata_key, unsigned char **err_buf, size_t err_buf_len);
7071

7172
int ngx_http_lua_ffi_ssl_ctx_set_cert(void *cdata_ctx,
72-
void *cdata_cert, char **err);
73+
void *cdata_cert, unsigned char **err_buf, size_t err_buf_len);
7374

7475
]]
7576

@@ -79,6 +80,7 @@ local _M = { version = base.version }
7980

8081
local charpp = ffi.new("char*[1]")
8182
local intp = ffi.new("int[1]")
83+
local err_buf = ffi.new("unsigned char *[1]")
8284

8385

8486
function _M.clear_certs()
@@ -297,19 +299,24 @@ function _M.create_ctx(options)
297299

298300
ctx = ffi_gc(ctx, C.ngx_http_lua_ffi_ssl_ctx_free)
299301

302+
local size = get_string_buf_size()
303+
local buf = get_string_buf(size)
304+
err_buf[0] = buf
305+
300306
if options.cert ~= nil then
301-
local rc = C.ngx_http_lua_ffi_ssl_ctx_set_cert(ctx,
302-
options.cert, errmsg)
307+
local rc = C.ngx_http_lua_ffi_ssl_ctx_set_cert(ctx, options.cert,
308+
err_buf, size)
303309
if rc ~= FFI_OK then
304-
return nil, ffi_str(errmsg[0])
310+
return nil, ffi_str(err_buf[0])
305311
end
306312
end
307313

308314
if options.priv_key ~= nil then
309315
local rc = C.ngx_http_lua_ffi_ssl_ctx_set_priv_key(ctx,
310-
options.priv_key, errmsg)
316+
options.priv_key,
317+
err_buf, size)
311318
if rc ~= FFI_OK then
312-
return nil, ffi_str(errmsg[0])
319+
return nil, ffi_str(err_buf[0])
313320
end
314321
end
315322

t/ssl-ctx.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,15 @@ TLSv1.2
274274
cert = cert
275275
})
276276
if ssl_ctx == nil then
277-
ngx.say("create_ctx err: ", err)
277+
ngx.say(err)
278278
end
279279
}
280280
}
281281
282282
--- request
283283
GET /t
284284
--- response_body
285-
create_ctx err: SSL_CTX_use_PrivateKey() failed
285+
error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch
286286
287287
288288

0 commit comments

Comments
 (0)