MachineKeySection.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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; }
  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; }
  98. }
  99. protected override ConfigurationPropertyCollection Properties {
  100. get { return properties; }
  101. }
  102. #region CompatabilityCode
  103. static byte [] autogenerated;
  104. static byte [] autogenerated_decrypt;
  105. static void AutoGenKeys ()
  106. {
  107. autogenerated = new byte [64];
  108. RandomNumberGenerator rng = RandomNumberGenerator.Create ();
  109. rng.GetBytes (autogenerated);
  110. autogenerated_decrypt = new byte [64];
  111. rng.GetBytes (autogenerated_decrypt);
  112. }
  113. static byte ToHexValue (char c, bool high)
  114. {
  115. byte v;
  116. if (c >= '0' && c <= '9')
  117. v = (byte) (c - '0');
  118. else if (c >= 'a' && c <= 'f')
  119. v = (byte) (c - 'a' + 10);
  120. else if (c >= 'A' && c <= 'F')
  121. v = (byte) (c - 'A' + 10);
  122. else
  123. throw new ArgumentException ("Invalid hex character");
  124. if (high)
  125. v <<= 4;
  126. return v;
  127. }
  128. internal static byte [] GetBytes (string key, int len)
  129. {
  130. byte [] result = new byte [len / 2];
  131. for (int i = 0; i < len; i += 2)
  132. result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
  133. return result;
  134. }
  135. static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
  136. {
  137. if (key == null || key.StartsWith ("AutoGenerate")){
  138. //isolate = key.IndexOf ("IsolateApps") != 1;
  139. return (decryption) ? autogenerated_decrypt : autogenerated;
  140. }
  141. //isolate = false;
  142. int len = key.Length;
  143. if (len < 40 || len > 128 || (len % 2) == 1)
  144. throw new ArgumentException ("Invalid key length");
  145. return GetBytes (key, len);
  146. }
  147. internal byte [] ValidationKeyBytes {
  148. get { return MakeKey (ValidationKey, false); }
  149. }
  150. #endregion
  151. }
  152. }
  153. #endif