s2n_server_hello.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <sys/param.h>
  16. #include <time.h>
  17. #include "api/s2n.h"
  18. #include "crypto/s2n_fips.h"
  19. #include "error/s2n_errno.h"
  20. #include "stuffer/s2n_stuffer.h"
  21. #include "tls/s2n_alerts.h"
  22. #include "tls/s2n_cipher_preferences.h"
  23. #include "tls/s2n_cipher_suites.h"
  24. #include "tls/s2n_connection.h"
  25. #include "tls/s2n_security_policies.h"
  26. #include "tls/s2n_server_extensions.h"
  27. #include "tls/s2n_tls.h"
  28. #include "tls/s2n_tls13.h"
  29. #include "tls/s2n_tls13_handshake.h"
  30. #include "tls/s2n_tls13_key_schedule.h"
  31. #include "utils/s2n_bitmap.h"
  32. #include "utils/s2n_random.h"
  33. #include "utils/s2n_safety.h"
  34. /* From RFC5246 7.4.1.2. */
  35. #define S2N_TLS_COMPRESSION_METHOD_NULL 0
  36. /* From RFC8446 4.1.3. */
  37. #define S2N_DOWNGRADE_PROTECTION_SIZE 8
  38. const uint8_t tls12_downgrade_protection_bytes[] = {
  39. 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x01
  40. };
  41. const uint8_t tls11_downgrade_protection_bytes[] = {
  42. 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44, 0x00
  43. };
  44. static int s2n_random_value_is_hello_retry(struct s2n_connection *conn)
  45. {
  46. POSIX_ENSURE_REF(conn);
  47. POSIX_ENSURE(memcmp(hello_retry_req_random, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN) == 0,
  48. S2N_ERR_INVALID_HELLO_RETRY);
  49. return S2N_SUCCESS;
  50. }
  51. static int s2n_client_detect_downgrade_mechanism(struct s2n_connection *conn)
  52. {
  53. POSIX_ENSURE_REF(conn);
  54. uint8_t *downgrade_bytes = &conn->handshake_params.server_random[S2N_TLS_RANDOM_DATA_LEN - S2N_DOWNGRADE_PROTECTION_SIZE];
  55. /* Detect downgrade attacks according to RFC 8446 section 4.1.3 */
  56. if (conn->client_protocol_version == S2N_TLS13 && conn->server_protocol_version == S2N_TLS12) {
  57. if (s2n_constant_time_equals(downgrade_bytes, tls12_downgrade_protection_bytes, S2N_DOWNGRADE_PROTECTION_SIZE)) {
  58. POSIX_BAIL(S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED);
  59. }
  60. } else if (conn->client_protocol_version == S2N_TLS13 && conn->server_protocol_version <= S2N_TLS11) {
  61. if (s2n_constant_time_equals(downgrade_bytes, tls11_downgrade_protection_bytes, S2N_DOWNGRADE_PROTECTION_SIZE)) {
  62. POSIX_BAIL(S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED);
  63. }
  64. }
  65. return 0;
  66. }
  67. static int s2n_server_add_downgrade_mechanism(struct s2n_connection *conn)
  68. {
  69. POSIX_ENSURE_REF(conn);
  70. uint8_t *downgrade_bytes = &conn->handshake_params.server_random[S2N_TLS_RANDOM_DATA_LEN - S2N_DOWNGRADE_PROTECTION_SIZE];
  71. /* Protect against downgrade attacks according to RFC 8446 section 4.1.3 */
  72. if (conn->server_protocol_version >= S2N_TLS13 && conn->actual_protocol_version == S2N_TLS12) {
  73. /* TLS1.3 servers MUST use a special random value when negotiating TLS1.2 */
  74. POSIX_CHECKED_MEMCPY(downgrade_bytes, tls12_downgrade_protection_bytes, S2N_DOWNGRADE_PROTECTION_SIZE);
  75. } else if (conn->server_protocol_version >= S2N_TLS13 && conn->actual_protocol_version <= S2N_TLS11) {
  76. /* TLS1.3 servers MUST, use a special random value when negotiating TLS1.1 or below */
  77. POSIX_CHECKED_MEMCPY(downgrade_bytes, tls11_downgrade_protection_bytes, S2N_DOWNGRADE_PROTECTION_SIZE);
  78. }
  79. return 0;
  80. }
  81. static int s2n_server_hello_parse(struct s2n_connection *conn)
  82. {
  83. POSIX_ENSURE_REF(conn);
  84. POSIX_ENSURE_REF(conn->secure);
  85. struct s2n_stuffer *in = &conn->handshake.io;
  86. uint8_t compression_method;
  87. uint8_t session_id_len;
  88. uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN];
  89. uint8_t session_id[S2N_TLS_SESSION_ID_MAX_LEN];
  90. POSIX_GUARD(s2n_stuffer_read_bytes(in, protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN));
  91. POSIX_GUARD(s2n_stuffer_read_bytes(in, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN));
  92. uint8_t legacy_version = (uint8_t) (protocol_version[0] * 10) + protocol_version[1];
  93. /**
  94. *= https://tools.ietf.org/rfc/rfc8446#4.1.3
  95. *# Upon receiving a message with type server_hello, implementations MUST
  96. *# first examine the Random value and, if it matches this value, process
  97. *# it as described in Section 4.1.4).
  98. **/
  99. if (s2n_random_value_is_hello_retry(conn) == S2N_SUCCESS) {
  100. /**
  101. *= https://tools.ietf.org/rfc/rfc8446#4.1.4
  102. *# If a client receives a second
  103. *# HelloRetryRequest in the same connection (i.e., where the ClientHello
  104. *# was itself in response to a HelloRetryRequest), it MUST abort the
  105. *# handshake with an "unexpected_message" alert.
  106. **/
  107. POSIX_ENSURE(!s2n_is_hello_retry_handshake(conn), S2N_ERR_INVALID_HELLO_RETRY);
  108. /**
  109. *= https://tools.ietf.org/rfc/rfc8446#4.1.4
  110. *# Upon receipt of a HelloRetryRequest, the client MUST check the
  111. *# legacy_version
  112. **/
  113. POSIX_ENSURE(legacy_version == S2N_TLS12, S2N_ERR_INVALID_HELLO_RETRY);
  114. POSIX_GUARD(s2n_set_hello_retry_required(conn));
  115. }
  116. POSIX_GUARD(s2n_stuffer_read_uint8(in, &session_id_len));
  117. S2N_ERROR_IF(session_id_len > S2N_TLS_SESSION_ID_MAX_LEN, S2N_ERR_BAD_MESSAGE);
  118. POSIX_GUARD(s2n_stuffer_read_bytes(in, session_id, session_id_len));
  119. uint8_t *cipher_suite_wire = s2n_stuffer_raw_read(in, S2N_TLS_CIPHER_SUITE_LEN);
  120. POSIX_ENSURE_REF(cipher_suite_wire);
  121. POSIX_GUARD(s2n_stuffer_read_uint8(in, &compression_method));
  122. /**
  123. *= https://tools.ietf.org/rfc/rfc8446#4.1.3
  124. *# legacy_compression_method: A single byte which MUST have the
  125. *# value 0.
  126. *
  127. *= https://tools.ietf.org/rfc/rfc8446#4.1.4
  128. *# Upon receipt of a HelloRetryRequest, the client MUST check the
  129. *# legacy_version, legacy_session_id_echo, cipher_suite, and
  130. *# legacy_compression_method
  131. **/
  132. S2N_ERROR_IF(compression_method != S2N_TLS_COMPRESSION_METHOD_NULL, S2N_ERR_BAD_MESSAGE);
  133. bool session_ids_match = session_id_len != 0 && session_id_len == conn->session_id_len
  134. && memcmp(session_id, conn->session_id, session_id_len) == 0;
  135. if (!session_ids_match) {
  136. conn->ems_negotiated = false;
  137. }
  138. POSIX_GUARD(s2n_server_extensions_recv(conn, in));
  139. /**
  140. *= https://tools.ietf.org/rfc/rfc8446#4.1.4
  141. *# The server's extensions MUST contain "supported_versions".
  142. **/
  143. if (s2n_is_hello_retry_message(conn)) {
  144. s2n_extension_type_id supported_versions_id = s2n_unsupported_extension;
  145. POSIX_GUARD(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_SUPPORTED_VERSIONS, &supported_versions_id));
  146. POSIX_ENSURE(S2N_CBIT_TEST(conn->extension_responses_received, supported_versions_id),
  147. S2N_ERR_MISSING_EXTENSION);
  148. }
  149. if (conn->server_protocol_version >= S2N_TLS13) {
  150. POSIX_ENSURE(!conn->handshake.renegotiation, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
  151. /**
  152. *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.3
  153. *# A client which
  154. *# receives a legacy_session_id_echo field that does not match what
  155. *# it sent in the ClientHello MUST abort the handshake with an
  156. *# "illegal_parameter" alert.
  157. *
  158. *= https://tools.ietf.org/rfc/rfc8446#4.1.4
  159. *# Upon receipt of a HelloRetryRequest, the client MUST check the
  160. *# legacy_version, legacy_session_id_echo
  161. **/
  162. POSIX_ENSURE(session_ids_match || (session_id_len == 0 && conn->session_id_len == 0), S2N_ERR_BAD_MESSAGE);
  163. conn->actual_protocol_version = conn->server_protocol_version;
  164. POSIX_GUARD(s2n_set_cipher_as_client(conn, cipher_suite_wire));
  165. /* Erase TLS 1.2 client session ticket which might have been set for session resumption */
  166. POSIX_GUARD(s2n_free(&conn->client_ticket));
  167. } else {
  168. conn->server_protocol_version = legacy_version;
  169. POSIX_ENSURE(!s2n_client_detect_downgrade_mechanism(conn), S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED);
  170. POSIX_ENSURE(!s2n_connection_is_quic_enabled(conn), S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
  171. /* Hello retries are only supported in >=TLS1.3. */
  172. POSIX_ENSURE(!s2n_is_hello_retry_handshake(conn), S2N_ERR_BAD_MESSAGE);
  173. /*
  174. *= https://tools.ietf.org/rfc/rfc8446#appendix-D.3
  175. *# A client that attempts to send 0-RTT data MUST fail a connection if
  176. *# it receives a ServerHello with TLS 1.2 or older.
  177. */
  178. POSIX_ENSURE(conn->early_data_state != S2N_EARLY_DATA_REQUESTED, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
  179. const struct s2n_security_policy *security_policy;
  180. POSIX_GUARD(s2n_connection_get_security_policy(conn, &security_policy));
  181. if (conn->server_protocol_version < security_policy->minimum_protocol_version
  182. || conn->server_protocol_version > conn->client_protocol_version) {
  183. POSIX_GUARD(s2n_queue_reader_unsupported_protocol_version_alert(conn));
  184. POSIX_BAIL(S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
  185. }
  186. conn->actual_protocol_version = MIN(conn->server_protocol_version, conn->client_protocol_version);
  187. /*
  188. *= https://tools.ietf.org/rfc/rfc5077#section-3.4
  189. *# If the server accepts the ticket
  190. *# and the Session ID is not empty, then it MUST respond with the same
  191. *# Session ID present in the ClientHello. This allows the client to
  192. *# easily differentiate when the server is resuming a session from when
  193. *# it is falling back to a full handshake.
  194. */
  195. if (session_ids_match) {
  196. /* check if the resumed session state is valid */
  197. POSIX_ENSURE(conn->resume_protocol_version == conn->actual_protocol_version, S2N_ERR_BAD_MESSAGE);
  198. POSIX_ENSURE(memcmp(conn->secure->cipher_suite->iana_value, cipher_suite_wire, S2N_TLS_CIPHER_SUITE_LEN) == 0,
  199. S2N_ERR_BAD_MESSAGE);
  200. /* Session is resumed */
  201. conn->client_session_resumed = 1;
  202. } else {
  203. conn->session_id_len = session_id_len;
  204. POSIX_CHECKED_MEMCPY(conn->session_id, session_id, session_id_len);
  205. POSIX_GUARD(s2n_set_cipher_as_client(conn, cipher_suite_wire));
  206. /* Erase master secret which might have been set for session resumption */
  207. POSIX_CHECKED_MEMSET((uint8_t *) conn->secrets.version.tls12.master_secret, 0, S2N_TLS_SECRET_LEN);
  208. /* Erase client session ticket which might have been set for session resumption */
  209. POSIX_GUARD(s2n_free(&conn->client_ticket));
  210. }
  211. }
  212. /* If it is not possible to accept early data on this connection
  213. * (for example, because no PSK was negotiated) we need to reject early data now.
  214. * Otherwise, early data logic may make certain invalid assumptions about the
  215. * state of the connection (for example, that the prf is the early data prf).
  216. */
  217. POSIX_GUARD_RESULT(s2n_early_data_accept_or_reject(conn));
  218. if (conn->early_data_state == S2N_EARLY_DATA_REJECTED) {
  219. POSIX_GUARD_RESULT(s2n_tls13_key_schedule_reset(conn));
  220. }
  221. return 0;
  222. }
  223. int s2n_server_hello_recv(struct s2n_connection *conn)
  224. {
  225. POSIX_ENSURE_REF(conn);
  226. /* Read the message off the wire */
  227. POSIX_GUARD(s2n_server_hello_parse(conn));
  228. conn->actual_protocol_version_established = 1;
  229. POSIX_GUARD(s2n_conn_set_handshake_type(conn));
  230. /* If this is a HelloRetryRequest, we don't process the ServerHello.
  231. * Instead we proceed with retry logic. */
  232. if (s2n_is_hello_retry_message(conn)) {
  233. POSIX_GUARD(s2n_server_hello_retry_recv(conn));
  234. return 0;
  235. }
  236. if (conn->actual_protocol_version < S2N_TLS13 && s2n_connection_is_session_resumed(conn)) {
  237. POSIX_GUARD(s2n_prf_key_expansion(conn));
  238. }
  239. /* Update the required hashes for this connection */
  240. POSIX_GUARD(s2n_conn_update_required_handshake_hashes(conn));
  241. return 0;
  242. }
  243. int s2n_server_hello_write_message(struct s2n_connection *conn)
  244. {
  245. POSIX_ENSURE_REF(conn);
  246. POSIX_ENSURE_REF(conn->secure);
  247. /* The actual_protocol_version is set while processing the CLIENT_HELLO message, so
  248. * it could be S2N_TLS13. SERVER_HELLO should always respond with the legacy version.
  249. * https://tools.ietf.org/html/rfc8446#section-4.1.3 */
  250. const uint16_t legacy_protocol_version = MIN(conn->actual_protocol_version, S2N_TLS12);
  251. uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN];
  252. protocol_version[0] = (uint8_t) (legacy_protocol_version / 10);
  253. protocol_version[1] = (uint8_t) (legacy_protocol_version % 10);
  254. POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN));
  255. POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN));
  256. POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, conn->session_id_len));
  257. POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, conn->session_id, conn->session_id_len));
  258. POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, conn->secure->cipher_suite->iana_value, S2N_TLS_CIPHER_SUITE_LEN));
  259. POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, S2N_TLS_COMPRESSION_METHOD_NULL));
  260. return 0;
  261. }
  262. int s2n_server_hello_send(struct s2n_connection *conn)
  263. {
  264. POSIX_ENSURE_REF(conn);
  265. struct s2n_stuffer server_random = { 0 };
  266. struct s2n_blob b = { 0 };
  267. POSIX_GUARD(s2n_blob_init(&b, conn->handshake_params.server_random, S2N_TLS_RANDOM_DATA_LEN));
  268. /* Create the server random data */
  269. POSIX_GUARD(s2n_stuffer_init(&server_random, &b));
  270. struct s2n_blob rand_data = { 0 };
  271. POSIX_GUARD(s2n_blob_init(&rand_data, s2n_stuffer_raw_write(&server_random, S2N_TLS_RANDOM_DATA_LEN), S2N_TLS_RANDOM_DATA_LEN));
  272. POSIX_ENSURE_REF(rand_data.data);
  273. POSIX_GUARD_RESULT(s2n_get_public_random_data(&rand_data));
  274. /* Add a downgrade detection mechanism if required */
  275. POSIX_GUARD(s2n_server_add_downgrade_mechanism(conn));
  276. POSIX_GUARD(s2n_server_hello_write_message(conn));
  277. POSIX_GUARD(s2n_server_extensions_send(conn, &conn->handshake.io));
  278. conn->actual_protocol_version_established = 1;
  279. return 0;
  280. }