您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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