Skip to content

Commit c42d26e

Browse files
committed
x
1 parent 908a46e commit c42d26e

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

src/Renci.SshNet/Messages/Message.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#nullable enable
22
using System;
33
using System.Diagnostics;
4+
using System.Globalization;
45
using System.IO;
56

67
using Renci.SshNet.Abstractions;
@@ -40,7 +41,10 @@ protected override void WriteBytes(SshDataStream stream)
4041
base.WriteBytes(stream);
4142
}
4243

43-
/// <returns>The number of bytes written to <paramref name="buffer"/>.</returns>
44+
/// <returns>The number of bytes occupied by the packet in <paramref name="buffer"/>.</returns>
45+
/// <remarks>
46+
/// [4 bytes] || packet_len || padding_len || payload || padding || [macLength bytes].
47+
/// </remarks>
4448
internal int GetPacket(ref byte[] buffer, byte paddingMultiplier, Compressor? compressor, bool excludePacketLengthFieldWhenPadding, int macLength)
4549
{
4650
const int outboundPacketSequenceSize = 4;
@@ -72,15 +76,20 @@ internal int GetPacket(ref byte[] buffer, byte paddingMultiplier, Compressor? co
7276
// in Encrypt-then-MAC mode or AEAD, the length field is not encrypted, so we should keep it out of the
7377
// padding length calculation
7478
var paddingLength = GetPaddingLength(
75-
paddingMultiplier, excludePacketLengthFieldWhenPadding ? 1 + messageLength : 4 + 1 + messageLength);
79+
paddingMultiplier, (excludePacketLengthFieldWhenPadding ? 0 : 4) + 1 + messageLength);
7680

7781
var packetLength = 1 + messageLength + paddingLength;
7882

7983
var bytesRequired = 4 + 4 + packetLength + macLength; // Includes space for sequence_num
8084

85+
if ((uint)bytesRequired > (uint)Session.MaximumSshPacketSize)
86+
{
87+
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Packet is too big. Maximum packet size is {0} bytes.", Session.MaximumSshPacketSize));
88+
}
89+
8190
if (buffer.Length < bytesRequired)
8291
{
83-
Array.Resize(ref buffer, Math.Max(bytesRequired, 2 * buffer.Length)); // TODO packetLength guard?
92+
Array.Resize(ref buffer, Math.Max(bytesRequired, 2 * buffer.Length));
8493
}
8594

8695
using (var sshDataStream = new SshDataStream(buffer))

src/Renci.SshNet/Session.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,11 +1085,6 @@ internal void SendMessage(Message message)
10851085
_clientEtm || _clientAead,
10861086
macLength);
10871087

1088-
if (activeBufferLength > MaximumSshPacketSize)
1089-
{
1090-
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Packet is too big. Maximum packet size is {0} bytes.", MaximumSshPacketSize));
1091-
}
1092-
10931088
// write outbound packet sequence to start of packet data
10941089
BinaryPrimitives.WriteUInt32BigEndian(_sendBuffer, _outboundPacketSequence);
10951090

0 commit comments

Comments
 (0)