2 // Unit tests for MachineKeySectionUtils (internals)
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Web.Configuration;
32 using System.Web.Util;
33 using NUnit.Framework;
35 namespace MonoTests.System.Web.Util {
38 public class MachineKeySectionUtilsTest {
40 static byte ChangeByte (byte b)
42 return (b == Byte.MaxValue) ? Byte.MinValue : (byte) (b + 1);
45 public void Encrypt_RoundTrip (MachineKeySection section)
47 byte [] data = new byte [14];
48 byte [] encdata = MachineKeySectionUtils.Encrypt (section, data);
49 byte [] decdata = MachineKeySectionUtils.Decrypt (section, encdata);
50 Assert.AreEqual (data, decdata, "roundtrip");
52 // changing length (missing first byte)
53 byte [] cut = new byte [encdata.Length - 1];
54 Array.Copy (encdata, 1, cut, 0, cut.Length);
55 Assert.IsNull (MachineKeySectionUtils.Decrypt (section, cut), "bad length");
57 // changing last byte (padding)
58 byte be = encdata [encdata.Length - 1];
59 encdata [encdata.Length - 1] = ChangeByte (be);
60 byte[] result = MachineKeySectionUtils.Decrypt (section, encdata);
61 // this will return null if a bad padding is detected - OTOH since we're using a random key and we
62 // encrypt a random IV it's possible the decrypted stuff will randomly have a "valid" padding (there's
63 // only so much possible values and the bots runs those tests pretty often and give false positive)
64 // To avoid this we fallback to ensure the data is invalid (if should be empty)
67 for (int i=0; i < result.Length; i++)
70 Assert.IsTrue (result == null || total != 0, "bad padding");
74 public void Encrypt_RoundTrip_Default ()
76 Encrypt_RoundTrip (new MachineKeySection ());
80 public void Encrypt_RoundTrip_AES ()
82 MachineKeySection section = new MachineKeySection ();
83 section.Validation = MachineKeyValidation.AES;
84 Encrypt_RoundTrip (section);
88 public void Encrypt_RoundTrip_TripleDES ()
90 MachineKeySection section = new MachineKeySection ();
91 section.Validation = MachineKeyValidation.TripleDES;
92 Encrypt_RoundTrip (section);
96 public void Encrypt_RoundTrip_MD5 ()
98 MachineKeySection section = new MachineKeySection ();
99 section.Validation = MachineKeyValidation.MD5;
100 Encrypt_RoundTrip (section);
104 public void Encrypt_RoundTrip_SHA1 ()
106 MachineKeySection section = new MachineKeySection ();
107 section.Validation = MachineKeyValidation.SHA1;
108 Encrypt_RoundTrip (section);
112 public void Encrypt_RoundTrip_HMACSHA256 ()
114 MachineKeySection section = new MachineKeySection ();
115 section.Validation = MachineKeyValidation.HMACSHA256;
116 EncryptSign_RoundTrip (section);
120 public void Encrypt_RoundTrip_HMACSHA384 ()
122 MachineKeySection section = new MachineKeySection ();
123 section.Validation = MachineKeyValidation.HMACSHA384;
124 EncryptSign_RoundTrip (section);
128 public void Encrypt_RoundTrip_HMACSHA512 ()
130 MachineKeySection section = new MachineKeySection ();
131 section.Validation = MachineKeyValidation.HMACSHA512;
132 EncryptSign_RoundTrip (section);
136 public void Encrypt_RoundTrip_Custom_RIPEMD160 ()
138 MachineKeySection section = new MachineKeySection ();
139 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
140 EncryptSign_RoundTrip (section);
143 public void EncryptSign_RoundTrip (MachineKeySection section)
145 byte [] data = new byte [14];
146 byte [] block = MachineKeySectionUtils.EncryptSign (section, data);
147 byte [] decdata = MachineKeySectionUtils.VerifyDecrypt (section, block);
148 Assert.AreEqual (data, decdata, "roundtrip");
150 // changing a byte of the data
152 block [0] = ChangeByte (b0);
153 Assert.IsNull (MachineKeySectionUtils.VerifyDecrypt (section, block), "bad data");
156 // changing a byte of the signature
157 byte be = block [block.Length - 1];
158 block [block.Length - 1] = ChangeByte (be);
159 Assert.IsNull (MachineKeySectionUtils.VerifyDecrypt (section, block), "bad signature");
163 public void EncryptSign_RoundTrip_Default ()
165 EncryptSign_RoundTrip (new MachineKeySection ());
169 public void EncryptSign_RoundTrip_AES ()
171 MachineKeySection section = new MachineKeySection ();
172 section.Validation = MachineKeyValidation.AES;
173 EncryptSign_RoundTrip (section);
177 public void EncryptSign_RoundTrip_TripleDES ()
179 MachineKeySection section = new MachineKeySection ();
180 section.Validation = MachineKeyValidation.TripleDES;
181 EncryptSign_RoundTrip (section);
185 public void EncryptSign_RoundTrip_MD5 ()
187 MachineKeySection section = new MachineKeySection ();
188 section.Validation = MachineKeyValidation.MD5;
189 EncryptSign_RoundTrip (section);
193 public void EncryptSign_RoundTrip_SHA1 ()
195 MachineKeySection section = new MachineKeySection ();
196 section.Validation = MachineKeyValidation.SHA1;
197 EncryptSign_RoundTrip (section);
201 public void EncryptSign_RoundTrip_HMACSHA256 ()
203 MachineKeySection section = new MachineKeySection ();
204 section.Validation = MachineKeyValidation.HMACSHA256;
205 EncryptSign_RoundTrip (section);
209 public void EncryptSign_RoundTrip_HMACSHA384 ()
211 MachineKeySection section = new MachineKeySection ();
212 section.Validation = MachineKeyValidation.HMACSHA384;
213 EncryptSign_RoundTrip (section);
217 public void EncryptSign_RoundTrip_HMACSHA512 ()
219 MachineKeySection section = new MachineKeySection ();
220 section.Validation = MachineKeyValidation.HMACSHA512;
221 EncryptSign_RoundTrip (section);
225 public void EncryptSign_RoundTrip_Custom_RIPEMD160 ()
227 MachineKeySection section = new MachineKeySection ();
228 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
229 EncryptSign_RoundTrip (section);
232 public void Validation_RoundTrip (MachineKeySection section)
234 byte [] data = new byte [] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
235 byte [] block = MachineKeySectionUtils.Sign (section, data);
236 Assert.AreEqual (data, MachineKeySectionUtils.Verify (section, block), "OK");
238 // changing last byte
239 for (int i = 0; i < data.Length; i++) {
241 block [i] = ChangeByte (b);
242 Assert.IsNull (MachineKeySectionUtils.Verify (section, block), "bad-" + i.ToString ());
248 public void Validation_RoundTrip_Default ()
250 Validation_RoundTrip (new MachineKeySection ());
254 public void Validation_RoundTrip_AES ()
256 MachineKeySection section = new MachineKeySection ();
257 section.Validation = MachineKeyValidation.AES;
258 Validation_RoundTrip (section);
262 public void Validation_RoundTrip_TripleDES ()
264 MachineKeySection section = new MachineKeySection ();
265 section.Validation = MachineKeyValidation.TripleDES;
266 Validation_RoundTrip (section);
270 public void Validation_RoundTrip_MD5 ()
272 MachineKeySection section = new MachineKeySection ();
273 section.Validation = MachineKeyValidation.MD5;
274 Validation_RoundTrip (section);
278 public void Validation_RoundTrip_SHA1 ()
280 MachineKeySection section = new MachineKeySection ();
281 section.Validation = MachineKeyValidation.SHA1;
282 Validation_RoundTrip (section);
287 public void Validation_RoundTrip_HMACSHA256 ()
289 MachineKeySection section = new MachineKeySection ();
290 section.Validation = MachineKeyValidation.HMACSHA256;
291 Validation_RoundTrip (section);
295 public void Validation_RoundTrip_HMACSHA384 ()
297 MachineKeySection section = new MachineKeySection ();
298 section.Validation = MachineKeyValidation.HMACSHA384;
299 Validation_RoundTrip (section);
303 public void Validation_RoundTrip_HMACSHA512 ()
305 MachineKeySection section = new MachineKeySection ();
306 section.Validation = MachineKeyValidation.HMACSHA512;
307 Validation_RoundTrip (section);
311 public void Validation_RoundTrip_Custom_RIPEMD160 ()
313 MachineKeySection section = new MachineKeySection ();
314 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
315 Validation_RoundTrip (section);
319 public void GetHexString ()
321 Assert.AreEqual ("DEADC0DE", MachineKeySectionUtils.GetHexString (new byte [] { 0xde, 0xad, 0xc0, 0xde }), "deadcode");