Skip to content

Commit 1a803b5

Browse files
committed
Partially revert changes from #643.
Fixes #653.
1 parent 2c1a4d9 commit 1a803b5

File tree

8 files changed

+57
-49
lines changed

8 files changed

+57
-49
lines changed

src/Renci.SshNet/Common/Extensions.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,6 @@ 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 byte[] ToBigNum2(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-
87-
return data;
88-
}
89-
9075
/// <summary>
9176
/// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format
9277
/// </summary>

src/Renci.SshNet/Security/KeyExchange.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,17 @@ protected void SendMessage(Message message)
345345
private byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, byte[] key, int size)
346346
{
347347
var result = new List<byte>(key);
348+
348349
while (size > result.Count)
349350
{
350-
result.AddRange(Hash(new _SessionKeyAdjustment
351-
{
352-
SharedKey = sharedKey,
353-
ExchangeHash = exchangeHash,
354-
Key = key,
355-
}.GetBytes()));
351+
var sessionKeyAdjustment = new SessionKeyAdjustment
352+
{
353+
SharedKey = sharedKey,
354+
ExchangeHash = exchangeHash,
355+
Key = key,
356+
};
357+
358+
result.AddRange(Hash(sessionKeyAdjustment.GetBytes()));
356359
}
357360

358361
return result.ToArray();
@@ -368,7 +371,7 @@ private byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, byte[]
368371
/// <returns></returns>
369372
private static byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, char p, byte[] sessionId)
370373
{
371-
var sessionKeyGeneration = new _SessionKeyGeneration
374+
var sessionKeyGeneration = new SessionKeyGeneration
372375
{
373376
SharedKey = sharedKey,
374377
ExchangeHash = exchangeHash,
@@ -378,11 +381,14 @@ private static byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash,
378381
return sessionKeyGeneration.GetBytes();
379382
}
380383

381-
private class _SessionKeyGeneration : SshData
384+
private class SessionKeyGeneration : SshData
382385
{
383386
public byte[] SharedKey { get; set; }
387+
384388
public byte[] ExchangeHash { get; set; }
389+
385390
public char Char { get; set; }
391+
386392
public byte[] SessionId { get; set; }
387393

388394
/// <summary>
@@ -419,10 +425,12 @@ protected override void SaveData()
419425
}
420426
}
421427

422-
private class _SessionKeyAdjustment : SshData
428+
private class SessionKeyAdjustment : SshData
423429
{
424430
public byte[] SharedKey { get; set; }
431+
425432
public byte[] ExchangeHash { get; set; }
433+
426434
public byte[] Key { get; set; }
427435

428436
/// <summary>

src/Renci.SshNet/Security/KeyExchangeEC.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Text;
22
using Renci.SshNet.Messages.Transport;
33
using Renci.SshNet.Common;
4-
using Renci.SshNet.Abstractions;
54

65
namespace Renci.SshNet.Security
76
{
@@ -45,21 +44,6 @@ internal abstract class KeyExchangeEC : KeyExchange
4544
/// </value>
4645
protected abstract int HashSize { get; }
4746

48-
/// <summary>
49-
/// Hashes the specified data bytes.
50-
/// </summary>
51-
/// <param name="hashData">The hash data.</param>
52-
/// <returns>
53-
/// Hashed bytes
54-
/// </returns>
55-
protected override byte[] Hash(byte[] hashData)
56-
{
57-
using (var sha256 = CryptoAbstraction.CreateSHA256())
58-
{
59-
return sha256.ComputeHash(hashData, 0, hashData.Length);
60-
}
61-
}
62-
6347
/// <summary>
6448
/// Calculates key exchange hash value.
6549
/// </summary>
@@ -68,7 +52,7 @@ protected override byte[] Hash(byte[] hashData)
6852
/// </returns>
6953
protected override byte[] CalculateHash()
7054
{
71-
var keyExchangeHashData = new KeyExchangeHashData
55+
var hashData = new KeyExchangeHashData
7256
{
7357
ClientVersion = Session.ClientVersion,
7458
ServerVersion = Session.ServerVersion,
@@ -77,10 +61,10 @@ protected override byte[] CalculateHash()
7761
HostKey = _hostKey,
7862
ClientExchangeValue = _clientExchangeValue,
7963
ServerExchangeValue = _serverExchangeValue,
80-
SharedKey = SharedKey
64+
SharedKey = SharedKey,
8165
};
8266

83-
return Hash(keyExchangeHashData.GetBytes());
67+
return Hash(hashData.GetBytes());
8468
}
8569

8670
/// <summary>
@@ -118,5 +102,5 @@ public override void Start(Session session, KeyExchangeInitMessage message)
118102
_serverPayload = message.GetBytes();
119103
_clientPayload = Session.ClientInitMessage.GetBytes();
120104
}
121-
}
105+
}
122106
}

src/Renci.SshNet/Security/KeyExchangeECCurve25519.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Renci.SshNet.Abstractions;
23
using Renci.SshNet.Common;
34
using Renci.SshNet.Messages.Transport;
45
using Renci.SshNet.Security.Chaos.NaCl;
@@ -65,6 +66,21 @@ public override void Finish()
6566
Session.KeyExchangeEcdhReplyMessageReceived -= Session_KeyExchangeEcdhReplyMessageReceived;
6667
}
6768

69+
/// <summary>
70+
/// Hashes the specified data bytes.
71+
/// </summary>
72+
/// <param name="hashData">The hash data.</param>
73+
/// <returns>
74+
/// Hashed bytes
75+
/// </returns>
76+
protected override byte[] Hash(byte[] hashData)
77+
{
78+
using (var sha256 = CryptoAbstraction.CreateSHA256())
79+
{
80+
return sha256.ComputeHash(hashData, 0, hashData.Length);
81+
}
82+
}
83+
6884
private void Session_KeyExchangeEcdhReplyMessageReceived(object sender, MessageEventArgs<KeyExchangeEcdhReplyMessage> e)
6985
{
7086
var message = e.Message;
@@ -92,7 +108,7 @@ private void HandleServerEcdhReply(byte[] hostKey, byte[] serverExchangeValue, b
92108

93109
var sharedKey = new byte[MontgomeryCurve25519.PublicKeySizeInBytes];
94110
MontgomeryOperations.scalarmult(sharedKey, 0, _privateKey, 0, serverExchangeValue, 0);
95-
SharedKey = sharedKey.ToBigNum2();
111+
SharedKey = sharedKey.ToBigInteger2().ToByteArray().Reverse();
96112
}
97113
}
98114
}

src/Renci.SshNet/Security/KeyExchangeECDH.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void HandleServerEcdhReply(byte[] hostKey, byte[] serverExchangeValue, b
100100
var publicKey = new ECPublicKeyParameters("ECDH", q, DomainParameters);
101101

102102
var k1 = KeyAgreement.CalculateAgreement(publicKey);
103-
SharedKey = k1.ToByteArray().ToBigNum2();
103+
SharedKey = k1.ToByteArray().ToBigInteger2().ToByteArray().Reverse();
104104
}
105105
}
106106
}

src/Renci.SshNet/Security/KeyExchangeECDH256.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Renci.SshNet.Security.Org.BouncyCastle.Asn1.Sec;
1+
using Renci.SshNet.Abstractions;
2+
using Renci.SshNet.Security.Org.BouncyCastle.Asn1.Sec;
23
using Renci.SshNet.Security.Org.BouncyCastle.Asn1.X9;
34

45
namespace Renci.SshNet.Security
@@ -34,5 +35,20 @@ protected override int HashSize
3435
{
3536
get { return 256; }
3637
}
38+
39+
/// <summary>
40+
/// Hashes the specified data bytes.
41+
/// </summary>
42+
/// <param name="hashData">The hash data.</param>
43+
/// <returns>
44+
/// Hashed bytes
45+
/// </returns>
46+
protected override byte[] Hash(byte[] hashData)
47+
{
48+
using (var sha256 = CryptoAbstraction.CreateSHA256())
49+
{
50+
return sha256.ComputeHash(hashData, 0, hashData.Length);
51+
}
52+
}
3753
}
3854
}

src/Renci.SshNet/Security/KeyExchangeHash.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Renci.SshNet.Common;
22
using System;
3-
using System.Linq;
43

54
namespace Renci.SshNet.Security
65
{

src/Renci.SshNet/Security/KeyHostAlgorithm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public override bool VerifySignature(byte[] data, byte[] signature)
8282
private class SshKeyData : SshData
8383
{
8484
private byte[] _name;
85-
private IList<byte[]> _keys;
85+
private List<byte[]> _keys;
8686

8787
public BigInteger[] Keys
8888
{

0 commit comments

Comments
 (0)