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 internal static class MachineKeySectionUtils
42 static byte [] autogenerated;
43 static byte [] autogenerated_decrypt;
44 static byte[] decryption_key;
45 static byte[] decryption_key_192bits;
46 static byte[] validation_key;
48 internal static void AutoGenKeys ()
54 if (autogenerated == null)
55 autogenerated = MachineKeyRegistryStorage.Retrieve (
56 MachineKeyRegistryStorage.KeyType.Validation);
57 if (autogenerated_decrypt == null)
58 autogenerated_decrypt = MachineKeyRegistryStorage.Retrieve (
59 MachineKeyRegistryStorage.KeyType.Encryption);
62 // Fall back to old method
63 RandomNumberGenerator rng = RandomNumberGenerator.Create ();
65 if (autogenerated == null) {
66 autogenerated = new byte [64];
67 rng.GetBytes (autogenerated);
70 if (autogenerated_decrypt == null) {
71 autogenerated_decrypt = new byte [64];
72 rng.GetBytes (autogenerated_decrypt);
77 static byte ToHexValue (char c, bool high)
80 if (c >= '0' && c <= '9')
82 else if (c >= 'a' && c <= 'f')
83 v = (byte) (c - 'a' + 10);
84 else if (c >= 'A' && c <= 'F')
85 v = (byte) (c - 'A' + 10);
87 throw new ArgumentException ("Invalid hex character");
95 internal static byte [] GetBytes (string key, int len)
97 byte [] result = new byte [len / 2];
98 for (int i = 0; i < len; i += 2)
99 result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
104 static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
106 if (key == null || key.StartsWith ("AutoGenerate")){
107 //isolate = key.IndexOf ("IsolateApps") != 1;
109 return (decryption) ? autogenerated_decrypt : autogenerated;
114 int len = key.Length;
115 if (len < 40 || len > 128 || (len % 2) == 1)
116 throw new ArgumentException ("Invalid key length");
118 return GetBytes (key, len);
121 internal static void SetDecryptionKey (string n)
123 decryption_key = MakeKey (n, true); //, out isolate_decryption);
124 decryption_key_192bits = new byte [24];
126 if (decryption_key.Length < 24)
127 count = decryption_key.Length;
128 Buffer.BlockCopy (decryption_key, 0, decryption_key_192bits, 0, count);
131 internal static void SetValidationKey (string n)
133 validation_key = MakeKey (n, false); //, out isolate_validation);
136 static MachineKeySection Config {
137 get { return WebConfigurationManager.GetSection ("system.web/machineKey") as MachineKeySection; }
140 internal static byte [] ValidationKeyBytes ()
142 return ValidationKeyBytes (Config);
145 internal static byte [] ValidationKeyBytes (MachineKeySection section)
148 throw new ArgumentNullException ("section");
150 if (validation_key == null)
151 SetValidationKey (section.ValidationKey);
152 return validation_key;
155 internal static byte [] DecryptionKeyBytes ()
157 return DecryptionKeyBytes (Config);
160 internal static byte [] DecryptionKeyBytes (MachineKeySection section)
163 throw new ArgumentNullException ("section");
165 if (decryption_key == null)
166 SetDecryptionKey (section.DecryptionKey);
167 return decryption_key;
170 internal static byte [] DecryptionKey192Bits ()
172 return DecryptionKey192Bits (Config);
175 internal static byte [] DecryptionKey192Bits (MachineKeySection section)
178 throw new ArgumentNullException ("section");
180 if (decryption_key_192bits == null)
181 SetDecryptionKey (section.DecryptionKey);
182 return decryption_key_192bits;