ProtocolVersion.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  7. {
  8. public sealed class ProtocolVersion
  9. {
  10. public static readonly ProtocolVersion SSLv3 = new ProtocolVersion(0x0300, "SSL 3.0");
  11. public static readonly ProtocolVersion TLSv10 = new ProtocolVersion(0x0301, "TLS 1.0");
  12. public static readonly ProtocolVersion TLSv11 = new ProtocolVersion(0x0302, "TLS 1.1");
  13. public static readonly ProtocolVersion TLSv12 = new ProtocolVersion(0x0303, "TLS 1.2");
  14. public static readonly ProtocolVersion DTLSv10 = new ProtocolVersion(0xFEFF, "DTLS 1.0");
  15. public static readonly ProtocolVersion DTLSv12 = new ProtocolVersion(0xFEFD, "DTLS 1.2");
  16. private readonly int version;
  17. private readonly String name;
  18. private ProtocolVersion(int v, String name)
  19. {
  20. this.version = v & 0xffff;
  21. this.name = name;
  22. }
  23. public int FullVersion
  24. {
  25. get { return version; }
  26. }
  27. public int MajorVersion
  28. {
  29. get { return version >> 8; }
  30. }
  31. public int MinorVersion
  32. {
  33. get { return version & 0xff; }
  34. }
  35. public bool IsDtls
  36. {
  37. get { return MajorVersion == 0xFE; }
  38. }
  39. public bool IsSsl
  40. {
  41. get { return this == SSLv3; }
  42. }
  43. public bool IsTls
  44. {
  45. get { return MajorVersion == 0x03; }
  46. }
  47. public ProtocolVersion GetEquivalentTLSVersion()
  48. {
  49. if (!IsDtls)
  50. {
  51. return this;
  52. }
  53. if (this == DTLSv10)
  54. {
  55. return TLSv11;
  56. }
  57. return TLSv12;
  58. }
  59. public bool IsEqualOrEarlierVersionOf(ProtocolVersion version)
  60. {
  61. if (MajorVersion != version.MajorVersion)
  62. {
  63. return false;
  64. }
  65. int diffMinorVersion = version.MinorVersion - MinorVersion;
  66. return IsDtls ? diffMinorVersion <= 0 : diffMinorVersion >= 0;
  67. }
  68. public bool IsLaterVersionOf(ProtocolVersion version)
  69. {
  70. if (MajorVersion != version.MajorVersion)
  71. {
  72. return false;
  73. }
  74. int diffMinorVersion = version.MinorVersion - MinorVersion;
  75. return IsDtls ? diffMinorVersion > 0 : diffMinorVersion < 0;
  76. }
  77. public override bool Equals(object other)
  78. {
  79. return this == other || (other is ProtocolVersion && Equals((ProtocolVersion)other));
  80. }
  81. public bool Equals(ProtocolVersion other)
  82. {
  83. return other != null && this.version == other.version;
  84. }
  85. public override int GetHashCode()
  86. {
  87. return version;
  88. }
  89. /// <exception cref="IOException"/>
  90. public static ProtocolVersion Get(int major, int minor)
  91. {
  92. switch (major)
  93. {
  94. case 0x03:
  95. {
  96. switch (minor)
  97. {
  98. case 0x00:
  99. return SSLv3;
  100. case 0x01:
  101. return TLSv10;
  102. case 0x02:
  103. return TLSv11;
  104. case 0x03:
  105. return TLSv12;
  106. }
  107. return GetUnknownVersion(major, minor, "TLS");
  108. }
  109. case 0xFE:
  110. {
  111. switch (minor)
  112. {
  113. case 0xFF:
  114. return DTLSv10;
  115. case 0xFE:
  116. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  117. case 0xFD:
  118. return DTLSv12;
  119. }
  120. return GetUnknownVersion(major, minor, "DTLS");
  121. }
  122. default:
  123. {
  124. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  125. }
  126. }
  127. }
  128. public override string ToString()
  129. {
  130. return name;
  131. }
  132. private static ProtocolVersion GetUnknownVersion(int major, int minor, string prefix)
  133. {
  134. TlsUtilities.CheckUint8(major);
  135. TlsUtilities.CheckUint8(minor);
  136. int v = (major << 8) | minor;
  137. String hex = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(Convert.ToString(0x10000 | v, 16).Substring(1));
  138. return new ProtocolVersion(v, prefix + " 0x" + hex);
  139. }
  140. }
  141. }
  142. #pragma warning restore
  143. #endif