MacStream.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO
  7. {
  8. public sealed class MacStream
  9. : Stream
  10. {
  11. private readonly Stream m_stream;
  12. private readonly IMac m_readMac;
  13. private readonly IMac m_writeMac;
  14. public MacStream(Stream stream, IMac readMac, IMac writeMac)
  15. {
  16. m_stream = stream;
  17. m_readMac = readMac;
  18. m_writeMac = writeMac;
  19. }
  20. public IMac ReadMac => m_readMac;
  21. public IMac WriteMac => m_writeMac;
  22. public override bool CanRead
  23. {
  24. get { return m_stream.CanRead; }
  25. }
  26. public sealed override bool CanSeek
  27. {
  28. get { return false; }
  29. }
  30. public override bool CanWrite
  31. {
  32. get { return m_stream.CanWrite; }
  33. }
  34. #if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER || (UNITY_2021_2_OR_NEWER && (NET_STANDARD_2_0 || NET_STANDARD_2_1))
  35. public override void CopyTo(Stream destination, int bufferSize)
  36. {
  37. if (m_readMac == null)
  38. {
  39. m_stream.CopyTo(destination, bufferSize);
  40. }
  41. else
  42. {
  43. base.CopyTo(destination, bufferSize);
  44. }
  45. }
  46. #endif
  47. public override void Flush()
  48. {
  49. m_stream.Flush();
  50. }
  51. public sealed override long Length
  52. {
  53. get { throw new NotSupportedException(); }
  54. }
  55. public sealed override long Position
  56. {
  57. get { throw new NotSupportedException(); }
  58. set { throw new NotSupportedException(); }
  59. }
  60. public override int Read(byte[] buffer, int offset, int count)
  61. {
  62. int n = m_stream.Read(buffer, offset, count);
  63. if (m_readMac != null && n > 0)
  64. {
  65. m_readMac.BlockUpdate(buffer, offset, n);
  66. }
  67. return n;
  68. }
  69. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  70. public override int Read(Span<byte> buffer)
  71. {
  72. int n = m_stream.Read(buffer);
  73. if (m_readMac != null && n > 0)
  74. {
  75. m_readMac.BlockUpdate(buffer[..n]);
  76. }
  77. return n;
  78. }
  79. #endif
  80. public override int ReadByte()
  81. {
  82. int b = m_stream.ReadByte();
  83. if (m_readMac != null && b >= 0)
  84. {
  85. m_readMac.Update((byte)b);
  86. }
  87. return b;
  88. }
  89. public sealed override long Seek(long offset, SeekOrigin origin)
  90. {
  91. throw new NotSupportedException();
  92. }
  93. public sealed override void SetLength(long length)
  94. {
  95. throw new NotSupportedException();
  96. }
  97. public override void Write(byte[] buffer, int offset, int count)
  98. {
  99. m_stream.Write(buffer, offset, count);
  100. if (m_writeMac != null && count > 0)
  101. {
  102. m_writeMac.BlockUpdate(buffer, offset, count);
  103. }
  104. }
  105. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  106. public override void Write(ReadOnlySpan<byte> buffer)
  107. {
  108. m_stream.Write(buffer);
  109. if (m_writeMac != null && !buffer.IsEmpty)
  110. {
  111. m_writeMac.BlockUpdate(buffer);
  112. }
  113. }
  114. #endif
  115. public override void WriteByte(byte value)
  116. {
  117. m_stream.WriteByte(value);
  118. if (m_writeMac != null)
  119. {
  120. m_writeMac.Update(value);
  121. }
  122. }
  123. protected override void Dispose(bool disposing)
  124. {
  125. if (disposing)
  126. {
  127. m_stream.Dispose();
  128. }
  129. base.Dispose(disposing);
  130. }
  131. }
  132. }
  133. #pragma warning restore
  134. #endif