DeferredHash.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using Org.BouncyCastle.Security;
  5. using Org.BouncyCastle.Utilities;
  6. namespace Org.BouncyCastle.Crypto.Tls
  7. {
  8. /**
  9. * Buffers input until the hash algorithm is determined.
  10. */
  11. internal class DeferredHash
  12. : TlsHandshakeHash
  13. {
  14. protected const int BUFFERING_HASH_LIMIT = 4;
  15. protected TlsContext mContext;
  16. private DigestInputBuffer mBuf;
  17. private IDictionary mHashes;
  18. private int mPrfHashAlgorithm;
  19. internal DeferredHash()
  20. {
  21. this.mBuf = new DigestInputBuffer();
  22. this.mHashes = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  23. this.mPrfHashAlgorithm = -1;
  24. }
  25. private DeferredHash(byte prfHashAlgorithm, IDigest prfHash)
  26. {
  27. this.mBuf = null;
  28. this.mHashes = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  29. this.mPrfHashAlgorithm = prfHashAlgorithm;
  30. mHashes[prfHashAlgorithm] = prfHash;
  31. }
  32. public virtual void Init(TlsContext context)
  33. {
  34. this.mContext = context;
  35. }
  36. public virtual TlsHandshakeHash NotifyPrfDetermined()
  37. {
  38. int prfAlgorithm = mContext.SecurityParameters.PrfAlgorithm;
  39. if (prfAlgorithm == PrfAlgorithm.tls_prf_legacy)
  40. {
  41. CombinedHash legacyHash = new CombinedHash();
  42. legacyHash.Init(mContext);
  43. mBuf.UpdateDigest(legacyHash);
  44. return legacyHash.NotifyPrfDetermined();
  45. }
  46. this.mPrfHashAlgorithm = TlsUtilities.GetHashAlgorithmForPrfAlgorithm(prfAlgorithm);
  47. CheckTrackingHash((byte)mPrfHashAlgorithm);
  48. return this;
  49. }
  50. public virtual void TrackHashAlgorithm(byte hashAlgorithm)
  51. {
  52. if (mBuf == null)
  53. throw new InvalidOperationException("Too late to track more hash algorithms");
  54. CheckTrackingHash(hashAlgorithm);
  55. }
  56. public virtual void SealHashAlgorithms()
  57. {
  58. CheckStopBuffering();
  59. }
  60. public virtual TlsHandshakeHash StopTracking()
  61. {
  62. byte prfHashAlgorithm = (byte)mPrfHashAlgorithm;
  63. IDigest prfHash = TlsUtilities.CloneHash(prfHashAlgorithm, (IDigest)mHashes[prfHashAlgorithm]);
  64. if (mBuf != null)
  65. {
  66. mBuf.UpdateDigest(prfHash);
  67. }
  68. DeferredHash result = new DeferredHash(prfHashAlgorithm, prfHash);
  69. result.Init(mContext);
  70. return result;
  71. }
  72. public virtual IDigest ForkPrfHash()
  73. {
  74. CheckStopBuffering();
  75. byte prfHashAlgorithm = (byte)mPrfHashAlgorithm;
  76. if (mBuf != null)
  77. {
  78. IDigest prfHash = TlsUtilities.CreateHash(prfHashAlgorithm);
  79. mBuf.UpdateDigest(prfHash);
  80. return prfHash;
  81. }
  82. return TlsUtilities.CloneHash(prfHashAlgorithm, (IDigest)mHashes[prfHashAlgorithm]);
  83. }
  84. public virtual byte[] GetFinalHash(byte hashAlgorithm)
  85. {
  86. IDigest d = (IDigest)mHashes[hashAlgorithm];
  87. if (d == null)
  88. throw new InvalidOperationException("HashAlgorithm." + HashAlgorithm.GetText(hashAlgorithm) + " is not being tracked");
  89. d = TlsUtilities.CloneHash(hashAlgorithm, d);
  90. if (mBuf != null)
  91. {
  92. mBuf.UpdateDigest(d);
  93. }
  94. return DigestUtilities.DoFinal(d);
  95. }
  96. public virtual string AlgorithmName
  97. {
  98. get { throw new InvalidOperationException("Use Fork() to get a definite IDigest"); }
  99. }
  100. public virtual int GetByteLength()
  101. {
  102. throw new InvalidOperationException("Use Fork() to get a definite IDigest");
  103. }
  104. public virtual int GetDigestSize()
  105. {
  106. throw new InvalidOperationException("Use Fork() to get a definite IDigest");
  107. }
  108. public virtual void Update(byte input)
  109. {
  110. if (mBuf != null)
  111. {
  112. mBuf.WriteByte(input);
  113. return;
  114. }
  115. foreach (IDigest hash in mHashes.Values)
  116. {
  117. hash.Update(input);
  118. }
  119. }
  120. public virtual void BlockUpdate(byte[] input, int inOff, int len)
  121. {
  122. if (mBuf != null)
  123. {
  124. mBuf.Write(input, inOff, len);
  125. return;
  126. }
  127. foreach (IDigest hash in mHashes.Values)
  128. {
  129. hash.BlockUpdate(input, inOff, len);
  130. }
  131. }
  132. public virtual int DoFinal(byte[] output, int outOff)
  133. {
  134. throw new InvalidOperationException("Use Fork() to get a definite IDigest");
  135. }
  136. public virtual void Reset()
  137. {
  138. if (mBuf != null)
  139. {
  140. mBuf.SetLength(0);
  141. return;
  142. }
  143. foreach (IDigest hash in mHashes.Values)
  144. {
  145. hash.Reset();
  146. }
  147. }
  148. protected virtual void CheckStopBuffering()
  149. {
  150. if (mBuf != null && mHashes.Count <= BUFFERING_HASH_LIMIT)
  151. {
  152. foreach (IDigest hash in mHashes.Values)
  153. {
  154. mBuf.UpdateDigest(hash);
  155. }
  156. this.mBuf = null;
  157. }
  158. }
  159. protected virtual void CheckTrackingHash(byte hashAlgorithm)
  160. {
  161. if (!mHashes.Contains(hashAlgorithm))
  162. {
  163. IDigest hash = TlsUtilities.CreateHash(hashAlgorithm);
  164. mHashes[hashAlgorithm] = hash;
  165. }
  166. }
  167. }
  168. }
  169. #endif