PkixCrlUtilities.cs 3.1 KB

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