|
1 | 1 | #nullable enable |
2 | 2 | using System; |
3 | 3 | using System.Diagnostics; |
| 4 | +using System.Globalization; |
4 | 5 | using System.IO; |
5 | 6 |
|
6 | 7 | using Renci.SshNet.Abstractions; |
@@ -40,7 +41,10 @@ protected override void WriteBytes(SshDataStream stream) |
40 | 41 | base.WriteBytes(stream); |
41 | 42 | } |
42 | 43 |
|
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> |
44 | 48 | internal int GetPacket(ref byte[] buffer, byte paddingMultiplier, Compressor? compressor, bool excludePacketLengthFieldWhenPadding, int macLength) |
45 | 49 | { |
46 | 50 | const int outboundPacketSequenceSize = 4; |
@@ -72,15 +76,20 @@ internal int GetPacket(ref byte[] buffer, byte paddingMultiplier, Compressor? co |
72 | 76 | // in Encrypt-then-MAC mode or AEAD, the length field is not encrypted, so we should keep it out of the |
73 | 77 | // padding length calculation |
74 | 78 | var paddingLength = GetPaddingLength( |
75 | | - paddingMultiplier, excludePacketLengthFieldWhenPadding ? 1 + messageLength : 4 + 1 + messageLength); |
| 79 | + paddingMultiplier, (excludePacketLengthFieldWhenPadding ? 0 : 4) + 1 + messageLength); |
76 | 80 |
|
77 | 81 | var packetLength = 1 + messageLength + paddingLength; |
78 | 82 |
|
79 | 83 | var bytesRequired = 4 + 4 + packetLength + macLength; // Includes space for sequence_num |
80 | 84 |
|
| 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 | + |
81 | 90 | if (buffer.Length < bytesRequired) |
82 | 91 | { |
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)); |
84 | 93 | } |
85 | 94 |
|
86 | 95 | using (var sshDataStream = new SshDataStream(buffer)) |
|
0 commit comments