KeyExchangeDiffieHellmanGroupSha1.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Renci.SshNet.Common;
  2. using Renci.SshNet.Messages;
  3. using Renci.SshNet.Messages.Transport;
  4. namespace Renci.SshNet.Security
  5. {
  6. /// <summary>
  7. /// Represents "diffie-hellman-group1-sha1" algorithm implementation.
  8. /// </summary>
  9. public abstract class KeyExchangeDiffieHellmanGroupSha1 : KeyExchangeDiffieHellman
  10. {
  11. /// <summary>
  12. /// Gets the group prime.
  13. /// </summary>
  14. /// <value>
  15. /// The group prime.
  16. /// </value>
  17. public abstract BigInteger GroupPrime { get; }
  18. /// <summary>
  19. /// Calculates key exchange hash value.
  20. /// </summary>
  21. /// <returns>
  22. /// Key exchange hash.
  23. /// </returns>
  24. protected override byte[] CalculateHash()
  25. {
  26. var hashData = new _ExchangeHashData
  27. {
  28. ClientVersion = this.Session.ClientVersion,
  29. ServerVersion = this.Session.ServerVersion,
  30. ClientPayload = this._clientPayload,
  31. ServerPayload = this._serverPayload,
  32. HostKey = this._hostKey,
  33. ClientExchangeValue = this._clientExchangeValue,
  34. ServerExchangeValue = this._serverExchangeValue,
  35. SharedKey = this.SharedKey,
  36. }.GetBytes();
  37. return this.Hash(hashData);
  38. }
  39. /// <summary>
  40. /// Starts key exchange algorithm
  41. /// </summary>
  42. /// <param name="session">The session.</param>
  43. /// <param name="message">Key exchange init message.</param>
  44. public override void Start(Session session, KeyExchangeInitMessage message)
  45. {
  46. base.Start(session, message);
  47. this.Session.RegisterMessage("SSH_MSG_KEXDH_REPLY");
  48. this.Session.MessageReceived += Session_MessageReceived;
  49. this._prime = this.GroupPrime;
  50. this._group = new BigInteger(new byte[] { 2 });
  51. this.PopulateClientExchangeValue();
  52. this.SendMessage(new KeyExchangeDhInitMessage(this._clientExchangeValue));
  53. }
  54. /// <summary>
  55. /// Finishes key exchange algorithm.
  56. /// </summary>
  57. public override void Finish()
  58. {
  59. base.Finish();
  60. this.Session.MessageReceived -= Session_MessageReceived;
  61. }
  62. private void Session_MessageReceived(object sender, MessageEventArgs<Message> e)
  63. {
  64. var message = e.Message as KeyExchangeDhReplyMessage;
  65. if (message != null)
  66. {
  67. // Unregister message once received
  68. this.Session.UnRegisterMessage("SSH_MSG_KEXDH_REPLY");
  69. this.HandleServerDhReply(message.HostKey, message.F, message.Signature);
  70. // When SSH_MSG_KEXDH_REPLY received key exchange is completed
  71. this.Finish();
  72. }
  73. }
  74. private class _ExchangeHashData : SshData
  75. {
  76. public string ServerVersion { get; set; }
  77. public string ClientVersion { get; set; }
  78. public byte[] ClientPayload { get; set; }
  79. public byte[] ServerPayload { get; set; }
  80. public byte[] HostKey { get; set; }
  81. public BigInteger ClientExchangeValue { get; set; }
  82. public BigInteger ServerExchangeValue { get; set; }
  83. public BigInteger SharedKey { get; set; }
  84. protected override void LoadData()
  85. {
  86. throw new System.NotImplementedException();
  87. }
  88. protected override void SaveData()
  89. {
  90. this.Write(this.ClientVersion);
  91. this.Write(this.ServerVersion);
  92. this.WriteBinaryString(this.ClientPayload);
  93. this.WriteBinaryString(this.ServerPayload);
  94. this.WriteBinaryString(this.HostKey);
  95. this.Write(this.ClientExchangeValue);
  96. this.Write(this.ServerExchangeValue);
  97. this.Write(this.SharedKey);
  98. }
  99. }
  100. }
  101. }