2 // System.Web.Configuration.MachineKeyConfig
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // Copyright (c) 2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Configuration;
36 using System.Security.Cryptography;
38 namespace System.Web.Configuration
40 class MachineKeyConfig
42 byte [] validation_key;
43 bool isolate_validation;
44 byte [] decryption_key;
45 byte [] decryption_key_192bits;
46 bool isolate_decryption; // For us, this is always true by now.
47 MachineKeyValidation validation_type;
49 static byte [] autogenerated;
50 static byte [] autogenerated_decrypt;
52 static MachineKeyConfig ()
54 autogenerated = new byte [64];
55 RandomNumberGenerator rng = RandomNumberGenerator.Create ();
56 rng.GetBytes (autogenerated);
57 autogenerated_decrypt = new byte [64];
58 rng.GetBytes (autogenerated_decrypt);
61 internal MachineKeyConfig (object parent)
63 if (parent is MachineKeyConfig) {
64 MachineKeyConfig p = (MachineKeyConfig) parent;
65 validation_key = p.validation_key;
66 decryption_key = p.decryption_key;
67 validation_type = p.validation_type;
71 static byte ToHexValue (char c, bool high)
74 if (c >= '0' && c <= '9')
76 else if (c >= 'a' && c <= 'f')
77 v = (byte) (c - 'a' + 10);
78 else if (c >= 'A' && c <= 'F')
79 v = (byte) (c - 'A' + 10);
81 throw new ArgumentException ("Invalid hex character");
89 internal static byte [] GetBytes (string key, int len)
91 byte [] result = new byte [len / 2];
92 for (int i = 0; i < len; i += 2)
93 result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
98 static byte [] MakeKey (string key, bool decryption, out bool isolate)
100 if (key == null || key.StartsWith ("AutoGenerate")){
101 isolate = key.IndexOf ("IsolateApps") != 1;
103 return (decryption) ? autogenerated_decrypt : autogenerated;
108 int len = key.Length;
109 if (len < 40 || len > 128 || (len % 2) == 1)
110 throw new ArgumentException ("Invalid key length");
112 return GetBytes (key, len);
115 internal void SetValidationKey (string n)
117 validation_key = MakeKey (n, false, out isolate_validation);
120 internal byte [] ValidationKey {
121 get { return validation_key; }
124 internal void SetDecryptionKey (string n)
126 decryption_key = MakeKey (n, true, out isolate_decryption);
127 decryption_key_192bits = new byte [24];
129 if (decryption_key.Length < 24)
130 count = decryption_key.Length;
131 Buffer.BlockCopy (decryption_key, 0, decryption_key_192bits, 0, count);
134 internal byte [] DecryptionKey {
135 get { return decryption_key; }
138 internal byte [] DecryptionKey192Bits {
139 get { return decryption_key_192bits; }
142 internal MachineKeyValidation ValidationType {
144 return validation_type;
147 validation_type = value;