Branch data Line data Source code
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_client_hello.h"
17 : :
18 : : #include <stdint.h>
19 : : #include <stdlib.h>
20 : : #include <time.h>
21 : :
22 : : #include "api/unstable/fingerprint.h"
23 : : #include "crypto/s2n_fips.h"
24 : : #include "crypto/s2n_hash.h"
25 : : #include "crypto/s2n_pq.h"
26 : : #include "error/s2n_errno.h"
27 : : #include "stuffer/s2n_stuffer.h"
28 : : #include "tls/extensions/s2n_client_server_name.h"
29 : : #include "tls/extensions/s2n_client_supported_groups.h"
30 : : #include "tls/extensions/s2n_extension_list.h"
31 : : #include "tls/extensions/s2n_server_key_share.h"
32 : : #include "tls/s2n_alerts.h"
33 : : #include "tls/s2n_auth_selection.h"
34 : : #include "tls/s2n_cipher_preferences.h"
35 : : #include "tls/s2n_cipher_suites.h"
36 : : #include "tls/s2n_connection.h"
37 : : #include "tls/s2n_handshake_type.h"
38 : : #include "tls/s2n_security_policies.h"
39 : : #include "tls/s2n_signature_algorithms.h"
40 : : #include "tls/s2n_tls.h"
41 : : #include "utils/s2n_bitmap.h"
42 : : #include "utils/s2n_random.h"
43 : : #include "utils/s2n_safety.h"
44 : :
45 : : struct s2n_client_hello *s2n_connection_get_client_hello(struct s2n_connection *conn)
46 : 175 : {
47 [ # # ][ - + ]: 175 : PTR_ENSURE_REF(conn);
48 [ + + ]: 175 : if (conn->client_hello.parsed != 1) {
49 : 3 : return NULL;
50 : 3 : }
51 : :
52 : 172 : return &conn->client_hello;
53 : 175 : }
54 : :
55 : : static uint32_t min_size(struct s2n_blob *blob, uint32_t max_length)
56 : 44 : {
57 [ + + ]: 44 : return blob->size < max_length ? blob->size : max_length;
58 : 44 : }
59 : :
60 : : static S2N_RESULT s2n_generate_client_session_id(struct s2n_connection *conn)
61 : 7885 : {
62 [ - + ][ # # ]: 7885 : RESULT_ENSURE_REF(conn);
63 [ - + ][ # # ]: 7885 : RESULT_ENSURE_REF(conn->config);
64 : :
65 : : /* Session id already generated - no-op */
66 [ + + ]: 7885 : if (conn->session_id_len) {
67 : 654 : return S2N_RESULT_OK;
68 : 654 : }
69 : :
70 : : /* Only generate the session id if using tickets */
71 : 7231 : bool generate = conn->config->use_tickets;
72 : :
73 : : /* TLS1.3 doesn't require session ids. The field is actually renamed to legacy_session_id.
74 : : * However, we still set a session id if dealing with troublesome middleboxes
75 : : * (middlebox compatibility mode) or if trying to use a TLS1.2 ticket.
76 : : */
77 [ + + ]: 7231 : if (conn->client_protocol_version >= S2N_TLS13) {
78 [ + + ][ + + ]: 5373 : generate = s2n_is_middlebox_compat_enabled(conn) || conn->resume_protocol_version;
79 : 5373 : }
80 : :
81 : : /* Session id not needed - no-op */
82 [ + + ]: 7231 : if (!generate) {
83 : 2056 : return S2N_RESULT_OK;
84 : 2056 : }
85 : :
86 : : /* QUIC should not allow session ids for any reason.
87 : : *
88 : : *= https://www.rfc-editor.org/rfc/rfc9001#section-8.4
89 : : *# A server SHOULD treat the receipt of a TLS ClientHello with a non-empty
90 : : *# legacy_session_id field as a connection error of type PROTOCOL_VIOLATION.
91 : : */
92 [ + + ][ + - ]: 5175 : RESULT_ENSURE(!conn->quic_enabled, S2N_ERR_UNSUPPORTED_WITH_QUIC);
93 : :
94 : 5174 : struct s2n_blob session_id = { 0 };
95 [ - + ]: 5174 : RESULT_GUARD_POSIX(s2n_blob_init(&session_id, conn->session_id, S2N_TLS_SESSION_ID_MAX_LEN));
96 [ - + ]: 5174 : RESULT_GUARD(s2n_get_public_random_data(&session_id));
97 : 5174 : conn->session_id_len = S2N_TLS_SESSION_ID_MAX_LEN;
98 : 5174 : return S2N_RESULT_OK;
99 : 5174 : }
100 : :
101 : : ssize_t s2n_client_hello_get_raw_message_length(struct s2n_client_hello *ch)
102 : 3 : {
103 [ + - ][ + + ]: 3 : POSIX_ENSURE_REF(ch);
104 : :
105 : 2 : return ch->raw_message.size;
106 : 3 : }
107 : :
108 : : ssize_t s2n_client_hello_get_raw_message(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length)
109 : 3 : {
110 [ + - ][ + + ]: 3 : POSIX_ENSURE_REF(ch);
111 [ # # ][ - + ]: 2 : POSIX_ENSURE_REF(out);
112 : :
113 : 2 : uint32_t len = min_size(&ch->raw_message, max_length);
114 [ - + ][ # # ]: 2 : POSIX_CHECKED_MEMCPY(out, ch->raw_message.data, len);
[ + - ]
115 : 2 : return len;
116 : 2 : }
117 : :
118 : : ssize_t s2n_client_hello_get_cipher_suites_length(struct s2n_client_hello *ch)
119 : 3 : {
120 [ + + ][ + - ]: 3 : POSIX_ENSURE_REF(ch);
121 : :
122 : 2 : return ch->cipher_suites.size;
123 : 3 : }
124 : :
125 : : int s2n_client_hello_cb_done(struct s2n_connection *conn)
126 : 28 : {
127 [ - + ][ # # ]: 28 : POSIX_ENSURE_REF(conn);
128 [ - + ][ # # ]: 28 : POSIX_ENSURE_REF(conn->config);
129 [ - + ][ # # ]: 28 : POSIX_ENSURE(conn->config->client_hello_cb_mode == S2N_CLIENT_HELLO_CB_NONBLOCKING, S2N_ERR_INVALID_STATE);
130 [ - + ][ # # ]: 28 : POSIX_ENSURE(conn->client_hello.callback_invoked == 1, S2N_ERR_ASYNC_NOT_PERFORMED);
131 [ - + ][ # # ]: 28 : POSIX_ENSURE(conn->client_hello.parsed == 1, S2N_ERR_INVALID_STATE);
132 : :
133 : 28 : conn->client_hello.callback_async_blocked = 0;
134 : 28 : conn->client_hello.callback_async_done = 1;
135 : :
136 : 28 : return S2N_SUCCESS;
137 : 28 : }
138 : :
139 : : ssize_t s2n_client_hello_get_cipher_suites(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length)
140 : 3 : {
141 [ + + ][ + - ]: 3 : POSIX_ENSURE_REF(ch);
142 [ - + ][ # # ]: 2 : POSIX_ENSURE_REF(out);
143 [ - + ][ # # ]: 2 : POSIX_ENSURE_REF(ch->cipher_suites.data);
144 : :
145 : 2 : uint32_t len = min_size(&ch->cipher_suites, max_length);
146 : :
147 [ # # ][ - + ]: 2 : POSIX_CHECKED_MEMCPY(out, ch->cipher_suites.data, len);
[ + - ]
148 : :
149 : 2 : return len;
150 : 2 : }
151 : :
152 : : ssize_t s2n_client_hello_get_extensions_length(struct s2n_client_hello *ch)
153 : 3 : {
154 [ + + ][ + - ]: 3 : POSIX_ENSURE_REF(ch);
155 : :
156 : 2 : return ch->extensions.raw.size;
157 : 3 : }
158 : :
159 : : ssize_t s2n_client_hello_get_extensions(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length)
160 : 3 : {
161 [ + + ][ + - ]: 3 : POSIX_ENSURE_REF(ch);
162 [ - + ][ # # ]: 2 : POSIX_ENSURE_REF(out);
163 [ # # ][ - + ]: 2 : POSIX_ENSURE_REF(ch->extensions.raw.data);
164 : :
165 : 2 : uint32_t len = min_size(&ch->extensions.raw, max_length);
166 : :
167 [ - + ][ # # ]: 2 : POSIX_CHECKED_MEMCPY(out, ch->extensions.raw.data, len);
[ + - ]
168 : :
169 : 2 : return len;
170 : 2 : }
171 : :
172 : : int s2n_client_hello_get_random(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length)
173 : 5 : {
174 [ + + ][ + - ]: 5 : POSIX_ENSURE_REF(ch);
175 [ + + ][ + - ]: 4 : POSIX_ENSURE_REF(out);
176 : :
177 [ + + ][ + - ]: 3 : POSIX_ENSURE(max_length >= S2N_TLS_RANDOM_DATA_LEN, S2N_ERR_INSUFFICIENT_MEM_SIZE);
178 [ - + ][ # # ]: 2 : POSIX_CHECKED_MEMCPY(out, ch->random, S2N_TLS_RANDOM_DATA_LEN);
[ + - ]
179 : 2 : return S2N_SUCCESS;
180 : 2 : }
181 : :
182 : : int s2n_client_hello_free_raw_message(struct s2n_client_hello *client_hello)
183 : 135156 : {
184 [ - + ][ # # ]: 135156 : POSIX_ENSURE_REF(client_hello);
185 : :
186 [ - + ]: 135156 : POSIX_GUARD(s2n_free(&client_hello->raw_message));
187 : :
188 : : /* These point to data in the raw_message stuffer,
189 : : so we don't need to free them */
190 : 135156 : client_hello->cipher_suites.data = NULL;
191 : 135156 : client_hello->extensions.raw.data = NULL;
192 : :
193 : 135156 : return 0;
194 : 135156 : }
195 : :
196 : : int s2n_client_hello_free(struct s2n_client_hello **ch)
197 : 212 : {
198 [ + + ][ + - ]: 212 : POSIX_ENSURE_REF(ch);
199 [ + + ]: 211 : if (*ch == NULL) {
200 : 97 : return S2N_SUCCESS;
201 : 97 : }
202 : :
203 [ + - ][ + + ]: 114 : POSIX_ENSURE((*ch)->alloced, S2N_ERR_INVALID_ARGUMENT);
204 [ - + ]: 102 : POSIX_GUARD(s2n_client_hello_free_raw_message(*ch));
205 [ - + ]: 102 : POSIX_GUARD(s2n_free_object((uint8_t **) ch, sizeof(struct s2n_client_hello)));
206 : 102 : *ch = NULL;
207 : 102 : return S2N_SUCCESS;
208 : 102 : }
209 : :
210 : : int s2n_collect_client_hello(struct s2n_client_hello *ch, struct s2n_stuffer *source)
211 : 8029 : {
212 [ - + ][ # # ]: 8029 : POSIX_ENSURE_REF(ch);
213 [ # # ][ - + ]: 8029 : POSIX_ENSURE_REF(source);
214 : :
215 : 8029 : uint32_t size = s2n_stuffer_data_available(source);
216 [ - + ][ # # ]: 8029 : S2N_ERROR_IF(size == 0, S2N_ERR_BAD_MESSAGE);
217 : :
218 [ - + ]: 8029 : POSIX_GUARD(s2n_realloc(&ch->raw_message, size));
219 [ - + ]: 8029 : POSIX_GUARD(s2n_stuffer_read(source, &ch->raw_message));
220 : :
221 : 8029 : return 0;
222 : 8029 : }
223 : :
224 : : static S2N_RESULT s2n_client_hello_verify_for_retry(struct s2n_connection *conn,
225 : : struct s2n_client_hello *old_ch, struct s2n_client_hello *new_ch,
226 : : uint8_t *previous_client_random)
227 : 7922 : {
228 [ - + ][ # # ]: 7922 : RESULT_ENSURE_REF(conn);
229 [ # # ][ - + ]: 7922 : RESULT_ENSURE_REF(old_ch);
230 [ - + ][ # # ]: 7922 : RESULT_ENSURE_REF(new_ch);
231 [ # # ][ - + ]: 7922 : RESULT_ENSURE_REF(previous_client_random);
232 : :
233 [ + + ]: 7922 : if (!s2n_is_hello_retry_handshake(conn)) {
234 : 7280 : return S2N_RESULT_OK;
235 : 7280 : }
236 : :
237 : : /*
238 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.2
239 : : *# The client will also send a
240 : : *# ClientHello when the server has responded to its ClientHello with a
241 : : *# HelloRetryRequest. In that case, the client MUST send the same
242 : : *# ClientHello without modification, except as follows:
243 : : *
244 : : * All of the exceptions that follow are extensions.
245 : : */
246 [ + - ][ + + ]: 642 : RESULT_ENSURE(old_ch->legacy_version == new_ch->legacy_version, S2N_ERR_BAD_MESSAGE);
247 [ - + ][ # # ]: 641 : RESULT_ENSURE(old_ch->compression_methods.size == new_ch->compression_methods.size, S2N_ERR_BAD_MESSAGE);
248 [ + - ][ + + ]: 641 : RESULT_ENSURE(s2n_constant_time_equals(old_ch->compression_methods.data, new_ch->compression_methods.data,
249 : 640 : new_ch->compression_methods.size),
250 : 640 : S2N_ERR_BAD_MESSAGE);
251 : :
252 : : /* Some clients are not compliant with TLS 1.3 RFC, and send mismatching values in their second
253 : : * ClientHello. For increased compatibility, these checks are skipped outside of tests. The
254 : : * checks are still included in tests to ensure the s2n-tls client remains compliant.
255 : : */
256 [ + + ]: 640 : if (s2n_in_test()) {
257 : : /* In the past, the s2n-tls client updated the client random in the second ClientHello
258 : : * which is not allowed by RFC8446: https://github.com/aws/s2n-tls/pull/3311. Although the
259 : : * issue was addressed, its existence means that old versions of the s2n-tls client will
260 : : * fail this validation.
261 : : */
262 [ + + ][ + - ]: 636 : RESULT_ENSURE(s2n_constant_time_equals(
263 : 635 : previous_client_random,
264 : 635 : conn->client_hello.random,
265 : 635 : S2N_TLS_RANDOM_DATA_LEN),
266 : 635 : S2N_ERR_BAD_MESSAGE);
267 : :
268 : : /* Some clients have been found to send a mismatching legacy session ID. */
269 [ # # ][ - + ]: 635 : RESULT_ENSURE(old_ch->session_id.size == new_ch->session_id.size, S2N_ERR_BAD_MESSAGE);
270 [ + + ][ + - ]: 635 : RESULT_ENSURE(s2n_constant_time_equals(old_ch->session_id.data, new_ch->session_id.data,
271 : 634 : new_ch->session_id.size),
272 : 634 : S2N_ERR_BAD_MESSAGE);
273 : :
274 : : /* Some clients have been found to send a mismatching cipher suite list. */
275 [ # # ][ - + ]: 634 : RESULT_ENSURE(old_ch->cipher_suites.size == new_ch->cipher_suites.size, S2N_ERR_BAD_MESSAGE);
276 [ + - ][ + + ]: 634 : RESULT_ENSURE(s2n_constant_time_equals(old_ch->cipher_suites.data, new_ch->cipher_suites.data,
277 : 634 : new_ch->cipher_suites.size),
278 : 634 : S2N_ERR_BAD_MESSAGE);
279 : 634 : }
280 : :
281 : : /*
282 : : * Now enforce that the extensions also exactly match,
283 : : * except for the exceptions described in the RFC.
284 : : */
285 [ + + ]: 13957 : for (size_t i = 0; i < s2n_array_len(s2n_supported_extensions); i++) {
286 : 13323 : s2n_parsed_extension *old_extension = &old_ch->extensions.parsed_extensions[i];
287 : 13323 : uint32_t old_size = old_extension->extension.size;
288 : 13323 : s2n_parsed_extension *new_extension = &new_ch->extensions.parsed_extensions[i];
289 : 13323 : uint32_t new_size = new_extension->extension.size;
290 : :
291 : : /* The extension type is only set if the extension is present.
292 : : * Look for a non-zero-length extension.
293 : : */
294 : 13323 : uint16_t extension_type = 0;
295 [ + + ]: 13323 : if (old_size != 0) {
296 : 4114 : extension_type = old_extension->extension_type;
297 [ + + ]: 9209 : } else if (new_size != 0) {
298 : 38 : extension_type = new_extension->extension_type;
299 : 9171 : } else {
300 : 9171 : continue;
301 : 9171 : }
302 : :
303 : 4152 : switch (extension_type) {
304 : : /*
305 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.2
306 : : *# - If a "key_share" extension was supplied in the HelloRetryRequest,
307 : : *# replacing the list of shares with a list containing a single
308 : : *# KeyShareEntry from the indicated group.
309 : : */
310 [ + + ]: 634 : case TLS_EXTENSION_KEY_SHARE:
311 : : /* Handled when parsing the key share extension */
312 : 634 : break;
313 : : /*
314 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.2
315 : : *# - Removing the "early_data" extension (Section 4.2.10) if one was
316 : : *# present. Early data is not permitted after a HelloRetryRequest.
317 : : */
318 [ - + ]: 0 : case TLS_EXTENSION_EARLY_DATA:
319 [ # # ][ # # ]: 0 : RESULT_ENSURE(new_size == 0, S2N_ERR_BAD_MESSAGE);
320 : 0 : break;
321 : : /*
322 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.2
323 : : *# - Including a "cookie" extension if one was provided in the
324 : : *# HelloRetryRequest.
325 : : */
326 [ + + ]: 37 : case TLS_EXTENSION_COOKIE:
327 : : /* Handled when parsing the cookie extension */
328 : 37 : break;
329 : : /*
330 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.1.2
331 : : *# - Updating the "pre_shared_key" extension if present by recomputing
332 : : *# the "obfuscated_ticket_age" and binder values and (optionally)
333 : : *# removing any PSKs which are incompatible with the server's
334 : : *# indicated cipher suite.
335 : : */
336 [ + + ]: 462 : case TLS_EXTENSION_PRE_SHARED_KEY:
337 : : /* Handled when parsing the psk extension */
338 : 462 : break;
339 : :
340 : : /* Some clients have been found to send mismatching supported versions in their second
341 : : * ClientHello. The extension isn't compared byte-for-byte for increased compatibility
342 : : * with these clients.
343 : : */
344 [ + + ]: 634 : case TLS_EXTENSION_SUPPORTED_VERSIONS:
345 : : /* Additional HRR validation for the supported versions extension is performed when
346 : : * parsing the extension.
347 : : */
348 : 634 : break;
349 : :
350 : : /*
351 : : * No more exceptions.
352 : : * All other extensions must match.
353 : : */
354 [ + + ]: 2385 : default:
355 [ + - ][ + + ]: 2385 : RESULT_ENSURE(old_size == new_size, S2N_ERR_BAD_MESSAGE);
356 [ + - ][ + + ]: 4152 : RESULT_ENSURE(s2n_constant_time_equals(
357 : 4152 : new_extension->extension.data,
358 : 4152 : old_extension->extension.data,
359 : 4152 : old_size),
360 : 4152 : S2N_ERR_BAD_MESSAGE);
361 : 4152 : }
362 : 4152 : }
363 : :
364 : 634 : return S2N_RESULT_OK;
365 : 637 : }
366 : :
367 : : S2N_RESULT s2n_client_hello_parse_raw(struct s2n_client_hello *client_hello)
368 : 8022 : {
369 [ - + ][ # # ]: 8022 : RESULT_ENSURE_REF(client_hello);
370 : :
371 : 8022 : struct s2n_stuffer in_stuffer = { 0 };
372 [ - + ]: 8022 : RESULT_GUARD_POSIX(s2n_stuffer_init_written(&in_stuffer, &client_hello->raw_message));
373 : 8022 : struct s2n_stuffer *in = &in_stuffer;
374 : :
375 : : /**
376 : : * https://tools.ietf.org/rfc/rfc8446#4.1.2
377 : : * Structure of this message:
378 : : *
379 : : * uint16 ProtocolVersion;
380 : : * opaque Random[32];
381 : : *
382 : : * uint8 CipherSuite[2];
383 : : *
384 : : * struct {
385 : : * ProtocolVersion legacy_version = 0x0303;
386 : : * Random random;
387 : : * opaque legacy_session_id<0..32>;
388 : : * CipherSuite cipher_suites<2..2^16-2>;
389 : : * opaque legacy_compression_methods<1..2^8-1>;
390 : : * Extension extensions<8..2^16-1>;
391 : : * } ClientHello;
392 : : **/
393 : :
394 : : /* legacy_version */
395 : 8022 : uint8_t client_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN] = { 0 };
396 [ + + ]: 8022 : RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(in, client_protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN));
397 : :
398 : : /* Encode the version as a 1 byte representation of the two protocol version bytes, with the
399 : : * major version in the tens place and the minor version in the ones place. For example, the
400 : : * TLS 1.2 protocol version is 0x0303, which is encoded as S2N_TLS12 (33).
401 : : */
402 : 8021 : client_hello->legacy_version = (client_protocol_version[0] * 10) + client_protocol_version[1];
403 : :
404 : : /* random - read and store it, erasing from raw message */
405 [ - + ]: 8021 : RESULT_GUARD_POSIX(s2n_stuffer_erase_and_read_bytes(in, client_hello->random, S2N_TLS_RANDOM_DATA_LEN));
406 : :
407 : : /* legacy_session_id */
408 : 8021 : uint8_t session_id_len = 0;
409 [ - + ]: 8021 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(in, &session_id_len));
410 [ + - ][ + + ]: 8021 : RESULT_ENSURE(session_id_len <= S2N_TLS_SESSION_ID_MAX_LEN, S2N_ERR_BAD_MESSAGE);
411 : 8020 : uint8_t *session_id = s2n_stuffer_raw_read(in, session_id_len);
412 [ - + ][ # # ]: 8020 : RESULT_ENSURE(session_id != NULL, S2N_ERR_BAD_MESSAGE);
413 [ - + ]: 8020 : RESULT_GUARD_POSIX(s2n_blob_init(&client_hello->session_id, session_id, session_id_len));
414 : :
415 : : /* cipher suites */
416 : 8020 : uint16_t cipher_suites_length = 0;
417 [ - + ]: 8020 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(in, &cipher_suites_length));
418 [ + - ][ + + ]: 8020 : RESULT_ENSURE(cipher_suites_length > 0, S2N_ERR_BAD_MESSAGE);
419 [ - + ][ # # ]: 8018 : RESULT_ENSURE(cipher_suites_length % S2N_TLS_CIPHER_SUITE_LEN == 0, S2N_ERR_BAD_MESSAGE);
420 : 8018 : uint8_t *cipher_suites = s2n_stuffer_raw_read(in, cipher_suites_length);
421 [ - + ][ # # ]: 8018 : RESULT_ENSURE(cipher_suites != NULL, S2N_ERR_BAD_MESSAGE);
422 [ - + ]: 8018 : RESULT_GUARD_POSIX(s2n_blob_init(&client_hello->cipher_suites, cipher_suites, cipher_suites_length));
423 : :
424 : : /* legacy_compression_methods */
425 : 8018 : uint8_t compression_methods_len = 0;
426 [ - + ]: 8018 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(in, &compression_methods_len));
427 : 8018 : uint8_t *compression_methods = s2n_stuffer_raw_read(in, compression_methods_len);
428 [ - + ][ # # ]: 8018 : RESULT_ENSURE(compression_methods != NULL, S2N_ERR_BAD_MESSAGE);
429 [ - + ]: 8018 : RESULT_GUARD_POSIX(s2n_blob_init(&client_hello->compression_methods, compression_methods, compression_methods_len));
430 : :
431 : : /* extensions */
432 [ + + ]: 8018 : RESULT_GUARD_POSIX(s2n_extension_list_parse(in, &client_hello->extensions));
433 : :
434 : 8017 : return S2N_RESULT_OK;
435 : 8018 : }
436 : :
437 : : int s2n_parse_client_hello(struct s2n_connection *conn)
438 : 7936 : {
439 [ # # ][ - + ]: 7936 : POSIX_ENSURE_REF(conn);
440 : :
441 : : /* SSLv2 ClientHellos are not allowed during a HelloRetryRequest */
442 [ + + ]: 7936 : if (s2n_is_hello_retry_handshake(conn)) {
443 [ + + ][ + - ]: 643 : POSIX_ENSURE(!conn->client_hello.sslv2, S2N_ERR_BAD_MESSAGE);
444 : 643 : }
445 : :
446 : : /* If a retry, move the old version of the client hello
447 : : * somewhere safe so we can compare it to the new client hello later.
448 : : */
449 : 7935 : DEFER_CLEANUP(struct s2n_client_hello previous_hello_retry = conn->client_hello,
450 : 7935 : s2n_client_hello_free_raw_message);
451 : :
452 : : /* Save the client random before clearing for retry validation */
453 : 7935 : uint8_t previous_client_random[S2N_TLS_RANDOM_DATA_LEN] = { 0 };
454 [ # # ][ - + ]: 7935 : POSIX_CHECKED_MEMCPY(previous_client_random, conn->client_hello.random,
[ + - ]
455 : 7935 : S2N_TLS_RANDOM_DATA_LEN);
456 : :
457 [ + + ]: 7935 : if (s2n_is_hello_retry_handshake(conn)) {
458 [ - + ][ # # ]: 642 : POSIX_CHECKED_MEMSET(&conn->client_hello, 0, sizeof(struct s2n_client_hello));
[ + - ]
459 : 642 : }
460 : :
461 [ - + ]: 7935 : POSIX_GUARD(s2n_collect_client_hello(&conn->client_hello, &conn->handshake.io));
462 : :
463 [ + + ]: 7935 : if (conn->client_hello.sslv2) {
464 [ + + ]: 7 : POSIX_GUARD(s2n_sslv2_client_hello_parse(conn));
465 : 6 : return S2N_SUCCESS;
466 : 7 : }
467 : :
468 : : /* Parse raw, collected client hello */
469 [ + + ]: 7928 : POSIX_GUARD_RESULT(s2n_client_hello_parse_raw(&conn->client_hello));
470 : :
471 : : /* Protocol version in the ClientHello is fixed at 0x0303(TLS 1.2) for
472 : : * future versions of TLS. Therefore, we will negotiate down if a client sends
473 : : * an unexpected value above 0x0303.
474 : : */
475 [ + + ]: 7925 : conn->client_protocol_version = S2N_MIN(conn->client_hello.legacy_version, S2N_TLS12);
476 : :
477 : : /* Copy the session id to the connection. */
478 : 7925 : conn->session_id_len = conn->client_hello.session_id.size;
479 [ # # ][ - + ]: 7925 : POSIX_CHECKED_MEMCPY(conn->session_id, conn->client_hello.session_id.data, conn->session_id_len);
[ + + ]
480 : :
481 [ + + ]: 7925 : POSIX_GUARD_RESULT(s2n_client_hello_verify_for_retry(conn,
482 : 7917 : &previous_hello_retry, &conn->client_hello, previous_client_random));
483 : 7917 : return S2N_SUCCESS;
484 : 7925 : }
485 : :
486 : : static S2N_RESULT s2n_client_hello_parse_message_impl(struct s2n_client_hello **result,
487 : : const uint8_t *raw_message, uint32_t raw_message_size)
488 : 103 : {
489 [ # # ][ - + ]: 103 : RESULT_ENSURE_REF(result);
490 [ + + ][ + - ]: 103 : RESULT_ENSURE(raw_message_size <= S2N_MAXIMUM_HANDSHAKE_MESSAGE_LENGTH, S2N_ERR_BAD_MESSAGE);
491 : :
492 : 102 : DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free);
493 [ - + ]: 102 : RESULT_GUARD_POSIX(s2n_alloc(&mem, sizeof(struct s2n_client_hello)));
494 [ - + ]: 102 : RESULT_GUARD_POSIX(s2n_blob_zero(&mem));
495 : :
496 : 102 : DEFER_CLEANUP(struct s2n_client_hello *client_hello = NULL, s2n_client_hello_free);
497 : 102 : client_hello = (struct s2n_client_hello *) (void *) mem.data;
498 : 102 : client_hello->alloced = true;
499 : 102 : ZERO_TO_DISABLE_DEFER_CLEANUP(mem);
500 : :
501 : 102 : DEFER_CLEANUP(struct s2n_stuffer in = { 0 }, s2n_stuffer_free);
502 [ - + ]: 102 : RESULT_GUARD_POSIX(s2n_stuffer_alloc(&in, raw_message_size));
503 [ - + ]: 102 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&in, raw_message, raw_message_size));
504 : :
505 : 102 : uint8_t message_type = 0;
506 : 102 : uint32_t message_len = 0;
507 [ - + ]: 102 : RESULT_GUARD(s2n_handshake_parse_header(&in, &message_type, &message_len));
508 [ + - ][ + + ]: 102 : RESULT_ENSURE(message_type == TLS_CLIENT_HELLO, S2N_ERR_BAD_MESSAGE);
509 [ + - ][ + + ]: 96 : RESULT_ENSURE(message_len == s2n_stuffer_data_available(&in), S2N_ERR_BAD_MESSAGE);
510 : :
511 [ - + ]: 94 : RESULT_GUARD_POSIX(s2n_collect_client_hello(client_hello, &in));
512 [ # # ][ - + ]: 94 : RESULT_ENSURE(s2n_stuffer_data_available(&in) == 0, S2N_ERR_BAD_MESSAGE);
513 : :
514 [ + + ]: 94 : RESULT_GUARD(s2n_client_hello_parse_raw(client_hello));
515 : :
516 : 92 : *result = client_hello;
517 : 92 : ZERO_TO_DISABLE_DEFER_CLEANUP(client_hello);
518 : 92 : return S2N_RESULT_OK;
519 : 94 : }
520 : :
521 : : struct s2n_client_hello *s2n_client_hello_parse_message(const uint8_t *raw_message, uint32_t raw_message_size)
522 : 103 : {
523 : 103 : struct s2n_client_hello *result = NULL;
524 [ + + ]: 103 : PTR_GUARD_RESULT(s2n_client_hello_parse_message_impl(&result, raw_message, raw_message_size));
525 : 92 : return result;
526 : 103 : }
527 : :
528 : : int s2n_process_client_hello(struct s2n_connection *conn)
529 : 7907 : {
530 [ # # ][ - + ]: 7907 : POSIX_ENSURE_REF(conn);
531 [ - + ][ # # ]: 7907 : POSIX_ENSURE_REF(conn->secure);
532 [ # # ][ - + ]: 7907 : POSIX_ENSURE_REF(conn->secure->cipher_suite);
533 : :
534 : : /* Client hello is parsed and config is finalized.
535 : : * Negotiate protocol version, cipher suite, ALPN, select a cert, etc. */
536 : 7907 : struct s2n_client_hello *client_hello = &conn->client_hello;
537 : :
538 : 7907 : const struct s2n_security_policy *security_policy = NULL;
539 [ - + ]: 7907 : POSIX_GUARD(s2n_connection_get_security_policy(conn, &security_policy));
540 : :
541 [ - + ][ + + ]: 7907 : if (!s2n_connection_supports_tls13(conn) || !s2n_security_policy_supports_tls13(security_policy)) {
542 [ + + ]: 2314 : conn->server_protocol_version = S2N_MIN(conn->server_protocol_version, S2N_TLS12);
543 [ + + ]: 2314 : conn->actual_protocol_version = S2N_MIN(conn->server_protocol_version, S2N_TLS12);
544 : 2314 : }
545 : :
546 : : /* Set default key exchange curve.
547 : : * This is going to be our fallback if the client has no preference.
548 : : *
549 : : * P-256 is our preferred fallback option because the TLS1.3 RFC requires
550 : : * all implementations to support it:
551 : : *
552 : : * https://tools.ietf.org/rfc/rfc8446#section-9.1
553 : : * A TLS-compliant application MUST support key exchange with secp256r1 (NIST P-256)
554 : : * and SHOULD support key exchange with X25519 [RFC7748]
555 : : *
556 : : *= https://www.rfc-editor.org/rfc/rfc4492#section-4
557 : : *# A client that proposes ECC cipher suites may choose not to include these extensions.
558 : : *# In this case, the server is free to choose any one of the elliptic curves or point formats listed in Section 5.
559 : : *
560 : : */
561 : 7907 : const struct s2n_ecc_preferences *ecc_pref = NULL;
562 [ - + ]: 7907 : POSIX_GUARD(s2n_connection_get_ecc_preferences(conn, &ecc_pref));
563 [ - + ][ # # ]: 7907 : POSIX_ENSURE_REF(ecc_pref);
564 : :
565 [ - + ]: 7907 : if (ecc_pref->count == 0) {
566 : 0 : conn->kex_params.server_ecc_evp_params.negotiated_curve = NULL;
567 [ + + ]: 7907 : } else if (s2n_ecc_preferences_includes_curve(ecc_pref, TLS_EC_CURVE_SECP_256_R1)) {
568 : 7881 : conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp256r1;
569 : 7881 : } else {
570 : : /* If P-256 isn't allowed by the current security policy, choose the first / most preferred curve. */
571 : 26 : conn->kex_params.server_ecc_evp_params.negotiated_curve = ecc_pref->ecc_curves[0];
572 : 26 : }
573 : :
574 [ + + ]: 7907 : POSIX_GUARD(s2n_extension_list_process(S2N_EXTENSION_LIST_CLIENT_HELLO, conn, &conn->client_hello.extensions));
575 : :
576 : : /* After parsing extensions, select a curve and corresponding keyshare to use */
577 [ + + ]: 7899 : if (conn->actual_protocol_version >= S2N_TLS13) {
578 [ - + ]: 5410 : POSIX_GUARD(s2n_extensions_server_key_share_select(conn));
579 : 5410 : }
580 : :
581 : : /* for pre TLS 1.3 connections, protocol selection is not done in supported_versions extensions, so do it here */
582 [ + + ]: 7899 : if (conn->actual_protocol_version < S2N_TLS13) {
583 [ + + ]: 2489 : conn->actual_protocol_version = S2N_MIN(conn->server_protocol_version, conn->client_protocol_version);
584 : 2489 : }
585 : :
586 [ + + ]: 7899 : if (conn->client_protocol_version < security_policy->minimum_protocol_version) {
587 [ - + ]: 1 : POSIX_GUARD(s2n_queue_reader_unsupported_protocol_version_alert(conn));
588 [ + - ]: 1 : POSIX_BAIL(S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
589 : 1 : }
590 : :
591 [ + + ]: 7898 : if (s2n_connection_is_quic_enabled(conn)) {
592 [ + + ][ + - ]: 12 : POSIX_ENSURE(conn->actual_protocol_version >= S2N_TLS13, S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
593 : 12 : }
594 : :
595 : : /* Find potential certificate matches before we choose the cipher. */
596 [ - + ]: 7897 : POSIX_GUARD(s2n_conn_find_name_matching_certs(conn));
597 : :
598 : : /* Save the previous cipher suite */
599 : 7897 : uint8_t previous_cipher_suite_iana[S2N_TLS_CIPHER_SUITE_LEN] = { 0 };
600 [ # # ][ - + ]: 7897 : POSIX_CHECKED_MEMCPY(previous_cipher_suite_iana, conn->secure->cipher_suite->iana_value, S2N_TLS_CIPHER_SUITE_LEN);
[ + - ]
601 : :
602 : : /* Now choose the ciphers we have certs for. */
603 [ + + ]: 7897 : if (conn->client_hello.sslv2) {
604 [ - + ]: 5 : POSIX_GUARD(s2n_set_cipher_as_sslv2_server(conn, client_hello->cipher_suites.data,
605 : 5 : client_hello->cipher_suites.size / S2N_SSLv2_CIPHER_SUITE_LEN));
606 : 7892 : } else {
607 [ + + ]: 7892 : POSIX_GUARD(s2n_set_cipher_as_tls_server(conn, client_hello->cipher_suites.data,
608 : 7892 : client_hello->cipher_suites.size / 2));
609 : 7892 : }
610 : :
611 : : /* Check if this is the second client hello in a hello retry handshake */
612 [ + + ][ + + ]: 7880 : if (s2n_is_hello_retry_handshake(conn) && conn->handshake.message_number > 0) {
613 : : /**
614 : : *= https://www.rfc-editor.org/rfc/rfc8446#4.1.4
615 : : *# Servers MUST ensure that they negotiate the
616 : : *# same cipher suite when receiving a conformant updated ClientHello (if
617 : : *# the server selects the cipher suite as the first step in the
618 : : *# negotiation, then this will happen automatically).
619 : : **/
620 [ + + ][ + - ]: 630 : POSIX_ENSURE(s2n_constant_time_equals(previous_cipher_suite_iana, conn->secure->cipher_suite->iana_value,
621 : 630 : S2N_TLS_CIPHER_SUITE_LEN),
622 : 630 : S2N_ERR_BAD_MESSAGE);
623 : 630 : }
624 : :
625 : : /* If we're using a PSK, we don't need to choose a signature algorithm or certificate,
626 : : * because no additional auth is required. */
627 [ + + ]: 7878 : if (conn->psk_params.chosen_psk != NULL) {
628 : 1066 : return S2N_SUCCESS;
629 : 1066 : }
630 : :
631 : : /* And set the signature and hash algorithm used for key exchange signatures */
632 [ + + ]: 6812 : POSIX_GUARD_RESULT(s2n_signature_algorithm_select(conn));
633 : :
634 : : /* And finally, set the certs specified by the final auth + sig_alg combo. */
635 [ + + ]: 6809 : POSIX_GUARD(s2n_select_certs_for_server_auth(conn, &conn->handshake_params.our_chain_and_key));
636 : :
637 : 6807 : return S2N_SUCCESS;
638 : 6809 : }
639 : :
640 : : static S2N_RESULT s2n_client_hello_process_cb_response(struct s2n_connection *conn, int rc)
641 : 119 : {
642 [ + + ]: 119 : if (rc < 0) {
643 : 4 : goto fail;
644 : 4 : }
645 [ - + ]: 115 : switch (conn->config->client_hello_cb_mode) {
646 [ + + ]: 87 : case S2N_CLIENT_HELLO_CB_BLOCKING: {
647 [ + + ]: 87 : if (rc) {
648 : 5 : conn->server_name_used = 1;
649 : 5 : }
650 : 87 : return S2N_RESULT_OK;
651 : 0 : }
652 [ + + ]: 28 : case S2N_CLIENT_HELLO_CB_NONBLOCKING: {
653 [ + + ]: 28 : if (conn->client_hello.callback_async_done) {
654 : 9 : return S2N_RESULT_OK;
655 : 9 : }
656 : 19 : conn->client_hello.callback_async_blocked = 1;
657 [ + - ]: 19 : RESULT_BAIL(S2N_ERR_ASYNC_BLOCKED);
658 : 19 : }
659 : 115 : }
660 : 4 : fail:
661 : : /* rc < 0 */
662 [ - + ]: 4 : RESULT_GUARD_POSIX(s2n_queue_reader_handshake_failure_alert(conn));
663 [ + - ]: 4 : RESULT_BAIL(S2N_ERR_CANCELLED);
664 : 4 : }
665 : :
666 : : int s2n_client_hello_recv(struct s2n_connection *conn)
667 : 7956 : {
668 [ + + ][ + - ]: 7956 : POSIX_ENSURE(!conn->client_hello.callback_async_blocked, S2N_ERR_ASYNC_BLOCKED);
669 : :
670 : : /* Only parse the ClientHello once */
671 [ + + ]: 7945 : if (!conn->client_hello.parsed) {
672 [ + + ]: 7921 : POSIX_GUARD(s2n_parse_client_hello(conn));
673 : : /* Mark the collected client hello as available when parsing is done and before the client hello callback */
674 : 7909 : conn->client_hello.parsed = true;
675 : 7909 : }
676 : :
677 : : /* Only invoke the ClientHello callback once.
678 : : * This means that we do NOT invoke the callback again on the second ClientHello
679 : : * in a TLS1.3 retry handshake. We explicitly check for a retry because the
680 : : * callback state may have been cleared while parsing the second ClientHello.
681 : : */
682 [ + + ][ + + ]: 7933 : if (!conn->client_hello.callback_invoked && !IS_HELLO_RETRY_HANDSHAKE(conn)) {
683 : : /* Mark the client hello callback as invoked to avoid calling it again. */
684 : 7279 : conn->client_hello.callback_invoked = true;
685 : :
686 : : /* Do NOT move this null check. A test exists to assert that a server connection can get
687 : : * as far as the client hello callback without using its config. To do this we need a
688 : : * specific error for a null config just before the client hello callback. The test's
689 : : * assertions are weakened if this check is moved. */
690 [ + - ][ + + ]: 7279 : POSIX_ENSURE(conn->config, S2N_ERR_CONFIG_NULL_BEFORE_CH_CALLBACK);
691 : :
692 : : /* Call client_hello_cb if exists, letting application to modify s2n_connection or swap s2n_config */
693 [ + + ]: 7276 : if (conn->config->client_hello_cb) {
694 : 121 : int rc = conn->config->client_hello_cb(conn, conn->config->client_hello_cb_ctx);
695 [ + + ]: 121 : POSIX_GUARD_RESULT(s2n_client_hello_process_cb_response(conn, rc));
696 : 121 : }
697 : 7276 : }
698 : :
699 [ + + ]: 7907 : POSIX_GUARD(s2n_process_client_hello(conn));
700 : :
701 : 7873 : return 0;
702 : 7907 : }
703 : :
704 : : S2N_RESULT s2n_cipher_suite_validate_available(struct s2n_connection *conn, struct s2n_cipher_suite *cipher)
705 : 196503 : {
706 [ # # ][ - + ]: 196503 : RESULT_ENSURE_REF(conn);
707 [ - + ][ # # ]: 196503 : RESULT_ENSURE_REF(cipher);
708 [ + + ][ + - ]: 196503 : RESULT_ENSURE_EQ(cipher->available, true);
709 [ + + ][ + - ]: 194528 : RESULT_ENSURE_LTE(cipher->minimum_required_tls_version, conn->client_protocol_version);
710 [ + + ]: 194078 : if (s2n_connection_is_quic_enabled(conn)) {
711 [ + + ][ + - ]: 215 : RESULT_ENSURE_GTE(cipher->minimum_required_tls_version, S2N_TLS13);
712 : 215 : }
713 : 193902 : return S2N_RESULT_OK;
714 : 194078 : }
715 : :
716 : : int s2n_client_hello_send(struct s2n_connection *conn)
717 : 7888 : {
718 [ # # ][ - + ]: 7888 : POSIX_ENSURE_REF(conn);
719 : :
720 : 7888 : const struct s2n_security_policy *security_policy = NULL;
721 [ - + ]: 7888 : POSIX_GUARD(s2n_connection_get_security_policy(conn, &security_policy));
722 : :
723 : 7888 : const struct s2n_cipher_preferences *cipher_preferences = security_policy->cipher_preferences;
724 [ # # ][ - + ]: 7888 : POSIX_ENSURE_REF(cipher_preferences);
725 : :
726 [ - + ][ + + ]: 7888 : if (!s2n_connection_supports_tls13(conn) || !s2n_security_policy_supports_tls13(security_policy)) {
727 [ + + ]: 1831 : conn->client_protocol_version = S2N_MIN(conn->client_protocol_version, S2N_TLS12);
728 [ + + ]: 1831 : conn->actual_protocol_version = S2N_MIN(conn->actual_protocol_version, S2N_TLS12);
729 : 1831 : }
730 : :
731 : 7888 : struct s2n_stuffer *out = &conn->handshake.io;
732 : 7888 : uint8_t client_protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN] = { 0 };
733 : :
734 [ + + ]: 7888 : uint8_t reported_protocol_version = S2N_MIN(conn->client_protocol_version, S2N_TLS12);
735 : 7888 : conn->client_hello.legacy_version = reported_protocol_version;
736 : 7888 : client_protocol_version[0] = reported_protocol_version / 10;
737 : 7888 : client_protocol_version[1] = reported_protocol_version % 10;
738 [ - + ]: 7888 : POSIX_GUARD(s2n_stuffer_write_bytes(out, client_protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN));
739 : :
740 : 7888 : struct s2n_blob client_random = { 0 };
741 [ - + ]: 7888 : POSIX_GUARD(s2n_blob_init(&client_random, conn->client_hello.random, S2N_TLS_RANDOM_DATA_LEN));
742 [ + + ]: 7888 : if (!s2n_is_hello_retry_handshake(conn)) {
743 : : /* Only generate the random data for our first client hello.
744 : : * If we retry, we'll reuse the value. */
745 [ - + ]: 7243 : POSIX_GUARD_RESULT(s2n_get_public_random_data(&client_random));
746 : 7243 : }
747 [ - + ]: 7888 : POSIX_GUARD(s2n_stuffer_write(out, &client_random));
748 : :
749 [ + + ]: 7888 : POSIX_GUARD_RESULT(s2n_generate_client_session_id(conn));
750 [ - + ]: 7887 : POSIX_GUARD(s2n_stuffer_write_uint8(out, conn->session_id_len));
751 [ + + ]: 7887 : if (conn->session_id_len > 0) {
752 [ - + ]: 5828 : POSIX_GUARD(s2n_stuffer_write_bytes(out, conn->session_id, conn->session_id_len));
753 : 5828 : }
754 : :
755 : : /* Reserve space for size of the list of available ciphers */
756 : 7887 : struct s2n_stuffer_reservation available_cipher_suites_size = { 0 };
757 [ - + ]: 7887 : POSIX_GUARD(s2n_stuffer_reserve_uint16(out, &available_cipher_suites_size));
758 : :
759 : : /* Now, write the IANA values of every available cipher suite in our list */
760 : 7887 : struct s2n_cipher_suite *cipher = NULL;
761 : 7887 : bool tls12_is_possible = false;
762 [ + + ]: 204390 : for (size_t i = 0; i < security_policy->cipher_preferences->count; i++) {
763 : 196503 : cipher = cipher_preferences->suites[i];
764 [ + + ]: 196503 : if (s2n_result_is_error(s2n_cipher_suite_validate_available(conn, cipher))) {
765 : 2601 : continue;
766 : 2601 : }
767 [ + + ]: 193902 : if (cipher->minimum_required_tls_version < S2N_TLS13) {
768 : 175957 : tls12_is_possible = true;
769 : 175957 : }
770 [ - + ]: 193902 : POSIX_GUARD(s2n_stuffer_write_bytes(out, cipher->iana_value, S2N_TLS_CIPHER_SUITE_LEN));
771 : 193902 : }
772 : :
773 : : /**
774 : : * For initial handshakes:
775 : : *= https://www.rfc-editor.org/rfc/rfc5746#3.4
776 : : *# o The client MUST include either an empty "renegotiation_info"
777 : : *# extension, or the TLS_EMPTY_RENEGOTIATION_INFO_SCSV signaling
778 : : *# cipher suite value in the ClientHello. Including both is NOT
779 : : *# RECOMMENDED.
780 : : * For maximum backwards compatibility, we choose to use the TLS_EMPTY_RENEGOTIATION_INFO_SCSV cipher suite
781 : : * rather than the "renegotiation_info" extension.
782 : : *
783 : : * For renegotiation handshakes:
784 : : *= https://www.rfc-editor.org/rfc/rfc5746#3.5
785 : : *# The SCSV MUST NOT be included.
786 : : */
787 [ + + ][ + + ]: 7887 : if (tls12_is_possible && !s2n_handshake_is_renegotiation(conn)) {
788 : 7558 : uint8_t renegotiation_info_scsv[S2N_TLS_CIPHER_SUITE_LEN] = { TLS_EMPTY_RENEGOTIATION_INFO_SCSV };
789 [ - + ]: 7558 : POSIX_GUARD(s2n_stuffer_write_bytes(out, renegotiation_info_scsv, S2N_TLS_CIPHER_SUITE_LEN));
790 : 7558 : }
791 : :
792 : : /* Write size of the list of available ciphers */
793 : 7887 : uint32_t ciphers_size = 0;
794 [ - + ]: 7887 : POSIX_GUARD(s2n_stuffer_get_vector_size(&available_cipher_suites_size, &ciphers_size));
795 [ + - ][ + + ]: 7887 : POSIX_ENSURE(ciphers_size > 0, S2N_ERR_INVALID_CIPHER_PREFERENCES);
796 [ - + ]: 7886 : POSIX_GUARD(s2n_stuffer_write_reservation(&available_cipher_suites_size, ciphers_size));
797 : :
798 : : /* Zero compression methods */
799 [ - + ]: 7886 : POSIX_GUARD(s2n_stuffer_write_uint8(out, 1));
800 [ - + ]: 7886 : POSIX_GUARD(s2n_stuffer_write_uint8(out, 0));
801 : :
802 : : /* Write the extensions */
803 [ - + ]: 7886 : POSIX_GUARD(s2n_extension_list_send(S2N_EXTENSION_LIST_CLIENT_HELLO, conn, out));
804 : :
805 : : /* Once the message is complete, finish calculating the PSK binders.
806 : : *
807 : : * The PSK binders require all the sizes in the ClientHello to be written correctly,
808 : : * including the extension size and extension list size, and therefore have
809 : : * to be calculated AFTER we finish writing the entire extension list. */
810 [ - + ]: 7886 : POSIX_GUARD_RESULT(s2n_finish_psk_extension(conn));
811 : :
812 : : /* If early data was not requested as part of the ClientHello, it never will be. */
813 [ + + ]: 7886 : if (conn->early_data_state == S2N_UNKNOWN_EARLY_DATA_STATE) {
814 [ - + ]: 6702 : POSIX_GUARD_RESULT(s2n_connection_set_early_data_state(conn, S2N_EARLY_DATA_NOT_REQUESTED));
815 : 6702 : }
816 : :
817 : 7886 : return S2N_SUCCESS;
818 : 7886 : }
819 : :
820 : : /*
821 : : * s2n-tls does NOT support SSLv2. However, it does support SSLv2 ClientHellos.
822 : : * Clients may send SSLv2 ClientHellos advertising higher protocol versions for
823 : : * backwards compatibility reasons. See https://tools.ietf.org/rfc/rfc2246 Appendix E.
824 : : *
825 : : * In this case, conn->client_hello.legacy_version and conn->client_protocol_version
826 : : * will be higher than SSLv2.
827 : : *
828 : : * See http://www-archive.mozilla.org/projects/security/pki/nss/ssl/draft02.html Section 2.5
829 : : * for a description of the expected SSLv2 format.
830 : : * Alternatively, the TLS1.0 RFC includes a more modern description of the format:
831 : : * https://tools.ietf.org/rfc/rfc2246 Appendix E.1
832 : : */
833 : : int s2n_sslv2_client_hello_parse(struct s2n_connection *conn)
834 : 7 : {
835 : 7 : struct s2n_client_hello *client_hello = &conn->client_hello;
836 [ - + ]: 7 : conn->client_protocol_version = S2N_MIN(client_hello->legacy_version, S2N_TLS12);
837 : :
838 : 7 : struct s2n_stuffer in_stuffer = { 0 };
839 [ - + ]: 7 : POSIX_GUARD(s2n_stuffer_init(&in_stuffer, &client_hello->raw_message));
840 [ - + ]: 7 : POSIX_GUARD(s2n_stuffer_skip_write(&in_stuffer, client_hello->raw_message.size));
841 : 7 : struct s2n_stuffer *in = &in_stuffer;
842 : :
843 : : /* We start 5 bytes into the record */
844 : 7 : uint16_t cipher_suites_length = 0;
845 [ - + ]: 7 : POSIX_GUARD(s2n_stuffer_read_uint16(in, &cipher_suites_length));
846 [ + + ][ + - ]: 7 : POSIX_ENSURE(cipher_suites_length > 0, S2N_ERR_BAD_MESSAGE);
847 [ - + ][ # # ]: 6 : POSIX_ENSURE(cipher_suites_length % S2N_SSLv2_CIPHER_SUITE_LEN == 0, S2N_ERR_BAD_MESSAGE);
848 : :
849 : 6 : uint16_t session_id_length = 0;
850 [ - + ]: 6 : POSIX_GUARD(s2n_stuffer_read_uint16(in, &session_id_length));
851 : :
852 : 6 : uint16_t challenge_length = 0;
853 [ - + ]: 6 : POSIX_GUARD(s2n_stuffer_read_uint16(in, &challenge_length));
854 : :
855 [ - + ][ # # ]: 6 : S2N_ERROR_IF(challenge_length > S2N_TLS_RANDOM_DATA_LEN, S2N_ERR_BAD_MESSAGE);
856 : :
857 : 6 : client_hello->cipher_suites.size = cipher_suites_length;
858 : 6 : client_hello->cipher_suites.data = s2n_stuffer_raw_read(in, cipher_suites_length);
859 [ - + ][ # # ]: 6 : POSIX_ENSURE_REF(client_hello->cipher_suites.data);
860 : :
861 [ - + ][ # # ]: 6 : S2N_ERROR_IF(session_id_length > s2n_stuffer_data_available(in), S2N_ERR_BAD_MESSAGE);
862 [ - + ]: 6 : POSIX_GUARD(s2n_blob_init(&client_hello->session_id, s2n_stuffer_raw_read(in, session_id_length), session_id_length));
863 [ - + ][ # # ]: 6 : if (session_id_length > 0 && session_id_length <= S2N_TLS_SESSION_ID_MAX_LEN) {
864 [ # # ][ # # ]: 0 : POSIX_CHECKED_MEMCPY(conn->session_id, client_hello->session_id.data, session_id_length);
[ # # ]
865 : 0 : conn->session_id_len = (uint8_t) session_id_length;
866 : 0 : }
867 : :
868 : 6 : struct s2n_blob b = { 0 };
869 [ - + ]: 6 : POSIX_GUARD(s2n_blob_init(&b, conn->client_hello.random, S2N_TLS_RANDOM_DATA_LEN));
870 : :
871 : 6 : b.data += S2N_TLS_RANDOM_DATA_LEN - challenge_length;
872 : 6 : b.size -= S2N_TLS_RANDOM_DATA_LEN - challenge_length;
873 : :
874 [ - + ]: 6 : POSIX_GUARD(s2n_stuffer_read(in, &b));
875 : :
876 : 6 : return 0;
877 : 6 : }
878 : :
879 : : int s2n_client_hello_get_parsed_extension(s2n_tls_extension_type extension_type,
880 : : s2n_parsed_extensions_list *parsed_extension_list, s2n_parsed_extension **parsed_extension)
881 : 785 : {
882 [ - + ][ # # ]: 785 : POSIX_ENSURE_REF(parsed_extension_list);
883 [ - + ][ # # ]: 785 : POSIX_ENSURE_REF(parsed_extension);
884 : :
885 : 785 : s2n_extension_type_id extension_type_id = 0;
886 [ - + ]: 785 : POSIX_GUARD(s2n_extension_supported_iana_value_to_id(extension_type, &extension_type_id));
887 : :
888 : 785 : s2n_parsed_extension *found_parsed_extension = &parsed_extension_list->parsed_extensions[extension_type_id];
889 [ + + ][ + - ]: 785 : POSIX_ENSURE(found_parsed_extension->extension.data, S2N_ERR_EXTENSION_NOT_RECEIVED);
890 [ + + ][ + - ]: 324 : POSIX_ENSURE(found_parsed_extension->extension_type == extension_type, S2N_ERR_INVALID_PARSED_EXTENSIONS);
891 : :
892 : 323 : *parsed_extension = found_parsed_extension;
893 : 323 : return S2N_SUCCESS;
894 : 324 : }
895 : :
896 : : ssize_t s2n_client_hello_get_extension_length(struct s2n_client_hello *ch, s2n_tls_extension_type extension_type)
897 : 41 : {
898 [ + + ][ + - ]: 41 : POSIX_ENSURE_REF(ch);
899 : :
900 : 39 : s2n_parsed_extension *parsed_extension = NULL;
901 [ + + ]: 39 : if (s2n_client_hello_get_parsed_extension(extension_type, &ch->extensions, &parsed_extension) != S2N_SUCCESS) {
902 : 2 : return 0;
903 : 2 : }
904 : :
905 : 37 : return parsed_extension->extension.size;
906 : 39 : }
907 : :
908 : : ssize_t s2n_client_hello_get_extension_by_id(struct s2n_client_hello *ch, s2n_tls_extension_type extension_type, uint8_t *out, uint32_t max_length)
909 : 42 : {
910 [ + + ][ + - ]: 42 : POSIX_ENSURE_REF(ch);
911 [ - + ][ # # ]: 40 : POSIX_ENSURE_REF(out);
912 : :
913 : 40 : s2n_parsed_extension *parsed_extension = NULL;
914 [ + + ]: 40 : if (s2n_client_hello_get_parsed_extension(extension_type, &ch->extensions, &parsed_extension) != S2N_SUCCESS) {
915 : 3 : return 0;
916 : 3 : }
917 : :
918 : 37 : uint32_t len = min_size(&parsed_extension->extension, max_length);
919 [ - + ][ # # ]: 37 : POSIX_CHECKED_MEMCPY(out, parsed_extension->extension.data, len);
[ + - ]
920 : 37 : return len;
921 : 37 : }
922 : :
923 : : int s2n_client_hello_get_session_id_length(struct s2n_client_hello *ch, uint32_t *out_length)
924 : 2 : {
925 [ - + ][ # # ]: 2 : POSIX_ENSURE_REF(ch);
926 [ - + ][ # # ]: 2 : POSIX_ENSURE_REF(out_length);
927 : 2 : *out_length = ch->session_id.size;
928 : 2 : return S2N_SUCCESS;
929 : 2 : }
930 : :
931 : : int s2n_client_hello_get_session_id(struct s2n_client_hello *ch, uint8_t *out, uint32_t *out_length, uint32_t max_length)
932 : 1 : {
933 [ - + ][ # # ]: 1 : POSIX_ENSURE_REF(ch);
934 [ - + ][ # # ]: 1 : POSIX_ENSURE_REF(out);
935 [ - + ][ # # ]: 1 : POSIX_ENSURE_REF(out_length);
936 : :
937 : 1 : uint32_t len = min_size(&ch->session_id, max_length);
938 [ # # ][ - + ]: 1 : POSIX_CHECKED_MEMCPY(out, ch->session_id.data, len);
[ + - ]
939 : 1 : *out_length = len;
940 : :
941 : 1 : return S2N_SUCCESS;
942 : 1 : }
943 : :
944 : : int s2n_client_hello_get_compression_methods_length(struct s2n_client_hello *ch, uint32_t *out_length)
945 : 4 : {
946 [ + - ][ + + ]: 4 : POSIX_ENSURE_REF(ch);
947 [ + - ][ + + ]: 3 : POSIX_ENSURE_REF(out_length);
948 : 2 : *out_length = ch->compression_methods.size;
949 : 2 : return S2N_SUCCESS;
950 : 3 : }
951 : :
952 : : int s2n_client_hello_get_compression_methods(struct s2n_client_hello *ch, uint8_t *list, uint32_t list_length, uint32_t *out_length)
953 : 6 : {
954 [ + - ][ + + ]: 6 : POSIX_ENSURE_REF(ch);
955 [ + - ][ + + ]: 5 : POSIX_ENSURE_REF(list);
956 [ + - ][ + + ]: 4 : POSIX_ENSURE_REF(out_length);
957 : :
958 [ + + ][ + - ]: 3 : POSIX_ENSURE(list_length >= ch->compression_methods.size, S2N_ERR_INSUFFICIENT_MEM_SIZE);
959 [ - + ][ # # ]: 2 : POSIX_CHECKED_MEMCPY(list, ch->compression_methods.data, ch->compression_methods.size);
[ + - ]
960 : 2 : *out_length = ch->compression_methods.size;
961 : 2 : return S2N_SUCCESS;
962 : 2 : }
963 : :
964 : : int s2n_client_hello_get_legacy_protocol_version(struct s2n_client_hello *ch, uint8_t *out)
965 : 3 : {
966 [ + + ][ + - ]: 3 : POSIX_ENSURE_REF(ch);
967 [ + + ][ + - ]: 2 : POSIX_ENSURE_REF(out);
968 : 1 : *out = ch->legacy_version;
969 : 1 : return S2N_SUCCESS;
970 : 2 : }
971 : :
972 : : int s2n_client_hello_get_legacy_record_version(struct s2n_client_hello *ch, uint8_t *out)
973 : 3 : {
974 [ + - ][ + + ]: 3 : POSIX_ENSURE_REF(ch);
975 [ + + ][ + - ]: 2 : POSIX_ENSURE_REF(out);
976 [ - + ][ # # ]: 1 : POSIX_ENSURE(ch->record_version_recorded, S2N_ERR_INVALID_ARGUMENT);
977 : 1 : *out = ch->legacy_record_version;
978 : 1 : return S2N_SUCCESS;
979 : 1 : }
980 : :
981 : : S2N_RESULT s2n_client_hello_get_raw_extension(uint16_t extension_iana,
982 : : struct s2n_blob *raw_extensions, struct s2n_blob *extension)
983 : 6 : {
984 [ # # ][ - + ]: 6 : RESULT_ENSURE_REF(raw_extensions);
985 [ - + ][ # # ]: 6 : RESULT_ENSURE_REF(extension);
986 : :
987 : 6 : *extension = (struct s2n_blob){ 0 };
988 : :
989 : 6 : struct s2n_stuffer raw_extensions_stuffer = { 0 };
990 [ - + ]: 6 : RESULT_GUARD_POSIX(s2n_stuffer_init(&raw_extensions_stuffer, raw_extensions));
991 [ - + ]: 6 : RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&raw_extensions_stuffer, raw_extensions->size));
992 : :
993 [ + + ]: 12 : while (s2n_stuffer_data_available(&raw_extensions_stuffer) > 0) {
994 : 10 : uint16_t extension_type = 0;
995 [ - + ]: 10 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&raw_extensions_stuffer, &extension_type));
996 : :
997 : 10 : uint16_t extension_size = 0;
998 [ - + ]: 10 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&raw_extensions_stuffer, &extension_size));
999 : :
1000 : 10 : uint8_t *extension_data = s2n_stuffer_raw_read(&raw_extensions_stuffer, extension_size);
1001 [ - + ][ # # ]: 10 : RESULT_ENSURE_REF(extension_data);
1002 : :
1003 [ + + ]: 10 : if (extension_iana == extension_type) {
1004 [ - + ]: 4 : RESULT_GUARD_POSIX(s2n_blob_init(extension, extension_data, extension_size));
1005 : 4 : return S2N_RESULT_OK;
1006 : 4 : }
1007 : 10 : }
1008 : 2 : return S2N_RESULT_OK;
1009 : 6 : }
1010 : :
1011 : : int s2n_client_hello_has_extension(struct s2n_client_hello *ch, uint16_t extension_iana, bool *exists)
1012 : 253 : {
1013 [ + + ][ + - ]: 253 : POSIX_ENSURE_REF(ch);
1014 [ - + ][ # # ]: 252 : POSIX_ENSURE_REF(exists);
1015 : :
1016 : 252 : *exists = false;
1017 : :
1018 : 252 : s2n_extension_type_id extension_type_id = s2n_unsupported_extension;
1019 [ + + ]: 252 : if (s2n_extension_supported_iana_value_to_id(extension_iana, &extension_type_id) == S2N_SUCCESS) {
1020 : 249 : s2n_parsed_extension *parsed_extension = NULL;
1021 [ + + ]: 249 : if (s2n_client_hello_get_parsed_extension(extension_iana, &ch->extensions, &parsed_extension) == S2N_SUCCESS) {
1022 : 89 : *exists = true;
1023 : 89 : }
1024 : 249 : return S2N_SUCCESS;
1025 : 249 : }
1026 : :
1027 : 3 : struct s2n_blob extension = { 0 };
1028 [ - + ]: 3 : POSIX_GUARD_RESULT(s2n_client_hello_get_raw_extension(extension_iana, &ch->extensions.raw, &extension));
1029 [ + + ]: 3 : if (extension.data != NULL) {
1030 : 2 : *exists = true;
1031 : 2 : }
1032 : 3 : return S2N_SUCCESS;
1033 : 3 : }
1034 : :
1035 : : int s2n_client_hello_get_supported_groups(struct s2n_client_hello *ch, uint16_t *groups,
1036 : : uint16_t groups_count_max, uint16_t *groups_count_out)
1037 : 34 : {
1038 [ + + ][ + - ]: 34 : POSIX_ENSURE_REF(groups_count_out);
1039 : 33 : *groups_count_out = 0;
1040 [ + - ][ + + ]: 33 : POSIX_ENSURE_REF(ch);
1041 [ + + ][ + - ]: 32 : POSIX_ENSURE_REF(groups);
1042 : :
1043 : 31 : s2n_parsed_extension *supported_groups_extension = NULL;
1044 [ + + ]: 31 : POSIX_GUARD(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SUPPORTED_GROUPS, &ch->extensions, &supported_groups_extension));
1045 [ - + ][ # # ]: 29 : POSIX_ENSURE_REF(supported_groups_extension);
1046 : :
1047 : 29 : struct s2n_stuffer extension_stuffer = { 0 };
1048 [ - + ]: 29 : POSIX_GUARD(s2n_stuffer_init_written(&extension_stuffer, &supported_groups_extension->extension));
1049 : :
1050 : 29 : uint16_t supported_groups_count = 0;
1051 [ + + ]: 29 : POSIX_GUARD_RESULT(s2n_supported_groups_parse_count(&extension_stuffer, &supported_groups_count));
1052 [ + - ][ + + ]: 27 : POSIX_ENSURE(supported_groups_count <= groups_count_max, S2N_ERR_INSUFFICIENT_MEM_SIZE);
1053 : :
1054 [ + + ]: 199 : for (size_t i = 0; i < supported_groups_count; i++) {
1055 : : /* s2n_stuffer_read_uint16 is used to read each of the supported groups in network-order
1056 : : * endianness.
1057 : : */
1058 [ - + ]: 173 : POSIX_GUARD(s2n_stuffer_read_uint16(&extension_stuffer, &groups[i]));
1059 : 173 : }
1060 : :
1061 : 26 : *groups_count_out = supported_groups_count;
1062 : :
1063 : 26 : return S2N_SUCCESS;
1064 : 26 : }
1065 : :
1066 : : int s2n_client_hello_get_server_name_length(struct s2n_client_hello *ch, uint16_t *length)
1067 : 3 : {
1068 [ + + ][ + - ]: 3 : POSIX_ENSURE_REF(ch);
1069 [ + + ][ + - ]: 2 : POSIX_ENSURE_REF(length);
1070 : 1 : *length = 0;
1071 : :
1072 : 1 : s2n_parsed_extension *server_name_extension = NULL;
1073 [ - + ]: 1 : POSIX_GUARD(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SERVER_NAME, &ch->extensions, &server_name_extension));
1074 [ # # ][ - + ]: 1 : POSIX_ENSURE_REF(server_name_extension);
1075 : :
1076 : 1 : struct s2n_stuffer extension_stuffer = { 0 };
1077 [ - + ]: 1 : POSIX_GUARD(s2n_stuffer_init_written(&extension_stuffer, &server_name_extension->extension));
1078 : :
1079 : 1 : struct s2n_blob blob = { 0 };
1080 [ - + ]: 1 : POSIX_GUARD_RESULT(s2n_client_server_name_parse(&extension_stuffer, &blob));
1081 : 1 : *length = blob.size;
1082 : :
1083 : 1 : return S2N_SUCCESS;
1084 : 1 : }
1085 : :
1086 : : int s2n_client_hello_get_server_name(struct s2n_client_hello *ch, uint8_t *server_name, uint16_t length, uint16_t *out_length)
1087 : 5 : {
1088 [ + + ][ + - ]: 5 : POSIX_ENSURE_REF(out_length);
1089 [ + - ][ + + ]: 4 : POSIX_ENSURE_REF(ch);
1090 [ + + ][ + - ]: 3 : POSIX_ENSURE_REF(server_name);
1091 : 2 : *out_length = 0;
1092 : :
1093 : 2 : s2n_parsed_extension *server_name_extension = NULL;
1094 [ - + ]: 2 : POSIX_GUARD(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_SERVER_NAME, &ch->extensions, &server_name_extension));
1095 [ - + ][ # # ]: 2 : POSIX_ENSURE_REF(server_name_extension);
1096 : :
1097 : 2 : struct s2n_stuffer extension_stuffer = { 0 };
1098 [ - + ]: 2 : POSIX_GUARD(s2n_stuffer_init_written(&extension_stuffer, &server_name_extension->extension));
1099 : :
1100 : 2 : struct s2n_blob blob = { 0 };
1101 [ - + ]: 2 : POSIX_GUARD_RESULT(s2n_client_server_name_parse(&extension_stuffer, &blob));
1102 [ + - ][ + + ]: 2 : POSIX_ENSURE_LTE(blob.size, length);
1103 [ - + ][ # # ]: 1 : POSIX_CHECKED_MEMCPY(server_name, blob.data, blob.size);
[ + - ]
1104 : :
1105 : 1 : *out_length = blob.size;
1106 : :
1107 : 1 : return S2N_SUCCESS;
1108 : 1 : }
|