AttributeTable.cs 6.1 KB

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