2
0

MachineKeySection.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. [MonoTODO]
  68. protected override void Reset (ConfigurationElement parentElement)
  69. {
  70. base.Reset (parentElement);
  71. }
  72. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  73. [StringValidator (MinLength = 1)]
  74. [ConfigurationProperty ("decryption", DefaultValue = "Auto")]
  75. public string Decryption {
  76. get { return (string) base [decryptionProp];}
  77. set { base[decryptionProp] = value; }
  78. }
  79. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  80. [StringValidator (MinLength = 1)]
  81. [ConfigurationProperty ("decryptionKey", DefaultValue = "AutoGenerate,IsolateApps")]
  82. public string DecryptionKey {
  83. get { return (string) base [decryptionKeyProp];}
  84. set { base[decryptionKeyProp] = value; SetDecryptionKey (value); }
  85. }
  86. [TypeConverter (typeof (MachineKeyValidationConverter))]
  87. [ConfigurationProperty ("validation", DefaultValue = "SHA1")]
  88. public MachineKeyValidation Validation {
  89. get { return (MachineKeyValidation) base [validationProp];}
  90. set { base[validationProp] = value; }
  91. }
  92. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  93. [StringValidator (MinLength = 1)]
  94. [ConfigurationProperty ("validationKey", DefaultValue = "AutoGenerate,IsolateApps")]
  95. public string ValidationKey {
  96. get { return (string) base [validationKeyProp];}
  97. set { base[validationKeyProp] = value; SetValidationKey (value); }
  98. }
  99. protected override ConfigurationPropertyCollection Properties {
  100. get { return properties; }
  101. }
  102. #region CompatabilityCode
  103. static byte [] autogenerated;
  104. static byte [] autogenerated_decrypt;
  105. byte[] decryption_key;
  106. byte[] decryption_key_192bits;
  107. byte[] validation_key;
  108. static void AutoGenKeys ()
  109. {
  110. autogenerated = new byte [64];
  111. RandomNumberGenerator rng = RandomNumberGenerator.Create ();
  112. rng.GetBytes (autogenerated);
  113. autogenerated_decrypt = new byte [64];
  114. rng.GetBytes (autogenerated_decrypt);
  115. }
  116. static byte ToHexValue (char c, bool high)
  117. {
  118. byte v;
  119. if (c >= '0' && c <= '9')
  120. v = (byte) (c - '0');
  121. else if (c >= 'a' && c <= 'f')
  122. v = (byte) (c - 'a' + 10);
  123. else if (c >= 'A' && c <= 'F')
  124. v = (byte) (c - 'A' + 10);
  125. else
  126. throw new ArgumentException ("Invalid hex character");
  127. if (high)
  128. v <<= 4;
  129. return v;
  130. }
  131. internal static byte [] GetBytes (string key, int len)
  132. {
  133. byte [] result = new byte [len / 2];
  134. for (int i = 0; i < len; i += 2)
  135. result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
  136. return result;
  137. }
  138. static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
  139. {
  140. if (key == null || key.StartsWith ("AutoGenerate")){
  141. //isolate = key.IndexOf ("IsolateApps") != 1;
  142. return (decryption) ? autogenerated_decrypt : autogenerated;
  143. }
  144. //isolate = false;
  145. int len = key.Length;
  146. if (len < 40 || len > 128 || (len % 2) == 1)
  147. throw new ArgumentException ("Invalid key length");
  148. return GetBytes (key, len);
  149. }
  150. internal void SetDecryptionKey (string n)
  151. {
  152. decryption_key = MakeKey (n, true); //, out isolate_decryption);
  153. decryption_key_192bits = new byte [24];
  154. int count = 24;
  155. if (decryption_key.Length < 24)
  156. count = decryption_key.Length;
  157. Buffer.BlockCopy (decryption_key, 0, decryption_key_192bits, 0, count);
  158. }
  159. internal void SetValidationKey (string n)
  160. {
  161. validation_key = MakeKey (n, false); //, out isolate_validation);
  162. }
  163. internal byte [] ValidationKeyBytes {
  164. get {
  165. if (validation_key == null)
  166. SetValidationKey (ValidationKey);
  167. return validation_key;
  168. }
  169. }
  170. internal byte [] DecryptionKeyBytes {
  171. get {
  172. if (decryption_key == null)
  173. SetDecryptionKey (DecryptionKey);
  174. return decryption_key;
  175. }
  176. }
  177. internal byte [] DecryptionKey192Bits {
  178. get {
  179. if (decryption_key_192bits == null)
  180. SetDecryptionKey (DecryptionKey);
  181. return decryption_key_192bits;
  182. }
  183. }
  184. #endregion
  185. }
  186. }
  187. #endif