KeyExchangeDiffieHellmanGroup1Sha1.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 class KeyExchangeDiffieHellmanGroup1Sha1 : KeyExchangeDiffieHellman
  15. {
  16. /// <summary>
  17. /// Gets algorithm name.
  18. /// </summary>
  19. public override string Name
  20. {
  21. get { return "diffie-hellman-group1-sha1"; }
  22. }
  23. /// <summary>
  24. /// Calculates key exchange hash value.
  25. /// </summary>
  26. /// <returns>
  27. /// Key exchange hash.
  28. /// </returns>
  29. protected override byte[] CalculateHash()
  30. {
  31. var hashData = new _ExchangeHashData
  32. {
  33. ClientVersion = this.Session.ClientVersion,
  34. ServerVersion = this.Session.ServerVersion,
  35. ClientPayload = this._clientPayload,
  36. ServerPayload = this._serverPayload,
  37. HostKey = this._hostKey,
  38. ClientExchangeValue = this._clientExchangeValue,
  39. ServerExchangeValue = this._serverExchangeValue,
  40. SharedKey = this.SharedKey,
  41. }.GetBytes();
  42. return this.Hash(hashData);
  43. }
  44. /// <summary>
  45. /// Starts key exchange algorithm
  46. /// </summary>
  47. /// <param name="session">The session.</param>
  48. /// <param name="message">Key exchange init message.</param>
  49. public override void Start(Session session, KeyExchangeInitMessage message)
  50. {
  51. base.Start(session, message);
  52. this.Session.RegisterMessage("SSH_MSG_KEXDH_REPLY");
  53. this.Session.MessageReceived += Session_MessageReceived;
  54. BigInteger prime;
  55. var secondOkleyGroup = @"00FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF";
  56. BigInteger.TryParse(secondOkleyGroup, System.Globalization.NumberStyles.AllowHexSpecifier, CultureInfo.CurrentCulture, out prime);
  57. this._prime = prime;
  58. this._group = new BigInteger(new byte[] { 2 });
  59. this.PopulateClientExchangeValue();
  60. this.SendMessage(new KeyExchangeDhInitMessage(this._clientExchangeValue));
  61. }
  62. /// <summary>
  63. /// Finishes key exchange algorithm.
  64. /// </summary>
  65. public override void Finish()
  66. {
  67. base.Finish();
  68. this.Session.MessageReceived -= Session_MessageReceived;
  69. }
  70. private void Session_MessageReceived(object sender, MessageEventArgs<Message> e)
  71. {
  72. var message = e.Message as KeyExchangeDhReplyMessage;
  73. if (message != null)
  74. {
  75. // Unregister message once received
  76. this.Session.UnRegisterMessage("SSH_MSG_KEXDH_REPLY");
  77. this.HandleServerDhReply(message.HostKey, message.F, message.Signature);
  78. // When SSH_MSG_KEXDH_REPLY received key exchange is completed
  79. this.Finish();
  80. }
  81. }
  82. private class _ExchangeHashData : SshData
  83. {
  84. public string ServerVersion { get; set; }
  85. public string ClientVersion { get; set; }
  86. public byte[] ClientPayload { get; set; }
  87. public byte[] ServerPayload { get; set; }
  88. public byte[] HostKey { get; set; }
  89. public BigInteger ClientExchangeValue { get; set; }
  90. public BigInteger ServerExchangeValue { get; set; }
  91. public BigInteger SharedKey { get; set; }
  92. protected override void LoadData()
  93. {
  94. throw new System.NotImplementedException();
  95. }
  96. protected override void SaveData()
  97. {
  98. this.Write(this.ClientVersion);
  99. this.Write(this.ServerVersion);
  100. this.WriteBinaryString(this.ClientPayload);
  101. this.WriteBinaryString(this.ServerPayload);
  102. this.WriteBinaryString(this.HostKey);
  103. this.Write(this.ClientExchangeValue);
  104. this.Write(this.ServerExchangeValue);
  105. this.Write(this.SharedKey);
  106. }
  107. }
  108. }
  109. }