Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/ssl/src/dtls_client_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@

-include("dtls_connection.hrl").
-include("dtls_handshake.hrl").
-include("dtls_record.hrl").
-include("ssl_alert.hrl").
-include("ssl_cipher.hrl").
-include("ssl_internal.hrl").
Expand Down Expand Up @@ -327,6 +328,10 @@ hello(internal, #change_cipher_spec{type = <<1>>}, State0) ->
end;
hello(state_timeout, Event, State) ->
dtls_gen_connection:handle_state_timeout(Event, ?STATE(hello), State);
hello(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(hello), State);
hello(info, Event, State) ->
dtls_gen_connection:gen_info(Event, ?STATE(hello), State);
hello(Type, Event, State) ->
Expand Down Expand Up @@ -360,6 +365,10 @@ abbreviated(internal = Type, #change_cipher_spec{} = Event,
ConnectionStates = dtls_record:next_epoch(ConnectionStates1, read),
gen_state(?STATE(abbreviated), Type, Event,
State#state{connection_states = ConnectionStates});
abbreviated(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(abbreviated), State);
abbreviated(Type, Event, State) ->
gen_state(?STATE(abbreviated), Type, Event, State).

Expand All @@ -372,6 +381,10 @@ wait_stapling(enter, _Event, State0) ->
{keep_state, State, Actions};
wait_stapling(state_timeout, Event, State) ->
dtls_gen_connection:handle_state_timeout(Event, ?STATE(wait_stapling), State);
wait_stapling(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(wait_stapling), State);
wait_stapling(info, Event, State) ->
dtls_gen_connection:gen_info(Event, ?STATE(wait_stapling), State);
wait_stapling(Type, Event, State) ->
Expand Down Expand Up @@ -403,6 +416,10 @@ certify(internal, #change_cipher_spec{type = <<1>>}, State0) ->
end;
certify(state_timeout, Event, State) ->
dtls_gen_connection:handle_state_timeout(Event, ?STATE(certify), State);
certify(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(certify), State);
certify(info, Event, State) ->
dtls_gen_connection:gen_info(Event, ?STATE(certify), State);
certify(Type, Event, State) ->
Expand Down Expand Up @@ -440,6 +457,10 @@ cipher(internal = Type, #finished{} = Event, #state{connection_states = Connecti
catch throw:#alert{} = Alert ->
ssl_gen_statem:handle_own_alert(Alert, cipher, State)
end;
cipher(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(cipher), State);
cipher(Type, Event, State) ->
gen_state(?STATE(cipher), Type, Event, State).

Expand Down
26 changes: 25 additions & 1 deletion lib/ssl/src/dtls_server_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@

-include("dtls_connection.hrl").
-include("dtls_handshake.hrl").
-include("dtls_record.hrl").
-include("ssl_alert.hrl").
-include("ssl_cipher.hrl").
-include("ssl_internal.hrl").
Expand Down Expand Up @@ -203,6 +204,10 @@ initial_hello({call, From}, {start, {Opts, EmOpts}, Timeout},
catch throw:Error ->
{stop_and_reply, {shutdown, normal}, {reply, From, {error, Error}}, State0}
end;
initial_hello(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(initial_hello), State);
initial_hello(Type, Event, State) ->
tls_dtls_server_connection:initial_hello(Type, Event, State).

Expand Down Expand Up @@ -299,7 +304,10 @@ hello(internal, #change_cipher_spec{type = <<1>>}, State0) ->
{stop, _, _} = Stop ->
Stop
end;

hello(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(hello), State);
hello(info, Event, State) ->
dtls_gen_connection:gen_info(Event, ?STATE(hello), State);
hello(state_timeout, Event, State) ->
Expand Down Expand Up @@ -335,6 +343,10 @@ abbreviated(internal = Type, #change_cipher_spec{} = Event,
ConnectionStates = dtls_record:next_epoch(ConnectionStates1, read),
gen_state(?STATE(abbreviated), Type, Event,
State#state{connection_states = ConnectionStates});
abbreviated(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(abbreviated), State);
abbreviated(Type, Event, State) ->
gen_state(?STATE(abbreviated), Type, Event, State).

Expand All @@ -360,6 +372,10 @@ certify(internal, #change_cipher_spec{type = <<1>>}, State0) ->
end;
certify(state_timeout, Event, State) ->
dtls_gen_connection:handle_state_timeout(Event, ?STATE(certify), State);
certify(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(certify), State);
certify(info, Event, State) ->
dtls_gen_connection:gen_info(Event, ?STATE(certify), State);
certify(Type, Event, State) ->
Expand All @@ -376,6 +392,10 @@ wait_cert_verify(state_timeout, Event, State) ->
dtls_gen_connection:handle_state_timeout(Event, ?STATE(wait_cert_verify), State);
wait_cert_verify(info, Event, State) ->
dtls_gen_connection:gen_info(Event, ?STATE(wait_cert_verify), State);
wait_cert_verify(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(wait_cert_verify), State);
wait_cert_verify(Type, Event, State) ->
gen_state(?STATE(wait_cert_verify), Type, Event, State).

Expand All @@ -402,6 +422,10 @@ cipher(internal = Type, #finished{} = Event, #state{connection_states = Connecti
State#state{connection_states = ConnectionStates,
protocol_specific =
PS#{flight_state => connection}}));
cipher(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(cipher), State);
cipher(Type, Event, State) ->
gen_state(?STATE(cipher), Type, Event, State).

Expand Down
20 changes: 20 additions & 0 deletions lib/ssl/src/tls_client_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ hello(internal, #server_hello{} = Hello,
catch throw:#alert{} = Alert ->
ssl_gen_statem:handle_own_alert(Alert, ?STATE(hello), State)
end;
hello(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(hello), State);
hello(info, Event, State) ->
tls_gen_connection:gen_info(Event, ?STATE(hello), State);
hello(Type, Event, State) ->
Expand All @@ -343,6 +347,10 @@ user_hello(Type, Event, State) ->
-spec abbreviated(gen_statem:event_type(), term(), #state{}) ->
gen_statem:state_function_result().
%%--------------------------------------------------------------------
abbreviated(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(abbreviated), State);
abbreviated(info, Event, State) ->
tls_gen_connection:gen_info(Event, ?STATE(abbreviated), State);
abbreviated(Type, Event, State) ->
Expand All @@ -352,6 +360,10 @@ abbreviated(Type, Event, State) ->
-spec wait_stapling(gen_statem:event_type(), term(), #state{}) ->
gen_statem:state_function_result().
%%--------------------------------------------------------------------
wait_stapling(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(wait_stapling), State);
wait_stapling(info, Event, State) ->
tls_gen_connection:gen_info(Event, ?STATE(wait_stapling), State);
wait_stapling(Type, Event, State) ->
Expand All @@ -361,6 +373,10 @@ wait_stapling(Type, Event, State) ->
-spec certify(gen_statem:event_type(), term(), #state{}) ->
gen_statem:state_function_result().
%%--------------------------------------------------------------------
certify(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(certify), State);
certify(info, Event, State) ->
tls_gen_connection:gen_info(Event, ?STATE(certify), State);
certify(Type, Event, State) ->
Expand All @@ -370,6 +386,10 @@ certify(Type, Event, State) ->
-spec cipher(gen_statem:event_type(), term(), #state{}) ->
gen_statem:state_function_result().
%%--------------------------------------------------------------------
cipher(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(cipher), State);
cipher(info, Event, State) ->
tls_gen_connection:gen_info(Event, ?STATE(cipher), State);
cipher(Type, Event, State) ->
Expand Down
6 changes: 6 additions & 0 deletions lib/ssl/src/tls_client_connection_1_3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ start(internal, #server_hello{}, State0) ->
%%so it is a previous version hello.
ssl_gen_statem:handle_own_alert(
?ALERT_REC(?FATAL, ?PROTOCOL_VERSION), ?STATE(start), State0);
start(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}}, State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(start), State);
start(info, Msg, State) ->
tls_gen_connection:gen_info(Msg, ?STATE(start), State);
start(Type, Msg, State) ->
Expand Down Expand Up @@ -299,6 +302,9 @@ wait_sh(internal, #server_hello{} = Hello,
tls_gen_connection:next_event(hello_middlebox_assert,
no_record, State1)
end;
wait_sh(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}}, State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(wait_sh), State);
wait_sh(info, Msg, State) ->
tls_gen_connection:gen_info(Msg, ?STATE(wait_sh), State);
wait_sh(Type, Msg, State) ->
Expand Down
32 changes: 1 addition & 31 deletions lib/ssl/src/tls_gen_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -400,37 +400,7 @@ next_event(StateName, #ssl_tls{} = Record, State, Actions) ->
next_event(StateName, #alert{} = Alert, State, Actions) ->
{next_state, StateName, State, [{next_event, internal, Alert} | Actions]}.

%%% TLS record protocol level application data messages
handle_protocol_record(#ssl_tls{type = ?APPLICATION_DATA}, StateName,
#state{static_env = #static_env{role = server},
handshake_env = #handshake_env{renegotiation = {false, first}}
} = State) when StateName == initial_hello;
StateName == hello;
StateName == certify;
StateName == wait_cert_verify;
StateName == wait_stapling;
StateName == abbreviated;
StateName == cipher
->
%% Application data can not be sent before initial handshake pre TLS-1.3.
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, StateName, State);
handle_protocol_record(#ssl_tls{type = ?APPLICATION_DATA, early_data = false}, StateName,
#state{static_env = #static_env{role = server}
} = State) when StateName == start;
StateName == recvd_ch;
StateName == negotiated;
StateName == wait_eoed ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, none_early_application_data_before_handshake),
ssl_gen_statem:handle_own_alert(Alert, StateName, State);
handle_protocol_record(#ssl_tls{type = ?APPLICATION_DATA}, StateName,
#state{static_env = #static_env{role = server}
} = State) when StateName == wait_cert;
StateName == wait_cv;
StateName == wait_finished->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE,
application_data_before_handshake_or_intervened_in_post_handshake_auth),
ssl_gen_statem:handle_own_alert(Alert, StateName, State);
%%% TLS record protocol level application data messages
handle_protocol_record(#ssl_tls{type = ?APPLICATION_DATA, fragment = Data}, StateName,
#state{recv = #recv{from = From},
socket_options = #socket_options{active = false}} = State0)
Expand Down
24 changes: 24 additions & 0 deletions lib/ssl/src/tls_server_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ initial_hello({call, From}, {start, {Opts, EmOpts}, Timeout},
catch throw:Error ->
{stop_and_reply, {shutdown, normal}, {reply, From, {error, Error}}, State0}
end;
initial_hello(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(initial_hello), State);
initial_hello(Type, Event, State) ->
tls_dtls_server_connection:initial_hello(Type, Event, State).

Expand Down Expand Up @@ -236,6 +240,10 @@ hello(internal, #client_hello{client_version = ClientVersion} = Hello,
State0#state{connection_env = NewCenv},
ssl_gen_statem:handle_own_alert(Alert, ?STATE(hello), AlertState)
end;
hello(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(hello), State);
hello(info, Event, State) ->
tls_gen_connection:gen_info(Event, ?STATE(hello), State);
hello(Type, Event, State) ->
Expand All @@ -254,6 +262,10 @@ user_hello(Type, Event, State) ->
%%--------------------------------------------------------------------
abbreviated(info, Event, State) ->
tls_gen_connection:gen_info(Event, ?STATE(abbreviated), State);
abbreviated(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(abbreviated), State);
abbreviated(Type, Event, State) ->
gen_state(?STATE(abbreviated), Type, Event, State).

Expand All @@ -263,6 +275,10 @@ abbreviated(Type, Event, State) ->
%%--------------------------------------------------------------------
certify(info, Event, State) ->
tls_gen_connection:gen_info(Event, ?STATE(certify), State);
certify(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(certify), State);
certify(Type, Event, State) ->
gen_state(?STATE(certify), Type, Event, State).

Expand Down Expand Up @@ -297,6 +313,10 @@ wait_cert_verify(internal, #certificate_verify{signature = Signature,
#alert{} = Alert ->
throw(Alert)
end;
wait_cert_verify(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(wait_cert_verify), State);
wait_cert_verify(Type, Event, State) ->
ssl_gen_statem:handle_common_event(Type, Event, ?STATE(wait_cert_verify), State).

Expand All @@ -306,6 +326,10 @@ wait_cert_verify(Type, Event, State) ->
%%--------------------------------------------------------------------
cipher(info, Event, State) ->
tls_gen_connection:gen_info(Event, ?STATE(cipher), State);
cipher(internal, {protocol_record, #ssl_tls{type = ?APPLICATION_DATA}},
#state{handshake_env = #handshake_env{renegotiation = {false, first}}} = State) ->
Alert = ?ALERT_REC(?FATAL, ?UNEXPECTED_MESSAGE, application_data_before_initial_handshake),
ssl_gen_statem:handle_own_alert(Alert, ?STATE(cipher), State);
cipher(Type, Event, State) ->
gen_state(?STATE(cipher), Type, Event, State).

Expand Down
Loading
Loading