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.
 
 
 
 
 
 

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