MachineKeyConfigHandler.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. //TODO: context?
  61. MachineKeyConfig config = new MachineKeyConfig (parent);
  62. string validationKey = AttValue ("validationKey", section);
  63. try {
  64. config.ValidationKey = MakeKey (validationKey);
  65. } catch (ArgumentException e) {
  66. ThrowException (e.Message, section);
  67. }
  68. string decryptionKey = AttValue ("decryptionKey", section);
  69. try {
  70. config.DecryptionKey = MakeKey (decryptionKey);
  71. } catch (ArgumentException e) {
  72. ThrowException (e.Message, section);
  73. }
  74. string validation = AttValue ("validation", section);
  75. if (validation != "SHA1" && validation != "MD5" && validation != "3DES")
  76. ThrowException ("Invalid 'validation' value", section);
  77. config.ValidationType = validation;
  78. if (section.Attributes != null && section.Attributes.Count != 0)
  79. ThrowException ("Unrecognized attribute", section);
  80. MachineKeyConfig.MachineKey = config;
  81. return config;
  82. }
  83. // A few methods to save some typing
  84. static string AttValue (string name, XmlNode node)
  85. {
  86. return HandlersUtil.ExtractAttributeValue (name, node, true);
  87. }
  88. static void ThrowException (string message, XmlNode node)
  89. {
  90. HandlersUtil.ThrowException (message, node);
  91. }
  92. //
  93. }
  94. }