2004-12-06 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / RSAPKCS1SignatureFormatterTest.cs
1 //
2 // RSAPKCS1SignatureFormatterTest.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 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Security.Cryptography;
33
34 namespace MonoTests.System.Security.Cryptography {
35
36         [TestFixture]
37         public class RSAPKCS1SignatureFormatterTest : Assertion {
38
39                 private static byte[] signatureRSASHA256 = { 0x0F, 0xE3, 0x15, 0x5B, 0x4D, 0xA1, 0xB4, 0x13, 0x93, 0x91, 0x1E, 0x17, 0xF9, 0x36, 0xB3, 0x2C, 0xAC, 0x51, 0x77, 0xBC, 0x86, 0x21, 0xB0, 0x69, 0x75, 0x57, 0xAF, 0xB0, 0xAD, 0xF9, 0x42, 0xF5, 0x58, 0xBC, 0xD5, 0x61, 0xD5, 0x14, 0x8E, 0xC6, 0xE0, 0xB3, 0xB5, 0x51, 0xCD, 0x17, 0x68, 0x58, 0x27, 0x74, 0x8A, 0xA7, 0x88, 0xB9, 0x24, 0xD6, 0xE4, 0xC4, 0x93, 0x82, 0x95, 0xB4, 0x36, 0x14, 0x48, 0xA7, 0xF6, 0x27, 0x87, 0xEB, 0xD8, 0xB9, 0x75, 0x14, 0x75, 0xFB, 0x6E, 0xA1, 0xF7, 0xAB, 0xA6, 0x78, 0x32, 0xEF, 0x1A, 0x23, 0x60, 0xD3, 0x0C, 0x8D, 0xFE, 0x89, 0x72, 0xB7, 0x93, 0x6D, 0x00, 0x25, 0xED, 0xF5, 0x55, 0x66, 0xA8, 0x52, 0x7F, 0x20, 0xFD, 0x77, 0xDA, 0x10, 0x77, 0xE9, 0xF0, 0x58, 0x8D, 0xE6, 0x3A, 0x5A, 0x00, 0x83, 0x64, 0x42, 0xA5, 0x15, 0x79, 0x3C, 0xB0, 0x8F };
40                 private static byte[] signatureRSASHA384 = { 0x86, 0x20, 0x2A, 0xB6, 0xA8, 0x0F, 0x59, 0x42, 0xCA, 0x83, 0xC3, 0x46, 0x2C, 0xA9, 0x2E, 0x62, 0x73, 0x2C, 0xEE, 0x52, 0xA5, 0xAE, 0x4F, 0xFD, 0xB1, 0x1F, 0xFA, 0x0C, 0x71, 0x4A, 0xFD, 0xE2, 0xAC, 0x64, 0x1C, 0x63, 0x41, 0xB8, 0x43, 0x3F, 0x8A, 0xF3, 0x7E, 0x1C, 0x25, 0xBE, 0xEE, 0xFC, 0x7C, 0xCB, 0x33, 0x72, 0x3B, 0x91, 0x1F, 0xF3, 0x78, 0xC2, 0xD0, 0xEA, 0xDF, 0x69, 0xE9, 0x31, 0x2F, 0x39, 0x32, 0x5F, 0x4A, 0x51, 0xAE, 0x24, 0x9E, 0x96, 0x77, 0xFB, 0x16, 0xC4, 0xDD, 0x98, 0xDA, 0xA9, 0x9D, 0xA0, 0x7C, 0x2C, 0x95, 0x12, 0x53, 0x1F, 0x7B, 0x23, 0xEE, 0x78, 0x95, 0x57, 0xFF, 0x02, 0x57, 0x2B, 0x4A, 0x3E, 0x62, 0x6A, 0xC0, 0x99, 0xDF, 0x4B, 0x7E, 0xBF, 0x86, 0xC4, 0xFB, 0x8E, 0xF3, 0x70, 0xA2, 0xEE, 0x7B, 0xCA, 0x8B, 0x22, 0xA4, 0x07, 0xBA, 0xBD, 0x16, 0xA9 };
41                 private static byte[] signatureRSASHA512 = { 0xB7, 0x7E, 0x7E, 0xEF, 0x95, 0xCE, 0xE8, 0x9D, 0x0F, 0x40, 0x35, 0x50, 0x88, 0xFE, 0x8B, 0xA3, 0x26, 0xD3, 0x9E, 0xA7, 0x82, 0x23, 0x1A, 0x46, 0x13, 0x46, 0x81, 0x59, 0xD1, 0x24, 0x45, 0xAC, 0x53, 0xEF, 0x5A, 0x06, 0x31, 0xA7, 0xC2, 0x76, 0xDC, 0x2B, 0x60, 0x69, 0xB1, 0x36, 0x1D, 0xE1, 0xFC, 0xD5, 0x9A, 0x01, 0x71, 0x08, 0xE9, 0x0C, 0xAE, 0xF4, 0x29, 0xCF, 0xC4, 0xB0, 0x60, 0xA4, 0xBE, 0x1C, 0x9B, 0x05, 0x2A, 0xA9, 0x6A, 0x12, 0xFF, 0x73, 0x84, 0x5C, 0xA8, 0x74, 0x5B, 0x9C, 0xA2, 0x07, 0x9D, 0x73, 0xB8, 0xE3, 0x20, 0x16, 0x3C, 0x47, 0x8F, 0x27, 0x7A, 0x48, 0xAF, 0x01, 0x07, 0xA0, 0x6A, 0x2D, 0x71, 0xAD, 0xDD, 0x8B, 0x68, 0xC8, 0x32, 0x61, 0x95, 0x68, 0x22, 0x1B, 0x8B, 0xD9, 0x86, 0xA7, 0xBE, 0x60, 0x06, 0x70, 0x7C, 0xED, 0x51, 0x28, 0x66, 0x28, 0xF0, 0x65 };
42
43                 private static RSA rsa;
44                 private static DSA dsa;
45
46                 private RSAPKCS1SignatureFormatter fmt;
47
48                 [SetUp]
49                 public void SetUp () 
50                 {
51                         if (rsa == null) {
52                                 rsa = RSA.Create ();
53                                 rsa.ImportParameters (AllTests.GetRsaKey (true));
54                         }
55                         if (dsa == null)
56                                 dsa = DSA.Create ();
57                 }
58
59                 public void AssertEquals (string msg, byte[] array1, byte[] array2) 
60                 {
61                         AllTests.AssertEquals (msg, array1, array2);
62                 }
63
64                 [Test]
65                 public void ConstructorEmpty () 
66                 {
67                         fmt = new RSAPKCS1SignatureFormatter ();
68                         AssertNotNull ("RSAPKCS1SignatureFormatter()", fmt);
69                 }
70
71                 [Test]
72 #if NET_2_0
73                 [ExpectedException (typeof (ArgumentNullException))]
74 #endif
75                 public void ConstructorNull () 
76                 {
77                         fmt = new RSAPKCS1SignatureFormatter (null);
78                         AssertNotNull ("RSAPKCS1SignatureFormatter(null)", fmt);
79                 }
80
81                 [Test]
82                 public void ConstructorRSA () 
83                 {
84                         fmt = new RSAPKCS1SignatureFormatter (rsa);
85                         AssertNotNull ("RSAPKCS1SignatureFormatter(rsa)", fmt);
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (InvalidCastException))]
90                 public void ConstructorDSA () 
91                 {
92                         fmt = new RSAPKCS1SignatureFormatter (dsa);
93                 }
94
95                 [Test]
96                 public void SetKeyRSA () 
97                 {
98                         fmt = new RSAPKCS1SignatureFormatter ();
99                         fmt.SetKey (rsa);
100                 }
101
102                 [Test]
103                 [ExpectedException (typeof (InvalidCastException))]
104                 public void SetKeyDSA () 
105                 {
106                         fmt = new RSAPKCS1SignatureFormatter ();
107                         fmt.SetKey (dsa);
108                 }
109
110                 [Test]
111 #if NET_2_0
112                 [ExpectedException (typeof (ArgumentNullException))]
113 #endif
114                 public void SetKeyNull ()
115                 {
116                         fmt = new RSAPKCS1SignatureFormatter ();
117                         fmt.SetKey (null);
118                 }
119
120                 [Test]
121                 public void SetHashAlgorithmSHA1 () 
122                 {
123                         fmt = new RSAPKCS1SignatureFormatter ();
124                         fmt.SetHashAlgorithm ("SHA1");
125                 }
126
127                 [Test]
128                 public void SetHashAlgorithmMD5 () 
129                 {
130                         fmt = new RSAPKCS1SignatureFormatter ();
131                         fmt.SetHashAlgorithm ("MD5");
132                 }
133
134                 [Test]
135                 public void SetHashAlgorithmSHA256 () 
136                 {
137                         fmt = new RSAPKCS1SignatureFormatter ();
138                         fmt.SetHashAlgorithm ("SHA256");
139                 }
140
141                 [Test]
142                 public void SetHashAlgorithmSHA384 () 
143                 {
144                         fmt = new RSAPKCS1SignatureFormatter ();
145                         fmt.SetHashAlgorithm ("SHA384");
146                 }
147
148                 [Test]
149                 public void SetHashAlgorithmSHA512 () 
150                 {
151                         fmt = new RSAPKCS1SignatureFormatter ();
152                         fmt.SetHashAlgorithm ("SHA512");
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (ArgumentNullException))]
157                 public void SetHashAlgorithmNull () 
158                 {
159                         fmt = new RSAPKCS1SignatureFormatter ();
160                         fmt.SetHashAlgorithm (null);
161                 }
162
163                 // see: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcongeneratingsignatures.asp
164
165                 [Test]
166                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
167                 public void CreateSignatureNullHash () 
168                 {
169                         fmt = new RSAPKCS1SignatureFormatter ();
170                         fmt.SetKey (rsa);
171                         byte[] hash = null;
172                         byte[] signature = fmt.CreateSignature (hash);
173                 }
174
175                 [Test]
176                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
177                 public void CreateSignatureNoHashAlgorithm () 
178                 {
179                         fmt = new RSAPKCS1SignatureFormatter ();
180                         // no hash algorithm
181                         byte[] hash = new byte [1];
182                         byte[] signature = fmt.CreateSignature (hash);
183                 }
184
185                 [Test]
186                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
187                 public void CreateSignatureNoKey () 
188                 {
189                         fmt = new RSAPKCS1SignatureFormatter ();
190                         // no key
191                         fmt.SetHashAlgorithm ("SHA1");
192                         byte[] hash = new byte [20];
193                         byte[] signature = fmt.CreateSignature (hash);
194                 }
195
196                 [Test]
197                 public void CreateSignatureRSASHA1 () 
198                 {
199                         fmt = new RSAPKCS1SignatureFormatter ();
200                         // we need the private key 
201                         fmt.SetKey (rsa);
202                         // good SHA1
203                         fmt.SetHashAlgorithm ("SHA1");
204                         byte[] hash = new byte [20];
205                         byte[] signature = fmt.CreateSignature (hash);
206                         AssertNotNull ("CreateSignature(SHA1)", signature);
207                 }
208
209                 [Test]
210                 [ExpectedException (typeof (CryptographicException))]
211                 public void CreateSignatureRSASHA1BadLength () 
212                 {
213                         fmt = new RSAPKCS1SignatureFormatter ();
214                         // we need the private key 
215                         fmt.SetKey (rsa);
216                         // wrong length SHA1
217                         fmt.SetHashAlgorithm ("SHA1");
218                         byte[] hash = new byte [19];
219                         byte[] signature = fmt.CreateSignature (hash);
220                 }
221
222                 [Test]
223                 public void CreateSignatureRSAMD5 () 
224                 {
225                         fmt = new RSAPKCS1SignatureFormatter ();
226                         // we need the private key 
227                         fmt.SetKey (rsa);
228                         // good MD5
229                         fmt.SetHashAlgorithm ("MD5");
230                         byte[] hash = new byte [16];
231                         byte[] signature = fmt.CreateSignature (hash);
232                         AssertNotNull ("CreateSignature(MD5)", signature);
233                 }
234
235                 private byte[] CreateSignature (string hashName, int hashSize) 
236                 {
237                         byte[] signature = null;
238                         fmt = new RSAPKCS1SignatureFormatter ();
239                         bool ms = false;
240                         // we need the private key 
241                         RSA rsa = RSA.Create ("Mono.Security.Cryptography.RSAManaged"); // only available with Mono::
242                         if (rsa == null) {
243                                 ms = true;
244                                 rsa = RSA.Create ();
245                         }
246                         rsa.ImportParameters (AllTests.GetRsaKey (true));
247                         fmt.SetKey (rsa);
248                         try {
249                                 HashAlgorithm ha = HashAlgorithm.Create (hashName);
250                                 byte[] data = new byte [ha.HashSize >> 3];
251                                 // this way we get the same results as CreateSignatureHash
252                                 data = ha.ComputeHash (data);
253
254                                 fmt.SetHashAlgorithm (hashName);
255                                 signature = fmt.CreateSignature (data);
256                                 if (ms)
257                                         Fail ("CreateSignature(" + hashName + ") Expected CryptographicException but got none");
258                         }
259                         catch (CryptographicException) {
260                                 if (!ms)
261                                         throw;
262                         }
263                         catch (Exception e) {
264                                 if (ms)
265                                         Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got " + e.ToString ());
266                         }
267                         return signature;
268                 }
269
270                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
271                 // supported by Mono::
272                 [Test]
273                 public void CreateSignatureRSASHA256 () 
274                 {
275                         byte[] signature = CreateSignature ("SHA256", 32);
276                         if (signature != null)
277                                 AssertEquals ("CreateSignature(SHA256)", signatureRSASHA256, signature);
278                 }
279
280                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
281                 // supported by Mono::
282                 [Test]
283                 public void CreateSignatureRSASHA384 () 
284                 {
285                         byte[] signature = CreateSignature ("SHA384", 48);
286                         if (signature != null)
287                                 AssertEquals ("CreateSignature(SHA384)", signatureRSASHA384, signature);
288                 }
289
290                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
291                 // supported by Mono::
292                 [Test]
293                 public void CreateSignatureRSASHA512 () 
294                 {
295                         byte[] signature = CreateSignature ("SHA512", 64);
296                         if (signature != null)
297                                 AssertEquals ("CreateSignature(SHA512)", signatureRSASHA512, signature);
298                 }
299
300                 [Test]
301                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
302                 public void CreateSignatureRSABadHash () 
303                 {
304                         fmt = new RSAPKCS1SignatureFormatter ();
305                         // we need the private key 
306                         fmt.SetKey (rsa);
307                         // null (bad ;-)
308                         byte[] hash = null;
309                         byte[] signature  = fmt.CreateSignature (hash);
310                 }
311
312                 [Test]
313                 [ExpectedException (typeof (ArgumentNullException))]
314                 public void CreateSignatureHashBadHash () 
315                 {
316                         fmt = new RSAPKCS1SignatureFormatter ();
317                         HashAlgorithm hash = null;
318                         byte[] data = new byte [20];
319                         // no hash algorithm
320                         byte[] signature = fmt.CreateSignature (hash);
321                 }
322
323                 [Test]
324                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
325                 public void CreateSignatureHashNoKey () 
326                 {
327                         fmt = new RSAPKCS1SignatureFormatter ();
328                         byte[] data = new byte [20];
329                         // no key
330                         HashAlgorithm hash = SHA1.Create ();
331                         hash.ComputeHash (data);
332                         byte[] signature = fmt.CreateSignature (hash);
333                 }
334
335                 [Test]
336                 public void CreateSignatureHashSHA1 () 
337                 {
338                         fmt = new RSAPKCS1SignatureFormatter ();
339                         byte[] data = new byte [20];
340                         // we need the private key 
341                         fmt.SetKey (rsa);
342                         // good SHA1
343                         HashAlgorithm hash = SHA1.Create ();
344                         hash.ComputeHash (data);
345                         byte[] signature = fmt.CreateSignature (hash);
346                         AssertNotNull ("CreateSignature(SHA1)", signature);
347                 }
348
349                 [Test]
350                 public void CreateSignatureHashMD5 () 
351                 {
352                         fmt = new RSAPKCS1SignatureFormatter ();
353                         byte[] data = new byte [16];
354                         // we need the private key 
355                         fmt.SetKey (rsa);
356                         // good MD5
357                         HashAlgorithm hash = MD5.Create ();
358                         hash.ComputeHash (data);
359                         byte[] signature = fmt.CreateSignature (hash);
360                         AssertNotNull ("CreateSignature(MD5)", signature);
361                 }
362
363                 private byte[] CreateSignatureHash (string hashName) 
364                 {
365                         byte[] signature = null;
366                         fmt = new RSAPKCS1SignatureFormatter ();
367                         bool ms = false;
368                         // we need the private key 
369                         RSA rsa = RSA.Create ("Mono.Security.Cryptography.RSAManaged"); // only available with Mono::
370                         if (rsa == null) {
371                                 ms = true;
372                                 rsa = RSA.Create ();
373                         }
374                         rsa.ImportParameters (AllTests.GetRsaKey (true));
375                         fmt.SetKey (rsa);
376                         try {
377                                 HashAlgorithm hash = HashAlgorithm.Create (hashName);
378                                 byte[] data = new byte [(hash.HashSize >> 3)];
379                                 hash.ComputeHash (data);
380                                 signature = fmt.CreateSignature (hash);
381                                 if (ms)
382                                         Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got none");
383                         }
384                         catch (CryptographicException) {
385                                 if (!ms)
386                                         throw;
387                         }
388                         catch (Exception e) {
389                                 if (ms)
390                                         Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got " + e.ToString ());
391                         }
392                         return signature;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
393                 }
394
395                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
396                 // supported by Mono::
397                 [Test]
398                 public void CreateSignatureHashSHA256 () 
399                 {
400                         byte[] signature = CreateSignatureHash ("SHA256");
401                         if (signature != null)
402                                 AssertEquals ("CreateSignatureHash(SHA256)", signatureRSASHA256, signature);
403                 }
404
405                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
406                 // supported by Mono::
407                 [Test]
408                 public void CreateSignatureHashSHA384 () 
409                 {
410                         byte[] signature = CreateSignatureHash ("SHA384");
411                         if (signature != null)
412                                 AssertEquals ("CreateSignatureHash(SHA384)", signatureRSASHA384, signature);
413                 }
414
415                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
416                 // supported by Mono::
417                 [Test]
418                 public void CreateSignatureHashSHA512 () 
419                 {
420                         byte[] signature = CreateSignatureHash ("SHA512");
421                         if (signature != null)
422                                 AssertEquals ("CreateSignatureHash(SHA512)", signatureRSASHA512, signature);
423                 }
424         }
425 }