AttributeTable.cs 5.3 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. public Attribute Get(
  90. DerObjectIdentifier oid)
  91. {
  92. return this[oid];
  93. }
  94. /**
  95. * Return all the attributes matching the OBJECT IDENTIFIER oid. The vector will be
  96. * empty if there are no attributes of the required type present.
  97. *
  98. * @param oid type of attribute required.
  99. * @return a vector of all the attributes found of type oid.
  100. */
  101. public Asn1EncodableVector GetAll(
  102. DerObjectIdentifier oid)
  103. {
  104. Asn1EncodableVector v = new Asn1EncodableVector();
  105. object obj = attributes[oid];
  106. if (obj is IList)
  107. {
  108. foreach (Attribute a in (IList)obj)
  109. {
  110. v.Add(a);
  111. }
  112. }
  113. else if (obj != null)
  114. {
  115. v.Add((Attribute) obj);
  116. }
  117. return v;
  118. }
  119. public int Count
  120. {
  121. get
  122. {
  123. int total = 0;
  124. foreach (object o in attributes.Values)
  125. {
  126. if (o is IList)
  127. {
  128. total += ((IList)o).Count;
  129. }
  130. else
  131. {
  132. ++total;
  133. }
  134. }
  135. return total;
  136. }
  137. }
  138. public IDictionary ToDictionary()
  139. {
  140. return BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable(attributes);
  141. }
  142. #if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
  143. public Hashtable ToHashtable()
  144. {
  145. return new Hashtable(attributes);
  146. }
  147. #endif
  148. public Asn1EncodableVector ToAsn1EncodableVector()
  149. {
  150. Asn1EncodableVector v = new Asn1EncodableVector();
  151. foreach (object obj in attributes.Values)
  152. {
  153. if (obj is IList)
  154. {
  155. foreach (object el in (IList)obj)
  156. {
  157. v.Add(Attribute.GetInstance(el));
  158. }
  159. }
  160. else
  161. {
  162. v.Add(Attribute.GetInstance(obj));
  163. }
  164. }
  165. return v;
  166. }
  167. public Attributes ToAttributes()
  168. {
  169. return new Attributes(this.ToAsn1EncodableVector());
  170. }
  171. /**
  172. * Return a new table with the passed in attribute added.
  173. *
  174. * @param attrType
  175. * @param attrValue
  176. * @return
  177. */
  178. public AttributeTable Add(DerObjectIdentifier attrType, Asn1Encodable attrValue)
  179. {
  180. AttributeTable newTable = new AttributeTable(attributes);
  181. newTable.AddAttribute(new Attribute(attrType, new DerSet(attrValue)));
  182. return newTable;
  183. }
  184. public AttributeTable Remove(DerObjectIdentifier attrType)
  185. {
  186. AttributeTable newTable = new AttributeTable(attributes);
  187. newTable.attributes.Remove(attrType);
  188. return newTable;
  189. }
  190. }
  191. }
  192. #pragma warning restore
  193. #endif