PersonalData.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.Math;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509.SigI
  9. {
  10. /**
  11. * Contains personal data for the otherName field in the subjectAltNames
  12. * extension.
  13. * <p/>
  14. * <pre>
  15. * PersonalData ::= SEQUENCE {
  16. * nameOrPseudonym NameOrPseudonym,
  17. * nameDistinguisher [0] INTEGER OPTIONAL,
  18. * dateOfBirth [1] GeneralizedTime OPTIONAL,
  19. * placeOfBirth [2] DirectoryString OPTIONAL,
  20. * gender [3] PrintableString OPTIONAL,
  21. * postalAddress [4] DirectoryString OPTIONAL
  22. * }
  23. * </pre>
  24. *
  25. * @see org.bouncycastle.asn1.x509.sigi.NameOrPseudonym
  26. * @see org.bouncycastle.asn1.x509.sigi.SigIObjectIdentifiers
  27. */
  28. public class PersonalData
  29. : Asn1Encodable
  30. {
  31. private readonly NameOrPseudonym nameOrPseudonym;
  32. private readonly BigInteger nameDistinguisher;
  33. private readonly DerGeneralizedTime dateOfBirth;
  34. private readonly DirectoryString placeOfBirth;
  35. private readonly string gender;
  36. private readonly DirectoryString postalAddress;
  37. public static PersonalData GetInstance(
  38. object obj)
  39. {
  40. if (obj == null || obj is PersonalData)
  41. {
  42. return (PersonalData) obj;
  43. }
  44. if (obj is Asn1Sequence)
  45. {
  46. return new PersonalData((Asn1Sequence) obj);
  47. }
  48. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  49. }
  50. /**
  51. * Constructor from Asn1Sequence.
  52. * <p/>
  53. * The sequence is of type NameOrPseudonym:
  54. * <p/>
  55. * <pre>
  56. * PersonalData ::= SEQUENCE {
  57. * nameOrPseudonym NameOrPseudonym,
  58. * nameDistinguisher [0] INTEGER OPTIONAL,
  59. * dateOfBirth [1] GeneralizedTime OPTIONAL,
  60. * placeOfBirth [2] DirectoryString OPTIONAL,
  61. * gender [3] PrintableString OPTIONAL,
  62. * postalAddress [4] DirectoryString OPTIONAL
  63. * }
  64. * </pre>
  65. *
  66. * @param seq The ASN.1 sequence.
  67. */
  68. private PersonalData(
  69. Asn1Sequence seq)
  70. {
  71. if (seq.Count < 1)
  72. throw new ArgumentException("Bad sequence size: " + seq.Count);
  73. IEnumerator e = seq.GetEnumerator();
  74. e.MoveNext();
  75. nameOrPseudonym = NameOrPseudonym.GetInstance(e.Current);
  76. while (e.MoveNext())
  77. {
  78. Asn1TaggedObject o = Asn1TaggedObject.GetInstance(e.Current);
  79. int tag = o.TagNo;
  80. switch (tag)
  81. {
  82. case 0:
  83. nameDistinguisher = DerInteger.GetInstance(o, false).Value;
  84. break;
  85. case 1:
  86. dateOfBirth = DerGeneralizedTime.GetInstance(o, false);
  87. break;
  88. case 2:
  89. placeOfBirth = DirectoryString.GetInstance(o, true);
  90. break;
  91. case 3:
  92. gender = DerPrintableString.GetInstance(o, false).GetString();
  93. break;
  94. case 4:
  95. postalAddress = DirectoryString.GetInstance(o, true);
  96. break;
  97. default:
  98. throw new ArgumentException("Bad tag number: " + o.TagNo);
  99. }
  100. }
  101. }
  102. /**
  103. * Constructor from a given details.
  104. *
  105. * @param nameOrPseudonym Name or pseudonym.
  106. * @param nameDistinguisher Name distinguisher.
  107. * @param dateOfBirth Date of birth.
  108. * @param placeOfBirth Place of birth.
  109. * @param gender Gender.
  110. * @param postalAddress Postal Address.
  111. */
  112. public PersonalData(
  113. NameOrPseudonym nameOrPseudonym,
  114. BigInteger nameDistinguisher,
  115. DerGeneralizedTime dateOfBirth,
  116. DirectoryString placeOfBirth,
  117. string gender,
  118. DirectoryString postalAddress)
  119. {
  120. this.nameOrPseudonym = nameOrPseudonym;
  121. this.dateOfBirth = dateOfBirth;
  122. this.gender = gender;
  123. this.nameDistinguisher = nameDistinguisher;
  124. this.postalAddress = postalAddress;
  125. this.placeOfBirth = placeOfBirth;
  126. }
  127. public NameOrPseudonym NameOrPseudonym
  128. {
  129. get { return nameOrPseudonym; }
  130. }
  131. public BigInteger NameDistinguisher
  132. {
  133. get { return nameDistinguisher; }
  134. }
  135. public DerGeneralizedTime DateOfBirth
  136. {
  137. get { return dateOfBirth; }
  138. }
  139. public DirectoryString PlaceOfBirth
  140. {
  141. get { return placeOfBirth; }
  142. }
  143. public string Gender
  144. {
  145. get { return gender; }
  146. }
  147. public DirectoryString PostalAddress
  148. {
  149. get { return postalAddress; }
  150. }
  151. /**
  152. * Produce an object suitable for an Asn1OutputStream.
  153. * <p/>
  154. * Returns:
  155. * <p/>
  156. * <pre>
  157. * PersonalData ::= SEQUENCE {
  158. * nameOrPseudonym NameOrPseudonym,
  159. * nameDistinguisher [0] INTEGER OPTIONAL,
  160. * dateOfBirth [1] GeneralizedTime OPTIONAL,
  161. * placeOfBirth [2] DirectoryString OPTIONAL,
  162. * gender [3] PrintableString OPTIONAL,
  163. * postalAddress [4] DirectoryString OPTIONAL
  164. * }
  165. * </pre>
  166. *
  167. * @return an Asn1Object
  168. */
  169. public override Asn1Object ToAsn1Object()
  170. {
  171. Asn1EncodableVector v = new Asn1EncodableVector(nameOrPseudonym);
  172. if (null != nameDistinguisher)
  173. {
  174. v.Add(new DerTaggedObject(false, 0, new DerInteger(nameDistinguisher)));
  175. }
  176. v.AddOptionalTagged(false, 1, dateOfBirth);
  177. v.AddOptionalTagged(true, 2, placeOfBirth);
  178. if (null != gender)
  179. {
  180. v.Add(new DerTaggedObject(false, 3, new DerPrintableString(gender, true)));
  181. }
  182. v.AddOptionalTagged(true, 4, postalAddress);
  183. return new DerSequence(v);
  184. }
  185. }
  186. }
  187. #pragma warning restore
  188. #endif