KeyExchangeDiffieHellman.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using Renci.SshNet.Messages.Transport;
  5. using Renci.SshNet.Common;
  6. namespace Renci.SshNet.Security
  7. {
  8. /// <summary>
  9. /// Represents base class for Diffie Hellman key exchange algorithm
  10. /// </summary>
  11. public abstract class KeyExchangeDiffieHellman : KeyExchange
  12. {
  13. /// <summary>
  14. /// Specifies key exchange group number.
  15. /// </summary>
  16. protected BigInteger _group;
  17. /// <summary>
  18. /// Specifies key exchange prime number.
  19. /// </summary>
  20. protected BigInteger _prime;
  21. /// <summary>
  22. /// Specifies client payload
  23. /// </summary>
  24. protected byte[] _clientPayload;
  25. /// <summary>
  26. /// Specifies server payload
  27. /// </summary>
  28. protected byte[] _serverPayload;
  29. /// <summary>
  30. /// Specifies client exchange number.
  31. /// </summary>
  32. protected BigInteger _clientExchangeValue;
  33. /// <summary>
  34. /// Specifies server exchange number.
  35. /// </summary>
  36. protected BigInteger _serverExchangeValue;
  37. /// <summary>
  38. /// Specifies random generated number.
  39. /// </summary>
  40. protected BigInteger _randomValue;
  41. /// <summary>
  42. /// Specifies host key data.
  43. /// </summary>
  44. protected byte[] _hostKey;
  45. /// <summary>
  46. /// Specifies signature data.
  47. /// </summary>
  48. protected byte[] _signature;
  49. /// <summary>
  50. /// Validates the exchange hash.
  51. /// </summary>
  52. /// <returns>
  53. /// true if exchange hash is valid; otherwise false.
  54. /// </returns>
  55. protected override bool ValidateExchangeHash()
  56. {
  57. var exchangeHash = this.CalculateHash();
  58. var length = (uint)(this._hostKey[0] << 24 | this._hostKey[1] << 16 | this._hostKey[2] << 8 | this._hostKey[3]);
  59. var algorithmName = Encoding.UTF8.GetString(this._hostKey, 4, (int)length);
  60. var key = this.Session.ConnectionInfo.HostKeyAlgorithms[algorithmName](this._hostKey);
  61. this.Session.ConnectionInfo.CurrentHostKeyAlgorithm = algorithmName;
  62. if (this.CanTrustHostKey(key))
  63. {
  64. return key.VerifySignature(exchangeHash, this._signature);
  65. }
  66. return false;
  67. }
  68. /// <summary>
  69. /// Starts key exchange algorithm
  70. /// </summary>
  71. /// <param name="session">The session.</param>
  72. /// <param name="message">Key exchange init message.</param>
  73. public override void Start(Session session, KeyExchangeInitMessage message)
  74. {
  75. base.Start(session, message);
  76. this._serverPayload = message.GetBytes().ToArray();
  77. this._clientPayload = this.Session.ClientInitMessage.GetBytes().ToArray();
  78. }
  79. /// <summary>
  80. /// Populates the client exchange value.
  81. /// </summary>
  82. protected void PopulateClientExchangeValue()
  83. {
  84. if (this._group.IsZero)
  85. throw new ArgumentNullException("_group");
  86. if (this._prime.IsZero)
  87. throw new ArgumentNullException("_prime");
  88. var bitLength = this._prime.BitLength;
  89. do
  90. {
  91. this._randomValue = BigInteger.Random(bitLength);
  92. this._clientExchangeValue = BigInteger.ModPow(this._group, this._randomValue, this._prime);
  93. } while (this._clientExchangeValue < 1 || this._clientExchangeValue > ((this._prime - 1)));
  94. }
  95. /// <summary>
  96. /// Handles the server DH reply message.
  97. /// </summary>
  98. /// <param name="hostKey">The host key.</param>
  99. /// <param name="serverExchangeValue">The server exchange value.</param>
  100. /// <param name="signature">The signature.</param>
  101. protected virtual void HandleServerDhReply(byte[] hostKey, BigInteger serverExchangeValue, byte[] signature)
  102. {
  103. this._serverExchangeValue = serverExchangeValue;
  104. this._hostKey = hostKey;
  105. this.SharedKey = BigInteger.ModPow(serverExchangeValue, this._randomValue, this._prime);
  106. this._signature = signature;
  107. }
  108. }
  109. }