New test.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / CryptoConfig.cs
1 //
2 // CryptoConfig.cs: Handles cryptographic implementations and OIDs mappings.
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //      Tim Coleman (tim@timcoleman.com)
7 //
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) Tim Coleman, 2004
10 // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 #if !MOONLIGHT
33
34 using System.Collections;
35 using System.Globalization;
36 using System.IO;
37 using System.Reflection;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Security.Permissions;
41 using System.Text;
42
43 using Mono.Xml;
44
45 namespace System.Security.Cryptography {
46
47 [ComVisible (true)]
48 public partial      class CryptoConfig {
49
50         static private object lockObject;
51         static private Hashtable algorithms;
52         static private Hashtable oid;
53
54         private const string defaultNamespace = "System.Security.Cryptography.";
55         private const string defaultSHA1 = defaultNamespace + "SHA1CryptoServiceProvider";
56         private const string defaultMD5 = defaultNamespace + "MD5CryptoServiceProvider";
57         private const string defaultSHA256 = defaultNamespace + "SHA256Managed";
58         private const string defaultSHA384 = defaultNamespace + "SHA384Managed";
59         private const string defaultSHA512 = defaultNamespace + "SHA512Managed";
60         private const string defaultRSA = defaultNamespace + "RSACryptoServiceProvider";
61         private const string defaultDSA = defaultNamespace + "DSACryptoServiceProvider";
62         private const string defaultDES = defaultNamespace + "DESCryptoServiceProvider";
63         private const string default3DES = defaultNamespace + "TripleDESCryptoServiceProvider";
64         private const string defaultRC2 = defaultNamespace + "RC2CryptoServiceProvider";
65         private const string defaultAES = defaultNamespace + "RijndaelManaged";
66         // LAMESPEC: undocumented names in CryptoConfig
67         private const string defaultRNG = defaultNamespace + "RNGCryptoServiceProvider";
68         private const string defaultHMAC = defaultNamespace + "HMACSHA1";
69         private const string defaultMAC3DES = defaultNamespace + "MACTripleDES";
70         // LAMESPEC: undocumented classes (also undocumented in CryptoConfig ;-)
71         private const string defaultDSASigDesc = defaultNamespace + "DSASignatureDescription";
72         private const string defaultRSASigDesc = defaultNamespace + "RSAPKCS1SHA1SignatureDescription";
73         private const string defaultRIPEMD160 = defaultNamespace + "RIPEMD160Managed";
74         private const string defaultHMACMD5 = defaultNamespace + "HMACMD5";
75         private const string defaultHMACRIPEMD160 = defaultNamespace + "HMACRIPEMD160";
76         private const string defaultHMACSHA256 = defaultNamespace + "HMACSHA256";
77         private const string defaultHMACSHA384 = defaultNamespace + "HMACSHA384";
78         private const string defaultHMACSHA512 = defaultNamespace + "HMACSHA512";
79
80         // LAMESPEC: undocumented names in CryptoConfig
81         private const string defaultC14N = defaultNamespace + "Xml.XmlDsigC14NTransform, " + Consts.AssemblySystem_Security;
82         private const string defaultC14NWithComments = defaultNamespace + "Xml.XmlDsigC14NWithCommentsTransform, " + Consts.AssemblySystem_Security;
83         private const string defaultBase64 = defaultNamespace + "Xml.XmlDsigBase64Transform, " + Consts.AssemblySystem_Security;
84         private const string defaultXPath = defaultNamespace + "Xml.XmlDsigXPathTransform, " + Consts.AssemblySystem_Security;
85         private const string defaultXslt = defaultNamespace + "Xml.XmlDsigXsltTransform, " + Consts.AssemblySystem_Security;
86         private const string defaultEnveloped = defaultNamespace + "Xml.XmlDsigEnvelopedSignatureTransform, " + Consts.AssemblySystem_Security;
87         private const string defaultXmlDecryption = defaultNamespace + "Xml.XmlDecryptionTransform, " + Consts.AssemblySystem_Security;
88         private const string defaultExcC14N = defaultNamespace + "Xml.XmlDsigExcC14NTransform, " + Consts.AssemblySystem_Security;
89         private const string defaultExcC14NWithComments = defaultNamespace + "Xml.XmlDsigExcC14NWithCommentsTransform, " + Consts.AssemblySystem_Security;
90
91         // LAMESPEC: only documentated in ".NET Framework Security" book
92         private const string defaultX509Data = defaultNamespace + "Xml.KeyInfoX509Data, " + Consts.AssemblySystem_Security;
93         private const string defaultKeyName = defaultNamespace + "Xml.KeyInfoName, " + Consts.AssemblySystem_Security;
94         private const string defaultKeyValueDSA = defaultNamespace + "Xml.DSAKeyValue, " + Consts.AssemblySystem_Security;
95         private const string defaultKeyValueRSA = defaultNamespace + "Xml.RSAKeyValue, " + Consts.AssemblySystem_Security;
96         private const string defaultRetrievalMethod = defaultNamespace + "Xml.KeyInfoRetrievalMethod, " + Consts.AssemblySystem_Security;
97
98         private const string managedSHA1 = defaultNamespace + "SHA1Managed";
99
100         // Oddly OID seems only available for hash algorithms
101         private const string oidSHA1 = "1.3.14.3.2.26";
102         private const string oidMD5 = "1.2.840.113549.2.5";
103         // changed in 2.0
104         private const string oidSHA256 = "2.16.840.1.101.3.4.2.1";
105         private const string oidSHA384 = "2.16.840.1.101.3.4.2.2";
106         private const string oidSHA512 = "2.16.840.1.101.3.4.2.3";
107         // new in 2.0
108 //      private const string oidRSA = "1.2.840.113549.1.1.1";
109 //      private const string oidDSA = "1.2.840.10040.4.1";
110         private const string oidDES = "1.3.14.3.2.7";
111         private const string oid3DES = "1.2.840.113549.3.7";
112         private const string oidRC2 = "1.2.840.113549.3.2";
113         // LAMESPEC: only documentated in ".NET Framework Security" book
114         private const string oid3DESKeyWrap = "1.2.840.113549.1.9.16.3.6";
115
116         private const string nameSHA1a = "SHA";
117         private const string nameSHA1b = "SHA1";
118         private const string nameSHA1c = "System.Security.Cryptography.SHA1";
119         private const string nameSHA1d = "System.Security.Cryptography.HashAlgorithm";
120         private const string nameMD5a = "MD5";
121         private const string nameMD5b = "System.Security.Cryptography.MD5";
122         private const string nameSHA256a = "SHA256";
123         private const string nameSHA256b = "SHA-256";
124         private const string nameSHA256c = "System.Security.Cryptography.SHA256";
125         private const string nameSHA384a = "SHA384";
126         private const string nameSHA384b = "SHA-384";
127         private const string nameSHA384c = "System.Security.Cryptography.SHA384";
128         private const string nameSHA512a = "SHA512";
129         private const string nameSHA512b = "SHA-512";
130         private const string nameSHA512c = "System.Security.Cryptography.SHA512";
131         private const string nameRSAa = "RSA";
132         private const string nameRSAb = "System.Security.Cryptography.RSA";
133         private const string nameRSAc = "System.Security.Cryptography.AsymmetricAlgorithm";
134         private const string nameDSAa = "DSA";
135         private const string nameDSAb = "System.Security.Cryptography.DSA";
136         private const string nameDESa = "DES";
137         private const string nameDESb = "System.Security.Cryptography.DES";
138         private const string name3DESa = "3DES";
139         private const string name3DESb = "TripleDES";
140         private const string name3DESc = "Triple DES";
141         private const string name3DESd = "System.Security.Cryptography.TripleDES";
142         private const string nameRC2a = "RC2";
143         private const string nameRC2b = "System.Security.Cryptography.RC2";
144         private const string nameAESa = "Rijndael";
145         private const string nameAESb = "System.Security.Cryptography.Rijndael";
146         private const string nameAESc = "System.Security.Cryptography.SymmetricAlgorithm";
147         // LAMESPEC: undocumented names in CryptoConfig
148         private const string nameRNGa = "RandomNumberGenerator";
149         private const string nameRNGb = "System.Security.Cryptography.RandomNumberGenerator";
150         private const string nameKeyHasha = "System.Security.Cryptography.KeyedHashAlgorithm";
151         private const string nameHMACSHA1a = "HMACSHA1";
152         private const string nameHMACSHA1b = "System.Security.Cryptography.HMACSHA1";
153         private const string nameMAC3DESa = "MACTripleDES";
154         private const string nameMAC3DESb = "System.Security.Cryptography.MACTripleDES";
155         // LAMESPEC: only documentated in ".NET Framework Security" book
156         private const string name3DESKeyWrap = "TripleDESKeyWrap";
157         private const string nameRIPEMD160a = "RIPEMD160";
158         private const string nameRIPEMD160b = "RIPEMD-160";
159         private const string nameRIPEMD160c = "System.Security.Cryptography.RIPEMD160";
160 //      private const string nameHMACa = "HMAC";
161         private const string nameHMACb = "System.Security.Cryptography.HMAC";
162         private const string nameHMACMD5a = "HMACMD5";
163         private const string nameHMACMD5b = "System.Security.Cryptography.HMACMD5";
164         private const string nameHMACRIPEMD160a = "HMACRIPEMD160";
165         private const string nameHMACRIPEMD160b = "System.Security.Cryptography.HMACRIPEMD160";
166         private const string nameHMACSHA256a = "HMACSHA256";
167         private const string nameHMACSHA256b = "System.Security.Cryptography.HMACSHA256";
168         private const string nameHMACSHA384a = "HMACSHA384";
169         private const string nameHMACSHA384b = "System.Security.Cryptography.HMACSHA384";
170         private const string nameHMACSHA512a = "HMACSHA512";
171         private const string nameHMACSHA512b = "System.Security.Cryptography.HMACSHA512";
172
173         private const string urlXmlDsig = "http://www.w3.org/2000/09/xmldsig#";
174         // LAMESPEC: undocumented URLs in CryptoConfig
175         private const string urlDSASHA1 = urlXmlDsig + "dsa-sha1";                      // no space
176         private const string urlRSASHA1 = urlXmlDsig + "rsa-sha1";                      // no space
177         private const string urlSHA1 = urlXmlDsig + "sha1";                             // no space
178         private const string urlC14N = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"; 
179         private const string urlC14NWithComments = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
180         private const string urlBase64 = "http://www.w3.org/2000/09/xmldsig#base64";
181         private const string urlXPath = "http://www.w3.org/TR/1999/REC-xpath-19991116";
182         private const string urlXslt = "http://www.w3.org/TR/1999/REC-xslt-19991116";
183         private const string urlEnveloped = urlXmlDsig + "enveloped-signature";         // no space
184         private const string urlXmlDecryption = "http://www.w3.org/2002/07/decrypt#XML";
185         private const string urlExcC14NWithComments = "http://www.w3.org/2001/10/xml-exc-c14n#WithComments";
186         private const string urlExcC14N = "http://www.w3.org/2001/10/xml-exc-c14n#";
187         private const string urlSHA256 = "http://www.w3.org/2001/04/xmlenc#sha256";
188         private const string urlSHA512 = "http://www.w3.org/2001/04/xmlenc#sha512";
189         private const string urlHMACSHA256 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
190         private const string urlHMACSHA384 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384";
191         private const string urlHMACSHA512 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512";
192         private const string urlHMACRIPEMD160 = "http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160";
193
194         // LAMESPEC: only documentated in ".NET Framework Security" book
195         private const string urlX509Data = urlXmlDsig + " X509Data";                    // space is required
196         private const string urlKeyName = urlXmlDsig + " KeyName";                      // space is required
197         private const string urlKeyValueDSA = urlXmlDsig + " KeyValue/DSAKeyValue";     // space is required
198         private const string urlKeyValueRSA = urlXmlDsig + " KeyValue/RSAKeyValue";     // space is required
199         private const string urlRetrievalMethod = urlXmlDsig + " RetrievalMethod";      // space is required
200
201         // new (2.0) X509 certificate extensions
202         private const string oidX509SubjectKeyIdentifier = "2.5.29.14";
203         private const string oidX509KeyUsage = "2.5.29.15";
204         private const string oidX509BasicConstraints = "2.5.29.19";
205         private const string oidX509EnhancedKeyUsage = "2.5.29.37";
206
207         private const string nameX509SubjectKeyIdentifier = defaultNamespace + "X509Certificates.X509SubjectKeyIdentifierExtension, " + Consts.AssemblySystem;
208         private const string nameX509KeyUsage = defaultNamespace + "X509Certificates.X509KeyUsageExtension, " + Consts.AssemblySystem;
209         private const string nameX509BasicConstraints = defaultNamespace + "X509Certificates.X509BasicConstraintsExtension, " + Consts.AssemblySystem;
210         private const string nameX509EnhancedKeyUsage = defaultNamespace + "X509Certificates.X509EnhancedKeyUsageExtension, " + Consts.AssemblySystem;
211
212         // new (2.0) X509 Chain
213         private const string nameX509Chain = "X509Chain";
214         private const string defaultX509Chain = defaultNamespace + "X509Certificates.X509Chain, " + Consts.AssemblySystem;
215
216         static CryptoConfig () 
217         {
218                 // lock(this) is bad
219                 // http://msdn.microsoft.com/library/en-us/dnaskdr/html/askgui06032003.asp?frame=true
220                 lockObject = new object ();
221         }
222
223         private static void Initialize () 
224         {
225                 Hashtable algorithms = new Hashtable (new CaseInsensitiveHashCodeProvider (), new CaseInsensitiveComparer ());
226                 // see list @ http://msdn.microsoft.com/library/en-us/cpref/html/
227                 // frlrfSystemSecurityCryptographyCryptoConfigClassTopic.asp
228                 algorithms.Add (nameSHA1a, defaultSHA1);
229                 algorithms.Add (nameSHA1b, defaultSHA1);
230                 algorithms.Add (nameSHA1c, defaultSHA1);
231                 algorithms.Add (nameSHA1d, defaultSHA1);
232
233                 algorithms.Add (nameMD5a, defaultMD5);
234                 algorithms.Add (nameMD5b, defaultMD5);
235
236                 algorithms.Add (nameSHA256a, defaultSHA256);
237                 algorithms.Add (nameSHA256b, defaultSHA256);
238                 algorithms.Add (nameSHA256c, defaultSHA256);
239
240                 algorithms.Add (nameSHA384a, defaultSHA384);
241                 algorithms.Add (nameSHA384b, defaultSHA384);
242                 algorithms.Add (nameSHA384c, defaultSHA384);
243
244                 algorithms.Add (nameSHA512a, defaultSHA512);
245                 algorithms.Add (nameSHA512b, defaultSHA512);
246                 algorithms.Add (nameSHA512c, defaultSHA512);
247
248                 algorithms.Add (nameRSAa, defaultRSA);
249                 algorithms.Add (nameRSAb, defaultRSA); 
250                 algorithms.Add (nameRSAc, defaultRSA);
251
252                 algorithms.Add (nameDSAa, defaultDSA);  
253                 algorithms.Add (nameDSAb, defaultDSA);  
254
255                 algorithms.Add (nameDESa, defaultDES);
256                 algorithms.Add (nameDESb, defaultDES);
257
258                 algorithms.Add (name3DESa, default3DES);
259                 algorithms.Add (name3DESb, default3DES);
260                 algorithms.Add (name3DESc, default3DES);
261                 algorithms.Add (name3DESd, default3DES);
262
263                 algorithms.Add (nameRC2a, defaultRC2);
264                 algorithms.Add (nameRC2b, defaultRC2);
265
266                 algorithms.Add (nameAESa, defaultAES);  
267                 algorithms.Add (nameAESb, defaultAES);
268                 // LAMESPEC SymmetricAlgorithm documented as TripleDESCryptoServiceProvider
269                 algorithms.Add (nameAESc, defaultAES);
270
271                 // LAMESPEC These names aren't documented but (hint) the classes also have
272                 // static Create methods. So logically they should (and are) here.
273                 algorithms.Add (nameRNGa, defaultRNG);
274                 algorithms.Add (nameRNGb, defaultRNG);
275                 algorithms.Add (nameKeyHasha, defaultHMAC);
276                 algorithms.Add (nameHMACSHA1a, defaultHMAC);
277                 algorithms.Add (nameHMACSHA1b, defaultHMAC);
278                 algorithms.Add (nameMAC3DESa, defaultMAC3DES);
279                 algorithms.Add (nameMAC3DESb, defaultMAC3DES);
280                 algorithms.Add (nameRIPEMD160a, defaultRIPEMD160);
281                 algorithms.Add (nameRIPEMD160b, defaultRIPEMD160);
282                 algorithms.Add (nameRIPEMD160c, defaultRIPEMD160);
283                 algorithms.Add (nameHMACb, defaultHMAC);
284                 algorithms.Add (nameHMACMD5a, defaultHMACMD5);
285                 algorithms.Add (nameHMACMD5b, defaultHMACMD5);
286                 algorithms.Add (nameHMACRIPEMD160a, defaultHMACRIPEMD160);
287                 algorithms.Add (nameHMACRIPEMD160b, defaultHMACRIPEMD160);
288                 algorithms.Add (nameHMACSHA256a, defaultHMACSHA256);
289                 algorithms.Add (nameHMACSHA256b, defaultHMACSHA256);
290                 algorithms.Add (nameHMACSHA384a, defaultHMACSHA384);
291                 algorithms.Add (nameHMACSHA384b, defaultHMACSHA384);
292                 algorithms.Add (nameHMACSHA512a, defaultHMACSHA512);
293                 algorithms.Add (nameHMACSHA512b, defaultHMACSHA512);
294
295                 // LAMESPEC These URLs aren't documented but (hint) installing the WSDK
296                 // add some of the XMLDSIG urls into machine.config (and they make a LOT
297                 // of sense for implementing XMLDSIG in System.Security.Cryptography.Xml)
298                 algorithms.Add (urlDSASHA1, defaultDSASigDesc); 
299                 algorithms.Add (urlRSASHA1, defaultRSASigDesc);
300                 algorithms.Add (urlSHA1, defaultSHA1);
301                 algorithms.Add (urlC14N, defaultC14N);
302                 algorithms.Add (urlC14NWithComments, defaultC14NWithComments);
303                 algorithms.Add (urlBase64, defaultBase64);
304                 algorithms.Add (urlXPath, defaultXPath);
305                 algorithms.Add (urlXslt, defaultXslt);
306                 algorithms.Add (urlEnveloped, defaultEnveloped);
307                 algorithms.Add (urlExcC14N, defaultExcC14N);
308                 algorithms.Add (urlExcC14NWithComments, defaultExcC14NWithComments);
309                 algorithms.Add (urlXmlDecryption, defaultXmlDecryption);
310                 algorithms.Add (urlSHA256, defaultSHA256);
311                 // xmlenc does not include a definition for SHA384
312                 algorithms.Add (urlSHA512, defaultSHA512);
313                 algorithms.Add (urlHMACSHA256, defaultHMACSHA256);
314                 algorithms.Add (urlHMACSHA384, defaultHMACSHA384);
315                 algorithms.Add (urlHMACSHA512, defaultHMACSHA512);
316                 algorithms.Add (urlHMACRIPEMD160, defaultHMACRIPEMD160);
317                 // LAMESPEC: only documentated in ".NET Framework Security" book
318                 algorithms.Add (urlX509Data, defaultX509Data);
319                 algorithms.Add (urlKeyName, defaultKeyName);
320                 algorithms.Add (urlKeyValueDSA, defaultKeyValueDSA);
321                 algorithms.Add (urlKeyValueRSA, defaultKeyValueRSA);
322                 algorithms.Add (urlRetrievalMethod, defaultRetrievalMethod);
323
324                 // note: X.509 extensions aren't part of OID but names
325                 algorithms.Add (oidX509SubjectKeyIdentifier, nameX509SubjectKeyIdentifier);
326                 algorithms.Add (oidX509KeyUsage, nameX509KeyUsage);
327                 algorithms.Add (oidX509BasicConstraints, nameX509BasicConstraints);
328                 algorithms.Add (oidX509EnhancedKeyUsage, nameX509EnhancedKeyUsage);
329                 // note: the default X.509Chain can also be created this way
330                 algorithms.Add (nameX509Chain, defaultX509Chain);
331
332                 Hashtable oid = new Hashtable (new CaseInsensitiveHashCodeProvider (), new CaseInsensitiveComparer ());
333
334                 // comments here are to match with MS implementation (but not with doc)
335                 // LAMESPEC: only HashAlgorithm seems to have their OID included
336                 oid.Add (defaultSHA1, oidSHA1);
337                 oid.Add (managedSHA1, oidSHA1);
338                 oid.Add (nameSHA1b, oidSHA1);
339                 oid.Add (nameSHA1c, oidSHA1);
340
341                 oid.Add (defaultMD5, oidMD5);
342                 oid.Add (nameMD5a, oidMD5);
343                 oid.Add (nameMD5b, oidMD5);
344
345                 oid.Add (defaultSHA256, oidSHA256);
346                 oid.Add (nameSHA256a, oidSHA256);
347                 oid.Add (nameSHA256c, oidSHA256);
348
349                 oid.Add (defaultSHA384, oidSHA384);
350                 oid.Add (nameSHA384a, oidSHA384);
351                 oid.Add (nameSHA384c, oidSHA384);
352
353                 oid.Add (defaultSHA512, oidSHA512);
354                 oid.Add (nameSHA512a, oidSHA512);
355                 oid.Add (nameSHA512c, oidSHA512);
356
357                 // surprise! documented in ".NET Framework Security" book
358                 oid.Add (name3DESKeyWrap, oid3DESKeyWrap);
359
360                 oid.Add (nameDESa, oidDES);
361                 oid.Add (name3DESb, oid3DES);
362                 oid.Add (nameRC2a, oidRC2);
363
364                 // Add/modify the config as specified by machine.config
365                 string config = Environment.GetMachineConfigPath ();
366                 LoadConfig (config, algorithms, oid);
367
368                 // update
369                 CryptoConfig.algorithms = algorithms;
370                 CryptoConfig.oid = oid;
371         }
372
373         [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
374         private static void LoadConfig (string filename, Hashtable algorithms, Hashtable oid)
375         {
376                 if (!File.Exists (filename))
377                         return;
378
379                 try {
380                         using (TextReader reader = new StreamReader (filename)) {
381                                 CryptoHandler handler = new CryptoHandler (algorithms, oid);
382                                 SmallXmlParser parser = new SmallXmlParser ();
383                                 parser.Parse (reader, handler);
384                         }
385                 }
386                 catch {
387                 }
388         }
389
390         public static object CreateFromName (string name)
391         {
392                 return CreateFromName (name, null);
393         }
394
395         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
396         public static object CreateFromName (string name, params object[] args)
397         {
398                 if (name == null)
399                         throw new ArgumentNullException ("name");
400
401                 lock (lockObject) {
402                         if (algorithms == null) {
403                                 Initialize ();
404                         }
405                 }
406         
407                 try {
408                         Type algoClass = null;
409                         string algo = (string) algorithms [name];
410                         // do we have an entry
411                         if (algo == null)
412                                 algo = name;
413                         algoClass = Type.GetType (algo);
414                         // call the constructor for the type
415                         return Activator.CreateInstance (algoClass, args);
416                 }
417                 catch {
418                         // method doesn't throw any exception
419                         return null;
420                 }
421         }
422
423         public static string MapNameToOID (string name)
424         {
425                 if (name == null)
426                         throw new ArgumentNullException ("name");
427
428                 lock (lockObject) {
429                         if (oid == null) {
430                                 Initialize ();
431                         }
432                 }
433
434                 return (string)oid [name];
435         }
436
437         class CryptoHandler: SmallXmlParser.IContentHandler {
438
439                 private Hashtable algorithms;
440                 private Hashtable oid;
441                 private Hashtable names;
442                 private Hashtable classnames;
443                 int level;
444
445                 public CryptoHandler (Hashtable algorithms, Hashtable oid)
446                 {
447                         this.algorithms = algorithms;
448                         this.oid = oid;
449                         // temporary tables to reconstruct algorithms
450                         names = new Hashtable ();
451                         classnames = new Hashtable ();
452                 }
453
454                 public void OnStartParsing (SmallXmlParser parser)
455                 {
456                         // don't care
457                 }
458
459                 public void OnEndParsing (SmallXmlParser parser)
460                 {
461                         foreach (DictionaryEntry de in names) {
462                                 try {
463                                         algorithms.Add (de.Key, classnames[de.Value]);
464                                 }
465                                 catch {
466                                 }
467                         }
468                         // matching is done, data no more required
469                         names.Clear ();
470                         classnames.Clear ();
471                 }
472
473                 private string Get (SmallXmlParser.IAttrList attrs, string name)
474                 {
475                         for (int i = 0; i < attrs.Names.Length; i++) {
476                                 if (attrs.Names[i] == name)
477                                         return attrs.Values[i];
478                         }
479                         return String.Empty;
480                 }
481
482                 public void OnStartElement (string name, SmallXmlParser.IAttrList attrs)
483                 {
484                         switch (level) {
485                         case 0:
486                                 if (name == "configuration")
487                                         level++;
488                                 break;
489                         case 1:
490                                 if (name == "mscorlib")
491                                         level++;
492                                 break;
493                         case 2:
494                                 if (name == "cryptographySettings")
495                                         level++;
496                                 break;
497                         case 3:
498                                 if (name == "oidMap")
499                                         level++;
500                                 else if (name == "cryptoNameMapping")
501                                         level++;
502                                 break;
503                         case 4:
504                                 if (name == "oidEntry") {
505                                         oid.Add (Get (attrs, "name"), Get (attrs, "OID"));
506                                 } else if (name == "nameEntry") {
507                                         names.Add (Get (attrs, "name"), Get (attrs, "class"));
508                                 } else if (name == "cryptoClasses") {
509                                         level++;
510                                 }
511                                 break;
512                         case 5:
513                                 if (name == "cryptoClass")
514                                         classnames.Add (attrs.Names[0], attrs.Values[0]);
515                                 break;
516                         }
517                 }
518
519                 public void OnEndElement (string name)
520                 {
521                         // parser will make sure the XML structure is respected
522                         switch (level) {
523                         case 1:
524                                 if (name == "configuration")
525                                         level--;
526                                 break;
527                         case 2:
528                                 if (name == "mscorlib")
529                                         level--;
530                                 break;
531                         case 3:
532                                 if (name == "cryptographySettings")
533                                         level--;
534                                 break;
535                         case 4:
536                                 if ((name == "oidMap") || (name == "cryptoNameMapping"))
537                                         level--;
538                                 break;
539                         case 5:
540                                 if (name == "cryptoClasses")
541                                         level--;
542                                 break;
543                         }
544                 }
545
546                 public void OnProcessingInstruction (string name, string text)
547                 {
548                         // don't care
549                 }
550
551                 public void OnChars (string text)
552                 {
553                         // don't care
554                 }
555
556                 public void OnIgnorableWhitespace (string text)
557                 {
558                         // don't care
559                 }
560         }
561 }
562 }
563
564 #endif
565