[sgen] Make sure we don't sweep a block if we're not supposed to
[mono.git] / mcs / class / corlib / System.Security.Cryptography / CryptoConfig.fullaot.cs
1 //
2 // CryptoConfig.cs: Handles cryptographic implementations and OIDs mappings.
3 //
4 // Authors:
5 //      Sebastien Pouliot (sebastien@xamarin.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 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 // This is a special version of CryptoConfig that is not configurable and
36 // every "choice" is statiscally compiled. As long as CreateFromName is not
37 // used the linker will be able to eliminate the crypto code from the applications
38
39 using System.Runtime.InteropServices;
40 using System.Security.Permissions;
41
42 namespace System.Security.Cryptography {
43
44         [ComVisible (true)]
45         public partial class CryptoConfig {
46
47                 // try to avoid hitting the CreateFromName overloads to help the linker
48
49                 public static object CreateFromName (string name)
50                 {
51                         return CreateFromName (name, null);
52                 }
53
54                 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
55                 public static object CreateFromName (string name, params object[] args)
56                 {
57                         if (name == null)
58                                 throw new ArgumentNullException ("name");
59
60                         switch (name.ToLowerInvariant ()) {
61                         case "system.security.cryptography.dsacryptoserviceprovider":
62                         case "system.security.cryptography.dsa":
63                         case "dsa":
64                                 return new DSACryptoServiceProvider ();
65                         case "system.security.cryptography.dsasignaturedeformatter":
66                                 return new DSASignatureDeformatter ();
67                         case "system.security.cryptography.dsasignatureformatter":
68                                 return new DSASignatureFormatter ();
69                         case "system.security.cryptography.dsasignaturedescription":
70                         case "http://www.w3.org/2000/09/xmldsig#dsa-sha1":
71                                 return new DSASignatureDescription ();
72                         case "system.security.cryptography.descryptoserviceprovider":
73                         case "system.security.cryptography.des":
74                         case "des":
75                                 return new DESCryptoServiceProvider ();
76                         case "system.security.cryptography.hmacmd5":
77                         case "hmacmd5":
78                                 return new HMACMD5 ();
79                         case "system.security.cryptography.hmacripemd160":
80                         case "hmacripemd160":
81                         case "http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160":
82                                 return new HMACRIPEMD160 ();
83                         case "system.security.cryptography.keyedhashalgorithm":
84                         case "system.security.cryptography.hmac":
85                         case "system.security.cryptography.hmacsha1":
86                         case "hmacsha1":
87                                 return new HMACSHA1 ();
88                         case "system.security.cryptography.hmacsha256":
89                         case "hmacsha256":
90                         case "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256":
91                                 return new HMACSHA256 ();
92                         case "system.security.cryptography.hmacsha384":
93                         case "hmacsha384":
94                         case "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384":
95                                 return new HMACSHA384 ();
96                         case "system.security.cryptography.hmacsha512":
97                         case "hmacsha512":
98                         case "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512":
99                                 return new HMACSHA512 ();
100                         case "system.security.cryptography.mactripledes":
101                         case "mactripledes":
102                                 return new MACTripleDES ();
103                         case "system.security.cryptography.md5cryptoserviceprovider":
104                         case "system.security.cryptography.md5":
105                         case "md5":
106                                 return new MD5CryptoServiceProvider ();
107                         case "system.security.cryptography.rc2cryptoserviceprovider":
108                         case "system.security.cryptography.rc2":
109                         case "rc2":
110                                 return new RC2CryptoServiceProvider ();
111                         case "system.security.cryptography.symmetricalgorithm":
112                         case "system.security.cryptography.rijndaelmanaged":
113                         case "system.security.cryptography.rijndael":
114                         case "rijndael":
115                                 return new RijndaelManaged ();
116                         case "system.security.cryptography.ripemd160managed":
117                         case "system.security.cryptography.ripemd160":
118                         case "ripemd-160":
119                         case "ripemd160":
120                                 return new RIPEMD160Managed ();
121                         case "system.security.cryptography.rngcryptoserviceprovider":
122                         case "system.security.cryptography.randomnumbergenerator":
123                         case "randomnumbergenerator":
124                                 return new RNGCryptoServiceProvider ();
125                         case "system.security.cryptography.asymmetricalgorithm":
126                         case "system.security.cryptography.rsa":
127                         case "rsa":
128                                 return new RSACryptoServiceProvider ();
129                         case "system.security.cryptography.rsapkcs1signaturedeformatter":
130                                 return new RSAPKCS1SignatureDeformatter ();
131                         case "system.security.cryptography.rsapkcs1signatureformatter":
132                                 return new RSAPKCS1SignatureFormatter ();
133                         case "system.security.cryptography.rsapkcs1sha1signaturedescription":
134                         case "http://www.w3.org/2000/09/xmldsig#rsa-sha1":
135                                 return new RSAPKCS1SHA1SignatureDescription ();
136                         case "system.security.cryptography.hashalgorithm":
137                         case "system.security.cryptography.sha1":
138                         case "system.security.cryptography.sha1cryptoserviceprovider":
139                         case "sha1":
140                         case "sha":
141                         case "http://www.w3.org/2000/09/xmldsig#sha1":
142                                 return new SHA1CryptoServiceProvider ();
143                         case "system.security.cryptography.sha1managed":
144                                 return new SHA1Managed ();
145                         case "system.security.cryptography.sha256managed":
146                         case "system.security.cryptography.sha256":
147                         case "sha256":
148                         case "sha-256":
149                         case "http://www.w3.org/2001/04/xmlenc#sha256":
150                                 return new SHA256Managed ();
151                         case "system.security.cryptography.sha384managed":
152                         case "system.security.cryptography.sha384":
153                         case "sha384":
154                         case "sha-384":
155                                 return new SHA384Managed ();
156                         case "system.security.cryptography.sha512managed":
157                         case "system.security.cryptography.sha512":
158                         case "sha512":
159                         case "sha-512":
160                         case "http://www.w3.org/2001/04/xmlenc#sha512":
161                                 return new SHA512Managed ();
162                         case "system.security.cryptography.tripledescryptoserviceprovider":
163                         case "system.security.cryptography.tripledes":
164                         case "triple des":
165                         case "tripledes":
166                         case "3des":
167                                 return new TripleDESCryptoServiceProvider ();
168                         case "x509chain":
169                                 name = "System.Security.Cryptography.X509Certificates.X509Chain, System";
170                                 break;
171                         case "aes":
172 #if MOBILE_STATIC
173                                 name = "System.Security.Cryptography.AesCryptoServiceProvider, System.Core";
174 #else
175                                 name = "System.Security.Cryptography.AesManaged, System.Core";
176 #endif
177                                 break;
178                         }
179
180                         try {
181                                 // last resort, the request type might be available (if care is taken for the type not to be linked 
182                                 // away) and that can allow some 3rd party code to work (e.g. extra algorithms) and make a few more
183                                 // unit tests happy
184                                 return Activator.CreateInstance (Type.GetType (name));
185                         }
186                         catch {
187                                 // method doesn't throw any exception
188                                 return null;
189                         }
190                 }
191
192                 internal static string MapNameToOID (string name, object arg)
193                 {
194                         return MapNameToOID (name);
195                 }
196
197                 public static string MapNameToOID (string name)
198                 {
199                         if (name == null)
200                                 throw new ArgumentNullException ("name");
201
202                         switch (name.ToLowerInvariant ()) {
203                         case "system.security.cryptography.sha1cryptoserviceprovider":
204                         case "system.security.cryptography.sha1managed":
205                         case "system.security.cryptography.sha1":
206                         case "sha1":
207                                 return "1.3.14.3.2.26";
208                         case "system.security.cryptography.md5cryptoserviceprovider":
209                         case "system.security.cryptography.md5":
210                         case "md5":
211                                 return "1.2.840.113549.2.5";
212                         case "system.security.cryptography.sha256managed":
213                         case "system.security.cryptography.sha256":
214                         case "sha256":
215                                 return "2.16.840.1.101.3.4.2.1";
216                         case "system.security.cryptography.sha384managed":
217                         case "system.security.cryptography.sha384":
218                         case "sha384":
219                                 return "2.16.840.1.101.3.4.2.2";
220                         case "system.security.cryptography.sha512managed":
221                         case "system.security.cryptography.sha512":
222                         case "sha512":
223                                 return "2.16.840.1.101.3.4.2.3";
224                         case "system.security.cryptography.ripemd160managed":
225                         case "system.security.cryptography.ripemd160":
226                         case "ripemd160":
227                                 return "1.3.36.3.2.1";
228                         case "tripledeskeywrap":
229                                 return "1.2.840.113549.1.9.16.3.6";
230                         case "des":
231                                 return "1.3.14.3.2.7";
232                         case "tripledes":
233                                 return "1.2.840.113549.3.7";
234                         case "rc2":
235                                 return "1.2.840.113549.3.2";
236                         default:
237                                 return null;
238                         }
239                 }
240         }
241 }
242
243 #endif