New test.
[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;  SetDecryptionKey (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;  SetValidationKey (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                 byte[] decryption_key;
122                 byte[] decryption_key_192bits;
123                 byte[] validation_key;
124
125                 static void AutoGenKeys ()
126                 {
127                         autogenerated = new byte [64];
128                         RandomNumberGenerator rng = RandomNumberGenerator.Create ();
129                         rng.GetBytes (autogenerated);
130                         autogenerated_decrypt = new byte [64];
131                         rng.GetBytes (autogenerated_decrypt);
132                 }
133
134                 static byte ToHexValue (char c, bool high)
135                 {
136                         byte v;
137                         if (c >= '0' && c <= '9')
138                                 v = (byte) (c - '0');
139                         else if (c >= 'a' && c <= 'f')
140                                 v = (byte) (c - 'a' + 10);
141                         else if (c >= 'A' && c <= 'F')
142                                 v = (byte) (c - 'A' + 10);
143                         else
144                                 throw new ArgumentException ("Invalid hex character");
145
146                         if (high)
147                                 v <<= 4;
148
149                         return v;
150                 }
151                 
152                 internal static byte [] GetBytes (string key, int len)
153                 {
154                         byte [] result = new byte [len / 2];
155                         for (int i = 0; i < len; i += 2)
156                                 result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
157
158                         return result;
159                 }
160
161                 static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
162                 {
163                         if (key == null || key.StartsWith ("AutoGenerate")){
164                                 //isolate = key.IndexOf ("IsolateApps") != 1;
165
166                                 return (decryption) ? autogenerated_decrypt : autogenerated;
167                         }
168
169                         //isolate = false;
170
171                         int len = key.Length;
172                         if (len < 40 || len > 128 || (len % 2) == 1)
173                                 throw new ArgumentException ("Invalid key length");
174
175                         return GetBytes (key, len);
176                 }
177
178                 internal void SetDecryptionKey (string n)
179                 {
180                         decryption_key = MakeKey (n, true); //, out isolate_decryption);
181                         decryption_key_192bits = new byte [24];
182                         int count = 24;
183                         if (decryption_key.Length < 24)
184                                 count = decryption_key.Length;
185                         Buffer.BlockCopy (decryption_key, 0, decryption_key_192bits, 0, count);
186                 }
187
188                 internal void SetValidationKey (string n)
189                 {
190                         validation_key = MakeKey (n, false); //, out isolate_validation);
191                 }
192                 
193                 internal byte [] ValidationKeyBytes {
194                         get {
195                                 if (validation_key == null)
196                                         SetValidationKey (ValidationKey);
197                                 return validation_key;
198                         }
199                 }
200
201                 internal byte [] DecryptionKeyBytes {
202                         get {
203                                 if (decryption_key == null)
204                                         SetDecryptionKey (DecryptionKey);
205                                 return decryption_key;
206                         }
207                 }
208
209                 internal byte [] DecryptionKey192Bits {
210                         get {
211                                 if (decryption_key_192bits == null)
212                                         SetDecryptionKey (DecryptionKey);
213                                 return decryption_key_192bits;
214                         }
215                 }
216 #endregion
217
218         }
219 }
220
221 #endif