mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 01:05:42 +00:00
@@ -72,21 +72,6 @@ namespace Renci.SshNet.Common
|
||||
return new BigInteger(reversed.Reverse());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format
|
||||
/// </summary>
|
||||
public static byte[] ToBigNum2(this byte[] data)
|
||||
{
|
||||
if ((data[0] & (1 << 7)) != 0)
|
||||
{
|
||||
var buf = new byte[data.Length + 1];
|
||||
Buffer.BlockCopy(data, 0, buf, 1, data.Length);
|
||||
data = buf;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format
|
||||
/// </summary>
|
||||
|
||||
@@ -345,14 +345,17 @@ namespace Renci.SshNet.Security
|
||||
private byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, byte[] key, int size)
|
||||
{
|
||||
var result = new List<byte>(key);
|
||||
|
||||
while (size > result.Count)
|
||||
{
|
||||
result.AddRange(Hash(new _SessionKeyAdjustment
|
||||
{
|
||||
SharedKey = sharedKey,
|
||||
ExchangeHash = exchangeHash,
|
||||
Key = key,
|
||||
}.GetBytes()));
|
||||
var sessionKeyAdjustment = new SessionKeyAdjustment
|
||||
{
|
||||
SharedKey = sharedKey,
|
||||
ExchangeHash = exchangeHash,
|
||||
Key = key,
|
||||
};
|
||||
|
||||
result.AddRange(Hash(sessionKeyAdjustment.GetBytes()));
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
@@ -368,7 +371,7 @@ namespace Renci.SshNet.Security
|
||||
/// <returns></returns>
|
||||
private static byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, char p, byte[] sessionId)
|
||||
{
|
||||
var sessionKeyGeneration = new _SessionKeyGeneration
|
||||
var sessionKeyGeneration = new SessionKeyGeneration
|
||||
{
|
||||
SharedKey = sharedKey,
|
||||
ExchangeHash = exchangeHash,
|
||||
@@ -378,11 +381,14 @@ namespace Renci.SshNet.Security
|
||||
return sessionKeyGeneration.GetBytes();
|
||||
}
|
||||
|
||||
private class _SessionKeyGeneration : SshData
|
||||
private class SessionKeyGeneration : SshData
|
||||
{
|
||||
public byte[] SharedKey { get; set; }
|
||||
|
||||
public byte[] ExchangeHash { get; set; }
|
||||
|
||||
public char Char { get; set; }
|
||||
|
||||
public byte[] SessionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
@@ -419,10 +425,12 @@ namespace Renci.SshNet.Security
|
||||
}
|
||||
}
|
||||
|
||||
private class _SessionKeyAdjustment : SshData
|
||||
private class SessionKeyAdjustment : SshData
|
||||
{
|
||||
public byte[] SharedKey { get; set; }
|
||||
|
||||
public byte[] ExchangeHash { get; set; }
|
||||
|
||||
public byte[] Key { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Text;
|
||||
using Renci.SshNet.Messages.Transport;
|
||||
using Renci.SshNet.Common;
|
||||
using Renci.SshNet.Abstractions;
|
||||
|
||||
namespace Renci.SshNet.Security
|
||||
{
|
||||
@@ -45,21 +44,6 @@ namespace Renci.SshNet.Security
|
||||
/// </value>
|
||||
protected abstract int HashSize { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Hashes the specified data bytes.
|
||||
/// </summary>
|
||||
/// <param name="hashData">The hash data.</param>
|
||||
/// <returns>
|
||||
/// Hashed bytes
|
||||
/// </returns>
|
||||
protected override byte[] Hash(byte[] hashData)
|
||||
{
|
||||
using (var sha256 = CryptoAbstraction.CreateSHA256())
|
||||
{
|
||||
return sha256.ComputeHash(hashData, 0, hashData.Length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates key exchange hash value.
|
||||
/// </summary>
|
||||
@@ -68,7 +52,7 @@ namespace Renci.SshNet.Security
|
||||
/// </returns>
|
||||
protected override byte[] CalculateHash()
|
||||
{
|
||||
var keyExchangeHashData = new KeyExchangeHashData
|
||||
var hashData = new KeyExchangeHashData
|
||||
{
|
||||
ClientVersion = Session.ClientVersion,
|
||||
ServerVersion = Session.ServerVersion,
|
||||
@@ -77,10 +61,10 @@ namespace Renci.SshNet.Security
|
||||
HostKey = _hostKey,
|
||||
ClientExchangeValue = _clientExchangeValue,
|
||||
ServerExchangeValue = _serverExchangeValue,
|
||||
SharedKey = SharedKey
|
||||
SharedKey = SharedKey,
|
||||
};
|
||||
|
||||
return Hash(keyExchangeHashData.GetBytes());
|
||||
return Hash(hashData.GetBytes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -118,5 +102,5 @@ namespace Renci.SshNet.Security
|
||||
_serverPayload = message.GetBytes();
|
||||
_clientPayload = Session.ClientInitMessage.GetBytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Renci.SshNet.Abstractions;
|
||||
using Renci.SshNet.Common;
|
||||
using Renci.SshNet.Messages.Transport;
|
||||
using Renci.SshNet.Security.Chaos.NaCl;
|
||||
@@ -65,6 +66,21 @@ namespace Renci.SshNet.Security
|
||||
Session.KeyExchangeEcdhReplyMessageReceived -= Session_KeyExchangeEcdhReplyMessageReceived;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hashes the specified data bytes.
|
||||
/// </summary>
|
||||
/// <param name="hashData">The hash data.</param>
|
||||
/// <returns>
|
||||
/// Hashed bytes
|
||||
/// </returns>
|
||||
protected override byte[] Hash(byte[] hashData)
|
||||
{
|
||||
using (var sha256 = CryptoAbstraction.CreateSHA256())
|
||||
{
|
||||
return sha256.ComputeHash(hashData, 0, hashData.Length);
|
||||
}
|
||||
}
|
||||
|
||||
private void Session_KeyExchangeEcdhReplyMessageReceived(object sender, MessageEventArgs<KeyExchangeEcdhReplyMessage> e)
|
||||
{
|
||||
var message = e.Message;
|
||||
@@ -92,7 +108,7 @@ namespace Renci.SshNet.Security
|
||||
|
||||
var sharedKey = new byte[MontgomeryCurve25519.PublicKeySizeInBytes];
|
||||
MontgomeryOperations.scalarmult(sharedKey, 0, _privateKey, 0, serverExchangeValue, 0);
|
||||
SharedKey = sharedKey.ToBigNum2();
|
||||
SharedKey = sharedKey.ToBigInteger2().ToByteArray().Reverse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ namespace Renci.SshNet.Security
|
||||
var publicKey = new ECPublicKeyParameters("ECDH", q, DomainParameters);
|
||||
|
||||
var k1 = KeyAgreement.CalculateAgreement(publicKey);
|
||||
SharedKey = k1.ToByteArray().ToBigNum2();
|
||||
SharedKey = k1.ToByteArray().ToBigInteger2().ToByteArray().Reverse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Renci.SshNet.Security.Org.BouncyCastle.Asn1.Sec;
|
||||
using Renci.SshNet.Abstractions;
|
||||
using Renci.SshNet.Security.Org.BouncyCastle.Asn1.Sec;
|
||||
using Renci.SshNet.Security.Org.BouncyCastle.Asn1.X9;
|
||||
|
||||
namespace Renci.SshNet.Security
|
||||
@@ -34,5 +35,20 @@ namespace Renci.SshNet.Security
|
||||
{
|
||||
get { return 256; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hashes the specified data bytes.
|
||||
/// </summary>
|
||||
/// <param name="hashData">The hash data.</param>
|
||||
/// <returns>
|
||||
/// Hashed bytes
|
||||
/// </returns>
|
||||
protected override byte[] Hash(byte[] hashData)
|
||||
{
|
||||
using (var sha256 = CryptoAbstraction.CreateSHA256())
|
||||
{
|
||||
return sha256.ComputeHash(hashData, 0, hashData.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using Renci.SshNet.Common;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Renci.SshNet.Security
|
||||
{
|
||||
|
||||
@@ -82,7 +82,7 @@ namespace Renci.SshNet.Security
|
||||
private class SshKeyData : SshData
|
||||
{
|
||||
private byte[] _name;
|
||||
private IList<byte[]> _keys;
|
||||
private List<byte[]> _keys;
|
||||
|
||||
public BigInteger[] Keys
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user