SignerLocation.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. public DerUtf8String CountryName
  104. {
  105. get { return countryName == null ? null : new DerUtf8String(countryName.GetString()); }
  106. }
  107. public DerUtf8String LocalityName
  108. {
  109. get { return localityName == null ? null : new DerUtf8String(localityName.GetString()); }
  110. }
  111. public Asn1Sequence PostalAddress
  112. {
  113. get { return postalAddress; }
  114. }
  115. /**
  116. * <pre>
  117. * SignerLocation ::= SEQUENCE {
  118. * countryName [0] DirectoryString OPTIONAL,
  119. * localityName [1] DirectoryString OPTIONAL,
  120. * postalAddress [2] PostalAddress OPTIONAL }
  121. *
  122. * PostalAddress ::= SEQUENCE SIZE(1..6) OF DirectoryString
  123. *
  124. * DirectoryString ::= CHOICE {
  125. * teletexString TeletexString (SIZE (1..MAX)),
  126. * printableString PrintableString (SIZE (1..MAX)),
  127. * universalString UniversalString (SIZE (1..MAX)),
  128. * utf8String UTF8String (SIZE (1.. MAX)),
  129. * bmpString BMPString (SIZE (1..MAX)) }
  130. * </pre>
  131. */
  132. public override Asn1Object ToAsn1Object()
  133. {
  134. Asn1EncodableVector v = new Asn1EncodableVector();
  135. v.AddOptionalTagged(true, 0, countryName);
  136. v.AddOptionalTagged(true, 1, localityName);
  137. v.AddOptionalTagged(true, 2, postalAddress);
  138. return new DerSequence(v);
  139. }
  140. }
  141. }
  142. #pragma warning restore
  143. #endif