ProcurationSyntax.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X500;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.IsisMtt.X509
  8. {
  9. /**
  10. * Attribute to indicate that the certificate holder may sign in the name of a
  11. * third person.
  12. * <p>
  13. * ISIS-MTT PROFILE: The corresponding ProcurationSyntax contains either the
  14. * name of the person who is represented (subcomponent thirdPerson) or a
  15. * reference to his/her base certificate (in the component signingFor,
  16. * subcomponent certRef), furthermore the optional components country and
  17. * typeSubstitution to indicate the country whose laws apply, and respectively
  18. * the type of procuration (e.g. manager, procuration, custody).
  19. * </p>
  20. * <p>
  21. * ISIS-MTT PROFILE: The GeneralName MUST be of type directoryName and MAY only
  22. * contain: - RFC3039 attributes, except pseudonym (countryName, commonName,
  23. * surname, givenName, serialNumber, organizationName, organizationalUnitName,
  24. * stateOrProvincename, localityName, postalAddress) and - SubjectDirectoryName
  25. * attributes (title, dateOfBirth, placeOfBirth, gender, countryOfCitizenship,
  26. * countryOfResidence and NameAtBirth).
  27. * </p>
  28. * <pre>
  29. * ProcurationSyntax ::= SEQUENCE {
  30. * country [1] EXPLICIT PrintableString(SIZE(2)) OPTIONAL,
  31. * typeOfSubstitution [2] EXPLICIT DirectoryString (SIZE(1..128)) OPTIONAL,
  32. * signingFor [3] EXPLICIT SigningFor
  33. * }
  34. *
  35. * SigningFor ::= CHOICE
  36. * {
  37. * thirdPerson GeneralName,
  38. * certRef IssuerSerial
  39. * }
  40. * </pre>
  41. *
  42. */
  43. public class ProcurationSyntax
  44. : Asn1Encodable
  45. {
  46. private readonly string country;
  47. private readonly DirectoryString typeOfSubstitution;
  48. private readonly GeneralName thirdPerson;
  49. private readonly IssuerSerial certRef;
  50. public static ProcurationSyntax GetInstance(object obj)
  51. {
  52. if (obj == null || obj is ProcurationSyntax)
  53. return (ProcurationSyntax) obj;
  54. if (obj is Asn1Sequence seq)
  55. return new ProcurationSyntax(seq);
  56. throw new ArgumentException("unknown object in factory: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  57. }
  58. /**
  59. * Constructor from Asn1Sequence.
  60. * <p/>
  61. * The sequence is of type ProcurationSyntax:
  62. * <p/>
  63. * <pre>
  64. * ProcurationSyntax ::= SEQUENCE {
  65. * country [1] EXPLICIT PrintableString(SIZE(2)) OPTIONAL,
  66. * typeOfSubstitution [2] EXPLICIT DirectoryString (SIZE(1..128)) OPTIONAL,
  67. * signingFor [3] EXPLICIT SigningFor
  68. * }
  69. * <p/>
  70. * SigningFor ::= CHOICE
  71. * {
  72. * thirdPerson GeneralName,
  73. * certRef IssuerSerial
  74. * }
  75. * </pre>
  76. *
  77. * @param seq The ASN.1 sequence.
  78. */
  79. private ProcurationSyntax(Asn1Sequence seq)
  80. {
  81. if (seq.Count < 1 || seq.Count > 3)
  82. throw new ArgumentException("Bad sequence size: " + seq.Count);
  83. var e = seq.GetEnumerator();
  84. while (e.MoveNext())
  85. {
  86. Asn1TaggedObject o = Asn1TaggedObject.GetInstance(e.Current);
  87. switch (o.TagNo)
  88. {
  89. case 1:
  90. country = DerPrintableString.GetInstance(o, true).GetString();
  91. break;
  92. case 2:
  93. typeOfSubstitution = DirectoryString.GetInstance(o, true);
  94. break;
  95. case 3:
  96. Asn1Object signingFor = o.GetObject();
  97. if (signingFor is Asn1TaggedObject)
  98. {
  99. thirdPerson = GeneralName.GetInstance(signingFor);
  100. }
  101. else
  102. {
  103. certRef = IssuerSerial.GetInstance(signingFor);
  104. }
  105. break;
  106. default:
  107. throw new ArgumentException("Bad tag number: " + o.TagNo);
  108. }
  109. }
  110. }
  111. /**
  112. * Constructor from a given details.
  113. * <p/>
  114. * <p/>
  115. * Either <code>generalName</code> or <code>certRef</code> MUST be
  116. * <code>null</code>.
  117. *
  118. * @param country The country code whose laws apply.
  119. * @param typeOfSubstitution The type of procuration.
  120. * @param certRef Reference to certificate of the person who is represented.
  121. */
  122. public ProcurationSyntax(
  123. string country,
  124. DirectoryString typeOfSubstitution,
  125. IssuerSerial certRef)
  126. {
  127. this.country = country;
  128. this.typeOfSubstitution = typeOfSubstitution;
  129. this.thirdPerson = null;
  130. this.certRef = certRef;
  131. }
  132. /**
  133. * Constructor from a given details.
  134. * <p/>
  135. * <p/>
  136. * Either <code>generalName</code> or <code>certRef</code> MUST be
  137. * <code>null</code>.
  138. *
  139. * @param country The country code whose laws apply.
  140. * @param typeOfSubstitution The type of procuration.
  141. * @param thirdPerson The GeneralName of the person who is represented.
  142. */
  143. public ProcurationSyntax(
  144. string country,
  145. DirectoryString typeOfSubstitution,
  146. GeneralName thirdPerson)
  147. {
  148. this.country = country;
  149. this.typeOfSubstitution = typeOfSubstitution;
  150. this.thirdPerson = thirdPerson;
  151. this.certRef = null;
  152. }
  153. public virtual string Country
  154. {
  155. get { return country; }
  156. }
  157. public virtual DirectoryString TypeOfSubstitution
  158. {
  159. get { return typeOfSubstitution; }
  160. }
  161. public virtual GeneralName ThirdPerson
  162. {
  163. get { return thirdPerson; }
  164. }
  165. public virtual IssuerSerial CertRef
  166. {
  167. get { return certRef; }
  168. }
  169. /**
  170. * Produce an object suitable for an Asn1OutputStream.
  171. * <p/>
  172. * Returns:
  173. * <p/>
  174. * <pre>
  175. * ProcurationSyntax ::= SEQUENCE {
  176. * country [1] EXPLICIT PrintableString(SIZE(2)) OPTIONAL,
  177. * typeOfSubstitution [2] EXPLICIT DirectoryString (SIZE(1..128)) OPTIONAL,
  178. * signingFor [3] EXPLICIT SigningFor
  179. * }
  180. * <p/>
  181. * SigningFor ::= CHOICE
  182. * {
  183. * thirdPerson GeneralName,
  184. * certRef IssuerSerial
  185. * }
  186. * </pre>
  187. *
  188. * @return an Asn1Object
  189. */
  190. public override Asn1Object ToAsn1Object()
  191. {
  192. Asn1EncodableVector v = new Asn1EncodableVector();
  193. if (country != null)
  194. {
  195. v.Add(new DerTaggedObject(true, 1, new DerPrintableString(country, true)));
  196. }
  197. v.AddOptionalTagged(true, 2, typeOfSubstitution);
  198. if (thirdPerson != null)
  199. {
  200. v.Add(new DerTaggedObject(true, 3, thirdPerson));
  201. }
  202. else
  203. {
  204. v.Add(new DerTaggedObject(true, 3, certRef));
  205. }
  206. return new DerSequence(v);
  207. }
  208. }
  209. }
  210. #pragma warning restore
  211. #endif