s2n_renegotiate.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "tls/s2n_renegotiate.h"
  16. #include "error/s2n_errno.h"
  17. #include "stuffer/s2n_stuffer.h"
  18. #include "tls/s2n_connection.h"
  19. #include "utils/s2n_safety.h"
  20. /* We don't want to introduce a new blocked status for renegotiation
  21. * because that would potentially require applications to update their
  22. * blocked status handling logic for no reason.
  23. *
  24. * It is impossible to use both early data and renegotiation for the same handshake,
  25. * and both are just application data received during the handshake.
  26. * Therefore, we will alias S2N_BLOCKED_ON_EARLY_DATA for reuse with renegotiation.
  27. */
  28. const s2n_blocked_status S2N_BLOCKED_ON_APPLICATION_DATA = S2N_BLOCKED_ON_EARLY_DATA;
  29. S2N_RESULT s2n_renegotiate_validate(struct s2n_connection *conn)
  30. {
  31. RESULT_ENSURE_REF(conn);
  32. RESULT_ENSURE(conn->mode == S2N_CLIENT, S2N_ERR_NO_RENEGOTIATION);
  33. RESULT_ENSURE(conn->secure_renegotiation, S2N_ERR_NO_RENEGOTIATION);
  34. RESULT_ENSURE(conn->handshake.renegotiation, S2N_ERR_INVALID_STATE);
  35. RESULT_ENSURE(!conn->ktls_send_enabled, S2N_ERR_KTLS_RENEG);
  36. RESULT_ENSURE(!conn->ktls_recv_enabled, S2N_ERR_KTLS_RENEG);
  37. return S2N_RESULT_OK;
  38. }
  39. /*
  40. * Prepare a connection to be reused for a second handshake.
  41. *
  42. * s2n-tls was not originally designed to support renegotiation.
  43. * s2n_connection is a very large structure with configuration fields set by the application
  44. * mixed in with internal state fields set by the handshake.
  45. * Ensuring all existing internal state fields (and any new fields added) are safe to reuse for
  46. * a renegotiated handshake would be extremely prone to errors.
  47. * For safety, we instead wipe the entire connection and only restore fields we know we need
  48. * in order to continue sending and receiving encrypted data.
  49. *
  50. * Any configuration fields set by the application will need to be set by the application again.
  51. */
  52. int s2n_renegotiate_wipe(struct s2n_connection *conn)
  53. {
  54. POSIX_ENSURE_REF(conn);
  55. /* We use this method to reset both clients and servers when testing.
  56. * However, outside of tests, it should only be called for client connections
  57. * because we only support renegotiation for clients.
  58. */
  59. POSIX_ENSURE(conn->mode == S2N_CLIENT || s2n_in_unit_test(), S2N_ERR_NO_RENEGOTIATION);
  60. /* Best effort check for pending input or output data.
  61. * This method should not be called until the application has stopped sending and receiving.
  62. * Saving partial read or parital write state would complicate this problem.
  63. */
  64. POSIX_ENSURE(s2n_stuffer_data_available(&conn->header_in) == 0, S2N_ERR_INVALID_STATE);
  65. POSIX_ENSURE(s2n_stuffer_data_available(&conn->in) == 0, S2N_ERR_INVALID_STATE);
  66. POSIX_ENSURE(s2n_stuffer_data_available(&conn->out) == 0, S2N_ERR_INVALID_STATE);
  67. /* Save the crypto parameters.
  68. * We need to continue encrypting / decrypting with the old secure parameters.
  69. */
  70. DEFER_CLEANUP(struct s2n_crypto_parameters *secure_crypto_params = conn->secure,
  71. s2n_crypto_parameters_free);
  72. conn->secure = NULL;
  73. /* Save the fragment length so we continue properly fragmenting our records
  74. * until a new fragment length is chosen.
  75. */
  76. uint16_t max_frag_len = conn->max_outgoing_fragment_length;
  77. /* Save the protocol versions.
  78. * Various checks when sending and receiving records rely on the protocol version. */
  79. uint8_t actual_protocol_version = conn->actual_protocol_version;
  80. uint8_t server_protocol_version = conn->server_protocol_version;
  81. uint8_t client_protocol_version = conn->client_protocol_version;
  82. POSIX_ENSURE(actual_protocol_version < S2N_TLS13, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
  83. /* Save byte tracking.
  84. * This isn't strictly necessary, but potentially useful. */
  85. uint64_t wire_bytes_in = conn->wire_bytes_in;
  86. uint64_t wire_bytes_out = conn->wire_bytes_out;
  87. /* Save io settings */
  88. bool send_managed = conn->managed_send_io;
  89. s2n_send_fn *send_fn = conn->send;
  90. void *send_ctx = conn->send_io_context;
  91. bool recv_managed = conn->managed_recv_io;
  92. s2n_recv_fn *recv_fn = conn->recv;
  93. void *recv_ctx = conn->recv_io_context;
  94. /* Treat IO as unmanaged, since we don't want to clean it up yet */
  95. conn->managed_send_io = false;
  96. conn->managed_recv_io = false;
  97. /* Save the secure_renegotiation flag.
  98. * This flag should always be true, since we don't support insecure renegotiation,
  99. * but copying its value seems safer than just setting it to 'true'.
  100. */
  101. bool secure_renegotiation = conn->secure_renegotiation;
  102. POSIX_ENSURE(secure_renegotiation, S2N_ERR_NO_RENEGOTIATION);
  103. /* Save the finished data.
  104. * This is required for the renegotiate_info extension on the new handshake.
  105. */
  106. uint8_t finished_len = conn->handshake.finished_len;
  107. uint8_t client_finished[sizeof(conn->handshake.client_finished)] = { 0 };
  108. POSIX_CHECKED_MEMCPY(client_finished, conn->handshake.client_finished, finished_len);
  109. uint8_t server_finished[sizeof(conn->handshake.server_finished)] = { 0 };
  110. POSIX_CHECKED_MEMCPY(server_finished, conn->handshake.server_finished, finished_len);
  111. POSIX_GUARD(s2n_connection_wipe(conn));
  112. /* Setup the new crypto parameters.
  113. * The new handshake will negotiate new secure crypto parameters,
  114. * so the current secure crypto parameters become the initial crypto parameters.
  115. */
  116. POSIX_GUARD_RESULT(s2n_crypto_parameters_free(&conn->initial));
  117. conn->initial = secure_crypto_params;
  118. ZERO_TO_DISABLE_DEFER_CLEANUP(secure_crypto_params);
  119. conn->client = conn->initial;
  120. conn->server = conn->initial;
  121. /* Restore saved values */
  122. POSIX_GUARD_RESULT(s2n_connection_set_max_fragment_length(conn, max_frag_len));
  123. POSIX_CHECKED_MEMCPY(conn->handshake.client_finished, client_finished, finished_len);
  124. POSIX_CHECKED_MEMCPY(conn->handshake.server_finished, server_finished, finished_len);
  125. conn->handshake.finished_len = finished_len;
  126. conn->actual_protocol_version = actual_protocol_version;
  127. conn->server_protocol_version = server_protocol_version;
  128. conn->client_protocol_version = client_protocol_version;
  129. conn->wire_bytes_in = wire_bytes_in;
  130. conn->wire_bytes_out = wire_bytes_out;
  131. conn->managed_send_io = send_managed;
  132. conn->send = send_fn;
  133. conn->send_io_context = send_ctx;
  134. conn->managed_recv_io = recv_managed;
  135. conn->recv = recv_fn;
  136. conn->recv_io_context = recv_ctx;
  137. conn->secure_renegotiation = secure_renegotiation;
  138. conn->handshake.renegotiation = true;
  139. return S2N_SUCCESS;
  140. }
  141. static S2N_RESULT s2n_renegotiate_read_app_data(struct s2n_connection *conn, uint8_t *app_data_buf, ssize_t app_data_buf_size,
  142. ssize_t *app_data_size, s2n_blocked_status *blocked)
  143. {
  144. RESULT_ENSURE_REF(blocked);
  145. ssize_t r = s2n_recv(conn, app_data_buf, app_data_buf_size, blocked);
  146. RESULT_GUARD_POSIX(r);
  147. *app_data_size = r;
  148. *blocked = S2N_BLOCKED_ON_APPLICATION_DATA;
  149. RESULT_BAIL(S2N_ERR_APP_DATA_BLOCKED);
  150. }
  151. int s2n_renegotiate(struct s2n_connection *conn, uint8_t *app_data_buf, ssize_t app_data_buf_size,
  152. ssize_t *app_data_size, s2n_blocked_status *blocked)
  153. {
  154. POSIX_GUARD_RESULT(s2n_renegotiate_validate(conn));
  155. POSIX_ENSURE_REF(app_data_size);
  156. *app_data_size = 0;
  157. /* If there is outstanding application data, pass it back to the application.
  158. * We can't read the next handshake record until we drain the buffer.
  159. */
  160. if (s2n_peek(conn) > 0) {
  161. POSIX_GUARD_RESULT(s2n_renegotiate_read_app_data(conn,
  162. app_data_buf, app_data_buf_size, app_data_size, blocked));
  163. }
  164. int result = s2n_negotiate(conn, blocked);
  165. /* If we encounter application data while reading handshake records,
  166. * pass it back to the application.
  167. */
  168. if (result != S2N_SUCCESS && s2n_errno == S2N_ERR_APP_DATA_BLOCKED) {
  169. POSIX_GUARD_RESULT(s2n_renegotiate_read_app_data(conn,
  170. app_data_buf, app_data_buf_size, app_data_size, blocked));
  171. }
  172. return result;
  173. }