LosFormatter.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 enable_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.enable_mac = enableMac;
  59. if (enableMac)
  60. algo = new HMACSHA1 (macKeyModifier);
  61. }
  62. int 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. return data_length;
  75. }
  76. public object Deserialize (Stream stream)
  77. {
  78. if (stream == null)
  79. throw new ArgumentNullException ("stream");
  80. byte [] bytes = new byte [stream.Length >= 0 ? stream.Length : 2048];
  81. MemoryStream ms = null;
  82. if ((stream is MemoryStream) && stream.Position == 0) {
  83. // We save allocating a new stream and reading in this case.
  84. ms = (MemoryStream) stream;
  85. } else {
  86. ms = new MemoryStream ();
  87. int n;
  88. while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
  89. ms.Write (bytes, 0, n);
  90. }
  91. string b64 = Encoding.ASCII.GetString (ms.GetBuffer (), 0, (int) ms.Length);
  92. return Deserialize (b64);
  93. }
  94. public object Deserialize (TextReader input)
  95. {
  96. if (input == null)
  97. throw new ArgumentNullException ("input");
  98. return Deserialize (input.ReadToEnd ());
  99. }
  100. public object Deserialize (string input)
  101. {
  102. if (input == null)
  103. return null;
  104. byte [] buffer = Convert.FromBase64String (input);
  105. int length = buffer.Length;
  106. if (enable_mac) {
  107. length = ValidateInput (buffer, 0, length);
  108. }
  109. return osf.Deserialize (new MemoryStream (buffer, 0, length, false, false));
  110. }
  111. internal string SerializeToBase64 (object value)
  112. {
  113. MemoryStream ms = new MemoryStream ();
  114. osf.Serialize (ms, value);
  115. if (enable_mac && ms.Length > 0) {
  116. byte [] hash = algo.ComputeHash (ms.GetBuffer (), 0, (int) ms.Length);
  117. ms.Write (hash, 0, hash.Length);
  118. }
  119. return Convert.ToBase64String (ms.GetBuffer (), 0, (int) ms.Length);
  120. }
  121. public void Serialize (Stream stream, object value)
  122. {
  123. if (stream == null)
  124. throw new ArgumentNullException ("stream");
  125. string b64 = SerializeToBase64 (value);
  126. byte [] bytes = Encoding.ASCII.GetBytes (b64);
  127. stream.Write (bytes, 0, bytes.Length);
  128. }
  129. public void Serialize (TextWriter output, object value)
  130. {
  131. if (output == null)
  132. throw new ArgumentNullException ("output");
  133. output.Write (SerializeToBase64 (value));
  134. }
  135. }
  136. }