CMSAuthenticatedDataParser.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  10. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  11. {
  12. /**
  13. * Parsing class for an CMS Authenticated Data object from an input stream.
  14. * <p>
  15. * Note: that because we are in a streaming mode only one recipient can be tried and it is important
  16. * that the methods on the parser are called in the appropriate order.
  17. * </p>
  18. * <p>
  19. * Example of use - assuming the first recipient matches the private key we have.
  20. * <pre>
  21. * CMSAuthenticatedDataParser ad = new CMSAuthenticatedDataParser(inputStream);
  22. *
  23. * RecipientInformationStore recipients = ad.getRecipientInfos();
  24. *
  25. * Collection c = recipients.getRecipients();
  26. * Iterator it = c.iterator();
  27. *
  28. * if (it.hasNext())
  29. * {
  30. * RecipientInformation recipient = (RecipientInformation)it.next();
  31. *
  32. * CMSTypedStream recData = recipient.getContentStream(privateKey, "BC");
  33. *
  34. * processDataStream(recData.getContentStream());
  35. *
  36. * if (!Arrays.equals(ad.getMac(), recipient.getMac())
  37. * {
  38. * System.err.println("Data corrupted!!!!");
  39. * }
  40. * }
  41. * </pre>
  42. * Note: this class does not introduce buffering - if you are processing large files you should create
  43. * the parser with:
  44. * <pre>
  45. * CMSAuthenticatedDataParser ep = new CMSAuthenticatedDataParser(new BufferedInputStream(inputStream, bufSize));
  46. * </pre>
  47. * where bufSize is a suitably large buffer size.
  48. * </p>
  49. */
  50. public class CmsAuthenticatedDataParser
  51. : CmsContentInfoParser
  52. {
  53. internal RecipientInformationStore _recipientInfoStore;
  54. internal AuthenticatedDataParser authData;
  55. private AlgorithmIdentifier macAlg;
  56. private byte[] mac;
  57. private Asn1.Cms.AttributeTable authAttrs;
  58. private Asn1.Cms.AttributeTable unauthAttrs;
  59. private bool authAttrNotRead;
  60. private bool unauthAttrNotRead;
  61. public CmsAuthenticatedDataParser(
  62. byte[] envelopedData)
  63. : this(new MemoryStream(envelopedData, false))
  64. {
  65. }
  66. public CmsAuthenticatedDataParser(
  67. Stream envelopedData)
  68. : base(envelopedData)
  69. {
  70. this.authAttrNotRead = true;
  71. this.authData = new AuthenticatedDataParser(
  72. (Asn1SequenceParser)contentInfo.GetContent(Asn1Tags.Sequence));
  73. // TODO Validate version?
  74. //DerInteger version = this.authData.getVersion();
  75. //
  76. // read the recipients
  77. //
  78. Asn1Set recipientInfos = Asn1Set.GetInstance(authData.GetRecipientInfos().ToAsn1Object());
  79. this.macAlg = authData.GetMacAlgorithm();
  80. //
  81. // read the authenticated content info
  82. //
  83. ContentInfoParser data = authData.GetEnapsulatedContentInfo();
  84. CmsReadable readable = new CmsProcessableInputStream(
  85. ((Asn1OctetStringParser)data.GetContent(Asn1Tags.OctetString)).GetOctetStream());
  86. CmsSecureReadable secureReadable = new CmsEnvelopedHelper.CmsAuthenticatedSecureReadable(
  87. this.macAlg, readable);
  88. //
  89. // build the RecipientInformationStore
  90. //
  91. this._recipientInfoStore = CmsEnvelopedHelper.BuildRecipientInformationStore(
  92. recipientInfos, secureReadable);
  93. }
  94. public AlgorithmIdentifier MacAlgorithmID
  95. {
  96. get { return macAlg; }
  97. }
  98. /**
  99. * return the object identifier for the mac algorithm.
  100. */
  101. public string MacAlgOid
  102. {
  103. get { return macAlg.Algorithm.Id; }
  104. }
  105. /**
  106. * return the ASN.1 encoded encryption algorithm parameters, or null if
  107. * there aren't any.
  108. */
  109. public Asn1Object MacAlgParams
  110. {
  111. get
  112. {
  113. Asn1Encodable ae = macAlg.Parameters;
  114. return ae == null ? null : ae.ToAsn1Object();
  115. }
  116. }
  117. /**
  118. * return a store of the intended recipients for this message
  119. */
  120. public RecipientInformationStore GetRecipientInfos()
  121. {
  122. return _recipientInfoStore;
  123. }
  124. public byte[] GetMac()
  125. {
  126. if (mac == null)
  127. {
  128. GetAuthAttrs();
  129. mac = authData.GetMac().GetOctets();
  130. }
  131. return Arrays.Clone(mac);
  132. }
  133. /**
  134. * return a table of the unauthenticated attributes indexed by
  135. * the OID of the attribute.
  136. * @exception java.io.IOException
  137. */
  138. public Asn1.Cms.AttributeTable GetAuthAttrs()
  139. {
  140. if (authAttrs == null && authAttrNotRead)
  141. {
  142. Asn1SetParser s = authData.GetAuthAttrs();
  143. authAttrNotRead = false;
  144. if (s != null)
  145. {
  146. Asn1EncodableVector v = new Asn1EncodableVector();
  147. IAsn1Convertible o;
  148. while ((o = s.ReadObject()) != null)
  149. {
  150. Asn1SequenceParser seq = (Asn1SequenceParser)o;
  151. v.Add(seq.ToAsn1Object());
  152. }
  153. authAttrs = new Asn1.Cms.AttributeTable(new DerSet(v));
  154. }
  155. }
  156. return authAttrs;
  157. }
  158. /**
  159. * return a table of the unauthenticated attributes indexed by
  160. * the OID of the attribute.
  161. * @exception java.io.IOException
  162. */
  163. public Asn1.Cms.AttributeTable GetUnauthAttrs()
  164. {
  165. if (unauthAttrs == null && unauthAttrNotRead)
  166. {
  167. Asn1SetParser s = authData.GetUnauthAttrs();
  168. unauthAttrNotRead = false;
  169. if (s != null)
  170. {
  171. Asn1EncodableVector v = new Asn1EncodableVector();
  172. IAsn1Convertible o;
  173. while ((o = s.ReadObject()) != null)
  174. {
  175. Asn1SequenceParser seq = (Asn1SequenceParser)o;
  176. v.Add(seq.ToAsn1Object());
  177. }
  178. unauthAttrs = new Asn1.Cms.AttributeTable(new DerSet(v));
  179. }
  180. }
  181. return unauthAttrs;
  182. }
  183. }
  184. }
  185. #pragma warning restore
  186. #endif