SimpleProtectedNegotiationTokenInit2.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Copyright (C) 2018 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.IO;
  10. using Utilities;
  11. namespace SMBLibrary.Authentication.GSSAPI
  12. {
  13. /// <summary>
  14. /// [MS-SPNG] - NegTokenInit2
  15. /// </summary>
  16. public class SimpleProtectedNegotiationTokenInit2 : SimpleProtectedNegotiationTokenInit
  17. {
  18. public const byte NegHintsTag = 0xA3;
  19. new public const byte MechanismListMICTag = 0xA4;
  20. public const byte HintNameTag = 0xA0;
  21. public const byte HintAddressTag = 0xA1;
  22. public string HintName;
  23. public byte[] HintAddress;
  24. public SimpleProtectedNegotiationTokenInit2()
  25. {
  26. HintName = "not_defined_in_RFC4178@please_ignore";
  27. }
  28. /// <param name="offset">The offset following the NegTokenInit2 tag</param>
  29. /// <exception cref="System.IO.InvalidDataException"></exception>
  30. public SimpleProtectedNegotiationTokenInit2(byte[] buffer, int offset)
  31. {
  32. int constructionLength = DerEncodingHelper.ReadLength(buffer, ref offset);
  33. byte tag = ByteReader.ReadByte(buffer, ref offset);
  34. if (tag != (byte)DerEncodingTag.Sequence)
  35. {
  36. throw new InvalidDataException();
  37. }
  38. int sequenceLength = DerEncodingHelper.ReadLength(buffer, ref offset);
  39. int sequenceEndOffset = offset + sequenceLength;
  40. while (offset < sequenceEndOffset)
  41. {
  42. tag = ByteReader.ReadByte(buffer, ref offset);
  43. if (tag == MechanismTypeListTag)
  44. {
  45. MechanismTypeList = ReadMechanismTypeList(buffer, ref offset);
  46. }
  47. else if (tag == RequiredFlagsTag)
  48. {
  49. throw new NotImplementedException("negTokenInit.ReqFlags is not implemented");
  50. }
  51. else if (tag == MechanismTokenTag)
  52. {
  53. MechanismToken = ReadMechanismToken(buffer, ref offset);
  54. }
  55. else if (tag == NegHintsTag)
  56. {
  57. HintName = ReadHints(buffer, ref offset, out HintAddress);
  58. }
  59. else if (tag == MechanismListMICTag)
  60. {
  61. MechanismListMIC = ReadMechanismListMIC(buffer, ref offset);
  62. }
  63. else
  64. {
  65. throw new InvalidDataException("Invalid negTokenInit structure");
  66. }
  67. }
  68. }
  69. public override byte[] GetBytes()
  70. {
  71. int sequenceLength = GetTokenFieldsLength();
  72. int sequenceLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(sequenceLength);
  73. int constructionLength = 1 + sequenceLengthFieldSize + sequenceLength;
  74. int constructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(constructionLength);
  75. int bufferSize = 1 + constructionLengthFieldSize + 1 + sequenceLengthFieldSize + sequenceLength;
  76. byte[] buffer = new byte[bufferSize];
  77. int offset = 0;
  78. ByteWriter.WriteByte(buffer, ref offset, NegTokenInitTag);
  79. DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
  80. ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.Sequence);
  81. DerEncodingHelper.WriteLength(buffer, ref offset, sequenceLength);
  82. if (MechanismTypeList != null)
  83. {
  84. WriteMechanismTypeList(buffer, ref offset, MechanismTypeList);
  85. }
  86. if (MechanismToken != null)
  87. {
  88. WriteMechanismToken(buffer, ref offset, MechanismToken);
  89. }
  90. if (HintName != null || HintAddress != null)
  91. {
  92. WriteHints(buffer, ref offset, HintName, HintAddress);
  93. }
  94. if (MechanismListMIC != null)
  95. {
  96. WriteMechanismListMIC(buffer, ref offset, MechanismListMIC);
  97. }
  98. return buffer;
  99. }
  100. protected override int GetTokenFieldsLength()
  101. {
  102. int result = base.GetTokenFieldsLength();;
  103. if (HintName != null || HintAddress != null)
  104. {
  105. int hintsSequenceLength = GetHintsSequenceLength(HintName, HintAddress);
  106. int hintsSequenceLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(hintsSequenceLength);
  107. int hintsSequenceConstructionLength = 1 + hintsSequenceLengthFieldSize + hintsSequenceLength;
  108. int hintsSequenceConstructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(hintsSequenceConstructionLength);
  109. int entryLength = 1 + hintsSequenceConstructionLengthFieldSize + 1 + hintsSequenceLengthFieldSize + hintsSequenceLength;
  110. result += entryLength;
  111. }
  112. return result;
  113. }
  114. protected static string ReadHints(byte[] buffer, ref int offset, out byte[] hintAddress)
  115. {
  116. string hintName = null;
  117. hintAddress = null;
  118. int constructionLength = DerEncodingHelper.ReadLength(buffer, ref offset);
  119. byte tag = ByteReader.ReadByte(buffer, ref offset);
  120. if (tag != (byte)DerEncodingTag.Sequence)
  121. {
  122. throw new InvalidDataException();
  123. }
  124. int sequenceLength = DerEncodingHelper.ReadLength(buffer, ref offset);
  125. int sequenceEndOffset = offset + sequenceLength;
  126. while (offset < sequenceEndOffset)
  127. {
  128. tag = ByteReader.ReadByte(buffer, ref offset);
  129. if (tag == HintNameTag)
  130. {
  131. hintName = ReadHintName(buffer, ref offset);
  132. }
  133. else if (tag == HintAddressTag)
  134. {
  135. hintAddress = ReadHintAddress(buffer, ref offset);
  136. }
  137. else
  138. {
  139. throw new InvalidDataException();
  140. }
  141. }
  142. return hintName;
  143. }
  144. protected static string ReadHintName(byte[] buffer, ref int offset)
  145. {
  146. int constructionLength = DerEncodingHelper.ReadLength(buffer, ref offset);
  147. byte tag = ByteReader.ReadByte(buffer, ref offset);
  148. if (tag != (byte)DerEncodingTag.GeneralString)
  149. {
  150. throw new InvalidDataException();
  151. }
  152. int hintLength = DerEncodingHelper.ReadLength(buffer, ref offset);
  153. byte[] hintNameBytes = ByteReader.ReadBytes(buffer, ref offset, hintLength);
  154. return DerEncodingHelper.DecodeGeneralString(hintNameBytes);
  155. }
  156. protected static byte[] ReadHintAddress(byte[] buffer, ref int offset)
  157. {
  158. int constructionLength = DerEncodingHelper.ReadLength(buffer, ref offset);
  159. byte tag = ByteReader.ReadByte(buffer, ref offset);
  160. if (tag != (byte)DerEncodingTag.ByteArray)
  161. {
  162. throw new InvalidDataException();
  163. }
  164. int hintLength = DerEncodingHelper.ReadLength(buffer, ref offset);
  165. return ByteReader.ReadBytes(buffer, ref offset, hintLength);
  166. }
  167. protected static int GetHintsSequenceLength(string hintName, byte[] hintAddress)
  168. {
  169. int sequenceLength = 0;
  170. if (hintName != null)
  171. {
  172. byte[] hintNameBytes = DerEncodingHelper.EncodeGeneralString(hintName);
  173. int lengthFieldSize = DerEncodingHelper.GetLengthFieldSize(hintNameBytes.Length);
  174. int constructionLength = 1 + lengthFieldSize + hintNameBytes.Length;
  175. int constructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(constructionLength);
  176. int entryLength = 1 + constructionLengthFieldSize + 1 + lengthFieldSize + hintNameBytes.Length;
  177. sequenceLength += entryLength;
  178. }
  179. if (hintAddress != null)
  180. {
  181. int lengthFieldSize = DerEncodingHelper.GetLengthFieldSize(hintAddress.Length);
  182. int constructionLength = 1 + lengthFieldSize + hintAddress.Length;
  183. int constructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(constructionLength);
  184. int entryLength = 1 + constructionLengthFieldSize + 1 + lengthFieldSize + hintAddress.Length;
  185. sequenceLength += entryLength;
  186. }
  187. return sequenceLength;
  188. }
  189. private static void WriteHints(byte[] buffer, ref int offset, string hintName, byte[] hintAddress)
  190. {
  191. int sequenceLength = GetHintsSequenceLength(hintName, hintAddress);
  192. int sequenceLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(sequenceLength);
  193. int constructionLength = 1 + sequenceLengthFieldSize + sequenceLength;
  194. ByteWriter.WriteByte(buffer, ref offset, NegHintsTag);
  195. DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
  196. ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.Sequence);
  197. DerEncodingHelper.WriteLength(buffer, ref offset, sequenceLength);
  198. if (hintName != null)
  199. {
  200. WriteHintName(buffer, ref offset, hintName);
  201. }
  202. if (hintAddress != null)
  203. {
  204. WriteHintAddress(buffer, ref offset, hintAddress);
  205. }
  206. }
  207. private static void WriteHintName(byte[] buffer, ref int offset, string hintName)
  208. {
  209. byte[] hintNameBytes = DerEncodingHelper.EncodeGeneralString(hintName);
  210. int constructionLength = 1 + DerEncodingHelper.GetLengthFieldSize(hintNameBytes.Length) + hintNameBytes.Length;
  211. ByteWriter.WriteByte(buffer, ref offset, HintNameTag);
  212. DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
  213. ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.GeneralString);
  214. DerEncodingHelper.WriteLength(buffer, ref offset, hintNameBytes.Length);
  215. ByteWriter.WriteBytes(buffer, ref offset, hintNameBytes);
  216. }
  217. private static void WriteHintAddress(byte[] buffer, ref int offset, byte[] hintAddress)
  218. {
  219. int constructionLength = 1 + DerEncodingHelper.GetLengthFieldSize(hintAddress.Length) + hintAddress.Length;
  220. ByteWriter.WriteByte(buffer, ref offset, HintAddressTag);
  221. DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
  222. ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ByteArray);
  223. DerEncodingHelper.WriteLength(buffer, ref offset, hintAddress.Length);
  224. ByteWriter.WriteBytes(buffer, ref offset, hintAddress);
  225. }
  226. new protected static void WriteMechanismListMIC(byte[] buffer, ref int offset, byte[] mechanismListMIC)
  227. {
  228. int mechanismListMICLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(mechanismListMIC.Length);
  229. ByteWriter.WriteByte(buffer, ref offset, MechanismListMICTag);
  230. DerEncodingHelper.WriteLength(buffer, ref offset, 1 + mechanismListMICLengthFieldSize + mechanismListMIC.Length);
  231. ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ByteArray);
  232. DerEncodingHelper.WriteLength(buffer, ref offset, mechanismListMIC.Length);
  233. ByteWriter.WriteBytes(buffer, ref offset, mechanismListMIC);
  234. }
  235. }
  236. }