RoleSyntax.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  6. {
  7. /**
  8. * Implementation of the RoleSyntax object as specified by the RFC3281.
  9. *
  10. * <pre>
  11. * RoleSyntax ::= SEQUENCE {
  12. * roleAuthority [0] GeneralNames OPTIONAL,
  13. * roleName [1] GeneralName
  14. * }
  15. * </pre>
  16. */
  17. public class RoleSyntax
  18. : Asn1Encodable
  19. {
  20. private readonly GeneralNames roleAuthority;
  21. private readonly GeneralName roleName;
  22. /**
  23. * RoleSyntax factory method.
  24. * @param obj the object used to construct an instance of <code>
  25. * RoleSyntax</code>. It must be an instance of <code>RoleSyntax
  26. * </code> or <code>Asn1Sequence</code>.
  27. * @return the instance of <code>RoleSyntax</code> built from the
  28. * supplied object.
  29. * @throws java.lang.ArgumentException if the object passed
  30. * to the factory is not an instance of <code>RoleSyntax</code> or
  31. * <code>Asn1Sequence</code>.
  32. */
  33. public static RoleSyntax GetInstance(
  34. object obj)
  35. {
  36. if (obj is RoleSyntax)
  37. return (RoleSyntax)obj;
  38. if (obj != null)
  39. return new RoleSyntax(Asn1Sequence.GetInstance(obj));
  40. return null;
  41. }
  42. /**
  43. * Constructor.
  44. * @param roleAuthority the role authority of this RoleSyntax.
  45. * @param roleName the role name of this RoleSyntax.
  46. */
  47. public RoleSyntax(
  48. GeneralNames roleAuthority,
  49. GeneralName roleName)
  50. {
  51. if (roleName == null
  52. || roleName.TagNo != GeneralName.UniformResourceIdentifier
  53. || ((IAsn1String) roleName.Name).GetString().Equals(""))
  54. {
  55. throw new ArgumentException("the role name MUST be non empty and MUST " +
  56. "use the URI option of GeneralName");
  57. }
  58. this.roleAuthority = roleAuthority;
  59. this.roleName = roleName;
  60. }
  61. /**
  62. * Constructor. Invoking this constructor is the same as invoking
  63. * <code>new RoleSyntax(null, roleName)</code>.
  64. * @param roleName the role name of this RoleSyntax.
  65. */
  66. public RoleSyntax(
  67. GeneralName roleName)
  68. : this(null, roleName)
  69. {
  70. }
  71. /**
  72. * Utility constructor. Takes a <code>string</code> argument representing
  73. * the role name, builds a <code>GeneralName</code> to hold the role name
  74. * and calls the constructor that takes a <code>GeneralName</code>.
  75. * @param roleName
  76. */
  77. public RoleSyntax(
  78. string roleName)
  79. : this(new GeneralName(GeneralName.UniformResourceIdentifier,
  80. (roleName == null)? "": roleName))
  81. {
  82. }
  83. /**
  84. * Constructor that builds an instance of <code>RoleSyntax</code> by
  85. * extracting the encoded elements from the <code>Asn1Sequence</code>
  86. * object supplied.
  87. * @param seq an instance of <code>Asn1Sequence</code> that holds
  88. * the encoded elements used to build this <code>RoleSyntax</code>.
  89. */
  90. private RoleSyntax(
  91. Asn1Sequence seq)
  92. {
  93. if (seq.Count < 1 || seq.Count > 2)
  94. {
  95. throw new ArgumentException("Bad sequence size: " + seq.Count);
  96. }
  97. for (int i = 0; i != seq.Count; i++)
  98. {
  99. Asn1TaggedObject taggedObject = Asn1TaggedObject.GetInstance(seq[i]);
  100. switch (taggedObject.TagNo)
  101. {
  102. case 0:
  103. roleAuthority = GeneralNames.GetInstance(taggedObject, false);
  104. break;
  105. case 1:
  106. roleName = GeneralName.GetInstance(taggedObject, true);
  107. break;
  108. default:
  109. throw new ArgumentException("Unknown tag in RoleSyntax");
  110. }
  111. }
  112. }
  113. /**
  114. * Gets the role authority of this RoleSyntax.
  115. * @return an instance of <code>GeneralNames</code> holding the
  116. * role authority of this RoleSyntax.
  117. */
  118. public GeneralNames RoleAuthority
  119. {
  120. get { return this.roleAuthority; }
  121. }
  122. /**
  123. * Gets the role name of this RoleSyntax.
  124. * @return an instance of <code>GeneralName</code> holding the
  125. * role name of this RoleSyntax.
  126. */
  127. public GeneralName RoleName
  128. {
  129. get { return this.roleName; }
  130. }
  131. /**
  132. * Gets the role name as a <code>java.lang.string</code> object.
  133. * @return the role name of this RoleSyntax represented as a
  134. * <code>string</code> object.
  135. */
  136. public string GetRoleNameAsString()
  137. {
  138. return ((IAsn1String) this.roleName.Name).GetString();
  139. }
  140. /**
  141. * Gets the role authority as a <code>string[]</code> object.
  142. * @return the role authority of this RoleSyntax represented as a
  143. * <code>string[]</code> array.
  144. */
  145. public string[] GetRoleAuthorityAsString()
  146. {
  147. if (roleAuthority == null)
  148. {
  149. return new string[0];
  150. }
  151. GeneralName[] names = roleAuthority.GetNames();
  152. string[] namesString = new string[names.Length];
  153. for(int i = 0; i < names.Length; i++)
  154. {
  155. Asn1Encodable asn1Value = names[i].Name;
  156. if (asn1Value is IAsn1String)
  157. {
  158. namesString[i] = ((IAsn1String) asn1Value).GetString();
  159. }
  160. else
  161. {
  162. namesString[i] = asn1Value.ToString();
  163. }
  164. }
  165. return namesString;
  166. }
  167. /**
  168. * Implementation of the method <code>ToAsn1Object</code> as
  169. * required by the superclass <code>ASN1Encodable</code>.
  170. *
  171. * <pre>
  172. * RoleSyntax ::= SEQUENCE {
  173. * roleAuthority [0] GeneralNames OPTIONAL,
  174. * roleName [1] GeneralName
  175. * }
  176. * </pre>
  177. */
  178. public override Asn1Object ToAsn1Object()
  179. {
  180. Asn1EncodableVector v = new Asn1EncodableVector();
  181. v.AddOptionalTagged(false, 0, roleAuthority);
  182. v.Add(new DerTaggedObject(true, 1, roleName));
  183. return new DerSequence(v);
  184. }
  185. public override string ToString()
  186. {
  187. StringBuilder buff = new StringBuilder("Name: " + this.GetRoleNameAsString() +
  188. " - Auth: ");
  189. if (this.roleAuthority == null || roleAuthority.GetNames().Length == 0)
  190. {
  191. buff.Append("N/A");
  192. }
  193. else
  194. {
  195. string[] names = this.GetRoleAuthorityAsString();
  196. buff.Append('[').Append(names[0]);
  197. for(int i = 1; i < names.Length; i++)
  198. {
  199. buff.Append(", ").Append(names[i]);
  200. }
  201. buff.Append(']');
  202. }
  203. return buff.ToString();
  204. }
  205. }
  206. }
  207. #pragma warning restore
  208. #endif