KeyExchangeEC.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Renci.SshNet.Messages.Transport;
  2. namespace Renci.SshNet.Security
  3. {
  4. internal abstract class KeyExchangeEC : KeyExchange
  5. {
  6. #pragma warning disable SA1401 // Fields should be private
  7. /// <summary>
  8. /// Specifies client payload.
  9. /// </summary>
  10. protected byte[] _clientPayload;
  11. /// <summary>
  12. /// Specifies server payload.
  13. /// </summary>
  14. protected byte[] _serverPayload;
  15. /// <summary>
  16. /// Specifies client exchange.
  17. /// </summary>
  18. protected byte[] _clientExchangeValue;
  19. /// <summary>
  20. /// Specifies server exchange.
  21. /// </summary>
  22. protected byte[] _serverExchangeValue;
  23. /// <summary>
  24. /// Specifies host key data.
  25. /// </summary>
  26. protected byte[] _hostKey;
  27. /// <summary>
  28. /// Specifies signature data.
  29. /// </summary>
  30. protected byte[] _signature;
  31. #pragma warning restore SA1401 // Fields should be private
  32. /// <summary>
  33. /// Gets the size, in bits, of the computed hash code.
  34. /// </summary>
  35. /// <value>
  36. /// The size, in bits, of the computed hash code.
  37. /// </value>
  38. protected abstract int HashSize { get; }
  39. /// <summary>
  40. /// Calculates key exchange hash value.
  41. /// </summary>
  42. /// <returns>
  43. /// Key exchange hash.
  44. /// </returns>
  45. protected override byte[] CalculateHash()
  46. {
  47. var hashData = new KeyExchangeHashData
  48. {
  49. ClientVersion = Session.ClientVersion,
  50. ServerVersion = Session.ServerVersion,
  51. ClientPayload = _clientPayload,
  52. ServerPayload = _serverPayload,
  53. HostKey = _hostKey,
  54. ClientExchangeValue = _clientExchangeValue,
  55. ServerExchangeValue = _serverExchangeValue,
  56. SharedKey = SharedKey,
  57. };
  58. return Hash(hashData.GetBytes());
  59. }
  60. /// <summary>
  61. /// Validates the exchange hash.
  62. /// </summary>
  63. /// <returns>
  64. /// true if exchange hash is valid; otherwise false.
  65. /// </returns>
  66. protected override bool ValidateExchangeHash()
  67. {
  68. return ValidateExchangeHash(_hostKey, _signature);
  69. }
  70. /// <inheritdoc/>
  71. public override void Start(Session session, KeyExchangeInitMessage message, bool sendClientInitMessage)
  72. {
  73. base.Start(session, message, sendClientInitMessage);
  74. _serverPayload = message.GetBytes();
  75. _clientPayload = Session.ClientInitMessage.GetBytes();
  76. }
  77. }
  78. }