LosFormatter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // System.Web.UI.LosFormatter
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.IO;
  31. using System.Security.Cryptography;
  32. using System.Security.Permissions;
  33. using System.Text;
  34. namespace System.Web.UI {
  35. // CAS - no InheritanceDemand here as the class is sealed
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. public sealed class LosFormatter {
  38. ObjectStateFormatter osf = new ObjectStateFormatter ();
  39. bool disable_mac;
  40. HashAlgorithm algo;
  41. public LosFormatter ()
  42. {
  43. }
  44. #if NET_1_1
  45. public LosFormatter (bool enableMac, string macKeyModifier)
  46. : this (enableMac, Convert.FromBase64String (macKeyModifier))
  47. {
  48. }
  49. #endif
  50. [MonoTODO]
  51. #if NET_2_0
  52. public
  53. #else
  54. internal
  55. #endif
  56. LosFormatter (bool enableMac, byte[] macKeyModifier)
  57. {
  58. this.disable_mac = !enableMac;
  59. if (enableMac)
  60. algo = new HMACSHA1 (macKeyModifier);
  61. }
  62. void ValidateInput (byte [] data, int offset, int size)
  63. {
  64. int hash_size = algo.HashSize / 8;
  65. if (size != 0 && size < hash_size)
  66. throw new HttpException ("Unable to validate data.");
  67. int data_length = size - hash_size;
  68. MemoryStream data_stream = new MemoryStream (data, offset, data_length, false, false);
  69. byte [] hash = algo.ComputeHash (data_stream);
  70. for (int i = 0; i < hash_size; i++) {
  71. if (hash [i] != data [data_length + i])
  72. throw new HttpException ("Unable to validate data.");
  73. }
  74. }
  75. public object Deserialize (Stream stream)
  76. {
  77. if (disable_mac)
  78. return osf.Deserialize (stream);
  79. byte [] bytes = new byte [stream.Length >= 0 ? stream.Length : 2048];
  80. MemoryStream ms = null;
  81. if ((stream is MemoryStream) && stream.Position == 0) {
  82. // We save allocating a new stream and reading in this case.
  83. ms = (MemoryStream) stream;
  84. } else {
  85. ms = new MemoryStream ();
  86. int n;
  87. while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
  88. ms.Write (bytes, 0, n);
  89. }
  90. byte [] buffer = ms.GetBuffer ();
  91. int length = (int) ms.Length;
  92. ValidateInput (buffer, 0, length);
  93. return osf.Deserialize (new MemoryStream (buffer, 0, length, false, false));
  94. }
  95. public object Deserialize (TextReader input)
  96. {
  97. if (input == null)
  98. throw new ArgumentNullException ("input");
  99. return Deserialize (input.ReadToEnd ());
  100. }
  101. public object Deserialize (string input)
  102. {
  103. if (disable_mac)
  104. return osf.Deserialize (input);
  105. byte [] input_bytes = Convert.FromBase64String (input);
  106. ValidateInput (input_bytes, 0, input_bytes.Length);
  107. return osf.Deserialize (new MemoryStream (input_bytes, 0, input_bytes.Length, false, false));
  108. }
  109. void SerializeAndHash (MemoryStream ms, object value)
  110. {
  111. osf.Serialize (ms, value);
  112. if (ms.Length == 0)
  113. return;
  114. byte [] hash = algo.ComputeHash (ms.GetBuffer (), 0, (int) ms.Length);
  115. ms.Write (hash, 0, hash.Length);
  116. }
  117. public void Serialize (Stream stream, object value)
  118. {
  119. if (disable_mac) {
  120. osf.Serialize (stream, value);
  121. return;
  122. }
  123. MemoryStream ms = null;
  124. if ((stream is MemoryStream) && stream.Position == 0) {
  125. // We save allocating a new stream and reading in this case.
  126. ms = (MemoryStream) stream;
  127. } else {
  128. ms = new MemoryStream ();
  129. }
  130. SerializeAndHash (ms, value);
  131. if (ms != stream)
  132. ms.WriteTo (stream);
  133. }
  134. public void Serialize (TextWriter output, object value)
  135. {
  136. if (output == null)
  137. throw new ArgumentNullException ("output");
  138. if (disable_mac) {
  139. output.Write (osf.Serialize (value));
  140. return;
  141. }
  142. MemoryStream ms = new MemoryStream ();
  143. SerializeAndHash (ms, value);
  144. output.Write (Convert.ToBase64String (ms.GetBuffer (), 0, (int) ms.Length));
  145. }
  146. }
  147. }