Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

407 rader
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 TLS1_VERSION:
  24. case TLS1_1_VERSION:
  25. case TLS1_2_VERSION:
  26. *out = version;
  27. return true;
  28. case TLS1_3_DRAFT23_VERSION:
  29. case TLS1_3_DRAFT28_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_DRAFT28_VERSION,
  47. TLS1_3_DRAFT23_VERSION,
  48. TLS1_2_VERSION,
  49. TLS1_1_VERSION,
  50. TLS1_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. bool ssl_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. case TLS1_3_DRAFT28_VERSION:
  86. return "TLSv1.3";
  87. case TLS1_2_VERSION:
  88. return "TLSv1.2";
  89. case TLS1_1_VERSION:
  90. return "TLSv1.1";
  91. case TLS1_VERSION:
  92. return "TLSv1";
  93. case DTLS1_VERSION:
  94. return "DTLSv1";
  95. case DTLS1_2_VERSION:
  96. return "DTLSv1.2";
  97. default:
  98. return "unknown";
  99. }
  100. }
  101. static uint16_t wire_version_to_api(uint16_t version) {
  102. switch (version) {
  103. // Report TLS 1.3 draft versions as TLS 1.3 in the public API.
  104. case TLS1_3_DRAFT23_VERSION:
  105. case TLS1_3_DRAFT28_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. version == TLS1_3_DRAFT28_VERSION) {
  117. return false;
  118. }
  119. if (version == TLS1_3_VERSION) {
  120. version = TLS1_3_DRAFT23_VERSION;
  121. }
  122. // Check it is a real protocol version.
  123. uint16_t unused;
  124. if (!ssl_protocol_version_from_wire(&unused, version)) {
  125. return false;
  126. }
  127. *out = version;
  128. return true;
  129. }
  130. static bool set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  131. uint16_t version) {
  132. if (!api_version_to_wire(&version, version) ||
  133. !ssl_method_supports_version(method, version) ||
  134. !ssl_protocol_version_from_wire(out, version)) {
  135. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
  136. return false;
  137. }
  138. return true;
  139. }
  140. static bool set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  141. uint16_t version) {
  142. // Zero is interpreted as the default minimum version.
  143. if (version == 0) {
  144. // TLS 1.0 does not exist in DTLS.
  145. *out = method->is_dtls ? TLS1_1_VERSION : TLS1_VERSION;
  146. return true;
  147. }
  148. return set_version_bound(method, out, version);
  149. }
  150. static bool set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  151. uint16_t version) {
  152. // Zero is interpreted as the default maximum version.
  153. if (version == 0) {
  154. *out = TLS1_2_VERSION;
  155. return true;
  156. }
  157. return set_version_bound(method, out, version);
  158. }
  159. const struct {
  160. uint16_t version;
  161. uint32_t flag;
  162. } kProtocolVersions[] = {
  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_HANDSHAKE *hs, 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 = hs->ssl->options;
  173. if (SSL_is_dtls(hs->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 = hs->config->conf_min_version;
  180. uint16_t max_version = hs->config->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 (!ssl_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. // If the TLS 1.3 variant is set to |tls13_default|, all variants are enabled,
  250. // otherwise only the matching version is enabled.
  251. if (protocol_version == TLS1_3_VERSION) {
  252. switch (ssl->tls13_variant) {
  253. case tls13_draft23:
  254. return version == TLS1_3_DRAFT23_VERSION;
  255. case tls13_draft28:
  256. return version == TLS1_3_DRAFT28_VERSION;
  257. case tls13_default:
  258. return true;
  259. }
  260. }
  261. return true;
  262. }
  263. bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) {
  264. const uint16_t *versions;
  265. size_t num_versions;
  266. get_method_versions(hs->ssl->method, &versions, &num_versions);
  267. for (size_t i = 0; i < num_versions; i++) {
  268. if (ssl_supports_version(hs, versions[i]) &&
  269. !CBB_add_u16(cbb, versions[i])) {
  270. return false;
  271. }
  272. }
  273. return true;
  274. }
  275. bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  276. uint16_t *out_version, const CBS *peer_versions) {
  277. const uint16_t *versions;
  278. size_t num_versions;
  279. get_method_versions(hs->ssl->method, &versions, &num_versions);
  280. for (size_t i = 0; i < num_versions; i++) {
  281. if (!ssl_supports_version(hs, versions[i])) {
  282. continue;
  283. }
  284. CBS copy = *peer_versions;
  285. while (CBS_len(&copy) != 0) {
  286. uint16_t version;
  287. if (!CBS_get_u16(&copy, &version)) {
  288. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  289. *out_alert = SSL_AD_DECODE_ERROR;
  290. return false;
  291. }
  292. if (version == versions[i]) {
  293. *out_version = version;
  294. return true;
  295. }
  296. }
  297. }
  298. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  299. *out_alert = SSL_AD_PROTOCOL_VERSION;
  300. return false;
  301. }
  302. bool ssl_is_draft28(uint16_t version) {
  303. return version == TLS1_3_DRAFT28_VERSION;
  304. }
  305. } // namespace bssl
  306. using namespace bssl;
  307. int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
  308. return set_min_version(ctx->method, &ctx->conf_min_version, version);
  309. }
  310. int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
  311. return set_max_version(ctx->method, &ctx->conf_max_version, version);
  312. }
  313. int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
  314. if (!ssl->config) {
  315. return 0;
  316. }
  317. return set_min_version(ssl->method, &ssl->config->conf_min_version, version);
  318. }
  319. int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
  320. if (!ssl->config) {
  321. return 0;
  322. }
  323. return set_max_version(ssl->method, &ssl->config->conf_max_version, version);
  324. }
  325. int SSL_version(const SSL *ssl) {
  326. return wire_version_to_api(ssl_version(ssl));
  327. }
  328. const char *SSL_get_version(const SSL *ssl) {
  329. return ssl_version_to_string(ssl_version(ssl));
  330. }
  331. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  332. return ssl_version_to_string(session->ssl_version);
  333. }
  334. uint16_t SSL_SESSION_get_protocol_version(const SSL_SESSION *session) {
  335. return wire_version_to_api(session->ssl_version);
  336. }
  337. int SSL_SESSION_set_protocol_version(SSL_SESSION *session, uint16_t version) {
  338. // This picks a representative TLS 1.3 version, but this API should only be
  339. // used on unit test sessions anyway.
  340. return api_version_to_wire(&session->ssl_version, version);
  341. }