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