Asn1Set.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #if PORTABLE || NETFX_CORE
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. #endif
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  13. {
  14. abstract public class Asn1Set
  15. : Asn1Object, IEnumerable
  16. {
  17. private readonly IList _set;
  18. /**
  19. * return an ASN1Set from the given object.
  20. *
  21. * @param obj the object we want converted.
  22. * @exception ArgumentException if the object cannot be converted.
  23. */
  24. public static Asn1Set GetInstance(
  25. object obj)
  26. {
  27. if (obj == null || obj is Asn1Set)
  28. {
  29. return (Asn1Set)obj;
  30. }
  31. else if (obj is Asn1SetParser)
  32. {
  33. return Asn1Set.GetInstance(((Asn1SetParser)obj).ToAsn1Object());
  34. }
  35. else if (obj is byte[])
  36. {
  37. try
  38. {
  39. return Asn1Set.GetInstance(FromByteArray((byte[])obj));
  40. }
  41. catch (IOException e)
  42. {
  43. throw new ArgumentException("failed to construct set from byte[]: " + e.Message);
  44. }
  45. }
  46. else if (obj is Asn1Encodable)
  47. {
  48. Asn1Object primitive = ((Asn1Encodable)obj).ToAsn1Object();
  49. if (primitive is Asn1Set)
  50. {
  51. return (Asn1Set)primitive;
  52. }
  53. }
  54. throw new ArgumentException("Unknown object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  55. }
  56. /**
  57. * Return an ASN1 set from a tagged object. There is a special
  58. * case here, if an object appears to have been explicitly tagged on
  59. * reading but we were expecting it to be implicitly tagged in the
  60. * normal course of events it indicates that we lost the surrounding
  61. * set - so we need to add it back (this will happen if the tagged
  62. * object is a sequence that contains other sequences). If you are
  63. * dealing with implicitly tagged sets you really <b>should</b>
  64. * be using this method.
  65. *
  66. * @param obj the tagged object.
  67. * @param explicitly true if the object is meant to be explicitly tagged
  68. * false otherwise.
  69. * @exception ArgumentException if the tagged object cannot
  70. * be converted.
  71. */
  72. public static Asn1Set GetInstance(
  73. Asn1TaggedObject obj,
  74. bool explicitly)
  75. {
  76. Asn1Object inner = obj.GetObject();
  77. if (explicitly)
  78. {
  79. if (!obj.IsExplicit())
  80. throw new ArgumentException("object implicit - explicit expected.");
  81. return (Asn1Set) inner;
  82. }
  83. //
  84. // constructed object which appears to be explicitly tagged
  85. // and it's really implicit means we have to add the
  86. // surrounding sequence.
  87. //
  88. if (obj.IsExplicit())
  89. {
  90. return new DerSet(inner);
  91. }
  92. if (inner is Asn1Set)
  93. {
  94. return (Asn1Set) inner;
  95. }
  96. //
  97. // in this case the parser returns a sequence, convert it
  98. // into a set.
  99. //
  100. if (inner is Asn1Sequence)
  101. {
  102. Asn1EncodableVector v = new Asn1EncodableVector();
  103. Asn1Sequence s = (Asn1Sequence) inner;
  104. foreach (Asn1Encodable ae in s)
  105. {
  106. v.Add(ae);
  107. }
  108. // TODO Should be able to construct set directly from sequence?
  109. return new DerSet(v, false);
  110. }
  111. throw new ArgumentException("Unknown object in GetInstance: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  112. }
  113. protected internal Asn1Set(
  114. int capacity)
  115. {
  116. _set = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(capacity);
  117. }
  118. public virtual IEnumerator GetEnumerator()
  119. {
  120. return _set.GetEnumerator();
  121. }
  122. [Obsolete("Use GetEnumerator() instead")]
  123. public IEnumerator GetObjects()
  124. {
  125. return GetEnumerator();
  126. }
  127. /**
  128. * return the object at the set position indicated by index.
  129. *
  130. * @param index the set number (starting at zero) of the object
  131. * @return the object at the set position indicated by index.
  132. */
  133. public virtual Asn1Encodable this[int index]
  134. {
  135. get { return (Asn1Encodable) _set[index]; }
  136. }
  137. [Obsolete("Use 'object[index]' syntax instead")]
  138. public Asn1Encodable GetObjectAt(
  139. int index)
  140. {
  141. return this[index];
  142. }
  143. [Obsolete("Use 'Count' property instead")]
  144. public int Size
  145. {
  146. get { return Count; }
  147. }
  148. public virtual int Count
  149. {
  150. get { return _set.Count; }
  151. }
  152. public virtual Asn1Encodable[] ToArray()
  153. {
  154. Asn1Encodable[] values = new Asn1Encodable[this.Count];
  155. for (int i = 0; i < this.Count; ++i)
  156. {
  157. values[i] = this[i];
  158. }
  159. return values;
  160. }
  161. private class Asn1SetParserImpl
  162. : Asn1SetParser
  163. {
  164. private readonly Asn1Set outer;
  165. private readonly int max;
  166. private int index;
  167. public Asn1SetParserImpl(
  168. Asn1Set outer)
  169. {
  170. this.outer = outer;
  171. this.max = outer.Count;
  172. }
  173. public IAsn1Convertible ReadObject()
  174. {
  175. if (index == max)
  176. return null;
  177. Asn1Encodable obj = outer[index++];
  178. if (obj is Asn1Sequence)
  179. return ((Asn1Sequence)obj).Parser;
  180. if (obj is Asn1Set)
  181. return ((Asn1Set)obj).Parser;
  182. // NB: Asn1OctetString implements Asn1OctetStringParser directly
  183. // if (obj is Asn1OctetString)
  184. // return ((Asn1OctetString)obj).Parser;
  185. return obj;
  186. }
  187. public virtual Asn1Object ToAsn1Object()
  188. {
  189. return outer;
  190. }
  191. }
  192. public Asn1SetParser Parser
  193. {
  194. get { return new Asn1SetParserImpl(this); }
  195. }
  196. protected override int Asn1GetHashCode()
  197. {
  198. int hc = Count;
  199. foreach (object o in this)
  200. {
  201. hc *= 17;
  202. if (o == null)
  203. {
  204. hc ^= DerNull.Instance.GetHashCode();
  205. }
  206. else
  207. {
  208. hc ^= o.GetHashCode();
  209. }
  210. }
  211. return hc;
  212. }
  213. protected override bool Asn1Equals(
  214. Asn1Object asn1Object)
  215. {
  216. Asn1Set other = asn1Object as Asn1Set;
  217. if (other == null)
  218. return false;
  219. if (Count != other.Count)
  220. {
  221. return false;
  222. }
  223. IEnumerator s1 = GetEnumerator();
  224. IEnumerator s2 = other.GetEnumerator();
  225. while (s1.MoveNext() && s2.MoveNext())
  226. {
  227. Asn1Object o1 = GetCurrent(s1).ToAsn1Object();
  228. Asn1Object o2 = GetCurrent(s2).ToAsn1Object();
  229. if (!o1.Equals(o2))
  230. return false;
  231. }
  232. return true;
  233. }
  234. private Asn1Encodable GetCurrent(IEnumerator e)
  235. {
  236. Asn1Encodable encObj = (Asn1Encodable)e.Current;
  237. // unfortunately null was allowed as a substitute for DER null
  238. if (encObj == null)
  239. return DerNull.Instance;
  240. return encObj;
  241. }
  242. protected internal void Sort()
  243. {
  244. if (_set.Count < 2)
  245. return;
  246. #if PORTABLE || NETFX_CORE
  247. var sorted = _set.Cast<Asn1Encodable>()
  248. .Select(a => new { Item = a, Key = a.GetEncoded(Asn1Encodable.Der) })
  249. .OrderBy(t => t.Key, new DerComparer())
  250. .Select(t => t.Item)
  251. .ToList();
  252. for (int i = 0; i < _set.Count; ++i)
  253. {
  254. _set[i] = sorted[i];
  255. }
  256. #else
  257. Asn1Encodable[] items = new Asn1Encodable[_set.Count];
  258. byte[][] keys = new byte[_set.Count][];
  259. for (int i = 0; i < _set.Count; ++i)
  260. {
  261. Asn1Encodable item = (Asn1Encodable)_set[i];
  262. items[i] = item;
  263. keys[i] = item.GetEncoded(Asn1Encodable.Der);
  264. }
  265. Array.Sort(keys, items, new DerComparer());
  266. for (int i = 0; i < _set.Count; ++i)
  267. {
  268. _set[i] = items[i];
  269. }
  270. #endif
  271. }
  272. protected internal void AddObject(Asn1Encodable obj)
  273. {
  274. _set.Add(obj);
  275. }
  276. public override string ToString()
  277. {
  278. return CollectionUtilities.ToString(_set);
  279. }
  280. #if PORTABLE || NETFX_CORE
  281. private class DerComparer
  282. : IComparer<byte[]>
  283. {
  284. public int Compare(byte[] x, byte[] y)
  285. {
  286. byte[] a = x, b = y;
  287. #else
  288. private class DerComparer
  289. : IComparer
  290. {
  291. public int Compare(object x, object y)
  292. {
  293. byte[] a = (byte[])x, b = (byte[])y;
  294. #endif
  295. int len = System.Math.Min(a.Length, b.Length);
  296. for (int i = 0; i != len; ++i)
  297. {
  298. byte ai = a[i], bi = b[i];
  299. if (ai != bi)
  300. return ai < bi ? -1 : 1;
  301. }
  302. if (a.Length > b.Length)
  303. return AllZeroesFrom(a, len) ? 0 : 1;
  304. if (a.Length < b.Length)
  305. return AllZeroesFrom(b, len) ? 0 : -1;
  306. return 0;
  307. }
  308. private bool AllZeroesFrom(byte[] bs, int pos)
  309. {
  310. while (pos < bs.Length)
  311. {
  312. if (bs[pos++] != 0)
  313. return false;
  314. }
  315. return true;
  316. }
  317. }
  318. }
  319. }
  320. #pragma warning restore
  321. #endif