Files
Rob Hague b199467971 Fix key material extension during key exchange (#1689)
When the key exchange produces less key material than is needed for the cipher or hmac
algorithms, there is an iterative procedure to produce more.

For example, a SHA-1 key exchange algorithm produces 20 bytes of key material.
A SHA-256 hmac uses a 32 byte key, so one iteration of the procedure produces another
20 bytes of key material for a total of 40 which is sufficient for the hmac key.

The library works correctly in such cases of one iteration, but the logic is wrong if
more than one iteration is needed. In #1660, the connection uses a SHA-1 kex algorithm
with a SHA-512 hmac (64 byte key), requiring 3 iterations of the extension procedure and
resulting in an error upon connection.

This change fixes the logic to use the output of the previous iteration per the spec.

closes #1660
2025-08-17 23:49:40 +02:00

93 lines
2.8 KiB
C#

using Renci.SshNet.IntegrationTests.Common;
using Renci.SshNet.TestTools.OpenSSH;
namespace Renci.SshNet.IntegrationTests
{
[TestClass]
public class HmacTests : IntegrationTestBase
{
private IConnectionInfoFactory _connectionInfoFactory;
private RemoteSshdConfig _remoteSshdConfig;
[TestInitialize]
public void SetUp()
{
_connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
_remoteSshdConfig = new RemoteSshd(new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort)).OpenConfig();
}
[TestCleanup]
public void TearDown()
{
_remoteSshdConfig?.Reset();
}
[TestMethod]
public void HmacSha1()
{
DoTest(MessageAuthenticationCodeAlgorithm.HmacSha1);
}
[TestMethod]
public void HmacSha2_256()
{
DoTest(MessageAuthenticationCodeAlgorithm.HmacSha2_256);
}
[TestMethod]
public void HmacSha2_512()
{
DoTest(MessageAuthenticationCodeAlgorithm.HmacSha2_512);
}
[TestMethod]
public void HmacSha2_512_ShortKexOutput()
{
_remoteSshdConfig.ClearMessageAuthenticationCodeAlgorithms()
.AddMessageAuthenticationCodeAlgorithm(MessageAuthenticationCodeAlgorithm.HmacSha2_512)
.ClearKeyExchangeAlgorithms()
.AddKeyExchangeAlgorithm(KeyExchangeAlgorithm.DiffieHellmanGroupExchangeSha1)
.Update()
.Restart();
using (var client = new SshClient(_connectionInfoFactory.Create()))
{
client.Connect();
client.Disconnect();
}
}
[TestMethod]
public void HmacSha1_Etm()
{
DoTest(MessageAuthenticationCodeAlgorithm.HmacSha1Etm);
}
[TestMethod]
public void HmacSha2_256_Etm()
{
DoTest(MessageAuthenticationCodeAlgorithm.HmacSha2_256_Etm);
}
[TestMethod]
public void HmacSha2_512_Etm()
{
DoTest(MessageAuthenticationCodeAlgorithm.HmacSha2_512_Etm);
}
private void DoTest(MessageAuthenticationCodeAlgorithm macAlgorithm)
{
_remoteSshdConfig.ClearMessageAuthenticationCodeAlgorithms()
.AddMessageAuthenticationCodeAlgorithm(macAlgorithm)
.Update()
.Restart();
using (var client = new SshClient(_connectionInfoFactory.Create()))
{
client.Connect();
client.Disconnect();
}
}
}
}