KeyExchangeDiffieHellman.cs 4.6 KB

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