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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
//-----------------------------------------------------------------------
// <copyright file="MachineKeyUtil.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth {
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;
/// <summary>
/// Provides helpers that mimic the ASP.NET 4.5 MachineKey.Protect / Unprotect APIs,
/// even when running on ASP.NET 4.0. Consumers are expected to follow the same
/// conventions used by the MachineKey.Protect / Unprotect APIs (consult MSDN docs
/// for how these are meant to be used). Additionally, since this helper class
/// dynamically switches between the two based on whether the current application is
/// .NET 4.0 or 4.5, consumers should never persist output from the Protect method
/// since the implementation will change when upgrading 4.0 -> 4.5. This should be
/// used for transient data only.
/// </summary>
internal static class MachineKeyUtil {
/// <summary>
/// MachineKey implementation depending on the target .NET framework version
/// </summary>
private static readonly IMachineKey MachineKeyImpl = GetMachineKeyImpl();
/// <summary>
/// ProtectUnprotect delegate.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="purposes">The purposes.</param>
/// <returns>Result of either Protect or Unprotect methods.</returns>
private delegate byte[] ProtectUnprotect(byte[] data, string[] purposes);
/// <summary>
/// Abstract the MachineKey implementation in .NET 4.0 and 4.5
/// </summary>
private interface IMachineKey {
/// <summary>
/// Protects the specified user data.
/// </summary>
/// <param name="userData">The user data.</param>
/// <param name="purposes">The purposes.</param>
/// <returns>The protected data.</returns>
byte[] Protect(byte[] userData, string[] purposes);
/// <summary>
/// Unprotects the specified protected data.
/// </summary>
/// <param name="protectedData">The protected data.</param>
/// <param name="purposes">The purposes.</param>
/// <returns>The unprotected data.</returns>
byte[] Unprotect(byte[] protectedData, string[] purposes);
}
/// <summary>
/// Protects the specified user data.
/// </summary>
/// <param name="userData">The user data.</param>
/// <param name="purposes">The purposes.</param>
/// <returns>The encrypted data</returns>
public static byte[] Protect(byte[] userData, params string[] purposes) {
return MachineKeyImpl.Protect(userData, purposes);
}
/// <summary>
/// Unprotects the specified protected data.
/// </summary>
/// <param name="protectedData">The protected data.</param>
/// <param name="purposes">The purposes.</param>
/// <returns>The unencrypted data</returns>
public static byte[] Unprotect(byte[] protectedData, params string[] purposes) {
return MachineKeyImpl.Unprotect(protectedData, purposes);
}
/// <summary>
/// Gets the machine key implementation based on the runtime framework version.
/// </summary>
/// <returns>The machine key implementation</returns>
private static IMachineKey GetMachineKeyImpl() {
// Late bind to the MachineKey.Protect / Unprotect methods only if <httpRuntime targetFramework="4.5" />.
// This helps ensure that round-tripping the payloads continues to work even if the application is
// deployed to a mixed 4.0 / 4.5 farm environment.
PropertyInfo targetFrameworkProperty = typeof(HttpRuntime).GetProperty("TargetFramework", typeof(Version));
Version targetFramework = (targetFrameworkProperty != null) ? targetFrameworkProperty.GetValue(null, null) as Version : null;
if (targetFramework != null && targetFramework >= new Version(4, 5)) {
ProtectUnprotect protectThunk = (ProtectUnprotect)Delegate.CreateDelegate(typeof(ProtectUnprotect), typeof(MachineKey), "Protect", ignoreCase: false, throwOnBindFailure: false);
ProtectUnprotect unprotectThunk = (ProtectUnprotect)Delegate.CreateDelegate(typeof(ProtectUnprotect), typeof(MachineKey), "Unprotect", ignoreCase: false, throwOnBindFailure: false);
if (protectThunk != null && unprotectThunk != null) {
return new MachineKey45(protectThunk, unprotectThunk); // ASP.NET 4.5
}
}
return new MachineKey40(); // ASP.NET 4.0
}
/// <summary>
/// On ASP.NET 4.0, we perform some transforms which mimic the behaviors of MachineKey.Protect
/// and Unprotect.
/// </summary>
private sealed class MachineKey40 : IMachineKey {
/// <summary>
/// This is the magic header that identifies a MachineKey40 payload.
/// It helps differentiate this from other encrypted payloads.</summary>
private const uint MagicHeader = 0x8519140c;
/// <summary>
/// The SHA-256 factory to be used.
/// </summary>
private static readonly Func<SHA256> sha256Factory = GetSHA256Factory();
/// <summary>
/// Protects the specified user data.
/// </summary>
/// <param name="userData">The user data.</param>
/// <param name="purposes">The purposes.</param>
/// <returns>The protected data</returns>
public byte[] Protect(byte[] userData, string[] purposes) {
if (userData == null) {
throw new ArgumentNullException("userData");
}
// dataWithHeader = {magic header} .. {purposes} .. {userData}
byte[] dataWithHeader = new byte[checked(4 /* magic header */ + (256 / 8) /* purposes */ + userData.Length)];
unchecked {
dataWithHeader[0] = (byte)(MagicHeader >> 24);
dataWithHeader[1] = (byte)(MagicHeader >> 16);
dataWithHeader[2] = (byte)(MagicHeader >> 8);
dataWithHeader[3] = (byte)MagicHeader;
}
byte[] purposeHash = ComputeSHA256(purposes);
Buffer.BlockCopy(purposeHash, 0, dataWithHeader, 4, purposeHash.Length);
Buffer.BlockCopy(userData, 0, dataWithHeader, 4 + (256 / 8), userData.Length);
// encrypt + sign
string hexValue = MachineKey.Encode(dataWithHeader, MachineKeyProtection.All);
// convert hex -> binary
byte[] binary = HexToBinary(hexValue);
return binary;
}
/// <summary>
/// Unprotects the specified protected data.
/// </summary>
/// <param name="protectedData">The protected data.</param>
/// <param name="purposes">The purposes.</param>
/// <returns>The unprotected data</returns>
public byte[] Unprotect(byte[] protectedData, string[] purposes) {
if (protectedData == null) {
throw new ArgumentNullException("protectedData");
}
// convert binary -> hex and calculate what the purpose should read
string hexEncodedData = BinaryToHex(protectedData);
byte[] purposeHash = ComputeSHA256(purposes);
try {
// decrypt / verify signature
byte[] dataWithHeader = MachineKey.Decode(hexEncodedData, MachineKeyProtection.All);
// validate magic header and purpose string
if (dataWithHeader != null
&& dataWithHeader.Length >= (4 + (256 / 8))
&& (uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(dataWithHeader, 0)) == MagicHeader
&& AreByteArraysEqual(new ArraySegment<byte>(purposeHash), new ArraySegment<byte>(dataWithHeader, 4, 256 / 8))) {
// validation succeeded
byte[] userData = new byte[dataWithHeader.Length - 4 - (256 / 8)];
Buffer.BlockCopy(dataWithHeader, 4 + (256 / 8), userData, 0, userData.Length);
return userData;
}
}
catch {
// swallow since will be rethrown immediately below
}
// if we reached this point, some cryptographic operation failed
throw new CryptographicException(Strings.Generic_CryptoFailure);
}
/// <summary>
/// Convert bytes to hex string.
/// </summary>
/// <param name="binary">The input array.</param>
/// <returns>Hex string</returns>
internal static string BinaryToHex(byte[] binary) {
StringBuilder builder = new StringBuilder(checked(binary.Length * 2));
for (int i = 0; i < binary.Length; i++) {
byte b = binary[i];
builder.Append(HexDigit(b >> 4));
builder.Append(HexDigit(b & 0x0F));
}
string result = builder.ToString();
return result;
}
/// <summary>
/// This method is specially written to take the same amount of time
/// regardless of where 'a' and 'b' differ. Please do not optimize it.</summary>
/// <param name="a">first array.</param>
/// <param name="b">second array.</param>
/// <returns><c href="true" /> if equal, others <c href="false" /></returns>
private static bool AreByteArraysEqual(ArraySegment<byte> a, ArraySegment<byte> b) {
if (a.Count != b.Count) {
return false;
}
bool areEqual = true;
for (int i = 0; i < a.Count; i++) {
areEqual &= a.Array[a.Offset + i] == b.Array[b.Offset + i];
}
return areEqual;
}
/// <summary>
/// Computes a SHA256 hash over all of the input parameters.
/// Each parameter is UTF8 encoded and preceded by a 7-bit encoded</summary>
/// integer describing the encoded byte length of the string.
/// <param name="parameters">The parameters.</param>
/// <returns>The output hash</returns>
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "MemoryStream is resilient to double-Dispose")]
private static byte[] ComputeSHA256(IList<string> parameters) {
using (MemoryStream ms = new MemoryStream()) {
using (BinaryWriter bw = new BinaryWriter(ms)) {
if (parameters != null) {
foreach (string parameter in parameters) {
bw.Write(parameter); // also writes the length as a prefix; unambiguous
}
bw.Flush();
}
using (SHA256 sha256 = sha256Factory()) {
byte[] retVal = sha256.ComputeHash(ms.GetBuffer(), 0, checked((int)ms.Length));
return retVal;
}
}
}
}
/// <summary>
/// Gets the SHA-256 factory.
/// </summary>
/// <returns>SHA256 factory</returns>
private static Func<SHA256> GetSHA256Factory() {
// Note: ASP.NET 4.5 always prefers CNG, but the CNG algorithms are not that
// performant on 4.0 and below. The following list is optimized for speed
// given our scenarios.
if (!CryptoConfig.AllowOnlyFipsAlgorithms) {
// This provider is not FIPS-compliant, so we can't use it if FIPS compliance
// is mandatory.
return () => new SHA256Managed();
}
try {
using (SHA256Cng sha256 = new SHA256Cng()) {
return () => new SHA256Cng();
}
}
catch (PlatformNotSupportedException) {
// CNG not supported (perhaps because we're not on Windows Vista or above); move on
}
// If all else fails, fall back to CAPI.
return () => new SHA256CryptoServiceProvider();
}
/// <summary>
/// Convert to hex character
/// </summary>
/// <param name="value">The value to be converted.</param>
/// <returns>Hex character</returns>
private static char HexDigit(int value) {
return (char)(value > 9 ? value + '7' : value + '0');
}
/// <summary>
/// Convert hdex string to bytes.
/// </summary>
/// <param name="hex">Input hex string.</param>
/// <returns>The bytes</returns>
private static byte[] HexToBinary(string hex) {
int size = hex.Length / 2;
byte[] bytes = new byte[size];
for (int idx = 0; idx < size; idx++) {
bytes[idx] = (byte)((HexValue(hex[idx * 2]) << 4) + HexValue(hex[(idx * 2) + 1]));
}
return bytes;
}
/// <summary>
/// Convert hex digit to byte.
/// </summary>
/// <param name="digit">The hex digit.</param>
/// <returns>The byte</returns>
private static int HexValue(char digit) {
return digit > '9' ? digit - '7' : digit - '0';
}
}
/// <summary>
/// On ASP.NET 4.5, we can just delegate to MachineKey.Protect and MachineKey.Unprotect directly,
/// which contain optimized code paths.
/// </summary>
private sealed class MachineKey45 : IMachineKey {
/// <summary>
/// Protect thunk
/// </summary>
private readonly ProtectUnprotect protectThunk;
/// <summary>
/// Unprotect thunk
/// </summary>
private readonly ProtectUnprotect unprotectThunk;
/// <summary>
/// Initializes a new instance of the <see cref="MachineKey45"/> class.
/// </summary>
/// <param name="protectThunk">The protect thunk.</param>
/// <param name="unprotectThunk">The unprotect thunk.</param>
public MachineKey45(ProtectUnprotect protectThunk, ProtectUnprotect unprotectThunk) {
this.protectThunk = protectThunk;
this.unprotectThunk = unprotectThunk;
}
/// <summary>
/// Protects the specified user data.
/// </summary>
/// <param name="userData">The user data.</param>
/// <param name="purposes">The purposes.</param>
/// <returns>The protected data</returns>
public byte[] Protect(byte[] userData, string[] purposes) {
return this.protectThunk(userData, purposes);
}
/// <summary>
/// Unprotects the specified protected data.
/// </summary>
/// <param name="protectedData">The protected data.</param>
/// <param name="purposes">The purposes.</param>
/// <returns>The unprotected data</returns>
public byte[] Unprotect(byte[] protectedData, string[] purposes) {
return this.unprotectThunk(protectedData, purposes);
}
}
}
}
|