CMSEnvelopedDataParser.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Crypto;
  10. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  11. {
  12. /**
  13. * Parsing class for an CMS Enveloped 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. * CmsEnvelopedDataParser ep = new CmsEnvelopedDataParser(inputStream);
  22. *
  23. * RecipientInformationStore recipients = ep.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);
  33. *
  34. * processDataStream(recData.getContentStream());
  35. * }
  36. * </pre>
  37. * Note: this class does not introduce buffering - if you are processing large files you should create
  38. * the parser with:
  39. * <pre>
  40. * CmsEnvelopedDataParser ep = new CmsEnvelopedDataParser(new BufferedInputStream(inputStream, bufSize));
  41. * </pre>
  42. * where bufSize is a suitably large buffer size.
  43. * </p>
  44. */
  45. public class CmsEnvelopedDataParser
  46. : CmsContentInfoParser
  47. {
  48. internal RecipientInformationStore recipientInfoStore;
  49. internal EnvelopedDataParser envelopedData;
  50. private AlgorithmIdentifier _encAlg;
  51. private Asn1.Cms.AttributeTable _unprotectedAttributes;
  52. private bool _attrNotRead;
  53. public CmsEnvelopedDataParser(
  54. byte[] envelopedData)
  55. : this(new MemoryStream(envelopedData, false))
  56. {
  57. }
  58. public CmsEnvelopedDataParser(
  59. Stream envelopedData)
  60. : base(envelopedData)
  61. {
  62. this._attrNotRead = true;
  63. this.envelopedData = new EnvelopedDataParser(
  64. (Asn1SequenceParser)this.contentInfo.GetContent(Asn1Tags.Sequence));
  65. // TODO Validate version?
  66. //DerInteger version = this.envelopedData.Version;
  67. //
  68. // read the recipients
  69. //
  70. Asn1Set recipientInfos = Asn1Set.GetInstance(this.envelopedData.GetRecipientInfos().ToAsn1Object());
  71. //
  72. // read the encrypted content info
  73. //
  74. EncryptedContentInfoParser encInfo = this.envelopedData.GetEncryptedContentInfo();
  75. this._encAlg = encInfo.ContentEncryptionAlgorithm;
  76. CmsReadable readable = new CmsProcessableInputStream(
  77. ((Asn1OctetStringParser)encInfo.GetEncryptedContent(Asn1Tags.OctetString)).GetOctetStream());
  78. CmsSecureReadable secureReadable = new CmsEnvelopedHelper.CmsEnvelopedSecureReadable(
  79. this._encAlg, readable);
  80. //
  81. // build the RecipientInformationStore
  82. //
  83. this.recipientInfoStore = CmsEnvelopedHelper.BuildRecipientInformationStore(
  84. recipientInfos, secureReadable);
  85. }
  86. public AlgorithmIdentifier EncryptionAlgorithmID
  87. {
  88. get { return _encAlg; }
  89. }
  90. /**
  91. * return the object identifier for the content encryption algorithm.
  92. */
  93. public string EncryptionAlgOid
  94. {
  95. get { return _encAlg.Algorithm.Id; }
  96. }
  97. /**
  98. * return the ASN.1 encoded encryption algorithm parameters, or null if
  99. * there aren't any.
  100. */
  101. public Asn1Object EncryptionAlgParams
  102. {
  103. get
  104. {
  105. Asn1Encodable ae = _encAlg.Parameters;
  106. return ae == null ? null : ae.ToAsn1Object();
  107. }
  108. }
  109. /**
  110. * return a store of the intended recipients for this message
  111. */
  112. public RecipientInformationStore GetRecipientInfos()
  113. {
  114. return this.recipientInfoStore;
  115. }
  116. /**
  117. * return a table of the unprotected attributes indexed by
  118. * the OID of the attribute.
  119. * @throws IOException
  120. */
  121. public Asn1.Cms.AttributeTable GetUnprotectedAttributes()
  122. {
  123. if (_unprotectedAttributes == null && _attrNotRead)
  124. {
  125. Asn1SetParser asn1Set = this.envelopedData.GetUnprotectedAttrs();
  126. _attrNotRead = false;
  127. if (asn1Set != null)
  128. {
  129. Asn1EncodableVector v = new Asn1EncodableVector();
  130. IAsn1Convertible o;
  131. while ((o = asn1Set.ReadObject()) != null)
  132. {
  133. Asn1SequenceParser seq = (Asn1SequenceParser)o;
  134. v.Add(seq.ToAsn1Object());
  135. }
  136. _unprotectedAttributes = new Asn1.Cms.AttributeTable(new DerSet(v));
  137. }
  138. }
  139. return _unprotectedAttributes;
  140. }
  141. }
  142. }
  143. #pragma warning restore
  144. #endif