BcTlsSecret.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  9. {
  10. /// <summary>BC light-weight support class for handling TLS secrets and deriving key material and other secrets
  11. /// from them.</summary>
  12. public class BcTlsSecret
  13. : AbstractTlsSecret
  14. {
  15. public static BcTlsSecret Convert(BcTlsCrypto crypto, TlsSecret secret)
  16. {
  17. if (secret is BcTlsSecret)
  18. return (BcTlsSecret)secret;
  19. if (secret is AbstractTlsSecret)
  20. {
  21. AbstractTlsSecret abstractTlsSecret = (AbstractTlsSecret)secret;
  22. return crypto.AdoptLocalSecret(CopyData(abstractTlsSecret));
  23. }
  24. throw new ArgumentException("unrecognized TlsSecret - cannot copy data: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(secret));
  25. }
  26. // SSL3 magic mix constants ("A", "BB", "CCC", ...)
  27. private static readonly byte[] Ssl3Const = GenerateSsl3Constants();
  28. private static byte[] GenerateSsl3Constants()
  29. {
  30. int n = 15;
  31. byte[] result = new byte[n * (n + 1) / 2];
  32. int pos = 0;
  33. for (int i = 0; i < n; ++i)
  34. {
  35. byte b = (byte)('A' + i);
  36. for (int j = 0; j <= i; ++j)
  37. {
  38. result[pos++] = b;
  39. }
  40. }
  41. return result;
  42. }
  43. protected readonly BcTlsCrypto m_crypto;
  44. public BcTlsSecret(BcTlsCrypto crypto, byte[] data)
  45. : base(data)
  46. {
  47. this.m_crypto = crypto;
  48. }
  49. public override TlsSecret DeriveUsingPrf(int prfAlgorithm, string label, byte[] seed, int length)
  50. {
  51. lock (this)
  52. {
  53. CheckAlive();
  54. switch (prfAlgorithm)
  55. {
  56. case PrfAlgorithm.tls13_hkdf_sha256:
  57. return TlsCryptoUtilities.HkdfExpandLabel(this, CryptoHashAlgorithm.sha256, label, seed, length);
  58. case PrfAlgorithm.tls13_hkdf_sha384:
  59. return TlsCryptoUtilities.HkdfExpandLabel(this, CryptoHashAlgorithm.sha384, label, seed, length);
  60. case PrfAlgorithm.tls13_hkdf_sm3:
  61. return TlsCryptoUtilities.HkdfExpandLabel(this, CryptoHashAlgorithm.sm3, label, seed, length);
  62. default:
  63. return m_crypto.AdoptLocalSecret(Prf(prfAlgorithm, label, seed, length));
  64. }
  65. }
  66. }
  67. public override TlsSecret HkdfExpand(int cryptoHashAlgorithm, byte[] info, int length)
  68. {
  69. lock (this)
  70. {
  71. if (length < 1)
  72. return m_crypto.AdoptLocalSecret(TlsUtilities.EmptyBytes);
  73. int hashLen = TlsCryptoUtilities.GetHashOutputSize(cryptoHashAlgorithm);
  74. if (length > (255 * hashLen))
  75. throw new ArgumentException("must be <= 255 * (output size of 'hashAlgorithm')", "length");
  76. CheckAlive();
  77. byte[] prk = m_data;
  78. HMac hmac = new HMac(m_crypto.CreateDigest(cryptoHashAlgorithm));
  79. hmac.Init(new KeyParameter(prk));
  80. byte[] okm = new byte[length];
  81. byte[] t = new byte[hashLen];
  82. byte counter = 0x00;
  83. int pos = 0;
  84. for (; ; )
  85. {
  86. hmac.BlockUpdate(info, 0, info.Length);
  87. hmac.Update((byte)++counter);
  88. hmac.DoFinal(t, 0);
  89. int remaining = length - pos;
  90. if (remaining <= hashLen)
  91. {
  92. Array.Copy(t, 0, okm, pos, remaining);
  93. break;
  94. }
  95. Array.Copy(t, 0, okm, pos, hashLen);
  96. pos += hashLen;
  97. hmac.BlockUpdate(t, 0, t.Length);
  98. }
  99. return m_crypto.AdoptLocalSecret(okm);
  100. }
  101. }
  102. public override TlsSecret HkdfExtract(int cryptoHashAlgorithm, TlsSecret ikm)
  103. {
  104. lock (this)
  105. {
  106. CheckAlive();
  107. byte[] salt = m_data;
  108. this.m_data = null;
  109. HMac hmac = new HMac(m_crypto.CreateDigest(cryptoHashAlgorithm));
  110. hmac.Init(new KeyParameter(salt));
  111. Convert(m_crypto, ikm).UpdateMac(hmac);
  112. byte[] prk = new byte[hmac.GetMacSize()];
  113. hmac.DoFinal(prk, 0);
  114. return m_crypto.AdoptLocalSecret(prk);
  115. }
  116. }
  117. protected override AbstractTlsCrypto Crypto
  118. {
  119. get { return m_crypto; }
  120. }
  121. protected virtual void HmacHash(IDigest digest, byte[] secret, int secretOff, int secretLen, byte[] seed,
  122. byte[] output)
  123. {
  124. HMac mac = new HMac(digest);
  125. mac.Init(new KeyParameter(secret, secretOff, secretLen));
  126. byte[] a = seed;
  127. int macSize = mac.GetMacSize();
  128. byte[] b1 = new byte[macSize];
  129. byte[] b2 = new byte[macSize];
  130. int pos = 0;
  131. while (pos < output.Length)
  132. {
  133. mac.BlockUpdate(a, 0, a.Length);
  134. mac.DoFinal(b1, 0);
  135. a = b1;
  136. mac.BlockUpdate(a, 0, a.Length);
  137. mac.BlockUpdate(seed, 0, seed.Length);
  138. mac.DoFinal(b2, 0);
  139. Array.Copy(b2, 0, output, pos, System.Math.Min(macSize, output.Length - pos));
  140. pos += macSize;
  141. }
  142. }
  143. protected virtual byte[] Prf(int prfAlgorithm, string label, byte[] seed, int length)
  144. {
  145. if (PrfAlgorithm.ssl_prf_legacy == prfAlgorithm)
  146. return Prf_Ssl(seed, length);
  147. byte[] labelSeed = Arrays.Concatenate(Strings.ToByteArray(label), seed);
  148. if (PrfAlgorithm.tls_prf_legacy == prfAlgorithm)
  149. return Prf_1_0(labelSeed, length);
  150. return Prf_1_2(prfAlgorithm, labelSeed, length);
  151. }
  152. protected virtual byte[] Prf_Ssl(byte[] seed, int length)
  153. {
  154. IDigest md5 = m_crypto.CreateDigest(CryptoHashAlgorithm.md5);
  155. IDigest sha1 = m_crypto.CreateDigest(CryptoHashAlgorithm.sha1);
  156. int md5Size = md5.GetDigestSize();
  157. int sha1Size = sha1.GetDigestSize();
  158. byte[] tmp = new byte[System.Math.Max(md5Size, sha1Size)];
  159. byte[] result = new byte[length];
  160. int constLen = 1, constPos = 0, resultPos = 0;
  161. while (resultPos < length)
  162. {
  163. sha1.BlockUpdate(Ssl3Const, constPos, constLen);
  164. constPos += constLen++;
  165. sha1.BlockUpdate(m_data, 0, m_data.Length);
  166. sha1.BlockUpdate(seed, 0, seed.Length);
  167. sha1.DoFinal(tmp, 0);
  168. md5.BlockUpdate(m_data, 0, m_data.Length);
  169. md5.BlockUpdate(tmp, 0, sha1Size);
  170. int remaining = length - resultPos;
  171. if (remaining < md5Size)
  172. {
  173. md5.DoFinal(tmp, 0);
  174. Array.Copy(tmp, 0, result, resultPos, remaining);
  175. resultPos += remaining;
  176. }
  177. else
  178. {
  179. md5.DoFinal(result, resultPos);
  180. resultPos += md5Size;
  181. }
  182. }
  183. return result;
  184. }
  185. protected virtual byte[] Prf_1_0(byte[] labelSeed, int length)
  186. {
  187. int s_half = (m_data.Length + 1) / 2;
  188. IDigest md5 = m_crypto.CreateDigest(CryptoHashAlgorithm.md5);
  189. byte[] b1 = new byte[length];
  190. HmacHash(md5, m_data, 0, s_half, labelSeed, b1);
  191. IDigest sha1 = m_crypto.CreateDigest(CryptoHashAlgorithm.sha1);
  192. byte[] b2 = new byte[length];
  193. HmacHash(sha1, m_data, m_data.Length - s_half, s_half, labelSeed, b2);
  194. for (int i = 0; i < length; i++)
  195. {
  196. b1[i] ^= b2[i];
  197. }
  198. return b1;
  199. }
  200. protected virtual byte[] Prf_1_2(int prfAlgorithm, byte[] labelSeed, int length)
  201. {
  202. IDigest digest = m_crypto.CreateDigest(TlsCryptoUtilities.GetHashForPrf(prfAlgorithm));
  203. byte[] result = new byte[length];
  204. HmacHash(digest, m_data, 0, m_data.Length, labelSeed, result);
  205. return result;
  206. }
  207. protected virtual void UpdateMac(IMac mac)
  208. {
  209. lock (this)
  210. {
  211. CheckAlive();
  212. mac.BlockUpdate(m_data, 0, m_data.Length);
  213. }
  214. }
  215. }
  216. }
  217. #pragma warning restore
  218. #endif