PkixBuilderParameters.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  10. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Pkix
  11. {
  12. /// <summary>
  13. /// Summary description for PkixBuilderParameters.
  14. /// </summary>
  15. public class PkixBuilderParameters
  16. : PkixParameters
  17. {
  18. private int maxPathLength = 5;
  19. private ISet<X509Certificate> excludedCerts = new HashSet<X509Certificate>();
  20. /**
  21. * Returns an instance of <code>PkixBuilderParameters</code>.
  22. * <p>
  23. * This method can be used to get a copy from other
  24. * <code>PKIXBuilderParameters</code>, <code>PKIXParameters</code>,
  25. * and <code>ExtendedPKIXParameters</code> instances.
  26. * </p>
  27. *
  28. * @param pkixParams The PKIX parameters to create a copy of.
  29. * @return An <code>PkixBuilderParameters</code> instance.
  30. */
  31. public static PkixBuilderParameters GetInstance(
  32. PkixParameters pkixParams)
  33. {
  34. PkixBuilderParameters parameters = new PkixBuilderParameters(
  35. pkixParams.GetTrustAnchors(),
  36. pkixParams.GetTargetConstraintsCert(),
  37. pkixParams.GetTargetConstraintsAttrCert());
  38. parameters.SetParams(pkixParams);
  39. return parameters;
  40. }
  41. public PkixBuilderParameters(ISet<TrustAnchor> trustAnchors, ISelector<X509Certificate> targetConstraintsCert)
  42. : this(trustAnchors, targetConstraintsCert, null)
  43. {
  44. }
  45. public PkixBuilderParameters(ISet<TrustAnchor> trustAnchors, ISelector<X509Certificate> targetConstraintsCert,
  46. ISelector<X509V2AttributeCertificate> targetConstraintsAttrCert)
  47. : base(trustAnchors)
  48. {
  49. SetTargetConstraintsCert(targetConstraintsCert);
  50. SetTargetConstraintsAttrCert(targetConstraintsAttrCert);
  51. }
  52. public virtual int MaxPathLength
  53. {
  54. get { return maxPathLength; }
  55. set
  56. {
  57. if (value < -1)
  58. {
  59. throw new InvalidParameterException(
  60. "The maximum path length parameter can not be less than -1.");
  61. }
  62. this.maxPathLength = value;
  63. }
  64. }
  65. /// <summary>
  66. /// Excluded certificates are not used for building a certification path.
  67. /// </summary>
  68. /// <returns>the excluded certificates.</returns>
  69. public virtual ISet<X509Certificate> GetExcludedCerts()
  70. {
  71. return new HashSet<X509Certificate>(excludedCerts);
  72. }
  73. /// <summary>
  74. /// Sets the excluded certificates which are not used for building a
  75. /// certification path. If the <code>ISet</code> is <code>null</code> an
  76. /// empty set is assumed.
  77. /// </summary>
  78. /// <remarks>
  79. /// The given set is cloned to protect it against subsequent modifications.
  80. /// </remarks>
  81. /// <param name="excludedCerts">The excluded certificates to set.</param>
  82. public virtual void SetExcludedCerts(ISet<X509Certificate> excludedCerts)
  83. {
  84. if (excludedCerts == null)
  85. {
  86. this.excludedCerts = new HashSet<X509Certificate>();
  87. }
  88. else
  89. {
  90. this.excludedCerts = new HashSet<X509Certificate>(excludedCerts);
  91. }
  92. }
  93. /**
  94. * Can alse handle <code>ExtendedPKIXBuilderParameters</code> and
  95. * <code>PKIXBuilderParameters</code>.
  96. *
  97. * @param params Parameters to set.
  98. * @see org.bouncycastle.x509.ExtendedPKIXParameters#setParams(java.security.cert.PKIXParameters)
  99. */
  100. protected override void SetParams(PkixParameters parameters)
  101. {
  102. base.SetParams(parameters);
  103. if (parameters is PkixBuilderParameters _params)
  104. {
  105. maxPathLength = _params.maxPathLength;
  106. excludedCerts = new HashSet<X509Certificate>(_params.excludedCerts);
  107. }
  108. }
  109. /**
  110. * Makes a copy of this <code>PKIXParameters</code> object. Changes to the
  111. * copy will not affect the original and vice versa.
  112. *
  113. * @return a copy of this <code>PKIXParameters</code> object
  114. */
  115. public override object Clone()
  116. {
  117. PkixBuilderParameters parameters = new PkixBuilderParameters(
  118. GetTrustAnchors(),
  119. GetTargetConstraintsCert(),
  120. GetTargetConstraintsAttrCert());
  121. parameters.SetParams(this);
  122. return parameters;
  123. }
  124. public override string ToString()
  125. {
  126. StringBuilder s = new StringBuilder();
  127. s.AppendLine("PkixBuilderParameters [");
  128. s.Append(base.ToString());
  129. s.Append(" Maximum Path Length: ");
  130. s.Append(MaxPathLength);
  131. s.AppendLine();
  132. s.AppendLine("]");
  133. return s.ToString();
  134. }
  135. }
  136. }
  137. #pragma warning restore
  138. #endif