Skip to content

Commit 66053a0

Browse files
committed
dtls: Handle unexpected epoch 0 message
1 parent 4e0ba96 commit 66053a0

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

lib/ssl/src/dtls_record.erl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ get_connection_state_by_epoch(Epoch, #{current_read := #{epoch := Epoch} = Curre
128128
Current;
129129
get_connection_state_by_epoch(Epoch, #{saved_read := #{epoch := Epoch} = Saved},
130130
read) ->
131-
Saved.
131+
Saved;
132+
%% This can be an attack on read side so return undefined so we can trigger alert
133+
%% on write side this would be a programming error, so let it crash.
134+
get_connection_state_by_epoch(_, _, read) ->
135+
undefined.
132136

133137
set_connection_state_by_epoch(WriteState, Epoch, #{current_write := #{epoch := Epoch}} = States,
134138
write) ->
@@ -253,10 +257,13 @@ encode_plain_text(Type, Version, Epoch, Data, ConnectionStates) ->
253257
%% Decoding
254258
%%====================================================================
255259

256-
decode_cipher_text(#ssl_tls{epoch = Epoch} = CipherText, ConnnectionStates0) ->
257-
ReadState = get_connection_state_by_epoch(Epoch, ConnnectionStates0, read),
258-
decode_cipher_text(CipherText, ReadState, ConnnectionStates0).
259-
260+
decode_cipher_text(#ssl_tls{epoch = Epoch} = CipherText, ConnectionStates0) ->
261+
case get_connection_state_by_epoch(Epoch, ConnectionStates0, read) of
262+
undefined ->
263+
?ALERT_REC(?FATAL, ?BAD_RECORD_MAC);
264+
ReadState ->
265+
decode_cipher_text(CipherText, ReadState, ConnectionStates0)
266+
end.
260267

261268
%%====================================================================
262269
%% Protocol version handling

0 commit comments

Comments
 (0)