PersonalData.cs 5.4 KB

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