Add this for backwards compatibility
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / MachineKeySection.cs
1 //
2 // System.Web.Configuration.MachineKeySection
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // (c) Copyright 2005 Novell, Inc (http://www.novell.com)
8 //
9
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;
32 using System.ComponentModel;
33 using System.Configuration;
34 using System.Security.Cryptography;
35
36 #if NET_2_0
37
38 namespace System.Web.Configuration {
39
40         public sealed class MachineKeySection : ConfigurationSection
41         {
42                 static ConfigurationProperty decryptionProp;
43                 static ConfigurationProperty decryptionKeyProp;
44                 static ConfigurationProperty validationProp;
45                 static ConfigurationProperty validationKeyProp;
46                 static ConfigurationPropertyCollection properties;
47
48                 static MachineKeySection ()
49                 {
50                         decryptionProp = new ConfigurationProperty ("decryption", typeof (string), "Auto",
51                                                                     PropertyHelper.WhiteSpaceTrimStringConverter,
52                                                                     PropertyHelper.NonEmptyStringValidator,
53                                                                     ConfigurationPropertyOptions.None);
54                         decryptionKeyProp = new ConfigurationProperty ("decryptionKey", typeof (string), "AutoGenerate,IsolateApps",
55                                                                        PropertyHelper.WhiteSpaceTrimStringConverter,
56                                                                        PropertyHelper.NonEmptyStringValidator,
57                                                                        ConfigurationPropertyOptions.None);
58                         validationProp = new ConfigurationProperty ("validation", typeof (MachineKeyValidation), MachineKeyValidation.SHA1,
59                                                                     new MachineKeyValidationConverter (),
60                                                                     PropertyHelper.DefaultValidator,
61                                                                     ConfigurationPropertyOptions.None);
62                         validationKeyProp = new ConfigurationProperty ("validationKey", typeof (string), "AutoGenerate,IsolateApps",
63                                                                        PropertyHelper.WhiteSpaceTrimStringConverter,
64                                                                        PropertyHelper.NonEmptyStringValidator,
65                                                                        ConfigurationPropertyOptions.None);
66
67                         properties = new ConfigurationPropertyCollection ();
68
69                         properties.Add (decryptionProp);
70                         properties.Add (decryptionKeyProp);
71                         properties.Add (validationProp);
72                         properties.Add (validationKeyProp);
73
74                         AutoGenKeys ();
75                 }
76
77                 [MonoTODO]
78                 protected override void Reset (ConfigurationElement parentElement)
79                 {
80                         base.Reset (parentElement);
81                 }
82
83                 [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
84                 [StringValidator (MinLength = 1)]
85                 [ConfigurationProperty ("decryption", DefaultValue = "Auto")]
86                 public string Decryption {
87                         get { return (string) base [decryptionProp];}
88                         set { base[decryptionProp] = value; }
89                 }
90
91                 [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
92                 [StringValidator (MinLength = 1)]
93                 [ConfigurationProperty ("decryptionKey", DefaultValue = "AutoGenerate,IsolateApps")]
94                 public string DecryptionKey {
95                         get { return (string) base [decryptionKeyProp];}
96                         set { base[decryptionKeyProp] = value; }
97                 }
98
99                 [TypeConverter (typeof (MachineKeyValidationConverter))]
100                 [ConfigurationProperty ("validation", DefaultValue = "SHA1")]
101                 public MachineKeyValidation Validation {
102                         get { return (MachineKeyValidation) base [validationProp];}
103                         set { base[validationProp] = value; }
104                 }
105
106                 [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
107                 [StringValidator (MinLength = 1)]
108                 [ConfigurationProperty ("validationKey", DefaultValue = "AutoGenerate,IsolateApps")]
109                 public string ValidationKey {
110                         get { return (string) base [validationKeyProp];}
111                         set { base[validationKeyProp] = value; }
112                 }
113
114                 protected override ConfigurationPropertyCollection Properties {
115                         get { return properties; }
116                 }
117
118 #region CompatabilityCode
119                 static byte [] autogenerated;
120                 static byte [] autogenerated_decrypt;
121
122                 static void AutoGenKeys ()
123                 {
124                         autogenerated = new byte [64];
125                         RandomNumberGenerator rng = RandomNumberGenerator.Create ();
126                         rng.GetBytes (autogenerated);
127                         autogenerated_decrypt = new byte [64];
128                         rng.GetBytes (autogenerated_decrypt);
129                 }
130
131                 static byte ToHexValue (char c, bool high)
132                 {
133                         byte v;
134                         if (c >= '0' && c <= '9')
135                                 v = (byte) (c - '0');
136                         else if (c >= 'a' && c <= 'f')
137                                 v = (byte) (c - 'a' + 10);
138                         else if (c >= 'A' && c <= 'F')
139                                 v = (byte) (c - 'A' + 10);
140                         else
141                                 throw new ArgumentException ("Invalid hex character");
142
143                         if (high)
144                                 v <<= 4;
145
146                         return v;
147                 }
148                 
149                 internal static byte [] GetBytes (string key, int len)
150                 {
151                         byte [] result = new byte [len / 2];
152                         for (int i = 0; i < len; i += 2)
153                                 result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
154
155                         return result;
156                 }
157
158                 static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
159                 {
160                         if (key == null || key.StartsWith ("AutoGenerate")){
161                                 //isolate = key.IndexOf ("IsolateApps") != 1;
162
163                                 return (decryption) ? autogenerated_decrypt : autogenerated;
164                         }
165
166                         //isolate = false;
167
168                         int len = key.Length;
169                         if (len < 40 || len > 128 || (len % 2) == 1)
170                                 throw new ArgumentException ("Invalid key length");
171
172                         return GetBytes (key, len);
173                 }
174                 
175                 internal byte [] ValidationKeyBytes {
176                         get { return MakeKey (ValidationKey, false); }
177                 }
178 #endregion
179
180         }
181 }
182
183 #endif