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.
 
 
 
 
 
 

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