TlsImplUtilities.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.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. byte[] seed = Arrays.Concatenate(securityParameters.ServerRandom, securityParameters.ClientRandom);
  51. return Prf(securityParameters, master_secret, ExporterLabel.key_expansion, seed, length).Extract();
  52. }
  53. public static TlsSecret Prf(SecurityParameters securityParameters, TlsSecret secret, string asciiLabel,
  54. byte[] seed, int length)
  55. {
  56. return secret.DeriveUsingPrf(securityParameters.PrfAlgorithm, asciiLabel, seed, length);
  57. }
  58. public static TlsSecret Prf(TlsCryptoParameters cryptoParams, TlsSecret secret, string asciiLabel, byte[] seed,
  59. int length)
  60. {
  61. return Prf(cryptoParams.SecurityParameters, secret, asciiLabel, seed, length);
  62. }
  63. }
  64. }
  65. #pragma warning restore
  66. #endif