PkixCrlUtilities.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  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 FindCrls(X509CrlStoreSelector crlselect, PkixParameters paramsPkix, DateTime currentDate)
  13. {
  14. ISet initialSet = new HashSet();
  15. // get complete CRL(s)
  16. try
  17. {
  18. initialSet.AddAll(FindCrls(crlselect, paramsPkix.GetAdditionalStores()));
  19. initialSet.AddAll(FindCrls(crlselect, paramsPkix.GetStores()));
  20. }
  21. catch (Exception e)
  22. {
  23. throw new Exception("Exception obtaining complete CRLs.", e);
  24. }
  25. ISet finalSet = new HashSet();
  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. if (crl.NextUpdate.Value.CompareTo(validityDate) > 0)
  35. {
  36. X509Certificate cert = crlselect.CertificateChecking;
  37. if (cert != null)
  38. {
  39. if (crl.ThisUpdate.CompareTo(cert.NotAfter) < 0)
  40. {
  41. finalSet.Add(crl);
  42. }
  43. }
  44. else
  45. {
  46. finalSet.Add(crl);
  47. }
  48. }
  49. }
  50. return finalSet;
  51. }
  52. public virtual ISet FindCrls(X509CrlStoreSelector crlselect, PkixParameters paramsPkix)
  53. {
  54. ISet completeSet = new HashSet();
  55. // get complete CRL(s)
  56. try
  57. {
  58. completeSet.AddAll(FindCrls(crlselect, paramsPkix.GetStores()));
  59. }
  60. catch (Exception e)
  61. {
  62. throw new Exception("Exception obtaining complete CRLs.", e);
  63. }
  64. return completeSet;
  65. }
  66. /// <summary>
  67. /// crl checking
  68. /// Return a Collection of all CRLs found in the X509Store's that are
  69. /// matching the crlSelect criteriums.
  70. /// </summary>
  71. /// <param name="crlSelect">a {@link X509CRLStoreSelector} object that will be used
  72. /// to select the CRLs</param>
  73. /// <param name="crlStores">a List containing only {@link org.bouncycastle.x509.X509Store
  74. /// X509Store} objects. These are used to search for CRLs</param>
  75. /// <returns>a Collection of all found {@link X509CRL X509CRL} objects. May be
  76. /// empty but never <code>null</code>.
  77. /// </returns>
  78. private ICollection FindCrls(X509CrlStoreSelector crlSelect, IList crlStores)
  79. {
  80. ISet crls = new HashSet();
  81. Exception lastException = null;
  82. bool foundValidStore = false;
  83. foreach (IX509Store store in crlStores)
  84. {
  85. try
  86. {
  87. crls.AddAll(store.GetMatches(crlSelect));
  88. foundValidStore = true;
  89. }
  90. catch (X509StoreException e)
  91. {
  92. lastException = new Exception("Exception searching in X.509 CRL store.", e);
  93. }
  94. }
  95. if (!foundValidStore && lastException != null)
  96. throw lastException;
  97. return crls;
  98. }
  99. }
  100. }
  101. #pragma warning restore
  102. #endif