GroupExchangeHashData.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using Renci.SshNet.Common;
  3. namespace Renci.SshNet.Security
  4. {
  5. internal sealed class GroupExchangeHashData : SshData
  6. {
  7. private byte[] _serverVersion;
  8. private byte[] _clientVersion;
  9. private byte[] _prime;
  10. private byte[] _subGroup;
  11. public string ServerVersion
  12. {
  13. private get { return Utf8.GetString(_serverVersion, 0, _serverVersion.Length); }
  14. set { _serverVersion = Utf8.GetBytes(value); }
  15. }
  16. public string ClientVersion
  17. {
  18. private get { return Utf8.GetString(_clientVersion, 0, _clientVersion.Length); }
  19. set { _clientVersion = Utf8.GetBytes(value); }
  20. }
  21. public byte[] ClientPayload { get; set; }
  22. public byte[] ServerPayload { get; set; }
  23. public byte[] HostKey { get; set; }
  24. public uint MinimumGroupSize { get; set; }
  25. public uint PreferredGroupSize { get; set; }
  26. public uint MaximumGroupSize { get; set; }
  27. public BigInteger Prime
  28. {
  29. private get { return _prime.ToBigInteger(); }
  30. set { _prime = value.ToByteArray().Reverse(); }
  31. }
  32. public BigInteger SubGroup
  33. {
  34. private get { return _subGroup.ToBigInteger(); }
  35. set { _subGroup = value.ToByteArray().Reverse(); }
  36. }
  37. public byte[] ClientExchangeValue { get; set; }
  38. public byte[] ServerExchangeValue { get; set; }
  39. public byte[] SharedKey { get; set; }
  40. /// <summary>
  41. /// Gets the size of the message in bytes.
  42. /// </summary>
  43. /// <value>
  44. /// The size of the messages in bytes.
  45. /// </value>
  46. protected override int BufferCapacity
  47. {
  48. get
  49. {
  50. var capacity = base.BufferCapacity;
  51. capacity += 4; // ClientVersion length
  52. capacity += _clientVersion.Length; // ClientVersion
  53. capacity += 4; // ServerVersion length
  54. capacity += _serverVersion.Length; // ServerVersion
  55. capacity += 4; // ClientPayload length
  56. capacity += ClientPayload.Length; // ClientPayload
  57. capacity += 4; // ServerPayload length
  58. capacity += ServerPayload.Length; // ServerPayload
  59. capacity += 4; // HostKey length
  60. capacity += HostKey.Length; // HostKey
  61. capacity += 4; // MinimumGroupSize
  62. capacity += 4; // PreferredGroupSize
  63. capacity += 4; // MaximumGroupSize
  64. capacity += 4; // Prime length
  65. capacity += _prime.Length; // Prime
  66. capacity += 4; // SubGroup length
  67. capacity += _subGroup.Length; // SubGroup
  68. capacity += 4; // ClientExchangeValue length
  69. capacity += ClientExchangeValue.Length; // ClientExchangeValue
  70. capacity += 4; // ServerExchangeValue length
  71. capacity += ServerExchangeValue.Length; // ServerExchangeValue
  72. capacity += 4; // SharedKey length
  73. capacity += SharedKey.Length; // SharedKey
  74. return capacity;
  75. }
  76. }
  77. protected override void LoadData()
  78. {
  79. throw new NotImplementedException();
  80. }
  81. protected override void SaveData()
  82. {
  83. WriteBinaryString(_clientVersion);
  84. WriteBinaryString(_serverVersion);
  85. WriteBinaryString(ClientPayload);
  86. WriteBinaryString(ServerPayload);
  87. WriteBinaryString(HostKey);
  88. Write(MinimumGroupSize);
  89. Write(PreferredGroupSize);
  90. Write(MaximumGroupSize);
  91. WriteBinaryString(_prime);
  92. WriteBinaryString(_subGroup);
  93. WriteBinaryString(ClientExchangeValue);
  94. WriteBinaryString(ServerExchangeValue);
  95. WriteBinaryString(SharedKey);
  96. }
  97. }
  98. }