KeyExchangeInitMessage.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. namespace Renci.SshNet.Messages.Transport
  5. {
  6. /// <summary>
  7. /// Represents SSH_MSG_KEXINIT message.
  8. /// </summary>
  9. [Message("SSH_MSG_KEXINIT", 20)]
  10. public class KeyExchangeInitMessage : Message, IKeyExchangedAllowed
  11. {
  12. private static RNGCryptoServiceProvider _randomizer = new System.Security.Cryptography.RNGCryptoServiceProvider();
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="KeyExchangeInitMessage"/> class.
  15. /// </summary>
  16. public KeyExchangeInitMessage()
  17. {
  18. var cookie = new byte[16];
  19. _randomizer.GetBytes(cookie);
  20. this.Cookie = cookie;
  21. }
  22. #region Message Properties
  23. /// <summary>
  24. /// Gets session cookie.
  25. /// </summary>
  26. public byte[] Cookie { get; private set; }
  27. /// <summary>
  28. /// Gets or sets supported key exchange algorithms.
  29. /// </summary>
  30. /// <value>
  31. /// Supported key exchange algorithms.
  32. /// </value>
  33. public string[] KeyExchangeAlgorithms { get; set; }
  34. /// <summary>
  35. /// Gets or sets supported server host key algorithms.
  36. /// </summary>
  37. /// <value>
  38. /// Supported server host key algorithms.
  39. /// </value>
  40. public string[] ServerHostKeyAlgorithms { get; set; }
  41. /// <summary>
  42. /// Gets or sets supported encryption algorithms client to server.
  43. /// </summary>
  44. /// <value>
  45. /// Supported encryption algorithms client to server.
  46. /// </value>
  47. public string[] EncryptionAlgorithmsClientToServer { get; set; }
  48. /// <summary>
  49. /// Gets or sets supported encryption algorithms server to client.
  50. /// </summary>
  51. /// <value>
  52. /// Supported encryption algorithms server to client.
  53. /// </value>
  54. public string[] EncryptionAlgorithmsServerToClient { get; set; }
  55. /// <summary>
  56. /// Gets or sets supported hash algorithms client to server.
  57. /// </summary>
  58. /// <value>
  59. /// Supported hash algorithms client to server.
  60. /// </value>
  61. public string[] MacAlgorithmsClientToServer { get; set; }
  62. /// <summary>
  63. /// Gets or sets supported hash algorithms server to client.
  64. /// </summary>
  65. /// <value>
  66. /// Supported hash algorithms server to client.
  67. /// </value>
  68. public string[] MacAlgorithmsServerToClient { get; set; }
  69. /// <summary>
  70. /// Gets or sets supported compression algorithms client to server.
  71. /// </summary>
  72. /// <value>
  73. /// Supported compression algorithms client to server.
  74. /// </value>
  75. public string[] CompressionAlgorithmsClientToServer { get; set; }
  76. /// <summary>
  77. /// Gets or sets supported compression algorithms server to client.
  78. /// </summary>
  79. /// <value>
  80. /// Supported compression algorithms server to client.
  81. /// </value>
  82. public string[] CompressionAlgorithmsServerToClient { get; set; }
  83. /// <summary>
  84. /// Gets or sets supported languages client to server.
  85. /// </summary>
  86. /// <value>
  87. /// Supported languages client to server.
  88. /// </value>
  89. public string[] LanguagesClientToServer { get; set; }
  90. /// <summary>
  91. /// Gets or sets supported languages server to client.
  92. /// </summary>
  93. /// <value>
  94. /// The languages server to client.
  95. /// </value>
  96. public string[] LanguagesServerToClient { get; set; }
  97. /// <summary>
  98. /// Gets or sets a value indicating whether first key exchange packet follows.
  99. /// </summary>
  100. /// <value>
  101. /// <c>true</c> if first key exchange packet follows; otherwise, <c>false</c>.
  102. /// </value>
  103. public bool FirstKexPacketFollows { get; set; }
  104. /// <summary>
  105. /// Gets or sets the reserved value.
  106. /// </summary>
  107. /// <value>
  108. /// The reserved value.
  109. /// </value>
  110. public UInt32 Reserved { get; set; }
  111. #endregion
  112. /// <summary>
  113. /// Called when type specific data need to be loaded.
  114. /// </summary>
  115. protected override void LoadData()
  116. {
  117. this.ResetReader();
  118. this.Cookie = this.ReadBytes(16);
  119. this.KeyExchangeAlgorithms = this.ReadNamesList();
  120. this.ServerHostKeyAlgorithms = this.ReadNamesList();
  121. this.EncryptionAlgorithmsClientToServer = this.ReadNamesList();
  122. this.EncryptionAlgorithmsServerToClient = this.ReadNamesList();
  123. this.MacAlgorithmsClientToServer = this.ReadNamesList();
  124. this.MacAlgorithmsServerToClient = this.ReadNamesList();
  125. this.CompressionAlgorithmsClientToServer = this.ReadNamesList();
  126. this.CompressionAlgorithmsServerToClient = this.ReadNamesList();
  127. this.LanguagesClientToServer = this.ReadNamesList();
  128. this.LanguagesServerToClient = this.ReadNamesList();
  129. this.FirstKexPacketFollows = this.ReadBoolean();
  130. this.Reserved = this.ReadUInt32();
  131. }
  132. /// <summary>
  133. /// Called when type specific data need to be saved.
  134. /// </summary>
  135. protected override void SaveData()
  136. {
  137. this.Write(this.Cookie);
  138. this.Write(this.KeyExchangeAlgorithms);
  139. this.Write(this.ServerHostKeyAlgorithms);
  140. this.Write(this.EncryptionAlgorithmsClientToServer);
  141. this.Write(this.EncryptionAlgorithmsServerToClient);
  142. this.Write(this.MacAlgorithmsClientToServer);
  143. this.Write(this.MacAlgorithmsServerToClient);
  144. this.Write(this.CompressionAlgorithmsClientToServer);
  145. this.Write(this.CompressionAlgorithmsServerToClient);
  146. this.Write(this.LanguagesClientToServer);
  147. this.Write(this.LanguagesServerToClient);
  148. this.Write(this.FirstKexPacketFollows);
  149. this.Write(this.Reserved);
  150. }
  151. }
  152. }