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 <time.h>
17 : :
18 : : #include "api/s2n.h"
19 : : #include "error/s2n_errno.h"
20 : : #include "stuffer/s2n_stuffer.h"
21 : : #include "tls/s2n_alerts.h"
22 : : #include "tls/s2n_connection.h"
23 : : #include "tls/s2n_record.h"
24 : : #include "tls/s2n_resume.h"
25 : : #include "tls/s2n_tls.h"
26 : : #include "tls/s2n_tls13_handshake.h"
27 : : #include "utils/s2n_random.h"
28 : : #include "utils/s2n_safety.h"
29 : :
30 : : /*
31 : : * The maximum size of the NewSessionTicket message, not taking into account the
32 : : * ticket itself.
33 : : *
34 : : * To get the actual maximum size required for the NewSessionTicket message, we'll need
35 : : * to add the size of the ticket, which is much less predictable.
36 : : *
37 : : * This constant is enforced via unit tests.
38 : : */
39 : 300 : #define S2N_TLS13_MAX_FIXED_NEW_SESSION_TICKET_SIZE 104
40 : :
41 : : int s2n_server_nst_recv(struct s2n_connection *conn)
42 : 20 : {
43 [ - + ]: 20 : POSIX_GUARD(s2n_stuffer_read_uint32(&conn->handshake.io, &conn->ticket_lifetime_hint));
44 : :
45 : 20 : uint16_t session_ticket_len = 0;
46 [ - + ]: 20 : POSIX_GUARD(s2n_stuffer_read_uint16(&conn->handshake.io, &session_ticket_len));
47 : :
48 [ + + ]: 20 : if (session_ticket_len > 0) {
49 [ - + ]: 19 : POSIX_GUARD(s2n_realloc(&conn->client_ticket, session_ticket_len));
50 : :
51 [ - + ]: 19 : POSIX_GUARD(s2n_stuffer_read(&conn->handshake.io, &conn->client_ticket));
52 : :
53 [ + + ]: 19 : if (conn->config->session_ticket_cb != NULL) {
54 : 7 : size_t session_len = s2n_connection_get_session_length(conn);
55 : :
56 : : /* Alloc some memory for the serialized session ticket */
57 : 7 : DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free);
58 [ - + ]: 7 : POSIX_GUARD(s2n_alloc(&mem,
59 : 7 : S2N_STATE_FORMAT_LEN + S2N_SESSION_TICKET_SIZE_LEN + conn->client_ticket.size + S2N_TLS12_STATE_SIZE_IN_BYTES));
60 : :
61 [ - + ]: 7 : POSIX_GUARD(s2n_connection_get_session(conn, mem.data, session_len));
62 : 7 : uint32_t session_lifetime = s2n_connection_get_session_ticket_lifetime_hint(conn);
63 : :
64 : 7 : struct s2n_session_ticket ticket = { .ticket_data = mem, .session_lifetime = session_lifetime };
65 : :
66 [ - + ][ # # ]: 7 : POSIX_ENSURE(conn->config->session_ticket_cb(conn, conn->config->session_ticket_ctx, &ticket) >= S2N_SUCCESS,
67 : 7 : S2N_ERR_CANCELLED);
68 : 7 : }
69 : 19 : }
70 : :
71 : 20 : return S2N_SUCCESS;
72 : 20 : }
73 : :
74 : : static S2N_RESULT s2n_generate_ticket_lifetime(struct s2n_connection *conn, uint64_t key_intro_time,
75 : : uint32_t *ticket_lifetime)
76 : 407 : {
77 [ # # ][ - + ]: 407 : RESULT_ENSURE_REF(conn);
78 [ - + ][ # # ]: 407 : RESULT_ENSURE_REF(conn->config);
79 [ - + ][ # # ]: 407 : RESULT_ENSURE_MUT(ticket_lifetime);
80 : :
81 : 407 : uint64_t now = 0;
82 [ - + ]: 407 : RESULT_GUARD(s2n_config_wall_clock(conn->config, &now));
83 : :
84 : : /* Calculate ticket key age */
85 [ # # ][ - + ]: 407 : RESULT_ENSURE_GTE(now, key_intro_time);
86 : 407 : uint64_t ticket_key_age_in_nanos = now - key_intro_time;
87 : :
88 : : /* Calculate remaining key lifetime */
89 : 407 : uint64_t key_lifetime_in_nanos = conn->config->encrypt_decrypt_key_lifetime_in_nanos + conn->config->decrypt_key_lifetime_in_nanos;
90 [ - + ][ # # ]: 407 : RESULT_ENSURE_GTE(key_lifetime_in_nanos, ticket_key_age_in_nanos);
91 : 407 : uint32_t remaining_key_lifetime = (key_lifetime_in_nanos - ticket_key_age_in_nanos) / ONE_SEC_IN_NANOS;
92 : :
93 : 407 : uint32_t session_lifetime = conn->config->session_state_lifetime_in_nanos / ONE_SEC_IN_NANOS;
94 : :
95 : : /* Min of remaining key lifetime and session */
96 [ + + ]: 407 : uint32_t min_lifetime = S2N_MIN(remaining_key_lifetime, session_lifetime);
97 : :
98 : : /* In TLS1.3 we take into account keying material lifetime */
99 [ + + ]: 407 : if (conn->actual_protocol_version == S2N_TLS13) {
100 : 388 : uint32_t key_material_lifetime = conn->server_keying_material_lifetime;
101 : 388 : struct s2n_psk *chosen_psk = conn->psk_params.chosen_psk;
102 [ + + ]: 388 : if (chosen_psk) {
103 [ - + ][ # # ]: 186 : RESULT_ENSURE_GTE(chosen_psk->keying_material_expiration, now);
104 : 186 : uint32_t psk_key_material_lifetime = (chosen_psk->keying_material_expiration - now) / ONE_SEC_IN_NANOS;
105 [ - + ]: 186 : key_material_lifetime = S2N_MIN(key_material_lifetime, psk_key_material_lifetime);
106 : 186 : }
107 [ + - ]: 388 : min_lifetime = S2N_MIN(min_lifetime, key_material_lifetime);
108 : 388 : }
109 : :
110 : : /**
111 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
112 : : *# Servers MUST NOT use any value greater than
113 : : *# 604800 seconds (7 days).
114 : : **/
115 [ + - ]: 407 : *ticket_lifetime = S2N_MIN(min_lifetime, ONE_WEEK_IN_SEC);
116 : :
117 : 407 : return S2N_RESULT_OK;
118 : 407 : }
119 : :
120 : : int s2n_server_nst_send(struct s2n_connection *conn)
121 : 23 : {
122 [ - + ][ # # ]: 23 : POSIX_ENSURE_REF(conn);
123 : :
124 : 23 : uint8_t data[S2N_TLS12_TICKET_SIZE_IN_BYTES] = { 0 };
125 : 23 : struct s2n_blob session_ticket = { 0 };
126 [ - + ]: 23 : POSIX_GUARD(s2n_blob_init(&session_ticket, data, sizeof(data)));
127 : :
128 : 23 : uint32_t lifetime_hint_in_secs = 0;
129 : :
130 : : /* Send a zero-length ticket in the NewSessionTicket message if the server changes
131 : : * its mind mid-handshake or if there are no valid encrypt keys currently available.
132 : : *
133 : : *= https://www.rfc-editor.org/rfc/rfc5077#section-3.3
134 : : *# If the server determines that it does not want to include a
135 : : *# ticket after it has included the SessionTicket extension in the
136 : : *# ServerHello, then it sends a zero-length ticket in the
137 : : *# NewSessionTicket handshake message.
138 : : **/
139 [ + + ]: 23 : if (s2n_result_is_error(s2n_server_nst_write(conn, &lifetime_hint_in_secs, &session_ticket))) {
140 [ - + ]: 3 : POSIX_GUARD(s2n_stuffer_write_uint32(&conn->handshake.io, 0));
141 [ - + ]: 3 : POSIX_GUARD(s2n_stuffer_write_uint16(&conn->handshake.io, 0));
142 : 3 : return S2N_SUCCESS;
143 : 3 : }
144 : :
145 [ - + ]: 20 : POSIX_GUARD(s2n_stuffer_write_uint32(&conn->handshake.io, lifetime_hint_in_secs));
146 [ - + ]: 20 : POSIX_GUARD(s2n_stuffer_write_uint16(&conn->handshake.io, session_ticket.size));
147 [ - + ]: 20 : POSIX_GUARD(s2n_stuffer_write(&conn->handshake.io, &session_ticket));
148 : :
149 : : /* For parity with TLS1.3, track the single ticket sent.
150 : : * This simplifies s2n_connection_get_tickets_sent.
151 : : */
152 : 20 : conn->tickets_sent++;
153 : 20 : return S2N_SUCCESS;
154 : 20 : }
155 : :
156 : : S2N_RESULT s2n_server_nst_write(struct s2n_connection *conn, uint32_t *lifetime_hint_in_secs,
157 : : struct s2n_blob *session_ticket)
158 : 23 : {
159 [ # # ][ - + ]: 23 : RESULT_ENSURE_REF(conn);
160 [ + - ][ + + ]: 23 : RESULT_ENSURE(s2n_server_sending_nst(conn), S2N_ERR_SENDING_NST);
[ + - ]
161 : :
162 : 21 : struct s2n_stuffer output = { 0 };
163 [ - + ]: 21 : RESULT_GUARD_POSIX(s2n_stuffer_init(&output, session_ticket));
164 : :
165 : : /* The lookup returns a pointer into the shared, unsynchronized config->ticket_keys array.
166 : : * A concurrent call to s2n_config_add_ticket_crypto_key (key rotation) can reallocate,
167 : : * shift, or overwrite that array, invalidating the pointer. Copy the key to the stack
168 : : * immediately so later operations never dereference memory another thread might change. */
169 : 21 : struct s2n_ticket_key *key_ptr = s2n_get_ticket_encrypt_decrypt_key(conn->config);
170 [ + - ][ + + ]: 21 : RESULT_ENSURE(key_ptr != NULL, S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY);
171 : 20 : struct s2n_ticket_key key = *key_ptr;
172 : :
173 [ - + ]: 20 : RESULT_GUARD(s2n_generate_ticket_lifetime(conn, key.intro_timestamp, lifetime_hint_in_secs));
174 [ - + ]: 20 : RESULT_GUARD(s2n_resume_encrypt_session_ticket(conn, &key, &output));
175 : :
176 : 20 : return S2N_RESULT_OK;
177 : 20 : }
178 : :
179 : : S2N_RESULT s2n_tls13_server_nst_send(struct s2n_connection *conn, s2n_blocked_status *blocked)
180 : 51450 : {
181 [ # # ][ - + ]: 51450 : RESULT_ENSURE_REF(conn);
182 [ + + ][ + - ]: 51450 : RESULT_ENSURE_GTE(conn->actual_protocol_version, S2N_TLS13);
183 : :
184 : : /* Usually tickets are sent immediately after the handshake.
185 : : * If possible, reuse the handshake IO stuffer before it's wiped.
186 : : *
187 : : * Note: handshake.io isn't explicitly dedicated to only reading or only writing,
188 : : * so we have to be careful using it outside of s2n_negotiate.
189 : : * If we use it for writing here, we CAN'T use it for reading any post-handshake messages.
190 : : */
191 : 51449 : struct s2n_stuffer *nst_stuffer = &conn->handshake.io;
192 : :
193 [ + + ][ + + ]: 51449 : if (conn->mode != S2N_SERVER || !conn->config->use_tickets) {
194 : 50983 : return S2N_RESULT_OK;
195 : 50983 : }
196 : :
197 : : /* Don't issue tickets under client auth: resumption is not supported in that
198 : : * case (see s2n_select_resumption_psk), so the ticket could never be used.
199 : : */
200 [ + + ]: 466 : if (s2n_connection_is_client_auth_enabled(conn)) {
201 : 2 : return S2N_RESULT_OK;
202 : 2 : }
203 : :
204 : : /* Legacy behavior is that the s2n server sends a NST even if the client did not indicate support
205 : : * for resumption or does not support the psk_dhe_ke mode. This is potentially wasteful so we
206 : : * choose to not extend this behavior to QUIC.
207 : : */
208 [ + + ][ + + ]: 464 : if (conn->quic_enabled && conn->psk_params.psk_ke_mode != S2N_PSK_DHE_KE) {
209 : 3 : return S2N_RESULT_OK;
210 : 3 : }
211 : :
212 : : /* No-op if all tickets already sent.
213 : : * Clean up the stuffer used for the ticket to conserve memory. */
214 [ + + ]: 461 : if (conn->tickets_to_send == conn->tickets_sent) {
215 [ - + ]: 160 : RESULT_GUARD_POSIX(s2n_stuffer_resize(nst_stuffer, 0));
216 : 160 : return S2N_RESULT_OK;
217 : 160 : }
218 : :
219 : : /**
220 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
221 : : *# Note that in principle it is possible to continue issuing new tickets
222 : : *# which indefinitely extend the lifetime of the keying material
223 : : *# originally derived from an initial non-PSK handshake (which was most
224 : : *# likely tied to the peer's certificate). It is RECOMMENDED that
225 : : *# implementations place limits on the total lifetime of such keying
226 : : *# material; these limits should take into account the lifetime of the
227 : : *# peer's certificate, the likelihood of intervening revocation, and the
228 : : *# time since the peer's online CertificateVerify signature.
229 : : */
230 [ + + ]: 301 : if (s2n_result_is_error(s2n_psk_validate_keying_material(conn))) {
231 : 1 : conn->tickets_to_send = conn->tickets_sent;
232 : 1 : return S2N_RESULT_OK;
233 : 1 : }
234 : :
235 [ # # ][ - + ]: 300 : RESULT_ENSURE(conn->tickets_sent <= conn->tickets_to_send, S2N_ERR_INTEGER_OVERFLOW);
236 : :
237 : 300 : size_t session_state_size = 0;
238 [ - + ]: 300 : RESULT_GUARD(s2n_connection_get_session_state_size(conn, &session_state_size));
239 : 300 : const size_t maximum_nst_size = session_state_size + S2N_TLS13_MAX_FIXED_NEW_SESSION_TICKET_SIZE;
240 [ + + ]: 300 : if (s2n_stuffer_space_remaining(nst_stuffer) < maximum_nst_size) {
241 [ - + ]: 129 : RESULT_GUARD_POSIX(s2n_stuffer_resize(nst_stuffer, maximum_nst_size));
242 : 129 : }
243 : :
244 [ + + ]: 599 : while (conn->tickets_to_send - conn->tickets_sent > 0) {
245 [ + + ]: 325 : if (s2n_result_is_error(s2n_tls13_server_nst_write(conn, nst_stuffer))) {
246 : 5 : return S2N_RESULT_OK;
247 : 5 : }
248 : :
249 [ + + ]: 320 : RESULT_GUARD(s2n_post_handshake_write_records(conn, blocked));
250 : 320 : }
251 : :
252 : 274 : return S2N_RESULT_OK;
253 : 300 : }
254 : :
255 : : /**
256 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
257 : : *# A per-ticket value that is unique across all tickets
258 : : *# issued on this connection.
259 : : **/
260 : : static S2N_RESULT s2n_generate_ticket_nonce(uint16_t value, struct s2n_blob *output)
261 : 387 : {
262 [ # # ][ - + ]: 387 : RESULT_ENSURE_MUT(output);
263 : :
264 : 387 : struct s2n_stuffer stuffer = { 0 };
265 [ - + ]: 387 : RESULT_GUARD_POSIX(s2n_stuffer_init(&stuffer, output));
266 [ - + ]: 387 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(&stuffer, value));
267 : :
268 : 387 : return S2N_RESULT_OK;
269 : 387 : }
270 : :
271 : : /**
272 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
273 : : *# A securely generated, random 32-bit value that is
274 : : *# used to obscure the age of the ticket that the client includes in
275 : : *# the "pre_shared_key" extension.
276 : : **/
277 : : static S2N_RESULT s2n_generate_ticket_age_add(struct s2n_blob *random_data, uint32_t *ticket_age_add)
278 : 387 : {
279 [ - + ][ # # ]: 387 : RESULT_ENSURE_REF(random_data);
280 [ - + ][ # # ]: 387 : RESULT_ENSURE_REF(ticket_age_add);
281 : :
282 : 387 : struct s2n_stuffer stuffer = { 0 };
283 [ - + ]: 387 : RESULT_GUARD_POSIX(s2n_stuffer_init(&stuffer, random_data));
284 [ - + ]: 387 : RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&stuffer, random_data->size));
285 [ - + ]: 387 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint32(&stuffer, ticket_age_add));
286 : :
287 : 387 : return S2N_RESULT_OK;
288 : 387 : }
289 : :
290 : : /**
291 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
292 : : *# The PSK associated with the ticket is computed as:
293 : : *#
294 : : *# HKDF-Expand-Label(resumption_master_secret,
295 : : *# "resumption", ticket_nonce, Hash.length)
296 : : **/
297 : : static int s2n_generate_session_secret(struct s2n_connection *conn, struct s2n_blob *nonce, struct s2n_blob *output)
298 : 855 : {
299 [ - + ][ # # ]: 855 : POSIX_ENSURE_REF(conn);
300 [ - + ][ # # ]: 855 : POSIX_ENSURE_REF(nonce);
301 [ - + ][ # # ]: 855 : POSIX_ENSURE_REF(output);
302 : :
303 [ - + ]: 855 : s2n_tls13_connection_keys(secrets, conn);
304 : 855 : struct s2n_blob master_secret = { 0 };
305 [ - + ]: 855 : POSIX_GUARD(s2n_blob_init(&master_secret, conn->secrets.version.tls13.resumption_master_secret, secrets.size));
306 [ - + ]: 855 : POSIX_GUARD(s2n_realloc(output, secrets.size));
307 [ - + ]: 855 : POSIX_GUARD_RESULT(s2n_tls13_derive_session_ticket_secret(&secrets, &master_secret, nonce, output));
308 : :
309 : 855 : return S2N_SUCCESS;
310 : 855 : }
311 : :
312 : : S2N_RESULT s2n_tls13_server_nst_write(struct s2n_connection *conn, struct s2n_stuffer *output)
313 : 429 : {
314 [ - + ][ # # ]: 429 : RESULT_ENSURE_REF(conn);
315 [ - + ][ # # ]: 429 : RESULT_ENSURE_REF(output);
316 : :
317 : : /* The lookup returns a pointer into the shared, unsynchronized config->ticket_keys array.
318 : : * A concurrent call to s2n_config_add_ticket_crypto_key (key rotation) can reallocate,
319 : : * shift, or overwrite that array, invalidating the pointer. Copy the key to the stack
320 : : * immediately so later operations never dereference memory another thread might change. */
321 : 429 : struct s2n_ticket_key *key_ptr = s2n_get_ticket_encrypt_decrypt_key(conn->config);
322 [ + + ][ + - ]: 429 : RESULT_ENSURE(key_ptr != NULL, S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY);
323 : 424 : struct s2n_ticket_key key = *key_ptr;
324 : :
325 : 424 : struct s2n_ticket_fields *ticket_fields = &conn->tls13_ticket_fields;
326 : :
327 : : /* Write message type because session resumption in TLS13 is a post-handshake message */
328 [ - + ]: 424 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(output, TLS_SERVER_NEW_SESSION_TICKET));
329 : :
330 : 424 : struct s2n_stuffer_reservation message_size = { 0 };
331 [ - + ]: 424 : RESULT_GUARD_POSIX(s2n_stuffer_reserve_uint24(output, &message_size));
332 : :
333 : 424 : uint32_t ticket_lifetime_in_secs = 0;
334 [ - + ]: 424 : RESULT_GUARD(s2n_generate_ticket_lifetime(conn, key.intro_timestamp, &ticket_lifetime_in_secs));
335 : :
336 [ + + ][ + - ]: 424 : RESULT_ENSURE(ticket_lifetime_in_secs > 0, S2N_ERR_ZERO_LIFETIME_TICKET);
337 [ - + ]: 422 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint32(output, ticket_lifetime_in_secs));
338 : :
339 : : /* Get random data to use as ticket_age_add value */
340 : 422 : uint8_t data[sizeof(uint32_t)] = { 0 };
341 : 422 : struct s2n_blob random_data = { 0 };
342 [ - + ]: 422 : RESULT_GUARD_POSIX(s2n_blob_init(&random_data, data, sizeof(data)));
343 : : /**
344 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
345 : : *# The server MUST generate a fresh value
346 : : *# for each ticket it sends.
347 : : **/
348 [ - + ]: 422 : RESULT_GUARD(s2n_get_private_random_data(&random_data));
349 [ - + ]: 422 : RESULT_GUARD(s2n_generate_ticket_age_add(&random_data, &ticket_fields->ticket_age_add));
350 [ - + ]: 422 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint32(output, ticket_fields->ticket_age_add));
351 : :
352 : : /* Write ticket nonce */
353 : 422 : uint8_t nonce_data[sizeof(uint16_t)] = { 0 };
354 : 422 : struct s2n_blob nonce = { 0 };
355 [ - + ]: 422 : RESULT_GUARD_POSIX(s2n_blob_init(&nonce, nonce_data, sizeof(nonce_data)));
356 [ - + ]: 422 : RESULT_GUARD(s2n_generate_ticket_nonce(conn->tickets_sent, &nonce));
357 [ - + ]: 422 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(output, nonce.size));
358 [ - + ]: 422 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, nonce.data, nonce.size));
359 : :
360 : : /* Derive individual session ticket secret */
361 [ - + ]: 422 : RESULT_GUARD_POSIX(s2n_generate_session_secret(conn, &nonce, &ticket_fields->session_secret));
362 : :
363 : : /* Write ticket */
364 : 422 : struct s2n_stuffer_reservation ticket_size = { 0 };
365 [ - + ]: 422 : RESULT_GUARD_POSIX(s2n_stuffer_reserve_uint16(output, &ticket_size));
366 [ - + ]: 422 : RESULT_GUARD(s2n_resume_encrypt_session_ticket(conn, &key, output));
367 [ + + ]: 422 : RESULT_GUARD_POSIX(s2n_stuffer_write_vector_size(&ticket_size));
368 : :
369 [ - + ]: 421 : RESULT_GUARD_POSIX(s2n_extension_list_send(S2N_EXTENSION_LIST_NST, conn, output));
370 : :
371 [ - + ]: 421 : RESULT_GUARD_POSIX(s2n_stuffer_write_vector_size(&message_size));
372 : :
373 [ + - ][ + + ]: 421 : RESULT_ENSURE(conn->tickets_sent < UINT16_MAX, S2N_ERR_INTEGER_OVERFLOW);
374 : 420 : conn->tickets_sent++;
375 : :
376 : 420 : return S2N_RESULT_OK;
377 : 421 : }
378 : :
379 : : /**
380 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
381 : : *# struct {
382 : : *# uint32 ticket_lifetime;
383 : : *# uint32 ticket_age_add;
384 : : *# opaque ticket_nonce<0..255>;
385 : : *# opaque ticket<1..2^16-1>;
386 : : *# Extension extensions<0..2^16-2>;
387 : : *# } NewSessionTicket;
388 : : **/
389 : : S2N_RESULT s2n_tls13_server_nst_recv(struct s2n_connection *conn, struct s2n_stuffer *input)
390 : 487 : {
391 [ # # ][ - + ]: 487 : RESULT_ENSURE_REF(conn);
392 [ - + ][ # # ]: 487 : RESULT_ENSURE_REF(input);
393 [ - + ][ # # ]: 487 : RESULT_ENSURE_REF(conn->config);
394 : :
395 [ + + ][ + - ]: 487 : RESULT_ENSURE(conn->actual_protocol_version >= S2N_TLS13, S2N_ERR_BAD_MESSAGE);
396 [ + + ][ + - ]: 486 : RESULT_ENSURE(conn->mode == S2N_CLIENT, S2N_ERR_BAD_MESSAGE);
397 : :
398 [ + + ]: 483 : if (!conn->config->use_tickets) {
399 : 1 : return S2N_RESULT_OK;
400 : 1 : }
401 : 482 : struct s2n_ticket_fields *ticket_fields = &conn->tls13_ticket_fields;
402 : :
403 : : /* Handle `ticket_lifetime` field */
404 : 482 : uint32_t ticket_lifetime = 0;
405 [ - + ]: 482 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint32(input, &ticket_lifetime));
406 : : /**
407 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
408 : : *# Servers MUST NOT use any value greater than
409 : : *# 604800 seconds (7 days).
410 : : */
411 [ + + ][ + - ]: 482 : RESULT_ENSURE(ticket_lifetime <= ONE_WEEK_IN_SEC, S2N_ERR_BAD_MESSAGE);
412 : : /**
413 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
414 : : *# The value of zero indicates that the
415 : : *# ticket should be discarded immediately.
416 : : */
417 [ + + ]: 481 : if (ticket_lifetime == 0) {
418 : 1 : return S2N_RESULT_OK;
419 : 1 : }
420 : 480 : conn->ticket_lifetime_hint = ticket_lifetime;
421 : :
422 : : /* Handle `ticket_age_add` field */
423 [ - + ]: 480 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint32(input, &ticket_fields->ticket_age_add));
424 : :
425 : : /* Handle `ticket_nonce` field */
426 : 480 : uint8_t ticket_nonce_len = 0;
427 [ - + ]: 480 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(input, &ticket_nonce_len));
428 : 480 : uint8_t nonce_data[UINT8_MAX] = { 0 };
429 : 480 : struct s2n_blob nonce = { 0 };
430 [ - + ]: 480 : RESULT_GUARD_POSIX(s2n_blob_init(&nonce, nonce_data, ticket_nonce_len));
431 [ - + ]: 480 : RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, nonce.data, ticket_nonce_len));
432 [ - + ]: 480 : RESULT_GUARD_POSIX(s2n_generate_session_secret(conn, &nonce, &ticket_fields->session_secret));
433 : :
434 : : /* Handle `ticket` field */
435 : 480 : uint16_t session_ticket_len = 0;
436 [ - + ]: 480 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(input, &session_ticket_len));
437 [ - + ][ # # ]: 480 : RESULT_ENSURE(session_ticket_len > 0, S2N_ERR_SAFETY);
438 [ - + ]: 480 : RESULT_GUARD_POSIX(s2n_realloc(&conn->client_ticket, session_ticket_len));
439 [ - + ]: 480 : RESULT_GUARD_POSIX(s2n_stuffer_read(input, &conn->client_ticket));
440 : :
441 : : /* Handle `extensions` field */
442 [ - + ]: 480 : RESULT_GUARD_POSIX(s2n_extension_list_recv(S2N_EXTENSION_LIST_NST, conn, input));
443 : :
444 [ + + ]: 480 : if (conn->config->session_ticket_cb != NULL) {
445 : : /* Retrieve serialized session data */
446 : 433 : const uint16_t session_state_size = s2n_connection_get_session_length(conn);
447 : 433 : DEFER_CLEANUP(struct s2n_blob session_state = { 0 }, s2n_free);
448 [ - + ]: 433 : RESULT_GUARD_POSIX(s2n_realloc(&session_state, session_state_size));
449 [ - + ]: 433 : RESULT_GUARD_POSIX(s2n_connection_get_session(conn, session_state.data, session_state.size));
450 : :
451 : 433 : struct s2n_session_ticket ticket = {
452 : 433 : .ticket_data = session_state,
453 : 433 : .session_lifetime = ticket_lifetime
454 : 433 : };
455 [ - + ][ # # ]: 433 : RESULT_ENSURE(conn->config->session_ticket_cb(conn, conn->config->session_ticket_ctx, &ticket) >= S2N_SUCCESS,
456 : 433 : S2N_ERR_CANCELLED);
457 : 433 : }
458 : :
459 : 480 : return S2N_RESULT_OK;
460 : 480 : }
|