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                 protected override void Reset (ConfigurationElement parentElement)
78                 {
79                         base.Reset (parentElement);
80                 }
81
82                 [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
83                 [StringValidator (MinLength = 1)]
84                 [ConfigurationProperty ("decryption", DefaultValue = "Auto")]
85                 public string Decryption {
86                         get { return (string) base [decryptionProp];}
87                         set { base[decryptionProp] = value; }
88                 }
89
90                 [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
91                 [StringValidator (MinLength = 1)]
92                 [ConfigurationProperty ("decryptionKey", DefaultValue = "AutoGenerate,IsolateApps")]
93                 public string DecryptionKey {
94                         get { return (string) base [decryptionKeyProp];}
95                         set { base[decryptionKeyProp] = value;  SetDecryptionKey (value); }
96                 }
97
98                 [TypeConverter (typeof (MachineKeyValidationConverter))]
99                 [ConfigurationProperty ("validation", DefaultValue = "SHA1")]
100                 public MachineKeyValidation Validation {
101                         get { return (MachineKeyValidation) base [validationProp];}
102                         set { base[validationProp] = value; }
103                 }
104
105                 [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
106                 [StringValidator (MinLength = 1)]
107                 [ConfigurationProperty ("validationKey", DefaultValue = "AutoGenerate,IsolateApps")]
108                 public string ValidationKey {
109                         get { return (string) base [validationKeyProp];}
110                         set { base[validationKeyProp] = value;  SetValidationKey (value); }
111                 }
112
113                 protected override ConfigurationPropertyCollection Properties {
114                         get { return properties; }
115                 }
116
117 #region CompatabilityCode
118                 static byte [] autogenerated;
119                 static byte [] autogenerated_decrypt;
120                 byte[] decryption_key;
121                 byte[] decryption_key_192bits;
122                 byte[] validation_key;
123
124                 static void AutoGenKeys ()
125                 {
126                         autogenerated = new byte [64];
127                         RandomNumberGenerator rng = RandomNumberGenerator.Create ();
128                         rng.GetBytes (autogenerated);
129                         autogenerated_decrypt = new byte [64];
130                         rng.GetBytes (autogenerated_decrypt);
131                 }
132
133                 static byte ToHexValue (char c, bool high)
134                 {
135                         byte v;
136                         if (c >= '0' && c <= '9')
137                                 v = (byte) (c - '0');
138                         else if (c >= 'a' && c <= 'f')
139                                 v = (byte) (c - 'a' + 10);
140                         else if (c >= 'A' && c <= 'F')
141                                 v = (byte) (c - 'A' + 10);
142                         else
143                                 throw new ArgumentException ("Invalid hex character");
144
145                         if (high)
146                                 v <<= 4;
147
148                         return v;
149                 }
150                 
151                 internal static byte [] GetBytes (string key, int len)
152                 {
153                         byte [] result = new byte [len / 2];
154                         for (int i = 0; i < len; i += 2)
155                                 result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
156
157                         return result;
158                 }
159
160                 static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
161                 {
162                         if (key == null || key.StartsWith ("AutoGenerate")){
163                                 //isolate = key.IndexOf ("IsolateApps") != 1;
164
165                                 return (decryption) ? autogenerated_decrypt : autogenerated;
166                         }
167
168                         //isolate = false;
169
170                         int len = key.Length;
171                         if (len < 40 || len > 128 || (len % 2) == 1)
172                                 throw new ArgumentException ("Invalid key length");
173
174                         return GetBytes (key, len);
175                 }
176
177                 internal void SetDecryptionKey (string n)
178                 {
179                         decryption_key = MakeKey (n, true); //, out isolate_decryption);
180                         decryption_key_192bits = new byte [24];
181                         int count = 24;
182                         if (decryption_key.Length < 24)
183                                 count = decryption_key.Length;
184                         Buffer.BlockCopy (decryption_key, 0, decryption_key_192bits, 0, count);
185                 }
186
187                 internal void SetValidationKey (string n)
188                 {
189                         validation_key = MakeKey (n, false); //, out isolate_validation);
190                 }
191                 
192                 internal byte [] ValidationKeyBytes {
193                         get {
194                                 if (validation_key == null)
195                                         SetValidationKey (ValidationKey);
196                                 return validation_key;
197                         }
198                 }
199
200                 internal byte [] DecryptionKeyBytes {
201                         get {
202                                 if (decryption_key == null)
203                                         SetDecryptionKey (DecryptionKey);
204                                 return decryption_key;
205                         }
206                 }
207
208                 internal byte [] DecryptionKey192Bits {
209                         get {
210                                 if (decryption_key_192bits == null)
211                                         SetDecryptionKey (DecryptionKey);
212                                 return decryption_key_192bits;
213                         }
214                 }
215 #endregion
216
217         }
218 }
219
220 #endif