This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 // (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 RSAPKCS1SignatureFormatterTest : Assertion {
19
20                 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 };
21                 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 };
22                 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 };
23
24                 private static RSA rsa;
25                 private static DSA dsa;
26
27                 private RSAPKCS1SignatureFormatter fmt;
28
29                 [SetUp]
30                 public void SetUp () 
31                 {
32                         if (rsa == null) {
33                                 rsa = RSA.Create ();
34                                 rsa.ImportParameters (AllTests.GetRsaKey (true));
35                         }
36                         if (dsa == null)
37                                 dsa = DSA.Create ();
38                 }
39
40                 public void AssertEquals (string msg, byte[] array1, byte[] array2) 
41                 {
42                         AllTests.AssertEquals (msg, array1, array2);
43                 }
44
45                 [Test]
46                 public void ConstructorEmpty () 
47                 {
48                         fmt = new RSAPKCS1SignatureFormatter ();
49                         AssertNotNull ("RSAPKCS1SignatureFormatter()", fmt);
50                 }
51
52                 [Test]
53                 public void ConstructorNull () 
54                 {
55                         fmt = new RSAPKCS1SignatureFormatter (null);
56                         AssertNotNull ("RSAPKCS1SignatureFormatter(null)", fmt);
57                 }
58
59                 [Test]
60                 public void ConstructorRSA () 
61                 {
62                         fmt = new RSAPKCS1SignatureFormatter (rsa);
63                         AssertNotNull ("RSAPKCS1SignatureFormatter(rsa)", fmt);
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (InvalidCastException))]
68                 public void ConstructorDSA () 
69                 {
70                         fmt = new RSAPKCS1SignatureFormatter (dsa);
71                 }
72
73                 [Test]
74                 public void SetKeyRSA () 
75                 {
76                         fmt = new RSAPKCS1SignatureFormatter ();
77                         fmt.SetKey (rsa);
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (InvalidCastException))]
82                 public void SetKeyDSA () 
83                 {
84                         fmt = new RSAPKCS1SignatureFormatter ();
85                         fmt.SetKey (dsa);
86                 }
87
88                 [Test]
89                 public void SetKeyNull () 
90                 {
91                         fmt = new RSAPKCS1SignatureFormatter ();
92                         fmt.SetKey (null);
93                 }
94
95                 [Test]
96                 public void SetHashAlgorithmSHA1 () 
97                 {
98                         fmt = new RSAPKCS1SignatureFormatter ();
99                         fmt.SetHashAlgorithm ("SHA1");
100                 }
101
102                 [Test]
103                 public void SetHashAlgorithmMD5 () 
104                 {
105                         fmt = new RSAPKCS1SignatureFormatter ();
106                         fmt.SetHashAlgorithm ("MD5");
107                 }
108
109                 [Test]
110                 public void SetHashAlgorithmSHA256 () 
111                 {
112                         fmt = new RSAPKCS1SignatureFormatter ();
113                         fmt.SetHashAlgorithm ("SHA256");
114                 }
115
116                 [Test]
117                 public void SetHashAlgorithmSHA384 () 
118                 {
119                         fmt = new RSAPKCS1SignatureFormatter ();
120                         fmt.SetHashAlgorithm ("SHA384");
121                 }
122
123                 [Test]
124                 public void SetHashAlgorithmSHA512 () 
125                 {
126                         fmt = new RSAPKCS1SignatureFormatter ();
127                         fmt.SetHashAlgorithm ("SHA512");
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof (ArgumentNullException))]
132                 public void SetHashAlgorithmNull () 
133                 {
134                         fmt = new RSAPKCS1SignatureFormatter ();
135                         fmt.SetHashAlgorithm (null);
136                 }
137
138                 // see: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcongeneratingsignatures.asp
139
140                 [Test]
141                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
142                 public void CreateSignatureNullHash () 
143                 {
144                         fmt = new RSAPKCS1SignatureFormatter ();
145                         fmt.SetKey (rsa);
146                         byte[] hash = null;
147                         byte[] signature = fmt.CreateSignature (hash);
148                 }
149
150                 [Test]
151                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
152                 public void CreateSignatureNoHashAlgorithm () 
153                 {
154                         fmt = new RSAPKCS1SignatureFormatter ();
155                         // no hash algorithm
156                         byte[] hash = new byte [1];
157                         byte[] signature = fmt.CreateSignature (hash);
158                 }
159
160                 [Test]
161                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
162                 public void CreateSignatureNoKey () 
163                 {
164                         fmt = new RSAPKCS1SignatureFormatter ();
165                         // no key
166                         fmt.SetHashAlgorithm ("SHA1");
167                         byte[] hash = new byte [20];
168                         byte[] signature = fmt.CreateSignature (hash);
169                 }
170
171                 [Test]
172                 public void CreateSignatureRSASHA1 () 
173                 {
174                         fmt = new RSAPKCS1SignatureFormatter ();
175                         // we need the private key 
176                         fmt.SetKey (rsa);
177                         // good SHA1
178                         fmt.SetHashAlgorithm ("SHA1");
179                         byte[] hash = new byte [20];
180                         byte[] signature = fmt.CreateSignature (hash);
181                         AssertNotNull ("CreateSignature(SHA1)", signature);
182                 }
183
184                 [Test]
185                 [ExpectedException (typeof (CryptographicException))]
186                 public void CreateSignatureRSASHA1BadLength () 
187                 {
188                         fmt = new RSAPKCS1SignatureFormatter ();
189                         // we need the private key 
190                         fmt.SetKey (rsa);
191                         // wrong length SHA1
192                         fmt.SetHashAlgorithm ("SHA1");
193                         byte[] hash = new byte [19];
194                         byte[] signature = fmt.CreateSignature (hash);
195                 }
196
197                 [Test]
198                 public void CreateSignatureRSAMD5 () 
199                 {
200                         fmt = new RSAPKCS1SignatureFormatter ();
201                         // we need the private key 
202                         fmt.SetKey (rsa);
203                         // good MD5
204                         fmt.SetHashAlgorithm ("MD5");
205                         byte[] hash = new byte [16];
206                         byte[] signature = fmt.CreateSignature (hash);
207                         AssertNotNull ("CreateSignature(MD5)", signature);
208                 }
209
210                 private byte[] CreateSignature (string hashName, int hashSize) 
211                 {
212                         byte[] signature = null;
213                         fmt = new RSAPKCS1SignatureFormatter ();
214                         bool ms = false;
215                         // we need the private key 
216                         RSA rsa = RSA.Create ("Mono.Security.Cryptography.RSAManaged"); // only available with Mono::
217                         if (rsa == null) {
218                                 ms = true;
219                                 rsa = RSA.Create ();
220                         }
221                         rsa.ImportParameters (AllTests.GetRsaKey (true));
222                         fmt.SetKey (rsa);
223                         try {
224                                 HashAlgorithm ha = HashAlgorithm.Create (hashName);
225                                 byte[] data = new byte [ha.HashSize >> 3];
226                                 // this way we get the same results as CreateSignatureHash
227                                 data = ha.ComputeHash (data);
228
229                                 fmt.SetHashAlgorithm (hashName);
230                                 signature = fmt.CreateSignature (data);
231                                 if (ms)
232                                         Fail ("CreateSignature(" + hashName + ") Expected CryptographicException but got none");
233                         }
234                         catch (CryptographicException) {
235                                 if (!ms)
236                                         throw;
237                         }
238                         catch (Exception e) {
239                                 if (ms)
240                                         Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got " + e.ToString ());
241                         }
242                         return signature;
243                 }
244
245                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
246                 // supported by Mono::
247                 [Test]
248                 public void CreateSignatureRSASHA256 () 
249                 {
250                         byte[] signature = CreateSignature ("SHA256", 32);
251                         if (signature != null)
252                                 AssertEquals ("CreateSignature(SHA256)", signatureRSASHA256, signature);
253                 }
254
255                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
256                 // supported by Mono::
257                 [Test]
258                 public void CreateSignatureRSASHA384 () 
259                 {
260                         byte[] signature = CreateSignature ("SHA384", 48);
261                         if (signature != null)
262                                 AssertEquals ("CreateSignature(SHA384)", signatureRSASHA384, signature);
263                 }
264
265                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
266                 // supported by Mono::
267                 [Test]
268                 public void CreateSignatureRSASHA512 () 
269                 {
270                         byte[] signature = CreateSignature ("SHA512", 64);
271                         if (signature != null)
272                                 AssertEquals ("CreateSignature(SHA512)", signatureRSASHA512, signature);
273                 }
274
275                 [Test]
276                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
277                 public void CreateSignatureRSABadHash () 
278                 {
279                         fmt = new RSAPKCS1SignatureFormatter ();
280                         // we need the private key 
281                         fmt.SetKey (rsa);
282                         // null (bad ;-)
283                         byte[] hash = null;
284                         byte[] signature  = fmt.CreateSignature (hash);
285                 }
286
287                 [Test]
288                 [ExpectedException (typeof (ArgumentNullException))]
289                 public void CreateSignatureHashBadHash () 
290                 {
291                         fmt = new RSAPKCS1SignatureFormatter ();
292                         HashAlgorithm hash = null;
293                         byte[] data = new byte [20];
294                         // no hash algorithm
295                         byte[] signature = fmt.CreateSignature (hash);
296                 }
297
298                 [Test]
299                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
300                 public void CreateSignatureHashNoKey () 
301                 {
302                         fmt = new RSAPKCS1SignatureFormatter ();
303                         byte[] data = new byte [20];
304                         // no key
305                         HashAlgorithm hash = SHA1.Create ();
306                         hash.ComputeHash (data);
307                         byte[] signature = fmt.CreateSignature (hash);
308                 }
309
310                 [Test]
311                 public void CreateSignatureHashSHA1 () 
312                 {
313                         fmt = new RSAPKCS1SignatureFormatter ();
314                         byte[] data = new byte [20];
315                         // we need the private key 
316                         fmt.SetKey (rsa);
317                         // good SHA1
318                         HashAlgorithm hash = SHA1.Create ();
319                         hash.ComputeHash (data);
320                         byte[] signature = fmt.CreateSignature (hash);
321                         AssertNotNull ("CreateSignature(SHA1)", signature);
322                 }
323
324                 [Test]
325                 public void CreateSignatureHashMD5 () 
326                 {
327                         fmt = new RSAPKCS1SignatureFormatter ();
328                         byte[] data = new byte [16];
329                         // we need the private key 
330                         fmt.SetKey (rsa);
331                         // good MD5
332                         HashAlgorithm hash = MD5.Create ();
333                         hash.ComputeHash (data);
334                         byte[] signature = fmt.CreateSignature (hash);
335                         AssertNotNull ("CreateSignature(MD5)", signature);
336                 }
337
338                 private byte[] CreateSignatureHash (string hashName) 
339                 {
340                         byte[] signature = null;
341                         fmt = new RSAPKCS1SignatureFormatter ();
342                         bool ms = false;
343                         // we need the private key 
344                         RSA rsa = RSA.Create ("Mono.Security.Cryptography.RSAManaged"); // only available with Mono::
345                         if (rsa == null) {
346                                 ms = true;
347                                 rsa = RSA.Create ();
348                         }
349                         rsa.ImportParameters (AllTests.GetRsaKey (true));
350                         fmt.SetKey (rsa);
351                         try {
352                                 HashAlgorithm hash = HashAlgorithm.Create (hashName);
353                                 byte[] data = new byte [(hash.HashSize >> 3)];
354                                 hash.ComputeHash (data);
355                                 signature = fmt.CreateSignature (hash);
356                                 if (ms)
357                                         Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got none");
358                         }
359                         catch (CryptographicException) {
360                                 if (!ms)
361                                         throw;
362                         }
363                         catch (Exception e) {
364                                 if (ms)
365                                         Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got " + e.ToString ());
366                         }
367                         return signature;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
368                 }
369
370                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
371                 // supported by Mono::
372                 [Test]
373                 public void CreateSignatureHashSHA256 () 
374                 {
375                         byte[] signature = CreateSignatureHash ("SHA256");
376                         if (signature != null)
377                                 AssertEquals ("CreateSignatureHash(SHA256)", signatureRSASHA256, signature);
378                 }
379
380                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
381                 // supported by Mono::
382                 [Test]
383                 public void CreateSignatureHashSHA384 () 
384                 {
385                         byte[] signature = CreateSignatureHash ("SHA384");
386                         if (signature != null)
387                                 AssertEquals ("CreateSignatureHash(SHA384)", signatureRSASHA384, signature);
388                 }
389
390                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
391                 // supported by Mono::
392                 [Test]
393                 public void CreateSignatureHashSHA512 () 
394                 {
395                         byte[] signature = CreateSignatureHash ("SHA512");
396                         if (signature != null)
397                                 AssertEquals ("CreateSignatureHash(SHA512)", signatureRSASHA512, signature);
398                 }
399         }
400 }