ssl_versions.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* Copyright (c) 2017, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/ssl.h>
  15. #include <assert.h>
  16. #include <openssl/bytestring.h>
  17. #include <openssl/err.h>
  18. #include "internal.h"
  19. #include "../crypto/internal.h"
  20. namespace bssl {
  21. bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version) {
  22. switch (version) {
  23. case SSL3_VERSION:
  24. case TLS1_VERSION:
  25. case TLS1_1_VERSION:
  26. case TLS1_2_VERSION:
  27. *out = version;
  28. return true;
  29. case TLS1_3_DRAFT_VERSION:
  30. case TLS1_3_DRAFT21_VERSION:
  31. case TLS1_3_DRAFT22_VERSION:
  32. case TLS1_3_EXPERIMENT_VERSION:
  33. case TLS1_3_EXPERIMENT2_VERSION:
  34. case TLS1_3_EXPERIMENT3_VERSION:
  35. *out = TLS1_3_VERSION;
  36. return true;
  37. case DTLS1_VERSION:
  38. // DTLS 1.0 is analogous to TLS 1.1, not TLS 1.0.
  39. *out = TLS1_1_VERSION;
  40. return true;
  41. case DTLS1_2_VERSION:
  42. *out = TLS1_2_VERSION;
  43. return true;
  44. default:
  45. return false;
  46. }
  47. }
  48. // The follow arrays are the supported versions for TLS and DTLS, in order of
  49. // decreasing preference.
  50. static const uint16_t kTLSVersions[] = {
  51. TLS1_3_DRAFT22_VERSION,
  52. TLS1_3_EXPERIMENT3_VERSION,
  53. TLS1_3_EXPERIMENT2_VERSION,
  54. TLS1_3_EXPERIMENT_VERSION,
  55. TLS1_3_DRAFT_VERSION,
  56. TLS1_3_DRAFT21_VERSION,
  57. TLS1_2_VERSION,
  58. TLS1_1_VERSION,
  59. TLS1_VERSION,
  60. SSL3_VERSION,
  61. };
  62. static const uint16_t kDTLSVersions[] = {
  63. DTLS1_2_VERSION,
  64. DTLS1_VERSION,
  65. };
  66. static void get_method_versions(const SSL_PROTOCOL_METHOD *method,
  67. const uint16_t **out, size_t *out_num) {
  68. if (method->is_dtls) {
  69. *out = kDTLSVersions;
  70. *out_num = OPENSSL_ARRAY_SIZE(kDTLSVersions);
  71. } else {
  72. *out = kTLSVersions;
  73. *out_num = OPENSSL_ARRAY_SIZE(kTLSVersions);
  74. }
  75. }
  76. static bool method_supports_version(const SSL_PROTOCOL_METHOD *method,
  77. uint16_t version) {
  78. const uint16_t *versions;
  79. size_t num_versions;
  80. get_method_versions(method, &versions, &num_versions);
  81. for (size_t i = 0; i < num_versions; i++) {
  82. if (versions[i] == version) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. // The following functions map between API versions and wire versions. The
  89. // public API works on wire versions, except that TLS 1.3 draft versions all
  90. // appear as TLS 1.3. This will get collapsed back down when TLS 1.3 is
  91. // finalized.
  92. static const char *ssl_version_to_string(uint16_t version) {
  93. switch (version) {
  94. case TLS1_3_DRAFT_VERSION:
  95. case TLS1_3_DRAFT21_VERSION:
  96. case TLS1_3_DRAFT22_VERSION:
  97. case TLS1_3_EXPERIMENT_VERSION:
  98. case TLS1_3_EXPERIMENT2_VERSION:
  99. case TLS1_3_EXPERIMENT3_VERSION:
  100. return "TLSv1.3";
  101. case TLS1_2_VERSION:
  102. return "TLSv1.2";
  103. case TLS1_1_VERSION:
  104. return "TLSv1.1";
  105. case TLS1_VERSION:
  106. return "TLSv1";
  107. case SSL3_VERSION:
  108. return "SSLv3";
  109. case DTLS1_VERSION:
  110. return "DTLSv1";
  111. case DTLS1_2_VERSION:
  112. return "DTLSv1.2";
  113. default:
  114. return "unknown";
  115. }
  116. }
  117. static uint16_t wire_version_to_api(uint16_t version) {
  118. switch (version) {
  119. // Report TLS 1.3 draft versions as TLS 1.3 in the public API.
  120. case TLS1_3_DRAFT_VERSION:
  121. case TLS1_3_DRAFT21_VERSION:
  122. case TLS1_3_DRAFT22_VERSION:
  123. case TLS1_3_EXPERIMENT_VERSION:
  124. case TLS1_3_EXPERIMENT2_VERSION:
  125. case TLS1_3_EXPERIMENT3_VERSION:
  126. return TLS1_3_VERSION;
  127. default:
  128. return version;
  129. }
  130. }
  131. // api_version_to_wire maps |version| to some representative wire version. In
  132. // particular, it picks an arbitrary TLS 1.3 representative. This should only be
  133. // used in context where that does not matter.
  134. static bool api_version_to_wire(uint16_t *out, uint16_t version) {
  135. if (version == TLS1_3_DRAFT_VERSION ||
  136. version == TLS1_3_DRAFT21_VERSION ||
  137. version == TLS1_3_DRAFT22_VERSION ||
  138. version == TLS1_3_EXPERIMENT_VERSION ||
  139. version == TLS1_3_EXPERIMENT2_VERSION ||
  140. version == TLS1_3_EXPERIMENT3_VERSION) {
  141. return false;
  142. }
  143. if (version == TLS1_3_VERSION) {
  144. version = TLS1_3_DRAFT_VERSION;
  145. }
  146. // Check it is a real protocol version.
  147. uint16_t unused;
  148. if (!ssl_protocol_version_from_wire(&unused, version)) {
  149. return false;
  150. }
  151. *out = version;
  152. return true;
  153. }
  154. static bool set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  155. uint16_t version) {
  156. if (!api_version_to_wire(&version, version) ||
  157. !method_supports_version(method, version) ||
  158. !ssl_protocol_version_from_wire(out, version)) {
  159. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
  160. return false;
  161. }
  162. return true;
  163. }
  164. static bool set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  165. uint16_t version) {
  166. // Zero is interpreted as the default minimum version.
  167. if (version == 0) {
  168. // SSL 3.0 is disabled by default and TLS 1.0 does not exist in DTLS.
  169. *out = method->is_dtls ? TLS1_1_VERSION : TLS1_VERSION;
  170. return true;
  171. }
  172. return set_version_bound(method, out, version);
  173. }
  174. static bool set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  175. uint16_t version) {
  176. // Zero is interpreted as the default maximum version.
  177. if (version == 0) {
  178. *out = TLS1_2_VERSION;
  179. return true;
  180. }
  181. return set_version_bound(method, out, version);
  182. }
  183. const struct {
  184. uint16_t version;
  185. uint32_t flag;
  186. } kProtocolVersions[] = {
  187. {SSL3_VERSION, SSL_OP_NO_SSLv3},
  188. {TLS1_VERSION, SSL_OP_NO_TLSv1},
  189. {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
  190. {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
  191. {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
  192. };
  193. bool ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  194. uint16_t *out_max_version) {
  195. // For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
  196. // DTLS 1.0 should be mapped to TLS 1.1.
  197. uint32_t options = ssl->options;
  198. if (SSL_is_dtls(ssl)) {
  199. options &= ~SSL_OP_NO_TLSv1_1;
  200. if (options & SSL_OP_NO_DTLSv1) {
  201. options |= SSL_OP_NO_TLSv1_1;
  202. }
  203. }
  204. uint16_t min_version = ssl->conf_min_version;
  205. uint16_t max_version = ssl->conf_max_version;
  206. // OpenSSL's API for controlling versions entails blacklisting individual
  207. // protocols. This has two problems. First, on the client, the protocol can
  208. // only express a contiguous range of versions. Second, a library consumer
  209. // trying to set a maximum version cannot disable protocol versions that get
  210. // added in a future version of the library.
  211. //
  212. // To account for both of these, OpenSSL interprets the client-side bitmask
  213. // as a min/max range by picking the lowest contiguous non-empty range of
  214. // enabled protocols. Note that this means it is impossible to set a maximum
  215. // version of the higest supported TLS version in a future-proof way.
  216. bool any_enabled = false;
  217. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) {
  218. // Only look at the versions already enabled.
  219. if (min_version > kProtocolVersions[i].version) {
  220. continue;
  221. }
  222. if (max_version < kProtocolVersions[i].version) {
  223. break;
  224. }
  225. if (!(options & kProtocolVersions[i].flag)) {
  226. // The minimum version is the first enabled version.
  227. if (!any_enabled) {
  228. any_enabled = true;
  229. min_version = kProtocolVersions[i].version;
  230. }
  231. continue;
  232. }
  233. // If there is a disabled version after the first enabled one, all versions
  234. // after it are implicitly disabled.
  235. if (any_enabled) {
  236. max_version = kProtocolVersions[i-1].version;
  237. break;
  238. }
  239. }
  240. if (!any_enabled) {
  241. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED);
  242. return false;
  243. }
  244. *out_min_version = min_version;
  245. *out_max_version = max_version;
  246. return true;
  247. }
  248. static uint16_t ssl_version(const SSL *ssl) {
  249. // In early data, we report the predicted version.
  250. if (SSL_in_early_data(ssl) && !ssl->server) {
  251. return ssl->s3->hs->early_session->ssl_version;
  252. }
  253. return ssl->version;
  254. }
  255. uint16_t ssl_protocol_version(const SSL *ssl) {
  256. assert(ssl->s3->have_version);
  257. uint16_t version;
  258. if (!ssl_protocol_version_from_wire(&version, ssl->version)) {
  259. // |ssl->version| will always be set to a valid version.
  260. assert(0);
  261. return 0;
  262. }
  263. return version;
  264. }
  265. bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) {
  266. SSL *const ssl = hs->ssl;
  267. uint16_t protocol_version;
  268. if (!method_supports_version(ssl->method, version) ||
  269. !ssl_protocol_version_from_wire(&protocol_version, version) ||
  270. hs->min_version > protocol_version ||
  271. protocol_version > hs->max_version) {
  272. return false;
  273. }
  274. // TLS 1.3 variants must additionally match |tls13_variant|.
  275. if (protocol_version != TLS1_3_VERSION ||
  276. (ssl->tls13_variant == tls13_experiment &&
  277. version == TLS1_3_EXPERIMENT_VERSION) ||
  278. (ssl->tls13_variant == tls13_experiment2 &&
  279. version == TLS1_3_EXPERIMENT2_VERSION) ||
  280. (ssl->tls13_variant == tls13_experiment3 &&
  281. version == TLS1_3_EXPERIMENT3_VERSION) ||
  282. (ssl->tls13_variant == tls13_draft21 &&
  283. version == TLS1_3_DRAFT21_VERSION) ||
  284. (ssl->tls13_variant == tls13_draft22 &&
  285. version == TLS1_3_DRAFT22_VERSION) ||
  286. (ssl->tls13_variant == tls13_default &&
  287. version == TLS1_3_DRAFT_VERSION)) {
  288. return true;
  289. }
  290. // The server, when not configured at |tls13_default|, should additionally
  291. // enable all variants, except draft-21 which is implemented solely for QUIC
  292. // interop testing and will not be deployed, and draft-22 which will be
  293. // enabled once the draft is finalized and ready to be deployed in Chrome.
  294. // Currently, this is to implement the draft-18 vs. experiments field trials.
  295. // In the future, this will be to transition cleanly to a final draft-22
  296. // which hopefully includes the deployability fixes.
  297. if (ssl->server &&
  298. ssl->tls13_variant != tls13_default &&
  299. version != TLS1_3_DRAFT21_VERSION &&
  300. version != TLS1_3_DRAFT22_VERSION) {
  301. return true;
  302. }
  303. return false;
  304. }
  305. bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) {
  306. const uint16_t *versions;
  307. size_t num_versions;
  308. get_method_versions(hs->ssl->method, &versions, &num_versions);
  309. for (size_t i = 0; i < num_versions; i++) {
  310. if (ssl_supports_version(hs, versions[i]) &&
  311. !CBB_add_u16(cbb, versions[i])) {
  312. return false;
  313. }
  314. }
  315. return true;
  316. }
  317. bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  318. uint16_t *out_version, const CBS *peer_versions) {
  319. const uint16_t *versions;
  320. size_t num_versions;
  321. get_method_versions(hs->ssl->method, &versions, &num_versions);
  322. for (size_t i = 0; i < num_versions; i++) {
  323. if (!ssl_supports_version(hs, versions[i])) {
  324. continue;
  325. }
  326. CBS copy = *peer_versions;
  327. while (CBS_len(&copy) != 0) {
  328. uint16_t version;
  329. if (!CBS_get_u16(&copy, &version)) {
  330. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  331. *out_alert = SSL_AD_DECODE_ERROR;
  332. return false;
  333. }
  334. if (version == versions[i]) {
  335. *out_version = version;
  336. return true;
  337. }
  338. }
  339. }
  340. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  341. *out_alert = SSL_AD_PROTOCOL_VERSION;
  342. return false;
  343. }
  344. bool ssl_is_draft21(uint16_t version) {
  345. return version == TLS1_3_DRAFT21_VERSION || version == TLS1_3_DRAFT22_VERSION;
  346. }
  347. bool ssl_is_draft22(uint16_t version) {
  348. return version == TLS1_3_DRAFT22_VERSION;
  349. }
  350. bool ssl_is_resumption_experiment(uint16_t version) {
  351. return version == TLS1_3_EXPERIMENT_VERSION ||
  352. version == TLS1_3_EXPERIMENT2_VERSION ||
  353. version == TLS1_3_EXPERIMENT3_VERSION ||
  354. version == TLS1_3_DRAFT22_VERSION;
  355. }
  356. bool ssl_is_resumption_variant(enum tls13_variant_t variant) {
  357. return variant == tls13_experiment || variant == tls13_experiment2 ||
  358. variant == tls13_experiment3 || variant == tls13_draft22;
  359. }
  360. bool ssl_is_resumption_client_ccs_experiment(uint16_t version) {
  361. return version == TLS1_3_EXPERIMENT_VERSION ||
  362. version == TLS1_3_EXPERIMENT2_VERSION ||
  363. version == TLS1_3_DRAFT22_VERSION;
  364. }
  365. bool ssl_is_resumption_record_version_experiment(uint16_t version) {
  366. return version == TLS1_3_EXPERIMENT2_VERSION ||
  367. version == TLS1_3_EXPERIMENT3_VERSION ||
  368. version == TLS1_3_DRAFT22_VERSION;
  369. }
  370. } // namespace bssl
  371. using namespace bssl;
  372. int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
  373. return set_min_version(ctx->method, &ctx->conf_min_version, version);
  374. }
  375. int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
  376. return set_max_version(ctx->method, &ctx->conf_max_version, version);
  377. }
  378. int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
  379. return set_min_version(ssl->method, &ssl->conf_min_version, version);
  380. }
  381. int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
  382. return set_max_version(ssl->method, &ssl->conf_max_version, version);
  383. }
  384. int SSL_version(const SSL *ssl) {
  385. return wire_version_to_api(ssl_version(ssl));
  386. }
  387. const char *SSL_get_version(const SSL *ssl) {
  388. return ssl_version_to_string(ssl_version(ssl));
  389. }
  390. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  391. return ssl_version_to_string(session->ssl_version);
  392. }
  393. uint16_t SSL_SESSION_get_protocol_version(const SSL_SESSION *session) {
  394. return wire_version_to_api(session->ssl_version);
  395. }
  396. int SSL_SESSION_set_protocol_version(SSL_SESSION *session, uint16_t version) {
  397. // This picks a representative TLS 1.3 version, but this API should only be
  398. // used on unit test sessions anyway.
  399. return api_version_to_wire(&session->ssl_version, version);
  400. }