SignerLocation.cs 4.3 KB

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