Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha authored and dndx committed Nov 24, 2020
1 parent d23ebac commit 42710cb
Showing 1 changed file with 67 additions and 65 deletions.
132 changes: 67 additions & 65 deletions lib/resty/core/socket_tcp.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
local ffi = require("ffi")
local base = require("resty.core.base")
-- Copyright (C) by OpenResty Inc.


local base = require "resty.core.base"
local ffi = require "ffi"


local C = ffi.C
local ffi_string = ffi.string
local ffi_str = ffi.string
local ffi_gc = ffi.gc
local FFI_ERROR = base.FFI_ERROR
local FFI_DONE = base.FFI_DONE
local FFI_OK = base.FFI_OK
local FFI_AGAIN = base.FFI_AGAIN
local FFI_NO_REQ_CTX = base.FFI_NO_REQ_CTX
local get_request = base.get_request
local new_tab = base.new_tab
local clear_tab = base.clear_tab
local error = error
local assert = assert
local type = type
local pcall = pcall
local select = select
local co_yield = coroutine._yield
local table_new = require("table.new")
local table_clear = require("table.clear")


ffi.cdef[[
typedef struct ngx_http_lua_socket_tcp_upstream_s
Expand All @@ -26,11 +31,12 @@ typedef struct ngx_http_lua_socket_tcp_upstream_s
int ngx_http_lua_ffi_socket_tcp_tlshandshake(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, void *sess,
int enable_session_reuse, ngx_str_t *server_name, int verify,
int ocsp_status_req, void *chain, void *pkey,
char **errmsg);
int ocsp_status_req, void *chain, void *pkey, char **errmsg);

int ngx_http_lua_ffi_socket_tcp_get_tlshandshake_result(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, void **sess,
char **errmsg, int *openssl_error_code);
ngx_http_lua_socket_tcp_upstream_t *u, void **sess, char **errmsg,
int *openssl_error_code);

void ngx_http_lua_ffi_tls_free_session(void *sess);
]]

Expand All @@ -42,22 +48,21 @@ local errmsg = base.get_errmsg_ptr()
local session_ptr = ffi.new("void *[1]")
local server_name_str = ffi.new("ngx_str_t[1]")
local openssl_error_code = ffi.new("int[1]")
local cached_options = table_new(0, 4)
local cached_options = new_tab(0, 4)


local function tlshandshake(self, options)
if not options then
table_clear(cached_options)
clear_tab(cached_options)
options = cached_options

elseif type(options) ~= "table" then
error("bad options table type")
error("bad options arg: table expected", 2)
end

local r = get_request()

if not r then
error("no request found")
error("no request found", 2)
end

local reused_session = options.reused_session
Expand All @@ -73,77 +78,73 @@ local function tlshandshake(self, options)
end

local client_cert = options.client_cert
local client_priv_key = options.client_priv_key
local client_pkey = options.client_priv_key
if client_cert then
if not client_priv_key then
error("client certificate supplied without "
.. "corresponding private key", 2)
if not client_pkey then
error("client certificate supplied without corresponding " ..
"private key", 2)
end

if type(client_cert) ~= "cdata"
or type(client_priv_key) ~= "cdata"
then
error("wrong type of client certificate or private key supplied", 2)
if type(client_cert) ~= "cdata" then
error("bad client_cert option type", 2)
end

if type(client_pkey) ~= "cdata" then
error("bad client_priv_key option type", 2)
end
end

local rc =
C.ngx_http_lua_ffi_socket_tcp_tlshandshake(r, self[SOCKET_CTX_INDEX],
session_ptr[0],
reused_session ~= false,
server_name_str,
options.verify and 1 or 0,
options.ocsp_status_req
and 1 or 0,
client_cert,
client_priv_key,
errmsg)
local u = self[SOCKET_CTX_INDEX]

local rc = C.ngx_http_lua_ffi_socket_tcp_tlshandshake(r, u,
session_ptr[0],
reused_session ~= false,
server_name_str,
options.verify and 1 or 0,
options.ocsp_status_req and 1 or 0,
client_cert, client_pkey, errmsg)

if rc == FFI_NO_REQ_CTX then
error("no request ctx found", 2)
end

::again::
while true do
if rc == FFI_ERROR then
if openssl_error_code[0] ~= 0 then
return nil, openssl_error_code[0] .. ": " .. ffi_str(errmsg[0])
end

if rc == FFI_ERROR then
if openssl_error_code[0] ~= 0 then
return nil, openssl_error_code[0] .. ": " .. ffi_string(errmsg[0])
return nil, ffi_str(errmsg[0])
end

return nil, ffi_string(errmsg[0])
end

if rc == FFI_DONE then
return options.reused_session
end

if rc == FFI_OK then
if options.reused_session == false then
return true
if rc == FFI_DONE then
return reused_session
end

rc = C.ngx_http_lua_ffi_socket_tcp_get_tlshandshake_result(r,
self[SOCKET_CTX_INDEX], session_ptr, errmsg, openssl_error_code)

assert(rc == FFI_OK)
if rc == FFI_OK then
if reused_session == false then
return true
end

if session_ptr[0] == nil then
return session_ptr[0]
end
rc = C.ngx_http_lua_ffi_socket_tcp_get_tlshandshake_result(r, u,
session_ptr, errmsg, openssl_error_code)

return ffi_gc(session_ptr[0], C.ngx_http_lua_ffi_tls_free_session)
end
assert(rc == FFI_OK)

assert(rc == FFI_AGAIN)
if session_ptr[0] == nil then
return nil
end

co_yield()
return ffi_gc(session_ptr[0], C.ngx_http_lua_ffi_tls_free_session)
end

rc = C.ngx_http_lua_ffi_socket_tcp_get_tlshandshake_result(r,
self[SOCKET_CTX_INDEX], session_ptr, errmsg, openssl_error_code)
assert(rc == FFI_AGAIN)

assert(rc == FFI_OK or rc == FFI_ERROR)
co_yield()

goto again
rc = C.ngx_http_lua_ffi_socket_tcp_get_tlshandshake_result(r, u,
session_ptr, errmsg, openssl_error_code)
end
end


Expand All @@ -152,8 +153,8 @@ local function sslhandshake(self, reused_session, server_name, ssl_verify,

local n = select("#", ...)
if not self or n > 1 then
error("ngx.socket sslhandshake: expecting 1 ~ 5 "
.. "arguments (including the object), but seen " .. n)
error("ngx.socket sslhandshake: expecting 1 ~ 5 arguments " ..
"(including the object), but seen " .. (self and 5 + n or 0))
end

cached_options.reused_session = reused_session
Expand All @@ -162,7 +163,8 @@ local function sslhandshake(self, reused_session, server_name, ssl_verify,
cached_options.ocsp_status_req = send_status_req

local res, err = tlshandshake(self, cached_options)
table_clear(cached_options)

clear_tab(cached_options)

return res, err
end
Expand Down

0 comments on commit 42710cb

Please sign in to comment.