MachineKeyConfigHandler.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. using System;
  10. using System.Collections;
  11. using System.Configuration;
  12. using System.Security.Cryptography;
  13. using System.Xml;
  14. namespace System.Web.Configuration
  15. {
  16. class MachineKeyConfigHandler : IConfigurationSectionHandler
  17. {
  18. static byte [] autogenerated;
  19. static MachineKeyConfigHandler ()
  20. {
  21. autogenerated = new byte [64];
  22. RNGCryptoServiceProvider cp = new RNGCryptoServiceProvider ();
  23. cp.GetBytes (autogenerated);
  24. }
  25. static byte ToHexValue (char c, bool high)
  26. {
  27. byte v;
  28. if (c >= '0' && c <= '9')
  29. v = (byte) (c - '0');
  30. else if (c >= 'a' && c <= 'f')
  31. v = (byte) (c - 'a' + 10);
  32. else if (c >= 'A' && c <= 'F')
  33. v = (byte) (c - 'A' + 10);
  34. else
  35. throw new ArgumentException ("Invalid hex character");
  36. if (high)
  37. v <<= 4;
  38. return v;
  39. }
  40. internal static byte [] GetBytes (string key, int len)
  41. {
  42. byte [] result = new byte [len / 2];
  43. for (int i = 0; i < len; i += 2)
  44. result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
  45. return result;
  46. }
  47. static byte [] MakeKey (string key)
  48. {
  49. if (key == null || key == "AutoGenerated")
  50. return autogenerated;
  51. int len = key.Length;
  52. if (len < 40 || len > 128 || (len % 2) == 1)
  53. throw new ArgumentException ("Invalid key length");
  54. return GetBytes (key, len);
  55. }
  56. public object Create (object parent, object context, XmlNode section)
  57. {
  58. if (section.HasChildNodes)
  59. ThrowException ("Child nodes not allowed here", section.FirstChild);
  60. MachineKeyConfig config = new MachineKeyConfig (parent);
  61. string validationKey = AttValue ("validationKey", section);
  62. try {
  63. config.ValidationKey = MakeKey (validationKey);
  64. } catch (ArgumentException e) {
  65. ThrowException (e.Message, section);
  66. }
  67. string decryptionKey = AttValue ("decryptionKey", section);
  68. try {
  69. config.DecryptionKey = MakeKey (decryptionKey);
  70. } catch (ArgumentException e) {
  71. ThrowException (e.Message, section);
  72. }
  73. string validation = AttValue ("validation", section);
  74. if (validation != "SHA1" && validation != "MD5" && validation != "3DES")
  75. ThrowException ("Invalid 'validation' value", section);
  76. config.ValidationType = validation;
  77. if (section.Attributes != null && section.Attributes.Count != 0)
  78. ThrowException ("Unrecognized attribute", section);
  79. MachineKeyConfig.MachineKey = config;
  80. return config;
  81. }
  82. // A few methods to save some typing
  83. static string AttValue (string name, XmlNode node)
  84. {
  85. return HandlersUtil.ExtractAttributeValue (name, node, true);
  86. }
  87. static void ThrowException (string message, XmlNode node)
  88. {
  89. HandlersUtil.ThrowException (message, node);
  90. }
  91. //
  92. }
  93. }