EnvelopedData.cs 5.3 KB

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