CMSAuthenticatedData.cs 3.1 KB

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