mscorlib(4.0.0.0) API with additions
CryptoConfig.cs
1 using Microsoft.Win32;
2 using System.Collections;
5 using System.IO;
6 using System.Reflection;
10 using System.Security.Util;
11 using System.Threading;
12 
14 {
16  [ComVisible(true)]
17  public class CryptoConfig
18  {
19  private static volatile Dictionary<string, string> defaultOidHT = null;
20 
21  private static volatile Dictionary<string, object> defaultNameHT = null;
22 
23  private static volatile Dictionary<string, string> machineOidHT = null;
24 
25  private static volatile Dictionary<string, string> machineNameHT = null;
26 
28 
30 
31  private const string MachineConfigFilename = "machine.config";
32 
33  private static volatile string version = null;
34 
35  private static volatile bool s_fipsAlgorithmPolicy;
36 
37  private static volatile bool s_haveFipsAlgorithmPolicy;
38 
39  private static object s_InternalSyncObject;
40 
44  public static bool AllowOnlyFipsAlgorithms
45  {
46  [SecuritySafeCritical]
47  get
48  {
49  if (!s_haveFipsAlgorithmPolicy)
50  {
51  if (Utils._GetEnforceFipsPolicySetting())
52  {
54  {
55  bool pfEnabled;
56  uint num = Win32Native.BCryptGetFipsAlgorithmMode(out pfEnabled);
57  bool flag = num == 0 || num == 3221225524u;
58  s_fipsAlgorithmPolicy = (!flag | pfEnabled);
59  s_haveFipsAlgorithmPolicy = true;
60  }
61  else
62  {
63  s_fipsAlgorithmPolicy = Utils.ReadLegacyFipsPolicy();
64  s_haveFipsAlgorithmPolicy = true;
65  }
66  }
67  else
68  {
69  s_fipsAlgorithmPolicy = false;
70  s_haveFipsAlgorithmPolicy = true;
71  }
72  }
73  return s_fipsAlgorithmPolicy;
74  }
75  }
76 
77  private static string Version
78  {
79  [SecurityCritical]
80  get
81  {
82  if (version == null)
83  {
84  version = ((RuntimeType)typeof(CryptoConfig)).GetRuntimeAssembly().GetVersion().ToString();
85  }
86  return version;
87  }
88  }
89 
90  private static object InternalSyncObject
91  {
92  get
93  {
94  if (s_InternalSyncObject == null)
95  {
96  object value = new object();
97  Interlocked.CompareExchange(ref s_InternalSyncObject, value, null);
98  }
99  return s_InternalSyncObject;
100  }
101  }
102 
103  private static Dictionary<string, string> DefaultOidHT
104  {
105  get
106  {
107  if (defaultOidHT == null)
108  {
109  Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
110  dictionary.Add("SHA", "1.3.14.3.2.26");
111  dictionary.Add("SHA1", "1.3.14.3.2.26");
112  dictionary.Add("System.Security.Cryptography.SHA1", "1.3.14.3.2.26");
113  dictionary.Add("System.Security.Cryptography.SHA1CryptoServiceProvider", "1.3.14.3.2.26");
114  dictionary.Add("System.Security.Cryptography.SHA1Cng", "1.3.14.3.2.26");
115  dictionary.Add("System.Security.Cryptography.SHA1Managed", "1.3.14.3.2.26");
116  dictionary.Add("SHA256", "2.16.840.1.101.3.4.2.1");
117  dictionary.Add("System.Security.Cryptography.SHA256", "2.16.840.1.101.3.4.2.1");
118  dictionary.Add("System.Security.Cryptography.SHA256CryptoServiceProvider", "2.16.840.1.101.3.4.2.1");
119  dictionary.Add("System.Security.Cryptography.SHA256Cng", "2.16.840.1.101.3.4.2.1");
120  dictionary.Add("System.Security.Cryptography.SHA256Managed", "2.16.840.1.101.3.4.2.1");
121  dictionary.Add("SHA384", "2.16.840.1.101.3.4.2.2");
122  dictionary.Add("System.Security.Cryptography.SHA384", "2.16.840.1.101.3.4.2.2");
123  dictionary.Add("System.Security.Cryptography.SHA384CryptoServiceProvider", "2.16.840.1.101.3.4.2.2");
124  dictionary.Add("System.Security.Cryptography.SHA384Cng", "2.16.840.1.101.3.4.2.2");
125  dictionary.Add("System.Security.Cryptography.SHA384Managed", "2.16.840.1.101.3.4.2.2");
126  dictionary.Add("SHA512", "2.16.840.1.101.3.4.2.3");
127  dictionary.Add("System.Security.Cryptography.SHA512", "2.16.840.1.101.3.4.2.3");
128  dictionary.Add("System.Security.Cryptography.SHA512CryptoServiceProvider", "2.16.840.1.101.3.4.2.3");
129  dictionary.Add("System.Security.Cryptography.SHA512Cng", "2.16.840.1.101.3.4.2.3");
130  dictionary.Add("System.Security.Cryptography.SHA512Managed", "2.16.840.1.101.3.4.2.3");
131  dictionary.Add("RIPEMD160", "1.3.36.3.2.1");
132  dictionary.Add("System.Security.Cryptography.RIPEMD160", "1.3.36.3.2.1");
133  dictionary.Add("System.Security.Cryptography.RIPEMD160Managed", "1.3.36.3.2.1");
134  dictionary.Add("MD5", "1.2.840.113549.2.5");
135  dictionary.Add("System.Security.Cryptography.MD5", "1.2.840.113549.2.5");
136  dictionary.Add("System.Security.Cryptography.MD5CryptoServiceProvider", "1.2.840.113549.2.5");
137  dictionary.Add("System.Security.Cryptography.MD5Managed", "1.2.840.113549.2.5");
138  dictionary.Add("TripleDESKeyWrap", "1.2.840.113549.1.9.16.3.6");
139  dictionary.Add("RC2", "1.2.840.113549.3.2");
140  dictionary.Add("System.Security.Cryptography.RC2CryptoServiceProvider", "1.2.840.113549.3.2");
141  dictionary.Add("DES", "1.3.14.3.2.7");
142  dictionary.Add("System.Security.Cryptography.DESCryptoServiceProvider", "1.3.14.3.2.7");
143  dictionary.Add("TripleDES", "1.2.840.113549.3.7");
144  dictionary.Add("System.Security.Cryptography.TripleDESCryptoServiceProvider", "1.2.840.113549.3.7");
145  defaultOidHT = dictionary;
146  }
147  return defaultOidHT;
148  }
149  }
150 
151  private static Dictionary<string, object> DefaultNameHT
152  {
153  get
154  {
155  if (defaultNameHT == null)
156  {
157  Dictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
158  Type typeFromHandle = typeof(SHA1CryptoServiceProvider);
159  Type typeFromHandle2 = typeof(MD5CryptoServiceProvider);
160  Type typeFromHandle3 = typeof(RIPEMD160Managed);
161  Type typeFromHandle4 = typeof(HMACMD5);
162  Type typeFromHandle5 = typeof(HMACRIPEMD160);
163  Type typeFromHandle6 = typeof(HMACSHA1);
164  Type typeFromHandle7 = typeof(HMACSHA256);
165  Type typeFromHandle8 = typeof(HMACSHA384);
166  Type typeFromHandle9 = typeof(HMACSHA512);
167  Type typeFromHandle10 = typeof(MACTripleDES);
168  Type typeFromHandle11 = typeof(RSACryptoServiceProvider);
169  Type typeFromHandle12 = typeof(DSACryptoServiceProvider);
170  Type typeFromHandle13 = typeof(DESCryptoServiceProvider);
171  Type typeFromHandle14 = typeof(TripleDESCryptoServiceProvider);
172  Type typeFromHandle15 = typeof(RC2CryptoServiceProvider);
173  Type typeFromHandle16 = typeof(RijndaelManaged);
174  Type typeFromHandle17 = typeof(DSASignatureDescription);
175  Type typeFromHandle18 = typeof(RSAPKCS1SHA1SignatureDescription);
176  Type typeFromHandle19 = typeof(RSAPKCS1SHA256SignatureDescription);
177  Type typeFromHandle20 = typeof(RSAPKCS1SHA384SignatureDescription);
178  Type typeFromHandle21 = typeof(RSAPKCS1SHA512SignatureDescription);
179  Type typeFromHandle22 = typeof(RNGCryptoServiceProvider);
180  string value = "System.Security.Cryptography.AesCryptoServiceProvider, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
181  string value2 = "System.Security.Cryptography.AesManaged, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
182  string value3 = "System.Security.Cryptography.ECDiffieHellmanCng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
183  string value4 = "System.Security.Cryptography.ECDsaCng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
184  string value5 = "System.Security.Cryptography.MD5Cng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
185  string value6 = "System.Security.Cryptography.SHA1Cng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
186  string text = "System.Security.Cryptography.SHA256Cng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
187  string value7 = "System.Security.Cryptography.SHA256CryptoServiceProvider, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
188  string text2 = "System.Security.Cryptography.SHA384Cng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
189  string value8 = "System.Security.Cryptography.SHA384CryptoServiceProvider, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
190  string text3 = "System.Security.Cryptography.SHA512Cng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
191  string value9 = "System.Security.Cryptography.SHA512CryptoServiceProvider, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
192  bool allowOnlyFipsAlgorithms = AllowOnlyFipsAlgorithms;
193  object value10 = typeof(SHA256Managed);
194  if (allowOnlyFipsAlgorithms)
195  {
196  value10 = text;
197  }
198  object value11 = allowOnlyFipsAlgorithms ? ((object)text2) : ((object)typeof(SHA384Managed));
199  object value12 = allowOnlyFipsAlgorithms ? ((object)text3) : ((object)typeof(SHA512Managed));
200  string value13 = "System.Security.Cryptography.DpapiDataProtector, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
201  dictionary.Add("RandomNumberGenerator", typeFromHandle22);
202  dictionary.Add("System.Security.Cryptography.RandomNumberGenerator", typeFromHandle22);
203  dictionary.Add("SHA", typeFromHandle);
204  dictionary.Add("SHA1", typeFromHandle);
205  dictionary.Add("System.Security.Cryptography.SHA1", typeFromHandle);
206  dictionary.Add("System.Security.Cryptography.SHA1Cng", value6);
207  dictionary.Add("System.Security.Cryptography.HashAlgorithm", typeFromHandle);
208  dictionary.Add("MD5", typeFromHandle2);
209  dictionary.Add("System.Security.Cryptography.MD5", typeFromHandle2);
210  dictionary.Add("System.Security.Cryptography.MD5Cng", value5);
211  dictionary.Add("SHA256", value10);
212  dictionary.Add("SHA-256", value10);
213  dictionary.Add("System.Security.Cryptography.SHA256", value10);
214  dictionary.Add("System.Security.Cryptography.SHA256Cng", text);
215  dictionary.Add("System.Security.Cryptography.SHA256CryptoServiceProvider", value7);
216  dictionary.Add("SHA384", value11);
217  dictionary.Add("SHA-384", value11);
218  dictionary.Add("System.Security.Cryptography.SHA384", value11);
219  dictionary.Add("System.Security.Cryptography.SHA384Cng", text2);
220  dictionary.Add("System.Security.Cryptography.SHA384CryptoServiceProvider", value8);
221  dictionary.Add("SHA512", value12);
222  dictionary.Add("SHA-512", value12);
223  dictionary.Add("System.Security.Cryptography.SHA512", value12);
224  dictionary.Add("System.Security.Cryptography.SHA512Cng", text3);
225  dictionary.Add("System.Security.Cryptography.SHA512CryptoServiceProvider", value9);
226  dictionary.Add("RIPEMD160", typeFromHandle3);
227  dictionary.Add("RIPEMD-160", typeFromHandle3);
228  dictionary.Add("System.Security.Cryptography.RIPEMD160", typeFromHandle3);
229  dictionary.Add("System.Security.Cryptography.RIPEMD160Managed", typeFromHandle3);
230  dictionary.Add("System.Security.Cryptography.HMAC", typeFromHandle6);
231  dictionary.Add("System.Security.Cryptography.KeyedHashAlgorithm", typeFromHandle6);
232  dictionary.Add("HMACMD5", typeFromHandle4);
233  dictionary.Add("System.Security.Cryptography.HMACMD5", typeFromHandle4);
234  dictionary.Add("HMACRIPEMD160", typeFromHandle5);
235  dictionary.Add("System.Security.Cryptography.HMACRIPEMD160", typeFromHandle5);
236  dictionary.Add("HMACSHA1", typeFromHandle6);
237  dictionary.Add("System.Security.Cryptography.HMACSHA1", typeFromHandle6);
238  dictionary.Add("HMACSHA256", typeFromHandle7);
239  dictionary.Add("System.Security.Cryptography.HMACSHA256", typeFromHandle7);
240  dictionary.Add("HMACSHA384", typeFromHandle8);
241  dictionary.Add("System.Security.Cryptography.HMACSHA384", typeFromHandle8);
242  dictionary.Add("HMACSHA512", typeFromHandle9);
243  dictionary.Add("System.Security.Cryptography.HMACSHA512", typeFromHandle9);
244  dictionary.Add("MACTripleDES", typeFromHandle10);
245  dictionary.Add("System.Security.Cryptography.MACTripleDES", typeFromHandle10);
246  dictionary.Add("RSA", typeFromHandle11);
247  dictionary.Add("System.Security.Cryptography.RSA", typeFromHandle11);
248  dictionary.Add("System.Security.Cryptography.AsymmetricAlgorithm", typeFromHandle11);
249  dictionary.Add("DSA", typeFromHandle12);
250  dictionary.Add("System.Security.Cryptography.DSA", typeFromHandle12);
251  dictionary.Add("ECDsa", value4);
252  dictionary.Add("ECDsaCng", value4);
253  dictionary.Add("System.Security.Cryptography.ECDsaCng", value4);
254  dictionary.Add("ECDH", value3);
255  dictionary.Add("ECDiffieHellman", value3);
256  dictionary.Add("ECDiffieHellmanCng", value3);
257  dictionary.Add("System.Security.Cryptography.ECDiffieHellmanCng", value3);
258  dictionary.Add("DES", typeFromHandle13);
259  dictionary.Add("System.Security.Cryptography.DES", typeFromHandle13);
260  dictionary.Add("3DES", typeFromHandle14);
261  dictionary.Add("TripleDES", typeFromHandle14);
262  dictionary.Add("Triple DES", typeFromHandle14);
263  dictionary.Add("System.Security.Cryptography.TripleDES", typeFromHandle14);
264  dictionary.Add("RC2", typeFromHandle15);
265  dictionary.Add("System.Security.Cryptography.RC2", typeFromHandle15);
266  dictionary.Add("Rijndael", typeFromHandle16);
267  dictionary.Add("System.Security.Cryptography.Rijndael", typeFromHandle16);
268  dictionary.Add("System.Security.Cryptography.SymmetricAlgorithm", typeFromHandle16);
269  dictionary.Add("AES", value);
270  dictionary.Add("AesCryptoServiceProvider", value);
271  dictionary.Add("System.Security.Cryptography.AesCryptoServiceProvider", value);
272  dictionary.Add("AesManaged", value2);
273  dictionary.Add("System.Security.Cryptography.AesManaged", value2);
274  dictionary.Add("DpapiDataProtector", value13);
275  dictionary.Add("System.Security.Cryptography.DpapiDataProtector", value13);
276  dictionary.Add("http://www.w3.org/2000/09/xmldsig#dsa-sha1", typeFromHandle17);
277  dictionary.Add("System.Security.Cryptography.DSASignatureDescription", typeFromHandle17);
278  dictionary.Add("http://www.w3.org/2000/09/xmldsig#rsa-sha1", typeFromHandle18);
279  dictionary.Add("System.Security.Cryptography.RSASignatureDescription", typeFromHandle18);
280  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", typeFromHandle19);
281  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", typeFromHandle20);
282  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", typeFromHandle21);
283  dictionary.Add("http://www.w3.org/2000/09/xmldsig#sha1", typeFromHandle);
284  dictionary.Add("http://www.w3.org/2001/04/xmlenc#sha256", value10);
285  dictionary.Add("http://www.w3.org/2001/04/xmlenc#sha512", value12);
286  dictionary.Add("http://www.w3.org/2001/04/xmlenc#ripemd160", typeFromHandle3);
287  dictionary.Add("http://www.w3.org/2001/04/xmlenc#des-cbc", typeFromHandle13);
288  dictionary.Add("http://www.w3.org/2001/04/xmlenc#tripledes-cbc", typeFromHandle14);
289  dictionary.Add("http://www.w3.org/2001/04/xmlenc#kw-tripledes", typeFromHandle14);
290  dictionary.Add("http://www.w3.org/2001/04/xmlenc#aes128-cbc", typeFromHandle16);
291  dictionary.Add("http://www.w3.org/2001/04/xmlenc#kw-aes128", typeFromHandle16);
292  dictionary.Add("http://www.w3.org/2001/04/xmlenc#aes192-cbc", typeFromHandle16);
293  dictionary.Add("http://www.w3.org/2001/04/xmlenc#kw-aes192", typeFromHandle16);
294  dictionary.Add("http://www.w3.org/2001/04/xmlenc#aes256-cbc", typeFromHandle16);
295  dictionary.Add("http://www.w3.org/2001/04/xmlenc#kw-aes256", typeFromHandle16);
296  dictionary.Add("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", "System.Security.Cryptography.Xml.XmlDsigC14NTransform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
297  dictionary.Add("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", "System.Security.Cryptography.Xml.XmlDsigC14NWithCommentsTransform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
298  dictionary.Add("http://www.w3.org/2001/10/xml-exc-c14n#", "System.Security.Cryptography.Xml.XmlDsigExcC14NTransform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
299  dictionary.Add("http://www.w3.org/2001/10/xml-exc-c14n#WithComments", "System.Security.Cryptography.Xml.XmlDsigExcC14NWithCommentsTransform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
300  dictionary.Add("http://www.w3.org/2000/09/xmldsig#base64", "System.Security.Cryptography.Xml.XmlDsigBase64Transform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
301  dictionary.Add("http://www.w3.org/TR/1999/REC-xpath-19991116", "System.Security.Cryptography.Xml.XmlDsigXPathTransform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
302  dictionary.Add("http://www.w3.org/TR/1999/REC-xslt-19991116", "System.Security.Cryptography.Xml.XmlDsigXsltTransform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
303  dictionary.Add("http://www.w3.org/2000/09/xmldsig#enveloped-signature", "System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
304  dictionary.Add("http://www.w3.org/2002/07/decrypt#XML", "System.Security.Cryptography.Xml.XmlDecryptionTransform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
305  dictionary.Add("urn:mpeg:mpeg21:2003:01-REL-R-NS:licenseTransform", "System.Security.Cryptography.Xml.XmlLicenseTransform, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
306  dictionary.Add("http://www.w3.org/2000/09/xmldsig# X509Data", "System.Security.Cryptography.Xml.KeyInfoX509Data, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
307  dictionary.Add("http://www.w3.org/2000/09/xmldsig# KeyName", "System.Security.Cryptography.Xml.KeyInfoName, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
308  dictionary.Add("http://www.w3.org/2000/09/xmldsig# KeyValue/DSAKeyValue", "System.Security.Cryptography.Xml.DSAKeyValue, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
309  dictionary.Add("http://www.w3.org/2000/09/xmldsig# KeyValue/RSAKeyValue", "System.Security.Cryptography.Xml.RSAKeyValue, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
310  dictionary.Add("http://www.w3.org/2000/09/xmldsig# RetrievalMethod", "System.Security.Cryptography.Xml.KeyInfoRetrievalMethod, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
311  dictionary.Add("http://www.w3.org/2001/04/xmlenc# EncryptedKey", "System.Security.Cryptography.Xml.KeyInfoEncryptedKey, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
312  dictionary.Add("http://www.w3.org/2000/09/xmldsig#hmac-sha1", typeFromHandle6);
313  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#md5", typeFromHandle2);
314  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#sha384", value11);
315  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#hmac-md5", typeFromHandle4);
316  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160", typeFromHandle5);
317  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", typeFromHandle7);
318  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#hmac-sha384", typeFromHandle8);
319  dictionary.Add("http://www.w3.org/2001/04/xmldsig-more#hmac-sha512", typeFromHandle9);
320  dictionary.Add("2.5.29.10", "System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
321  dictionary.Add("2.5.29.19", "System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
322  dictionary.Add("2.5.29.14", "System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
323  dictionary.Add("2.5.29.15", "System.Security.Cryptography.X509Certificates.X509KeyUsageExtension, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
324  dictionary.Add("2.5.29.37", "System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
325  dictionary.Add("X509Chain", "System.Security.Cryptography.X509Certificates.X509Chain, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
326  dictionary.Add("1.2.840.113549.1.9.3", "System.Security.Cryptography.Pkcs.Pkcs9ContentType, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
327  dictionary.Add("1.2.840.113549.1.9.4", "System.Security.Cryptography.Pkcs.Pkcs9MessageDigest, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
328  dictionary.Add("1.2.840.113549.1.9.5", "System.Security.Cryptography.Pkcs.Pkcs9SigningTime, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
329  dictionary.Add("1.3.6.1.4.1.311.88.2.1", "System.Security.Cryptography.Pkcs.Pkcs9DocumentName, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
330  dictionary.Add("1.3.6.1.4.1.311.88.2.2", "System.Security.Cryptography.Pkcs.Pkcs9DocumentDescription, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
331  defaultNameHT = dictionary;
332  }
333  return defaultNameHT;
334  }
335  }
336 
337  [SecurityCritical]
338  private static void InitializeConfigInfo()
339  {
340  if (machineNameHT == null)
341  {
342  lock (InternalSyncObject)
343  {
344  if (machineNameHT == null)
345  {
346  ConfigNode configNode = OpenCryptoConfig();
347  if (configNode != null)
348  {
349  foreach (ConfigNode child in configNode.Children)
350  {
351  if (machineNameHT != null && machineOidHT != null)
352  {
353  break;
354  }
355  if (machineNameHT == null && string.Compare(child.Name, "cryptoNameMapping", StringComparison.Ordinal) == 0)
356  {
357  machineNameHT = InitializeNameMappings(child);
358  }
359  else if (machineOidHT == null && string.Compare(child.Name, "oidMap", StringComparison.Ordinal) == 0)
360  {
361  machineOidHT = InitializeOidMappings(child);
362  }
363  }
364  }
365  if (machineNameHT == null)
366  {
367  machineNameHT = new Dictionary<string, string>();
368  }
369  if (machineOidHT == null)
370  {
371  machineOidHT = new Dictionary<string, string>();
372  }
373  }
374  }
375  }
376  }
377 
384  [SecurityCritical]
385  public static void AddAlgorithm(Type algorithm, params string[] names)
386  {
387  if (algorithm == null)
388  {
389  throw new ArgumentNullException("algorithm");
390  }
391  if (!algorithm.IsVisible)
392  {
393  throw new ArgumentException(Environment.GetResourceString("Cryptography_AlgorithmTypesMustBeVisible"), "algorithm");
394  }
395  if (names == null)
396  {
397  throw new ArgumentNullException("names");
398  }
399  string[] array = new string[names.Length];
400  Array.Copy(names, array, array.Length);
401  string[] array2 = array;
402  foreach (string value in array2)
403  {
404  if (string.IsNullOrEmpty(value))
405  {
406  throw new ArgumentException(Environment.GetResourceString("Cryptography_AddNullOrEmptyName"));
407  }
408  }
409  lock (InternalSyncObject)
410  {
411  string[] array3 = array;
412  foreach (string key in array3)
413  {
414  appNameHT[key] = algorithm;
415  }
416  }
417  }
418 
425  [SecuritySafeCritical]
426  public static object CreateFromName(string name, params object[] args)
427  {
428  if (name == null)
429  {
430  throw new ArgumentNullException("name");
431  }
432  Type type = null;
433  InitializeConfigInfo();
434  lock (InternalSyncObject)
435  {
436  type = appNameHT.GetValueOrDefault(name);
437  }
438  if (type == null)
439  {
440  string valueOrDefault = machineNameHT.GetValueOrDefault(name);
441  if (valueOrDefault != null)
442  {
443  type = Type.GetType(valueOrDefault, throwOnError: false, ignoreCase: false);
444  if (type != null && !type.IsVisible)
445  {
446  type = null;
447  }
448  }
449  }
450  if (type == null)
451  {
452  object valueOrDefault2 = DefaultNameHT.GetValueOrDefault(name);
453  if (valueOrDefault2 != null)
454  {
455  if (valueOrDefault2 is Type)
456  {
457  type = (Type)valueOrDefault2;
458  }
459  else if (valueOrDefault2 is string)
460  {
461  type = Type.GetType((string)valueOrDefault2, throwOnError: false, ignoreCase: false);
462  if (type != null && !type.IsVisible)
463  {
464  type = null;
465  }
466  }
467  }
468  }
469  if (type == null)
470  {
471  type = Type.GetType(name, throwOnError: false, ignoreCase: false);
472  if (type != null && !type.IsVisible)
473  {
474  type = null;
475  }
476  }
477  if (type == null)
478  {
479  return null;
480  }
481  RuntimeType runtimeType = type as RuntimeType;
482  if (runtimeType == null)
483  {
484  return null;
485  }
486  if (args == null)
487  {
488  args = new object[0];
489  }
490  MethodBase[] constructors = runtimeType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance);
491  if (constructors == null)
492  {
493  return null;
494  }
495  List<MethodBase> list = new List<MethodBase>();
496  foreach (MethodBase methodBase in constructors)
497  {
498  if (methodBase.GetParameters().Length == args.Length)
499  {
500  list.Add(methodBase);
501  }
502  }
503  if (list.Count == 0)
504  {
505  return null;
506  }
507  constructors = list.ToArray();
508  object state;
509  RuntimeConstructorInfo runtimeConstructorInfo = Type.DefaultBinder.BindToMethod(BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, constructors, ref args, null, null, null, out state) as RuntimeConstructorInfo;
510  if (runtimeConstructorInfo == null || typeof(Delegate).IsAssignableFrom(runtimeConstructorInfo.DeclaringType))
511  {
512  return null;
513  }
514  object result = runtimeConstructorInfo.Invoke(BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, Type.DefaultBinder, args, null);
515  if (state != null)
516  {
517  Type.DefaultBinder.ReorderArgumentArray(ref args, state);
518  }
519  return result;
520  }
521 
527  public static object CreateFromName(string name)
528  {
529  return CreateFromName(name, null);
530  }
531 
537  [SecurityCritical]
538  public static void AddOID(string oid, params string[] names)
539  {
540  if (oid == null)
541  {
542  throw new ArgumentNullException("oid");
543  }
544  if (names == null)
545  {
546  throw new ArgumentNullException("names");
547  }
548  string[] array = new string[names.Length];
549  Array.Copy(names, array, array.Length);
550  string[] array2 = array;
551  foreach (string value in array2)
552  {
553  if (string.IsNullOrEmpty(value))
554  {
555  throw new ArgumentException(Environment.GetResourceString("Cryptography_AddNullOrEmptyName"));
556  }
557  }
558  lock (InternalSyncObject)
559  {
560  string[] array3 = array;
561  foreach (string key in array3)
562  {
563  appOidHT[key] = oid;
564  }
565  }
566  }
567 
572  public static string MapNameToOID(string name)
573  {
574  return MapNameToOID(name, OidGroup.AllGroups);
575  }
576 
577  [SecuritySafeCritical]
578  internal static string MapNameToOID(string name, OidGroup oidGroup)
579  {
580  if (name == null)
581  {
582  throw new ArgumentNullException("name");
583  }
584  InitializeConfigInfo();
585  string text = null;
586  lock (InternalSyncObject)
587  {
588  text = appOidHT.GetValueOrDefault(name);
589  }
590  if (text == null)
591  {
592  text = machineOidHT.GetValueOrDefault(name);
593  }
594  if (text == null)
595  {
596  text = DefaultOidHT.GetValueOrDefault(name);
597  }
598  if (text == null)
599  {
600  text = X509Utils.GetOidFromFriendlyName(name, oidGroup);
601  }
602  return text;
603  }
604 
610  public static byte[] EncodeOID(string str)
611  {
612  if (str == null)
613  {
614  throw new ArgumentNullException("str");
615  }
616  char[] separator = new char[1]
617  {
618  '.'
619  };
620  string[] array = str.Split(separator);
621  uint[] array2 = new uint[array.Length];
622  for (int i = 0; i < array.Length; i++)
623  {
624  array2[i] = (uint)int.Parse(array[i], CultureInfo.InvariantCulture);
625  }
626  byte[] array3 = new byte[array2.Length * 5];
627  int num = 0;
628  if (array2.Length < 2)
629  {
630  throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_InvalidOID"));
631  }
632  uint dwValue = array2[0] * 40 + array2[1];
633  byte[] array4 = EncodeSingleOIDNum(dwValue);
634  Array.Copy(array4, 0, array3, num, array4.Length);
635  num += array4.Length;
636  for (int j = 2; j < array2.Length; j++)
637  {
638  array4 = EncodeSingleOIDNum(array2[j]);
639  Buffer.InternalBlockCopy(array4, 0, array3, num, array4.Length);
640  num += array4.Length;
641  }
642  if (num > 127)
643  {
644  throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_Config_EncodedOIDError"));
645  }
646  array4 = new byte[num + 2];
647  array4[0] = 6;
648  array4[1] = (byte)num;
649  Buffer.InternalBlockCopy(array3, 0, array4, 2, num);
650  return array4;
651  }
652 
653  private static byte[] EncodeSingleOIDNum(uint dwValue)
654  {
655  if ((int)dwValue < 128)
656  {
657  return new byte[1]
658  {
659  (byte)dwValue
660  };
661  }
662  if (dwValue < 16384)
663  {
664  return new byte[2]
665  {
666  (byte)((dwValue >> 7) | 0x80),
667  (byte)(dwValue & 0x7F)
668  };
669  }
670  if (dwValue < 2097152)
671  {
672  return new byte[3]
673  {
674  (byte)((dwValue >> 14) | 0x80),
675  (byte)((dwValue >> 7) | 0x80),
676  (byte)(dwValue & 0x7F)
677  };
678  }
679  if (dwValue < 268435456)
680  {
681  return new byte[4]
682  {
683  (byte)((dwValue >> 21) | 0x80),
684  (byte)((dwValue >> 14) | 0x80),
685  (byte)((dwValue >> 7) | 0x80),
686  (byte)(dwValue & 0x7F)
687  };
688  }
689  return new byte[5]
690  {
691  (byte)((dwValue >> 28) | 0x80),
692  (byte)((dwValue >> 21) | 0x80),
693  (byte)((dwValue >> 14) | 0x80),
694  (byte)((dwValue >> 7) | 0x80),
695  (byte)(dwValue & 0x7F)
696  };
697  }
698 
699  private static Dictionary<string, string> InitializeNameMappings(ConfigNode nameMappingNode)
700  {
703  foreach (ConfigNode child in nameMappingNode.Children)
704  {
705  if (string.Compare(child.Name, "cryptoClasses", StringComparison.Ordinal) == 0)
706  {
707  foreach (ConfigNode child2 in child.Children)
708  {
709  if (string.Compare(child2.Name, "cryptoClass", StringComparison.Ordinal) == 0 && child2.Attributes.Count > 0)
710  {
711  DictionaryEntry dictionaryEntry = child2.Attributes[0];
712  dictionary2.Add((string)dictionaryEntry.Key, (string)dictionaryEntry.Value);
713  }
714  }
715  }
716  else if (string.Compare(child.Name, "nameEntry", StringComparison.Ordinal) == 0)
717  {
718  string text = null;
719  string text2 = null;
720  foreach (DictionaryEntry attribute in child.Attributes)
721  {
722  if (string.Compare((string)attribute.Key, "name", StringComparison.Ordinal) == 0)
723  {
724  text = (string)attribute.Value;
725  }
726  else if (string.Compare((string)attribute.Key, "class", StringComparison.Ordinal) == 0)
727  {
728  text2 = (string)attribute.Value;
729  }
730  }
731  if (text != null && text2 != null)
732  {
733  string valueOrDefault = dictionary2.GetValueOrDefault(text2);
734  if (valueOrDefault != null)
735  {
736  dictionary.Add(text, valueOrDefault);
737  }
738  }
739  }
740  }
741  return dictionary;
742  }
743 
744  private static Dictionary<string, string> InitializeOidMappings(ConfigNode oidMappingNode)
745  {
747  foreach (ConfigNode child in oidMappingNode.Children)
748  {
749  if (string.Compare(child.Name, "oidEntry", StringComparison.Ordinal) == 0)
750  {
751  string text = null;
752  string text2 = null;
753  foreach (DictionaryEntry attribute in child.Attributes)
754  {
755  if (string.Compare((string)attribute.Key, "OID", StringComparison.Ordinal) == 0)
756  {
757  text = (string)attribute.Value;
758  }
759  else if (string.Compare((string)attribute.Key, "name", StringComparison.Ordinal) == 0)
760  {
761  text2 = (string)attribute.Value;
762  }
763  }
764  if (text2 != null && text != null)
765  {
766  dictionary.Add(text2, text);
767  }
768  }
769  }
770  return dictionary;
771  }
772 
773  [SecurityCritical]
774  private static ConfigNode OpenCryptoConfig()
775  {
776  string text = Config.MachineDirectory + "machine.config";
778  if (!File.Exists(text))
779  {
780  return null;
781  }
782  CodeAccessPermission.RevertAssert();
783  ConfigTreeParser configTreeParser = new ConfigTreeParser();
784  ConfigNode configNode = configTreeParser.Parse(text, "configuration", skipSecurityStuff: true);
785  if (configNode == null)
786  {
787  return null;
788  }
789  ConfigNode configNode2 = null;
790  foreach (ConfigNode child in configNode.Children)
791  {
792  bool flag = false;
793  if (string.Compare(child.Name, "mscorlib", StringComparison.Ordinal) == 0)
794  {
795  foreach (DictionaryEntry attribute in child.Attributes)
796  {
797  if (string.Compare((string)attribute.Key, "version", StringComparison.Ordinal) == 0)
798  {
799  flag = true;
800  if (string.Compare((string)attribute.Value, Version, StringComparison.Ordinal) == 0)
801  {
802  configNode2 = child;
803  break;
804  }
805  }
806  }
807  if (!flag)
808  {
809  configNode2 = child;
810  }
811  }
812  if (configNode2 != null)
813  {
814  break;
815  }
816  }
817  if (configNode2 == null)
818  {
819  return null;
820  }
821  foreach (ConfigNode child2 in configNode2.Children)
822  {
823  if (string.Compare(child2.Name, "cryptographySettings", StringComparison.Ordinal) == 0)
824  {
825  return child2;
826  }
827  }
828  return null;
829  }
830  }
831 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
bool IsVisible
Gets a value indicating whether the T:System.Type can be accessed by code outside the assembly.
Definition: Type.cs:240
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static void AddOID(string oid, params string[] names)
Adds a set of names to object identifier (OID) mappings to be used for the current application domain...
FileIOPermissionAccess
Specifies the type of file access requested.
static bool Exists(string path)
Determines whether the specified file exists.
Definition: File.cs:435
abstract object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
When overridden in a derived class, invokes the reflected method or constructor with the given parame...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
static object CreateFromName(string name, params object[] args)
Creates a new instance of the specified cryptographic object with the specified arguments.
Definition: __Canon.cs:3
static OperatingSystem OSVersion
Gets an T:System.OperatingSystem object that contains the current platform identifier and version num...
Definition: Environment.cs:477
abstract void ReorderArgumentArray(ref object[] args, object state)
Upon returning from M:System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,...
OidGroup
Identifies Windows cryptographic object identifier (OID) groups.
Definition: OidGroup.cs:4
static Binder DefaultBinder
Gets a reference to the default binder, which implements internal rules for selecting the appropriate...
Definition: Type.cs:109
Version Version
Gets a T:System.Version object that identifies the operating system.
override string ToString()
Converts the value of the current T:System.Version object to its equivalent T:System....
Definition: Version.cs:437
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static void AddAlgorithm(Type algorithm, params string[] names)
Adds a set of names to algorithm mappings to be used for the current application domain.
int Major
Gets the value of the major component of the version number for the current T:System....
Definition: Version.cs:103
Accesses the cryptography configuration information.
Definition: CryptoConfig.cs:17
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
Provides information about methods and constructors.
Definition: MethodBase.cs:19
static byte [] EncodeOID(string str)
Encodes the specified object identifier (OID).
The exception that is thrown when one of the arguments provided to a method is not valid.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
static bool AllowOnlyFipsAlgorithms
Indicates whether the runtime should enforce the policy to create only Federal Information Processing...
Definition: CryptoConfig.cs:45
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
static string MapNameToOID(string name)
Gets the object identifier (OID) of the algorithm corresponding to the specified simple name.
object Value
Gets or sets the value in the key/value pair.
static StringComparer OrdinalIgnoreCase
Gets a T:System.StringComparer object that performs a case-insensitive ordinal string comparison.
Provides static methods for the creation, copying, deletion, moving, and opening of a single file,...
Definition: File.cs:14
Manipulates arrays of primitive types.
Definition: Buffer.cs:11
void Add(TKey key, TValue value)
Adds the specified key and value to the dictionary.
Definition: Dictionary.cs:1244
static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Gets the T:System.Type with the specified name, specifying whether to throw an exception if the type ...
Definition: Type.cs:853
object Key
Gets or sets the key in the key/value pair.
static object CreateFromName(string name)
Creates a new instance of the specified cryptographic object.
The exception that is thrown when an unexpected operation occurs during a cryptographic operation.
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Controls the ability to access files and folders. This class cannot be inherited.
abstract MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
Selects a method to invoke from the given set of methods, based on the supplied arguments.
Defines a dictionary key/value pair that can be set or retrieved.
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
Represents a string comparison operation that uses specific case and culture-based or ordinal compari...
abstract ParameterInfo [] GetParameters()
When overridden in a derived class, gets the parameters of the specified method or constructor.