2 // System.Web.Configuration.MachineKeySection
5 // Chris Toshok (toshok@ximian.com)
7 // (c) Copyright 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.ComponentModel;
33 using System.Configuration;
34 using System.Security.Cryptography;
38 namespace System.Web.Configuration {
40 public sealed class MachineKeySection : ConfigurationSection
42 static ConfigurationProperty decryptionProp;
43 static ConfigurationProperty decryptionKeyProp;
44 static ConfigurationProperty validationProp;
45 static ConfigurationProperty validationKeyProp;
46 static ConfigurationPropertyCollection properties;
48 static MachineKeySection ()
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);
67 properties = new ConfigurationPropertyCollection ();
69 properties.Add (decryptionProp);
70 properties.Add (decryptionKeyProp);
71 properties.Add (validationProp);
72 properties.Add (validationKeyProp);
78 protected override void Reset (ConfigurationElement parentElement)
80 base.Reset (parentElement);
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; }
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); }
99 [TypeConverter (typeof (MachineKeyValidationConverter))]
100 [ConfigurationProperty ("validation", DefaultValue = "SHA1")]
101 public MachineKeyValidation Validation {
102 get { return (MachineKeyValidation) base [validationProp];}
103 set { base[validationProp] = value; }
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); }
114 protected override ConfigurationPropertyCollection Properties {
115 get { return properties; }
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;
125 static void AutoGenKeys ()
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);
134 static byte ToHexValue (char c, bool high)
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);
144 throw new ArgumentException ("Invalid hex character");
152 internal static byte [] GetBytes (string key, int len)
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));
161 static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
163 if (key == null || key.StartsWith ("AutoGenerate")){
164 //isolate = key.IndexOf ("IsolateApps") != 1;
166 return (decryption) ? autogenerated_decrypt : autogenerated;
171 int len = key.Length;
172 if (len < 40 || len > 128 || (len % 2) == 1)
173 throw new ArgumentException ("Invalid key length");
175 return GetBytes (key, len);
178 internal void SetDecryptionKey (string n)
180 decryption_key = MakeKey (n, true); //, out isolate_decryption);
181 decryption_key_192bits = new byte [24];
183 if (decryption_key.Length < 24)
184 count = decryption_key.Length;
185 Buffer.BlockCopy (decryption_key, 0, decryption_key_192bits, 0, count);
188 internal void SetValidationKey (string n)
190 validation_key = MakeKey (n, false); //, out isolate_validation);
193 internal byte [] ValidationKeyBytes {
195 if (validation_key == null)
196 SetValidationKey (ValidationKey);
197 return validation_key;
201 internal byte [] DecryptionKeyBytes {
203 if (decryption_key == null)
204 SetDecryptionKey (DecryptionKey);
205 return decryption_key;
209 internal byte [] DecryptionKey192Bits {
211 if (decryption_key_192bits == null)
212 SetDecryptionKey (DecryptionKey);
213 return decryption_key_192bits;