BcTlsSecret.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace Best.HTTP.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: " + 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. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  68. public override TlsSecret DeriveUsingPrf(int prfAlgorithm, ReadOnlySpan<char> label, ReadOnlySpan<byte> seed,
  69. int length)
  70. {
  71. lock (this)
  72. {
  73. CheckAlive();
  74. switch (prfAlgorithm)
  75. {
  76. case PrfAlgorithm.tls13_hkdf_sha256:
  77. return TlsCryptoUtilities.HkdfExpandLabel(this, CryptoHashAlgorithm.sha256, label, seed, length);
  78. case PrfAlgorithm.tls13_hkdf_sha384:
  79. return TlsCryptoUtilities.HkdfExpandLabel(this, CryptoHashAlgorithm.sha384, label, seed, length);
  80. case PrfAlgorithm.tls13_hkdf_sm3:
  81. return TlsCryptoUtilities.HkdfExpandLabel(this, CryptoHashAlgorithm.sm3, label, seed, length);
  82. default:
  83. return m_crypto.AdoptLocalSecret(Prf(prfAlgorithm, label, seed, length));
  84. }
  85. }
  86. }
  87. #endif
  88. public override TlsSecret HkdfExpand(int cryptoHashAlgorithm, byte[] info, int length)
  89. {
  90. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  91. return HkdfExpand(cryptoHashAlgorithm, info.AsSpan(), length);
  92. #else
  93. lock (this)
  94. {
  95. if (length < 1)
  96. return m_crypto.AdoptLocalSecret(TlsUtilities.EmptyBytes);
  97. int hashLen = TlsCryptoUtilities.GetHashOutputSize(cryptoHashAlgorithm);
  98. if (length > (255 * hashLen))
  99. throw new ArgumentException("must be <= 255 * (output size of 'hashAlgorithm')", "length");
  100. CheckAlive();
  101. byte[] prk = m_data;
  102. HMac hmac = new HMac(m_crypto.CreateDigest(cryptoHashAlgorithm));
  103. hmac.Init(new KeyParameter(prk));
  104. byte[] okm = new byte[length];
  105. byte[] t = new byte[hashLen];
  106. byte counter = 0x00;
  107. int pos = 0;
  108. for (;;)
  109. {
  110. hmac.BlockUpdate(info, 0, info.Length);
  111. hmac.Update(++counter);
  112. hmac.DoFinal(t, 0);
  113. int remaining = length - pos;
  114. if (remaining <= hashLen)
  115. {
  116. Array.Copy(t, 0, okm, pos, remaining);
  117. break;
  118. }
  119. Array.Copy(t, 0, okm, pos, hashLen);
  120. pos += hashLen;
  121. hmac.BlockUpdate(t, 0, t.Length);
  122. }
  123. return m_crypto.AdoptLocalSecret(okm);
  124. }
  125. #endif
  126. }
  127. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  128. public override TlsSecret HkdfExpand(int cryptoHashAlgorithm, ReadOnlySpan<byte> info, int length)
  129. {
  130. lock (this)
  131. {
  132. if (length < 1)
  133. return m_crypto.AdoptLocalSecret(TlsUtilities.EmptyBytes);
  134. int hashLen = TlsCryptoUtilities.GetHashOutputSize(cryptoHashAlgorithm);
  135. if (length > (255 * hashLen))
  136. throw new ArgumentException("must be <= 255 * (output size of 'hashAlgorithm')", "length");
  137. CheckAlive();
  138. ReadOnlySpan<byte> prk = m_data;
  139. HMac hmac = new HMac(m_crypto.CreateDigest(cryptoHashAlgorithm));
  140. hmac.Init(new KeyParameter(prk));
  141. byte[] okm = new byte[length];
  142. Span<byte> t = hashLen <= 128
  143. ? stackalloc byte[hashLen]
  144. : new byte[hashLen];
  145. byte counter = 0x00;
  146. int pos = 0;
  147. for (;;)
  148. {
  149. hmac.BlockUpdate(info);
  150. hmac.Update(++counter);
  151. hmac.DoFinal(t);
  152. int remaining = length - pos;
  153. if (remaining <= hashLen)
  154. {
  155. t[..remaining].CopyTo(okm.AsSpan(pos));
  156. break;
  157. }
  158. t.CopyTo(okm.AsSpan(pos));
  159. pos += hashLen;
  160. hmac.BlockUpdate(t);
  161. }
  162. return m_crypto.AdoptLocalSecret(okm);
  163. }
  164. }
  165. #endif
  166. public override TlsSecret HkdfExtract(int cryptoHashAlgorithm, TlsSecret ikm)
  167. {
  168. lock (this)
  169. {
  170. CheckAlive();
  171. byte[] salt = m_data;
  172. this.m_data = null;
  173. HMac hmac = new HMac(m_crypto.CreateDigest(cryptoHashAlgorithm));
  174. hmac.Init(new KeyParameter(salt));
  175. Convert(m_crypto, ikm).UpdateMac(hmac);
  176. byte[] prk = new byte[hmac.GetMacSize()];
  177. hmac.DoFinal(prk, 0);
  178. return m_crypto.AdoptLocalSecret(prk);
  179. }
  180. }
  181. protected override AbstractTlsCrypto Crypto
  182. {
  183. get { return m_crypto; }
  184. }
  185. protected virtual void HmacHash(int cryptoHashAlgorithm, byte[] secret, int secretOff, int secretLen,
  186. byte[] seed, byte[] output)
  187. {
  188. IDigest digest = m_crypto.CreateDigest(cryptoHashAlgorithm);
  189. HMac hmac = new HMac(digest);
  190. hmac.Init(new KeyParameter(secret, secretOff, secretLen));
  191. byte[] a = seed;
  192. int macSize = hmac.GetMacSize();
  193. byte[] b1 = new byte[macSize];
  194. byte[] b2 = new byte[macSize];
  195. int pos = 0;
  196. while (pos < output.Length)
  197. {
  198. hmac.BlockUpdate(a, 0, a.Length);
  199. hmac.DoFinal(b1, 0);
  200. a = b1;
  201. hmac.BlockUpdate(a, 0, a.Length);
  202. hmac.BlockUpdate(seed, 0, seed.Length);
  203. hmac.DoFinal(b2, 0);
  204. Array.Copy(b2, 0, output, pos, System.Math.Min(macSize, output.Length - pos));
  205. pos += macSize;
  206. }
  207. }
  208. protected virtual byte[] Prf(int prfAlgorithm, string label, byte[] seed, int length)
  209. {
  210. if (PrfAlgorithm.ssl_prf_legacy == prfAlgorithm)
  211. return Prf_Ssl(seed, length);
  212. byte[] labelSeed = Arrays.Concatenate(Strings.ToByteArray(label), seed);
  213. if (PrfAlgorithm.tls_prf_legacy == prfAlgorithm)
  214. return Prf_1_0(labelSeed, length);
  215. return Prf_1_2(prfAlgorithm, labelSeed, length);
  216. }
  217. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  218. protected virtual byte[] Prf(int prfAlgorithm, ReadOnlySpan<char> label, ReadOnlySpan<byte> seed, int length)
  219. {
  220. if (PrfAlgorithm.ssl_prf_legacy == prfAlgorithm)
  221. return Prf_Ssl(seed, length);
  222. byte[] labelSeed = new byte[label.Length + seed.Length];
  223. for (int i = 0; i < label.Length; ++i)
  224. {
  225. labelSeed[i] = (byte)label[i];
  226. }
  227. seed.CopyTo(labelSeed.AsSpan(label.Length));
  228. if (PrfAlgorithm.tls_prf_legacy == prfAlgorithm)
  229. return Prf_1_0(labelSeed, length);
  230. return Prf_1_2(prfAlgorithm, labelSeed, length);
  231. }
  232. #endif
  233. protected virtual byte[] Prf_Ssl(byte[] seed, int length)
  234. {
  235. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  236. return Prf_Ssl(seed.AsSpan(), length);
  237. #else
  238. IDigest md5 = m_crypto.CreateDigest(CryptoHashAlgorithm.md5);
  239. IDigest sha1 = m_crypto.CreateDigest(CryptoHashAlgorithm.sha1);
  240. int md5Size = md5.GetDigestSize();
  241. int sha1Size = sha1.GetDigestSize();
  242. byte[] tmp = new byte[System.Math.Max(md5Size, sha1Size)];
  243. byte[] result = new byte[length];
  244. int constLen = 1, constPos = 0, resultPos = 0;
  245. while (resultPos < length)
  246. {
  247. sha1.BlockUpdate(Ssl3Const, constPos, constLen);
  248. constPos += constLen++;
  249. sha1.BlockUpdate(m_data, 0, m_data.Length);
  250. sha1.BlockUpdate(seed, 0, seed.Length);
  251. sha1.DoFinal(tmp, 0);
  252. md5.BlockUpdate(m_data, 0, m_data.Length);
  253. md5.BlockUpdate(tmp, 0, sha1Size);
  254. int remaining = length - resultPos;
  255. if (remaining < md5Size)
  256. {
  257. md5.DoFinal(tmp, 0);
  258. Array.Copy(tmp, 0, result, resultPos, remaining);
  259. resultPos += remaining;
  260. }
  261. else
  262. {
  263. md5.DoFinal(result, resultPos);
  264. resultPos += md5Size;
  265. }
  266. }
  267. return result;
  268. #endif
  269. }
  270. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  271. protected virtual byte[] Prf_Ssl(ReadOnlySpan<byte> seed, int length)
  272. {
  273. IDigest md5 = m_crypto.CreateDigest(CryptoHashAlgorithm.md5);
  274. IDigest sha1 = m_crypto.CreateDigest(CryptoHashAlgorithm.sha1);
  275. int md5Size = md5.GetDigestSize();
  276. int sha1Size = sha1.GetDigestSize();
  277. Span<byte> tmp = stackalloc byte[System.Math.Max(md5Size, sha1Size)];
  278. byte[] result = new byte[length];
  279. int constLen = 1, constPos = 0, resultPos = 0;
  280. while (resultPos < length)
  281. {
  282. sha1.BlockUpdate(Ssl3Const.AsSpan(constPos, constLen));
  283. constPos += constLen++;
  284. sha1.BlockUpdate(m_data);
  285. sha1.BlockUpdate(seed);
  286. sha1.DoFinal(tmp);
  287. md5.BlockUpdate(m_data);
  288. md5.BlockUpdate(tmp[..sha1Size]);
  289. int remaining = length - resultPos;
  290. if (remaining < md5Size)
  291. {
  292. md5.DoFinal(tmp);
  293. tmp[..remaining].CopyTo(result.AsSpan(resultPos));
  294. resultPos += remaining;
  295. }
  296. else
  297. {
  298. md5.DoFinal(result.AsSpan(resultPos));
  299. resultPos += md5Size;
  300. }
  301. }
  302. return result;
  303. }
  304. #endif
  305. protected virtual byte[] Prf_1_0(byte[] labelSeed, int length)
  306. {
  307. int s_half = (m_data.Length + 1) / 2;
  308. byte[] b1 = new byte[length];
  309. HmacHash(CryptoHashAlgorithm.md5, m_data, 0, s_half, labelSeed, b1);
  310. byte[] b2 = new byte[length];
  311. HmacHash(CryptoHashAlgorithm.sha1, m_data, m_data.Length - s_half, s_half, labelSeed, b2);
  312. for (int i = 0; i < length; i++)
  313. {
  314. b1[i] ^= b2[i];
  315. }
  316. return b1;
  317. }
  318. protected virtual byte[] Prf_1_2(int prfAlgorithm, byte[] labelSeed, int length)
  319. {
  320. int cryptoHashAlgorithm = TlsCryptoUtilities.GetHashForPrf(prfAlgorithm);
  321. byte[] result = new byte[length];
  322. HmacHash(cryptoHashAlgorithm, m_data, 0, m_data.Length, labelSeed, result);
  323. return result;
  324. }
  325. protected virtual void UpdateMac(IMac mac)
  326. {
  327. lock (this)
  328. {
  329. CheckAlive();
  330. mac.BlockUpdate(m_data, 0, m_data.Length);
  331. }
  332. }
  333. }
  334. }
  335. #pragma warning restore
  336. #endif