NameOrPseudonym.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.X500;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509.SigI
  8. {
  9. /**
  10. * Structure for a name or pseudonym.
  11. *
  12. * <pre>
  13. * NameOrPseudonym ::= CHOICE {
  14. * surAndGivenName SEQUENCE {
  15. * surName DirectoryString,
  16. * givenName SEQUENCE OF DirectoryString
  17. * },
  18. * pseudonym DirectoryString
  19. * }
  20. * </pre>
  21. *
  22. * @see org.bouncycastle.asn1.x509.sigi.PersonalData
  23. *
  24. */
  25. public class NameOrPseudonym
  26. : Asn1Encodable, IAsn1Choice
  27. {
  28. private readonly DirectoryString pseudonym;
  29. private readonly DirectoryString surname;
  30. private readonly Asn1Sequence givenName;
  31. public static NameOrPseudonym GetInstance(
  32. object obj)
  33. {
  34. if (obj == null || obj is NameOrPseudonym)
  35. {
  36. return (NameOrPseudonym)obj;
  37. }
  38. if (obj is IAsn1String)
  39. {
  40. return new NameOrPseudonym(DirectoryString.GetInstance(obj));
  41. }
  42. if (obj is Asn1Sequence)
  43. {
  44. return new NameOrPseudonym((Asn1Sequence) obj);
  45. }
  46. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  47. }
  48. /**
  49. * Constructor from DERString.
  50. * <p/>
  51. * The sequence is of type NameOrPseudonym:
  52. * <p/>
  53. * <pre>
  54. * NameOrPseudonym ::= CHOICE {
  55. * surAndGivenName SEQUENCE {
  56. * surName DirectoryString,
  57. * givenName SEQUENCE OF DirectoryString
  58. * },
  59. * pseudonym DirectoryString
  60. * }
  61. * </pre>
  62. * @param pseudonym pseudonym value to use.
  63. */
  64. public NameOrPseudonym(
  65. DirectoryString pseudonym)
  66. {
  67. this.pseudonym = pseudonym;
  68. }
  69. /**
  70. * Constructor from Asn1Sequence.
  71. * <p/>
  72. * The sequence is of type NameOrPseudonym:
  73. * <p/>
  74. * <pre>
  75. * NameOrPseudonym ::= CHOICE {
  76. * surAndGivenName SEQUENCE {
  77. * surName DirectoryString,
  78. * givenName SEQUENCE OF DirectoryString
  79. * },
  80. * pseudonym DirectoryString
  81. * }
  82. * </pre>
  83. *
  84. * @param seq The ASN.1 sequence.
  85. */
  86. private NameOrPseudonym(
  87. Asn1Sequence seq)
  88. {
  89. if (seq.Count != 2)
  90. throw new ArgumentException("Bad sequence size: " + seq.Count);
  91. if (!(seq[0] is IAsn1String))
  92. throw new ArgumentException("Bad object encountered: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(seq[0]));
  93. surname = DirectoryString.GetInstance(seq[0]);
  94. givenName = Asn1Sequence.GetInstance(seq[1]);
  95. }
  96. /**
  97. * Constructor from a given details.
  98. *
  99. * @param pseudonym The pseudonym.
  100. */
  101. public NameOrPseudonym(
  102. string pseudonym)
  103. : this(new DirectoryString(pseudonym))
  104. {
  105. }
  106. /**
  107. * Constructor from a given details.
  108. *
  109. * @param surname The surname.
  110. * @param givenName A sequence of directory strings making up the givenName
  111. */
  112. public NameOrPseudonym(
  113. DirectoryString surname,
  114. Asn1Sequence givenName)
  115. {
  116. this.surname = surname;
  117. this.givenName = givenName;
  118. }
  119. public DirectoryString Pseudonym
  120. {
  121. get { return pseudonym; }
  122. }
  123. public DirectoryString Surname
  124. {
  125. get { return surname; }
  126. }
  127. public DirectoryString[] GetGivenName()
  128. {
  129. DirectoryString[] items = new DirectoryString[givenName.Count];
  130. int count = 0;
  131. foreach (object o in givenName)
  132. {
  133. items[count++] = DirectoryString.GetInstance(o);
  134. }
  135. return items;
  136. }
  137. /**
  138. * Produce an object suitable for an Asn1OutputStream.
  139. * <p/>
  140. * Returns:
  141. * <p/>
  142. * <pre>
  143. * NameOrPseudonym ::= CHOICE {
  144. * surAndGivenName SEQUENCE {
  145. * surName DirectoryString,
  146. * givenName SEQUENCE OF DirectoryString
  147. * },
  148. * pseudonym DirectoryString
  149. * }
  150. * </pre>
  151. *
  152. * @return an Asn1Object
  153. */
  154. public override Asn1Object ToAsn1Object()
  155. {
  156. if (pseudonym != null)
  157. {
  158. return pseudonym.ToAsn1Object();
  159. }
  160. return new DerSequence(surname, givenName);
  161. }
  162. }
  163. }
  164. #pragma warning restore
  165. #endif