TlsImplUtilities.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl
  6. {
  7. /// <summary>Useful utility methods.</summary>
  8. public abstract class TlsImplUtilities
  9. {
  10. public static bool IsSsl(TlsCryptoParameters cryptoParams)
  11. {
  12. return cryptoParams.ServerVersion.IsSsl;
  13. }
  14. public static bool IsTlsV10(ProtocolVersion version)
  15. {
  16. return ProtocolVersion.TLSv10.IsEqualOrEarlierVersionOf(version.GetEquivalentTlsVersion());
  17. }
  18. public static bool IsTlsV10(TlsCryptoParameters cryptoParams)
  19. {
  20. return IsTlsV10(cryptoParams.ServerVersion);
  21. }
  22. public static bool IsTlsV11(ProtocolVersion version)
  23. {
  24. return ProtocolVersion.TLSv11.IsEqualOrEarlierVersionOf(version.GetEquivalentTlsVersion());
  25. }
  26. public static bool IsTlsV11(TlsCryptoParameters cryptoParams)
  27. {
  28. return IsTlsV11(cryptoParams.ServerVersion);
  29. }
  30. public static bool IsTlsV12(ProtocolVersion version)
  31. {
  32. return ProtocolVersion.TLSv12.IsEqualOrEarlierVersionOf(version.GetEquivalentTlsVersion());
  33. }
  34. public static bool IsTlsV12(TlsCryptoParameters cryptoParams)
  35. {
  36. return IsTlsV12(cryptoParams.ServerVersion);
  37. }
  38. public static bool IsTlsV13(ProtocolVersion version)
  39. {
  40. return ProtocolVersion.TLSv13.IsEqualOrEarlierVersionOf(version.GetEquivalentTlsVersion());
  41. }
  42. public static bool IsTlsV13(TlsCryptoParameters cryptoParams)
  43. {
  44. return IsTlsV13(cryptoParams.ServerVersion);
  45. }
  46. public static byte[] CalculateKeyBlock(TlsCryptoParameters cryptoParams, int length)
  47. {
  48. SecurityParameters securityParameters = cryptoParams.SecurityParameters;
  49. TlsSecret master_secret = securityParameters.MasterSecret;
  50. int prfAlgorithm = securityParameters.PrfAlgorithm;
  51. byte[] seed = Arrays.Concatenate(securityParameters.ServerRandom, securityParameters.ClientRandom);
  52. return master_secret.DeriveUsingPrf(prfAlgorithm, ExporterLabel.key_expansion, seed, length).Extract();
  53. }
  54. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  55. public static void CalculateKeyBlock(TlsCryptoParameters cryptoParams, Span<byte> keyBlock)
  56. {
  57. SecurityParameters securityParameters = cryptoParams.SecurityParameters;
  58. TlsSecret master_secret = securityParameters.MasterSecret;
  59. int prfAlgorithm = securityParameters.PrfAlgorithm;
  60. Span<byte> cr = securityParameters.ClientRandom, sr = securityParameters.ServerRandom;
  61. Span<byte> seed = stackalloc byte[sr.Length + cr.Length];
  62. sr.CopyTo(seed);
  63. cr.CopyTo(seed[sr.Length..]);
  64. TlsSecret derived = master_secret.DeriveUsingPrf(prfAlgorithm, ExporterLabel.key_expansion, seed,
  65. keyBlock.Length);
  66. derived.ExtractTo(keyBlock);
  67. }
  68. #endif
  69. }
  70. }
  71. #pragma warning restore
  72. #endif