2 // System.Web.Configuration.MachineKeyRegistryStorage
5 // Marek Habersack <mhabersack@novell.com>
7 // (C) 2007 Novell, Inc
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.
31 using Microsoft.Win32;
33 namespace System.Web.Configuration
35 internal class MachineKeyRegistryStorage
43 static string keyEncryption;
44 static string keyValidation;
46 static MachineKeyRegistryStorage ()
48 string appName = AppDomain.CurrentDomain.SetupInformation.ApplicationName;
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 ();
59 public static byte[] Retrieve (KeyType kt)
64 case KeyType.Validation:
68 case KeyType.Encryption:
73 throw new ArgumentException ("Unknown key type.");
82 RegistryKey v = OpenRegistryKey (key, false);
83 o = v.GetValue ("AutoGenKey", null);
88 if (o == null || o.GetType () != typeof (byte[]))
93 static RegistryKey OpenRegistryKey (string path, bool write)
96 string[] keys = path.Split ('\\');
97 int klen = keys.Length;
99 ret = Registry.CurrentUser;
100 for (int i = 0; i < klen; i++) {
101 tmp = ret.OpenSubKey (keys [i], true);
105 tmp = ret.CreateSubKey (keys [i]);
113 public static void Store (byte[] buf, KeyType kt)
120 case KeyType.Validation:
124 case KeyType.Encryption:
129 throw new ArgumentException ("Unknown key type.");
136 using (RegistryKey rk = OpenRegistryKey (key, true)) {
138 rk.SetValue ("AutoGenKey", buf, RegistryValueKind.Binary);
139 rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks, RegistryValueKind.QWord);
140 rk.SetValue ("AutoGenKeyFormat", 2, RegistryValueKind.DWord);
142 rk.SetValue ("AutoGenKey", buf);
143 rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks);
144 rk.SetValue ("AutoGenKeyFormat", 2);
146 rk.Flush (); // we want it synchronous
148 } catch (Exception ex) {
149 Console.Error.WriteLine ("(info) Auto generated encryption keys not saved: {0}", ex);