2
0

MachineKeyRegistryStorage.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // System.Web.Configuration.MachineKeyRegistryStorage
  3. //
  4. // Authors:
  5. // Marek Habersack <[email protected]>
  6. //
  7. // (C) 2007 Novell, Inc
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Security.Cryptography;
  31. using Microsoft.Win32;
  32. namespace System.Web.Configuration
  33. {
  34. internal class MachineKeyRegistryStorage
  35. {
  36. public enum KeyType
  37. {
  38. Validation,
  39. Encryption
  40. };
  41. const int encryptionKeyLength = 64;
  42. const int validationKeyLength = 64;
  43. static string keyEncryption;
  44. static string keyValidation;
  45. static bool noStore;
  46. static MachineKeyRegistryStorage ()
  47. {
  48. string appName = AppDomain.CurrentDomain.SetupInformation.ApplicationName;
  49. if (appName == null) {
  50. noStore = true;
  51. return;
  52. }
  53. string hash = appName.GetHashCode ().ToString ("x");
  54. keyEncryption = "software\\mono\\asp.net\\" + Environment.Version.ToString () +
  55. "\\autogenkeys\\" + hash + "-" + ((int)KeyType.Encryption).ToString ();
  56. keyValidation = "software\\mono\\asp.net\\" + Environment.Version.ToString () +
  57. "\\autogenkeys\\" + hash + "-" + ((int)KeyType.Validation).ToString ();
  58. }
  59. public static byte[] Retrieve (KeyType kt)
  60. {
  61. byte[] ret = GetKey (kt);
  62. if (ret == null) {
  63. ret = Generate (kt);
  64. if (ret != null)
  65. Store (ret, kt);
  66. }
  67. return ret;
  68. }
  69. static byte[] GetKey (KeyType kt)
  70. {
  71. string key = null;
  72. int len;
  73. switch (kt) {
  74. case KeyType.Validation:
  75. key = keyValidation;
  76. len = validationKeyLength;
  77. break;
  78. case KeyType.Encryption:
  79. key = keyEncryption;
  80. len = validationKeyLength;
  81. break;
  82. default:
  83. throw new ArgumentException ("Unknown key type.");
  84. }
  85. if (key == null)
  86. return null;
  87. object o = null;
  88. try {
  89. RegistryKey v = OpenRegistryKey (key, false);
  90. o = v.GetValue ("AutoGenKey", null);
  91. } catch (Exception) {
  92. return null;
  93. }
  94. if (o == null || o.GetType () != typeof (byte[]))
  95. return null;
  96. byte[] ret = (byte[])o;
  97. if (ret.Length != len)
  98. return null;
  99. return ret;
  100. }
  101. static RegistryKey OpenRegistryKey (string path, bool write)
  102. {
  103. RegistryKey ret, tmp;
  104. string[] keys = path.Split ('\\');
  105. int klen = keys.Length;
  106. ret = Registry.CurrentUser;
  107. for (int i = 0; i < klen; i++) {
  108. tmp = ret.OpenSubKey (keys [i], true);
  109. if (tmp == null) {
  110. if (!write)
  111. return null;
  112. tmp = ret.CreateSubKey (keys [i]);
  113. }
  114. ret = tmp;
  115. }
  116. return ret;
  117. }
  118. static void Store (byte[] buf, KeyType kt)
  119. {
  120. if (buf == null)
  121. return;
  122. string key = null;
  123. int len;
  124. switch (kt) {
  125. case KeyType.Validation:
  126. key = keyValidation;
  127. len = validationKeyLength;
  128. break;
  129. case KeyType.Encryption:
  130. key = keyEncryption;
  131. len = validationKeyLength;
  132. break;
  133. default:
  134. throw new ArgumentException ("Unknown key type.");
  135. }
  136. if (key == null)
  137. return;
  138. if (buf.Length != len)
  139. throw new ArgumentException ("Key has invalid length");
  140. try {
  141. using (RegistryKey rk = OpenRegistryKey (key, true)) {
  142. #if NET_2_0
  143. rk.SetValue ("AutoGenKey", buf, RegistryValueKind.Binary);
  144. rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks, RegistryValueKind.QWord);
  145. rk.SetValue ("AutoGenKeyFormat", 2, RegistryValueKind.DWord);
  146. #else
  147. rk.SetValue ("AutoGenKey", buf);
  148. rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks);
  149. rk.SetValue ("AutoGenKeyFormat", 2);
  150. #endif
  151. rk.Flush (); // we want it synchronous
  152. }
  153. } catch (Exception ex) {
  154. throw new ApplicationException ("Failed to store encryption key in the registry.", ex);
  155. }
  156. }
  157. static byte[] Generate (KeyType kt)
  158. {
  159. RandomNumberGenerator rng = RandomNumberGenerator.Create ();
  160. byte[] ret = null;
  161. switch (kt) {
  162. case KeyType.Validation:
  163. ret = new byte [validationKeyLength];
  164. break;
  165. case KeyType.Encryption:
  166. ret = new byte [encryptionKeyLength];
  167. break;
  168. default:
  169. throw new ArgumentException ("Unknown key type.");
  170. }
  171. rng.GetBytes (ret);
  172. return ret;
  173. }
  174. }
  175. }