This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / RSAPKCS1SignatureDeformatterTest.cs
1 //
2 // RSAPKCS1SignatureDeformatterTest.cs - NUnit tests for PKCS#1 v.1.5 signature.
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 using NUnit.Framework;
12 using System;
13 using System.Security.Cryptography;
14
15 namespace MonoTests.System.Security.Cryptography {
16
17         [TestFixture]
18         public class RSAPKCS1SignatureDeformatterTest : Assertion {
19
20                 private static byte[] shaSignature = { 0x51, 0xE1, 0x69, 0xC4, 0x84, 0x0C, 0x33, 0xD9, 0x80, 0xC0, 0xBD, 0x85, 0x87, 0x6E, 0x85, 0x91, 0xB9, 0xD5, 0xB6, 0xE1, 0xAB, 0xD3, 0x06, 0x83, 0xCF, 0x33, 0x56, 0xB9, 0xE6, 0x2C, 0x37, 0xC0, 0x08, 0xFC, 0x81, 0x15, 0xAB, 0x57, 0x80, 0xE4, 0xB9, 0x95, 0x4B, 0xFA, 0x63, 0x13, 0x5E, 0xA9, 0x6E, 0xAB, 0xB0, 0x89, 0xF3, 0xD0, 0xE9, 0xC7, 0xE7, 0xA0, 0xE2, 0xB6, 0x0A, 0xFF, 0x46, 0x2B, 0x8B, 0xC1, 0x4C, 0xEA, 0xDB, 0xEA, 0xD6, 0xF5, 0xA5, 0x2C, 0x8C, 0x1D, 0x57, 0xDF, 0x2D, 0xF0, 0x6B, 0x1D, 0xA9, 0xAE, 0x7F, 0x10, 0x02, 0xE2, 0x05, 0x7E, 0xD2, 0x80, 0xFC, 0x0E, 0x5A, 0xFD, 0xE9, 0xDB, 0x1B, 0xBA, 0xB4, 0xF7, 0x50, 0x88, 0x73, 0x95, 0xBD, 0x3C, 0xCB, 0x33, 0x02, 0xF5, 0x55, 0x10, 0xA6, 0x1B, 0xFD, 0x1D, 0xB1, 0x0E, 0xE3, 0xD0, 0xB7, 0x14, 0x8D, 0x45, 0xC4, 0xF3 };
21                 private static byte[] md5Signature = { 0xB4, 0xA9, 0xE9, 0x76, 0x04, 0x0E, 0x0E, 0x04, 0xA3, 0x68, 0x9E, 0x50, 0xD1, 0x29, 0x07, 0x22, 0x45, 0x41, 0x72, 0x1F, 0xBE, 0x74, 0x78, 0xDA, 0x5F, 0x22, 0x4B, 0x45, 0xA8, 0x5F, 0x2D, 0xA5, 0x5F, 0x01, 0x84, 0xA7, 0xF3, 0x6E, 0xB8, 0x8B, 0xF3, 0x29, 0xB2, 0x82, 0xE6, 0x5D, 0x1A, 0x98, 0xAE, 0x9C, 0x2E, 0xB0, 0xDD, 0x3F, 0x8D, 0xF9, 0x1C, 0x9E, 0x40, 0x25, 0x01, 0x9F, 0x92, 0x4E, 0xBE, 0x11, 0xE5, 0xE8, 0xE0, 0xF6, 0x3E, 0xDF, 0x8D, 0x1A, 0xC7, 0x26, 0x37, 0xF7, 0x01, 0x95, 0x48, 0xD8, 0x07, 0x4D, 0x0E, 0xDE, 0xB2, 0x76, 0xD1, 0x23, 0xBD, 0x74, 0xE9, 0xC3, 0x63, 0xB3, 0xE7, 0xCE, 0xA2, 0xEA, 0x20, 0x19, 0x1C, 0x4D, 0x8D, 0xBB, 0xAB, 0x6E, 0xB0, 0xD0, 0x08, 0xC2, 0x2B, 0x69, 0xA4, 0xF3, 0xE9, 0x23, 0xAC, 0x93, 0xB2, 0x0F, 0x90, 0x95, 0x6A, 0x66, 0xDC, 0x44 };
22
23                 private static RSA rsa;
24                 private static DSA dsa;
25
26                 [SetUp]
27                 public void SetUp () 
28                 {
29                         shaSignature [0] = 0x51;
30                         md5Signature [0] = 0xB4;
31
32                         if (rsa == null)
33                                 rsa = RSA.Create ();
34                         if (dsa == null)
35                                 dsa = DSA.Create ();
36                 }
37
38                 public void AssertEquals (string msg, byte[] array1, byte[] array2) 
39                 {
40                         AllTests.AssertEquals (msg, array1, array2);
41                 }
42
43                 [Test]
44                 public void RSAConstructors () 
45                 {
46                         RSAPKCS1SignatureDeformatter fmt;
47                         fmt = new RSAPKCS1SignatureDeformatter ();
48                         AssertNotNull ("RSAPKCS1SignatureDeformatter()", fmt);
49
50                         fmt = new RSAPKCS1SignatureDeformatter (null);
51                         AssertNotNull ("RSAPKCS1SignatureDeformatter(null)", fmt);
52
53                         fmt = new RSAPKCS1SignatureDeformatter (rsa);
54                         AssertNotNull ("RSAPKCS1SignatureDeformatter(rsa)", fmt);
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (InvalidCastException))]
59                 public void DSAConstructor () 
60                 {
61                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter (dsa);
62                 }
63
64                 [Test]
65                 public void SetRSAKey () 
66                 {
67                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
68                         fmt.SetKey (rsa);
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (InvalidCastException))]
73                 public void SetDSAKey () 
74                 {
75                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
76                         fmt.SetKey (dsa);
77                 }
78
79                 [Test]
80                 public void SetNullKey () 
81                 {
82                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
83                         fmt.SetKey (null);
84                 }
85
86                 [Test]
87                 [ExpectedException (typeof (ArgumentNullException))]
88                 public void SetNullHashAlgorithm () 
89                 {
90                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
91                         fmt.SetHashAlgorithm (null);
92                 }
93
94                 [Test]
95                 public void SetInvalidHashAlgorithm () 
96                 {
97                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
98                         fmt.SetHashAlgorithm ("MD3");
99                 }
100
101                 [Test]
102                 public void SetSHA1HashAlgorithm () 
103                 {
104                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
105                         fmt.SetHashAlgorithm ("SHA1");
106                 }
107
108                 [Test]
109                 public void SetMD5HashAlgorithm () 
110                 {
111                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
112                         fmt.SetHashAlgorithm ("MD5");
113                 }
114
115                 [Test]
116                 public void SetSHA256HashAlgorithm () 
117                 {
118                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
119                         fmt.SetHashAlgorithm ("SHA256");
120                 }
121
122                 [Test]
123                 public void SetSHA384HashAlgorithm () 
124                 {
125                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
126                         fmt.SetHashAlgorithm ("SHA384");
127                 }
128
129                 [Test]
130                 public void SetSHA512HashAlgorithm () 
131                 {
132                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
133                         fmt.SetHashAlgorithm ("SHA512");
134                 }
135
136                 [Test]
137                 [ExpectedException (typeof (ArgumentNullException))]
138                 public void VerifySignatureNullHash () 
139                 {
140                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
141                         fmt.SetHashAlgorithm ("SHA1");
142                         fmt.SetKey (rsa);
143                         byte[] hash = null;
144                         byte[] signature = new byte [128];
145                         fmt.VerifySignature (hash, signature);
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (ArgumentNullException))]
150                 public void VerifySignatureNullSignature ()
151                 {
152                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
153                         fmt.SetHashAlgorithm ("SHA1");
154                         fmt.SetKey (rsa);
155                         byte[] hash = new byte [20];
156                         fmt.VerifySignature (hash, null);
157                 }
158
159                 [Test]
160                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
161                 public void VerifySignatureWithBadHash () 
162                 {
163                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
164                         fmt.SetKey (rsa);
165                         // no hash algorithm
166                         byte[] hash = new byte [1];
167                         byte[] signature = new byte [1];
168                         fmt.VerifySignature (hash, signature);
169                 }
170
171                 [Test]
172 // v.1.0        [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
173 // v.1.1        [ExpectedException (typeof (NullReferenceException))]
174                 public void VerifySHA1SignatureWithNullKey () 
175                 {
176                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
177                         fmt.SetHashAlgorithm ("SHA1");
178                         byte[] hash = new byte [20];
179                         try {
180                                 // no key
181                                 fmt.VerifySignature (hash, shaSignature);
182                                 Fail ("VerifySHA1SignatureWithNullKey - Expected CryptographicUnexpectedOperationException but none");
183                         }
184                         catch (CryptographicUnexpectedOperationException) {
185                                 // this was expected
186                         }
187                         catch (NullReferenceException) {
188                                 // this wasn't expected - but that's the result from framework 1.1
189                         }
190                         catch (Exception e) {
191                                 Fail ("VerifySHA1SignatureWithNullKey - Expected CryptographicUnexpectedOperationException but got: " + e.ToString ());
192                         }
193                 }
194
195                 private RSAPKCS1SignatureDeformatter GetDefaultDeformatter (string hashName) 
196                 {
197                         // no need for the private key 
198                         RSA rsa = RSA.Create ();
199                         rsa.ImportParameters (AllTests.GetRsaKey (false));
200
201                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
202                         fmt.SetKey (rsa);
203                         fmt.SetHashAlgorithm (hashName);
204                         return fmt;
205                 }
206
207                 [Test]
208                 public void VerifySHA1SignatureWithRSAKey () 
209                 {
210                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("SHA1");
211                         // good SHA1
212                         byte[] hash = new byte [20];
213                         Assert ("VerifySignature(SHA1, sign)", fmt.VerifySignature (hash, shaSignature));
214                         // bad signature
215                         shaSignature [0] = (byte) ~shaSignature [0];
216                         Assert ("VerifySignature(SHA1, badSign)", !fmt.VerifySignature (hash, shaSignature));
217                 }
218
219                 [Test]
220                 [ExpectedException (typeof (CryptographicException))]
221                 public void VerifySHA1SignatureWithWrongHashLength () 
222                 {
223                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("SHA1");
224                         // wrong SHA1 length
225                         byte[] hash = new byte [19];
226                         fmt.VerifySignature (hash, shaSignature);
227                 }
228                         
229                 [Test]
230                 public void VerifySHA1SignatureWithWrongSignatureLength () 
231                 {
232                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("SHA1");
233                         // wrong signature length
234                         byte[] hash = new byte [20];
235                         byte[] badSignature = new byte [shaSignature.Length-1];
236                         Assert ("VerifySignature(SHA1, badSign)", !fmt.VerifySignature (hash, badSignature));
237                 }
238
239                 [Test]
240                 public void VerifyMD5SignatureWithRSAKey () 
241                 {
242                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("MD5");
243                         // good MD5
244                         byte[] hash = new byte [16];
245                         Assert ("VerifySignature(MD5, sign)", fmt.VerifySignature (hash, md5Signature));
246                         // bad signature
247                         md5Signature [0] = (byte) ~md5Signature [0];
248                         Assert ("VerifySignature(MD5, badSign)", !fmt.VerifySignature (hash, md5Signature));
249                 }
250
251                 [Test]
252                 [ExpectedException (typeof (CryptographicException))]
253                 public void VerifyMD5SignatureWithWrongHashLength () 
254                 {
255                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("MD5");
256                         // wrong MD5 length
257                         byte[] hash = new byte [17];
258                         fmt.VerifySignature (hash, md5Signature);
259                 }
260                         
261                 [Test]
262                 public void VerifyMD5SignatureWithWrongSignatureLength () 
263                 {
264                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("MD5");
265                         // wrong signature length
266                         byte[] hash = new byte [16];
267                         byte[] badSignature = new byte [md5Signature.Length-1];
268                         Assert ("VerifySignature(MD5, badSign)", !fmt.VerifySignature (hash, badSignature));
269                 }
270
271                 [Test]
272                 [ExpectedException (typeof (ArgumentNullException))]
273                 public void VerifySignatureNullHashAlgorithm () 
274                 {
275                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
276                         HashAlgorithm hash = null;
277                         byte[] data = new byte [20];
278                         // no hash algorithm
279                         byte[] signature = new byte [1];
280                         fmt.VerifySignature (hash, signature);
281                 }
282
283                 [Test]
284 // v.1.0        [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
285 // v.1.1        [ExpectedException (typeof (NullReferenceException))]
286                 public void VerifySignatureHashNoKey ()
287                 {
288                         RSAPKCS1SignatureDeformatter fmt = new RSAPKCS1SignatureDeformatter ();
289                         HashAlgorithm hash = hash = SHA1.Create ();
290                         try {
291                                 // no key
292                                 fmt.VerifySignature (hash, shaSignature);
293                                 Fail ("VerifySignatureHashNoKey - Expected CryptographicUnexpectedOperationException but none");
294                         }
295                         catch (CryptographicUnexpectedOperationException) {
296                                 // this was expected
297                         }
298                         catch (NullReferenceException) {
299                                 // this wasn't expected - but that's the result from framework 1.1
300                         }
301                         catch (Exception e) {
302                                 Fail ("VerifySignatureHashNoKey - Expected CryptographicUnexpectedOperationException but got: " + e.ToString ());
303                         }
304                 }
305
306                 [Test]
307                 public void VerifySignatureSHA1Hash () 
308                 {
309                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("SHA1");
310                         // good SHA1
311                         byte[] data = new byte [20];
312                         HashAlgorithm hash = SHA1.Create ();
313                         hash.ComputeHash (data);
314
315                         byte[] shaSignature = { 0x7C, 0xA0, 0x13, 0xFB, 0xCB, 0x4D, 0x08, 0x02, 0x3C, 0x6B, 0x88, 0xA6, 0x25, 0x43, 0x17, 0x51, 0xA6, 0xA8, 0x8F, 0x5B, 0xAE, 0xC3, 0x57, 0x75, 0x2A, 0x8B, 0xD8, 0xBA, 0xCF, 0x9B, 0xBB, 0x5A, 0xD5, 0xB0, 0x11, 0xF2, 0xA9, 0xCC, 0xB5, 0x22, 0x59, 0xEE, 0x85, 0x49, 0x11, 0xB6, 0x9C, 0x50, 0x61, 0x4A, 0xEC, 0xA3, 0x50, 0x96, 0xE3, 0x2F, 0x1A, 0x6D, 0x9B, 0x6B, 0x6E, 0xC4, 0x50, 0x50, 0x84, 0x29, 0x92, 0x93, 0xE0, 0x0F, 0xCB, 0xBB, 0x61, 0x5D, 0x36, 0x51, 0x1A, 0xBB, 0x73, 0x75, 0x83, 0xEF, 0xDB, 0x4B, 0x2A, 0x38, 0x2C, 0x37, 0x0A, 0x1F, 0x84, 0xE0, 0x9B, 0x24, 0xDF, 0x69, 0x0E, 0x5C, 0xD9, 0xAF, 0x89, 0x72, 0x45, 0x30, 0xA1, 0xDB, 0xA8, 0x22, 0x40, 0x42, 0x07, 0xCC, 0x2A, 0x0E, 0x90, 0x9A, 0x4D, 0xE5, 0x2B, 0x48, 0x86, 0x4D, 0x01, 0x25, 0x23, 0x95, 0xB5, 0xBD };
316                         Assert ("VerifySignature(SHA1, sign)", fmt.VerifySignature (hash, shaSignature));
317                         // bad signature
318                         shaSignature [0] = (byte) ~shaSignature [0];
319                         Assert ("VerifySignature(SHA1, badSign)", !fmt.VerifySignature (hash, shaSignature));
320                 }
321
322                 [Test]
323 // v.1.0        [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
324 // v.1.1        [ExpectedException (typeof (NullReferenceException))]
325                 public void VerifySignatureSHA1HashBadSignatureLength () 
326                 {
327                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("SHA1");
328                         // wrong signature length
329                         byte[] badSignature = new byte [shaSignature.Length-1];
330                         HashAlgorithm hash = SHA1.Create ();
331                         try {
332                                 fmt.VerifySignature (hash, badSignature);
333                                 Fail ("VerifySignatureSHA1HashBadSignatureLength - Expected CryptographicUnexpectedOperationException but none");
334                         }
335                         catch (CryptographicUnexpectedOperationException) {
336                                 // this was expected
337                         }
338                         catch (NullReferenceException) {
339                                 // this wasn't expected - but that's the result from framework 1.1
340                         }
341                         catch (Exception e) {
342                                 Fail ("VerifySignatureSHA1HashBadSignatureLength - Expected CryptographicUnexpectedOperationException but got: " + e.ToString ());
343                         }
344                 }
345
346                 [Test]
347                 public void VerifySignatureMD5Hash () 
348                 {
349                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("MD5");
350                         // good MD5
351                         byte[] data = new byte [20];
352                         HashAlgorithm hash = MD5.Create ();
353                         hash.ComputeHash (data);
354                         byte[] signature = { 0x0F, 0xD6, 0x16, 0x2C, 0x31, 0xD6, 0xD7, 0xA0, 0xE8, 0xA0, 0x89, 0x53, 0x7B, 0x36, 0x8F, 0x25, 0xA5, 0xF6, 0x4A, 0x0B, 0xD3, 0xB9, 0x9B, 0xC4, 0xAE, 0xDC, 0xD4, 0x58, 0x5C, 0xD9, 0x58, 0x61, 0xE3, 0x66, 0x89, 0xB1, 0x1E, 0x33, 0x88, 0xDF, 0x58, 0xC4, 0x2E, 0xAE, 0xE7, 0x7B, 0x96, 0x61, 0x77, 0x91, 0xBD, 0xBD, 0x99, 0x9E, 0x1C, 0x3E, 0x0A, 0x5C, 0x15, 0x69, 0x00, 0xFA, 0xEE, 0xD7, 0xDC, 0xD2, 0x62, 0xA3, 0x31, 0x6A, 0x33, 0x75, 0xC8, 0x8E, 0x47, 0x5C, 0x1E, 0xD8, 0x91, 0x36, 0x65, 0xF3, 0x67, 0x63, 0xFC, 0x2B, 0x37, 0x7D, 0xE6, 0x2C, 0x2C, 0x09, 0x45, 0xE1, 0x8D, 0x8C, 0x8F, 0xFC, 0x6A, 0x4A, 0xD1, 0x4D, 0x06, 0xF3, 0x79, 0x9F, 0xDB, 0x0F, 0x4B, 0xD1, 0x94, 0x6F, 0xC7, 0xE7, 0x4E, 0x06, 0xDA, 0xDB, 0x2A, 0x51, 0x62, 0xCA, 0x1A, 0x31, 0x51, 0x2B, 0x83, 0xDD };
355                         Assert ("VerifySignature(MD5, sign)", fmt.VerifySignature (hash, signature));
356                 }
357
358                 [Test]
359 // v.1.0        [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
360 // v.1.1        [ExpectedException (typeof (NullReferenceException))]
361                 public void VerifyBadSignatureMD5Hash () 
362                 {
363                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("MD5");
364                         // bad signature
365                         byte[] badSignature = new Byte [md5Signature.Length];
366                         Array.Copy (md5Signature, 0, badSignature, 0, badSignature.Length);
367                         badSignature[0] = (byte) ~md5Signature [0];
368                         HashAlgorithm hash = MD5.Create ();
369                         try {
370                                 fmt.VerifySignature (hash, md5Signature);
371                                 Fail ("VerifyBadSignatureMD5Hash - Expected CryptographicUnexpectedOperationException but none");
372                         }
373                         catch (CryptographicUnexpectedOperationException) {
374                                 // this was expected
375                         }
376                         catch (NullReferenceException) {
377                                 // this wasn't expected - but that's the result from framework 1.1
378                         }
379                         catch (Exception e) {
380                                 Fail ("VerifyBadSignatureMD5Hash - Expected CryptographicUnexpectedOperationException but got: " + e.ToString ());
381                         }
382                 }
383
384                 [Test]
385 // v.1.0        [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
386 // v.1.1        [ExpectedException (typeof (NullReferenceException))]
387                 public void VerifySignatureMD5HashBadSignatureLength () 
388                 {
389                         RSAPKCS1SignatureDeformatter fmt = GetDefaultDeformatter ("MD5");
390                         // wrong signature length
391                         byte[] badSignature = new byte [md5Signature.Length-1];
392                         HashAlgorithm hash = MD5.Create ();
393                         try {
394                                 fmt.VerifySignature (hash, md5Signature);
395                                 Fail ("VerifySignatureMD5HashBadSignatureLength - Expected CryptographicUnexpectedOperationException but none");
396                         }
397                         catch (CryptographicUnexpectedOperationException) {
398                                 // this was expected
399                         }
400                         catch (NullReferenceException) {
401                                 // this wasn't expected - but that's the result from framework 1.1
402                         }
403                         catch (Exception e) {
404                                 Fail ("VerifySignatureMD5HashBadSignatureLength - Expected CryptographicUnexpectedOperationException but got: " + e.ToString ());
405                         }
406                 }
407         }
408 }