Skip to content

Commit

Permalink
ssl: Remove unnecessary boilerplate code
Browse files Browse the repository at this point in the history
  • Loading branch information
IngelaAndin committed Nov 6, 2024
1 parent 44ffe88 commit 3531157
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 92 deletions.
18 changes: 10 additions & 8 deletions lib/ssl/src/dtls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,18 @@ accept(dtls, #config{transport_info = {Transport,_,_,_,_},
end.

connect(Address, Port, #config{transport_info = {Transport, _, _, _, _} = CbInfo,
connection_cb = ConnectionCb,
ssl = SslOpts,
emulated = EmOpts,
inet_ssl = SocketOpts}, Timeout) ->
ssl = SslOpts,
emulated = EmOpts,
inet_ssl = SocketOpts}, Timeout) ->
case Transport:open(0, SocketOpts ++ internal_inet_values()) of
{ok, Socket} ->
ssl_gen_statem:connect(ConnectionCb, Address, Port, {{Address, Port},Socket},
{SslOpts,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout);
try dtls_gen_connection:start_fsm(client, Address, Port, {{Address, Port}, Socket},
{SslOpts, emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout)
catch
exit:{noproc, _} ->
{error, ssl_not_started}
end;
{error, _} = Error->
Error
end.
Expand Down
25 changes: 14 additions & 11 deletions lib/ssl/src/ssl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,7 @@ handshake(#sslsocket{} = Socket, Timeout)

handshake(ListenSocket, SslOptions) ->
handshake(ListenSocket, SslOptions, infinity).

-doc """
Performs the TLS/DTLS server-side handshake.
Expand Down Expand Up @@ -2395,37 +2396,39 @@ handshake(#sslsocket{fd = {_, _, _, Trackers}} = Socket, SslOpts, Timeout)
Tracker = proplists:get_value(option_tracker, Trackers),
{ok, EmOpts, _} = tls_socket:get_all_opts(Tracker),
ssl_gen_statem:handshake(Socket, {SslOpts,
tls_socket:emulated_socket_options(EmOpts, #socket_options{})}, Timeout)
tls_socket:emulated_socket_options(EmOpts, #socket_options{})}, Timeout)
catch
Error = {error, _Reason} -> Error
Error = {error, _Reason} -> Error
end;
handshake(#sslsocket{pid = [Pid|_], fd = {_, _, _}} = Socket, SslOpts, Timeout)
when is_list(SslOpts), ?IS_TIMEOUT(Timeout) ->
try
{ok, EmOpts, _} = dtls_packet_demux:get_all_opts(Pid),
ssl_gen_statem:handshake(Socket, {SslOpts,
tls_socket:emulated_socket_options(EmOpts, #socket_options{})}, Timeout)
dtls_socket:emulated_socket_options(EmOpts, #socket_options{})}, Timeout)
catch
Error = {error, _Reason} -> Error
Error = {error, _Reason} -> Error
end;
handshake(Socket, SslOptions, Timeout)
when is_list(SslOptions), ?IS_TIMEOUT(Timeout) ->
try
CbInfo = handle_option_cb_info(SslOptions, tls),
Transport = element(1, CbInfo),
ConnetionCb = connection_cb(SslOptions),
{ok, #config{transport_info = CbInfo, ssl = SslOpts, emulated = EmOpts}} =
handle_options(Transport, Socket, SslOptions, server, undefined),
ok = tls_socket:setopts(Transport, Socket, tls_socket:internal_inet_values()),
{ok, Port} = tls_socket:port(Transport, Socket),
{ok, SessionIdHandle} = tls_socket:session_id_tracker(ssl_unknown_listener, SslOpts),
ssl_gen_statem:handshake(ConnetionCb, Port, Socket,
{SslOpts,
tls_socket:emulated_socket_options(EmOpts, #socket_options{}),
[{session_id_tracker, SessionIdHandle}]},
self(), CbInfo, Timeout)
tls_gen_connection:start_fsm(server, "localhost", Port, Socket,
{SslOpts,
tls_socket:emulated_socket_options(EmOpts, #socket_options{}),
[{session_id_tracker, SessionIdHandle}]},
self(), CbInfo, Timeout)
catch
Error = {error, _Reason} -> Error
exit:{noproc, _} ->
{error, ssl_not_started};
Error = {error, _Reason} ->
Error
end.

%%--------------------------------------------------------------------
Expand Down
39 changes: 0 additions & 39 deletions lib/ssl/src/ssl_gen_statem.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
-export([opposite_role/1,
init_ssl_config/3,
ssl_config/3,
connect/8,
handshake/7,
handshake/2,
handshake/3,
handshake_continue/3,
Expand Down Expand Up @@ -268,43 +266,6 @@ ssl_config(Opts, Role, #state{static_env = InitStatEnv0,
connection_env = CEnv#connection_env{cert_key_alts = CertKeyAlts},
ssl_options = Opts}.

%%--------------------------------------------------------------------
-spec connect(tls_gen_connection | dtls_gen_connection,
ssl:host(), inet:port_number(),
port() | {tuple(), port()}, %% TLS | DTLS
{ssl_options(), #socket_options{},
%% Tracker only needed on server side
undefined},
pid(), tuple(), timeout()) ->
{ok, #sslsocket{}} | {error, ssl:reason()}.
%%
%% Description: Connect to an ssl server.
%%--------------------------------------------------------------------
connect(Connection, Host, Port, Socket, Options, User, CbInfo, Timeout) ->
try Connection:start_fsm(client, Host, Port, Socket, Options, User, CbInfo,
Timeout)
catch
exit:{noproc, _} ->
{error, ssl_not_started}
end.
%%--------------------------------------------------------------------
-spec handshake(tls_gen_connection | dtls_gen_connection,
inet:port_number(), port(),
{ssl_options(), #socket_options{}, list()},
pid(), tuple(), timeout()) ->
{ok, #sslsocket{}} | {error, ssl:reason()}.
%%
%% Description: Performs accept on an ssl listen socket. e.i. performs
%% ssl handshake.
%%--------------------------------------------------------------------
handshake(Connection, Port, Socket, Opts, User, CbInfo, Timeout) ->
try Connection:start_fsm(server, "localhost", Port, Socket, Opts, User,
CbInfo, Timeout)
catch
exit:{noproc, _} ->
{error, ssl_not_started}
end.

%%--------------------------------------------------------------------
-spec handshake(#sslsocket{}, timeout()) -> {ok, #sslsocket{}} |
{ok, #sslsocket{}, map()}| {error, ssl:reason()}.
Expand Down
46 changes: 25 additions & 21 deletions lib/ssl/src/tls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,30 @@ accept(ListenSocket, #config{transport_info = {Transport,_,_,_,_} = CbInfo,
end.

upgrade(Socket, #config{transport_info = {Transport,_,_,_,_}= CbInfo,
ssl = SslOptions,
emulated = EmOpts, connection_cb = ConnectionCb}, Timeout) ->
ssl = SslOpts,
emulated = EmOpts}, Timeout) ->
ok = setopts(Transport, Socket, tls_socket:internal_inet_values()),
case peername(Transport, Socket) of
{ok, {Address, Port}} ->
ssl_gen_statem:connect(ConnectionCb, Address, Port, Socket,
{SslOptions,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout);
{ok, {Host, Port}} ->
start_client_fsm(Host, Port, Socket,
{SslOpts, emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout);
{error, Error} ->
{error, Error}
end.

connect(Address, Port,
connect(Host, Port,
#config{transport_info = CbInfo, inet_user = UserOpts, ssl = SslOpts,
emulated = EmOpts, inet_ssl = SocketOpts, connection_cb = ConnetionCb},
emulated = EmOpts, inet_ssl = SocketOpts},
Timeout) ->
{Transport, _, _, _, _} = CbInfo,
try Transport:connect(Address, Port, SocketOpts, Timeout) of
try Transport:connect(Host, Port, SocketOpts, Timeout) of
{ok, Socket} ->
ssl_gen_statem:connect(ConnetionCb, Address, Port, Socket,
{SslOpts,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout);
{error, Reason} ->
{error, Reason}
start_client_fsm(Host, Port, Socket,
{SslOpts, emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout);
{error, Reason} ->
{error, Reason}
catch
exit:{function_clause, _} ->
{error, {options, {cb_info, CbInfo}}};
Expand All @@ -148,11 +146,17 @@ connect(Address, Port,
{error, {options, {socket_options, UserOpts}}}
end.

socket(Pids, Transport, Socket, ConnectionCb, Trackers) ->
#sslsocket{pid = Pids,
%% "The name "fd" is kept for backwards compatibility
fd = {Transport, Socket, ConnectionCb, Trackers}}.
setopts(gen_tcp, Socket = #sslsocket{pid = {ListenSocket, #config{trackers = Trackers}}}, Options) ->

start_client_fsm(Host, Port, Socket, Options, User, CbInfo, Timeout) ->
try tls_gen_connection:start_fsm(client, Host, Port, Socket, Options, User, CbInfo,
Timeout)
catch
exit:{noproc, _} ->
{error, ssl_not_started}
end.

setopts(gen_tcp, Socket = #sslsocket{socket_handle = ListenSocket,
listener_config = #config{trackers = Trackers}}, Options) ->
Tracker = proplists:get_value(option_tracker, Trackers),
{SockOpts, EmulatedOpts} = split_options(Options),
ok = set_emulated_opts(Tracker, EmulatedOpts),
Expand Down
17 changes: 8 additions & 9 deletions lib/ssl/test/dtls_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -430,21 +430,21 @@ client_restarts(Config) ->
Msgs = lists:sort(flush()),

ReConnect = %% Whitebox re-connect test
fun({sslsocket, {gen_udp,_,dtls_gen_connection}, [Pid]} = Socket, ssl) ->
fun(#sslsocket{connection_handler = Pid} = Socket, ssl) ->
?CT_LOG("Client Socket: ~p ~n", [Socket]),
{ok, IntSocket} = gen_statem:call(Pid, {downgrade, self()}),
{{Address,CPort},UDPSocket}=IntSocket,
?CT_LOG("Info: ~p~n", [inet:info(UDPSocket)]),

{ok, #config{transport_info = CbInfo, connection_cb = ConnectionCb,
{ok, #config{transport_info = CbInfo,
ssl = SslOpts0}} =
ssl:handle_options(ClientOpts, client, Address),
SslOpts = {SslOpts0, #socket_options{}, undefined},

ct:sleep(250),
?CT_LOG("Client second connect: ~p ~p~n", [Socket, CbInfo]),
{ok, NewSocket} = ssl_gen_statem:connect(ConnectionCb, Address, CPort, IntSocket,
SslOpts, self(), CbInfo, infinity),
{ok, NewSocket} = dtls_gen_connection:start_fsm(client, Address, CPort, IntSocket,
SslOpts, self(), CbInfo, infinity),
{replace, NewSocket}
end,

Expand Down Expand Up @@ -514,21 +514,20 @@ client_restarts_multiple_acceptors(Config) ->
Msgs = lists:sort(flush()),

ReConnect = %% Whitebox re-connect test
fun({sslsocket, {gen_udp,_,dtls_gen_connection}, [Pid]} = Socket, ssl) ->
fun(#sslsocket{connection_handler = Pid} = Socket, ssl) ->
?CT_LOG("Client Socket: ~p ~n", [Socket]),
{ok, IntSocket} = gen_statem:call(Pid, {downgrade, self()}),
{{Address,CPort},UDPSocket}=IntSocket,
?CT_LOG("Info: ~p~n", [inet:info(UDPSocket)]),

{ok, #config{transport_info = CbInfo, connection_cb = ConnectionCb,
{ok, #config{transport_info = CbInfo,
ssl = SslOpts0}} =
ssl:handle_options(ClientOpts, client, Address),
SslOpts = {SslOpts0, #socket_options{}, undefined},

ct:sleep(250),
?CT_LOG("Client second connect: ~p ~p~n", [Socket, CbInfo]),
{ok, NewSocket} = ssl_gen_statem:connect(ConnectionCb, Address, CPort, IntSocket,
SslOpts, self(), CbInfo, infinity),
{ok, NewSocket} = dtls_gen_connection:start_fsm(client, Address, CPort, IntSocket,
SslOpts, self(), CbInfo, infinity),
{replace, NewSocket}
end,

Expand Down
5 changes: 1 addition & 4 deletions lib/ssl/test/ssl_trace_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ tc_api_profile(Config) ->
tls_server_connection, initial_hello},
{" (client) <- tls_client_connection:initial_hello/3 returned",
tls_client_connection, initial_hello},
{" (client) <- ssl_gen_statem:connect/8 returned",
ssl_gen_statem, connect},
{" (client) <- ssl:connect/3 returned", ssl, connect},
{" (server) <- ssl:handshake/2 returned", ssl, handshake},
{" (client) <- tls_sender:init/3 returned", tls_sender, init},
Expand All @@ -161,8 +159,7 @@ tc_api_profile(Config) ->
"rle ('?') -> ssl:listen/2 (*server) Args",
"rle ('?') -> ssl:connect/3 (*client) Args",
"rle ('?') -> tls_sender:init/3 (*server)",
"rle ('?') -> tls_sender:init/3 (*client)",
"api (client) -> ssl_gen_statem:connect/8"]},
"rle ('?') -> tls_sender:init/3 (*client)"]},
TracesAfterDisconnect =
#{
call =>
Expand Down

0 comments on commit 3531157

Please sign in to comment.