You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

404 line
11 KiB

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