RecipientInfo.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.Cms
  6. {
  7. public class RecipientInfo
  8. : Asn1Encodable, IAsn1Choice
  9. {
  10. internal Asn1Encodable info;
  11. public RecipientInfo(
  12. KeyTransRecipientInfo info)
  13. {
  14. this.info = info;
  15. }
  16. public RecipientInfo(
  17. KeyAgreeRecipientInfo info)
  18. {
  19. this.info = new DerTaggedObject(false, 1, info);
  20. }
  21. public RecipientInfo(
  22. KekRecipientInfo info)
  23. {
  24. this.info = new DerTaggedObject(false, 2, info);
  25. }
  26. public RecipientInfo(
  27. PasswordRecipientInfo info)
  28. {
  29. this.info = new DerTaggedObject(false, 3, info);
  30. }
  31. public RecipientInfo(
  32. OtherRecipientInfo info)
  33. {
  34. this.info = new DerTaggedObject(false, 4, info);
  35. }
  36. public RecipientInfo(
  37. Asn1Object info)
  38. {
  39. this.info = info;
  40. }
  41. public static RecipientInfo GetInstance(
  42. object o)
  43. {
  44. if (o == null || o is RecipientInfo)
  45. return (RecipientInfo) o;
  46. if (o is Asn1Sequence)
  47. return new RecipientInfo((Asn1Sequence) o);
  48. if (o is Asn1TaggedObject)
  49. return new RecipientInfo((Asn1TaggedObject) o);
  50. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(o));
  51. }
  52. public DerInteger Version
  53. {
  54. get
  55. {
  56. if (info is Asn1TaggedObject)
  57. {
  58. Asn1TaggedObject o = (Asn1TaggedObject) info;
  59. switch (o.TagNo)
  60. {
  61. case 1:
  62. return KeyAgreeRecipientInfo.GetInstance(o, false).Version;
  63. case 2:
  64. return GetKekInfo(o).Version;
  65. case 3:
  66. return PasswordRecipientInfo.GetInstance(o, false).Version;
  67. case 4:
  68. return new DerInteger(0); // no syntax version for OtherRecipientInfo
  69. default:
  70. throw new InvalidOperationException("unknown tag");
  71. }
  72. }
  73. return KeyTransRecipientInfo.GetInstance(info).Version;
  74. }
  75. }
  76. public bool IsTagged
  77. {
  78. get { return info is Asn1TaggedObject; }
  79. }
  80. public Asn1Encodable Info
  81. {
  82. get
  83. {
  84. if (info is Asn1TaggedObject)
  85. {
  86. Asn1TaggedObject o = (Asn1TaggedObject) info;
  87. switch (o.TagNo)
  88. {
  89. case 1:
  90. return KeyAgreeRecipientInfo.GetInstance(o, false);
  91. case 2:
  92. return GetKekInfo(o);
  93. case 3:
  94. return PasswordRecipientInfo.GetInstance(o, false);
  95. case 4:
  96. return OtherRecipientInfo.GetInstance(o, false);
  97. default:
  98. throw new InvalidOperationException("unknown tag");
  99. }
  100. }
  101. return KeyTransRecipientInfo.GetInstance(info);
  102. }
  103. }
  104. private KekRecipientInfo GetKekInfo(
  105. Asn1TaggedObject o)
  106. {
  107. // For compatibility with erroneous version, we don't always pass 'false' here
  108. return KekRecipientInfo.GetInstance(o, o.IsExplicit());
  109. }
  110. /**
  111. * Produce an object suitable for an Asn1OutputStream.
  112. * <pre>
  113. * RecipientInfo ::= CHOICE {
  114. * ktri KeyTransRecipientInfo,
  115. * kari [1] KeyAgreeRecipientInfo,
  116. * kekri [2] KekRecipientInfo,
  117. * pwri [3] PasswordRecipientInfo,
  118. * ori [4] OtherRecipientInfo }
  119. * </pre>
  120. */
  121. public override Asn1Object ToAsn1Object()
  122. {
  123. return info.ToAsn1Object();
  124. }
  125. }
  126. }
  127. #pragma warning restore
  128. #endif