ssl_versions.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_DRAFT23_VERSION:
  30. *out = TLS1_3_VERSION;
  31. return true;
  32. case DTLS1_VERSION:
  33. // DTLS 1.0 is analogous to TLS 1.1, not TLS 1.0.
  34. *out = TLS1_1_VERSION;
  35. return true;
  36. case DTLS1_2_VERSION:
  37. *out = TLS1_2_VERSION;
  38. return true;
  39. default:
  40. return false;
  41. }
  42. }
  43. // The follow arrays are the supported versions for TLS and DTLS, in order of
  44. // decreasing preference.
  45. static const uint16_t kTLSVersions[] = {
  46. TLS1_3_DRAFT23_VERSION,
  47. TLS1_2_VERSION,
  48. TLS1_1_VERSION,
  49. TLS1_VERSION,
  50. SSL3_VERSION,
  51. };
  52. static const uint16_t kDTLSVersions[] = {
  53. DTLS1_2_VERSION,
  54. DTLS1_VERSION,
  55. };
  56. static void get_method_versions(const SSL_PROTOCOL_METHOD *method,
  57. const uint16_t **out, size_t *out_num) {
  58. if (method->is_dtls) {
  59. *out = kDTLSVersions;
  60. *out_num = OPENSSL_ARRAY_SIZE(kDTLSVersions);
  61. } else {
  62. *out = kTLSVersions;
  63. *out_num = OPENSSL_ARRAY_SIZE(kTLSVersions);
  64. }
  65. }
  66. static bool method_supports_version(const SSL_PROTOCOL_METHOD *method,
  67. uint16_t version) {
  68. const uint16_t *versions;
  69. size_t num_versions;
  70. get_method_versions(method, &versions, &num_versions);
  71. for (size_t i = 0; i < num_versions; i++) {
  72. if (versions[i] == version) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. // The following functions map between API versions and wire versions. The
  79. // public API works on wire versions, except that TLS 1.3 draft versions all
  80. // appear as TLS 1.3. This will get collapsed back down when TLS 1.3 is
  81. // finalized.
  82. static const char *ssl_version_to_string(uint16_t version) {
  83. switch (version) {
  84. case TLS1_3_DRAFT23_VERSION:
  85. return "TLSv1.3";
  86. case TLS1_2_VERSION:
  87. return "TLSv1.2";
  88. case TLS1_1_VERSION:
  89. return "TLSv1.1";
  90. case TLS1_VERSION:
  91. return "TLSv1";
  92. case SSL3_VERSION:
  93. return "SSLv3";
  94. case DTLS1_VERSION:
  95. return "DTLSv1";
  96. case DTLS1_2_VERSION:
  97. return "DTLSv1.2";
  98. default:
  99. return "unknown";
  100. }
  101. }
  102. static uint16_t wire_version_to_api(uint16_t version) {
  103. switch (version) {
  104. // Report TLS 1.3 draft versions as TLS 1.3 in the public API.
  105. case TLS1_3_DRAFT23_VERSION:
  106. return TLS1_3_VERSION;
  107. default:
  108. return version;
  109. }
  110. }
  111. // api_version_to_wire maps |version| to some representative wire version. In
  112. // particular, it picks an arbitrary TLS 1.3 representative. This should only be
  113. // used in context where that does not matter.
  114. static bool api_version_to_wire(uint16_t *out, uint16_t version) {
  115. if (version == TLS1_3_DRAFT23_VERSION) {
  116. return false;
  117. }
  118. if (version == TLS1_3_VERSION) {
  119. version = TLS1_3_DRAFT23_VERSION;
  120. }
  121. // Check it is a real protocol version.
  122. uint16_t unused;
  123. if (!ssl_protocol_version_from_wire(&unused, version)) {
  124. return false;
  125. }
  126. *out = version;
  127. return true;
  128. }
  129. static bool set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  130. uint16_t version) {
  131. if (!api_version_to_wire(&version, version) ||
  132. !method_supports_version(method, version) ||
  133. !ssl_protocol_version_from_wire(out, version)) {
  134. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
  135. return false;
  136. }
  137. return true;
  138. }
  139. static bool set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  140. uint16_t version) {
  141. // Zero is interpreted as the default minimum version.
  142. if (version == 0) {
  143. // SSL 3.0 is disabled by default and TLS 1.0 does not exist in DTLS.
  144. *out = method->is_dtls ? TLS1_1_VERSION : TLS1_VERSION;
  145. return true;
  146. }
  147. return set_version_bound(method, out, version);
  148. }
  149. static bool set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  150. uint16_t version) {
  151. // Zero is interpreted as the default maximum version.
  152. if (version == 0) {
  153. *out = TLS1_2_VERSION;
  154. return true;
  155. }
  156. return set_version_bound(method, out, version);
  157. }
  158. const struct {
  159. uint16_t version;
  160. uint32_t flag;
  161. } kProtocolVersions[] = {
  162. {SSL3_VERSION, SSL_OP_NO_SSLv3},
  163. {TLS1_VERSION, SSL_OP_NO_TLSv1},
  164. {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
  165. {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
  166. {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
  167. };
  168. bool ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  169. uint16_t *out_max_version) {
  170. // For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
  171. // DTLS 1.0 should be mapped to TLS 1.1.
  172. uint32_t options = ssl->options;
  173. if (SSL_is_dtls(ssl)) {
  174. options &= ~SSL_OP_NO_TLSv1_1;
  175. if (options & SSL_OP_NO_DTLSv1) {
  176. options |= SSL_OP_NO_TLSv1_1;
  177. }
  178. }
  179. uint16_t min_version = ssl->conf_min_version;
  180. uint16_t max_version = ssl->conf_max_version;
  181. // OpenSSL's API for controlling versions entails blacklisting individual
  182. // protocols. This has two problems. First, on the client, the protocol can
  183. // only express a contiguous range of versions. Second, a library consumer
  184. // trying to set a maximum version cannot disable protocol versions that get
  185. // added in a future version of the library.
  186. //
  187. // To account for both of these, OpenSSL interprets the client-side bitmask
  188. // as a min/max range by picking the lowest contiguous non-empty range of
  189. // enabled protocols. Note that this means it is impossible to set a maximum
  190. // version of the higest supported TLS version in a future-proof way.
  191. bool any_enabled = false;
  192. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) {
  193. // Only look at the versions already enabled.
  194. if (min_version > kProtocolVersions[i].version) {
  195. continue;
  196. }
  197. if (max_version < kProtocolVersions[i].version) {
  198. break;
  199. }
  200. if (!(options & kProtocolVersions[i].flag)) {
  201. // The minimum version is the first enabled version.
  202. if (!any_enabled) {
  203. any_enabled = true;
  204. min_version = kProtocolVersions[i].version;
  205. }
  206. continue;
  207. }
  208. // If there is a disabled version after the first enabled one, all versions
  209. // after it are implicitly disabled.
  210. if (any_enabled) {
  211. max_version = kProtocolVersions[i-1].version;
  212. break;
  213. }
  214. }
  215. if (!any_enabled) {
  216. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED);
  217. return false;
  218. }
  219. *out_min_version = min_version;
  220. *out_max_version = max_version;
  221. return true;
  222. }
  223. static uint16_t ssl_version(const SSL *ssl) {
  224. // In early data, we report the predicted version.
  225. if (SSL_in_early_data(ssl) && !ssl->server) {
  226. return ssl->s3->hs->early_session->ssl_version;
  227. }
  228. return ssl->version;
  229. }
  230. uint16_t ssl_protocol_version(const SSL *ssl) {
  231. assert(ssl->s3->have_version);
  232. uint16_t version;
  233. if (!ssl_protocol_version_from_wire(&version, ssl->version)) {
  234. // |ssl->version| will always be set to a valid version.
  235. assert(0);
  236. return 0;
  237. }
  238. return version;
  239. }
  240. bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) {
  241. SSL *const ssl = hs->ssl;
  242. uint16_t protocol_version;
  243. if (!method_supports_version(ssl->method, version) ||
  244. !ssl_protocol_version_from_wire(&protocol_version, version) ||
  245. hs->min_version > protocol_version ||
  246. protocol_version > hs->max_version) {
  247. return false;
  248. }
  249. // This logic is part of the TLS 1.3 variants mechanism used in TLS 1.3
  250. // experimentation. Although we currently only have one variant, TLS 1.3 does
  251. // not a final stable deployment yet, so leave the logic in place for now.
  252. if (protocol_version != TLS1_3_VERSION ||
  253. (ssl->tls13_variant == tls13_default &&
  254. version == TLS1_3_DRAFT23_VERSION)) {
  255. return true;
  256. }
  257. // The server, when not configured at |tls13_default|, should additionally
  258. // enable all variants.
  259. if (ssl->server && ssl->tls13_variant != tls13_default) {
  260. return true;
  261. }
  262. return false;
  263. }
  264. bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) {
  265. const uint16_t *versions;
  266. size_t num_versions;
  267. get_method_versions(hs->ssl->method, &versions, &num_versions);
  268. for (size_t i = 0; i < num_versions; i++) {
  269. if (ssl_supports_version(hs, versions[i]) &&
  270. !CBB_add_u16(cbb, versions[i])) {
  271. return false;
  272. }
  273. }
  274. return true;
  275. }
  276. bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  277. uint16_t *out_version, const CBS *peer_versions) {
  278. const uint16_t *versions;
  279. size_t num_versions;
  280. get_method_versions(hs->ssl->method, &versions, &num_versions);
  281. for (size_t i = 0; i < num_versions; i++) {
  282. if (!ssl_supports_version(hs, versions[i])) {
  283. continue;
  284. }
  285. CBS copy = *peer_versions;
  286. while (CBS_len(&copy) != 0) {
  287. uint16_t version;
  288. if (!CBS_get_u16(&copy, &version)) {
  289. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  290. *out_alert = SSL_AD_DECODE_ERROR;
  291. return false;
  292. }
  293. if (version == versions[i]) {
  294. *out_version = version;
  295. return true;
  296. }
  297. }
  298. }
  299. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  300. *out_alert = SSL_AD_PROTOCOL_VERSION;
  301. return false;
  302. }
  303. } // namespace bssl
  304. using namespace bssl;
  305. int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
  306. return set_min_version(ctx->method, &ctx->conf_min_version, version);
  307. }
  308. int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
  309. return set_max_version(ctx->method, &ctx->conf_max_version, version);
  310. }
  311. int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
  312. return set_min_version(ssl->method, &ssl->conf_min_version, version);
  313. }
  314. int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
  315. return set_max_version(ssl->method, &ssl->conf_max_version, version);
  316. }
  317. int SSL_version(const SSL *ssl) {
  318. return wire_version_to_api(ssl_version(ssl));
  319. }
  320. const char *SSL_get_version(const SSL *ssl) {
  321. return ssl_version_to_string(ssl_version(ssl));
  322. }
  323. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  324. return ssl_version_to_string(session->ssl_version);
  325. }
  326. uint16_t SSL_SESSION_get_protocol_version(const SSL_SESSION *session) {
  327. return wire_version_to_api(session->ssl_version);
  328. }
  329. int SSL_SESSION_set_protocol_version(SSL_SESSION *session, uint16_t version) {
  330. // This picks a representative TLS 1.3 version, but this API should only be
  331. // used on unit test sessions anyway.
  332. return api_version_to_wire(&session->ssl_version, version);
  333. }