EnvelopedData.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  7. {
  8. public class EnvelopedData
  9. : Asn1Encodable
  10. {
  11. private DerInteger version;
  12. private OriginatorInfo originatorInfo;
  13. private Asn1Set recipientInfos;
  14. private EncryptedContentInfo encryptedContentInfo;
  15. private Asn1Set unprotectedAttrs;
  16. public EnvelopedData(
  17. OriginatorInfo originatorInfo,
  18. Asn1Set recipientInfos,
  19. EncryptedContentInfo encryptedContentInfo,
  20. Asn1Set unprotectedAttrs)
  21. {
  22. this.version = new DerInteger(CalculateVersion(originatorInfo, recipientInfos, unprotectedAttrs));
  23. this.originatorInfo = originatorInfo;
  24. this.recipientInfos = recipientInfos;
  25. this.encryptedContentInfo = encryptedContentInfo;
  26. this.unprotectedAttrs = unprotectedAttrs;
  27. }
  28. public EnvelopedData(
  29. OriginatorInfo originatorInfo,
  30. Asn1Set recipientInfos,
  31. EncryptedContentInfo encryptedContentInfo,
  32. Attributes unprotectedAttrs)
  33. {
  34. this.version = new DerInteger(CalculateVersion(originatorInfo, recipientInfos, Asn1Set.GetInstance(unprotectedAttrs)));
  35. this.originatorInfo = originatorInfo;
  36. this.recipientInfos = recipientInfos;
  37. this.encryptedContentInfo = encryptedContentInfo;
  38. this.unprotectedAttrs = Asn1Set.GetInstance(unprotectedAttrs);
  39. }
  40. [Obsolete("Use 'GetInstance' instead")]
  41. public EnvelopedData(
  42. Asn1Sequence seq)
  43. {
  44. int index = 0;
  45. version = (DerInteger) seq[index++];
  46. object tmp = seq[index++];
  47. if (tmp is Asn1TaggedObject)
  48. {
  49. originatorInfo = OriginatorInfo.GetInstance((Asn1TaggedObject) tmp, false);
  50. tmp = seq[index++];
  51. }
  52. recipientInfos = Asn1Set.GetInstance(tmp);
  53. encryptedContentInfo = EncryptedContentInfo.GetInstance(seq[index++]);
  54. if (seq.Count > index)
  55. {
  56. unprotectedAttrs = Asn1Set.GetInstance((Asn1TaggedObject) seq[index], false);
  57. }
  58. }
  59. /**
  60. * return an EnvelopedData object from a tagged object.
  61. *
  62. * @param obj the tagged object holding the object we want.
  63. * @param explicitly true if the object is meant to be explicitly
  64. * tagged false otherwise.
  65. * @exception ArgumentException if the object held by the
  66. * tagged object cannot be converted.
  67. */
  68. public static EnvelopedData GetInstance(
  69. Asn1TaggedObject obj,
  70. bool explicitly)
  71. {
  72. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  73. }
  74. /**
  75. * return an EnvelopedData object from the given object.
  76. *
  77. * @param obj the object we want converted.
  78. * @exception ArgumentException if the object cannot be converted.
  79. */
  80. public static EnvelopedData GetInstance(
  81. object obj)
  82. {
  83. if (obj is EnvelopedData)
  84. return (EnvelopedData)obj;
  85. if (obj == null)
  86. return null;
  87. return new EnvelopedData(Asn1Sequence.GetInstance(obj));
  88. }
  89. public DerInteger Version
  90. {
  91. get { return version; }
  92. }
  93. public OriginatorInfo OriginatorInfo
  94. {
  95. get { return originatorInfo; }
  96. }
  97. public Asn1Set RecipientInfos
  98. {
  99. get { return recipientInfos; }
  100. }
  101. public EncryptedContentInfo EncryptedContentInfo
  102. {
  103. get { return encryptedContentInfo; }
  104. }
  105. public Asn1Set UnprotectedAttrs
  106. {
  107. get { return unprotectedAttrs; }
  108. }
  109. /**
  110. * Produce an object suitable for an Asn1OutputStream.
  111. * <pre>
  112. * EnvelopedData ::= Sequence {
  113. * version CMSVersion,
  114. * originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
  115. * recipientInfos RecipientInfos,
  116. * encryptedContentInfo EncryptedContentInfo,
  117. * unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL
  118. * }
  119. * </pre>
  120. */
  121. public override Asn1Object ToAsn1Object()
  122. {
  123. Asn1EncodableVector v = new Asn1EncodableVector(version);
  124. if (originatorInfo != null)
  125. {
  126. v.Add(new DerTaggedObject(false, 0, originatorInfo));
  127. }
  128. v.Add(recipientInfos, encryptedContentInfo);
  129. if (unprotectedAttrs != null)
  130. {
  131. v.Add(new DerTaggedObject(false, 1, unprotectedAttrs));
  132. }
  133. return new BerSequence(v);
  134. }
  135. public static int CalculateVersion(OriginatorInfo originatorInfo, Asn1Set recipientInfos, Asn1Set unprotectedAttrs)
  136. {
  137. if (originatorInfo != null || unprotectedAttrs != null)
  138. {
  139. return 2;
  140. }
  141. foreach (object o in recipientInfos)
  142. {
  143. RecipientInfo ri = RecipientInfo.GetInstance(o);
  144. if (ri.Version.Value.IntValue != 0)
  145. {
  146. return 2;
  147. }
  148. }
  149. return 0;
  150. }
  151. }
  152. }
  153. #pragma warning restore
  154. #endif