AuthEnvelopedData.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 AuthEnvelopedData
  8. : Asn1Encodable
  9. {
  10. private DerInteger version;
  11. private OriginatorInfo originatorInfo;
  12. private Asn1Set recipientInfos;
  13. private EncryptedContentInfo authEncryptedContentInfo;
  14. private Asn1Set authAttrs;
  15. private Asn1OctetString mac;
  16. private Asn1Set unauthAttrs;
  17. public AuthEnvelopedData(
  18. OriginatorInfo originatorInfo,
  19. Asn1Set recipientInfos,
  20. EncryptedContentInfo authEncryptedContentInfo,
  21. Asn1Set authAttrs,
  22. Asn1OctetString mac,
  23. Asn1Set unauthAttrs)
  24. {
  25. // "It MUST be set to 0."
  26. this.version = new DerInteger(0);
  27. this.originatorInfo = originatorInfo;
  28. // "There MUST be at least one element in the collection."
  29. this.recipientInfos = recipientInfos;
  30. if (this.recipientInfos.Count < 1)
  31. throw new ArgumentException("AuthEnvelopedData requires at least 1 RecipientInfo");
  32. this.authEncryptedContentInfo = authEncryptedContentInfo;
  33. // "The authAttrs MUST be present if the content type carried in
  34. // EncryptedContentInfo is not id-data."
  35. this.authAttrs = authAttrs;
  36. if (!authEncryptedContentInfo.ContentType.Equals(CmsObjectIdentifiers.Data))
  37. {
  38. if (authAttrs == null || authAttrs.Count < 1)
  39. throw new ArgumentException("authAttrs must be present with non-data content");
  40. }
  41. this.mac = mac;
  42. this.unauthAttrs = unauthAttrs;
  43. }
  44. private AuthEnvelopedData(
  45. Asn1Sequence seq)
  46. {
  47. int index = 0;
  48. // "It MUST be set to 0."
  49. Asn1Object tmp = seq[index++].ToAsn1Object();
  50. version = DerInteger.GetInstance(tmp);
  51. if (!version.HasValue(0))
  52. throw new ArgumentException("AuthEnvelopedData version number must be 0");
  53. tmp = seq[index++].ToAsn1Object();
  54. if (tmp is Asn1TaggedObject)
  55. {
  56. originatorInfo = OriginatorInfo.GetInstance((Asn1TaggedObject)tmp, false);
  57. tmp = seq[index++].ToAsn1Object();
  58. }
  59. // "There MUST be at least one element in the collection."
  60. recipientInfos = Asn1Set.GetInstance(tmp);
  61. if (recipientInfos.Count < 1)
  62. throw new ArgumentException("AuthEnvelopedData requires at least 1 RecipientInfo");
  63. tmp = seq[index++].ToAsn1Object();
  64. authEncryptedContentInfo = EncryptedContentInfo.GetInstance(tmp);
  65. tmp = seq[index++].ToAsn1Object();
  66. if (tmp is Asn1TaggedObject)
  67. {
  68. authAttrs = Asn1Set.GetInstance((Asn1TaggedObject)tmp, false);
  69. tmp = seq[index++].ToAsn1Object();
  70. }
  71. else
  72. {
  73. // "The authAttrs MUST be present if the content type carried in
  74. // EncryptedContentInfo is not id-data."
  75. if (!authEncryptedContentInfo.ContentType.Equals(CmsObjectIdentifiers.Data))
  76. {
  77. if (authAttrs == null || authAttrs.Count < 1)
  78. throw new ArgumentException("authAttrs must be present with non-data content");
  79. }
  80. }
  81. mac = Asn1OctetString.GetInstance(tmp);
  82. if (seq.Count > index)
  83. {
  84. tmp = seq[index++].ToAsn1Object();
  85. unauthAttrs = Asn1Set.GetInstance((Asn1TaggedObject)tmp, false);
  86. }
  87. }
  88. /**
  89. * return an AuthEnvelopedData object from a tagged object.
  90. *
  91. * @param obj the tagged object holding the object we want.
  92. * @param isExplicit true if the object is meant to be explicitly
  93. * tagged false otherwise.
  94. * @throws ArgumentException if the object held by the
  95. * tagged object cannot be converted.
  96. */
  97. public static AuthEnvelopedData GetInstance(
  98. Asn1TaggedObject obj,
  99. bool isExplicit)
  100. {
  101. return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
  102. }
  103. /**
  104. * return an AuthEnvelopedData object from the given object.
  105. *
  106. * @param obj the object we want converted.
  107. * @throws ArgumentException if the object cannot be converted.
  108. */
  109. public static AuthEnvelopedData GetInstance(
  110. object obj)
  111. {
  112. if (obj == null || obj is AuthEnvelopedData)
  113. return (AuthEnvelopedData)obj;
  114. if (obj is Asn1Sequence)
  115. return new AuthEnvelopedData((Asn1Sequence)obj);
  116. throw new ArgumentException("Invalid AuthEnvelopedData: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  117. }
  118. public DerInteger Version
  119. {
  120. get { return version; }
  121. }
  122. public OriginatorInfo OriginatorInfo
  123. {
  124. get { return originatorInfo; }
  125. }
  126. public Asn1Set RecipientInfos
  127. {
  128. get { return recipientInfos; }
  129. }
  130. public EncryptedContentInfo AuthEncryptedContentInfo
  131. {
  132. get { return authEncryptedContentInfo; }
  133. }
  134. public Asn1Set AuthAttrs
  135. {
  136. get { return authAttrs; }
  137. }
  138. public Asn1OctetString Mac
  139. {
  140. get { return mac; }
  141. }
  142. public Asn1Set UnauthAttrs
  143. {
  144. get { return unauthAttrs; }
  145. }
  146. /**
  147. * Produce an object suitable for an Asn1OutputStream.
  148. * <pre>
  149. * AuthEnvelopedData ::= SEQUENCE {
  150. * version CMSVersion,
  151. * originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
  152. * recipientInfos RecipientInfos,
  153. * authEncryptedContentInfo EncryptedContentInfo,
  154. * authAttrs [1] IMPLICIT AuthAttributes OPTIONAL,
  155. * mac MessageAuthenticationCode,
  156. * unauthAttrs [2] IMPLICIT UnauthAttributes OPTIONAL }
  157. * </pre>
  158. */
  159. public override Asn1Object ToAsn1Object()
  160. {
  161. Asn1EncodableVector v = new Asn1EncodableVector(version);
  162. v.AddOptionalTagged(false, 0, originatorInfo);
  163. v.Add(recipientInfos, authEncryptedContentInfo);
  164. // "authAttrs optionally contains the authenticated attributes."
  165. // "AuthAttributes MUST be DER encoded, even if the rest of the
  166. // AuthEnvelopedData structure is BER encoded."
  167. v.AddOptionalTagged(false, 1, authAttrs);
  168. v.Add(mac);
  169. // "unauthAttrs optionally contains the unauthenticated attributes."
  170. v.AddOptionalTagged(false, 2, unauthAttrs);
  171. return new BerSequence(v);
  172. }
  173. }
  174. }
  175. #pragma warning restore
  176. #endif