PkixCrlUtilities.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509.Store;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkix
  9. {
  10. public class PkixCrlUtilities
  11. {
  12. public virtual ISet<X509Crl> FindCrls(X509CrlStoreSelector crlSelector, PkixParameters paramsPkix,
  13. DateTime currentDate)
  14. {
  15. HashSet<X509Crl> initialSet;
  16. // get complete CRL(s)
  17. try
  18. {
  19. initialSet = FindCrls(crlSelector, paramsPkix.GetStoresCrl());
  20. }
  21. catch (Exception e)
  22. {
  23. throw new Exception("Exception obtaining complete CRLs.", e);
  24. }
  25. var finalSet = new HashSet<X509Crl>();
  26. DateTime validityDate = currentDate;
  27. if (paramsPkix.Date != null)
  28. {
  29. validityDate = paramsPkix.Date.Value;
  30. }
  31. // based on RFC 5280 6.3.3
  32. foreach (X509Crl crl in initialSet)
  33. {
  34. DateTime? nextUpdate = crl.NextUpdate;
  35. if (null == nextUpdate || nextUpdate.Value.CompareTo(validityDate) > 0)
  36. {
  37. X509Certificate cert = crlSelector.CertificateChecking;
  38. if (null == cert || crl.ThisUpdate.CompareTo(cert.NotAfter) < 0)
  39. {
  40. finalSet.Add(crl);
  41. }
  42. }
  43. }
  44. return finalSet;
  45. }
  46. public virtual ISet<X509Crl> FindCrls(X509CrlStoreSelector crlSelector, PkixParameters paramsPkix)
  47. {
  48. // get complete CRL(s)
  49. try
  50. {
  51. return FindCrls(crlSelector, paramsPkix.GetStoresCrl());
  52. }
  53. catch (Exception e)
  54. {
  55. throw new Exception("Exception obtaining complete CRLs.", e);
  56. }
  57. }
  58. /// <summary>
  59. /// crl checking
  60. /// Return a Collection of all CRLs found in the X509Store's that are
  61. /// matching the crlSelect criteriums.
  62. /// </summary>
  63. /// <param name="crlSelector">a {@link X509CRLStoreSelector} object that will be used
  64. /// to select the CRLs</param>
  65. /// <param name="crlStores">a List containing only {@link org.bouncycastle.x509.X509Store
  66. /// X509Store} objects. These are used to search for CRLs</param>
  67. /// <returns>a Collection of all found {@link X509CRL X509CRL} objects. May be
  68. /// empty but never <code>null</code>.
  69. /// </returns>
  70. private HashSet<X509Crl> FindCrls(ISelector<X509Crl> crlSelector, IList<IStore<X509Crl>> crlStores)
  71. {
  72. var crls = new HashSet<X509Crl>();
  73. Exception lastException = null;
  74. bool foundValidStore = false;
  75. foreach (var crlStore in crlStores)
  76. {
  77. try
  78. {
  79. crls.UnionWith(crlStore.EnumerateMatches(crlSelector));
  80. foundValidStore = true;
  81. }
  82. catch (Exception e)
  83. {
  84. lastException = new Exception("Exception searching in X.509 CRL store.", e);
  85. }
  86. }
  87. if (!foundValidStore && lastException != null)
  88. throw lastException;
  89. return crls;
  90. }
  91. }
  92. }
  93. #pragma warning restore
  94. #endif