ssl_versions.cc 11 KB

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