1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
//
// System.Web.Util.MachineKeySectionUtils
//
// Authors:
// Chris Toshok (toshok@ximian.com)
// Sebastien Pouliot <sebastien@ximian.com>
//
// (c) Copyright 2005, 2010 Novell, Inc (http://www.novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.ComponentModel;
using System.Configuration;
using System.Configuration.Provider;
using System.Security.Cryptography;
using System.Text;
using System.Web.Configuration;
#if NET_2_0
namespace System.Web.Util {
static class MachineKeySectionUtils {
static byte ToHexValue (char c, bool high)
{
byte v;
if (c >= '0' && c <= '9')
v = (byte) (c - '0');
else if (c >= 'a' && c <= 'f')
v = (byte) (c - 'a' + 10);
else if (c >= 'A' && c <= 'F')
v = (byte) (c - 'A' + 10);
else
throw new ArgumentException ("Invalid hex character");
if (high)
v <<= 4;
return v;
}
internal static byte [] GetBytes (string key, int len)
{
byte [] result = new byte [len / 2];
for (int i = 0; i < len; i += 2)
result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));
return result;
}
static public string GetHexString (byte [] bytes)
{
StringBuilder sb = new StringBuilder (bytes.Length * 2);
int letterPart = 55;
const int numberPart = 48;
for (int i = 0; i < bytes.Length; i++) {
int tmp = (int) bytes [i];
int second = tmp & 15;
int first = (tmp >> 4) & 15;
sb.Append ((char) (first > 9 ? letterPart + first : numberPart + first));
sb.Append ((char) (second > 9 ? letterPart + second : numberPart + second));
}
return sb.ToString ();
}
// decryption="Auto" [Auto | DES | 3DES | AES | alg:algorithm_name]
// http://msdn.microsoft.com/en-us/library/w8h3skw9.aspx
public static SymmetricAlgorithm GetDecryptionAlgorithm (string name)
{
SymmetricAlgorithm sa = null;
switch (name) {
case "AES":
case "Auto":
sa = Rijndael.Create ();
break;
case "DES":
sa = DES.Create ();
break;
case "3DES":
sa = TripleDES.Create ();
break;
default:
#if NET_4_0
if (name.StartsWith ("alg:")) {
sa = SymmetricAlgorithm.Create (name.Substring (4));
break;
}
#endif
throw new ConfigurationErrorsException ();
}
return sa;
}
// validation="HMACSHA256" [SHA1 | MD5 | 3DES | AES | HMACSHA256 | HMACSHA384 | HMACSHA512 | alg:algorithm_name]
// [1] http://msdn.microsoft.com/en-us/library/system.web.configuration.machinekeyvalidation.aspx
// [2] http://msdn.microsoft.com/en-us/library/w8h3skw9.aspx
public static KeyedHashAlgorithm GetValidationAlgorithm (MachineKeySection section)
{
KeyedHashAlgorithm kha = null;
switch (section.Validation) {
case MachineKeyValidation.MD5:
kha = new HMACMD5 ();
break;
case MachineKeyValidation.AES: // see link [1] or [2]
case MachineKeyValidation.TripleDES: // see link [2]
case MachineKeyValidation.SHA1:
kha = new HMACSHA1 ();
break;
#if NET_4_0
case MachineKeyValidation.HMACSHA256:
kha = new HMACSHA256 ();
break;
case MachineKeyValidation.HMACSHA384:
kha = new HMACSHA384 ();
break;
case MachineKeyValidation.HMACSHA512:
kha = new HMACSHA512 ();
break;
case MachineKeyValidation.Custom:
// remove the "alg:" from the start of the string
string algo = section.ValidationAlgorithm;
if (algo.StartsWith ("alg:"))
kha = KeyedHashAlgorithm.Create (algo.Substring (4));
break;
#endif
}
return kha;
}
// helpers to ease unit testing of the cryptographic code
#if TEST
static byte [] decryption_key;
static byte [] validation_key;
static SymmetricAlgorithm GetDecryptionAlgorithm (MachineKeySection section)
{
return GetDecryptionAlgorithm (section.Decryption);
}
static byte [] GetDecryptionKey (MachineKeySection section)
{
if (decryption_key == null)
decryption_key = GetDecryptionAlgorithm (section).Key;
return decryption_key;
}
static byte [] GetValidationKey (MachineKeySection section)
{
if (validation_key == null)
validation_key = GetValidationAlgorithm (section).Key;
return validation_key;
}
#else
static SymmetricAlgorithm GetDecryptionAlgorithm (MachineKeySection section)
{
return section.GetDecryptionAlgorithm ();
}
static byte[] GetDecryptionKey (MachineKeySection section)
{
return section.GetDecryptionKey ();
}
public static byte [] GetValidationKey (MachineKeySection section)
{
return section.GetValidationKey ();
}
#endif
static public byte [] Decrypt (MachineKeySection section, byte [] encodedData)
{
return Decrypt (section, encodedData, 0, encodedData.Length);
}
static byte [] Decrypt (MachineKeySection section, byte [] encodedData, int offset, int length)
{
using (SymmetricAlgorithm sa = GetDecryptionAlgorithm (section)) {
sa.Key = GetDecryptionKey (section);
return Decrypt (sa, encodedData, offset, length);
}
}
static public byte [] Decrypt (SymmetricAlgorithm alg, byte [] encodedData, int offset, int length)
{
// alg.IV is randomly set (default behavior) and perfect for our needs
// iv is the first part of the encodedPassword
byte [] iv = new byte [alg.IV.Length];
Array.Copy (encodedData, 0, iv, 0, iv.Length);
using (ICryptoTransform decryptor = alg.CreateDecryptor (alg.Key, iv)) {
try {
return decryptor.TransformFinalBlock (encodedData, iv.Length + offset, length - iv.Length);
}
catch (CryptographicException) {
return null;
}
}
}
static public byte [] Encrypt (MachineKeySection section, byte [] data)
{
using (SymmetricAlgorithm sa = GetDecryptionAlgorithm (section)) {
sa.Key = GetDecryptionKey (section);
return Encrypt (sa, data);
}
}
static public byte [] Encrypt (SymmetricAlgorithm alg, byte [] data)
{
// alg.IV is randomly set (default behavior) and perfect for our needs
byte [] iv = alg.IV;
using (ICryptoTransform encryptor = alg.CreateEncryptor (alg.Key, iv)) {
byte [] encrypted = encryptor.TransformFinalBlock (data, 0, data.Length);
byte [] output = new byte [iv.Length + encrypted.Length];
// note: the IV can be public, however it should not be based on the password
Array.Copy (iv, 0, output, 0, iv.Length);
Array.Copy (encrypted, 0, output, iv.Length, encrypted.Length);
return output;
}
}
// in [data]
// return [data][signature]
public static byte [] Sign (MachineKeySection section, byte [] data)
{
return Sign (section, data, 0, data.Length);
}
static byte [] Sign (MachineKeySection section, byte [] data, int offset, int length)
{
using (KeyedHashAlgorithm kha = GetValidationAlgorithm (section)) {
kha.Key = GetValidationKey (section);
byte [] signature = kha.ComputeHash (data, offset, length);
byte [] block = new byte [length + signature.Length];
Array.Copy (data, block, length);
Array.Copy (signature, 0, block, length, signature.Length);
return block;
}
}
public static byte [] Verify (MachineKeySection section, byte [] data)
{
byte [] unsigned_data = null;
bool valid = true;
using (KeyedHashAlgorithm kha = GetValidationAlgorithm (section)) {
kha.Key = GetValidationKey (section);
int signlen = kha.HashSize >> 3; // bits to bytes
byte [] signature = Sign (section, data, 0, data.Length - signlen);
for (int i = 0; i < signature.Length; i++) {
if (signature [i] != data [data.Length - signature.Length + i])
valid = false; // do not return (timing attack)
}
unsigned_data = new byte [data.Length - signlen];
Array.Copy (data, 0, unsigned_data, 0, unsigned_data.Length);
}
return valid ? unsigned_data : null;
}
// do NOT sign then encrypt
public static byte [] EncryptSign (MachineKeySection section, byte [] data)
{
byte [] encdata = Encrypt (section, data);
return Sign (section, encdata);
}
// note: take no shortcut (timing attack) while verifying or decrypting
public static byte [] VerifyDecrypt (MachineKeySection section, byte [] block)
{
bool valid = true;
int signlen;
using (KeyedHashAlgorithm kha = GetValidationAlgorithm (section)) {
kha.Key = GetValidationKey (section);
signlen = kha.HashSize >> 3; // bits to bytes
byte [] signature = Sign (section, block, 0, block.Length - signlen);
for (int i = 0; i < signature.Length; i++) {
if (signature [i] != block [block.Length - signature.Length + i])
valid = false; // do not return (timing attack)
}
}
// whatever the signature continue with decryption
try {
byte [] decdata = Decrypt (section, block, 0, block.Length - signlen);
return valid ? decdata : null;
}
catch {
return null;
}
}
}
}
#endif
|