NegotiateContext.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using Utilities;
  10. namespace SMBLibrary.SMB2
  11. {
  12. /// <summary>
  13. /// [MS-SMB2] 2.2.3.1 - NEGOTIATE_CONTEXT
  14. /// </summary>
  15. public class NegotiateContext
  16. {
  17. public const int FixedLength = 8;
  18. public NegotiateContextType ContextType;
  19. private ushort DataLength;
  20. public uint Reserved;
  21. public byte[] Data = new byte[0];
  22. public NegotiateContext()
  23. {
  24. }
  25. public NegotiateContext(byte[] buffer, int offset)
  26. {
  27. ContextType = (NegotiateContextType)LittleEndianConverter.ToUInt16(buffer, offset + 0);
  28. DataLength = LittleEndianConverter.ToUInt16(buffer, offset + 2);
  29. Reserved = LittleEndianConverter.ToUInt32(buffer, offset + 4);
  30. ByteReader.ReadBytes(buffer, offset + 8, DataLength);
  31. }
  32. public void WriteBytes(byte[] buffer, int offset)
  33. {
  34. DataLength = (ushort)Data.Length;
  35. LittleEndianWriter.WriteUInt16(buffer, offset + 0, (ushort)ContextType);
  36. LittleEndianWriter.WriteUInt16(buffer, offset + 2, DataLength);
  37. LittleEndianWriter.WriteUInt32(buffer, offset + 4, Reserved);
  38. ByteWriter.WriteBytes(buffer, offset + 8, Data);
  39. }
  40. public int Length
  41. {
  42. get
  43. {
  44. return FixedLength + Data.Length;
  45. }
  46. }
  47. public static List<NegotiateContext> ReadNegotiateContextList(byte[] buffer, int offset, int count)
  48. {
  49. List<NegotiateContext> result = new List<NegotiateContext>();
  50. for (int index = 0; index < count; index++)
  51. {
  52. NegotiateContext context = new NegotiateContext(buffer, offset);
  53. result.Add(context);
  54. offset += context.Length;
  55. }
  56. return result;
  57. }
  58. public static void WriteNegotiateContextList(byte[] buffer, int offset, List<NegotiateContext> negotiateContextList)
  59. {
  60. // Subsequent negotiate contexts MUST appear at the first 8-byte aligned offset following the previous negotiate context
  61. for (int index = 0; index < negotiateContextList.Count; index++)
  62. {
  63. NegotiateContext context = negotiateContextList[index];
  64. int length = context.Length;
  65. int paddedLength = (int)Math.Ceiling((double)length / 8) * 8;
  66. context.WriteBytes(buffer, offset);
  67. offset += paddedLength;
  68. }
  69. }
  70. public static int GetNegotiateContextListLength(List<NegotiateContext> negotiateContextList)
  71. {
  72. int result = 0;
  73. for (int index = 0; index < negotiateContextList.Count; index++)
  74. {
  75. NegotiateContext context = negotiateContextList[index];
  76. int length = context.Length;
  77. if (index < negotiateContextList.Count - 1)
  78. {
  79. int paddedLength = (int)Math.Ceiling((double)length / 8) * 8;
  80. result += paddedLength;
  81. }
  82. else
  83. {
  84. result += length;
  85. }
  86. }
  87. return result;
  88. }
  89. }
  90. }