2
0

MachineKeySection.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // System.Web.Configuration.MachineKeySection
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (c) Copyright 2005 Novell, Inc (http://www.novell.com)
  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.ComponentModel;
  31. using System.Configuration;
  32. using System.Security.Cryptography;
  33. #if NET_2_0
  34. namespace System.Web.Configuration {
  35. public sealed class MachineKeySection : ConfigurationSection
  36. {
  37. static ConfigurationProperty decryptionProp;
  38. static ConfigurationProperty decryptionKeyProp;
  39. static ConfigurationProperty validationProp;
  40. static ConfigurationProperty validationKeyProp;
  41. static ConfigurationPropertyCollection properties;
  42. static MachineKeySection ()
  43. {
  44. decryptionProp = new ConfigurationProperty ("decryption", typeof (string), "Auto",
  45. PropertyHelper.WhiteSpaceTrimStringConverter,
  46. PropertyHelper.NonEmptyStringValidator,
  47. ConfigurationPropertyOptions.None);
  48. decryptionKeyProp = new ConfigurationProperty ("decryptionKey", typeof (string), "AutoGenerate,IsolateApps",
  49. PropertyHelper.WhiteSpaceTrimStringConverter,
  50. PropertyHelper.NonEmptyStringValidator,
  51. ConfigurationPropertyOptions.None);
  52. validationProp = new ConfigurationProperty ("validation", typeof (MachineKeyValidation), MachineKeyValidation.SHA1,
  53. new MachineKeyValidationConverter (),
  54. PropertyHelper.DefaultValidator,
  55. ConfigurationPropertyOptions.None);
  56. validationKeyProp = new ConfigurationProperty ("validationKey", typeof (string), "AutoGenerate,IsolateApps",
  57. PropertyHelper.WhiteSpaceTrimStringConverter,
  58. PropertyHelper.NonEmptyStringValidator,
  59. ConfigurationPropertyOptions.None);
  60. properties = new ConfigurationPropertyCollection ();
  61. properties.Add (decryptionProp);
  62. properties.Add (decryptionKeyProp);
  63. properties.Add (validationProp);
  64. properties.Add (validationKeyProp);
  65. AutoGenKeys ();
  66. }
  67. protected override void Reset (ConfigurationElement parentElement)
  68. {
  69. base.Reset (parentElement);
  70. }
  71. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  72. [StringValidator (MinLength = 1)]
  73. [ConfigurationProperty ("decryption", DefaultValue = "Auto")]
  74. public string Decryption {
  75. get { return (string) base [decryptionProp];}
  76. set { base[decryptionProp] = value; }
  77. }
  78. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  79. [StringValidator (MinLength = 1)]
  80. [ConfigurationProperty ("decryptionKey", DefaultValue = "AutoGenerate,IsolateApps")]
  81. public string DecryptionKey {
  82. get { return (string) base [decryptionKeyProp];}
  83. set { base[decryptionKeyProp] = value; SetDecryptionKey (value); }
  84. }
  85. [TypeConverter (typeof (MachineKeyValidationConverter))]
  86. [ConfigurationProperty ("validation", DefaultValue = "SHA1")]
  87. public MachineKeyValidation Validation {
  88. get { return (MachineKeyValidation) base [validationProp];}
  89. set { base[validationProp] = value; }
  90. }
  91. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  92. [StringValidator (MinLength = 1)]
  93. [ConfigurationProperty ("validationKey", DefaultValue = "AutoGenerate,IsolateApps")]
  94. public string ValidationKey {
  95. get { return (string) base [validationKeyProp];}
  96. set { base[validationKeyProp] = value; SetValidationKey (value); }
  97. }
  98. protected override ConfigurationPropertyCollection Properties {
  99. get { return properties; }
  100. }
  101. #region CompatabilityCode
  102. static byte [] autogenerated;
  103. static byte [] autogenerated_decrypt;
  104. byte[] decryption_key;
  105. byte[] decryption_key_192bits;
  106. byte[] validation_key;
  107. static void AutoGenKeys ()
  108. {
  109. #if TARGET_J2EE
  110. {
  111. #else
  112. try {
  113. autogenerated = MachineKeyRegistryStorage.Retrieve (MachineKeyRegistryStorage.KeyType.Validation);
  114. autogenerated_decrypt = MachineKeyRegistryStorage.Retrieve (MachineKeyRegistryStorage.KeyType.Encryption);
  115. } catch (Exception) {
  116. #endif
  117. // Fall back to old method
  118. autogenerated = new byte [64];
  119. RandomNumberGenerator rng = RandomNumberGenerator.Create ();
  120. rng.GetBytes (autogenerated);
  121. autogenerated_decrypt = new byte [64];
  122. rng.GetBytes (autogenerated_decrypt);
  123. }
  124. }
  125. static byte ToHexValue (char c, bool high)
  126. {
  127. byte v;
  128. if (c >= '0' && c <= '9')
  129. v = (byte) (c - '0');
  130. else if (c >= 'a' && c <= 'f')
  131. v = (byte) (c - 'a' + 10);
  132. else if (c >= 'A' && c <= 'F')
  133. v = (byte) (c - 'A' + 10);
  134. else
  135. throw new ArgumentException ("Invalid hex character");
  136. if (high)
  137. v <<= 4;
  138. return v;
  139. }
  140. internal static byte [] GetBytes (string key, int len)
  141. {
  142. byte [] result = new byte [len / 2];
  143. for (int i = 0; i < len; i += 2)
  144. result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
  145. return result;
  146. }
  147. static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
  148. {
  149. if (key == null || key.StartsWith ("AutoGenerate")){
  150. //isolate = key.IndexOf ("IsolateApps") != 1;
  151. return (decryption) ? autogenerated_decrypt : autogenerated;
  152. }
  153. //isolate = false;
  154. int len = key.Length;
  155. if (len < 40 || len > 128 || (len % 2) == 1)
  156. throw new ArgumentException ("Invalid key length");
  157. return GetBytes (key, len);
  158. }
  159. internal void SetDecryptionKey (string n)
  160. {
  161. decryption_key = MakeKey (n, true); //, out isolate_decryption);
  162. decryption_key_192bits = new byte [24];
  163. int count = 24;
  164. if (decryption_key.Length < 24)
  165. count = decryption_key.Length;
  166. Buffer.BlockCopy (decryption_key, 0, decryption_key_192bits, 0, count);
  167. }
  168. internal void SetValidationKey (string n)
  169. {
  170. validation_key = MakeKey (n, false); //, out isolate_validation);
  171. }
  172. internal byte [] ValidationKeyBytes {
  173. get {
  174. if (validation_key == null)
  175. SetValidationKey (ValidationKey);
  176. return validation_key;
  177. }
  178. }
  179. internal byte [] DecryptionKeyBytes {
  180. get {
  181. if (decryption_key == null)
  182. SetDecryptionKey (DecryptionKey);
  183. return decryption_key;
  184. }
  185. }
  186. internal byte [] DecryptionKey192Bits {
  187. get {
  188. if (decryption_key_192bits == null)
  189. SetDecryptionKey (DecryptionKey);
  190. return decryption_key_192bits;
  191. }
  192. }
  193. #endregion
  194. }
  195. }
  196. #endif