DeferredHash.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls
  8. {
  9. /// <summary>Buffers input until the hash algorithm is determined.</summary>
  10. internal sealed class DeferredHash
  11. : TlsHandshakeHash
  12. {
  13. private const int BufferingHashLimit = 4;
  14. private readonly TlsContext m_context;
  15. private DigestInputBuffer m_buf;
  16. private IDictionary<int, TlsHash> m_hashes;
  17. private bool m_forceBuffering;
  18. private bool m_sealed;
  19. internal DeferredHash(TlsContext context)
  20. {
  21. this.m_context = context;
  22. this.m_buf = new DigestInputBuffer();
  23. this.m_hashes = new Dictionary<int, TlsHash>();
  24. this.m_forceBuffering = false;
  25. this.m_sealed = false;
  26. }
  27. /// <exception cref="IOException"/>
  28. public void CopyBufferTo(Stream output)
  29. {
  30. if (m_buf == null)
  31. {
  32. // If you see this, you need to call ForceBuffering() before SealHashAlgorithms()
  33. throw new InvalidOperationException("Not buffering");
  34. }
  35. m_buf.CopyInputTo(output);
  36. }
  37. public void ForceBuffering()
  38. {
  39. if (m_sealed)
  40. throw new InvalidOperationException("Too late to force buffering");
  41. this.m_forceBuffering = true;
  42. }
  43. public void NotifyPrfDetermined()
  44. {
  45. SecurityParameters securityParameters = m_context.SecurityParameters;
  46. switch (securityParameters.PrfAlgorithm)
  47. {
  48. case PrfAlgorithm.ssl_prf_legacy:
  49. case PrfAlgorithm.tls_prf_legacy:
  50. {
  51. CheckTrackingHash(CryptoHashAlgorithm.md5);
  52. CheckTrackingHash(CryptoHashAlgorithm.sha1);
  53. break;
  54. }
  55. default:
  56. {
  57. CheckTrackingHash(securityParameters.PrfCryptoHashAlgorithm);
  58. break;
  59. }
  60. }
  61. }
  62. public void TrackHashAlgorithm(int cryptoHashAlgorithm)
  63. {
  64. if (m_sealed)
  65. throw new InvalidOperationException("Too late to track more hash algorithms");
  66. CheckTrackingHash(cryptoHashAlgorithm);
  67. }
  68. public void SealHashAlgorithms()
  69. {
  70. if (m_sealed)
  71. throw new InvalidOperationException("Already sealed");
  72. this.m_sealed = true;
  73. CheckStopBuffering();
  74. }
  75. public void StopTracking()
  76. {
  77. SecurityParameters securityParameters = m_context.SecurityParameters;
  78. IDictionary<int, TlsHash> newHashes = new Dictionary<int, TlsHash>();
  79. switch (securityParameters.PrfAlgorithm)
  80. {
  81. case PrfAlgorithm.ssl_prf_legacy:
  82. case PrfAlgorithm.tls_prf_legacy:
  83. {
  84. CloneHash(newHashes, CryptoHashAlgorithm.md5);
  85. CloneHash(newHashes, CryptoHashAlgorithm.sha1);
  86. break;
  87. }
  88. default:
  89. {
  90. CloneHash(newHashes, securityParameters.PrfCryptoHashAlgorithm);
  91. break;
  92. }
  93. }
  94. this.m_buf = null;
  95. this.m_hashes = newHashes;
  96. this.m_forceBuffering = false;
  97. this.m_sealed = true;
  98. }
  99. public TlsHash ForkPrfHash()
  100. {
  101. CheckStopBuffering();
  102. SecurityParameters securityParameters = m_context.SecurityParameters;
  103. TlsHash prfHash;
  104. switch (securityParameters.PrfAlgorithm)
  105. {
  106. case PrfAlgorithm.ssl_prf_legacy:
  107. case PrfAlgorithm.tls_prf_legacy:
  108. {
  109. TlsHash md5Hash = CloneHash(CryptoHashAlgorithm.md5);
  110. TlsHash sha1Hash = CloneHash(CryptoHashAlgorithm.sha1);
  111. prfHash = new CombinedHash(m_context, md5Hash, sha1Hash);
  112. break;
  113. }
  114. default:
  115. {
  116. prfHash = CloneHash(securityParameters.PrfCryptoHashAlgorithm);
  117. break;
  118. }
  119. }
  120. if (m_buf != null)
  121. {
  122. m_buf.UpdateDigest(prfHash);
  123. }
  124. return prfHash;
  125. }
  126. public byte[] GetFinalHash(int cryptoHashAlgorithm)
  127. {
  128. if (!m_hashes.TryGetValue(cryptoHashAlgorithm, out var hash))
  129. throw new InvalidOperationException("CryptoHashAlgorithm." + cryptoHashAlgorithm
  130. + " is not being tracked");
  131. CheckStopBuffering();
  132. hash = hash.CloneHash();
  133. if (m_buf != null)
  134. {
  135. m_buf.UpdateDigest(hash);
  136. }
  137. return hash.CalculateHash();
  138. }
  139. public void Update(byte[] input, int inOff, int len)
  140. {
  141. if (m_buf != null)
  142. {
  143. m_buf.Write(input, inOff, len);
  144. return;
  145. }
  146. foreach (TlsHash hash in m_hashes.Values)
  147. {
  148. hash.Update(input, inOff, len);
  149. }
  150. }
  151. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  152. public void Update(ReadOnlySpan<byte> input)
  153. {
  154. if (m_buf != null)
  155. {
  156. m_buf.Write(input);
  157. return;
  158. }
  159. foreach (TlsHash hash in m_hashes.Values)
  160. {
  161. hash.Update(input);
  162. }
  163. }
  164. #endif
  165. public byte[] CalculateHash()
  166. {
  167. throw new InvalidOperationException("Use 'ForkPrfHash' to get a definite hash");
  168. }
  169. public TlsHash CloneHash()
  170. {
  171. throw new InvalidOperationException("attempt to clone a DeferredHash");
  172. }
  173. public void Reset()
  174. {
  175. if (m_buf != null)
  176. {
  177. m_buf.SetLength(0);
  178. return;
  179. }
  180. foreach (TlsHash hash in m_hashes.Values)
  181. {
  182. hash.Reset();
  183. }
  184. }
  185. private void CheckStopBuffering()
  186. {
  187. if (!m_forceBuffering && m_sealed && m_buf != null && m_hashes.Count <= BufferingHashLimit)
  188. {
  189. foreach (TlsHash hash in m_hashes.Values)
  190. {
  191. m_buf.UpdateDigest(hash);
  192. }
  193. this.m_buf = null;
  194. }
  195. }
  196. private void CheckTrackingHash(int cryptoHashAlgorithm)
  197. {
  198. if (!m_hashes.ContainsKey(cryptoHashAlgorithm))
  199. {
  200. TlsHash hash = m_context.Crypto.CreateHash(cryptoHashAlgorithm);
  201. m_hashes[cryptoHashAlgorithm] = hash;
  202. }
  203. }
  204. private TlsHash CloneHash(int cryptoHashAlgorithm)
  205. {
  206. return m_hashes[cryptoHashAlgorithm].CloneHash();
  207. }
  208. private void CloneHash(IDictionary<int, TlsHash> newHashes, int cryptoHashAlgorithm)
  209. {
  210. TlsHash hash = CloneHash(cryptoHashAlgorithm);
  211. if (m_buf != null)
  212. {
  213. m_buf.UpdateDigest(hash);
  214. }
  215. newHashes[cryptoHashAlgorithm] = hash;
  216. }
  217. }
  218. }
  219. #pragma warning restore
  220. #endif