MachineKeySection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // System.Web.Configuration.MachineKeySection
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (c) Copyright 2005, 2010 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. #if NET_2_0
  31. using System;
  32. using System.ComponentModel;
  33. using System.Configuration;
  34. using System.Security.Cryptography;
  35. namespace System.Web.Configuration {
  36. public sealed class MachineKeySection : ConfigurationSection
  37. {
  38. static ConfigurationProperty decryptionProp;
  39. static ConfigurationProperty decryptionKeyProp;
  40. static ConfigurationProperty validationProp;
  41. static ConfigurationProperty validationKeyProp;
  42. static ConfigurationPropertyCollection properties;
  43. static MachineKeyValidationConverter converter = new MachineKeyValidationConverter ();
  44. #if NET_4_0
  45. MachineKeyValidation validation;
  46. #endif
  47. static MachineKeySection ()
  48. {
  49. decryptionProp = new ConfigurationProperty ("decryption", typeof (string), "Auto",
  50. PropertyHelper.WhiteSpaceTrimStringConverter,
  51. PropertyHelper.NonEmptyStringValidator,
  52. ConfigurationPropertyOptions.None);
  53. decryptionKeyProp = new ConfigurationProperty ("decryptionKey", typeof (string), "AutoGenerate,IsolateApps",
  54. PropertyHelper.WhiteSpaceTrimStringConverter,
  55. PropertyHelper.NonEmptyStringValidator,
  56. ConfigurationPropertyOptions.None);
  57. #if NET_4_0
  58. validationProp = new ConfigurationProperty ("validation", typeof (string), "HMACSHA256",
  59. PropertyHelper.WhiteSpaceTrimStringConverter,
  60. PropertyHelper.NonEmptyStringValidator,
  61. ConfigurationPropertyOptions.None);
  62. #else
  63. validationProp = new ConfigurationProperty ("validation", typeof (MachineKeyValidation),
  64. MachineKeyValidation.SHA1, converter,
  65. PropertyHelper.DefaultValidator,
  66. ConfigurationPropertyOptions.None);
  67. #endif
  68. validationKeyProp = new ConfigurationProperty ("validationKey", typeof (string), "AutoGenerate,IsolateApps",
  69. PropertyHelper.WhiteSpaceTrimStringConverter,
  70. PropertyHelper.NonEmptyStringValidator,
  71. ConfigurationPropertyOptions.None);
  72. properties = new ConfigurationPropertyCollection ();
  73. properties.Add (decryptionProp);
  74. properties.Add (decryptionKeyProp);
  75. properties.Add (validationProp);
  76. properties.Add (validationKeyProp);
  77. Config.AutoGenerate (MachineKeyRegistryStorage.KeyType.Encryption);
  78. Config.AutoGenerate (MachineKeyRegistryStorage.KeyType.Validation);
  79. }
  80. #if NET_4_0
  81. public MachineKeySection ()
  82. {
  83. // get DefaultValue from ValidationAlgorithm
  84. validation = (MachineKeyValidation) converter.ConvertFrom (null, null, ValidationAlgorithm);
  85. }
  86. [MonoTODO]
  87. public MachineKeyCompatibilityMode CompatibilityMode {
  88. get; set;
  89. }
  90. #endif
  91. protected override void Reset (ConfigurationElement parentElement)
  92. {
  93. base.Reset (parentElement);
  94. decryption_key = null;
  95. validation_key = null;
  96. decryption_template = null;
  97. validation_template = null;
  98. }
  99. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  100. [StringValidator (MinLength = 1)]
  101. [ConfigurationProperty ("decryption", DefaultValue = "Auto")]
  102. public string Decryption {
  103. get { return (string) base [decryptionProp];}
  104. set {
  105. decryption_template = MachineKeySectionUtils.GetDecryptionAlgorithm (value);
  106. base[decryptionProp] = value;
  107. }
  108. }
  109. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  110. [StringValidator (MinLength = 1)]
  111. [ConfigurationProperty ("decryptionKey", DefaultValue = "AutoGenerate,IsolateApps")]
  112. public string DecryptionKey {
  113. get { return (string) base [decryptionKeyProp];}
  114. set {
  115. base[decryptionKeyProp] = value;
  116. // SetDecryptionKey (value);
  117. }
  118. }
  119. #if NET_4_0
  120. // property exists for backward compatibility
  121. public MachineKeyValidation Validation {
  122. get { return validation; }
  123. set {
  124. if (value == MachineKeyValidation.Custom)
  125. throw new ArgumentException ();
  126. // ValidationAlgorithm = value.ToString ();
  127. }
  128. }
  129. [StringValidator (MinLength = 1)]
  130. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  131. [ConfigurationProperty ("validation", DefaultValue = "HMACSHA256")]
  132. public string ValidationAlgorithm {
  133. get { return (string) base [validationProp];}
  134. set {
  135. if (value == null)
  136. return;
  137. if (value.StartsWith ("alg:"))
  138. validation = MachineKeyValidation.Custom;
  139. else
  140. validation = (MachineKeyValidation) converter.ConvertFrom (null, null, value);
  141. base[validationProp] = value;
  142. }
  143. }
  144. #else
  145. [TypeConverter (typeof (MachineKeyValidationConverter))]
  146. [ConfigurationProperty ("validation", DefaultValue = "SHA1")]
  147. public MachineKeyValidation Validation {
  148. get { return (MachineKeyValidation) base [validationProp];}
  149. set { base[validationProp] = value; }
  150. }
  151. #endif
  152. [TypeConverter (typeof (WhiteSpaceTrimStringConverter))]
  153. [StringValidator (MinLength = 1)]
  154. [ConfigurationProperty ("validationKey", DefaultValue = "AutoGenerate,IsolateApps")]
  155. public string ValidationKey {
  156. get { return (string) base [validationKeyProp];}
  157. set {
  158. base[validationKeyProp] = value;
  159. // SetValidationKey (value);
  160. }
  161. }
  162. protected override ConfigurationPropertyCollection Properties {
  163. get { return properties; }
  164. }
  165. internal static MachineKeySection Config {
  166. get { return WebConfigurationManager.GetSection ("system.web/machineKey") as MachineKeySection; }
  167. }
  168. private byte[] decryption_key;
  169. private byte[] validation_key;
  170. private SymmetricAlgorithm decryption_template;
  171. private KeyedHashAlgorithm validation_template;
  172. internal SymmetricAlgorithm GetDecryptionAlgorithm ()
  173. {
  174. // code location to help with unit testing the code
  175. return MachineKeySectionUtils.GetDecryptionAlgorithm (Decryption);
  176. }
  177. // not to be reused outside algorithm and key validation purpose
  178. private SymmetricAlgorithm DecryptionTemplate {
  179. get {
  180. if (decryption_template == null)
  181. decryption_template = GetDecryptionAlgorithm ();
  182. return decryption_template;
  183. }
  184. }
  185. internal byte [] GetDecryptionKey ()
  186. {
  187. if (decryption_key == null)
  188. SetDecryptionKey (DecryptionKey);
  189. return decryption_key;
  190. }
  191. void SetDecryptionKey (string key)
  192. {
  193. if ((key == null) || key.StartsWith ("AutoGenerate")) {
  194. decryption_key = AutoGenerate (MachineKeyRegistryStorage.KeyType.Encryption);
  195. } else {
  196. try {
  197. decryption_key = MachineKeySectionUtils.GetBytes (key, key.Length);
  198. DecryptionTemplate.Key = decryption_key;
  199. }
  200. catch {
  201. decryption_key = null;
  202. throw new ArgumentException ("Invalid key length");
  203. }
  204. }
  205. }
  206. internal KeyedHashAlgorithm GetValidationAlgorithm ()
  207. {
  208. // code location to help with unit testing the code
  209. return MachineKeySectionUtils.GetValidationAlgorithm (this);
  210. }
  211. // not to be reused outside algorithm and key validation purpose
  212. private KeyedHashAlgorithm ValidationTemplate {
  213. get {
  214. if (validation_template == null)
  215. validation_template = GetValidationAlgorithm ();
  216. return validation_template;
  217. }
  218. }
  219. internal byte [] GetValidationKey ()
  220. {
  221. if (validation_key == null)
  222. SetValidationKey (ValidationKey);
  223. return validation_key;
  224. }
  225. // key can be expended for HMAC - i.e. a small key, e.g. 32 bytes, is still accepted as valid
  226. // the HMAC class already deals with keys larger than what it can use (digested to right size)
  227. void SetValidationKey (string key)
  228. {
  229. if ((key == null) || key.StartsWith ("AutoGenerate")) {
  230. validation_key = AutoGenerate (MachineKeyRegistryStorage.KeyType.Validation);
  231. } else {
  232. try {
  233. validation_key = MachineKeySectionUtils.GetBytes (key, key.Length);
  234. ValidationTemplate.Key = validation_key;
  235. }
  236. catch (CryptographicException) {
  237. // second chance, use the key length that the HMAC really wants
  238. try {
  239. byte[] expanded_key = new byte [ValidationTemplate.Key.Length];
  240. Array.Copy (validation_key, 0, expanded_key, 0, validation_key.Length);
  241. ValidationTemplate.Key = expanded_key;
  242. validation_key = expanded_key;
  243. }
  244. catch {
  245. validation_key = null;
  246. throw new ArgumentException ("Invalid key length");
  247. }
  248. }
  249. }
  250. }
  251. byte[] AutoGenerate (MachineKeyRegistryStorage.KeyType type)
  252. {
  253. byte[] key = null;
  254. #if TARGET_J2EE
  255. {
  256. #else
  257. try {
  258. key = MachineKeyRegistryStorage.Retrieve (type);
  259. // ensure the stored key is usable with the selection algorithm
  260. if (type == MachineKeyRegistryStorage.KeyType.Encryption)
  261. DecryptionTemplate.Key = key;
  262. else if (type == MachineKeyRegistryStorage.KeyType.Validation)
  263. ValidationTemplate.Key = key;
  264. } catch (Exception) {
  265. key = null;
  266. }
  267. #endif
  268. // some algorithms have special needs for key (e.g. length, parity, weak keys...)
  269. // so we better ask them to provide a default key (than to generate/use bad ones)
  270. if (key == null) {
  271. if (type == MachineKeyRegistryStorage.KeyType.Encryption)
  272. key = DecryptionTemplate.Key;
  273. else if (type == MachineKeyRegistryStorage.KeyType.Validation)
  274. key = ValidationTemplate.Key;
  275. #if !TARGET_J2EE
  276. MachineKeyRegistryStorage.Store (key, type);
  277. #endif
  278. }
  279. return key;
  280. }
  281. }
  282. }
  283. #endif