NegotiateHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 SMBLibrary.Authentication.GSSAPI;
  10. using SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. /// <summary>
  15. /// Negotiate helper
  16. /// </summary>
  17. internal class NegotiateHelper
  18. {
  19. public const string SMB2002Dialect = "SMB 2.002";
  20. public const string SMB2xxxDialect = "SMB 2.???";
  21. // Special case - SMB2 client initially connecting using SMB1
  22. internal static SMB2Command GetNegotiateResponse(List<string> smb2Dialects, GSSProvider securityProvider, ConnectionState state, Guid serverGuid, DateTime serverStartTime)
  23. {
  24. NegotiateResponse response = new NegotiateResponse();
  25. response.Header.Credits = 1;
  26. if (smb2Dialects.Contains(SMB2xxxDialect))
  27. {
  28. response.DialectRevision = SMB2Dialect.SMB2xx;
  29. }
  30. else if (smb2Dialects.Contains(SMB2002Dialect))
  31. {
  32. state.Dialect = SMBDialect.SMB202;
  33. response.DialectRevision = SMB2Dialect.SMB202;
  34. }
  35. else
  36. {
  37. throw new ArgumentException("SMB2 dialect is not present");
  38. }
  39. response.SecurityMode = SecurityMode.SigningEnabled;
  40. response.ServerGuid = serverGuid;
  41. response.MaxTransactSize = 65536;
  42. response.MaxReadSize = 65536;
  43. response.MaxWriteSize = 65536;
  44. response.SystemTime = DateTime.Now;
  45. response.ServerStartTime = serverStartTime;
  46. response.SecurityBuffer = securityProvider.GetSPNEGOTokenInitBytes();
  47. return response;
  48. }
  49. internal static SMB2Command GetNegotiateResponse(NegotiateRequest request, GSSProvider securityProvider, ConnectionState state, Guid serverGuid, DateTime serverStartTime)
  50. {
  51. NegotiateResponse response = new NegotiateResponse();
  52. if (request.Dialects.Contains(SMB2Dialect.SMB210))
  53. {
  54. state.Dialect = SMBDialect.SMB210;
  55. response.DialectRevision = SMB2Dialect.SMB210;
  56. }
  57. else if (request.Dialects.Contains(SMB2Dialect.SMB202))
  58. {
  59. state.Dialect = SMBDialect.SMB202;
  60. response.DialectRevision = SMB2Dialect.SMB202;
  61. }
  62. else
  63. {
  64. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  65. }
  66. response.SecurityMode = SecurityMode.SigningEnabled;
  67. response.ServerGuid = serverGuid;
  68. response.MaxTransactSize = 65536;
  69. response.MaxReadSize = 65536;
  70. response.MaxWriteSize = 65536;
  71. response.SystemTime = DateTime.Now;
  72. response.ServerStartTime = serverStartTime;
  73. response.SecurityBuffer = securityProvider.GetSPNEGOTokenInitBytes();
  74. return response;
  75. }
  76. internal static List<string> FindSMB2Dialects(SMBLibrary.SMB1.SMB1Message message)
  77. {
  78. if (message.Commands.Count > 0 && message.Commands[0] is SMBLibrary.SMB1.NegotiateRequest)
  79. {
  80. SMBLibrary.SMB1.NegotiateRequest request = (SMBLibrary.SMB1.NegotiateRequest)message.Commands[0];
  81. return FindSMB2Dialects(request);
  82. }
  83. return new List<string>();
  84. }
  85. internal static List<string> FindSMB2Dialects(SMBLibrary.SMB1.NegotiateRequest request)
  86. {
  87. List<string> result = new List<string>();
  88. if (request.Dialects.Contains(SMB2002Dialect))
  89. {
  90. result.Add(SMB2002Dialect);
  91. }
  92. if (request.Dialects.Contains(SMB2xxxDialect))
  93. {
  94. result.Add(SMB2xxxDialect);
  95. }
  96. return result;
  97. }
  98. }
  99. }