Asn1Sequence.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  9. {
  10. public abstract class Asn1Sequence
  11. : Asn1Object, IEnumerable
  12. {
  13. // NOTE: Only non-readonly to support LazyDerSequence
  14. internal Asn1Encodable[] elements;
  15. /**
  16. * return an Asn1Sequence from the given object.
  17. *
  18. * @param obj the object we want converted.
  19. * @exception ArgumentException if the object cannot be converted.
  20. */
  21. public static Asn1Sequence GetInstance(
  22. object obj)
  23. {
  24. if (obj == null || obj is Asn1Sequence)
  25. {
  26. return (Asn1Sequence)obj;
  27. }
  28. else if (obj is Asn1SequenceParser)
  29. {
  30. return GetInstance(((Asn1SequenceParser)obj).ToAsn1Object());
  31. }
  32. else if (obj is byte[])
  33. {
  34. try
  35. {
  36. return GetInstance(FromByteArray((byte[])obj));
  37. }
  38. catch (IOException e)
  39. {
  40. throw new ArgumentException("failed to construct sequence from byte[]: " + e.Message);
  41. }
  42. }
  43. else if (obj is Asn1Encodable)
  44. {
  45. Asn1Object primitive = ((Asn1Encodable)obj).ToAsn1Object();
  46. if (primitive is Asn1Sequence)
  47. {
  48. return (Asn1Sequence)primitive;
  49. }
  50. }
  51. throw new ArgumentException("Unknown object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  52. }
  53. /**
  54. * Return an ASN1 sequence from a tagged object. There is a special
  55. * case here, if an object appears to have been explicitly tagged on
  56. * reading but we were expecting it to be implicitly tagged in the
  57. * normal course of events it indicates that we lost the surrounding
  58. * sequence - so we need to add it back (this will happen if the tagged
  59. * object is a sequence that contains other sequences). If you are
  60. * dealing with implicitly tagged sequences you really <b>should</b>
  61. * be using this method.
  62. *
  63. * @param obj the tagged object.
  64. * @param explicitly true if the object is meant to be explicitly tagged,
  65. * false otherwise.
  66. * @exception ArgumentException if the tagged object cannot
  67. * be converted.
  68. */
  69. public static Asn1Sequence GetInstance(
  70. Asn1TaggedObject obj,
  71. bool explicitly)
  72. {
  73. Asn1Object inner = obj.GetObject();
  74. if (explicitly)
  75. {
  76. if (!obj.IsExplicit())
  77. throw new ArgumentException("object implicit - explicit expected.");
  78. return (Asn1Sequence) inner;
  79. }
  80. //
  81. // constructed object which appears to be explicitly tagged
  82. // when it should be implicit means we have to add the
  83. // surrounding sequence.
  84. //
  85. if (obj.IsExplicit())
  86. {
  87. if (obj is BerTaggedObject)
  88. {
  89. return new BerSequence(inner);
  90. }
  91. return new DerSequence(inner);
  92. }
  93. if (inner is Asn1Sequence)
  94. {
  95. return (Asn1Sequence) inner;
  96. }
  97. throw new ArgumentException("Unknown object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  98. }
  99. protected internal Asn1Sequence()
  100. {
  101. this.elements = Asn1EncodableVector.EmptyElements;
  102. }
  103. protected internal Asn1Sequence(Asn1Encodable element)
  104. {
  105. if (null == element)
  106. throw new ArgumentNullException("element");
  107. this.elements = new Asn1Encodable[]{ element };
  108. }
  109. protected internal Asn1Sequence(params Asn1Encodable[] elements)
  110. {
  111. if (Arrays.IsNullOrContainsNull(elements))
  112. throw new NullReferenceException("'elements' cannot be null, or contain null");
  113. this.elements = Asn1EncodableVector.CloneElements(elements);
  114. }
  115. protected internal Asn1Sequence(Asn1EncodableVector elementVector)
  116. {
  117. if (null == elementVector)
  118. throw new ArgumentNullException("elementVector");
  119. this.elements = elementVector.TakeElements();
  120. }
  121. public virtual IEnumerator GetEnumerator()
  122. {
  123. return elements.GetEnumerator();
  124. }
  125. private class Asn1SequenceParserImpl
  126. : Asn1SequenceParser
  127. {
  128. private readonly Asn1Sequence outer;
  129. private readonly int max;
  130. private int index;
  131. public Asn1SequenceParserImpl(
  132. Asn1Sequence outer)
  133. {
  134. this.outer = outer;
  135. this.max = outer.Count;
  136. }
  137. public IAsn1Convertible ReadObject()
  138. {
  139. if (index == max)
  140. return null;
  141. Asn1Encodable obj = outer[index++];
  142. if (obj is Asn1Sequence)
  143. return ((Asn1Sequence)obj).Parser;
  144. if (obj is Asn1Set)
  145. return ((Asn1Set)obj).Parser;
  146. // NB: Asn1OctetString implements Asn1OctetStringParser directly
  147. // if (obj is Asn1OctetString)
  148. // return ((Asn1OctetString)obj).Parser;
  149. return obj;
  150. }
  151. public Asn1Object ToAsn1Object()
  152. {
  153. return outer;
  154. }
  155. }
  156. public virtual Asn1SequenceParser Parser
  157. {
  158. get { return new Asn1SequenceParserImpl(this); }
  159. }
  160. /**
  161. * return the object at the sequence position indicated by index.
  162. *
  163. * @param index the sequence number (starting at zero) of the object
  164. * @return the object at the sequence position indicated by index.
  165. */
  166. public virtual Asn1Encodable this[int index]
  167. {
  168. get { return elements[index]; }
  169. }
  170. public virtual int Count
  171. {
  172. get { return elements.Length; }
  173. }
  174. public virtual Asn1Encodable[] ToArray()
  175. {
  176. return Asn1EncodableVector.CloneElements(elements);
  177. }
  178. protected override int Asn1GetHashCode()
  179. {
  180. //return Arrays.GetHashCode(elements);
  181. int i = elements.Length;
  182. int hc = i + 1;
  183. while (--i >= 0)
  184. {
  185. hc *= 257;
  186. hc ^= elements[i].ToAsn1Object().CallAsn1GetHashCode();
  187. }
  188. return hc;
  189. }
  190. protected override bool Asn1Equals(Asn1Object asn1Object)
  191. {
  192. Asn1Sequence that = asn1Object as Asn1Sequence;
  193. if (null == that)
  194. return false;
  195. int count = this.Count;
  196. if (that.Count != count)
  197. return false;
  198. for (int i = 0; i < count; ++i)
  199. {
  200. Asn1Object o1 = this.elements[i].ToAsn1Object();
  201. Asn1Object o2 = that.elements[i].ToAsn1Object();
  202. if (o1 != o2 && !o1.CallAsn1Equals(o2))
  203. return false;
  204. }
  205. return true;
  206. }
  207. public override string ToString()
  208. {
  209. return CollectionUtilities.ToString(elements);
  210. }
  211. }
  212. }
  213. #pragma warning restore
  214. #endif