OriginatorIdentifierOrKey.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  7. {
  8. public class OriginatorIdentifierOrKey
  9. : Asn1Encodable, IAsn1Choice
  10. {
  11. private Asn1Encodable id;
  12. public OriginatorIdentifierOrKey(
  13. IssuerAndSerialNumber id)
  14. {
  15. this.id = id;
  16. }
  17. public OriginatorIdentifierOrKey(
  18. Asn1OctetString id)
  19. : this(new SubjectKeyIdentifier(id))
  20. {
  21. }
  22. public OriginatorIdentifierOrKey(
  23. SubjectKeyIdentifier id)
  24. {
  25. this.id = new DerTaggedObject(false, 0, id);
  26. }
  27. public OriginatorIdentifierOrKey(
  28. OriginatorPublicKey id)
  29. {
  30. this.id = new DerTaggedObject(false, 1, id);
  31. }
  32. public OriginatorIdentifierOrKey(
  33. Asn1Object id)
  34. {
  35. this.id = id;
  36. }
  37. private OriginatorIdentifierOrKey(
  38. Asn1TaggedObject id)
  39. {
  40. // TODO Add validation
  41. this.id = id;
  42. }
  43. /**
  44. * return an OriginatorIdentifierOrKey object from a tagged object.
  45. *
  46. * @param o the tagged object holding the object we want.
  47. * @param explicitly true if the object is meant to be explicitly
  48. * tagged false otherwise.
  49. * @exception ArgumentException if the object held by the
  50. * tagged object cannot be converted.
  51. */
  52. public static OriginatorIdentifierOrKey GetInstance(
  53. Asn1TaggedObject o,
  54. bool explicitly)
  55. {
  56. if (!explicitly)
  57. {
  58. throw new ArgumentException(
  59. "Can't implicitly tag OriginatorIdentifierOrKey");
  60. }
  61. return GetInstance(o.GetObject());
  62. }
  63. /**
  64. * return an OriginatorIdentifierOrKey object from the given object.
  65. *
  66. * @param o the object we want converted.
  67. * @exception ArgumentException if the object cannot be converted.
  68. */
  69. public static OriginatorIdentifierOrKey GetInstance(
  70. object o)
  71. {
  72. if (o == null || o is OriginatorIdentifierOrKey)
  73. return (OriginatorIdentifierOrKey)o;
  74. if (o is IssuerAndSerialNumber)
  75. return new OriginatorIdentifierOrKey((IssuerAndSerialNumber)o);
  76. if (o is SubjectKeyIdentifier)
  77. return new OriginatorIdentifierOrKey((SubjectKeyIdentifier)o);
  78. if (o is OriginatorPublicKey)
  79. return new OriginatorIdentifierOrKey((OriginatorPublicKey)o);
  80. if (o is Asn1TaggedObject)
  81. return new OriginatorIdentifierOrKey((Asn1TaggedObject)o);
  82. throw new ArgumentException("Invalid OriginatorIdentifierOrKey: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(o));
  83. }
  84. public Asn1Encodable ID
  85. {
  86. get { return id; }
  87. }
  88. public IssuerAndSerialNumber IssuerAndSerialNumber
  89. {
  90. get
  91. {
  92. if (id is IssuerAndSerialNumber)
  93. {
  94. return (IssuerAndSerialNumber)id;
  95. }
  96. return null;
  97. }
  98. }
  99. public SubjectKeyIdentifier SubjectKeyIdentifier
  100. {
  101. get
  102. {
  103. if (id is Asn1TaggedObject && ((Asn1TaggedObject)id).TagNo == 0)
  104. {
  105. return SubjectKeyIdentifier.GetInstance((Asn1TaggedObject)id, false);
  106. }
  107. return null;
  108. }
  109. }
  110. public OriginatorPublicKey OriginatorKey
  111. {
  112. get { return OriginatorPublicKey; }
  113. }
  114. public OriginatorPublicKey OriginatorPublicKey
  115. {
  116. get
  117. {
  118. if (id is Asn1TaggedObject && ((Asn1TaggedObject)id).TagNo == 1)
  119. {
  120. return OriginatorPublicKey.GetInstance((Asn1TaggedObject)id, false);
  121. }
  122. return null;
  123. }
  124. }
  125. /**
  126. * Produce an object suitable for an Asn1OutputStream.
  127. * <pre>
  128. * OriginatorIdentifierOrKey ::= CHOICE {
  129. * issuerAndSerialNumber IssuerAndSerialNumber,
  130. * subjectKeyIdentifier [0] SubjectKeyIdentifier,
  131. * originatorKey [1] OriginatorPublicKey
  132. * }
  133. *
  134. * SubjectKeyIdentifier ::= OCTET STRING
  135. * </pre>
  136. */
  137. public override Asn1Object ToAsn1Object()
  138. {
  139. return id.ToAsn1Object();
  140. }
  141. }
  142. }
  143. #pragma warning restore
  144. #endif