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.
32 using System.Security.Cryptography;
33 using Microsoft.Win32;
35 namespace System.Web.Configuration
37 internal class MachineKeyRegistryStorage
45 const int encryptionKeyLength = 64;
46 const int validationKeyLength = 64;
48 static string keyEncryption;
49 static string keyValidation;
52 static MachineKeyRegistryStorage ()
54 string appName = AppDomain.CurrentDomain.SetupInformation.ApplicationName;
55 if (appName == null) {
60 string hash = appName.GetHashCode ().ToString ("x");
61 keyEncryption = String.Format ("software\\mono\\asp.net\\{0}\\autogenkeys\\{1}-{2}",
64 (int)KeyType.Encryption);
65 keyValidation = String.Format ("software\\mono\\asp.net\\{0}\\autogenkeys\\{1}-{2}",
68 (int)KeyType.Validation);
71 public static byte[] Retrieve (KeyType kt)
73 byte[] ret = GetKey (kt);
83 static byte[] GetKey (KeyType kt)
89 case KeyType.Validation:
91 len = validationKeyLength;
94 case KeyType.Encryption:
96 len = validationKeyLength;
100 throw new ArgumentException ("Unknown key type.");
109 RegistryKey v = OpenRegistryKey (key, false);
110 o = v.GetValue ("AutoGenKey", null);
111 } catch (Exception) {
115 if (o == null || o.GetType () != typeof (byte[]))
117 byte[] ret = (byte[])o;
118 if (ret.Length != len)
124 static RegistryKey OpenRegistryKey (string path, bool write)
126 RegistryKey ret, tmp;
127 string[] keys = path.Split ('\\');
128 int klen = keys.Length;
130 ret = Registry.CurrentUser;
131 for (int i = 0; i < klen; i++) {
132 tmp = ret.OpenSubKey (keys [i], true);
136 tmp = ret.CreateSubKey (keys [i]);
144 static void Store (byte[] buf, KeyType kt)
153 case KeyType.Validation:
155 len = validationKeyLength;
158 case KeyType.Encryption:
160 len = validationKeyLength;
164 throw new ArgumentException ("Unknown key type.");
170 if (buf.Length != len)
171 throw new ArgumentException ("Key has invalid length");
174 using (RegistryKey rk = OpenRegistryKey (key, true)) {
176 rk.SetValue ("AutoGenKey", buf, RegistryValueKind.Binary);
177 rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks, RegistryValueKind.QWord);
178 rk.SetValue ("AutoGenKeyFormat", 2, RegistryValueKind.DWord);
180 rk.SetValue ("AutoGenKey", buf);
181 rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks);
182 rk.SetValue ("AutoGenKeyFormat", 2);
184 rk.Flush (); // we want it synchronous
186 } catch (Exception ex) {
187 throw new ApplicationException ("Failed to store encryption key in the registry.", ex);
191 static byte[] Generate (KeyType kt)
193 RandomNumberGenerator rng = RandomNumberGenerator.Create ();
197 case KeyType.Validation:
198 ret = new byte [validationKeyLength];
201 case KeyType.Encryption:
202 ret = new byte [encryptionKeyLength];
206 throw new ArgumentException ("Unknown key type.");