From 353115789c723234e8d6acd15b36560c9def2997 Mon Sep 17 00:00:00 2001 From: Ingela Anderton Andin Date: Thu, 31 Oct 2024 16:52:27 +0100 Subject: [PATCH] ssl: Remove unnecessary boilerplate code --- lib/ssl/src/dtls_socket.erl | 18 +++++++------ lib/ssl/src/ssl.erl | 25 +++++++++-------- lib/ssl/src/ssl_gen_statem.erl | 39 --------------------------- lib/ssl/src/tls_socket.erl | 46 +++++++++++++++++--------------- lib/ssl/test/dtls_api_SUITE.erl | 17 ++++++------ lib/ssl/test/ssl_trace_SUITE.erl | 5 +--- 6 files changed, 58 insertions(+), 92 deletions(-) diff --git a/lib/ssl/src/dtls_socket.erl b/lib/ssl/src/dtls_socket.erl index 2f8f7ed5d238..3d0ff93c4f8b 100644 --- a/lib/ssl/src/dtls_socket.erl +++ b/lib/ssl/src/dtls_socket.erl @@ -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. diff --git a/lib/ssl/src/ssl.erl b/lib/ssl/src/ssl.erl index 6d00dd39364d..07910c22de9c 100644 --- a/lib/ssl/src/ssl.erl +++ b/lib/ssl/src/ssl.erl @@ -2339,6 +2339,7 @@ handshake(#sslsocket{} = Socket, Timeout) handshake(ListenSocket, SslOptions) -> handshake(ListenSocket, SslOptions, infinity). + -doc """ Performs the TLS/DTLS server-side handshake. @@ -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. %%-------------------------------------------------------------------- diff --git a/lib/ssl/src/ssl_gen_statem.erl b/lib/ssl/src/ssl_gen_statem.erl index 17c5e37bb7bc..d53f9a33518c 100644 --- a/lib/ssl/src/ssl_gen_statem.erl +++ b/lib/ssl/src/ssl_gen_statem.erl @@ -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, @@ -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()}. diff --git a/lib/ssl/src/tls_socket.erl b/lib/ssl/src/tls_socket.erl index ddc911633e11..b7bce813be9d 100644 --- a/lib/ssl/src/tls_socket.erl +++ b/lib/ssl/src/tls_socket.erl @@ -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}}}; @@ -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), diff --git a/lib/ssl/test/dtls_api_SUITE.erl b/lib/ssl/test/dtls_api_SUITE.erl index 5f05eb5f56ca..e1709ad643c5 100644 --- a/lib/ssl/test/dtls_api_SUITE.erl +++ b/lib/ssl/test/dtls_api_SUITE.erl @@ -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, @@ -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, diff --git a/lib/ssl/test/ssl_trace_SUITE.erl b/lib/ssl/test/ssl_trace_SUITE.erl index d4cd4abf1aeb..456031300f3d 100644 --- a/lib/ssl/test/ssl_trace_SUITE.erl +++ b/lib/ssl/test/ssl_trace_SUITE.erl @@ -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}, @@ -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 =>