Skip to content

Commit 4654519

Browse files
committed
KEX with Elliptic Curves ecdh-sha2-nistp{256,384,521}
Add Kex Algos ecdh-sha2-nistp{256,384,521} We have to use a minimalistic BouncyCastle Import for ECDH, since Microsoft's System.Security.Cryptography is not usable in this case. ECDiffieHellmanCng.DeriveKeyMaterial() already does the hashing and it's not possible to get the unhased key material for further processing. https://blogs.msdn.microsoft.com/shawnfa/2007/01/22/elliptic-curve-diffie-hellman/
1 parent 2fe968b commit 4654519

15 files changed

+575
-22
lines changed

src/Renci.SshNet.Tests.NET35/Renci.SshNet.Tests.NET35.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@
351351
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\ExtensionsTest_Take_OffsetAndCount.cs">
352352
<Link>Classes\Common\ExtensionsTest_Take_OffsetAndCount.cs</Link>
353353
</Compile>
354+
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\ExtensionsTest_ToBigInteger2.cs">
355+
<Link>Classes\Common\ExtensionsTest_ToBigInteger2.cs</Link>
356+
</Compile>
354357
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\ExtensionsTest_TrimLeadingZeros.cs">
355358
<Link>Classes\Common\ExtensionsTest_TrimLeadingZeros.cs</Link>
356359
</Compile>
@@ -1750,4 +1753,4 @@
17501753
<Target Name="AfterBuild">
17511754
</Target>
17521755
-->
1753-
</Project>
1756+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Renci.SshNet.Common;
5+
6+
namespace Renci.SshNet.Tests.Classes.Common
7+
{
8+
[TestClass]
9+
[SuppressMessage("ReSharper", "InvokeAsExtensionMethod")]
10+
public class ExtensionsTest_ToBigInteger2
11+
{
12+
[TestMethod]
13+
public void ShouldNotAppendZero()
14+
{
15+
byte[] value = { 0x0a, 0x0d };
16+
17+
var actual = value.ToBigInteger2().ToByteArray().Reverse();
18+
19+
Assert.IsNotNull(actual);
20+
Assert.AreEqual(2, actual.Length);
21+
Assert.AreEqual(0x0a, actual[0]);
22+
Assert.AreEqual(0x0d, actual[1]);
23+
}
24+
25+
[TestMethod]
26+
public void ShouldAppendZero()
27+
{
28+
byte[] value = { 0xff, 0x0a, 0x0d };
29+
30+
var actual = value.ToBigInteger2().ToByteArray().Reverse();
31+
32+
Assert.IsNotNull(actual);
33+
Assert.AreEqual(4, actual.Length);
34+
Assert.AreEqual(0x00, actual[0]);
35+
Assert.AreEqual(0xff, actual[1]);
36+
Assert.AreEqual(0x0a, actual[2]);
37+
Assert.AreEqual(0x0d, actual[3]);
38+
}
39+
}
40+
}

src/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
<Compile Include="Classes\Common\ExtensionsTest_Reverse.cs" />
170170
<Compile Include="Classes\Common\ExtensionsTest_Take_Count.cs" />
171171
<Compile Include="Classes\Common\ExtensionsTest_Take_OffsetAndCount.cs" />
172+
<Compile Include="Classes\Common\ExtensionsTest_ToBigInteger2.cs" />
172173
<Compile Include="Classes\Common\ExtensionsTest_TrimLeadingZeros.cs" />
173174
<Compile Include="Classes\Common\PackTest.cs" />
174175
<Compile Include="Classes\Common\PosixPathTest_GetFileName.cs" />
@@ -717,4 +718,4 @@
717718
<Target Name="AfterBuild">
718719
</Target>
719720
-->
720-
</Project>
721+
</Project>

src/Renci.SshNet/Common/Extensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ internal static BigInteger ToBigInteger(this byte[] data)
7272
return new BigInteger(reversed.Reverse());
7373
}
7474

75+
/// <summary>
76+
/// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format
77+
/// </summary>
78+
public static BigInteger ToBigInteger2(this byte[] data)
79+
{
80+
if ((data[0] & (1 << 7)) != 0)
81+
{
82+
var buf = new byte[data.Length + 1];
83+
Buffer.BlockCopy(data, 0, buf, 1, data.Length);
84+
data = buf;
85+
}
86+
return data.ToBigInteger();
87+
}
88+
7589
/// <summary>
7690
/// Reverses the sequence of the elements in the entire one-dimensional <see cref="Array"/>.
7791
/// </summary>

src/Renci.SshNet/ConnectionInfo.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,13 @@ public ConnectionInfo(string host, int port, string username, ProxyTypes proxyTy
322322

323323
KeyExchangeAlgorithms = new Dictionary<string, Type>
324324
{
325+
{"ecdh-sha2-nistp256", typeof(KeyExchangeECDH256)},
326+
{"ecdh-sha2-nistp384", typeof(KeyExchangeECDH384)},
327+
{"ecdh-sha2-nistp521", typeof(KeyExchangeECDH521)},
325328
{"diffie-hellman-group-exchange-sha256", typeof (KeyExchangeDiffieHellmanGroupExchangeSha256)},
326329
{"diffie-hellman-group-exchange-sha1", typeof (KeyExchangeDiffieHellmanGroupExchangeSha1)},
327330
{"diffie-hellman-group14-sha1", typeof (KeyExchangeDiffieHellmanGroup14Sha1)},
328331
{"diffie-hellman-group1-sha1", typeof (KeyExchangeDiffieHellmanGroup1Sha1)},
329-
//{"ecdh-sha2-nistp256", typeof(KeyExchangeEllipticCurveDiffieHellman)},
330-
//{"ecdh-sha2-nistp256", typeof(...)},
331-
//{"ecdh-sha2-nistp384", typeof(...)},
332-
//{"ecdh-sha2-nistp521", typeof(...)},
333332
//"gss-group1-sha1-toWM5Slw5Ew8Mqkay+al2g==" - WinSSHD
334333
//"gss-gex-sha1-toWM5Slw5Ew8Mqkay+al2g==" - WinSSHD
335334
};

src/Renci.SshNet/Messages/Transport/KeyExchangeEcdhInitMessage.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
#if false
2-
3-
using System;
1+
using System;
42
using Renci.SshNet.Common;
53

64
namespace Renci.SshNet.Messages.Transport
75
{
86
/// <summary>
97
/// Represents SSH_MSG_KEXECDH_INIT message.
108
/// </summary>
11-
[Message("SSH_MSG_KEXECDH_INIT", 30)]
9+
[Message("SSH_MSG_KEX_ECDH_INIT", 30)]
1210
internal class KeyExchangeEcdhInitMessage : Message, IKeyExchangedAllowed
1311
{
1412
/// <summary>
@@ -33,6 +31,14 @@ protected override int BufferCapacity
3331
}
3432
}
3533

34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="KeyExchangeEcdhInitMessage"/> class.
36+
/// </summary>
37+
public KeyExchangeEcdhInitMessage(byte[] q)
38+
{
39+
QC = q;
40+
}
41+
3642
/// <summary>
3743
/// Initializes a new instance of the <see cref="KeyExchangeEcdhInitMessage"/> class.
3844
/// </summary>
@@ -63,7 +69,10 @@ protected override void SaveData()
6369
{
6470
WriteBinaryString(QC);
6571
}
66-
}
67-
}
6872

69-
#endif // false
73+
internal override void Process(Session session)
74+
{
75+
throw new NotImplementedException();
76+
}
77+
}
78+
}

src/Renci.SshNet/Messages/Transport/KeyExchangeEcdhReplyMessage.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#if false
2-
3-
namespace Renci.SshNet.Messages.Transport
1+
namespace Renci.SshNet.Messages.Transport
42
{
53
/// <summary>
64
/// Represents SSH_MSG_KEXECDH_REPLY message.
75
/// </summary>
8-
[Message("SSH_MSG_KEXECDH_REPLY", 31)]
6+
[Message("SSH_MSG_KEX_ECDH_REPLY", 31)]
97
public class KeyExchangeEcdhReplyMessage : Message
108
{
119
/// <summary>
@@ -65,7 +63,10 @@ protected override void SaveData()
6563
WriteBinaryString(QS);
6664
WriteBinaryString(Signature);
6765
}
68-
}
69-
}
7066

71-
#endif // false
67+
internal override void Process(Session session)
68+
{
69+
session.OnKeyExchangeEcdhReplyMessageReceived(this);
70+
}
71+
}
72+
}

src/Renci.SshNet/Renci.SshNet.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@
309309
<Compile Include="Security\GroupExchangeHashData.cs" />
310310
<Compile Include="Security\IKeyExchange.cs" />
311311
<Compile Include="Security\KeyExchangeDiffieHellmanGroupExchangeShaBase.cs" />
312+
<Compile Include="Security\KeyExchangeEC.cs" />
313+
<Compile Include="Security\KeyExchangeECDH.cs" />
314+
<Compile Include="Security\KeyExchangeECDH521.cs" />
315+
<Compile Include="Security\KeyExchangeECDH384.cs" />
316+
<Compile Include="Security\KeyExchangeECDH256.cs" />
312317
<Compile Include="ServiceFactory.cs" />
313318
<Compile Include="ServiceFactory.NET.cs" />
314319
<Compile Include="Sftp\ISftpFileReader.cs" />

0 commit comments

Comments
 (0)