KeyExchangeDiffieHellman.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // TODO: See how to improve this area
  65. var bytes = this._hostKey;
  66. var length = (uint)(this._hostKey[0] << 24 | this._hostKey[1] << 16 | this._hostKey[2] << 8 | this._hostKey[3]);
  67. var algorithmName = Renci.SshNet.Common.ASCIIEncoding.Current.GetString(bytes.Skip(4).Take((int)length).ToArray());
  68. var data = bytes.Skip(4 + algorithmName.Length);
  69. var key = this.Session.ConnectionInfo.HostKeyAlgorithms[algorithmName](this._hostKey);
  70. return key.VerifySignature(exchangeHash, this._signature);
  71. }
  72. /// <summary>
  73. /// Starts key exchange algorithm
  74. /// </summary>
  75. /// <param name="session">The session.</param>
  76. /// <param name="message">Key exchange init message.</param>
  77. public override void Start(Session session, KeyExchangeInitMessage message)
  78. {
  79. base.Start(session, message);
  80. this._serverPayload = message.GetBytes().ToArray();
  81. this._clientPayload = this.Session.ClientInitMessage.GetBytes().ToArray();
  82. }
  83. /// <summary>
  84. /// Populates the client exchange value.
  85. /// </summary>
  86. protected void PopulateClientExchangeValue()
  87. {
  88. if (this._group.IsZero)
  89. throw new ArgumentNullException("_group");
  90. if (this._prime.IsZero)
  91. throw new ArgumentNullException("_prime");
  92. var bytesArray = new byte[128];
  93. do
  94. {
  95. _randomizer.GetBytes(bytesArray);
  96. bytesArray[bytesArray.Length - 1] = (byte)(bytesArray[bytesArray.Length - 1] & 0x7F); // Ensure not a negative value
  97. this._randomValue = new BigInteger(bytesArray);
  98. this._clientExchangeValue = BigInteger.ModPow(this._group, this._randomValue, this._prime);
  99. } while (this._clientExchangeValue < 1 || this._clientExchangeValue > ((this._prime - 1)));
  100. }
  101. /// <summary>
  102. /// Handles the server DH reply message.
  103. /// </summary>
  104. /// <param name="hostKey">The host key.</param>
  105. /// <param name="serverExchangeValue">The server exchange value.</param>
  106. /// <param name="signature">The signature.</param>
  107. protected virtual void HandleServerDhReply(byte[] hostKey, BigInteger serverExchangeValue, byte[] signature)
  108. {
  109. this._serverExchangeValue = serverExchangeValue;
  110. this._hostKey = hostKey;
  111. this.SharedKey = BigInteger.ModPow(serverExchangeValue, this._randomValue, this._prime);
  112. this._signature = signature;
  113. }
  114. }
  115. }