Merge branch 'cecil-light'
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / MachineKeyRegistryStorage.cs
1 //
2 // System.Web.Configuration.MachineKeyRegistryStorage
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // (C) 2007 Novell, Inc
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 Microsoft.Win32;
32
33 namespace System.Web.Configuration
34 {
35         internal class MachineKeyRegistryStorage
36         {
37                 public enum KeyType
38                 {
39                         Validation,
40                         Encryption
41                 };
42
43                 static string keyEncryption;
44                 static string keyValidation;
45                 
46                 static MachineKeyRegistryStorage ()
47                 {
48                         string appName = AppDomain.CurrentDomain.SetupInformation.ApplicationName;
49                         if (appName == null)
50                                 return;
51                         
52                         string hash = appName.GetHashCode ().ToString ("x");
53                         keyEncryption = "software\\mono\\asp.net\\" + Environment.Version.ToString () +
54                                 "\\autogenkeys\\" + hash + "-" + ((int)KeyType.Encryption).ToString ();
55                         keyValidation = "software\\mono\\asp.net\\" + Environment.Version.ToString () +
56                                 "\\autogenkeys\\" + hash + "-" + ((int)KeyType.Validation).ToString ();
57                 }
58                 
59                 public static byte[] Retrieve (KeyType kt)
60                 {
61                         string key = null;
62                         
63                         switch (kt) {
64                                 case KeyType.Validation:
65                                         key = keyValidation;
66                                         break;
67
68                                 case KeyType.Encryption:
69                                         key = keyEncryption;
70                                         break;
71
72                                 default:
73                                         throw new ArgumentException ("Unknown key type.");
74                         }
75
76                         if (key == null)
77                                 return null;
78                         
79                         object o = null;
80
81                         try {
82                                 RegistryKey v = OpenRegistryKey (key, false);
83                                 o = v.GetValue ("AutoGenKey", null);
84                         } catch (Exception) {
85                                 return null;
86                         }
87
88                         if (o == null || o.GetType () != typeof (byte[]))
89                                 return null;
90                         return (byte[]) o;
91                 }
92
93                 static RegistryKey OpenRegistryKey (string path, bool write)
94                 {
95                         RegistryKey ret, tmp;
96                         string[] keys = path.Split ('\\');
97                         int klen = keys.Length;
98
99                         ret = Registry.CurrentUser;
100                         for (int i = 0; i < klen; i++) {
101                                 tmp = ret.OpenSubKey (keys [i], true);
102                                 if (tmp == null) {
103                                         if (!write)
104                                                 return null;
105                                         tmp = ret.CreateSubKey (keys [i]);
106                                 }
107                                 ret = tmp;
108                         }
109
110                         return ret;
111                 }
112                 
113                 public static void Store (byte[] buf, KeyType kt)
114                 {
115                         if (buf == null)
116                                 return;
117                         
118                         string key = null;
119                         switch (kt) {
120                                 case KeyType.Validation:
121                                         key = keyValidation;
122                                         break;
123
124                                 case KeyType.Encryption:
125                                         key = keyEncryption;
126                                         break;
127
128                                 default:
129                                         throw new ArgumentException ("Unknown key type.");
130                         }
131
132                         if (key == null)
133                                 return;
134
135                         try {
136                                 using (RegistryKey rk = OpenRegistryKey (key, true)) {
137 #if NET_2_0
138                                         rk.SetValue ("AutoGenKey", buf, RegistryValueKind.Binary);
139                                         rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks, RegistryValueKind.QWord);
140                                         rk.SetValue ("AutoGenKeyFormat", 2, RegistryValueKind.DWord);
141 #else
142                                         rk.SetValue ("AutoGenKey", buf);
143                                         rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks);
144                                         rk.SetValue ("AutoGenKeyFormat", 2);
145 #endif
146                                         rk.Flush (); // we want it synchronous
147                                 }
148                         } catch (Exception ex) {
149                                 Console.Error.WriteLine ("(info) Auto generated encryption keys not saved: {0}", ex);
150                         }
151                 }
152         }
153 }