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