ProtocolVersion.cs 13 KB

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