ProtocolVersion.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.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 TLSv13 = new ProtocolVersion(0x0304, "TLS 1.3");
  15. public static readonly ProtocolVersion DTLSv10 = new ProtocolVersion(0xFEFF, "DTLS 1.0");
  16. public static readonly ProtocolVersion DTLSv12 = new ProtocolVersion(0xFEFD, "DTLS 1.2");
  17. internal static readonly ProtocolVersion CLIENT_EARLIEST_SUPPORTED_DTLS = DTLSv10;
  18. internal static readonly ProtocolVersion CLIENT_EARLIEST_SUPPORTED_TLS = SSLv3;
  19. internal static readonly ProtocolVersion CLIENT_LATEST_SUPPORTED_DTLS = DTLSv12;
  20. internal static readonly ProtocolVersion CLIENT_LATEST_SUPPORTED_TLS = TLSv13;
  21. internal static readonly ProtocolVersion SERVER_EARLIEST_SUPPORTED_DTLS = DTLSv10;
  22. internal static readonly ProtocolVersion SERVER_EARLIEST_SUPPORTED_TLS = SSLv3;
  23. internal static readonly ProtocolVersion SERVER_LATEST_SUPPORTED_DTLS = DTLSv12;
  24. internal static readonly ProtocolVersion SERVER_LATEST_SUPPORTED_TLS = TLSv13;
  25. public static bool Contains(ProtocolVersion[] versions, ProtocolVersion version)
  26. {
  27. if (versions != null && version != null)
  28. {
  29. for (int i = 0; i < versions.Length; ++i)
  30. {
  31. if (version.Equals(versions[i]))
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. public static ProtocolVersion GetEarliestDtls(ProtocolVersion[] versions)
  38. {
  39. ProtocolVersion earliest = null;
  40. if (null != versions)
  41. {
  42. for (int i = 0; i < versions.Length; ++i)
  43. {
  44. ProtocolVersion next = versions[i];
  45. if (null != next && next.IsDtls)
  46. {
  47. if (null == earliest || next.MinorVersion > earliest.MinorVersion)
  48. {
  49. earliest = next;
  50. }
  51. }
  52. }
  53. }
  54. return earliest;
  55. }
  56. public static ProtocolVersion GetEarliestTls(ProtocolVersion[] versions)
  57. {
  58. ProtocolVersion earliest = null;
  59. if (null != versions)
  60. {
  61. for (int i = 0; i < versions.Length; ++i)
  62. {
  63. ProtocolVersion next = versions[i];
  64. if (null != next && next.IsTls)
  65. {
  66. if (null == earliest || next.MinorVersion < earliest.MinorVersion)
  67. {
  68. earliest = next;
  69. }
  70. }
  71. }
  72. }
  73. return earliest;
  74. }
  75. public static ProtocolVersion GetLatestDtls(ProtocolVersion[] versions)
  76. {
  77. ProtocolVersion latest = null;
  78. if (null != versions)
  79. {
  80. for (int i = 0; i < versions.Length; ++i)
  81. {
  82. ProtocolVersion next = versions[i];
  83. if (null != next && next.IsDtls)
  84. {
  85. if (null == latest || next.MinorVersion < latest.MinorVersion)
  86. {
  87. latest = next;
  88. }
  89. }
  90. }
  91. }
  92. return latest;
  93. }
  94. public static ProtocolVersion GetLatestTls(ProtocolVersion[] versions)
  95. {
  96. ProtocolVersion latest = null;
  97. if (null != versions)
  98. {
  99. for (int i = 0; i < versions.Length; ++i)
  100. {
  101. ProtocolVersion next = versions[i];
  102. if (null != next && next.IsTls)
  103. {
  104. if (null == latest || next.MinorVersion > latest.MinorVersion)
  105. {
  106. latest = next;
  107. }
  108. }
  109. }
  110. }
  111. return latest;
  112. }
  113. internal static bool IsSupportedDtlsVersionClient(ProtocolVersion version)
  114. {
  115. return null != version
  116. && version.IsEqualOrLaterVersionOf(CLIENT_EARLIEST_SUPPORTED_DTLS)
  117. && version.IsEqualOrEarlierVersionOf(CLIENT_LATEST_SUPPORTED_DTLS);
  118. }
  119. internal static bool IsSupportedDtlsVersionServer(ProtocolVersion version)
  120. {
  121. return null != version
  122. && version.IsEqualOrLaterVersionOf(SERVER_EARLIEST_SUPPORTED_DTLS)
  123. && version.IsEqualOrEarlierVersionOf(SERVER_LATEST_SUPPORTED_DTLS);
  124. }
  125. internal static bool IsSupportedTlsVersionClient(ProtocolVersion version)
  126. {
  127. if (null == version)
  128. return false;
  129. int fullVersion = version.FullVersion;
  130. return fullVersion >= CLIENT_EARLIEST_SUPPORTED_TLS.FullVersion
  131. && fullVersion <= CLIENT_LATEST_SUPPORTED_TLS.FullVersion;
  132. }
  133. internal static bool IsSupportedTlsVersionServer(ProtocolVersion version)
  134. {
  135. if (null == version)
  136. return false;
  137. int fullVersion = version.FullVersion;
  138. return fullVersion >= SERVER_EARLIEST_SUPPORTED_TLS.FullVersion
  139. && fullVersion <= SERVER_LATEST_SUPPORTED_TLS.FullVersion;
  140. }
  141. private readonly int version;
  142. private readonly string name;
  143. private ProtocolVersion(int v, string name)
  144. {
  145. this.version = v & 0xFFFF;
  146. this.name = name;
  147. }
  148. public ProtocolVersion[] DownTo(ProtocolVersion min)
  149. {
  150. if (!IsEqualOrLaterVersionOf(min))
  151. throw new ArgumentException("must be an equal or earlier version of this one", "min");
  152. IList result = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  153. result.Add(this);
  154. ProtocolVersion current = this;
  155. while (!current.Equals(min))
  156. {
  157. current = current.GetPreviousVersion();
  158. result.Add(current);
  159. }
  160. ProtocolVersion[] versions = new ProtocolVersion[result.Count];
  161. for (int i = 0; i < result.Count; ++i)
  162. {
  163. versions[i] = (ProtocolVersion)result[i];
  164. }
  165. return versions;
  166. }
  167. public int FullVersion
  168. {
  169. get { return version; }
  170. }
  171. public int MajorVersion
  172. {
  173. get { return version >> 8; }
  174. }
  175. public int MinorVersion
  176. {
  177. get { return version & 0xFF; }
  178. }
  179. public string Name
  180. {
  181. get { return name; }
  182. }
  183. public bool IsDtls
  184. {
  185. get { return MajorVersion == 0xFE; }
  186. }
  187. public bool IsSsl
  188. {
  189. get { return this == SSLv3; }
  190. }
  191. public bool IsTls
  192. {
  193. get { return MajorVersion == 0x03; }
  194. }
  195. public ProtocolVersion GetEquivalentTlsVersion()
  196. {
  197. switch (MajorVersion)
  198. {
  199. case 0x03:
  200. return this;
  201. case 0xFE:
  202. switch (MinorVersion)
  203. {
  204. case 0xFF: return TLSv11;
  205. case 0xFD: return TLSv12;
  206. default: return null;
  207. }
  208. default:
  209. return null;
  210. }
  211. }
  212. public ProtocolVersion GetNextVersion()
  213. {
  214. int major = MajorVersion, minor = MinorVersion;
  215. switch (major)
  216. {
  217. case 0x03:
  218. switch (minor)
  219. {
  220. case 0xFF: return null;
  221. default: return Get(major, minor + 1);
  222. }
  223. case 0xFE:
  224. switch (minor)
  225. {
  226. case 0x00: return null;
  227. case 0xFF: return DTLSv12;
  228. default: return Get(major, minor - 1);
  229. }
  230. default:
  231. return null;
  232. }
  233. }
  234. public ProtocolVersion GetPreviousVersion()
  235. {
  236. int major = MajorVersion, minor = MinorVersion;
  237. switch (major)
  238. {
  239. case 0x03:
  240. switch (minor)
  241. {
  242. case 0x00: return null;
  243. default: return Get(major, minor - 1);
  244. }
  245. case 0xFE:
  246. switch (minor)
  247. {
  248. case 0xFF: return null;
  249. case 0xFD: return DTLSv10;
  250. default: return Get(major, minor + 1);
  251. }
  252. default:
  253. return null;
  254. }
  255. }
  256. public bool IsEarlierVersionOf(ProtocolVersion version)
  257. {
  258. if (null == version || MajorVersion != version.MajorVersion)
  259. return false;
  260. int diffMinorVersion = MinorVersion - version.MinorVersion;
  261. return IsDtls ? diffMinorVersion > 0 : diffMinorVersion < 0;
  262. }
  263. public bool IsEqualOrEarlierVersionOf(ProtocolVersion version)
  264. {
  265. if (null == version || MajorVersion != version.MajorVersion)
  266. return false;
  267. int diffMinorVersion = MinorVersion - version.MinorVersion;
  268. return IsDtls ? diffMinorVersion >= 0 : diffMinorVersion <= 0;
  269. }
  270. public bool IsEqualOrLaterVersionOf(ProtocolVersion version)
  271. {
  272. if (null == version || MajorVersion != version.MajorVersion)
  273. return false;
  274. int diffMinorVersion = MinorVersion - version.MinorVersion;
  275. return IsDtls ? diffMinorVersion <= 0 : diffMinorVersion >= 0;
  276. }
  277. public bool IsLaterVersionOf(ProtocolVersion version)
  278. {
  279. if (null == version || MajorVersion != version.MajorVersion)
  280. return false;
  281. int diffMinorVersion = MinorVersion - version.MinorVersion;
  282. return IsDtls ? diffMinorVersion < 0 : diffMinorVersion > 0;
  283. }
  284. public override bool Equals(object other)
  285. {
  286. return this == other || (other is ProtocolVersion && Equals((ProtocolVersion)other));
  287. }
  288. public bool Equals(ProtocolVersion other)
  289. {
  290. return other != null && this.version == other.version;
  291. }
  292. public override int GetHashCode()
  293. {
  294. return version;
  295. }
  296. public static ProtocolVersion Get(int major, int minor)
  297. {
  298. switch (major)
  299. {
  300. case 0x03:
  301. {
  302. switch (minor)
  303. {
  304. case 0x00:
  305. return SSLv3;
  306. case 0x01:
  307. return TLSv10;
  308. case 0x02:
  309. return TLSv11;
  310. case 0x03:
  311. return TLSv12;
  312. case 0x04:
  313. return TLSv13;
  314. }
  315. return GetUnknownVersion(major, minor, "TLS");
  316. }
  317. case 0xFE:
  318. {
  319. switch (minor)
  320. {
  321. case 0xFF:
  322. return DTLSv10;
  323. case 0xFE:
  324. throw new ArgumentException("{0xFE, 0xFE} is a reserved protocol version");
  325. case 0xFD:
  326. return DTLSv12;
  327. }
  328. return GetUnknownVersion(major, minor, "DTLS");
  329. }
  330. default:
  331. {
  332. return GetUnknownVersion(major, minor, "UNKNOWN");
  333. }
  334. }
  335. }
  336. public ProtocolVersion[] Only()
  337. {
  338. return new ProtocolVersion[]{ this };
  339. }
  340. public override string ToString()
  341. {
  342. return name;
  343. }
  344. private static void CheckUint8(int versionOctet)
  345. {
  346. if (!TlsUtilities.IsValidUint8(versionOctet))
  347. throw new ArgumentException("not a valid octet", "versionOctet");
  348. }
  349. private static ProtocolVersion GetUnknownVersion(int major, int minor, string prefix)
  350. {
  351. CheckUint8(major);
  352. CheckUint8(minor);
  353. int v = (major << 8) | minor;
  354. string hex = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(Convert.ToString(0x10000 | v, 16).Substring(1));
  355. return new ProtocolVersion(v, prefix + " 0x" + hex);
  356. }
  357. }
  358. }
  359. #pragma warning restore
  360. #endif