MachineKeyConfigHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // System.Web.Configuration.MachineKeyConfigHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.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.Collections;
  31. using System.Configuration;
  32. using System.Security.Cryptography;
  33. using System.Xml;
  34. namespace System.Web.Configuration
  35. {
  36. class MachineKeyConfigHandler : IConfigurationSectionHandler
  37. {
  38. static byte [] autogenerated;
  39. static MachineKeyConfigHandler ()
  40. {
  41. autogenerated = new byte [64];
  42. RNGCryptoServiceProvider cp = new RNGCryptoServiceProvider ();
  43. cp.GetBytes (autogenerated);
  44. }
  45. static byte ToHexValue (char c, bool high)
  46. {
  47. byte v;
  48. if (c >= '0' && c <= '9')
  49. v = (byte) (c - '0');
  50. else if (c >= 'a' && c <= 'f')
  51. v = (byte) (c - 'a' + 10);
  52. else if (c >= 'A' && c <= 'F')
  53. v = (byte) (c - 'A' + 10);
  54. else
  55. throw new ArgumentException ("Invalid hex character");
  56. if (high)
  57. v <<= 4;
  58. return v;
  59. }
  60. internal static byte [] GetBytes (string key, int len)
  61. {
  62. byte [] result = new byte [len / 2];
  63. for (int i = 0; i < len; i += 2)
  64. result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
  65. return result;
  66. }
  67. static byte [] MakeKey (string key)
  68. {
  69. if (key == null || key == "AutoGenerated")
  70. return autogenerated;
  71. int len = key.Length;
  72. if (len < 40 || len > 128 || (len % 2) == 1)
  73. throw new ArgumentException ("Invalid key length");
  74. return GetBytes (key, len);
  75. }
  76. public object Create (object parent, object context, XmlNode section)
  77. {
  78. if (section.HasChildNodes)
  79. ThrowException ("Child nodes not allowed here", section.FirstChild);
  80. MachineKeyConfig config = new MachineKeyConfig (parent);
  81. string validationKey = AttValue ("validationKey", section);
  82. try {
  83. config.ValidationKey = MakeKey (validationKey);
  84. } catch (ArgumentException e) {
  85. ThrowException (e.Message, section);
  86. }
  87. string decryptionKey = AttValue ("decryptionKey", section);
  88. try {
  89. config.DecryptionKey = MakeKey (decryptionKey);
  90. } catch (ArgumentException e) {
  91. ThrowException (e.Message, section);
  92. }
  93. string validation = AttValue ("validation", section);
  94. if (validation != "SHA1" && validation != "MD5" && validation != "3DES")
  95. ThrowException ("Invalid 'validation' value", section);
  96. config.ValidationType = validation;
  97. if (section.Attributes != null && section.Attributes.Count != 0)
  98. ThrowException ("Unrecognized attribute", section);
  99. MachineKeyConfig.MachineKey = config;
  100. return config;
  101. }
  102. // A few methods to save some typing
  103. static string AttValue (string name, XmlNode node)
  104. {
  105. return HandlersUtil.ExtractAttributeValue (name, node, true);
  106. }
  107. static void ThrowException (string message, XmlNode node)
  108. {
  109. HandlersUtil.ThrowException (message, node);
  110. }
  111. //
  112. }
  113. }