Target.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  6. {
  7. /**
  8. * Target structure used in target information extension for attribute
  9. * certificates from RFC 3281.
  10. *
  11. * <pre>
  12. * Target ::= CHOICE {
  13. * targetName [0] GeneralName,
  14. * targetGroup [1] GeneralName,
  15. * targetCert [2] TargetCert
  16. * }
  17. * </pre>
  18. *
  19. * <p>
  20. * The targetCert field is currently not supported and must not be used
  21. * according to RFC 3281.</p>
  22. */
  23. public class Target
  24. : Asn1Encodable, IAsn1Choice
  25. {
  26. public enum Choice
  27. {
  28. Name = 0,
  29. Group = 1
  30. };
  31. private readonly GeneralName targetName;
  32. private readonly GeneralName targetGroup;
  33. /**
  34. * Creates an instance of a Target from the given object.
  35. * <p>
  36. * <code>obj</code> can be a Target or a {@link Asn1TaggedObject}</p>
  37. *
  38. * @param obj The object.
  39. * @return A Target instance.
  40. * @throws ArgumentException if the given object cannot be
  41. * interpreted as Target.
  42. */
  43. public static Target GetInstance(
  44. object obj)
  45. {
  46. if (obj is Target)
  47. {
  48. return (Target) obj;
  49. }
  50. if (obj is Asn1TaggedObject)
  51. {
  52. return new Target((Asn1TaggedObject) obj);
  53. }
  54. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  55. }
  56. /**
  57. * Constructor from Asn1TaggedObject.
  58. *
  59. * @param tagObj The tagged object.
  60. * @throws ArgumentException if the encoding is wrong.
  61. */
  62. private Target(
  63. Asn1TaggedObject tagObj)
  64. {
  65. switch ((Choice) tagObj.TagNo)
  66. {
  67. case Choice.Name: // GeneralName is already a choice so explicit
  68. targetName = GeneralName.GetInstance(tagObj, true);
  69. break;
  70. case Choice.Group:
  71. targetGroup = GeneralName.GetInstance(tagObj, true);
  72. break;
  73. default:
  74. throw new ArgumentException("unknown tag: " + tagObj.TagNo);
  75. }
  76. }
  77. /**
  78. * Constructor from given details.
  79. * <p>
  80. * Exactly one of the parameters must be not <code>null</code>.</p>
  81. *
  82. * @param type the choice type to apply to the name.
  83. * @param name the general name.
  84. * @throws ArgumentException if type is invalid.
  85. */
  86. public Target(
  87. Choice type,
  88. GeneralName name)
  89. : this(new DerTaggedObject((int) type, name))
  90. {
  91. }
  92. /**
  93. * @return Returns the targetGroup.
  94. */
  95. public virtual GeneralName TargetGroup
  96. {
  97. get { return targetGroup; }
  98. }
  99. /**
  100. * @return Returns the targetName.
  101. */
  102. public virtual GeneralName TargetName
  103. {
  104. get { return targetName; }
  105. }
  106. /**
  107. * Produce an object suitable for an Asn1OutputStream.
  108. *
  109. * Returns:
  110. *
  111. * <pre>
  112. * Target ::= CHOICE {
  113. * targetName [0] GeneralName,
  114. * targetGroup [1] GeneralName,
  115. * targetCert [2] TargetCert
  116. * }
  117. * </pre>
  118. *
  119. * @return an Asn1Object
  120. */
  121. public override Asn1Object ToAsn1Object()
  122. {
  123. // GeneralName is a choice already so most be explicitly tagged
  124. if (targetName != null)
  125. {
  126. return new DerTaggedObject(true, 0, targetName);
  127. }
  128. return new DerTaggedObject(true, 1, targetGroup);
  129. }
  130. }
  131. }
  132. #pragma warning restore
  133. #endif