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