Asn1Set.cs 11 KB

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