AVPairUtils.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Copyright (C) 2014 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 System.Text;
  10. using Utilities;
  11. namespace SMBLibrary.Authentication
  12. {
  13. public class AVPairUtils
  14. {
  15. public static byte[] GetAVPairSequence(string domainName, string computerName)
  16. {
  17. KeyValuePairList<AVPairKey, byte[]> pairs = new KeyValuePairList<AVPairKey, byte[]>();
  18. pairs.Add(AVPairKey.NbDomainName, UnicodeEncoding.Unicode.GetBytes(domainName));
  19. pairs.Add(AVPairKey.NbComputerName, UnicodeEncoding.Unicode.GetBytes(computerName));
  20. return AVPairUtils.GetAVPairSequenceBytes(pairs);
  21. }
  22. public static byte[] GetAVPairSequenceBytes(KeyValuePairList<AVPairKey, byte[]> pairs)
  23. {
  24. int length = 0;
  25. foreach (KeyValuePair<AVPairKey, byte[]> pair in pairs)
  26. {
  27. length += 4 + pair.Value.Length;
  28. }
  29. length += 4;
  30. byte[] result = new byte[length];
  31. int offset = 0;
  32. foreach (KeyValuePair<AVPairKey, byte[]> pair in pairs)
  33. {
  34. WriteAVPair(result, ref offset, pair.Key, pair.Value);
  35. }
  36. LittleEndianWriter.WriteUInt16(result, ref offset, (ushort)AVPairKey.EOL);
  37. LittleEndianWriter.WriteUInt16(result, ref offset, 0);
  38. return result;
  39. }
  40. public static void WriteAVPair(byte[] buffer, ref int offset, AVPairKey key, byte[] value)
  41. {
  42. LittleEndianWriter.WriteUInt16(buffer, ref offset, (ushort)key);
  43. LittleEndianWriter.WriteUInt16(buffer, ref offset, (ushort)value.Length);
  44. ByteWriter.WriteBytes(buffer, ref offset, value);
  45. }
  46. public static KeyValuePairList<AVPairKey, byte[]> ReadAVPairSequence(byte[] buffer, int offset)
  47. {
  48. KeyValuePairList<AVPairKey, byte[]> result = new KeyValuePairList<AVPairKey,byte[]>();
  49. AVPairKey key = (AVPairKey)LittleEndianConverter.ToUInt16(buffer, offset);
  50. while (key != AVPairKey.EOL)
  51. {
  52. KeyValuePair<AVPairKey, byte[]> pair = ReadAVPair(buffer, ref offset);
  53. result.Add(pair);
  54. key = (AVPairKey)LittleEndianConverter.ToUInt16(buffer, offset);
  55. }
  56. return result;
  57. }
  58. public static KeyValuePair<AVPairKey, byte[]> ReadAVPair(byte[] buffer, ref int offset)
  59. {
  60. AVPairKey key = (AVPairKey)LittleEndianReader.ReadUInt16(buffer, ref offset);
  61. ushort length = LittleEndianReader.ReadUInt16(buffer, ref offset);
  62. byte[] value = ByteReader.ReadBytes(buffer, ref offset, length);
  63. return new KeyValuePair<AVPairKey, byte[]>(key, value);
  64. }
  65. }
  66. }