MachineKeyRegistryStorage.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Microsoft.Win32;
  30. namespace System.Web.Configuration
  31. {
  32. internal class MachineKeyRegistryStorage
  33. {
  34. public enum KeyType
  35. {
  36. Validation,
  37. Encryption
  38. };
  39. static string keyEncryption;
  40. static string keyValidation;
  41. static MachineKeyRegistryStorage ()
  42. {
  43. string appName = AppDomain.CurrentDomain.SetupInformation.ApplicationName;
  44. if (appName == null)
  45. return;
  46. string hash = appName.GetHashCode ().ToString ("x");
  47. keyEncryption = "software\\mono\\asp.net\\" + Environment.Version.ToString () +
  48. "\\autogenkeys\\" + hash + "-" + ((int)KeyType.Encryption).ToString ();
  49. keyValidation = "software\\mono\\asp.net\\" + Environment.Version.ToString () +
  50. "\\autogenkeys\\" + hash + "-" + ((int)KeyType.Validation).ToString ();
  51. }
  52. public static byte[] Retrieve (KeyType kt)
  53. {
  54. string key = null;
  55. switch (kt) {
  56. case KeyType.Validation:
  57. key = keyValidation;
  58. break;
  59. case KeyType.Encryption:
  60. key = keyEncryption;
  61. break;
  62. default:
  63. throw new ArgumentException ("Unknown key type.");
  64. }
  65. if (key == null)
  66. return null;
  67. object o = null;
  68. try {
  69. RegistryKey v = OpenRegistryKey (key, false);
  70. o = v.GetValue ("AutoGenKey", null);
  71. } catch (Exception) {
  72. return null;
  73. }
  74. if (o == null || o.GetType () != typeof (byte[]))
  75. return null;
  76. return (byte[]) o;
  77. }
  78. static RegistryKey OpenRegistryKey (string path, bool write)
  79. {
  80. RegistryKey ret, tmp;
  81. string[] keys = path.Split ('\\');
  82. int klen = keys.Length;
  83. ret = Registry.CurrentUser;
  84. for (int i = 0; i < klen; i++) {
  85. tmp = ret.OpenSubKey (keys [i], true);
  86. if (tmp == null) {
  87. if (!write)
  88. return null;
  89. tmp = ret.CreateSubKey (keys [i]);
  90. }
  91. ret = tmp;
  92. }
  93. return ret;
  94. }
  95. public static void Store (byte[] buf, KeyType kt)
  96. {
  97. if (buf == null)
  98. return;
  99. string key = null;
  100. switch (kt) {
  101. case KeyType.Validation:
  102. key = keyValidation;
  103. break;
  104. case KeyType.Encryption:
  105. key = keyEncryption;
  106. break;
  107. default:
  108. throw new ArgumentException ("Unknown key type.");
  109. }
  110. if (key == null)
  111. return;
  112. try {
  113. using (RegistryKey rk = OpenRegistryKey (key, true)) {
  114. #if NET_2_0
  115. rk.SetValue ("AutoGenKey", buf, RegistryValueKind.Binary);
  116. rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks, RegistryValueKind.QWord);
  117. rk.SetValue ("AutoGenKeyFormat", 2, RegistryValueKind.DWord);
  118. #else
  119. rk.SetValue ("AutoGenKey", buf);
  120. rk.SetValue ("AutoGenKeyCreationTime", DateTime.Now.Ticks);
  121. rk.SetValue ("AutoGenKeyFormat", 2);
  122. #endif
  123. rk.Flush (); // we want it synchronous
  124. }
  125. } catch (Exception ex) {
  126. throw new ApplicationException ("Failed to store encryption key in the registry.", ex);
  127. }
  128. }
  129. }
  130. }