RecipientInformation.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  15. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  16. {
  17. public abstract class RecipientInformation
  18. {
  19. internal RecipientID rid = new RecipientID();
  20. internal AlgorithmIdentifier keyEncAlg;
  21. internal CmsSecureReadable secureReadable;
  22. private byte[] resultMac;
  23. internal RecipientInformation(
  24. AlgorithmIdentifier keyEncAlg,
  25. CmsSecureReadable secureReadable)
  26. {
  27. this.keyEncAlg = keyEncAlg;
  28. this.secureReadable = secureReadable;
  29. }
  30. internal string GetContentAlgorithmName()
  31. {
  32. AlgorithmIdentifier algorithm = secureReadable.Algorithm;
  33. // return CmsEnvelopedHelper.Instance.GetSymmetricCipherName(algorithm.Algorithm.Id);
  34. return algorithm.Algorithm.Id;
  35. }
  36. public RecipientID RecipientID
  37. {
  38. get { return rid; }
  39. }
  40. public AlgorithmIdentifier KeyEncryptionAlgorithmID
  41. {
  42. get { return keyEncAlg; }
  43. }
  44. /**
  45. * return the object identifier for the key encryption algorithm.
  46. *
  47. * @return OID for key encryption algorithm.
  48. */
  49. public string KeyEncryptionAlgOid
  50. {
  51. get { return keyEncAlg.Algorithm.Id; }
  52. }
  53. /**
  54. * return the ASN.1 encoded key encryption algorithm parameters, or null if
  55. * there aren't any.
  56. *
  57. * @return ASN.1 encoding of key encryption algorithm parameters.
  58. */
  59. public Asn1Object KeyEncryptionAlgParams
  60. {
  61. get
  62. {
  63. Asn1Encodable ae = keyEncAlg.Parameters;
  64. return ae == null ? null : ae.ToAsn1Object();
  65. }
  66. }
  67. internal CmsTypedStream GetContentFromSessionKey(
  68. KeyParameter sKey)
  69. {
  70. CmsReadable readable = secureReadable.GetReadable(sKey);
  71. try
  72. {
  73. return new CmsTypedStream(readable.GetInputStream());
  74. }
  75. catch (IOException e)
  76. {
  77. throw new CmsException("error getting .", e);
  78. }
  79. }
  80. public byte[] GetContent(
  81. ICipherParameters key)
  82. {
  83. try
  84. {
  85. return CmsUtilities.StreamToByteArray(GetContentStream(key).ContentStream);
  86. }
  87. catch (IOException e)
  88. {
  89. throw new Exception("unable to parse internal stream: " + e);
  90. }
  91. }
  92. /**
  93. * Return the MAC calculated for the content stream. Note: this call is only meaningful once all
  94. * the content has been read.
  95. *
  96. * @return byte array containing the mac.
  97. */
  98. public byte[] GetMac()
  99. {
  100. if (resultMac == null)
  101. {
  102. object cryptoObject = secureReadable.CryptoObject;
  103. if (cryptoObject is IMac)
  104. {
  105. resultMac = MacUtilities.DoFinal((IMac)cryptoObject);
  106. }
  107. }
  108. return Arrays.Clone(resultMac);
  109. }
  110. public abstract CmsTypedStream GetContentStream(ICipherParameters key);
  111. }
  112. }
  113. #pragma warning restore
  114. #endif