AuthenticatedData.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  7. {
  8. public class AuthenticatedData
  9. : Asn1Encodable
  10. {
  11. private DerInteger version;
  12. private OriginatorInfo originatorInfo;
  13. private Asn1Set recipientInfos;
  14. private AlgorithmIdentifier macAlgorithm;
  15. private AlgorithmIdentifier digestAlgorithm;
  16. private ContentInfo encapsulatedContentInfo;
  17. private Asn1Set authAttrs;
  18. private Asn1OctetString mac;
  19. private Asn1Set unauthAttrs;
  20. public AuthenticatedData(
  21. OriginatorInfo originatorInfo,
  22. Asn1Set recipientInfos,
  23. AlgorithmIdentifier macAlgorithm,
  24. AlgorithmIdentifier digestAlgorithm,
  25. ContentInfo encapsulatedContent,
  26. Asn1Set authAttrs,
  27. Asn1OctetString mac,
  28. Asn1Set unauthAttrs)
  29. {
  30. if (digestAlgorithm != null || authAttrs != null)
  31. {
  32. if (digestAlgorithm == null || authAttrs == null)
  33. {
  34. throw new ArgumentException("digestAlgorithm and authAttrs must be set together");
  35. }
  36. }
  37. version = new DerInteger(CalculateVersion(originatorInfo));
  38. this.originatorInfo = originatorInfo;
  39. this.macAlgorithm = macAlgorithm;
  40. this.digestAlgorithm = digestAlgorithm;
  41. this.recipientInfos = recipientInfos;
  42. this.encapsulatedContentInfo = encapsulatedContent;
  43. this.authAttrs = authAttrs;
  44. this.mac = mac;
  45. this.unauthAttrs = unauthAttrs;
  46. }
  47. private AuthenticatedData(
  48. Asn1Sequence seq)
  49. {
  50. int index = 0;
  51. version = (DerInteger)seq[index++];
  52. Asn1Encodable tmp = seq[index++];
  53. if (tmp is Asn1TaggedObject)
  54. {
  55. originatorInfo = OriginatorInfo.GetInstance((Asn1TaggedObject)tmp, false);
  56. tmp = seq[index++];
  57. }
  58. recipientInfos = Asn1Set.GetInstance(tmp);
  59. macAlgorithm = AlgorithmIdentifier.GetInstance(seq[index++]);
  60. tmp = seq[index++];
  61. if (tmp is Asn1TaggedObject)
  62. {
  63. digestAlgorithm = AlgorithmIdentifier.GetInstance((Asn1TaggedObject)tmp, false);
  64. tmp = seq[index++];
  65. }
  66. encapsulatedContentInfo = ContentInfo.GetInstance(tmp);
  67. tmp = seq[index++];
  68. if (tmp is Asn1TaggedObject)
  69. {
  70. authAttrs = Asn1Set.GetInstance((Asn1TaggedObject)tmp, false);
  71. tmp = seq[index++];
  72. }
  73. mac = Asn1OctetString.GetInstance(tmp);
  74. if (seq.Count > index)
  75. {
  76. unauthAttrs = Asn1Set.GetInstance((Asn1TaggedObject)seq[index], false);
  77. }
  78. }
  79. /**
  80. * return an AuthenticatedData object from a tagged object.
  81. *
  82. * @param obj the tagged object holding the object we want.
  83. * @param isExplicit true if the object is meant to be explicitly
  84. * tagged false otherwise.
  85. * @throws ArgumentException if the object held by the
  86. * tagged object cannot be converted.
  87. */
  88. public static AuthenticatedData GetInstance(
  89. Asn1TaggedObject obj,
  90. bool isExplicit)
  91. {
  92. return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
  93. }
  94. /**
  95. * return an AuthenticatedData object from the given object.
  96. *
  97. * @param obj the object we want converted.
  98. * @throws ArgumentException if the object cannot be converted.
  99. */
  100. public static AuthenticatedData GetInstance(
  101. object obj)
  102. {
  103. if (obj == null || obj is AuthenticatedData)
  104. {
  105. return (AuthenticatedData)obj;
  106. }
  107. if (obj is Asn1Sequence)
  108. {
  109. return new AuthenticatedData((Asn1Sequence)obj);
  110. }
  111. throw new ArgumentException("Invalid AuthenticatedData: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  112. }
  113. public DerInteger Version
  114. {
  115. get { return version; }
  116. }
  117. public OriginatorInfo OriginatorInfo
  118. {
  119. get { return originatorInfo; }
  120. }
  121. public Asn1Set RecipientInfos
  122. {
  123. get { return recipientInfos; }
  124. }
  125. public AlgorithmIdentifier MacAlgorithm
  126. {
  127. get { return macAlgorithm; }
  128. }
  129. public AlgorithmIdentifier DigestAlgorithm
  130. {
  131. get { return digestAlgorithm; }
  132. }
  133. public ContentInfo EncapsulatedContentInfo
  134. {
  135. get { return encapsulatedContentInfo; }
  136. }
  137. public Asn1Set AuthAttrs
  138. {
  139. get { return authAttrs; }
  140. }
  141. public Asn1OctetString Mac
  142. {
  143. get { return mac; }
  144. }
  145. public Asn1Set UnauthAttrs
  146. {
  147. get { return unauthAttrs; }
  148. }
  149. /**
  150. * Produce an object suitable for an Asn1OutputStream.
  151. * <pre>
  152. * AuthenticatedData ::= SEQUENCE {
  153. * version CMSVersion,
  154. * originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
  155. * recipientInfos RecipientInfos,
  156. * macAlgorithm MessageAuthenticationCodeAlgorithm,
  157. * digestAlgorithm [1] DigestAlgorithmIdentifier OPTIONAL,
  158. * encapContentInfo EncapsulatedContentInfo,
  159. * authAttrs [2] IMPLICIT AuthAttributes OPTIONAL,
  160. * mac MessageAuthenticationCode,
  161. * unauthAttrs [3] IMPLICIT UnauthAttributes OPTIONAL }
  162. *
  163. * AuthAttributes ::= SET SIZE (1..MAX) OF Attribute
  164. *
  165. * UnauthAttributes ::= SET SIZE (1..MAX) OF Attribute
  166. *
  167. * MessageAuthenticationCode ::= OCTET STRING
  168. * </pre>
  169. */
  170. public override Asn1Object ToAsn1Object()
  171. {
  172. Asn1EncodableVector v = new Asn1EncodableVector(version);
  173. v.AddOptionalTagged(false, 0, originatorInfo);
  174. v.Add(recipientInfos, macAlgorithm);
  175. v.AddOptionalTagged(false, 1, digestAlgorithm);
  176. v.Add(encapsulatedContentInfo);
  177. v.AddOptionalTagged(false, 2, authAttrs);
  178. v.Add(mac);
  179. v.AddOptionalTagged(false, 3, unauthAttrs);
  180. return new BerSequence(v);
  181. }
  182. public static int CalculateVersion(OriginatorInfo origInfo)
  183. {
  184. if (origInfo == null)
  185. return 0;
  186. int ver = 0;
  187. foreach (object obj in origInfo.Certificates)
  188. {
  189. if (obj is Asn1TaggedObject)
  190. {
  191. Asn1TaggedObject tag = (Asn1TaggedObject)obj;
  192. if (tag.TagNo == 2)
  193. {
  194. ver = 1;
  195. }
  196. else if (tag.TagNo == 3)
  197. {
  198. ver = 3;
  199. break;
  200. }
  201. }
  202. }
  203. foreach (object obj in origInfo.Crls)
  204. {
  205. if (obj is Asn1TaggedObject)
  206. {
  207. Asn1TaggedObject tag = (Asn1TaggedObject)obj;
  208. if (tag.TagNo == 1)
  209. {
  210. ver = 3;
  211. break;
  212. }
  213. }
  214. }
  215. return ver;
  216. }
  217. }
  218. }
  219. #pragma warning restore
  220. #endif