Author: mhabersack
Date: 2007-08-30 13:32:52 -0400 (Thu, 30 Aug 2007)
New Revision: 85063

Added:
   
trunk/mcs/class/System.Web/System.Web.Configuration/MachineKeyRegistryStorage.cs
Modified:
   trunk/mcs/class/System.Web/ChangeLog
   trunk/mcs/class/System.Web/System.Web.Configuration/ChangeLog
   trunk/mcs/class/System.Web/System.Web.Configuration_2.0/ChangeLog
   trunk/mcs/class/System.Web/System.Web.Configuration_2.0/MachineKeySection.cs
   trunk/mcs/class/System.Web/System.Web.dll.sources
Log:
2007-08-30  Marek Habersack  <[EMAIL PROTECTED]>

            * System.Web.dll.sources: added MachineKeyRegistryStorage.cs

2007-08-30  Marek Habersack  <[EMAIL PROTECTED]>

            * MachineKeySection.cs: retrieve the keys from the registry before
            falling back to the old method. Fixes bug #76606

2007-08-30  Marek Habersack  <[EMAIL PROTECTED]>

            * MachineKeyRegistryStorage.cs: added



Modified: trunk/mcs/class/System.Web/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/ChangeLog        2007-08-30 17:18:14 UTC (rev 
85062)
+++ trunk/mcs/class/System.Web/ChangeLog        2007-08-30 17:32:52 UTC (rev 
85063)
@@ -1,3 +1,7 @@
+2007-08-30  Marek Habersack  <[EMAIL PROTECTED]>
+
+       * System.Web.dll.sources: added MachineKeyRegistryStorage.cs
+
 2007-08-14  Gert Driesen  <[EMAIL PROTECTED]>
 
        * System.Web_test.dll.sources: Added AggregateCacheDependencyTest.cs.

Modified: trunk/mcs/class/System.Web/System.Web.Configuration/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Configuration/ChangeLog       
2007-08-30 17:18:14 UTC (rev 85062)
+++ trunk/mcs/class/System.Web/System.Web.Configuration/ChangeLog       
2007-08-30 17:32:52 UTC (rev 85063)
@@ -1,3 +1,7 @@
+2007-08-30  Marek Habersack  <[EMAIL PROTECTED]>
+
+       * MachineKeyRegistryStorage.cs: added
+
 2007-08-23  Robert Jordan  <[EMAIL PROTECTED]>
 
        * WebConfigurationSettings.cs: Move RemotingConfiguration to

Added: 
trunk/mcs/class/System.Web/System.Web.Configuration/MachineKeyRegistryStorage.cs
===================================================================
--- 
trunk/mcs/class/System.Web/System.Web.Configuration/MachineKeyRegistryStorage.cs
    2007-08-30 17:18:14 UTC (rev 85062)
+++ 
trunk/mcs/class/System.Web/System.Web.Configuration/MachineKeyRegistryStorage.cs
    2007-08-30 17:32:52 UTC (rev 85063)
@@ -0,0 +1,213 @@
+//
+// System.Web.Configuration.MachineKeyRegistryStorage
+//
+// Authors:
+//     Marek Habersack <[EMAIL PROTECTED]>
+//
+// (C) 2007 Novell, Inc
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Security.Cryptography;
+using Microsoft.Win32;
+
+namespace System.Web.Configuration
+{
+       internal class MachineKeyRegistryStorage
+       {
+               public enum KeyType
+               {
+                       Validation,
+                       Encryption
+               };
+
+               const int encryptionKeyLength = 64;
+               const int validationKeyLength = 64;
+               
+               static string keyEncryption;
+               static string keyValidation;
+               static bool noStore;
+               
+               static MachineKeyRegistryStorage ()
+               {
+                       string appName = 
AppDomain.CurrentDomain.SetupInformation.ApplicationName;
+                       if (appName == null) {
+                               noStore = true;
+                               return;
+                       }
+                       
+                       string hash = appName.GetHashCode ().ToString ("x");
+                       keyEncryption = String.Format 
("software\\mono\\asp.net\\{0}\\autogenkeys\\{1}-{2}",
+                                                      Environment.Version,
+                                                      hash,
+                                                      (int)KeyType.Encryption);
+                       keyValidation = String.Format 
("software\\mono\\asp.net\\{0}\\autogenkeys\\{1}-{2}",
+                                                      Environment.Version,
+                                                      hash,
+                                                      (int)KeyType.Validation);
+               }
+               
+               public static byte[] Retrieve (KeyType kt)
+               {
+                       byte[] ret = GetKey (kt);
+                       if (ret == null) {
+                               ret = Generate (kt);
+                               if (ret != null)
+                                       Store (ret, kt);
+                       }
+                       
+                       return ret;
+               }
+
+               static byte[] GetKey (KeyType kt)
+               {
+                       string key = null;
+                       int len;
+                       
+                       switch (kt) {
+                               case KeyType.Validation:
+                                       key = keyValidation;
+                                       len = validationKeyLength;
+                                       break;
+
+                               case KeyType.Encryption:
+                                       key = keyEncryption;
+                                       len = validationKeyLength;
+                                       break;
+
+                               default:
+                                       throw new ArgumentException ("Unknown 
key type.");
+                       }
+
+                       if (key == null)
+                               return null;
+                       
+                       object o = null;
+
+                       try {
+                               RegistryKey v = OpenRegistryKey (key, false);
+                               o = v.GetValue ("AutoGenKey", null);
+                       } catch (Exception) {
+                               return null;
+                       }
+
+                       if (o == null || o.GetType () != typeof (byte[]))
+                               return null;
+                       byte[] ret = (byte[])o;
+                       if (ret.Length != len)
+                               return null;
+
+                       return ret;
+               }
+
+               static RegistryKey OpenRegistryKey (string path, bool write)
+               {
+                       RegistryKey ret, tmp;
+                       string[] keys = path.Split ('\\');
+                       int klen = keys.Length;
+
+                       ret = Registry.CurrentUser;
+                       for (int i = 0; i < klen; i++) {
+                               tmp = ret.OpenSubKey (keys [i], true);
+                               if (tmp == null) {
+                                       if (!write)
+                                               return null;
+                                       tmp = ret.CreateSubKey (keys [i]);
+                               }
+                               ret = tmp;
+                       }
+
+                       return ret;
+               }
+               
+               static void Store (byte[] buf, KeyType kt)
+               {
+                       if (buf == null)
+                               return;
+                       
+                       string key = null;
+                       int len;
+                       
+                       switch (kt) {
+                               case KeyType.Validation:
+                                       key = keyValidation;
+                                       len = validationKeyLength;
+                                       break;
+
+                               case KeyType.Encryption:
+                                       key = keyEncryption;
+                                       len = validationKeyLength;
+                                       break;
+
+                               default:
+                                       throw new ArgumentException ("Unknown 
key type.");
+                       }
+
+                       if (key == null)
+                               return;
+
+                       if (buf.Length != len)
+                               throw new ArgumentException ("Key has invalid 
length");
+
+                       try {
+                               using (RegistryKey rk = OpenRegistryKey (key, 
true)) {
+#if NET_2_0
+                                       rk.SetValue ("AutoGenKey", buf, 
RegistryValueKind.Binary);
+                                       rk.SetValue ("AutoGenKeyCreationTime", 
DateTime.Now.Ticks, RegistryValueKind.QWord);
+                                       rk.SetValue ("AutoGenKeyFormat", 2, 
RegistryValueKind.DWord);
+#else
+                                       rk.SetValue ("AutoGenKey", buf);
+                                       rk.SetValue ("AutoGenKeyCreationTime", 
DateTime.Now.Ticks);
+                                       rk.SetValue ("AutoGenKeyFormat", 2);
+#endif
+                                       rk.Flush (); // we want it synchronous
+                               }
+                       } catch (Exception ex) {
+                               throw new ApplicationException ("Failed to 
store encryption key in the registry.", ex);
+                       }
+               }
+
+               static byte[] Generate (KeyType kt)
+               {
+                       RandomNumberGenerator rng = 
RandomNumberGenerator.Create ();
+                       byte[] ret = null;
+                       
+                       switch (kt) {
+                               case KeyType.Validation:
+                                       ret = new byte [validationKeyLength];
+                                       break;
+
+                               case KeyType.Encryption:
+                                       ret = new byte [encryptionKeyLength];
+                                       break;
+
+                               default:
+                                       throw new ArgumentException ("Unknown 
key type.");
+                       }
+                       
+                       rng.GetBytes (ret);
+                       return ret;
+               }
+       }
+}


Property changes on: 
trunk/mcs/class/System.Web/System.Web.Configuration/MachineKeyRegistryStorage.cs
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/mcs/class/System.Web/System.Web.Configuration_2.0/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Configuration_2.0/ChangeLog   
2007-08-30 17:18:14 UTC (rev 85062)
+++ trunk/mcs/class/System.Web/System.Web.Configuration_2.0/ChangeLog   
2007-08-30 17:32:52 UTC (rev 85063)
@@ -1,3 +1,8 @@
+2007-08-30  Marek Habersack  <[EMAIL PROTECTED]>
+
+       * MachineKeySection.cs: retrieve the keys from the registry before
+       falling back to the old method. Fixes bug #76606
+
 2007-08-23  Marek Habersack  <[EMAIL PROTECTED]>
 
        * ProvidersHelper.cs: HttpApplication.LoadTypeFromPrivateBin

Modified: 
trunk/mcs/class/System.Web/System.Web.Configuration_2.0/MachineKeySection.cs
===================================================================
--- 
trunk/mcs/class/System.Web/System.Web.Configuration_2.0/MachineKeySection.cs    
    2007-08-30 17:18:14 UTC (rev 85062)
+++ 
trunk/mcs/class/System.Web/System.Web.Configuration_2.0/MachineKeySection.cs    
    2007-08-30 17:32:52 UTC (rev 85063)
@@ -123,11 +123,17 @@
 
                static void AutoGenKeys ()
                {
-                       autogenerated = new byte [64];
-                       RandomNumberGenerator rng = 
RandomNumberGenerator.Create ();
-                       rng.GetBytes (autogenerated);
-                       autogenerated_decrypt = new byte [64];
-                       rng.GetBytes (autogenerated_decrypt);
+                       try {
+                               autogenerated = 
MachineKeyRegistryStorage.Retrieve 
(MachineKeyRegistryStorage.KeyType.Validation);
+                               autogenerated_decrypt = 
MachineKeyRegistryStorage.Retrieve 
(MachineKeyRegistryStorage.KeyType.Encryption);
+                       } catch (Exception) {
+                               // Fall back to old method
+                               autogenerated = new byte [64];
+                               RandomNumberGenerator rng = 
RandomNumberGenerator.Create ();
+                               rng.GetBytes (autogenerated);
+                               autogenerated_decrypt = new byte [64];
+                               rng.GetBytes (autogenerated_decrypt);
+                       }
                }
 
                static byte ToHexValue (char c, bool high)

Modified: trunk/mcs/class/System.Web/System.Web.dll.sources
===================================================================
--- trunk/mcs/class/System.Web/System.Web.dll.sources   2007-08-30 17:18:14 UTC 
(rev 85062)
+++ trunk/mcs/class/System.Web/System.Web.dll.sources   2007-08-30 17:32:52 UTC 
(rev 85063)
@@ -103,6 +103,7 @@
 System.Web.Configuration/HttpRuntimeConfigurationHandler.cs
 System.Web.Configuration/MachineKeyConfig.cs
 System.Web.Configuration/MachineKeyConfigHandler.cs
+System.Web.Configuration/MachineKeyRegistryStorage.cs
 System.Web.Configuration/MachineKeyValidation.cs
 System.Web.Configuration/ModulesConfiguration.cs
 System.Web.Configuration/PagesConfiguration.cs

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to