2 // CryptoConfig.cs: Handles cryptographic implementations and OIDs mappings.
5 // Sebastien Pouliot (sebastien@xamarin.com)
6 // Tim Coleman (tim@timcoleman.com)
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)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
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
39 using System.Runtime.InteropServices;
40 using System.Security.Permissions;
42 namespace System.Security.Cryptography {
45 public partial class CryptoConfig {
47 // try to avoid hitting the CreateFromName overloads to help the linker
49 public static object CreateFromName (string name)
51 return CreateFromName (name, null);
54 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
55 public static object CreateFromName (string name, params object[] args)
58 throw new ArgumentNullException ("name");
60 switch (name.ToLowerInvariant ()) {
61 case "system.security.cryptography.dsacryptoserviceprovider":
62 case "system.security.cryptography.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":
75 return new DESCryptoServiceProvider ();
76 case "system.security.cryptography.hmacmd5":
78 return new HMACMD5 ();
79 case "system.security.cryptography.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":
87 return new HMACSHA1 ();
88 case "system.security.cryptography.hmacsha256":
90 case "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256":
91 return new HMACSHA256 ();
92 case "system.security.cryptography.hmacsha384":
94 case "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384":
95 return new HMACSHA384 ();
96 case "system.security.cryptography.hmacsha512":
98 case "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512":
99 return new HMACSHA512 ();
100 case "system.security.cryptography.mactripledes":
102 return new MACTripleDES ();
103 case "system.security.cryptography.md5cryptoserviceprovider":
104 case "system.security.cryptography.md5":
106 return new MD5CryptoServiceProvider ();
107 case "system.security.cryptography.rc2cryptoserviceprovider":
108 case "system.security.cryptography.rc2":
110 return new RC2CryptoServiceProvider ();
111 case "system.security.cryptography.symmetricalgorithm":
112 case "system.security.cryptography.rijndaelmanaged":
113 case "system.security.cryptography.rijndael":
115 return new RijndaelManaged ();
116 case "system.security.cryptography.ripemd160managed":
117 case "system.security.cryptography.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":
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":
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":
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":
155 return new SHA384Managed ();
156 case "system.security.cryptography.sha512managed":
157 case "system.security.cryptography.sha512":
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":
167 return new TripleDESCryptoServiceProvider ();
169 name = "System.Security.Cryptography.X509Certificates.X509Chain, System";
172 name = "System.Security.Cryptography.AesManaged, System.Core";
177 // last resort, the request type might be available (if care is taken for the type not to be linked
178 // away) and that can allow some 3rd party code to work (e.g. extra algorithms) and make a few more
180 return Activator.CreateInstance (Type.GetType (name));
183 // method doesn't throw any exception
188 public static string MapNameToOID (string name)
191 throw new ArgumentNullException ("name");
193 switch (name.ToLowerInvariant ()) {
194 case "system.security.cryptography.sha1cryptoserviceprovider":
195 case "system.security.cryptography.sha1managed":
196 case "system.security.cryptography.sha1":
198 return "1.3.14.3.2.26";
199 case "system.security.cryptography.md5cryptoserviceprovider":
200 case "system.security.cryptography.md5":
202 return "1.2.840.113549.2.5";
203 case "system.security.cryptography.sha256managed":
204 case "system.security.cryptography.sha256":
206 return "2.16.840.1.101.3.4.2.1";
207 case "system.security.cryptography.sha384managed":
208 case "system.security.cryptography.sha384":
210 return "2.16.840.1.101.3.4.2.2";
211 case "system.security.cryptography.sha512managed":
212 case "system.security.cryptography.sha512":
214 return "2.16.840.1.101.3.4.2.3";
215 case "system.security.cryptography.ripemd160managed":
216 case "system.security.cryptography.ripemd160":
218 return "1.3.36.3.2.1";
219 case "tripledeskeywrap":
220 return "1.2.840.113549.1.9.16.3.6";
222 return "1.3.14.3.2.7";
224 return "1.2.840.113549.3.7";
226 return "1.2.840.113549.3.2";