DeferredHash.cs 7.2 KB

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