CMSAuthenticatedDataParser.cs 5.5 KB

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