GeneralSubtree.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  6. {
  7. /**
  8. * Class for containing a restriction object subtrees in NameConstraints. See
  9. * RFC 3280.
  10. *
  11. * <pre>
  12. *
  13. * GeneralSubtree ::= SEQUENCE
  14. * {
  15. * baseName GeneralName,
  16. * minimum [0] BaseDistance DEFAULT 0,
  17. * maximum [1] BaseDistance OPTIONAL
  18. * }
  19. * </pre>
  20. *
  21. * @see org.bouncycastle.asn1.x509.NameConstraints
  22. *
  23. */
  24. public class GeneralSubtree
  25. : Asn1Encodable
  26. {
  27. private readonly GeneralName baseName;
  28. private readonly DerInteger minimum;
  29. private readonly DerInteger maximum;
  30. private GeneralSubtree(
  31. Asn1Sequence seq)
  32. {
  33. baseName = GeneralName.GetInstance(seq[0]);
  34. switch (seq.Count)
  35. {
  36. case 1:
  37. break;
  38. case 2:
  39. {
  40. Asn1TaggedObject o = Asn1TaggedObject.GetInstance(seq[1]);
  41. switch (o.TagNo)
  42. {
  43. case 0:
  44. minimum = DerInteger.GetInstance(o, false);
  45. break;
  46. case 1:
  47. maximum = DerInteger.GetInstance(o, false);
  48. break;
  49. default:
  50. throw new ArgumentException("Bad tag number: " + o.TagNo);
  51. }
  52. break;
  53. }
  54. case 3:
  55. {
  56. {
  57. Asn1TaggedObject oMin = Asn1TaggedObject.GetInstance(seq[1]);
  58. if (oMin.TagNo != 0)
  59. throw new ArgumentException("Bad tag number for 'minimum': " + oMin.TagNo);
  60. minimum = DerInteger.GetInstance(oMin, false);
  61. }
  62. {
  63. Asn1TaggedObject oMax = Asn1TaggedObject.GetInstance(seq[2]);
  64. if (oMax.TagNo != 1)
  65. throw new ArgumentException("Bad tag number for 'maximum': " + oMax.TagNo);
  66. maximum = DerInteger.GetInstance(oMax, false);
  67. }
  68. break;
  69. }
  70. default:
  71. throw new ArgumentException("Bad sequence size: " + seq.Count);
  72. }
  73. }
  74. /**
  75. * Constructor from a given details.
  76. *
  77. * According RFC 3280, the minimum and maximum fields are not used with any
  78. * name forms, thus minimum MUST be zero, and maximum MUST be absent.
  79. * <p>
  80. * If minimum is <code>null</code>, zero is assumed, if
  81. * maximum is <code>null</code>, maximum is absent.</p>
  82. *
  83. * @param baseName
  84. * A restriction.
  85. * @param minimum
  86. * Minimum
  87. *
  88. * @param maximum
  89. * Maximum
  90. */
  91. public GeneralSubtree(
  92. GeneralName baseName,
  93. BigInteger minimum,
  94. BigInteger maximum)
  95. {
  96. this.baseName = baseName;
  97. if (minimum != null)
  98. {
  99. this.minimum = new DerInteger(minimum);
  100. }
  101. if (maximum != null)
  102. {
  103. this.maximum = new DerInteger(maximum);
  104. }
  105. }
  106. public GeneralSubtree(
  107. GeneralName baseName)
  108. : this(baseName, null, null)
  109. {
  110. }
  111. public static GeneralSubtree GetInstance(
  112. Asn1TaggedObject o,
  113. bool isExplicit)
  114. {
  115. return new GeneralSubtree(Asn1Sequence.GetInstance(o, isExplicit));
  116. }
  117. public static GeneralSubtree GetInstance(
  118. object obj)
  119. {
  120. if (obj == null)
  121. {
  122. return null;
  123. }
  124. if (obj is GeneralSubtree)
  125. {
  126. return (GeneralSubtree) obj;
  127. }
  128. return new GeneralSubtree(Asn1Sequence.GetInstance(obj));
  129. }
  130. public GeneralName Base
  131. {
  132. get { return baseName; }
  133. }
  134. public BigInteger Minimum
  135. {
  136. get { return minimum == null ? BigInteger.Zero : minimum.Value; }
  137. }
  138. public BigInteger Maximum
  139. {
  140. get { return maximum == null ? null : maximum.Value; }
  141. }
  142. /**
  143. * Produce an object suitable for an Asn1OutputStream.
  144. *
  145. * Returns:
  146. *
  147. * <pre>
  148. * GeneralSubtree ::= SEQUENCE
  149. * {
  150. * baseName GeneralName,
  151. * minimum [0] BaseDistance DEFAULT 0,
  152. * maximum [1] BaseDistance OPTIONAL
  153. * }
  154. * </pre>
  155. *
  156. * @return a DERObject
  157. */
  158. public override Asn1Object ToAsn1Object()
  159. {
  160. Asn1EncodableVector v = new Asn1EncodableVector(baseName);
  161. if (minimum != null && !minimum.HasValue(0))
  162. {
  163. v.Add(new DerTaggedObject(false, 0, minimum));
  164. }
  165. v.AddOptionalTagged(false, 1, maximum);
  166. return new DerSequence(v);
  167. }
  168. }
  169. }
  170. #pragma warning restore
  171. #endif