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 8, 2024
1 parent 171fb25 commit a0449ea
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 97 deletions.
22 changes: 11 additions & 11 deletions lib/ssl/src/dtls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

-export([emulated_options/0,
emulated_options/1,
emulated_socket_options/2,
internal_inet_values/0,
default_inet_values/0,
default_cb_info/0]).
Expand Down Expand Up @@ -80,16 +81,19 @@ accept({Listener,_}, #config{transport_info = Info,
end.

connect(Address, Port, #config{transport_info = {Transport, _, _, _, _} = CbInfo,
connection_cb = ConnectionCb,
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 Expand Up @@ -221,7 +225,7 @@ port(Transport, Socket) ->
Transport:port(Socket).

emulated_options() ->
[mode, active, packet, packet_size].
[mode, active].

emulated_options(Opts) ->
emulated_options(Opts, internal_inet_values(), default_inet_values()).
Expand All @@ -230,20 +234,16 @@ internal_inet_values() ->
[{active, false}, {mode,binary}].

default_inet_values() ->
[{active, true}, {mode, list}, {packet, 0}, {packet_size, 0}].
[{active, true}, {mode, list}].

default_cb_info() ->
{gen_udp, udp, udp_closed, udp_error, udp_passive}.

emulated_socket_options(InetValues, #socket_options{
mode = Mode,
packet = Packet,
packet_size = PacketSize,
active = Active}) ->
#socket_options{
mode = proplists:get_value(mode, InetValues, Mode),
packet = proplists:get_value(packet, InetValues, Packet),
packet_size = proplists:get_value(packet_size, InetValues, PacketSize),
active = emulated_active_option(InetValues, Active)
}.

Expand Down
36 changes: 20 additions & 16 deletions lib/ssl/src/ssl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,7 @@ handshake(#sslsocket{connection_handler = Controller} = Socket, Timeout)

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

-doc """
Performs the TLS/DTLS server-side handshake.
Expand Down Expand Up @@ -2381,6 +2382,7 @@ If option `active` is set to `once`, `true`, or an integer value, the process
owning the [`sslsocket()`](`t:sslsocket/0`) will receive messages of type
[`active_msgs()`](`t:active_msgs/0`).
""".

-doc(#{title => <<"Server API">>,
since => <<"OTP 21.0">>}).
-spec handshake(Socket, Options, Timeout) ->
Expand All @@ -2404,37 +2406,42 @@ handshake(#sslsocket{connection_cb = tls_gen_connection,
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{socket_handle = {Controller,_}, connection_cb = dtls_gen_connection} = Socket, SslOpts, Timeout)
handshake(#sslsocket{socket_handle = {Controller,_}, connection_cb = dtls_gen_connection} = Socket,
SslOpts, Timeout)
when is_list(SslOpts), ?IS_TIMEOUT(Timeout) ->
try
{ok, EmOpts, _} = dtls_packet_demux:get_all_opts(Controller),
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_options(EmOpts),
[{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 Expand Up @@ -5163,10 +5170,7 @@ make_next_protocol_selector(What) ->
connection_cb(tls) ->
tls_gen_connection;
connection_cb(dtls) ->
dtls_gen_connection;
connection_cb(Opts) ->
connection_cb(proplists:get_value(protocol, Opts, tls)).

dtls_gen_connection.

%% Assert that basic options are on the format {Key, Value}
%% with a few exceptions and phase out log_alert
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
41 changes: 24 additions & 17 deletions lib/ssl/src/tls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
-export([send/3,
listen/3,
accept/3,
socket/5,
socket/5,
connect/4,
upgrade/3,
setopts/3,
Expand Down Expand Up @@ -114,32 +114,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 @@ -149,13 +147,22 @@ connect(Address, Port,
{error, {options, {socket_options, UserOpts}}}
end.

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.

socket([Receiver, Sender], Transport, Socket, ConnectionCb, Trackers) ->
#sslsocket{socket_handle = Socket,
connection_handler = Receiver,
payload_sender = Sender,
transport_cb = Transport,
connection_cb = ConnectionCb,
listener_config = Trackers}.

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

ReConnect = %% Whitebox re-connect test
fun(#sslsocket{connection_cb = dtls_gen_connection,
connection_handler = 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 @@ -516,20 +515,19 @@ client_restarts_multiple_acceptors(Config) ->
Msgs = lists:sort(flush()),

ReConnect = %% Whitebox re-connect test
fun(#sslsocket{connection_cb = dtls_gen_connection,
connection_handler = 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 a0449ea

Please sign in to comment.