EnvelopedData.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. public EnvelopedData(
  41. Asn1Sequence seq)
  42. {
  43. int index = 0;
  44. version = (DerInteger) seq[index++];
  45. object tmp = seq[index++];
  46. if (tmp is Asn1TaggedObject)
  47. {
  48. originatorInfo = OriginatorInfo.GetInstance((Asn1TaggedObject) tmp, false);
  49. tmp = seq[index++];
  50. }
  51. recipientInfos = Asn1Set.GetInstance(tmp);
  52. encryptedContentInfo = EncryptedContentInfo.GetInstance(seq[index++]);
  53. if (seq.Count > index)
  54. {
  55. unprotectedAttrs = Asn1Set.GetInstance((Asn1TaggedObject) seq[index], false);
  56. }
  57. }
  58. /**
  59. * return an EnvelopedData object from a tagged object.
  60. *
  61. * @param obj the tagged object holding the object we want.
  62. * @param explicitly true if the object is meant to be explicitly
  63. * tagged false otherwise.
  64. * @exception ArgumentException if the object held by the
  65. * tagged object cannot be converted.
  66. */
  67. public static EnvelopedData GetInstance(
  68. Asn1TaggedObject obj,
  69. bool explicitly)
  70. {
  71. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  72. }
  73. /**
  74. * return an EnvelopedData object from the given object.
  75. *
  76. * @param obj the object we want converted.
  77. * @exception ArgumentException if the object cannot be converted.
  78. */
  79. public static EnvelopedData GetInstance(
  80. object obj)
  81. {
  82. if (obj is EnvelopedData)
  83. return (EnvelopedData)obj;
  84. if (obj == null)
  85. return null;
  86. return new EnvelopedData(Asn1Sequence.GetInstance(obj));
  87. }
  88. public DerInteger Version
  89. {
  90. get { return version; }
  91. }
  92. public OriginatorInfo OriginatorInfo
  93. {
  94. get { return originatorInfo; }
  95. }
  96. public Asn1Set RecipientInfos
  97. {
  98. get { return recipientInfos; }
  99. }
  100. public EncryptedContentInfo EncryptedContentInfo
  101. {
  102. get { return encryptedContentInfo; }
  103. }
  104. public Asn1Set UnprotectedAttrs
  105. {
  106. get { return unprotectedAttrs; }
  107. }
  108. /**
  109. * Produce an object suitable for an Asn1OutputStream.
  110. * <pre>
  111. * EnvelopedData ::= Sequence {
  112. * version CMSVersion,
  113. * originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
  114. * recipientInfos RecipientInfos,
  115. * encryptedContentInfo EncryptedContentInfo,
  116. * unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL
  117. * }
  118. * </pre>
  119. */
  120. public override Asn1Object ToAsn1Object()
  121. {
  122. Asn1EncodableVector v = new Asn1EncodableVector(version);
  123. v.AddOptionalTagged(false, 0, originatorInfo);
  124. v.Add(recipientInfos, encryptedContentInfo);
  125. v.AddOptionalTagged(false, 1, unprotectedAttrs);
  126. return new BerSequence(v);
  127. }
  128. public static int CalculateVersion(OriginatorInfo originatorInfo, Asn1Set recipientInfos, Asn1Set unprotectedAttrs)
  129. {
  130. if (originatorInfo != null || unprotectedAttrs != null)
  131. {
  132. return 2;
  133. }
  134. foreach (object o in recipientInfos)
  135. {
  136. RecipientInfo ri = RecipientInfo.GetInstance(o);
  137. if (!ri.Version.HasValue(0))
  138. {
  139. return 2;
  140. }
  141. }
  142. return 0;
  143. }
  144. }
  145. }
  146. #pragma warning restore
  147. #endif