Normalization of cryptographic uses in asp.net
[mono.git] / mcs / class / System.Web / System.Web.UI / LosFormatter.cs
1 //
2 // System.Web.UI.LosFormatter
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2003 Ben Maurer
9 // Copyright (C) 2005-2010 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
31 using System.Configuration;
32 using System.IO;
33 using System.Security.Permissions;
34 using System.Text;
35 using System.Web.Configuration;
36
37 namespace System.Web.UI {
38
39         // CAS - no InheritanceDemand here as the class is sealed
40         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         public sealed class LosFormatter
42         {
43                 ObjectStateFormatter osf;
44                 
45                 public LosFormatter ()
46                 {
47                         osf = new ObjectStateFormatter ();
48                 }
49
50                 public LosFormatter (bool enableMac, string macKeyModifier)
51                 {
52                         osf = new ObjectStateFormatter ();
53                         if (enableMac && !String.IsNullOrEmpty (macKeyModifier)) {
54                                 SetMacKey (Convert.FromBase64String (macKeyModifier));
55                         }
56                 }
57
58                 public LosFormatter (bool enableMac, byte[] macKeyModifier)
59                 {
60                         osf = new ObjectStateFormatter ();
61                         if (enableMac && (macKeyModifier != null)) {
62                                 SetMacKey (macKeyModifier);
63                         }
64                 }
65
66                 private void SetMacKey (byte[] macKeyModifier)
67                 {
68                         try {
69                                 osf.Section.ValidationKey = MachineKeySectionUtils.GetHexString (macKeyModifier);
70                         }
71                         catch (ArgumentException) {
72                         }
73                         catch (ConfigurationErrorsException) {
74                                 // bad key (e.g. size), default key will be used
75                         }
76                 }
77
78                 public object Deserialize (Stream stream)
79                 {
80                         if (stream == null)
81                                 throw new ArgumentNullException ("stream");
82 #if NET_4_0
83                         using (StreamReader sr = new StreamReader (stream)) {
84                                 return Deserialize (sr.ReadToEnd ());
85                         }
86 #else
87                         long streamLength = -1;
88                         if (stream.CanSeek)
89                                 streamLength = stream.Length;
90                         MemoryStream ms = null;
91                         if (streamLength  != -1 && (stream is MemoryStream) && stream.Position == 0) {
92                                 // We save allocating a new stream and reading in this case.
93                                 ms = (MemoryStream) stream;
94                         } else {
95                                 byte [] bytes = new byte [streamLength >= 0 ? streamLength : 2048];
96                                 ms = new MemoryStream ();
97                                 int n;
98                                 while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
99                                         ms.Write (bytes, 0, n);
100                                 streamLength = ms.Length;
101                         }
102                         string b64 = Encoding.ASCII.GetString (ms.GetBuffer (),
103                                 0, (int) streamLength);
104                         return Deserialize (b64);
105 #endif
106                 }
107
108                 public object Deserialize (TextReader input)
109                 {
110                         if (input == null)
111                                 throw new ArgumentNullException ("input");
112
113                         return Deserialize (input.ReadToEnd ());
114                 }
115
116                 public object Deserialize (string input)
117                 {
118                         if (input == null)
119                                 return null;
120
121                         return osf.Deserialize (input);
122                 }
123
124                 internal string SerializeToBase64 (object value)
125                 {
126                         return osf.Serialize (value);
127                 }
128
129                 public void Serialize (Stream stream, object value)
130                 {
131                         if (stream == null)
132                                 throw new ArgumentNullException ("stream");
133 #if NET_4_0
134                         if (!stream.CanSeek)
135                                 throw new NotSupportedException ();
136 #endif
137                         string b64 = SerializeToBase64 (value);
138                         byte [] bytes = Encoding.ASCII.GetBytes (b64);
139                         stream.Write (bytes, 0, bytes.Length);
140                 }
141
142                 public void Serialize (TextWriter output, object value)
143                 {
144                         if (output == null)
145                                 throw new ArgumentNullException ("output");
146
147                         output.Write (SerializeToBase64 (value));
148                 }       
149         }
150 }
151