OriginatorIdentifierOrKey.cs 3.9 KB

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