2009-07-14 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / RSAPKCS1KeyExchangeFormatterTest.cs
1 //
2 // RSAPKCS1KeyExchangeFormatterTest.cs - NUnit Test Cases for RSAPKCS1KeyExchangeFormatter
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 RSAPKCS1KeyExchangeFormatterTest : Assertion {
38
39         protected static RSA key;
40
41         [SetUp]
42         public void SetUp () 
43         {
44                 // generating a keypair is REALLY long and the framework
45                 // makes sure that we generate one (even if create an object
46                 // to import an exsting key)
47                 if (key == null) {
48                         key = RSA.Create ();
49                         key.ImportParameters (AllTests.GetRsaKey (true));
50                 }
51         }
52
53         public void AssertEquals (string msg, byte[] array1, byte[] array2)
54         {
55                 AllTests.AssertEquals (msg, array1, array2);
56         }
57
58         [Test]
59         public void Properties () 
60         {
61                 RSAPKCS1KeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter ();
62                 keyex.SetKey (key);
63                 AssertEquals("RSAPKCS1KeyExchangeFormatter.Parameters", "<enc:KeyEncryptionMethod enc:Algorithm=\"http://www.microsoft.com/xml/security/algorithm/PKCS1-v1.5-KeyEx\" xmlns:enc=\"http://www.microsoft.com/xml/security/encryption/v1.0\" />", keyex.Parameters);
64                 // null (default)
65                 AssertNull("RSAPKCS1KeyExchangeFormatter.Rng", keyex.Rng);
66                 AssertEquals("RSAPKCS1KeyExchangeFormatter.ToString()", "System.Security.Cryptography.RSAPKCS1KeyExchangeFormatter", keyex.ToString ());
67         }
68
69         [Test]
70         [ExpectedException (typeof (ArgumentNullException))]
71 #if ! NET_2_0
72         [Category ("NotDotNet")] // Sometime causes System.ExecutionEngineException on MS implementation
73 #endif
74         public void KeyExchangeNull ()
75         {
76                 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
77                 byte[] EM = keyex.CreateKeyExchange (null);
78         }
79
80         // TestExchangeMin (1)
81         [Test]
82         public void KeyExchangeMin ()
83         {
84                 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
85                 byte[] M = { 0x01 };
86                 byte[] EM = keyex.CreateKeyExchange (M);
87
88                 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
89                 byte[] Mback = keyback.DecryptKeyExchange (EM);
90                 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
91         }
92
93         // test with a message 128 bits (16 bytes) long
94         [Test]
95         public void KeyExchange128bits ()
96         {
97                 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
98                 byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
99                 byte[] EM = keyex.CreateKeyExchange (M, typeof (Rijndael));
100
101                 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
102                 byte[] Mback = keyback.DecryptKeyExchange (EM);
103                 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
104         }
105
106         // test with a message 160 bits (20 bytes) long
107         [Test]
108         public void KeyExchange160bits () 
109         {
110                 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
111                 byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49, 0x00, 0x00, 0x00, 0x00 };
112                 byte[] EM = keyex.CreateKeyExchange (M);
113
114                 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
115                 byte[] Mback = keyback.DecryptKeyExchange (EM);
116                 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
117         }
118
119         // Max = k - m - 11
120         [Test]
121         public void KeyExchangeMax()
122         {
123                 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
124                 byte[] M = new byte [(key.KeySize >> 3)- 11];
125                 byte[] EM = keyex.CreateKeyExchange (M);
126
127                 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
128                 byte[] Mback = keyback.DecryptKeyExchange (EM);
129                 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
130         }
131
132         // TestExchangeTooBig
133         [Test]
134         [ExpectedException (typeof (CryptographicException))]
135         public void KeyExchangeTooBig()
136         {
137                 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
138                 byte[] M = new byte [(key.KeySize >> 3)- 10];
139                 byte[] EM = keyex.CreateKeyExchange (M);
140         }
141
142         [Test]
143         public void Rng () 
144         {
145                 RSAPKCS1KeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
146                 AssertNull ("Rng", keyex.Rng);
147                 keyex.Rng = RandomNumberGenerator.Create ();
148                 AssertNotNull ("Rng", keyex.Rng);
149         }
150
151         [Test]
152 #if NET_2_0
153         [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
154 #else
155         [ExpectedException (typeof (NullReferenceException))]
156 #endif
157         public void ExchangeNoKey () 
158         {
159                 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter ();
160                 byte[] M = keyex.CreateKeyExchange (new byte [16]);
161         }
162
163         [Test]
164         [ExpectedException (typeof (InvalidCastException))]
165         public void ExchangeDSAKey () 
166         {
167                 DSA dsa = DSA.Create ();
168                 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (dsa);
169         }
170         
171         [Test]
172         [ExpectedException (typeof (ArgumentNullException))]
173         public void CreateWithNullKey ()
174         {
175                 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (null);
176         }
177
178         [Test]
179         [ExpectedException (typeof (ArgumentNullException))]
180         public void CreateAndSetNullKey ()
181         {
182                 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter ();
183                 keyex.SetKey (null);
184         }
185 }
186
187 }