PkixBuilderParameters.cs 4.0 KB

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