From 23f2d5617d7f262876fe190f30cbd1fbd8c6841b Mon Sep 17 00:00:00 2001 From: Ingela Anderton Andin Date: Fri, 3 Jan 2025 10:09:33 +0100 Subject: [PATCH] ssl: Some code polishing --- lib/ssl/src/ssl.erl | 12 +++--------- lib/ssl/src/ssl_gen_statem.erl | 4 ++-- lib/ssl/src/tls_socket.erl | 27 ++++++++++++++++++--------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/ssl/src/ssl.erl b/lib/ssl/src/ssl.erl index f4a027f235c..dfc7b59b1bf 100644 --- a/lib/ssl/src/ssl.erl +++ b/lib/ssl/src/ssl.erl @@ -2174,7 +2174,7 @@ connect(TCPSocket, TLSOptions, Timeout) try tls_gen_connection = connection_cb(TLSOptions), {ok, Config} = ssl_config:handle_options(TCPSocket, TLSOptions, client, undefined), - tls_socket:upgrade(TCPSocket, Config, Timeout) + tls_socket:upgrade(client, TCPSocket, Config, Timeout) catch error:{badmatch, _} -> {error, {dtls_upgrade, notsup}}; @@ -2448,15 +2448,9 @@ handshake(Socket, SslOptions, Timeout) when is_list(SslOptions), ?IS_TIMEOUT(Timeout) -> try tls_gen_connection = connection_cb(SslOptions), - {ok, #config{transport_info = CbInfo, ssl = SslOpts, emulated = EmOpts}} = + {ok, Config} = ssl_config:handle_options(Socket, SslOptions, server, undefined), - Transport = element(1, CbInfo), - 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), - Trackers = [{session_id_tracker, SessionIdHandle}], - {ok, SSocket} = tls_socket:start_tls_server_connection(SslOpts, Port, Socket, EmOpts, Trackers, CbInfo), - ssl_gen_statem:handshake(SSocket, Timeout) + tls_socket:upgrade(server, Socket, Config, Timeout) catch error:{badmatch, _} -> {error, {dtls_upgrade, notsup}}; diff --git a/lib/ssl/src/ssl_gen_statem.erl b/lib/ssl/src/ssl_gen_statem.erl index 8efc6966146..e525ba6f9e7 100644 --- a/lib/ssl/src/ssl_gen_statem.erl +++ b/lib/ssl/src/ssl_gen_statem.erl @@ -108,7 +108,7 @@ %%% Initial Erlang process setup %%-------------------------------------------------------------------- %%-------------------------------------------------------------------- --spec tls_start_link(client| server, ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) -> +-spec tls_start_link(client | server, ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) -> {ok, pid()} | ignore | {error, ssl:reason()}. %% %% Description: Creates a process which calls Module:init/1 to @@ -121,7 +121,7 @@ tls_start_link(Role, Host, Port, Socket, {SslOpts, _, _} = Options, User, CbInfo {ok, Pid}. %%-------------------------------------------------------------------- --spec dtls_start_link(atom(), ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) -> +-spec dtls_start_link(client | server, ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) -> {ok, pid()} | ignore | {error, ssl:reason()}. %% %% Description: Creates a gen_statem process which calls Module:init/1 to diff --git a/lib/ssl/src/tls_socket.erl b/lib/ssl/src/tls_socket.erl index 320a86d2143..b957f396f31 100644 --- a/lib/ssl/src/tls_socket.erl +++ b/lib/ssl/src/tls_socket.erl @@ -31,7 +31,7 @@ accept/3, socket/6, connect/4, - upgrade/3, + upgrade/4, setopts/3, getopts/3, getstat/3, @@ -41,8 +41,7 @@ close/2]). -export([split_options/1, - get_socket_opts/3, - start_tls_server_connection/6]). + get_socket_opts/3]). -export([emulated_options/0, emulated_options/1, @@ -52,7 +51,6 @@ start_link/3, terminate/2, inherit_tracker/3, - session_id_tracker/2, emulated_socket_options/2, get_emulated_opts/1, set_emulated_opts/2, @@ -113,16 +111,27 @@ accept(ListenSocket, #config{transport_info = {Transport,_,_,_,_} = CbInfo, {error, Reason} end. -upgrade(Socket, #config{transport_info = {Transport,_,_,_,_}= CbInfo, - ssl = SslOptions, - emulated = EmOpts}, Timeout) -> - ok = setopts(Transport, Socket, tls_socket:internal_inet_values()), +upgrade(client, Socket, #config{transport_info = CbInfo, + ssl = SslOptions, + emulated = EmOpts}, Timeout) -> + Transport = element(1, CbInfo), + ok = setopts(Transport, Socket, internal_inet_values()), case peername(Transport, Socket) of {ok, {Host, Port}} -> start_tls_client_connection(Host, Port, Socket, SslOptions, EmOpts, CbInfo, Timeout); {error, Error} -> {error, Error} - end. + end; +upgrade(server, Socket, #config{transport_info = CbInfo, + ssl = SslOpts, + emulated = EmOpts}, Timeout) -> + Transport = element(1, CbInfo), + ok = setopts(Transport, Socket, internal_inet_values()), + {ok, Port} = port(Transport, Socket), + {ok, SessionIdHandle} = session_id_tracker(ssl_unknown_listener, SslOpts), + Trackers = [{session_id_tracker, SessionIdHandle}], + {ok, SSocket} = start_tls_server_connection(SslOpts, Port, Socket, EmOpts, Trackers, CbInfo), + ssl_gen_statem:handshake(SSocket, Timeout). connect(Host, Port, #config{transport_info = CbInfo, inet_user = UserOpts, ssl = SslOpts,