Skip to content

Commit 7145585

Browse files
committed
KEX with Elliptic Curves
Add Kex Algos ecdh-sha2-nistp{256,384,521} and curve25519-sha256(@libssh.org) 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 bd01d97 commit 7145585

File tree

282 files changed

+39115
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

282 files changed

+39115
-21
lines changed

src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Diagnostics;
1+
using System;
2+
3+
using System.Diagnostics;
24
#if FEATURE_DIAGNOSTICS_TRACESOURCE
35
using System.Threading;
46
#endif // FEATURE_DIAGNOSTICS_TRACESOURCE
@@ -27,6 +29,7 @@ public static bool IsEnabled(TraceEventType traceEventType)
2729
[Conditional("DEBUG")]
2830
public static void Log(string text)
2931
{
32+
Console.WriteLine(text);
3033
#if FEATURE_DIAGNOSTICS_TRACESOURCE
3134
Loggging.TraceEvent(TraceEventType.Verbose, Thread.CurrentThread.ManagedThreadId, text);
3235
#endif // FEATURE_DIAGNOSTICS_TRACESOURCE

src/Renci.SshNet/Common/BigInteger.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ public static BigInteger Random(int bitLength)
177177
return new BigInteger(bytesArray);
178178
}
179179

180+
/// <summary>
181+
/// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format
182+
/// </summary>
183+
public static BigInteger SshFormatBignum2(byte[] data)
184+
{
185+
if ((data[0] & (1 << 7)) != 0)
186+
{
187+
var buf = new byte[data.Length + 1];
188+
Buffer.BlockCopy(data, 0, buf, 1, data.Length);
189+
data = buf;
190+
}
191+
return new BigInteger(data.Reverse());
192+
}
193+
180194
#endregion SSH.NET additions
181195

182196
private BigInteger(short sign, uint[] data)

src/Renci.SshNet/ConnectionInfo.cs

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

323323
KeyExchangeAlgorithms = new Dictionary<string, Type>
324324
{
325+
{"curve25519-sha256", typeof(KeyExchangeECCurve25519)},
326+
{"curve25519-sha256@libssh.org", typeof(KeyExchangeECCurve25519)},
327+
{"ecdh-sha2-nistp256", typeof(KeyExchangeECDH256)},
328+
{"ecdh-sha2-nistp384", typeof(KeyExchangeECDH384)},
329+
{"ecdh-sha2-nistp521", typeof(KeyExchangeECDH521)},
325330
{"diffie-hellman-group-exchange-sha256", typeof (KeyExchangeDiffieHellmanGroupExchangeSha256)},
326331
{"diffie-hellman-group-exchange-sha1", typeof (KeyExchangeDiffieHellmanGroupExchangeSha1)},
327332
{"diffie-hellman-group14-sha1", typeof (KeyExchangeDiffieHellmanGroup14Sha1)},
328333
{"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(...)},
333334
//"gss-group1-sha1-toWM5Slw5Ew8Mqkay+al2g==" - WinSSHD
334335
//"gss-gex-sha1-toWM5Slw5Ew8Mqkay+al2g==" - WinSSHD
335336
};

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+
}

0 commit comments

Comments
 (0)