SignerLocation.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Esf
  7. {
  8. /**
  9. * Signer-Location attribute (RFC3126).
  10. *
  11. * <pre>
  12. * SignerLocation ::= SEQUENCE {
  13. * countryName [0] DirectoryString OPTIONAL,
  14. * localityName [1] DirectoryString OPTIONAL,
  15. * postalAddress [2] PostalAddress OPTIONAL }
  16. *
  17. * PostalAddress ::= SEQUENCE SIZE(1..6) OF DirectoryString
  18. * </pre>
  19. */
  20. public class SignerLocation
  21. : Asn1Encodable
  22. {
  23. private DirectoryString countryName;
  24. private DirectoryString localityName;
  25. private Asn1Sequence postalAddress;
  26. public SignerLocation(
  27. Asn1Sequence seq)
  28. {
  29. foreach (Asn1TaggedObject obj in seq)
  30. {
  31. switch (obj.TagNo)
  32. {
  33. case 0:
  34. this.countryName = DirectoryString.GetInstance(obj, true);
  35. break;
  36. case 1:
  37. this.localityName = DirectoryString.GetInstance(obj, true);
  38. break;
  39. case 2:
  40. bool isExplicit = obj.IsExplicit(); // handle erroneous implicitly tagged sequences
  41. this.postalAddress = Asn1Sequence.GetInstance(obj, isExplicit);
  42. if (postalAddress != null && postalAddress.Count > 6)
  43. throw new ArgumentException("postal address must contain less than 6 strings");
  44. break;
  45. default:
  46. throw new ArgumentException("illegal tag");
  47. }
  48. }
  49. }
  50. private SignerLocation(
  51. DirectoryString countryName,
  52. DirectoryString localityName,
  53. Asn1Sequence postalAddress)
  54. {
  55. if (postalAddress != null && postalAddress.Count > 6)
  56. throw new ArgumentException("postal address must contain less than 6 strings");
  57. this.countryName = countryName;
  58. this.localityName = localityName;
  59. this.postalAddress = postalAddress;
  60. }
  61. public SignerLocation(
  62. DirectoryString countryName,
  63. DirectoryString localityName,
  64. DirectoryString[] postalAddress)
  65. : this(countryName, localityName, new DerSequence(postalAddress))
  66. {
  67. }
  68. public SignerLocation(
  69. DerUtf8String countryName,
  70. DerUtf8String localityName,
  71. Asn1Sequence postalAddress)
  72. : this(DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), postalAddress)
  73. {
  74. }
  75. public static SignerLocation GetInstance(
  76. object obj)
  77. {
  78. if (obj == null || obj is SignerLocation)
  79. {
  80. return (SignerLocation) obj;
  81. }
  82. return new SignerLocation(Asn1Sequence.GetInstance(obj));
  83. }
  84. public DirectoryString Country
  85. {
  86. get { return countryName; }
  87. }
  88. public DirectoryString Locality
  89. {
  90. get { return localityName; }
  91. }
  92. public DirectoryString[] GetPostal()
  93. {
  94. if (postalAddress == null)
  95. return null;
  96. DirectoryString[] dirStrings = new DirectoryString[postalAddress.Count];
  97. for (int i = 0; i != dirStrings.Length; i++)
  98. {
  99. dirStrings[i] = DirectoryString.GetInstance(postalAddress[i]);
  100. }
  101. return dirStrings;
  102. }
  103. [Obsolete("Use 'Country' property instead")]
  104. public DerUtf8String CountryName
  105. {
  106. get { return countryName == null ? null : new DerUtf8String(countryName.GetString()); }
  107. }
  108. [Obsolete("Use 'Locality' property instead")]
  109. public DerUtf8String LocalityName
  110. {
  111. get { return localityName == null ? null : new DerUtf8String(localityName.GetString()); }
  112. }
  113. public Asn1Sequence PostalAddress
  114. {
  115. get { return postalAddress; }
  116. }
  117. /**
  118. * <pre>
  119. * SignerLocation ::= SEQUENCE {
  120. * countryName [0] DirectoryString OPTIONAL,
  121. * localityName [1] DirectoryString OPTIONAL,
  122. * postalAddress [2] PostalAddress OPTIONAL }
  123. *
  124. * PostalAddress ::= SEQUENCE SIZE(1..6) OF DirectoryString
  125. *
  126. * DirectoryString ::= CHOICE {
  127. * teletexString TeletexString (SIZE (1..MAX)),
  128. * printableString PrintableString (SIZE (1..MAX)),
  129. * universalString UniversalString (SIZE (1..MAX)),
  130. * utf8String UTF8String (SIZE (1.. MAX)),
  131. * bmpString BMPString (SIZE (1..MAX)) }
  132. * </pre>
  133. */
  134. public override Asn1Object ToAsn1Object()
  135. {
  136. Asn1EncodableVector v = new Asn1EncodableVector();
  137. if (countryName != null)
  138. {
  139. v.Add(new DerTaggedObject(true, 0, countryName));
  140. }
  141. if (localityName != null)
  142. {
  143. v.Add(new DerTaggedObject(true, 1, localityName));
  144. }
  145. if (postalAddress != null)
  146. {
  147. v.Add(new DerTaggedObject(true, 2, postalAddress));
  148. }
  149. return new DerSequence(v);
  150. }
  151. }
  152. }
  153. #pragma warning restore
  154. #endif