File 4681-Honor-max_fragment_lenght-in-dtls.patch of Package erlang
From 7ade7d0a7f610ba092d5ff2b612739a72f6ae107 Mon Sep 17 00:00:00 2001
From: Dan Gudmundsson <dgud@erlang.org>
Date: Wed, 2 Feb 2022 10:55:09 +0100
Subject: [PATCH 1/4] Honor max_fragment_lenght in dtls
'max_fragment_lenght' was not considered when packets where packed.
---
lib/ssl/src/dtls_gen_connection.erl | 16 ++++++++--------
lib/ssl/src/dtls_handshake.erl | 3 ++-
2 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/lib/ssl/src/dtls_gen_connection.erl b/lib/ssl/src/dtls_gen_connection.erl
index dce727c7da..117c4c194b 100644
--- a/lib/ssl/src/dtls_gen_connection.erl
+++ b/lib/ssl/src/dtls_gen_connection.erl
@@ -256,7 +256,7 @@ send_handshake_flight(#state{static_env = #static_env{socket = Socket,
MaxSize = min(MaxFragmentLength, ?PMTUEstimate),
{Encoded, ConnectionStates} =
encode_handshake_flight(lists:reverse(Flight), Version, MaxSize, Epoch, ConnectionStates0),
- send_packets(Transport, Socket, Encoded),
+ send_packets(Transport, Socket, MaxSize, Encoded),
ssl_logger:debug(LogLevel, outbound, 'record', Encoded),
{State0#state{connection_states = ConnectionStates}, []};
@@ -274,7 +274,7 @@ send_handshake_flight(#state{static_env = #static_env{socket = Socket,
{HsBefore, ConnectionStates1} =
encode_handshake_flight(lists:reverse(Flight0), Version, MaxSize, Epoch, ConnectionStates0),
{EncChangeCipher, ConnectionStates} = encode_change_cipher(ChangeCipher, Version, Epoch, ConnectionStates1),
- send_packets(Transport, Socket, HsBefore ++ EncChangeCipher),
+ send_packets(Transport, Socket, MaxSize, HsBefore ++ EncChangeCipher),
ssl_logger:debug(LogLevel, outbound, 'record', [HsBefore]),
ssl_logger:debug(LogLevel, outbound, 'record', [EncChangeCipher]),
{State0#state{connection_states = ConnectionStates}, []};
@@ -296,7 +296,7 @@ send_handshake_flight(#state{static_env = #static_env{socket = Socket,
encode_change_cipher(ChangeCipher, Version, Epoch-1, ConnectionStates1),
{HsAfter, ConnectionStates} =
encode_handshake_flight(lists:reverse(Flight1), Version, MaxSize, Epoch, ConnectionStates2),
- send_packets(Transport, Socket, HsBefore ++ EncChangeCipher ++ HsAfter),
+ send_packets(Transport, Socket, MaxSize, HsBefore ++ EncChangeCipher ++ HsAfter),
ssl_logger:debug(LogLevel, outbound, 'record', [HsBefore]),
ssl_logger:debug(LogLevel, outbound, 'record', [EncChangeCipher]),
ssl_logger:debug(LogLevel, outbound, 'record', [HsAfter]),
@@ -317,7 +317,7 @@ send_handshake_flight(#state{static_env = #static_env{socket = Socket,
encode_change_cipher(ChangeCipher, Version, Epoch-1, ConnectionStates0),
{HsAfter, ConnectionStates} =
encode_handshake_flight(lists:reverse(Flight1), Version, MaxSize, Epoch, ConnectionStates1),
- send_packets(Transport, Socket, EncChangeCipher ++ HsAfter),
+ send_packets(Transport, Socket, MaxSize, EncChangeCipher ++ HsAfter),
ssl_logger:debug(LogLevel, outbound, 'record', [EncChangeCipher]),
ssl_logger:debug(LogLevel, outbound, 'record', [HsAfter]),
{State0#state{connection_states = ConnectionStates}, []}.
@@ -482,12 +482,12 @@ send(Transport, {Listener, Socket}, Data) when is_pid(Listener) ->
send(Transport, Socket, Data) -> % Client socket
dtls_socket:send(Transport, Socket, Data).
-send_packets(_Transport, _Socket, []) ->
+send_packets(_Transport, _Socket, _Max, []) ->
ok;
-send_packets(Transport, Socket, Packets) ->
- {Packet, Rest} = pack_packets(Packets, 0, ?PMTUEstimate+80, []),
+send_packets(Transport, Socket, Max, Packets) ->
+ {Packet, Rest} = pack_packets(Packets, 0, Max, []),
case send(Transport, Socket, Packet) of
- ok -> send_packets(Transport, Socket, Rest);
+ ok -> send_packets(Transport, Socket, Max, Rest);
Err -> Err
end.
diff --git a/lib/ssl/src/dtls_handshake.erl b/lib/ssl/src/dtls_handshake.erl
index 46ee95db87..cd22c74f18 100644
--- a/lib/ssl/src/dtls_handshake.erl
+++ b/lib/ssl/src/dtls_handshake.erl
@@ -142,8 +142,9 @@ fragment_handshake(Bin, _) when is_binary(Bin)->
%% This is the change_cipher_spec not a "real handshake" but part of the flight
Bin;
fragment_handshake([MsgType, Len, Seq, _, Len, Bin], Size) ->
- Bins = bin_fragments(Bin, Size),
+ Bins = bin_fragments(Bin, Size-26), %% Remove packet headers
handshake_fragments(MsgType, Seq, Len, Bins, []).
+
encode_handshake(Handshake, Version, Seq) ->
{MsgType, Bin} = enc_handshake(Handshake, Version),
Len = byte_size(Bin),
--
2.34.1