AttributeTable.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  8. {
  9. public class AttributeTable
  10. {
  11. private readonly IDictionary attributes;
  12. #if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
  13. [Obsolete]
  14. public AttributeTable(
  15. Hashtable attrs)
  16. {
  17. this.attributes = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable(attrs);
  18. }
  19. #endif
  20. public AttributeTable(
  21. IDictionary attrs)
  22. {
  23. this.attributes = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable(attrs);
  24. }
  25. public AttributeTable(
  26. Asn1EncodableVector v)
  27. {
  28. this.attributes = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable(v.Count);
  29. foreach (Asn1Encodable o in v)
  30. {
  31. Attribute a = Attribute.GetInstance(o);
  32. AddAttribute(a);
  33. }
  34. }
  35. public AttributeTable(
  36. Asn1Set s)
  37. {
  38. this.attributes = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable(s.Count);
  39. for (int i = 0; i != s.Count; i++)
  40. {
  41. Attribute a = Attribute.GetInstance(s[i]);
  42. AddAttribute(a);
  43. }
  44. }
  45. public AttributeTable(
  46. Attributes attrs)
  47. : this(Asn1Set.GetInstance(attrs.ToAsn1Object()))
  48. {
  49. }
  50. private void AddAttribute(
  51. Attribute a)
  52. {
  53. DerObjectIdentifier oid = a.AttrType;
  54. object obj = attributes[oid];
  55. if (obj == null)
  56. {
  57. attributes[oid] = a;
  58. }
  59. else
  60. {
  61. IList v;
  62. if (obj is Attribute)
  63. {
  64. v = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  65. v.Add(obj);
  66. v.Add(a);
  67. }
  68. else
  69. {
  70. v = (IList) obj;
  71. v.Add(a);
  72. }
  73. attributes[oid] = v;
  74. }
  75. }
  76. /// <summary>Return the first attribute matching the given OBJECT IDENTIFIER</summary>
  77. public Attribute this[DerObjectIdentifier oid]
  78. {
  79. get
  80. {
  81. object obj = attributes[oid];
  82. if (obj is IList)
  83. {
  84. return (Attribute)((IList)obj)[0];
  85. }
  86. return (Attribute) obj;
  87. }
  88. }
  89. [Obsolete("Use 'object[oid]' syntax instead")]
  90. public Attribute Get(
  91. DerObjectIdentifier oid)
  92. {
  93. return this[oid];
  94. }
  95. /**
  96. * Return all the attributes matching the OBJECT IDENTIFIER oid. The vector will be
  97. * empty if there are no attributes of the required type present.
  98. *
  99. * @param oid type of attribute required.
  100. * @return a vector of all the attributes found of type oid.
  101. */
  102. public Asn1EncodableVector GetAll(
  103. DerObjectIdentifier oid)
  104. {
  105. Asn1EncodableVector v = new Asn1EncodableVector();
  106. object obj = attributes[oid];
  107. if (obj is IList)
  108. {
  109. foreach (Attribute a in (IList)obj)
  110. {
  111. v.Add(a);
  112. }
  113. }
  114. else if (obj != null)
  115. {
  116. v.Add((Attribute) obj);
  117. }
  118. return v;
  119. }
  120. public int Count
  121. {
  122. get
  123. {
  124. int total = 0;
  125. foreach (object o in attributes.Values)
  126. {
  127. if (o is IList)
  128. {
  129. total += ((IList)o).Count;
  130. }
  131. else
  132. {
  133. ++total;
  134. }
  135. }
  136. return total;
  137. }
  138. }
  139. public IDictionary ToDictionary()
  140. {
  141. return BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable(attributes);
  142. }
  143. #if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
  144. [Obsolete("Use 'ToDictionary' instead")]
  145. public Hashtable ToHashtable()
  146. {
  147. return new Hashtable(attributes);
  148. }
  149. #endif
  150. public Asn1EncodableVector ToAsn1EncodableVector()
  151. {
  152. Asn1EncodableVector v = new Asn1EncodableVector();
  153. foreach (object obj in attributes.Values)
  154. {
  155. if (obj is IList)
  156. {
  157. foreach (object el in (IList)obj)
  158. {
  159. v.Add(Attribute.GetInstance(el));
  160. }
  161. }
  162. else
  163. {
  164. v.Add(Attribute.GetInstance(obj));
  165. }
  166. }
  167. return v;
  168. }
  169. public Attributes ToAttributes()
  170. {
  171. return new Attributes(this.ToAsn1EncodableVector());
  172. }
  173. /**
  174. * Return a new table with the passed in attribute added.
  175. *
  176. * @param attrType
  177. * @param attrValue
  178. * @return
  179. */
  180. public AttributeTable Add(DerObjectIdentifier attrType, Asn1Encodable attrValue)
  181. {
  182. AttributeTable newTable = new AttributeTable(attributes);
  183. newTable.AddAttribute(new Attribute(attrType, new DerSet(attrValue)));
  184. return newTable;
  185. }
  186. public AttributeTable Remove(DerObjectIdentifier attrType)
  187. {
  188. AttributeTable newTable = new AttributeTable(attributes);
  189. newTable.attributes.Remove(attrType);
  190. return newTable;
  191. }
  192. }
  193. }
  194. #pragma warning restore
  195. #endif