2007-11-06 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / MachineKeySectionUtils.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         internal static class MachineKeySectionUtils
41         {
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;
47
48                 internal static void AutoGenKeys ()
49                 {
50 #if TARGET_J2EE
51                         {
52 #else
53                         try {
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);
60                         } catch (Exception) {
61 #endif
62                                 // Fall back to old method
63                                 RandomNumberGenerator rng = RandomNumberGenerator.Create ();
64                                 
65                                 if (autogenerated == null) {
66                                         autogenerated = new byte [64];
67                                         rng.GetBytes (autogenerated);
68                                 }
69
70                                 if (autogenerated_decrypt == null) {
71                                         autogenerated_decrypt = new byte [64];
72                                         rng.GetBytes (autogenerated_decrypt);
73                                 }
74                         }
75                 }
76
77                 static byte ToHexValue (char c, bool high)
78                 {
79                         byte v;
80                         if (c >= '0' && c <= '9')
81                                 v = (byte) (c - '0');
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);
86                         else
87                                 throw new ArgumentException ("Invalid hex character");
88
89                         if (high)
90                                 v <<= 4;
91
92                         return v;
93                 }
94                 
95                 internal static byte [] GetBytes (string key, int len)
96                 {
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));
100
101                         return result;
102                 }
103
104                 static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
105                 {
106                         if (key == null || key.StartsWith ("AutoGenerate")){
107                                 //isolate = key.IndexOf ("IsolateApps") != 1;
108                                 AutoGenKeys ();
109                                 return (decryption) ? autogenerated_decrypt : autogenerated;
110                         }
111
112                         //isolate = false;
113
114                         int len = key.Length;
115                         if (len < 40 || len > 128 || (len % 2) == 1)
116                                 throw new ArgumentException ("Invalid key length");
117
118                         return GetBytes (key, len);
119                 }
120
121                 internal static void SetDecryptionKey (string n)
122                 {
123                         decryption_key = MakeKey (n, true); //, out isolate_decryption);
124                         decryption_key_192bits = new byte [24];
125                         int count = 24;
126                         if (decryption_key.Length < 24)
127                                 count = decryption_key.Length;
128                         Buffer.BlockCopy (decryption_key, 0, decryption_key_192bits, 0, count);
129                 }
130
131                 internal static void SetValidationKey (string n)
132                 {
133                         validation_key = MakeKey (n, false); //, out isolate_validation);
134                 }
135
136                 static MachineKeySection Config {
137                         get { return WebConfigurationManager.GetSection ("system.web/machineKey") as MachineKeySection; }
138                 }               
139
140                 internal static byte [] ValidationKeyBytes ()
141                 {
142                         return ValidationKeyBytes (Config);
143                 }
144                 
145                 internal static byte [] ValidationKeyBytes (MachineKeySection section)
146                 {
147                         if (section == null)
148                                 throw new ArgumentNullException ("section");
149                         
150                         if (validation_key == null)
151                                 SetValidationKey (section.ValidationKey);
152                         return validation_key;
153                 }
154
155                 internal static byte [] DecryptionKeyBytes ()
156                 {
157                         return DecryptionKeyBytes (Config);
158                 }
159                 
160                 internal static byte [] DecryptionKeyBytes (MachineKeySection section)
161                 {
162                         if (section == null)
163                                 throw new ArgumentNullException ("section");
164                         
165                         if (decryption_key == null)
166                                 SetDecryptionKey (section.DecryptionKey);
167                         return decryption_key;
168                 }
169
170                 internal static byte [] DecryptionKey192Bits ()
171                 {
172                         return DecryptionKey192Bits (Config);
173                 }
174                 
175                 internal static byte [] DecryptionKey192Bits (MachineKeySection section)
176                 {
177                         if (section == null)
178                                 throw new ArgumentNullException ("section");
179                         
180                         if (decryption_key_192bits == null)
181                                 SetDecryptionKey (section.DecryptionKey);
182                         return decryption_key_192bits;
183                 }
184         }
185 }
186
187 #endif