KeyExchangeEcdhInitMessage.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using Renci.SshNet.Common;
  3. namespace Renci.SshNet.Messages.Transport
  4. {
  5. /// <summary>
  6. /// Represents SSH_MSG_KEXECDH_INIT message.
  7. /// </summary>
  8. internal sealed class KeyExchangeEcdhInitMessage : Message, IKeyExchangedAllowed
  9. {
  10. /// <inheritdoc />
  11. public override string MessageName
  12. {
  13. get
  14. {
  15. return "SSH_MSG_KEX_ECDH_INIT";
  16. }
  17. }
  18. /// <inheritdoc />
  19. public override byte MessageNumber
  20. {
  21. get
  22. {
  23. return 30;
  24. }
  25. }
  26. /// <summary>
  27. /// Gets the client's ephemeral contribution to the ECDH exchange, encoded as an octet string.
  28. /// </summary>
  29. public byte[] QC { get; private set; }
  30. /// <summary>
  31. /// Gets the size of the message in bytes.
  32. /// </summary>
  33. /// <value>
  34. /// The size of the messages in bytes.
  35. /// </value>
  36. protected override int BufferCapacity
  37. {
  38. get
  39. {
  40. var capacity = base.BufferCapacity;
  41. capacity += 4; // QC length
  42. capacity += QC.Length; // QC
  43. return capacity;
  44. }
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="KeyExchangeEcdhInitMessage"/> class.
  48. /// </summary>
  49. public KeyExchangeEcdhInitMessage(byte[] q)
  50. {
  51. QC = q;
  52. }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="KeyExchangeEcdhInitMessage"/> class.
  55. /// </summary>
  56. public KeyExchangeEcdhInitMessage(BigInteger d, BigInteger q)
  57. {
  58. var dBytes = d.ToByteArray().Reverse();
  59. var qBytes = q.ToByteArray().Reverse();
  60. var data = new byte[dBytes.Length + qBytes.Length + 1];
  61. data[0] = 0x04;
  62. Buffer.BlockCopy(dBytes, 0, data, 1, dBytes.Length);
  63. Buffer.BlockCopy(qBytes, 0, data, dBytes.Length + 1, qBytes.Length);
  64. QC = data;
  65. }
  66. /// <summary>
  67. /// Called when type specific data need to be loaded.
  68. /// </summary>
  69. protected override void LoadData()
  70. {
  71. QC = ReadBinary();
  72. }
  73. /// <summary>
  74. /// Called when type specific data need to be saved.
  75. /// </summary>
  76. protected override void SaveData()
  77. {
  78. WriteBinaryString(QC);
  79. }
  80. internal override void Process(Session session)
  81. {
  82. throw new NotImplementedException();
  83. }
  84. }
  85. }