This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / CryptoConfigTest.cs
1 //
2 // CryptoConfigTest.cs - NUnit Test Cases for CryptoConfig
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.Security.Cryptography;
13
14 namespace MonoTests.System.Security.Cryptography {
15
16 [TestFixture]
17 public class CryptoConfigTest : Assertion {
18
19         public void AssertEquals (string msg, byte[] array1, byte[] array2)
20         {
21                 AllTests.AssertEquals (msg, array1, array2);
22         }
23
24         void CreateFromName (string name, string objectname)
25         {
26                 object o = CryptoConfig.CreateFromName (name);
27                 if (objectname == null)
28                         AssertNull (name, o);
29                 else
30                         AssertEquals (name, o.ToString(), objectname);
31         }
32
33         [Test]
34         [ExpectedException (typeof (ArgumentNullException))]
35         public void CreateFromNameNull () 
36         {
37                 object o = CryptoConfig.CreateFromName (null);
38         }
39
40         // validate that CryptoConfig create the exact same implementation between mono and MS
41         [Test]
42         public void CreateFromName () 
43         {
44                 CreateFromName ("SHA", "System.Security.Cryptography.SHA1CryptoServiceProvider");
45                 // FIXME: We need to support the machine.config file to get exact same results
46                 // with the MS .NET Framework
47                 CreateFromName ("SHA1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
48                 CreateFromName( "System.Security.Cryptography.SHA1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
49                 // after installing the WSDK - changes to the machine.config file (not documented)
50 //              CreateFromName ("SHA1", "System.Security.Cryptography.SHA1Managed");
51 //              CreateFromName ("System.Security.Cryptography.SHA1", "System.Security.Cryptography.SHA1Managed");
52                 CreateFromName ("System.Security.Cryptography.HashAlgorithm", "System.Security.Cryptography.SHA1CryptoServiceProvider");
53                 CreateFromName ("System.Security.Cryptography.SHA1CryptoServiceProvider", "System.Security.Cryptography.SHA1CryptoServiceProvider");
54                 CreateFromName ("MD5", "System.Security.Cryptography.MD5CryptoServiceProvider");  
55                 CreateFromName ("System.Security.Cryptography.MD5", "System.Security.Cryptography.MD5CryptoServiceProvider");  
56                 CreateFromName ("System.Security.Cryptography.MD5CryptoServiceProvider", "System.Security.Cryptography.MD5CryptoServiceProvider");
57                 CreateFromName ("SHA256", "System.Security.Cryptography.SHA256Managed");  
58                 CreateFromName ("SHA-256", "System.Security.Cryptography.SHA256Managed");  
59                 CreateFromName ("System.Security.Cryptography.SHA256", "System.Security.Cryptography.SHA256Managed");  
60                 CreateFromName ("SHA384", "System.Security.Cryptography.SHA384Managed");  
61                 CreateFromName ("SHA-384", "System.Security.Cryptography.SHA384Managed");  
62                 CreateFromName ("System.Security.Cryptography.SHA384", "System.Security.Cryptography.SHA384Managed");  
63                 CreateFromName ("SHA512", "System.Security.Cryptography.SHA512Managed");  
64                 CreateFromName ("SHA-512", "System.Security.Cryptography.SHA512Managed");  
65                 CreateFromName ("System.Security.Cryptography.SHA512", "System.Security.Cryptography.SHA512Managed");  
66                 CreateFromName ("RSA", "System.Security.Cryptography.RSACryptoServiceProvider");  
67                 CreateFromName ("System.Security.Cryptography.RSA", "System.Security.Cryptography.RSACryptoServiceProvider");  
68                 CreateFromName ("System.Security.Cryptography.AsymmetricAlgorithm", "System.Security.Cryptography.RSACryptoServiceProvider");  
69                 CreateFromName ("DSA", "System.Security.Cryptography.DSACryptoServiceProvider");  
70                 CreateFromName ("System.Security.Cryptography.DSA", "System.Security.Cryptography.DSACryptoServiceProvider");  
71                 CreateFromName ("DES", "System.Security.Cryptography.DESCryptoServiceProvider");  
72                 CreateFromName ("System.Security.Cryptography.DES", "System.Security.Cryptography.DESCryptoServiceProvider");  
73                 CreateFromName ("3DES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");  
74                 CreateFromName ("TripleDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");  
75                 CreateFromName ("Triple DES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");  
76                 CreateFromName ("System.Security.Cryptography.TripleDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");  
77                 // LAMESPEC SymmetricAlgorithm documented as TripleDESCryptoServiceProvider
78                 CreateFromName ("System.Security.Cryptography.SymmetricAlgorithm", "System.Security.Cryptography.RijndaelManaged");  
79                 CreateFromName ("RC2", "System.Security.Cryptography.RC2CryptoServiceProvider");  
80                 CreateFromName ("System.Security.Cryptography.RC2", "System.Security.Cryptography.RC2CryptoServiceProvider");  
81                 CreateFromName ("Rijndael", "System.Security.Cryptography.RijndaelManaged");  
82                 CreateFromName ("System.Security.Cryptography.Rijndael", "System.Security.Cryptography.RijndaelManaged");
83                 // LAMESPEC Undocumented Names in CryptoConfig
84                 CreateFromName ("RandomNumberGenerator", "System.Security.Cryptography.RNGCryptoServiceProvider");
85                 CreateFromName ("System.Security.Cryptography.RandomNumberGenerator", "System.Security.Cryptography.RNGCryptoServiceProvider");
86                 CreateFromName ("System.Security.Cryptography.KeyedHashAlgorithm", "System.Security.Cryptography.HMACSHA1");
87                 CreateFromName ("HMACSHA1", "System.Security.Cryptography.HMACSHA1");
88                 CreateFromName ("System.Security.Cryptography.HMACSHA1", "System.Security.Cryptography.HMACSHA1");
89                 CreateFromName ("MACTripleDES", "System.Security.Cryptography.MACTripleDES");
90                 CreateFromName ("System.Security.Cryptography.MACTripleDES", "System.Security.Cryptography.MACTripleDES");
91                 // note: CryptoConfig can create any object !
92                 CreateFromName ("System.Security.Cryptography.CryptoConfig", "System.Security.Cryptography.CryptoConfig");
93                 CreateFromName ("System.IO.MemoryStream", "System.IO.MemoryStream");
94                 // non existing algo should return null (without exception)
95                 AssertNull ("NonExistingAlgorithm", CryptoConfig.CreateFromName("NonExistingAlgorithm"));
96         }
97
98         // additional names (URL) used for XMLDSIG (System.Security.Cryptography.Xml)
99         // URL taken from http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/
100         [Test]
101         public void CreateFromURL () 
102         {
103                 // URL used in SignatureMethod element
104                 CreateFromName ("http://www.w3.org/2000/09/xmldsig#dsa-sha1", "System.Security.Cryptography.DSASignatureDescription");
105                 CreateFromName ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", "System.Security.Cryptography.RSAPKCS1SHA1SignatureDescription");
106                 CreateFromName ("http://www.w3.org/2000/09/xmldsig#hmac-sha1", null);
107                 // URL used in DigestMethod element 
108                 CreateFromName ("http://www.w3.org/2000/09/xmldsig#sha1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
109                 // URL used in Canonicalization or Transform elements 
110                 CreateFromName ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", "System.Security.Cryptography.Xml.XmlDsigC14NTransform");
111                 CreateFromName ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", "System.Security.Cryptography.Xml.XmlDsigC14NWithCommentsTransform");
112                 // URL used in Transform element 
113                 CreateFromName ("http://www.w3.org/2000/09/xmldsig#base64", "System.Security.Cryptography.Xml.XmlDsigBase64Transform");
114                 // after installing the WSDK - changes to the machine.config file (not documented)
115 //              CreateFromName ("http://www.w3.org/TR/1999/REC-xpath-19991116", "Microsoft.WSDK.Security.XmlDsigXPathTransform");
116                 CreateFromName ("http://www.w3.org/TR/1999/REC-xpath-19991116", "System.Security.Cryptography.Xml.XmlDsigXPathTransform");
117                 CreateFromName ("http://www.w3.org/TR/1999/REC-xslt-19991116", "System.Security.Cryptography.Xml.XmlDsigXsltTransform");
118                 CreateFromName ("http://www.w3.org/2000/09/xmldsig#enveloped-signature", "System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform");
119                 // URL used in Reference element 
120                 CreateFromName ("http://www.w3.org/2000/09/xmldsig#Object", null);
121                 CreateFromName ("http://www.w3.org/2000/09/xmldsig#Manifest", null);
122                 CreateFromName ("http://www.w3.org/2000/09/xmldsig#SignatureProperties", null);
123                 // LAMESPEC: only documentated in ".NET Framework Security" book
124                 CreateFromName ("http://www.w3.org/2000/09/xmldsig# X509Data", "System.Security.Cryptography.Xml.KeyInfoX509Data");
125                 CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyName", "System.Security.Cryptography.Xml.KeyInfoName");
126                 CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyValue/DSAKeyValue", "System.Security.Cryptography.Xml.DSAKeyValue");
127                 CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyValue/RSAKeyValue", "System.Security.Cryptography.Xml.RSAKeyValue");
128                 CreateFromName ("http://www.w3.org/2000/09/xmldsig# RetrievalMethod", "System.Security.Cryptography.Xml.KeyInfoRetrievalMethod");
129         }
130
131         // Tests created using "A Layer Man Guide to ASN.1" from RSA, page 19-20
132         // Need to find an OID ? goto http://www.alvestrand.no/~hta/objectid/top.html
133         static byte[] oidETSI = { 0x06, 0x03, 0x04, 0x00, 0x00 };
134         static byte[] oidSHA1 = { 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A };
135         static byte[] oidASN1CharacterModule = { 0x06, 0x04, 0x51, 0x00, 0x00, 0x00 };
136         static byte[] oidmd5withRSAEncryption = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x04 };
137
138         [Test]
139         [ExpectedException (typeof (NullReferenceException))]
140         // LAMESPEC NullReferenceException is thrown (not ArgumentNullException) if parameter is NULL
141         public void EncodeOIDNull () 
142         {
143                 byte[] o = CryptoConfig.EncodeOID (null);
144         }
145
146         [Test]
147         public void EncodeOID () 
148         {
149                 // OID starts with 0, 1 or 2
150                 AssertEquals ("OID starting with 0.", oidETSI, CryptoConfig.EncodeOID ("0.4.0.0"));
151                 AssertEquals ("OID starting with 1.", oidSHA1, CryptoConfig.EncodeOID ("1.3.14.3.2.26"));
152                 AssertEquals ("OID starting with 2.", oidASN1CharacterModule, CryptoConfig.EncodeOID ("2.1.0.0.0"));
153                 // OID numbers can span multiple bytes
154                 AssertEquals ("OID with numbers spanning multiple bytes", oidmd5withRSAEncryption, CryptoConfig.EncodeOID ("1.2.840.113549.1.1.4"));
155         }
156
157         [Test]
158         [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
159         // LAMESPEC: OID greater that 0x7F (127) bytes aren't supported by the MS Framework
160         public void EncodeOID_BiggerThan127bytes () 
161         {
162                 // "ms"-invalid OID - greater than 127 bytes (length encoding)
163                 // OID longer than 127 bytes (so length must be encoded on multiple bytes)
164                 string baseOID = "1.3.6.1.4.1.11071.0.";
165                 string lastPart = "1111111111"; // must fit in int32
166                 for (int i = 1; i < 30; i++) {
167                         baseOID += lastPart + ".";
168                 }
169                 baseOID += "0";
170                 byte[] tooLongOID = CryptoConfig.EncodeOID (baseOID);
171         }
172                 
173         [Test]
174         [ExpectedException (typeof (OverflowException))]
175         // LAMESPEC: OID with numbers > Int32 aren't supported by the MS BCL
176         public void EncodeOID_BiggerThanInt32 () 
177         {
178                 // "ms"-invalid OID - where a number of the OID > Int32
179                 byte[] tooLongOID = CryptoConfig.EncodeOID ("1.1.4294967295");
180         }
181
182         [Test]
183         public void EncodeOID_InvalidStart () 
184         {
185                 // invalid OID - must start with 0, 1 or 2
186                 // however it works with MS BCL
187                 byte[] oid3 = CryptoConfig.EncodeOID ("3.0");
188                 byte[] res3 = { 0x06, 0x01, 0x78 };
189                 AssertEquals ("OID: 3.0", res3, oid3);
190         }
191
192         [Test]
193         [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
194         public void EncodeOID_TooShort () 
195         {
196                 // invalid OID - must have at least 2 parts (according to X.208)
197                 byte[] tooShortOID = CryptoConfig.EncodeOID ("0");
198         }
199
200         [Test]
201         public void EncodeOID_InvalidSecondPart () 
202         {
203                 // invalid OID - second value < 40 for 0. and 1. (modulo 40)
204                 // however it works with MS BCL
205                 byte[] tooBigSecondPartOID = CryptoConfig.EncodeOID ("0.40");
206                 byte[] tooBigSecondPartRes = { 0x06, 0x01, 0x28 };
207                 AssertEquals ("OID: 0.40", tooBigSecondPartRes, tooBigSecondPartOID);
208         }
209
210         [Test]
211         [ExpectedException (typeof (ArgumentNullException))]
212         public void MapNameToOIDNull () 
213         {
214                 CryptoConfig.MapNameToOID (null);
215         }
216
217         private void MapNameToOID (string name, string oid)
218         {
219                 AssertEquals ("oid(" + name + ")", oid, CryptoConfig.MapNameToOID (name));
220         }
221
222         // LAMESPEC: doesn't support all names defined in CryptoConfig 
223         // non supported names (in MSFW) are commented or null-ed
224         // LAMESPEC: undocumented but full class name is supported
225         [Test]
226         public void MapNameToOID() 
227         {
228 //              MapNameToOID ("SHA", "1.3.14.3.2.26");
229                 MapNameToOID ("SHA1", "1.3.14.3.2.26");
230                 MapNameToOID ("System.Security.Cryptography.SHA1", "1.3.14.3.2.26");
231 //              MapNameToOID ("System.Security.Cryptography.HashAlgorithm", "1.3.14.3.2.26");
232                 MapNameToOID ("System.Security.Cryptography.SHA1CryptoServiceProvider", "1.3.14.3.2.26");
233                 MapNameToOID ("System.Security.Cryptography.SHA1Managed", "1.3.14.3.2.26");
234                 MapNameToOID ("MD5", "1.2.840.113549.2.5");
235                 MapNameToOID ("System.Security.Cryptography.MD5", "1.2.840.113549.2.5");
236                 MapNameToOID ("System.Security.Cryptography.MD5CryptoServiceProvider", "1.2.840.113549.2.5");
237                 MapNameToOID ("SHA256", "2.16.840.1.101.3.4.1");
238 //              MapNameToOID ("SHA-256", "2.16.840.1.101.3.4.1");
239                 MapNameToOID ("System.Security.Cryptography.SHA256", "2.16.840.1.101.3.4.1");
240                 MapNameToOID ("System.Security.Cryptography.SHA256Managed", "2.16.840.1.101.3.4.1");
241                 MapNameToOID ("SHA384", "2.16.840.1.101.3.4.2");
242 //              MapNameToOID ("SHA-384", "2.16.840.1.101.3.4.2");
243                 MapNameToOID ("System.Security.Cryptography.SHA384", "2.16.840.1.101.3.4.2");
244                 MapNameToOID ("System.Security.Cryptography.SHA384Managed", "2.16.840.1.101.3.4.2");
245                 MapNameToOID ("SHA512", "2.16.840.1.101.3.4.3");
246 //              MapNameToOID ("SHA-512", "2.16.840.1.101.3.4.3");
247                 MapNameToOID ("System.Security.Cryptography.SHA512", "2.16.840.1.101.3.4.3");
248                 MapNameToOID ("System.Security.Cryptography.SHA512Managed", "2.16.840.1.101.3.4.3");
249                 // LAMESPEC: only documentated in ".NET Framework Security" book
250                 MapNameToOID ("TripleDESKeyWrap", "1.2.840.113549.1.9.16.3.6");
251                 // no OID defined ?
252                 MapNameToOID ("RSA", null);
253                 MapNameToOID ("System.Security.Cryptography.RSA", null);
254                 MapNameToOID ("System.Security.Cryptography.AsymmetricAlgorithm", null);
255                 MapNameToOID ("DSA", null);
256                 MapNameToOID ("System.Security.Cryptography.DSA", null);
257                 MapNameToOID ("DES", null);
258                 MapNameToOID ("System.Security.Cryptography.DES", null);
259                 MapNameToOID ("3DES", null);
260                 MapNameToOID ("TripleDES", null);
261                 MapNameToOID ("Triple DES", null);
262                 MapNameToOID ("System.Security.Cryptography.TripleDES", null);
263                 MapNameToOID ("RC2", null);
264                 MapNameToOID ("System.Security.Cryptography.RC2", null);
265                 MapNameToOID ("Rijndael", null);
266                 MapNameToOID ("System.Security.Cryptography.Rijndael", null);
267                 MapNameToOID ("System.Security.Cryptography.SymmetricAlgorithm", null);
268                 // LAMESPEC Undocumented Names in CryptoConfig
269                 MapNameToOID ("RandomNumberGenerator", null);
270                 MapNameToOID ("System.Security.Cryptography.RandomNumberGenerator", null);
271                 MapNameToOID ("System.Security.Cryptography.KeyedHashAlgorithm", null);
272                 MapNameToOID ("HMACSHA1", null);
273                 MapNameToOID ("System.Security.Cryptography.HMACSHA1", null);
274                 MapNameToOID ("MACTripleDES", null);
275                 MapNameToOID ("System.Security.Cryptography.MACTripleDES", null);
276                 // non existing algo should return null (without exception)
277                 MapNameToOID ("NonExistingAlgorithm", null);
278         }
279
280         [Test]
281         public void CCToString () 
282         {
283                 // under normal circumstance there are no need to create a CryptoConfig object
284                 // because all interesting stuff are in static methods
285                 CryptoConfig cc = new CryptoConfig ();
286                 AssertEquals ("System.Security.Cryptography.CryptoConfig", cc.ToString ());
287         }
288 }
289
290 }