Skip to content

Commit

Permalink
ssl: Handle user initiated cancelation during handshake
Browse files Browse the repository at this point in the history
An initiated handshake should always be closed with an alert,
elminate controlled shutdown that does not send alert.
  • Loading branch information
IngelaAndin committed Jan 8, 2025
1 parent 0129381 commit 73d4a81
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions lib/ssl/src/ssl_gen_statem.erl
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ handle_common_event(timeout, hibernate, _, _) ->
handle_common_event({timeout, handshake}, close, _StateName,
#state{recv = #recv{from = StartFrom} = Recv} = State) ->
{stop_and_reply,
{shutdown, user_timeout},
{shutdown, cancel_hs},
{reply, StartFrom, {error, timeout}}, State#state{recv = Recv#recv{from = undefined}}};
handle_common_event({timeout, recv}, timeout, StateName,
#state{recv = #recv{from = RecvFrom} = Recv} = State) ->
Expand Down Expand Up @@ -887,7 +887,7 @@ handle_info({ErrorTag, Socket, econnaborted}, StateName,
Pids = Connection:pids(State),
alert_user(Pids, Transport, Trackers, Socket,
StartFrom, ?ALERT_REC(?FATAL, ?CLOSE_NOTIFY), Role, StateName, Connection),
{stop, {shutdown, normal}, State};
{stop, {shutdown, transport_closed}, State};

handle_info({ErrorTag, Socket, Reason}, StateName, #state{static_env = #static_env{
role = Role,
Expand All @@ -897,23 +897,27 @@ handle_info({ErrorTag, Socket, Reason}, StateName, #state{static_env = #static_e
?SSL_LOG(info, "Socket error", [{error_tag, ErrorTag}, {description, Reason}]),
Alert = ?ALERT_REC(?FATAL, ?CLOSE_NOTIFY, {transport_error, Reason}),
handle_normal_shutdown(Alert#alert{role = Role}, StateName, State),
{stop, {shutdown,normal}, State};

{stop, {shutdown, transport_closed}, State};
handle_info({'DOWN', MonitorRef, _, _, Reason}, _,
#state{connection_env = #connection_env{user_application = {MonitorRef, _Pid}},
ssl_options = #{erl_dist := true}}) ->
{stop, {shutdown, Reason}};
handle_info({'DOWN', MonitorRef, _, _, _}, _,
handle_info({'DOWN', MonitorRef, _, _, _}, connection,
#state{connection_env = #connection_env{user_application = {MonitorRef, _Pid}}}) ->
{stop, {shutdown, normal}};
handle_info({'DOWN', MonitorRef, _, _, _}, _,
#state{connection_env = #connection_env{user_application = {MonitorRef, _Pid}}}) ->
{stop, {shutdown, cancel_hs}};
handle_info({'EXIT', Pid, _Reason}, StateName,
#state{connection_env = #connection_env{user_application = {_MonitorRef, Pid}}} = State) ->
%% It seems the user application has linked to us
%% - ignore that and let the monitor handle this
{next_state, StateName, State};
%%% So that terminate will be run when supervisor issues shutdown
handle_info({'EXIT', _Sup, shutdown}, _StateName, State) ->
handle_info({'EXIT', _Sup, shutdown}, connection, State) ->
{stop, shutdown, State};
handle_info({'EXIT', _Sup, shutdown}, _, State) ->
{stop, {shutdown, cancel_hs}, State};
handle_info({'EXIT', Socket, normal}, _StateName, #state{static_env = #static_env{socket = Socket}} = State) ->
%% Handle as transport close"
{stop,{shutdown, transport_closed}, State};
Expand Down Expand Up @@ -1247,19 +1251,29 @@ terminate(Reason, connection, #state{static_env = #static_env{
socket = Socket},
connection_states = ConnectionStates
} = State) ->

handle_trusted_certs_db(State),
Alert = terminate_alert(Reason),
%% Send the termination ALERT if possible
catch Connection:send_alert_in_connection(Alert, State),
Connection:close({close, ?DEFAULT_TIMEOUT}, Socket, Transport, ConnectionStates);
terminate({shutdown, cancel_hs} = Reason , _StateName,
#state{static_env = #static_env{transport_cb = Transport,
protocol_cb = Connection,
socket = Socket}
} = State0) ->
handle_trusted_certs_db(State0),
CancelAlert = ?ALERT_REC(?WARNING, ?USER_CANCELED),
CloseAlert = ?ALERT_REC(?WARNING, ?CLOSE_NOTIFY),
State = Connection:send_alert(CancelAlert, State0),
Connection:send_alert(CloseAlert, State),
Connection:close(Reason, Socket, Transport, undefined);
terminate(Reason, _StateName, #state{static_env = #static_env{transport_cb = Transport,
protocol_cb = Connection,
socket = Socket}
} = State) ->
} = State) ->
handle_trusted_certs_db(State),
Connection:close(Reason, Socket, Transport, undefined).

%%====================================================================
%% Log handling
%%====================================================================
Expand Down

0 comments on commit 73d4a81

Please sign in to comment.