CMSAuthenticatedData.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * containing class for an CMS Authenticated Data object
  13. */
  14. public class CmsAuthenticatedData
  15. {
  16. internal RecipientInformationStore recipientInfoStore;
  17. internal ContentInfo contentInfo;
  18. private AlgorithmIdentifier macAlg;
  19. private Asn1Set authAttrs;
  20. private Asn1Set unauthAttrs;
  21. private byte[] mac;
  22. public CmsAuthenticatedData(
  23. byte[] authData)
  24. : this(CmsUtilities.ReadContentInfo(authData))
  25. {
  26. }
  27. public CmsAuthenticatedData(
  28. Stream authData)
  29. : this(CmsUtilities.ReadContentInfo(authData))
  30. {
  31. }
  32. public CmsAuthenticatedData(
  33. ContentInfo contentInfo)
  34. {
  35. this.contentInfo = contentInfo;
  36. AuthenticatedData authData = AuthenticatedData.GetInstance(contentInfo.Content);
  37. //
  38. // read the recipients
  39. //
  40. Asn1Set recipientInfos = authData.RecipientInfos;
  41. this.macAlg = authData.MacAlgorithm;
  42. //
  43. // read the authenticated content info
  44. //
  45. ContentInfo encInfo = authData.EncapsulatedContentInfo;
  46. CmsReadable readable = new CmsProcessableByteArray(
  47. Asn1OctetString.GetInstance(encInfo.Content).GetOctets());
  48. CmsSecureReadable secureReadable = new CmsEnvelopedHelper.CmsAuthenticatedSecureReadable(
  49. this.macAlg, readable);
  50. //
  51. // build the RecipientInformationStore
  52. //
  53. this.recipientInfoStore = CmsEnvelopedHelper.BuildRecipientInformationStore(
  54. recipientInfos, secureReadable);
  55. this.authAttrs = authData.AuthAttrs;
  56. this.mac = authData.Mac.GetOctets();
  57. this.unauthAttrs = authData.UnauthAttrs;
  58. }
  59. public byte[] GetMac()
  60. {
  61. return Arrays.Clone(mac);
  62. }
  63. public AlgorithmIdentifier MacAlgorithmID
  64. {
  65. get { return macAlg; }
  66. }
  67. /**
  68. * return the object identifier for the content MAC algorithm.
  69. */
  70. public string MacAlgOid
  71. {
  72. get { return macAlg.Algorithm.Id; }
  73. }
  74. /**
  75. * return a store of the intended recipients for this message
  76. */
  77. public RecipientInformationStore GetRecipientInfos()
  78. {
  79. return recipientInfoStore;
  80. }
  81. /**
  82. * return the ContentInfo
  83. */
  84. public ContentInfo ContentInfo
  85. {
  86. get { return contentInfo; }
  87. }
  88. /**
  89. * return a table of the digested attributes indexed by
  90. * the OID of the attribute.
  91. */
  92. public Asn1.Cms.AttributeTable GetAuthAttrs()
  93. {
  94. if (authAttrs == null)
  95. return null;
  96. return new Asn1.Cms.AttributeTable(authAttrs);
  97. }
  98. /**
  99. * return a table of the undigested attributes indexed by
  100. * the OID of the attribute.
  101. */
  102. public Asn1.Cms.AttributeTable GetUnauthAttrs()
  103. {
  104. if (unauthAttrs == null)
  105. return null;
  106. return new Asn1.Cms.AttributeTable(unauthAttrs);
  107. }
  108. /**
  109. * return the ASN.1 encoded representation of this object.
  110. */
  111. public byte[] GetEncoded()
  112. {
  113. return contentInfo.GetEncoded();
  114. }
  115. }
  116. }
  117. #pragma warning restore
  118. #endif