MachineKeyConfig.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // System.Web.Configuration.MachineKeyConfig
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. // Copyright (c) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Configuration;
  33. using System.Xml;
  34. using System.Security.Cryptography;
  35. namespace System.Web.Configuration
  36. {
  37. class MachineKeyConfig
  38. {
  39. byte [] validation_key;
  40. //bool isolate_validation;
  41. byte [] decryption_key;
  42. byte [] decryption_key_192bits;
  43. //bool isolate_decryption; // For us, this is always true by now.
  44. MachineKeyValidation validation_type;
  45. static byte [] autogenerated;
  46. static byte [] autogenerated_decrypt;
  47. static MachineKeyConfig ()
  48. {
  49. autogenerated = new byte [64];
  50. RandomNumberGenerator rng = RandomNumberGenerator.Create ();
  51. rng.GetBytes (autogenerated);
  52. autogenerated_decrypt = new byte [64];
  53. rng.GetBytes (autogenerated_decrypt);
  54. }
  55. internal MachineKeyConfig (object parent)
  56. {
  57. if (parent is MachineKeyConfig) {
  58. MachineKeyConfig p = (MachineKeyConfig) parent;
  59. validation_key = p.validation_key;
  60. decryption_key = p.decryption_key;
  61. validation_type = p.validation_type;
  62. }
  63. }
  64. static byte ToHexValue (char c, bool high)
  65. {
  66. byte v;
  67. if (c >= '0' && c <= '9')
  68. v = (byte) (c - '0');
  69. else if (c >= 'a' && c <= 'f')
  70. v = (byte) (c - 'a' + 10);
  71. else if (c >= 'A' && c <= 'F')
  72. v = (byte) (c - 'A' + 10);
  73. else
  74. throw new ArgumentException ("Invalid hex character");
  75. if (high)
  76. v <<= 4;
  77. return v;
  78. }
  79. internal static byte [] GetBytes (string key, int len)
  80. {
  81. byte [] result = new byte [len / 2];
  82. for (int i = 0; i < len; i += 2)
  83. result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
  84. return result;
  85. }
  86. static byte [] MakeKey (string key, bool decryption) //, out bool isolate)
  87. {
  88. if (key == null || key.StartsWith ("AutoGenerate")){
  89. //isolate = key.IndexOf ("IsolateApps") != 1;
  90. return (decryption) ? autogenerated_decrypt : autogenerated;
  91. }
  92. //isolate = false;
  93. int len = key.Length;
  94. if (len < 40 || len > 128 || (len % 2) == 1)
  95. throw new ArgumentException ("Invalid key length");
  96. return GetBytes (key, len);
  97. }
  98. internal void SetValidationKey (string n)
  99. {
  100. validation_key = MakeKey (n, false); //, out isolate_validation);
  101. }
  102. internal byte [] ValidationKey {
  103. get { return validation_key; }
  104. }
  105. internal void SetDecryptionKey (string n)
  106. {
  107. decryption_key = MakeKey (n, true); //, out isolate_decryption);
  108. decryption_key_192bits = new byte [24];
  109. int count = 24;
  110. if (decryption_key.Length < 24)
  111. count = decryption_key.Length;
  112. Buffer.BlockCopy (decryption_key, 0, decryption_key_192bits, 0, count);
  113. }
  114. internal byte [] DecryptionKey {
  115. get { return decryption_key; }
  116. }
  117. internal byte [] DecryptionKey192Bits {
  118. get { return decryption_key_192bits; }
  119. }
  120. internal MachineKeyValidation ValidationType {
  121. get {
  122. return validation_type;
  123. }
  124. set {
  125. validation_type = value;
  126. }
  127. }
  128. }
  129. }