KeyExchangeDiffieHellman.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Text;
  3. using Renci.SshNet.Messages.Transport;
  4. using Renci.SshNet.Common;
  5. namespace Renci.SshNet.Security
  6. {
  7. /// <summary>
  8. /// Represents base class for Diffie Hellman key exchange algorithm
  9. /// </summary>
  10. internal abstract class KeyExchangeDiffieHellman : KeyExchange
  11. {
  12. /// <summary>
  13. /// Specifies key exchange group number.
  14. /// </summary>
  15. protected BigInteger _group;
  16. /// <summary>
  17. /// Specifies key exchange prime number.
  18. /// </summary>
  19. protected BigInteger _prime;
  20. /// <summary>
  21. /// Specifies client payload
  22. /// </summary>
  23. protected byte[] _clientPayload;
  24. /// <summary>
  25. /// Specifies server payload
  26. /// </summary>
  27. protected byte[] _serverPayload;
  28. /// <summary>
  29. /// Specifies client exchange number.
  30. /// </summary>
  31. protected BigInteger _clientExchangeValue;
  32. /// <summary>
  33. /// Specifies server exchange number.
  34. /// </summary>
  35. protected BigInteger _serverExchangeValue;
  36. /// <summary>
  37. /// Specifies random generated number.
  38. /// </summary>
  39. protected BigInteger _privateExponent;
  40. /// <summary>
  41. /// Specifies host key data.
  42. /// </summary>
  43. protected byte[] _hostKey;
  44. /// <summary>
  45. /// Specifies signature data.
  46. /// </summary>
  47. protected byte[] _signature;
  48. /// <summary>
  49. /// Gets the size, in bits, of the computed hash code.
  50. /// </summary>
  51. /// <value>
  52. /// The size, in bits, of the computed hash code.
  53. /// </value>
  54. protected abstract int HashSize { get; }
  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 = CalculateHash();
  64. var length = Pack.BigEndianToUInt32(_hostKey);
  65. var algorithmName = Encoding.UTF8.GetString(_hostKey, 4, (int)length);
  66. var key = Session.ConnectionInfo.HostKeyAlgorithms[algorithmName](_hostKey);
  67. Session.ConnectionInfo.CurrentHostKeyAlgorithm = algorithmName;
  68. if (CanTrustHostKey(key))
  69. {
  70. return key.VerifySignature(exchangeHash, _signature);
  71. }
  72. return false;
  73. }
  74. /// <summary>
  75. /// Starts key exchange algorithm
  76. /// </summary>
  77. /// <param name="session">The session.</param>
  78. /// <param name="message">Key exchange init message.</param>
  79. public override void Start(Session session, KeyExchangeInitMessage message)
  80. {
  81. base.Start(session, message);
  82. _serverPayload = message.GetBytes();
  83. _clientPayload = Session.ClientInitMessage.GetBytes();
  84. }
  85. /// <summary>
  86. /// Populates the client exchange value.
  87. /// </summary>
  88. protected void PopulateClientExchangeValue()
  89. {
  90. if (_group.IsZero)
  91. throw new ArgumentNullException("_group");
  92. if (_prime.IsZero)
  93. throw new ArgumentNullException("_prime");
  94. // generate private exponent that is twice the hash size (RFC 4419) with a minimum
  95. // of 1024 bits (whatever is less)
  96. var privateExponentSize = Math.Max(HashSize * 2, 1024);
  97. do
  98. {
  99. // create private component
  100. _privateExponent = BigInteger.Random(privateExponentSize);
  101. // generate public component
  102. _clientExchangeValue = BigInteger.ModPow(_group, _privateExponent, _prime);
  103. } while (_clientExchangeValue < 1 || _clientExchangeValue > (_prime - 1));
  104. }
  105. /// <summary>
  106. /// Handles the server DH reply message.
  107. /// </summary>
  108. /// <param name="hostKey">The host key.</param>
  109. /// <param name="serverExchangeValue">The server exchange value.</param>
  110. /// <param name="signature">The signature.</param>
  111. protected virtual void HandleServerDhReply(byte[] hostKey, BigInteger serverExchangeValue, byte[] signature)
  112. {
  113. _serverExchangeValue = serverExchangeValue;
  114. _hostKey = hostKey;
  115. SharedKey = BigInteger.ModPow(serverExchangeValue, _privateExponent, _prime);
  116. _signature = signature;
  117. }
  118. }
  119. }