DeferredHash.cs 5.9 KB

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