MessageBase.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Mono.Security.Protocol.Ntlm.MessageBase
  3. // abstract class for all NTLM messages
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. // Atsushi Enomoto <[email protected]>
  8. //
  9. // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  10. // Copyright (C) 2004, 2007 Novell, Inc (http://www.novell.com)
  11. //
  12. // References
  13. // a. NTLM Authentication Scheme for HTTP, Ronald Tschalär
  14. // http://www.innovation.ch/java/ntlm.html
  15. // b. The NTLM Authentication Protocol, Copyright © 2003 Eric Glass
  16. // http://davenport.sourceforge.net/ntlm.html
  17. //
  18. // Permission is hereby granted, free of charge, to any person obtaining
  19. // a copy of this software and associated documentation files (the
  20. // "Software"), to deal in the Software without restriction, including
  21. // without limitation the rights to use, copy, modify, merge, publish,
  22. // distribute, sublicense, and/or sell copies of the Software, and to
  23. // permit persons to whom the Software is furnished to do so, subject to
  24. // the following conditions:
  25. //
  26. // The above copyright notice and this permission notice shall be
  27. // included in all copies or substantial portions of the Software.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  33. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  34. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  35. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. //
  37. using System;
  38. using System.Globalization;
  39. namespace Mono.Security.Protocol.Ntlm {
  40. public abstract class MessageBase {
  41. static byte [] _current_os_version = GetOSVersion ();
  42. static byte [] GetOSVersion ()
  43. {
  44. Version v = Environment.OSVersion.Version;
  45. byte [] bytes = new byte [8];
  46. bytes [0] = (byte) v.Major;
  47. bytes [1] = (byte) v.Minor;
  48. bytes [2] = (byte) v.Build;
  49. bytes [3] = (byte) (v.Build >> 8);
  50. bytes [7] = 0xF;
  51. return bytes;
  52. }
  53. static private byte[] header = { 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00 };
  54. private int _type;
  55. private NtlmFlags _flags;
  56. private NtlmVersion _version;
  57. private byte [] _osversion = _current_os_version;
  58. protected MessageBase (int messageType) : this (messageType, NtlmVersion.Version1)
  59. {
  60. }
  61. protected MessageBase (int messageType, NtlmVersion version)
  62. {
  63. _type = messageType;
  64. _version = version;
  65. }
  66. public NtlmFlags Flags {
  67. get { return _flags; }
  68. set { _flags = value; }
  69. }
  70. public byte [] OSVersion {
  71. get { return (byte []) _osversion.Clone (); }
  72. set { _osversion = (byte []) value.Clone (); }
  73. }
  74. public int Type {
  75. get { return _type; }
  76. }
  77. public NtlmVersion Version {
  78. get { return _version; }
  79. }
  80. protected byte[] PrepareMessage (int messageSize)
  81. {
  82. byte[] message = new byte [messageSize];
  83. Buffer.BlockCopy (header, 0, message, 0, 8);
  84. message [ 8] = (byte) _type;
  85. message [ 9] = (byte)(_type >> 8);
  86. message [10] = (byte)(_type >> 16);
  87. message [11] = (byte)(_type >> 24);
  88. return message;
  89. }
  90. protected virtual void Decode (byte[] message)
  91. {
  92. if (message == null)
  93. throw new ArgumentNullException ("message");
  94. if (message.Length < 12) {
  95. string msg = Locale.GetText ("Minimum message length is 12 bytes.");
  96. throw new ArgumentOutOfRangeException ("message", message.Length, msg);
  97. }
  98. if (!CheckHeader (message)) {
  99. string msg = String.Format (Locale.GetText ("Invalid Type{0} message."), _type);
  100. throw new ArgumentException (msg, "message");
  101. }
  102. }
  103. protected bool CheckHeader (byte[] message)
  104. {
  105. for (int i=0; i < header.Length; i++) {
  106. if (message [i] != header [i])
  107. return false;
  108. }
  109. return (BitConverterLE.ToUInt32 (message, 8) == _type);
  110. }
  111. public abstract byte[] GetBytes ();
  112. internal byte [] CreateSubArray (byte [] source, int offset, int length)
  113. {
  114. byte [] ret = new byte [length];
  115. Array.Copy (source, offset, ret, 0, length);
  116. return ret;
  117. }
  118. }
  119. }