ProtocolVersion.cs 4.7 KB

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