Skip to content

Commit e74ede1

Browse files
committed
bugfix: sslhandshake read error details on immediate FFI_ERROR
With the C side now returning FFI_ERROR instead of FFI_OK when u->error_ret is set after an immediate handshake, the early FFI_OK path is removed. Added get_sslhandshake_result() in the FFI_ERROR branch so errmsg and openssl_error_code are populated when the error comes from the first call rather than from an async resume.
1 parent e68e857 commit e74ede1

1 file changed

Lines changed: 44 additions & 28 deletions

File tree

lib/resty/core/socket.lua

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -424,52 +424,68 @@ local function sslhandshake(cosocket, reused_session, server_name, ssl_verify,
424424
error("no request ctx found", 2)
425425
end
426426

427-
if rc == FFI_OK then
428-
if reused_session == false then
429-
return true
427+
local res
428+
429+
if rc == FFI_ERROR then
430+
res = C.ngx_http_lua_ffi_socket_tcp_get_sslhandshake_result(r, u,
431+
session_ptr, errmsg, openssl_error_code)
432+
433+
assert(res == FFI_ERROR)
434+
435+
if openssl_error_code[0] ~= 0 then
436+
return nil, openssl_error_code[0] .. ": " .. ffi_str(errmsg[0])
430437
end
431438

432-
rc = C.ngx_http_lua_ffi_socket_tcp_get_sslhandshake_result(r, u,
433-
session_ptr, errmsg, openssl_error_code)
439+
return nil, ffi_str(errmsg[0])
434440
end
435441

436-
while true do
437-
if rc == FFI_ERROR then
438-
if openssl_error_code[0] ~= 0 then
439-
return nil, openssl_error_code[0] .. ": " .. ffi_str(errmsg[0])
440-
end
442+
if rc == FFI_DONE then
443+
return reused_session
444+
end
441445

442-
return nil, ffi_str(errmsg[0])
446+
if rc == FFI_OK then
447+
if reused_session == false then
448+
return true
443449
end
444450

445-
if rc == FFI_DONE then
446-
return reused_session
451+
res = C.ngx_http_lua_ffi_socket_tcp_get_sslhandshake_result(r, u,
452+
session_ptr, errmsg, openssl_error_code)
453+
454+
assert(res == FFI_OK)
455+
456+
if session_ptr[0] == nil then
457+
return session_ptr[0]
447458
end
448459

449-
if rc == FFI_OK then
450-
if reused_session == false then
451-
return true
452-
end
460+
return ffi_gc(session_ptr[0], C.ngx_http_lua_ffi_ssl_free_session)
461+
end
453462

454-
rc = C.ngx_http_lua_ffi_socket_tcp_get_sslhandshake_result(r, u,
455-
session_ptr, errmsg, openssl_error_code)
463+
assert(rc == FFI_AGAIN)
456464

457-
assert(rc == FFI_OK)
465+
co_yield()
458466

459-
if session_ptr[0] == nil then
460-
return session_ptr[0]
461-
end
467+
res = C.ngx_http_lua_ffi_socket_tcp_get_sslhandshake_result(r, u,
468+
session_ptr, errmsg, openssl_error_code)
462469

463-
return ffi_gc(session_ptr[0], C.ngx_http_lua_ffi_ssl_free_session)
470+
if res == FFI_ERROR then
471+
if openssl_error_code[0] ~= 0 then
472+
return nil, openssl_error_code[0] .. ": " .. ffi_str(errmsg[0])
464473
end
465474

466-
assert(rc == FFI_AGAIN)
475+
return nil, ffi_str(errmsg[0])
476+
end
467477

468-
co_yield()
478+
assert(res == FFI_OK)
469479

470-
rc = C.ngx_http_lua_ffi_socket_tcp_get_sslhandshake_result(r, u,
471-
session_ptr, errmsg, openssl_error_code)
480+
if reused_session == false then
481+
return true
482+
end
483+
484+
if session_ptr[0] == nil then
485+
return session_ptr[0]
472486
end
487+
488+
return ffi_gc(session_ptr[0], C.ngx_http_lua_ffi_ssl_free_session)
473489
end
474490

475491

0 commit comments

Comments
 (0)