Debian Codegraph @

crt/s2n/tls/s2n_renegotiate.c

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