KeyExchangeDiffieHellmanGroupSha1.cs 4.1 KB

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