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);
111 public void Encrypt_RoundTrip_HMACSHA256 ()
113 MachineKeySection section = new MachineKeySection ();
114 section.Validation = MachineKeyValidation.HMACSHA256;
115 EncryptSign_RoundTrip (section);
119 public void Encrypt_RoundTrip_HMACSHA384 ()
121 MachineKeySection section = new MachineKeySection ();
122 section.Validation = MachineKeyValidation.HMACSHA384;
123 EncryptSign_RoundTrip (section);
127 public void Encrypt_RoundTrip_HMACSHA512 ()
129 MachineKeySection section = new MachineKeySection ();
130 section.Validation = MachineKeyValidation.HMACSHA512;
131 EncryptSign_RoundTrip (section);
135 public void Encrypt_RoundTrip_Custom_RIPEMD160 ()
137 MachineKeySection section = new MachineKeySection ();
138 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
139 EncryptSign_RoundTrip (section);
141 public void EncryptSign_RoundTrip (MachineKeySection section)
143 byte [] data = new byte [14];
144 byte [] block = MachineKeySectionUtils.EncryptSign (section, data);
145 byte [] decdata = MachineKeySectionUtils.VerifyDecrypt (section, block);
146 Assert.AreEqual (data, decdata, "roundtrip");
148 // changing a byte of the data
150 block [0] = ChangeByte (b0);
151 Assert.IsNull (MachineKeySectionUtils.VerifyDecrypt (section, block), "bad data");
154 // changing a byte of the signature
155 byte be = block [block.Length - 1];
156 block [block.Length - 1] = ChangeByte (be);
157 Assert.IsNull (MachineKeySectionUtils.VerifyDecrypt (section, block), "bad signature");
161 public void EncryptSign_RoundTrip_Default ()
163 EncryptSign_RoundTrip (new MachineKeySection ());
167 public void EncryptSign_RoundTrip_AES ()
169 MachineKeySection section = new MachineKeySection ();
170 section.Validation = MachineKeyValidation.AES;
171 EncryptSign_RoundTrip (section);
175 public void EncryptSign_RoundTrip_TripleDES ()
177 MachineKeySection section = new MachineKeySection ();
178 section.Validation = MachineKeyValidation.TripleDES;
179 EncryptSign_RoundTrip (section);
183 public void EncryptSign_RoundTrip_MD5 ()
185 MachineKeySection section = new MachineKeySection ();
186 section.Validation = MachineKeyValidation.MD5;
187 EncryptSign_RoundTrip (section);
191 public void EncryptSign_RoundTrip_SHA1 ()
193 MachineKeySection section = new MachineKeySection ();
194 section.Validation = MachineKeyValidation.SHA1;
195 EncryptSign_RoundTrip (section);
198 public void EncryptSign_RoundTrip_HMACSHA256 ()
200 MachineKeySection section = new MachineKeySection ();
201 section.Validation = MachineKeyValidation.HMACSHA256;
202 EncryptSign_RoundTrip (section);
206 public void EncryptSign_RoundTrip_HMACSHA384 ()
208 MachineKeySection section = new MachineKeySection ();
209 section.Validation = MachineKeyValidation.HMACSHA384;
210 EncryptSign_RoundTrip (section);
214 public void EncryptSign_RoundTrip_HMACSHA512 ()
216 MachineKeySection section = new MachineKeySection ();
217 section.Validation = MachineKeyValidation.HMACSHA512;
218 EncryptSign_RoundTrip (section);
222 public void EncryptSign_RoundTrip_Custom_RIPEMD160 ()
224 MachineKeySection section = new MachineKeySection ();
225 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
226 EncryptSign_RoundTrip (section);
228 public void Validation_RoundTrip (MachineKeySection section)
230 byte [] data = new byte [] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
231 byte [] block = MachineKeySectionUtils.Sign (section, data);
232 Assert.AreEqual (data, MachineKeySectionUtils.Verify (section, block), "OK");
234 // changing last byte
235 for (int i = 0; i < data.Length; i++) {
237 block [i] = ChangeByte (b);
238 Assert.IsNull (MachineKeySectionUtils.Verify (section, block), "bad-" + i.ToString ());
244 public void Validation_RoundTrip_Default ()
246 Validation_RoundTrip (new MachineKeySection ());
250 public void Validation_RoundTrip_AES ()
252 MachineKeySection section = new MachineKeySection ();
253 section.Validation = MachineKeyValidation.AES;
254 Validation_RoundTrip (section);
258 public void Validation_RoundTrip_TripleDES ()
260 MachineKeySection section = new MachineKeySection ();
261 section.Validation = MachineKeyValidation.TripleDES;
262 Validation_RoundTrip (section);
266 public void Validation_RoundTrip_MD5 ()
268 MachineKeySection section = new MachineKeySection ();
269 section.Validation = MachineKeyValidation.MD5;
270 Validation_RoundTrip (section);
274 public void Validation_RoundTrip_SHA1 ()
276 MachineKeySection section = new MachineKeySection ();
277 section.Validation = MachineKeyValidation.SHA1;
278 Validation_RoundTrip (section);
282 public void Validation_RoundTrip_HMACSHA256 ()
284 MachineKeySection section = new MachineKeySection ();
285 section.Validation = MachineKeyValidation.HMACSHA256;
286 Validation_RoundTrip (section);
290 public void Validation_RoundTrip_HMACSHA384 ()
292 MachineKeySection section = new MachineKeySection ();
293 section.Validation = MachineKeyValidation.HMACSHA384;
294 Validation_RoundTrip (section);
298 public void Validation_RoundTrip_HMACSHA512 ()
300 MachineKeySection section = new MachineKeySection ();
301 section.Validation = MachineKeyValidation.HMACSHA512;
302 Validation_RoundTrip (section);
306 public void Validation_RoundTrip_Custom_RIPEMD160 ()
308 MachineKeySection section = new MachineKeySection ();
309 section.ValidationAlgorithm = "alg:HMACRIPEMD160";
310 Validation_RoundTrip (section);
313 public void GetHexString ()
315 Assert.AreEqual ("DEADC0DE", MachineKeySectionUtils.GetHexString (new byte [] { 0xde, 0xad, 0xc0, 0xde }), "deadcode");