mscorlib(4.0.0.0) API with additions
X509Certificate2Collection.cs
4 
6 {
9  {
10  internal delegate int FindProcDelegate(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData);
11 
12  private const uint X509_STORE_CONTENT_FLAGS = 5938u;
13 
22  public new X509Certificate2 this[int index]
23  {
24  get
25  {
26  return (X509Certificate2)base.List[index];
27  }
28  set
29  {
30  if (value == null)
31  {
32  throw new ArgumentNullException("value");
33  }
34  base.List[index] = value;
35  }
36  }
37 
40  {
41  }
42 
46  {
47  Add(certificate);
48  }
49 
53  {
54  AddRange(certificates);
55  }
56 
60  {
61  AddRange(certificates);
62  }
63 
69  public int Add(X509Certificate2 certificate)
70  {
71  if (certificate == null)
72  {
73  throw new ArgumentNullException("certificate");
74  }
75  return base.List.Add(certificate);
76  }
77 
82  public void AddRange(X509Certificate2[] certificates)
83  {
84  if (certificates == null)
85  {
86  throw new ArgumentNullException("certificates");
87  }
88  int i = 0;
89  try
90  {
91  for (; i < certificates.Length; i++)
92  {
93  Add(certificates[i]);
94  }
95  }
96  catch
97  {
98  for (int j = 0; j < i; j++)
99  {
100  Remove(certificates[j]);
101  }
102  throw;
103  }
104  }
105 
110  public void AddRange(X509Certificate2Collection certificates)
111  {
112  if (certificates == null)
113  {
114  throw new ArgumentNullException("certificates");
115  }
116  int num = 0;
117  try
118  {
119  X509Certificate2Enumerator enumerator = certificates.GetEnumerator();
120  while (enumerator.MoveNext())
121  {
122  X509Certificate2 current = enumerator.Current;
123  Add(current);
124  num++;
125  }
126  }
127  catch
128  {
129  for (int i = 0; i < num; i++)
130  {
131  Remove(certificates[i]);
132  }
133  throw;
134  }
135  }
136 
143  public bool Contains(X509Certificate2 certificate)
144  {
145  if (certificate == null)
146  {
147  throw new ArgumentNullException("certificate");
148  }
149  return base.List.Contains(certificate);
150  }
151 
161  public void Insert(int index, X509Certificate2 certificate)
162  {
163  if (certificate == null)
164  {
165  throw new ArgumentNullException("certificate");
166  }
167  base.List.Insert(index, certificate);
168  }
169 
173  {
174  return new X509Certificate2Enumerator(this);
175  }
176 
181  public void Remove(X509Certificate2 certificate)
182  {
183  if (certificate == null)
184  {
185  throw new ArgumentNullException("certificate");
186  }
187  base.List.Remove(certificate);
188  }
189 
194  public void RemoveRange(X509Certificate2[] certificates)
195  {
196  if (certificates == null)
197  {
198  throw new ArgumentNullException("certificates");
199  }
200  int i = 0;
201  try
202  {
203  for (; i < certificates.Length; i++)
204  {
205  Remove(certificates[i]);
206  }
207  }
208  catch
209  {
210  for (int j = 0; j < i; j++)
211  {
212  Add(certificates[j]);
213  }
214  throw;
215  }
216  }
217 
222  public void RemoveRange(X509Certificate2Collection certificates)
223  {
224  if (certificates == null)
225  {
226  throw new ArgumentNullException("certificates");
227  }
228  int num = 0;
229  try
230  {
231  X509Certificate2Enumerator enumerator = certificates.GetEnumerator();
232  while (enumerator.MoveNext())
233  {
234  X509Certificate2 current = enumerator.Current;
235  Remove(current);
236  num++;
237  }
238  }
239  catch
240  {
241  for (int i = 0; i < num; i++)
242  {
243  Add(certificates[i]);
244  }
245  throw;
246  }
247  }
248 
257  public X509Certificate2Collection Find(X509FindType findType, object findValue, bool validOnly)
258  {
259  StorePermission storePermission = new StorePermission(StorePermissionFlags.AllFlags);
260  storePermission.Assert();
261  System.Security.Cryptography.SafeCertStoreHandle safeCertStoreHandle = System.Security.Cryptography.X509Certificates.X509Utils.ExportToMemoryStore(this);
262  System.Security.Cryptography.SafeCertStoreHandle safeCertStoreHandle2 = FindCertInStore(safeCertStoreHandle, findType, findValue, validOnly);
263  X509Certificate2Collection certificates = System.Security.Cryptography.X509Certificates.X509Utils.GetCertificates(safeCertStoreHandle2);
264  safeCertStoreHandle2.Dispose();
265  safeCertStoreHandle.Dispose();
266  return certificates;
267  }
268 
271  public void Import(byte[] rawData)
272  {
273  Import(rawData, null, X509KeyStorageFlags.DefaultKeySet);
274  }
275 
280  public void Import(byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
281  {
282  uint dwFlags = System.Security.Cryptography.X509Certificates.X509Utils.MapKeyStorageFlags(keyStorageFlags);
284  StorePermission storePermission = new StorePermission(StorePermissionFlags.AllFlags);
285  storePermission.Assert();
286  invalidHandle = LoadStoreFromBlob(rawData, password, dwFlags, (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) != X509KeyStorageFlags.DefaultKeySet);
287  X509Certificate2Collection certificates = System.Security.Cryptography.X509Certificates.X509Utils.GetCertificates(invalidHandle);
288  invalidHandle.Dispose();
289  X509Certificate2[] array = new X509Certificate2[certificates.Count];
290  certificates.CopyTo(array, 0);
291  AddRange(array);
292  }
293 
296  public void Import(string fileName)
297  {
298  Import(fileName, null, X509KeyStorageFlags.DefaultKeySet);
299  }
300 
305  public void Import(string fileName, string password, X509KeyStorageFlags keyStorageFlags)
306  {
307  uint dwFlags = System.Security.Cryptography.X509Certificates.X509Utils.MapKeyStorageFlags(keyStorageFlags);
309  StorePermission storePermission = new StorePermission(StorePermissionFlags.AllFlags);
310  storePermission.Assert();
311  invalidHandle = LoadStoreFromFile(fileName, password, dwFlags, (keyStorageFlags & X509KeyStorageFlags.PersistKeySet) != X509KeyStorageFlags.DefaultKeySet);
312  X509Certificate2Collection certificates = System.Security.Cryptography.X509Certificates.X509Utils.GetCertificates(invalidHandle);
313  invalidHandle.Dispose();
314  X509Certificate2[] array = new X509Certificate2[certificates.Count];
315  certificates.CopyTo(array, 0);
316  AddRange(array);
317  }
318 
322  public byte[] Export(X509ContentType contentType)
323  {
324  return Export(contentType, null);
325  }
326 
332  public byte[] Export(X509ContentType contentType, string password)
333  {
334  StorePermission storePermission = new StorePermission(StorePermissionFlags.AllFlags);
335  storePermission.Assert();
336  System.Security.Cryptography.SafeCertStoreHandle safeCertStoreHandle = System.Security.Cryptography.X509Certificates.X509Utils.ExportToMemoryStore(this);
337  byte[] result = ExportCertificatesToBlob(safeCertStoreHandle, contentType, password);
338  safeCertStoreHandle.Dispose();
339  return result;
340  }
341 
342  private unsafe static byte[] ExportCertificatesToBlob(System.Security.Cryptography.SafeCertStoreHandle safeCertStoreHandle, X509ContentType contentType, string password)
343  {
345  uint dwSaveAs = 2u;
346  byte[] array = null;
347  CAPIBase.CRYPTOAPI_BLOB cRYPTOAPI_BLOB = default(CAPIBase.CRYPTOAPI_BLOB);
348  SafeLocalAllocHandle safeLocalAllocHandle = SafeLocalAllocHandle.InvalidHandle;
349  switch (contentType)
350  {
351  case X509ContentType.Cert:
352  safeCertContextHandle = CAPI.CertEnumCertificatesInStore(safeCertStoreHandle, safeCertContextHandle);
353  if (safeCertContextHandle != null && !safeCertContextHandle.IsInvalid)
354  {
355  CAPIBase.CERT_CONTEXT cERT_CONTEXT = *(CAPIBase.CERT_CONTEXT*)(void*)safeCertContextHandle.DangerousGetHandle();
356  array = new byte[cERT_CONTEXT.cbCertEncoded];
357  Marshal.Copy(cERT_CONTEXT.pbCertEncoded, array, 0, array.Length);
358  }
359  break;
360  case X509ContentType.SerializedCert:
361  {
362  safeCertContextHandle = CAPI.CertEnumCertificatesInStore(safeCertStoreHandle, safeCertContextHandle);
363  uint num = 0u;
364  if (safeCertContextHandle != null && !safeCertContextHandle.IsInvalid)
365  {
366  if (!CAPISafe.CertSerializeCertificateStoreElement(safeCertContextHandle, 0u, safeLocalAllocHandle, new IntPtr(&num)))
367  {
369  }
370  safeLocalAllocHandle = CAPI.LocalAlloc(0u, new IntPtr(num));
371  if (!CAPISafe.CertSerializeCertificateStoreElement(safeCertContextHandle, 0u, safeLocalAllocHandle, new IntPtr(&num)))
372  {
373  throw new CryptographicException(Marshal.GetLastWin32Error());
374  }
375  array = new byte[num];
376  Marshal.Copy(safeLocalAllocHandle.DangerousGetHandle(), array, 0, array.Length);
377  }
378  break;
379  }
380  case X509ContentType.Pfx:
381  if (!CAPI.PFXExportCertStore(safeCertStoreHandle, new IntPtr(&cRYPTOAPI_BLOB), password, 6u))
382  {
383  throw new CryptographicException(Marshal.GetLastWin32Error());
384  }
385  safeLocalAllocHandle = CAPI.LocalAlloc(0u, new IntPtr(cRYPTOAPI_BLOB.cbData));
386  cRYPTOAPI_BLOB.pbData = safeLocalAllocHandle.DangerousGetHandle();
387  if (!CAPI.PFXExportCertStore(safeCertStoreHandle, new IntPtr(&cRYPTOAPI_BLOB), password, 6u))
388  {
389  throw new CryptographicException(Marshal.GetLastWin32Error());
390  }
391  array = new byte[cRYPTOAPI_BLOB.cbData];
392  Marshal.Copy(cRYPTOAPI_BLOB.pbData, array, 0, array.Length);
393  break;
394  case X509ContentType.SerializedStore:
395  dwSaveAs = 1u;
396  goto case X509ContentType.Pkcs7;
397  case X509ContentType.Pkcs7:
398  if (!CAPI.CertSaveStore(safeCertStoreHandle, 65537u, dwSaveAs, 2u, new IntPtr(&cRYPTOAPI_BLOB), 0u))
399  {
400  throw new CryptographicException(Marshal.GetLastWin32Error());
401  }
402  safeLocalAllocHandle = CAPI.LocalAlloc(0u, new IntPtr(cRYPTOAPI_BLOB.cbData));
403  cRYPTOAPI_BLOB.pbData = safeLocalAllocHandle.DangerousGetHandle();
404  if (!CAPI.CertSaveStore(safeCertStoreHandle, 65537u, dwSaveAs, 2u, new IntPtr(&cRYPTOAPI_BLOB), 0u))
405  {
406  throw new CryptographicException(Marshal.GetLastWin32Error());
407  }
408  array = new byte[cRYPTOAPI_BLOB.cbData];
409  Marshal.Copy(cRYPTOAPI_BLOB.pbData, array, 0, array.Length);
410  break;
411  default:
412  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidContentType"));
413  }
414  safeLocalAllocHandle.Dispose();
415  safeCertContextHandle.Dispose();
416  return array;
417  }
418 
419  private unsafe static System.Security.Cryptography.SafeCertStoreHandle FindCertInStore(System.Security.Cryptography.SafeCertStoreHandle safeSourceStoreHandle, X509FindType findType, object findValue, bool validOnly)
420  {
421  if (findValue == null)
422  {
423  throw new ArgumentNullException("findValue");
424  }
425  IntPtr pvFindPara = IntPtr.Zero;
426  object obj = null;
427  object pvCallbackData = null;
428  FindProcDelegate pfnCertCallback = null;
429  FindProcDelegate pfnCertCallback2 = null;
430  uint dwFindType = 0u;
431  CAPIBase.CRYPTOAPI_BLOB cRYPTOAPI_BLOB = default(CAPIBase.CRYPTOAPI_BLOB);
432  SafeLocalAllocHandle safeLocalAllocHandle = SafeLocalAllocHandle.InvalidHandle;
434  string text = null;
435  switch (findType)
436  {
437  case X509FindType.FindByThumbprint:
438  {
439  if (findValue.GetType() != typeof(string))
440  {
441  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
442  }
443  byte[] array2 = System.Security.Cryptography.X509Certificates.X509Utils.DecodeHexString((string)findValue);
444  safeLocalAllocHandle = System.Security.Cryptography.X509Certificates.X509Utils.ByteToPtr(array2);
445  cRYPTOAPI_BLOB.pbData = safeLocalAllocHandle.DangerousGetHandle();
446  cRYPTOAPI_BLOB.cbData = (uint)array2.Length;
447  dwFindType = 65536u;
448  pvFindPara = new IntPtr(&cRYPTOAPI_BLOB);
449  break;
450  }
451  case X509FindType.FindBySubjectName:
452  {
453  if (findValue.GetType() != typeof(string))
454  {
455  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
456  }
457  string text2 = (string)findValue;
458  dwFindType = 524295u;
459  safeLocalAllocHandle = System.Security.Cryptography.X509Certificates.X509Utils.StringToUniPtr(text2);
460  pvFindPara = safeLocalAllocHandle.DangerousGetHandle();
461  break;
462  }
463  case X509FindType.FindBySubjectDistinguishedName:
464  {
465  if (findValue.GetType() != typeof(string))
466  {
467  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
468  }
469  string text2 = (string)findValue;
470  pfnCertCallback = FindSubjectDistinguishedNameCallback;
471  obj = text2;
472  break;
473  }
474  case X509FindType.FindByIssuerName:
475  {
476  if (findValue.GetType() != typeof(string))
477  {
478  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
479  }
480  string s = (string)findValue;
481  dwFindType = 524292u;
482  safeLocalAllocHandle = System.Security.Cryptography.X509Certificates.X509Utils.StringToUniPtr(s);
483  pvFindPara = safeLocalAllocHandle.DangerousGetHandle();
484  break;
485  }
486  case X509FindType.FindByIssuerDistinguishedName:
487  {
488  if (findValue.GetType() != typeof(string))
489  {
490  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
491  }
492  string s = (string)findValue;
493  pfnCertCallback = FindIssuerDistinguishedNameCallback;
494  obj = s;
495  break;
496  }
497  case X509FindType.FindBySerialNumber:
498  {
499  if (findValue.GetType() != typeof(string))
500  {
501  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
502  }
503  pfnCertCallback = FindSerialNumberCallback;
504  pfnCertCallback2 = FindSerialNumberCallback;
505  BigInt bigInt = new BigInt();
506  bigInt.FromHexadecimal((string)findValue);
507  obj = bigInt.ToByteArray();
508  bigInt.FromDecimal((string)findValue);
509  pvCallbackData = bigInt.ToByteArray();
510  break;
511  }
512  case X509FindType.FindByTimeValid:
513  if (findValue.GetType() != typeof(DateTime))
514  {
515  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
516  }
517  *(long*)(&fILETIME) = ((DateTime)findValue).ToFileTime();
518  pfnCertCallback = FindTimeValidCallback;
519  obj = fILETIME;
520  break;
521  case X509FindType.FindByTimeNotYetValid:
522  if (findValue.GetType() != typeof(DateTime))
523  {
524  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
525  }
526  *(long*)(&fILETIME) = ((DateTime)findValue).ToFileTime();
527  pfnCertCallback = FindTimeNotBeforeCallback;
528  obj = fILETIME;
529  break;
530  case X509FindType.FindByTimeExpired:
531  if (findValue.GetType() != typeof(DateTime))
532  {
533  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
534  }
535  *(long*)(&fILETIME) = ((DateTime)findValue).ToFileTime();
536  pfnCertCallback = FindTimeNotAfterCallback;
537  obj = fILETIME;
538  break;
539  case X509FindType.FindByTemplateName:
540  if (findValue.GetType() != typeof(string))
541  {
542  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
543  }
544  obj = (string)findValue;
545  pfnCertCallback = FindTemplateNameCallback;
546  break;
547  case X509FindType.FindByApplicationPolicy:
548  if (findValue.GetType() != typeof(string))
549  {
550  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
551  }
552  text = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfoWithFallback(2u, (string)findValue, System.Security.Cryptography.OidGroup.Policy);
553  if (text == null)
554  {
555  text = (string)findValue;
556  System.Security.Cryptography.X509Certificates.X509Utils.ValidateOidValue(text);
557  }
558  obj = text;
559  pfnCertCallback = FindApplicationPolicyCallback;
560  break;
561  case X509FindType.FindByCertificatePolicy:
562  if (findValue.GetType() != typeof(string))
563  {
564  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
565  }
566  text = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfoWithFallback(2u, (string)findValue, System.Security.Cryptography.OidGroup.Policy);
567  if (text == null)
568  {
569  text = (string)findValue;
570  System.Security.Cryptography.X509Certificates.X509Utils.ValidateOidValue(text);
571  }
572  obj = text;
573  pfnCertCallback = FindCertificatePolicyCallback;
574  break;
575  case X509FindType.FindByExtension:
576  if (findValue.GetType() != typeof(string))
577  {
578  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
579  }
580  text = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfoWithFallback(2u, (string)findValue, System.Security.Cryptography.OidGroup.ExtensionOrAttribute);
581  if (text == null)
582  {
583  text = (string)findValue;
584  System.Security.Cryptography.X509Certificates.X509Utils.ValidateOidValue(text);
585  }
586  obj = text;
587  pfnCertCallback = FindExtensionCallback;
588  break;
589  case X509FindType.FindByKeyUsage:
590  if (findValue.GetType() == typeof(string))
591  {
592  CAPIBase.KEY_USAGE_STRUCT[] array = new CAPIBase.KEY_USAGE_STRUCT[9]
593  {
594  new CAPIBase.KEY_USAGE_STRUCT("DigitalSignature", 128u),
595  new CAPIBase.KEY_USAGE_STRUCT("NonRepudiation", 64u),
596  new CAPIBase.KEY_USAGE_STRUCT("KeyEncipherment", 32u),
597  new CAPIBase.KEY_USAGE_STRUCT("DataEncipherment", 16u),
598  new CAPIBase.KEY_USAGE_STRUCT("KeyAgreement", 8u),
599  new CAPIBase.KEY_USAGE_STRUCT("KeyCertSign", 4u),
600  new CAPIBase.KEY_USAGE_STRUCT("CrlSign", 2u),
601  new CAPIBase.KEY_USAGE_STRUCT("EncipherOnly", 1u),
602  new CAPIBase.KEY_USAGE_STRUCT("DecipherOnly", 32768u)
603  };
604  for (uint num = 0u; num < array.Length; num++)
605  {
606  if (string.Compare(array[num].pwszKeyUsage, (string)findValue, StringComparison.OrdinalIgnoreCase) == 0)
607  {
608  obj = array[num].dwKeyUsageBit;
609  break;
610  }
611  }
612  if (obj == null)
613  {
614  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindType"));
615  }
616  }
617  else if (findValue.GetType() == typeof(X509KeyUsageFlags))
618  {
619  obj = findValue;
620  }
621  else
622  {
623  if (!(findValue.GetType() == typeof(uint)) && !(findValue.GetType() == typeof(int)))
624  {
625  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindType"));
626  }
627  obj = findValue;
628  }
629  pfnCertCallback = FindKeyUsageCallback;
630  break;
631  case X509FindType.FindBySubjectKeyIdentifier:
632  if (findValue.GetType() != typeof(string))
633  {
634  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
635  }
636  obj = System.Security.Cryptography.X509Certificates.X509Utils.DecodeHexString((string)findValue);
637  pfnCertCallback = FindSubjectKeyIdentifierCallback;
638  break;
639  default:
640  throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindType"));
641  }
642  System.Security.Cryptography.SafeCertStoreHandle safeCertStoreHandle = CAPI.CertOpenStore(new IntPtr(2L), 65537u, IntPtr.Zero, 8704u, null);
643  if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid)
644  {
645  throw new CryptographicException(Marshal.GetLastWin32Error());
646  }
647  FindByCert(safeSourceStoreHandle, dwFindType, pvFindPara, validOnly, pfnCertCallback, pfnCertCallback2, obj, pvCallbackData, safeCertStoreHandle);
648  safeLocalAllocHandle.Dispose();
649  return safeCertStoreHandle;
650  }
651 
652  private static void FindByCert(System.Security.Cryptography.SafeCertStoreHandle safeSourceStoreHandle, uint dwFindType, IntPtr pvFindPara, bool validOnly, FindProcDelegate pfnCertCallback1, FindProcDelegate pfnCertCallback2, object pvCallbackData1, object pvCallbackData2, System.Security.Cryptography.SafeCertStoreHandle safeTargetStoreHandle)
653  {
654  int num = 0;
656  for (invalidHandle = CAPI.CertFindCertificateInStore(safeSourceStoreHandle, 65537u, 0u, dwFindType, pvFindPara, invalidHandle); invalidHandle != null && !invalidHandle.IsInvalid; GC.SuppressFinalize(invalidHandle), invalidHandle = CAPI.CertFindCertificateInStore(safeSourceStoreHandle, 65537u, 0u, dwFindType, pvFindPara, invalidHandle))
657  {
658  if (pfnCertCallback1 != null)
659  {
660  num = pfnCertCallback1(invalidHandle, pvCallbackData1);
661  if (num == 1)
662  {
663  if (pfnCertCallback2 != null)
664  {
665  num = pfnCertCallback2(invalidHandle, pvCallbackData2);
666  }
667  if (num == 1)
668  {
669  continue;
670  }
671  }
672  if (num != 0)
673  {
674  break;
675  }
676  }
677  if (validOnly)
678  {
679  num = System.Security.Cryptography.X509Certificates.X509Utils.VerifyCertificate(invalidHandle, null, null, X509RevocationMode.NoCheck, X509RevocationFlag.ExcludeRoot, DateTime.Now, new TimeSpan(0, 0, 0), null, new IntPtr(1L), IntPtr.Zero);
680  if (num == 1)
681  {
682  continue;
683  }
684  if (num != 0)
685  {
686  break;
687  }
688  }
689  if (!CAPI.CertAddCertificateLinkToStore(safeTargetStoreHandle, invalidHandle, 4u, System.Security.Cryptography.SafeCertContextHandle.InvalidHandle))
690  {
692  break;
693  }
694  }
695  if (invalidHandle != null && !invalidHandle.IsInvalid)
696  {
697  invalidHandle.Dispose();
698  }
699  if (num != 1 && num != 0)
700  {
701  throw new CryptographicException(num);
702  }
703  }
704 
705  private static int FindSubjectDistinguishedNameCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
706  {
707  string certNameInfo = CAPI.GetCertNameInfo(safeCertContextHandle, 0u, 2u);
708  if (string.Compare(certNameInfo, (string)pvCallbackData, StringComparison.OrdinalIgnoreCase) != 0)
709  {
710  return 1;
711  }
712  return 0;
713  }
714 
715  private static int FindIssuerDistinguishedNameCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
716  {
717  string certNameInfo = CAPI.GetCertNameInfo(safeCertContextHandle, 1u, 2u);
718  if (string.Compare(certNameInfo, (string)pvCallbackData, StringComparison.OrdinalIgnoreCase) != 0)
719  {
720  return 1;
721  }
722  return 0;
723  }
724 
725  private unsafe static int FindSerialNumberCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
726  {
727  CAPIBase.CERT_CONTEXT cERT_CONTEXT = *(CAPIBase.CERT_CONTEXT*)(void*)safeCertContextHandle.DangerousGetHandle();
728  CAPIBase.CERT_INFO cERT_INFO = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cERT_CONTEXT.pCertInfo, typeof(CAPIBase.CERT_INFO));
729  byte[] array = new byte[cERT_INFO.SerialNumber.cbData];
730  Marshal.Copy(cERT_INFO.SerialNumber.pbData, array, 0, array.Length);
731  int hexArraySize = System.Security.Cryptography.X509Certificates.X509Utils.GetHexArraySize(array);
732  byte[] array2 = (byte[])pvCallbackData;
733  if (array2.Length != hexArraySize)
734  {
735  return 1;
736  }
737  for (int i = 0; i < array2.Length; i++)
738  {
739  if (array2[i] != array[i])
740  {
741  return 1;
742  }
743  }
744  return 0;
745  }
746 
747  private unsafe static int FindTimeValidCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
748  {
750  CAPIBase.CERT_CONTEXT cERT_CONTEXT = *(CAPIBase.CERT_CONTEXT*)(void*)safeCertContextHandle.DangerousGetHandle();
751  if (CAPISafe.CertVerifyTimeValidity(ref pTimeToVerify, cERT_CONTEXT.pCertInfo) == 0)
752  {
753  return 0;
754  }
755  return 1;
756  }
757 
758  private unsafe static int FindTimeNotAfterCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
759  {
761  CAPIBase.CERT_CONTEXT cERT_CONTEXT = *(CAPIBase.CERT_CONTEXT*)(void*)safeCertContextHandle.DangerousGetHandle();
762  if (CAPISafe.CertVerifyTimeValidity(ref pTimeToVerify, cERT_CONTEXT.pCertInfo) == 1)
763  {
764  return 0;
765  }
766  return 1;
767  }
768 
769  private unsafe static int FindTimeNotBeforeCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
770  {
772  CAPIBase.CERT_CONTEXT cERT_CONTEXT = *(CAPIBase.CERT_CONTEXT*)(void*)safeCertContextHandle.DangerousGetHandle();
773  if (CAPISafe.CertVerifyTimeValidity(ref pTimeToVerify, cERT_CONTEXT.pCertInfo) == -1)
774  {
775  return 0;
776  }
777  return 1;
778  }
779 
780  private unsafe static int FindTemplateNameCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
781  {
782  IntPtr zero = IntPtr.Zero;
783  IntPtr zero2 = IntPtr.Zero;
784  CAPIBase.CERT_CONTEXT cERT_CONTEXT = *(CAPIBase.CERT_CONTEXT*)(void*)safeCertContextHandle.DangerousGetHandle();
785  CAPIBase.CERT_INFO cERT_INFO = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cERT_CONTEXT.pCertInfo, typeof(CAPIBase.CERT_INFO));
786  zero = CAPISafe.CertFindExtension("1.3.6.1.4.1.311.20.2", cERT_INFO.cExtension, cERT_INFO.rgExtension);
787  zero2 = CAPISafe.CertFindExtension("1.3.6.1.4.1.311.21.7", cERT_INFO.cExtension, cERT_INFO.rgExtension);
788  if (zero == IntPtr.Zero && zero2 == IntPtr.Zero)
789  {
790  return 1;
791  }
792  if (zero != IntPtr.Zero)
793  {
794  CAPIBase.CERT_EXTENSION cERT_EXTENSION = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(zero, typeof(CAPIBase.CERT_EXTENSION));
795  byte[] array = new byte[cERT_EXTENSION.Value.cbData];
796  Marshal.Copy(cERT_EXTENSION.Value.pbData, array, 0, array.Length);
797  uint cbDecodedValue = 0u;
798  SafeLocalAllocHandle decodedValue = null;
799  if (CAPI.DecodeObject(new IntPtr(24L), array, out decodedValue, out cbDecodedValue))
800  {
801  CAPIBase.CERT_NAME_VALUE cERT_NAME_VALUE = (CAPIBase.CERT_NAME_VALUE)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_NAME_VALUE));
802  string strA = Marshal.PtrToStringUni(cERT_NAME_VALUE.Value.pbData);
803  if (string.Compare(strA, (string)pvCallbackData, StringComparison.OrdinalIgnoreCase) == 0)
804  {
805  return 0;
806  }
807  }
808  }
809  if (zero2 != IntPtr.Zero)
810  {
811  CAPIBase.CERT_EXTENSION cERT_EXTENSION2 = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(zero2, typeof(CAPIBase.CERT_EXTENSION));
812  byte[] array2 = new byte[cERT_EXTENSION2.Value.cbData];
813  Marshal.Copy(cERT_EXTENSION2.Value.pbData, array2, 0, array2.Length);
814  uint cbDecodedValue2 = 0u;
815  SafeLocalAllocHandle decodedValue2 = null;
816  if (CAPI.DecodeObject(new IntPtr(64L), array2, out decodedValue2, out cbDecodedValue2))
817  {
818  CAPIBase.CERT_TEMPLATE_EXT cERT_TEMPLATE_EXT = (CAPIBase.CERT_TEMPLATE_EXT)Marshal.PtrToStructure(decodedValue2.DangerousGetHandle(), typeof(CAPIBase.CERT_TEMPLATE_EXT));
819  string text = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfoWithFallback(2u, (string)pvCallbackData, System.Security.Cryptography.OidGroup.Template);
820  if (text == null)
821  {
822  text = (string)pvCallbackData;
823  }
824  if (string.Compare(cERT_TEMPLATE_EXT.pszObjId, text, StringComparison.OrdinalIgnoreCase) == 0)
825  {
826  return 0;
827  }
828  }
829  }
830  return 1;
831  }
832 
833  private unsafe static int FindApplicationPolicyCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
834  {
835  string text = (string)pvCallbackData;
836  if (text.Length == 0)
837  {
838  return 1;
839  }
840  IntPtr intPtr = safeCertContextHandle.DangerousGetHandle();
841  int num = 0;
842  uint num2 = 0u;
843  SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;
844  if (!CAPISafe.CertGetValidUsages(1u, new IntPtr(&intPtr), new IntPtr(&num), invalidHandle, new IntPtr(&num2)))
845  {
846  return 1;
847  }
848  invalidHandle = CAPI.LocalAlloc(0u, new IntPtr(num2));
849  if (!CAPISafe.CertGetValidUsages(1u, new IntPtr(&intPtr), new IntPtr(&num), invalidHandle, new IntPtr(&num2)))
850  {
851  return 1;
852  }
853  if (num == -1)
854  {
855  return 0;
856  }
857  for (int i = 0; i < num; i++)
858  {
859  IntPtr ptr = Marshal.ReadIntPtr(new IntPtr((long)invalidHandle.DangerousGetHandle() + i * Marshal.SizeOf(typeof(IntPtr))));
860  string strB = Marshal.PtrToStringAnsi(ptr);
861  if (string.Compare(text, strB, StringComparison.OrdinalIgnoreCase) == 0)
862  {
863  return 0;
864  }
865  }
866  return 1;
867  }
868 
869  private unsafe static int FindCertificatePolicyCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
870  {
871  string text = (string)pvCallbackData;
872  if (text.Length == 0)
873  {
874  return 1;
875  }
876  CAPIBase.CERT_CONTEXT cERT_CONTEXT = *(CAPIBase.CERT_CONTEXT*)(void*)safeCertContextHandle.DangerousGetHandle();
877  CAPIBase.CERT_INFO cERT_INFO = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cERT_CONTEXT.pCertInfo, typeof(CAPIBase.CERT_INFO));
878  IntPtr intPtr = CAPISafe.CertFindExtension("2.5.29.32", cERT_INFO.cExtension, cERT_INFO.rgExtension);
879  if (intPtr == IntPtr.Zero)
880  {
881  return 1;
882  }
883  CAPIBase.CERT_EXTENSION cERT_EXTENSION = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(intPtr, typeof(CAPIBase.CERT_EXTENSION));
884  byte[] array = new byte[cERT_EXTENSION.Value.cbData];
885  Marshal.Copy(cERT_EXTENSION.Value.pbData, array, 0, array.Length);
886  uint cbDecodedValue = 0u;
887  SafeLocalAllocHandle decodedValue = null;
888  if (CAPI.DecodeObject(new IntPtr(16L), array, out decodedValue, out cbDecodedValue))
889  {
890  CAPIBase.CERT_POLICIES_INFO cERT_POLICIES_INFO = (CAPIBase.CERT_POLICIES_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_POLICIES_INFO));
891  for (int i = 0; i < cERT_POLICIES_INFO.cPolicyInfo; i++)
892  {
893  IntPtr ptr = new IntPtr((long)cERT_POLICIES_INFO.rgPolicyInfo + i * Marshal.SizeOf(typeof(CAPIBase.CERT_POLICY_INFO)));
894  CAPIBase.CERT_POLICY_INFO cERT_POLICY_INFO = (CAPIBase.CERT_POLICY_INFO)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CERT_POLICY_INFO));
895  if (string.Compare(text, cERT_POLICY_INFO.pszPolicyIdentifier, StringComparison.OrdinalIgnoreCase) == 0)
896  {
897  return 0;
898  }
899  }
900  }
901  return 1;
902  }
903 
904  private unsafe static int FindExtensionCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
905  {
906  CAPIBase.CERT_CONTEXT cERT_CONTEXT = *(CAPIBase.CERT_CONTEXT*)(void*)safeCertContextHandle.DangerousGetHandle();
907  CAPIBase.CERT_INFO cERT_INFO = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cERT_CONTEXT.pCertInfo, typeof(CAPIBase.CERT_INFO));
908  IntPtr value = CAPISafe.CertFindExtension((string)pvCallbackData, cERT_INFO.cExtension, cERT_INFO.rgExtension);
909  if (value == IntPtr.Zero)
910  {
911  return 1;
912  }
913  return 0;
914  }
915 
916  private unsafe static int FindKeyUsageCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
917  {
918  CAPIBase.CERT_CONTEXT cERT_CONTEXT = *(CAPIBase.CERT_CONTEXT*)(void*)safeCertContextHandle.DangerousGetHandle();
919  uint num = 0u;
920  if (!CAPISafe.CertGetIntendedKeyUsage(65537u, cERT_CONTEXT.pCertInfo, new IntPtr(&num), 4u))
921  {
922  return 0;
923  }
924  uint num2 = Convert.ToUInt32(pvCallbackData, null);
925  if ((num & num2) == num2)
926  {
927  return 0;
928  }
929  return 1;
930  }
931 
932  private static int FindSubjectKeyIdentifierCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
933  {
934  SafeLocalAllocHandle invalidHandle = SafeLocalAllocHandle.InvalidHandle;
935  uint pcbData = 0u;
936  if (!CAPISafe.CertGetCertificateContextProperty(safeCertContextHandle, 20u, invalidHandle, ref pcbData))
937  {
938  return 1;
939  }
940  invalidHandle = CAPI.LocalAlloc(0u, new IntPtr(pcbData));
941  if (!CAPISafe.CertGetCertificateContextProperty(safeCertContextHandle, 20u, invalidHandle, ref pcbData))
942  {
943  return 1;
944  }
945  byte[] array = (byte[])pvCallbackData;
946  if (array.Length != pcbData)
947  {
948  return 1;
949  }
950  byte[] array2 = new byte[pcbData];
951  Marshal.Copy(invalidHandle.DangerousGetHandle(), array2, 0, array2.Length);
952  invalidHandle.Dispose();
953  for (uint num = 0u; num < pcbData; num++)
954  {
955  if (array[num] != array2[num])
956  {
957  return 1;
958  }
959  }
960  return 0;
961  }
962 
963  private unsafe static System.Security.Cryptography.SafeCertStoreHandle LoadStoreFromBlob(byte[] rawData, string password, uint dwFlags, bool persistKeyContainers)
964  {
965  uint num = 0u;
967  if (!CAPI.CryptQueryObject(2u, rawData, 5938u, 14u, 0u, IntPtr.Zero, new IntPtr(&num), IntPtr.Zero, ref phCertStore, IntPtr.Zero, IntPtr.Zero))
968  {
969  throw new CryptographicException(Marshal.GetLastWin32Error());
970  }
971  if (num == 12)
972  {
973  phCertStore.Dispose();
974  phCertStore = CAPI.PFXImportCertStore(2u, rawData, password, dwFlags, persistKeyContainers);
975  }
976  if (phCertStore == null || phCertStore.IsInvalid)
977  {
978  throw new CryptographicException(Marshal.GetLastWin32Error());
979  }
980  return phCertStore;
981  }
982 
983  private unsafe static System.Security.Cryptography.SafeCertStoreHandle LoadStoreFromFile(string fileName, string password, uint dwFlags, bool persistKeyContainers)
984  {
985  uint num = 0u;
987  if (!CAPI.CryptQueryObject(1u, fileName, 5938u, 14u, 0u, IntPtr.Zero, new IntPtr(&num), IntPtr.Zero, ref phCertStore, IntPtr.Zero, IntPtr.Zero))
988  {
989  throw new CryptographicException(Marshal.GetLastWin32Error());
990  }
991  if (num == 12)
992  {
993  phCertStore.Dispose();
994  phCertStore = CAPI.PFXImportCertStore(1u, fileName, password, dwFlags, persistKeyContainers);
995  }
996  if (phCertStore == null || phCertStore.IsInvalid)
997  {
998  throw new CryptographicException(Marshal.GetLastWin32Error());
999  }
1000  return phCertStore;
1001  }
1002  }
1003 }
The exception that is thrown when an error occurs during a cryptographic operation.
void AddRange(X509Certificate2[] certificates)
Adds multiple T:System.Security.Cryptography.X509Certificates.X509Certificate2 objects in an array to...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
void Import(byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
Imports a certificate, in the form of a byte array that requires a password to access the certificate...
bool Contains(X509Certificate2 certificate)
Determines whether the T:System.Security.Cryptography.X509Certificates.X509Certificate2Collection obj...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
X509Certificate2Collection()
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
Defines a collection that stores T:System.Security.Cryptography.X509Certificates.X509Certificate obje...
X509Certificate2Collection(X509Certificate2[] certificates)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
Definition: __Canon.cs:3
byte [] Export(X509ContentType contentType, string password)
Exports X.509 certificate information into a byte array using a password.
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
Definition: Marshal.cs:159
static int GetHRForLastWin32Error()
Returns the HRESULT corresponding to the last error incurred by Win32 code executed using T:System....
Definition: Marshal.cs:1072
void Remove(X509Certificate2 certificate)
Removes the first occurrence of a certificate from the T:System.Security.Cryptography....
byte [] Export(X509ContentType contentType)
Exports X.509 certificate information into a byte array.
X509ContentType
Specifies the format of an X.509 certificate.
new X509Certificate2Enumerator GetEnumerator()
Returns an enumerator that can iterate through a T:System.Security.Cryptography.X509Certificates....
A type representing a date and time value.
X509RevocationMode
Specifies the mode used to check for X509 certificate revocation.
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed 32-bit signed integer array to an unmanaged memory pointe...
Definition: Marshal.cs:301
void RemoveRange(X509Certificate2Collection certificates)
Removes multiple T:System.Security.Cryptography.X509Certificates.X509Certificate2 objects in an T:Sys...
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
void CopyTo(X509Certificate[] array, int index)
Copies the T:System.Security.Cryptography.X509Certificates.X509Certificate values in the current T:Sy...
X509Certificate2 Current
Gets the current element in the T:System.Security.Cryptography.X509Certificates.X509Certificate2Colle...
Controls access to stores containing X.509 certificates. This class cannot be inherited.
void Import(string fileName, string password, X509KeyStorageFlags keyStorageFlags)
Imports a certificate file that requires a password into a T:System.Security.Cryptography....
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
void Import(string fileName)
Imports a certificate file into a T:System.Security.Cryptography.X509Certificates....
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
void AddRange(X509Certificate2Collection certificates)
Adds multiple T:System.Security.Cryptography.X509Certificates.X509Certificate2 objects in an T:System...
StorePermissionFlags
Specifies the permitted access to X.509 certificate stores.
X509FindType
Specifies the type of value the M:System.Security.Cryptography.X509Certificates.X509Certificate2Colle...
Definition: X509FindType.cs:4
int Count
Gets the number of elements contained in the T:System.Collections.CollectionBase instance....
static unsafe string PtrToStringUni(IntPtr ptr, int len)
Allocates a managed T:System.String and copies a specified number of characters from an unmanaged Uni...
Definition: Marshal.cs:103
X509Certificate2Collection(X509Certificate2Collection certificates)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
X509Certificate2Collection(X509Certificate2 certificate)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
Represents the number of 100-nanosecond intervals since January 1, 1601. This structure is a 64-bit v...
Definition: FILETIME.cs:5
bool MoveNext()
Advances the enumerator to the next element in the T:System.Security.Cryptography....
static void PtrToStructure(IntPtr ptr, object structure)
Marshals data from an unmanaged block of memory to a managed object.
Definition: Marshal.cs:1198
void RemoveRange(X509Certificate2[] certificates)
Removes multiple T:System.Security.Cryptography.X509Certificates.X509Certificate2 objects in an array...
void Insert(int index, X509Certificate2 certificate)
Inserts an object into the T:System.Security.Cryptography.X509Certificates.X509Certificate2Collection...
int Add(X509Certificate2 certificate)
Adds an object to the end of the T:System.Security.Cryptography.X509Certificates.X509Certificate2Coll...
X509KeyStorageFlags
Defines where and how to import the private key of an X.509 certificate.
X509KeyUsageFlags
Defines how the certificate key can be used. If this value is not defined, the key can be used for an...
void Import(byte[] rawData)
Imports a certificate in the form of a byte array into a T:System.Security.Cryptography....
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
static unsafe string PtrToStringAnsi(IntPtr ptr)
Copies all characters up to the first null character from an unmanaged ANSI string to a managed T:Sys...
Definition: Marshal.cs:61
Represents a collection of T:System.Security.Cryptography.X509Certificates.X509Certificate2 objects....
Supports a simple iteration over a T:System.Security.Cryptography.X509Certificates....
X509RevocationFlag
Specifies which X509 certificates in the chain should be checked for revocation.
X509Certificate2Collection Find(X509FindType findType, object findValue, bool validOnly)
Searches an T:System.Security.Cryptography.X509Certificates.X509Certificate2Collection object using t...
static IntPtr ReadIntPtr([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
Reads a processor native sized integer from unmanaged memory.
Definition: Marshal.cs:681