KeyExchangeDiffieHellman.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. if (this.CanTrustHostKey(this._hostKey))
  64. {
  65. var exchangeHash = this.CalculateHash();
  66. var length = (uint)(this._hostKey[0] << 24 | this._hostKey[1] << 16 | this._hostKey[2] << 8 | this._hostKey[3]);
  67. var algorithmName = Encoding.UTF8.GetString(this._hostKey, 4, (int)length);
  68. var key = this.Session.ConnectionInfo.HostKeyAlgorithms[algorithmName](this._hostKey);
  69. return key.VerifySignature(exchangeHash, this._signature);
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. /// <summary>
  77. /// Starts key exchange algorithm
  78. /// </summary>
  79. /// <param name="session">The session.</param>
  80. /// <param name="message">Key exchange init message.</param>
  81. public override void Start(Session session, KeyExchangeInitMessage message)
  82. {
  83. base.Start(session, message);
  84. this._serverPayload = message.GetBytes().ToArray();
  85. this._clientPayload = this.Session.ClientInitMessage.GetBytes().ToArray();
  86. }
  87. /// <summary>
  88. /// Populates the client exchange value.
  89. /// </summary>
  90. protected void PopulateClientExchangeValue()
  91. {
  92. if (this._group.IsZero)
  93. throw new ArgumentNullException("_group");
  94. if (this._prime.IsZero)
  95. throw new ArgumentNullException("_prime");
  96. var bitLength = this._prime.BitLength;
  97. do
  98. {
  99. this._randomValue = BigInteger.Random(bitLength);
  100. this._clientExchangeValue = BigInteger.ModPow(this._group, this._randomValue, this._prime);
  101. } while (this._clientExchangeValue < 1 || this._clientExchangeValue > ((this._prime - 1)));
  102. }
  103. /// <summary>
  104. /// Handles the server DH reply message.
  105. /// </summary>
  106. /// <param name="hostKey">The host key.</param>
  107. /// <param name="serverExchangeValue">The server exchange value.</param>
  108. /// <param name="signature">The signature.</param>
  109. protected virtual void HandleServerDhReply(byte[] hostKey, BigInteger serverExchangeValue, byte[] signature)
  110. {
  111. this._serverExchangeValue = serverExchangeValue;
  112. this._hostKey = hostKey;
  113. this.SharedKey = BigInteger.ModPow(serverExchangeValue, this._randomValue, this._prime);
  114. this._signature = signature;
  115. }
  116. }
  117. }