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 : : #include "tls/s2n_resume.h"
16 : :
17 : : #include <math.h>
18 : :
19 : : #include "api/s2n.h"
20 : : #include "error/s2n_errno.h"
21 : : #include "stuffer/s2n_stuffer.h"
22 : : #include "tls/s2n_cipher_suites.h"
23 : : #include "tls/s2n_connection.h"
24 : : #include "tls/s2n_crypto.h"
25 : : #include "tls/s2n_tls.h"
26 : : #include "utils/s2n_blob.h"
27 : : #include "utils/s2n_random.h"
28 : : #include "utils/s2n_safety.h"
29 : :
30 : : int s2n_allowed_to_cache_connection(struct s2n_connection *conn)
31 : 12646 : {
32 : : /* We're unable to cache connections with a Client Cert since we currently don't serialize the Client Cert,
33 : : * which means that callers won't have access to the Client's Cert if the connection is resumed. */
34 [ + + ]: 12646 : if (s2n_connection_is_client_auth_enabled(conn)) {
35 : 4807 : return 0;
36 : 4807 : }
37 : :
38 : 7839 : struct s2n_config *config = conn->config;
39 : :
40 [ # # ][ - + ]: 7839 : POSIX_ENSURE_REF(config);
41 : 7839 : return config->use_session_cache;
42 : 7839 : }
43 : :
44 : : /* If a protocol version is required before the actual_protocol_version
45 : : * is negotiated, we should fall back to resume_protocol_version if available.
46 : : *
47 : : * This covers the case where the application requests a ticket / session state
48 : : * before a NewSessionTicket message has been sent or received. Historically,
49 : : * in that case we return the ticket / session state already set for the connection.
50 : : * resume_protocol_version represents the protocol version of that existing ticket / state.
51 : : */
52 : : static uint8_t s2n_resume_protocol_version(struct s2n_connection *conn)
53 : 976 : {
54 [ + + ][ - + ]: 976 : if (!IS_NEGOTIATED(conn) && conn->resume_protocol_version) {
55 : 0 : return conn->resume_protocol_version;
56 : 976 : } else {
57 : 976 : return conn->actual_protocol_version;
58 : 976 : }
59 : 976 : }
60 : :
61 : : /**
62 : : * The header framing a server session ticket. On the wire, the encrypted
63 : : * session state follows this header. The header itself will always be plaintext.
64 : : */
65 : : struct s2n_ticket_header {
66 : : uint8_t ticket_version;
67 : : uint8_t key_name[S2N_TICKET_KEY_NAME_LEN];
68 : : uint8_t nonce[S2N_AES256_KEY_LEN];
69 : : uint8_t iv[S2N_TLS_GCM_IV_LEN];
70 : : };
71 : :
72 : : static S2N_RESULT s2n_ticket_header_init(struct s2n_ticket_header *header, struct s2n_ticket_key *key)
73 : 210 : {
74 [ # # ][ - + ]: 210 : RESULT_ENSURE_REF(header);
75 [ # # ][ - + ]: 210 : RESULT_ENSURE_REF(key);
76 : :
77 : 210 : header->ticket_version = S2N_PRE_ENCRYPTED_STATE_V1;
78 [ # # ][ - + ]: 210 : RESULT_CHECKED_MEMCPY(header->key_name, key->key_name, sizeof(header->key_name));
[ + - ]
79 : :
80 : : /* The nonce is both serialized and used as the KDF input to derive the
81 : : * per-ticket encryption key, so it must be random per ticket. */
82 : 210 : struct s2n_blob nonce_blob = { 0 };
83 [ - + ]: 210 : RESULT_GUARD_POSIX(s2n_blob_init(&nonce_blob, header->nonce, sizeof(header->nonce)));
84 [ - + ]: 210 : RESULT_GUARD(s2n_get_public_random_data(&nonce_blob));
85 : :
86 : 210 : struct s2n_blob iv_blob = { 0 };
87 [ - + ]: 210 : RESULT_GUARD_POSIX(s2n_blob_init(&iv_blob, header->iv, sizeof(header->iv)));
88 [ - + ]: 210 : RESULT_GUARD(s2n_get_public_random_data(&iv_blob));
89 : :
90 : 210 : return S2N_RESULT_OK;
91 : 210 : }
92 : :
93 : : static S2N_RESULT s2n_ticket_header_decode(struct s2n_ticket_header *header, struct s2n_stuffer *from)
94 : 35 : {
95 [ # # ][ - + ]: 35 : RESULT_ENSURE_REF(header);
96 [ # # ][ - + ]: 35 : RESULT_ENSURE_REF(from);
97 : :
98 [ - + ]: 35 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(from, &header->ticket_version));
99 [ + + ][ + - ]: 35 : RESULT_ENSURE_EQ(header->ticket_version, S2N_PRE_ENCRYPTED_STATE_V1);
100 [ - + ]: 28 : RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(from, header->key_name, sizeof(header->key_name)));
101 [ - + ]: 28 : RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(from, header->nonce, sizeof(header->nonce)));
102 [ - + ]: 28 : RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(from, header->iv, sizeof(header->iv)));
103 : :
104 : 28 : return S2N_RESULT_OK;
105 : 28 : }
106 : :
107 : : static S2N_RESULT s2n_ticket_header_encode(struct s2n_ticket_header *header, struct s2n_stuffer *to)
108 : 210 : {
109 [ - + ][ # # ]: 210 : RESULT_ENSURE_REF(header);
110 [ # # ][ - + ]: 210 : RESULT_ENSURE_REF(to);
111 : :
112 [ - + ]: 210 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(to, header->ticket_version));
113 [ - + ]: 210 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(to, header->key_name, sizeof(header->key_name)));
114 [ - + ]: 210 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(to, header->nonce, sizeof(header->nonce)));
115 [ - + ]: 210 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(to, header->iv, sizeof(header->iv)));
116 : :
117 : 210 : return S2N_RESULT_OK;
118 : 210 : }
119 : :
120 : : /**
121 : : * The state encrypted and embedded into a session ticket by the server for a
122 : : * TLS 1.2 connection.
123 : : */
124 : : struct s2n_resumption_state_tls12 {
125 : : /* the serialization version of the resumption state */
126 : : uint8_t version;
127 : : /* s2n-internal protocol version representation. NOT the format used in TLS messages */
128 : : uint8_t condensed_protocol_version;
129 : : uint8_t cipher_id[S2N_TLS_CIPHER_SUITE_LEN];
130 : : uint64_t ticket_issue_unix_ns;
131 : : uint8_t master_secret[S2N_TLS_SECRET_LEN];
132 : : uint8_t ems_negotiated;
133 : : };
134 : :
135 : : static S2N_RESULT s2n_resumption_state_tls12_init(struct s2n_resumption_state_tls12 *state, struct s2n_connection *conn)
136 : 41 : {
137 [ - + ][ # # ]: 41 : RESULT_ENSURE_REF(state);
138 [ # # ][ - + ]: 41 : RESULT_ENSURE_REF(conn);
139 [ - + ][ # # ]: 41 : RESULT_ENSURE_REF(conn->secure);
140 [ # # ][ - + ]: 41 : RESULT_ENSURE_REF(conn->secure->cipher_suite);
141 : :
142 : 41 : uint64_t now = 0;
143 [ - + ]: 41 : RESULT_GUARD(s2n_config_wall_clock(conn->config, &now));
144 : :
145 : 41 : state->version = S2N_SERIALIZED_FORMAT_TLS12_V3;
146 : 41 : state->condensed_protocol_version = s2n_resume_protocol_version(conn);
147 [ # # ][ - + ]: 41 : RESULT_CHECKED_MEMCPY(state->cipher_id, conn->secure->cipher_suite->iana_value, sizeof(state->cipher_id));
[ + - ]
148 : 41 : state->ticket_issue_unix_ns = now;
149 [ - + ][ # # ]: 41 : RESULT_CHECKED_MEMCPY(state->master_secret, conn->secrets.version.tls12.master_secret, sizeof(state->master_secret));
[ + - ]
150 : 41 : state->ems_negotiated = conn->ems_negotiated;
151 : 41 : return S2N_RESULT_OK;
152 : 41 : }
153 : :
154 : : static S2N_RESULT s2n_resumption_state_tls12_encode(struct s2n_resumption_state_tls12 *state, struct s2n_stuffer *to)
155 : 41 : {
156 [ - + ][ # # ]: 41 : RESULT_ENSURE_REF(state);
157 [ # # ][ - + ]: 41 : RESULT_ENSURE_REF(to);
158 : :
159 [ - + ]: 41 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(to, state->version));
160 [ - + ]: 41 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(to, state->condensed_protocol_version));
161 [ - + ]: 41 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(to, state->cipher_id, sizeof(state->cipher_id)));
162 [ - + ]: 41 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint64(to, state->ticket_issue_unix_ns));
163 [ - + ]: 41 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(to, state->master_secret, sizeof(state->master_secret)));
164 [ - + ]: 41 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(to, state->ems_negotiated));
165 : :
166 : 41 : return S2N_RESULT_OK;
167 : 41 : }
168 : :
169 : : static S2N_RESULT s2n_resumption_state_tls12_decode(struct s2n_resumption_state_tls12 *state, struct s2n_stuffer *from)
170 : 24 : {
171 [ # # ][ - + ]: 24 : RESULT_ENSURE_REF(state);
172 [ - + ][ # # ]: 24 : RESULT_ENSURE_REF(from);
173 : :
174 [ - + ]: 24 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(from, &state->version));
175 [ - + ][ # # ]: 24 : RESULT_ENSURE(state->version == S2N_SERIALIZED_FORMAT_TLS12_V3, S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
176 [ - + ]: 24 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(from, &state->condensed_protocol_version));
177 [ - + ]: 24 : RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(from, state->cipher_id, sizeof(state->cipher_id)));
178 [ - + ]: 24 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint64(from, &state->ticket_issue_unix_ns));
179 [ - + ]: 24 : RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(from, state->master_secret, sizeof(state->master_secret)));
180 [ - + ]: 24 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(from, &state->ems_negotiated));
181 : :
182 : 24 : return S2N_RESULT_OK;
183 : 24 : }
184 : :
185 : : /**
186 : : * Try to apply a TLS 1.2 session ticket to a server connection.
187 : : *
188 : : * This internally checks that the cipher suite, expiration, etc are all correct
189 : : */
190 : : static S2N_RESULT s2n_resumption_state_tls12_apply_server(struct s2n_resumption_state_tls12 *state, struct s2n_connection *conn)
191 : 11 : {
192 [ - + ][ # # ]: 11 : RESULT_ENSURE_REF(state);
193 [ # # ][ - + ]: 11 : RESULT_ENSURE_REF(conn);
194 [ - + ][ # # ]: 11 : RESULT_ENSURE_REF(conn->secure);
195 [ # # ][ - + ]: 11 : RESULT_ENSURE_REF(conn->secure->cipher_suite);
196 [ - + ][ # # ]: 11 : RESULT_ENSURE_REF(conn->config);
197 : :
198 [ # # ][ - + ]: 11 : RESULT_ENSURE(state->condensed_protocol_version == conn->actual_protocol_version,
199 : 11 : S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
200 : :
201 [ - + ][ # # ]: 11 : RESULT_ENSURE(s2n_constant_time_equals(conn->secure->cipher_suite->iana_value, state->cipher_id,
202 : 11 : sizeof(state->cipher_id)),
203 : 11 : S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
204 : :
205 : 11 : uint64_t now = 0;
206 [ - + ]: 11 : RESULT_GUARD(s2n_config_wall_clock(conn->config, &now));
207 [ - + ][ # # ]: 11 : RESULT_ENSURE(state->ticket_issue_unix_ns <= now, S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
208 [ - + ][ # # ]: 11 : RESULT_ENSURE(now - state->ticket_issue_unix_ns <= conn->config->session_state_lifetime_in_nanos,
209 : 11 : S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
210 : :
211 [ - + ][ # # ]: 11 : RESULT_CHECKED_MEMCPY(conn->secrets.version.tls12.master_secret, state->master_secret,
[ + - ]
212 : 11 : sizeof(state->master_secret));
213 : :
214 : : /**
215 : : *= https://www.rfc-editor.org/rfc/rfc7627#section-5.3
216 : : *# o If the original session did not use the "extended_master_secret"
217 : : *# extension but the new ClientHello contains the extension, then the
218 : : *# server MUST NOT perform the abbreviated handshake. Instead, it
219 : : *# SHOULD continue with a full handshake (as described in
220 : : *# Section 5.2) to negotiate a new session.
221 : : *#
222 : : *# o If the original session used the "extended_master_secret"
223 : : *# extension but the new ClientHello does not contain it, the server
224 : : *# MUST abort the abbreviated handshake.
225 : : **/
226 [ + + ]: 11 : if (conn->ems_negotiated != state->ems_negotiated) {
227 : : /* The session ticket needs to have the same EMS state as the current session. If it doesn't
228 : : * have the same state, the current session takes the state of the session ticket and errors.
229 : : * If the deserialization process errors, we will use this state in a few extra checks
230 : : * to determine if we can fallback to a full handshake.
231 : : */
232 : 4 : conn->ems_negotiated = state->ems_negotiated;
233 [ + - ]: 4 : RESULT_BAIL(S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
234 : 4 : }
235 : :
236 : 7 : return S2N_RESULT_OK;
237 : 11 : }
238 : :
239 : : /* Computes the keying material expiration timestamp to serialize.
240 : : *
241 : : * For clients, this is a sentinel value of 0. They don't issue tickets, so this
242 : : * doesn't apply. But we want to be able to use the same serialization code.
243 : : */
244 : : static S2N_RESULT s2n_tls13_keying_material_expiration(struct s2n_connection *conn,
245 : : uint64_t now, uint64_t *expiration_timestamp)
246 : 396 : {
247 [ - + ][ # # ]: 396 : RESULT_ENSURE_REF(conn);
248 [ - + ][ # # ]: 396 : RESULT_ENSURE_REF(expiration_timestamp);
249 : :
250 [ + + ]: 396 : if (conn->mode != S2N_SERVER) {
251 : 216 : *expiration_timestamp = 0;
252 : 216 : return S2N_RESULT_OK;
253 : 216 : }
254 : :
255 : 180 : uint64_t expiration = now + (conn->server_keying_material_lifetime * (uint64_t) ONE_SEC_IN_NANOS);
256 : :
257 : 180 : struct s2n_psk *chosen_psk = conn->psk_params.chosen_psk;
258 [ + + ][ + - ]: 180 : if (chosen_psk && chosen_psk->type == S2N_PSK_TYPE_RESUMPTION) {
259 [ + - ]: 5 : expiration = S2N_MIN(chosen_psk->keying_material_expiration, expiration);
260 : 5 : }
261 : :
262 : 180 : *expiration_timestamp = expiration;
263 : 180 : return S2N_RESULT_OK;
264 : 396 : }
265 : :
266 : : /**
267 : : * The state serialized into a TLS1.3 session ticket / client session.
268 : : *
269 : : * All blobs are borrowed, not owned.
270 : : */
271 : : struct s2n_resumption_state_tls13 {
272 : : /* the serialization version of the resumption state */
273 : : uint8_t version;
274 : : /* s2n-internal protocol version representation. NOT the format used in TLS messages */
275 : : uint8_t condensed_protocol_version;
276 : : uint8_t cipher_id[S2N_TLS_CIPHER_SUITE_LEN];
277 : : uint64_t ticket_issue_unix_ns;
278 : : uint32_t ticket_age_add;
279 : : struct s2n_blob resumption_secret;
280 : : uint64_t keying_material_expiration;
281 : : uint32_t max_early_data_size;
282 : : struct s2n_blob app_protocol;
283 : : struct s2n_blob early_data_context;
284 : : };
285 : :
286 : : static S2N_RESULT s2n_resumption_state_tls13_init(struct s2n_resumption_state_tls13 *state, struct s2n_connection *conn)
287 : 396 : {
288 [ - + ][ # # ]: 396 : RESULT_ENSURE_REF(state);
289 [ # # ][ - + ]: 396 : RESULT_ENSURE_REF(conn);
290 [ - + ][ # # ]: 396 : RESULT_ENSURE_REF(conn->secure);
291 [ - + ][ # # ]: 396 : RESULT_ENSURE_REF(conn->secure->cipher_suite);
292 : :
293 : 396 : uint64_t current_time = 0;
294 [ - + ]: 396 : RESULT_GUARD(s2n_config_wall_clock(conn->config, ¤t_time));
295 : :
296 : 396 : struct s2n_ticket_fields *ticket_fields = &conn->tls13_ticket_fields;
297 : :
298 : 396 : state->version = S2N_SERIALIZED_FORMAT_TLS13_V2;
299 : 396 : state->condensed_protocol_version = conn->actual_protocol_version;
300 [ # # ][ - + ]: 396 : RESULT_CHECKED_MEMCPY(state->cipher_id, conn->secure->cipher_suite->iana_value, sizeof(state->cipher_id));
[ + - ]
301 : 396 : state->ticket_issue_unix_ns = current_time;
302 : 396 : state->ticket_age_add = ticket_fields->ticket_age_add;
303 : 396 : state->resumption_secret = ticket_fields->session_secret;
304 [ - + ]: 396 : RESULT_GUARD(s2n_tls13_keying_material_expiration(conn, current_time, &state->keying_material_expiration));
305 : :
306 : 396 : uint32_t server_max_early_data = 0;
307 [ - + ]: 396 : RESULT_GUARD(s2n_early_data_get_server_max_size(conn, &server_max_early_data));
308 : 396 : state->max_early_data_size = server_max_early_data;
309 [ + + ]: 396 : if (server_max_early_data > 0) {
310 [ - + ]: 81 : RESULT_GUARD_POSIX(s2n_blob_init(&state->app_protocol, (uint8_t *) conn->application_protocol,
311 : 81 : strlen(conn->application_protocol)));
312 : 81 : state->early_data_context = conn->server_early_data_context;
313 : 81 : }
314 : :
315 : 396 : return S2N_RESULT_OK;
316 : 396 : }
317 : :
318 : : static S2N_RESULT s2n_resumption_state_tls13_encode(struct s2n_resumption_state_tls13 *state, struct s2n_stuffer *to)
319 : 396 : {
320 [ - + ][ # # ]: 396 : RESULT_ENSURE_REF(state);
321 [ - + ][ # # ]: 396 : RESULT_ENSURE_REF(to);
322 : :
323 [ - + ]: 396 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(to, state->version));
324 [ - + ]: 396 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(to, state->condensed_protocol_version));
325 [ - + ]: 396 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(to, state->cipher_id, sizeof(state->cipher_id)));
326 [ - + ]: 396 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint64(to, state->ticket_issue_unix_ns));
327 [ - + ]: 396 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint32(to, state->ticket_age_add));
328 [ # # ][ - + ]: 396 : RESULT_ENSURE_INCLUSIVE_RANGE(1, state->resumption_secret.size, UINT8_MAX);
[ # # ][ - + ]
329 [ - + ]: 396 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(to, state->resumption_secret.size));
330 [ - + ]: 396 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(to, state->resumption_secret.data, state->resumption_secret.size));
331 : : /* Always serialized, regardless of mode: see s2n_tls13_keying_material_expiration. */
332 [ - + ]: 396 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint64(to, state->keying_material_expiration));
333 [ - + ]: 396 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint32(to, state->max_early_data_size));
334 [ + + ]: 396 : if (state->max_early_data_size > 0) {
335 [ - + ][ # # ]: 81 : RESULT_ENSURE_LTE(state->app_protocol.size, UINT8_MAX);
336 [ - + ]: 81 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(to, state->app_protocol.size));
337 [ - + ]: 81 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(to, state->app_protocol.data, state->app_protocol.size));
338 [ - + ]: 81 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(to, state->early_data_context.size));
339 [ - + ]: 81 : RESULT_GUARD_POSIX(s2n_stuffer_write(to, &state->early_data_context));
340 : 81 : }
341 : :
342 : 396 : return S2N_RESULT_OK;
343 : 396 : }
344 : :
345 : : static S2N_RESULT s2n_serialize_resumption_state(struct s2n_connection *conn, struct s2n_stuffer *out)
346 : 437 : {
347 [ + + ]: 437 : if (s2n_resume_protocol_version(conn) < S2N_TLS13) {
348 : 41 : struct s2n_resumption_state_tls12 state = { 0 };
349 [ - + ]: 41 : RESULT_GUARD(s2n_resumption_state_tls12_init(&state, conn));
350 [ - + ]: 41 : RESULT_GUARD(s2n_resumption_state_tls12_encode(&state, out));
351 : 396 : } else {
352 : 396 : struct s2n_resumption_state_tls13 state = { 0 };
353 [ - + ]: 396 : RESULT_GUARD(s2n_resumption_state_tls13_init(&state, conn));
354 [ - + ]: 396 : RESULT_GUARD(s2n_resumption_state_tls13_encode(&state, out));
355 : 396 : }
356 : 437 : return S2N_RESULT_OK;
357 : 437 : }
358 : :
359 : : static int s2n_client_serialize_resumption_state(struct s2n_connection *conn, struct s2n_stuffer *to)
360 : 227 : {
361 : : /* Serialize session ticket */
362 [ + + ][ + + ]: 227 : if (conn->config->use_tickets && conn->client_ticket.size > 0) {
363 [ - + ]: 222 : POSIX_GUARD(s2n_stuffer_write_uint8(to, S2N_STATE_WITH_SESSION_TICKET));
364 [ - + ]: 222 : POSIX_GUARD(s2n_stuffer_write_uint16(to, conn->client_ticket.size));
365 [ - + ]: 222 : POSIX_GUARD(s2n_stuffer_write(to, &conn->client_ticket));
366 : 222 : } else {
367 : : /* Serialize session id */
368 [ - + ][ # # ]: 5 : POSIX_ENSURE_LT(conn->actual_protocol_version, S2N_TLS13);
369 [ - + ]: 5 : POSIX_GUARD(s2n_stuffer_write_uint8(to, S2N_STATE_WITH_SESSION_ID));
370 [ - + ]: 5 : POSIX_GUARD(s2n_stuffer_write_uint8(to, conn->session_id_len));
371 [ - + ]: 5 : POSIX_GUARD(s2n_stuffer_write_bytes(to, conn->session_id, conn->session_id_len));
372 : 5 : }
373 : :
374 : : /* Serialize session state */
375 [ - + ]: 227 : POSIX_GUARD_RESULT(s2n_serialize_resumption_state(conn, to));
376 : :
377 : 227 : return 0;
378 : 227 : }
379 : :
380 : : /**
381 : : * - `state`: The previously-stored view of the client's state.
382 : : * - `opaque_ticket`: The contents of the NewSessionTicket sent by the server.
383 : : * This is an unknown format and should not be parsed. NULL in the case of session-id
384 : : * based resumption.
385 : : */
386 : : static S2N_RESULT s2n_resumption_state_tls12_apply_client(struct s2n_resumption_state_tls12 *state,
387 : : struct s2n_connection *conn, struct s2n_blob *opaque_ticket)
388 : 13 : {
389 [ - + ][ # # ]: 13 : RESULT_ENSURE_REF(conn);
390 [ - + ][ # # ]: 13 : RESULT_ENSURE_REF(state);
391 : :
392 : : /* Operate on a copy of the connection to avoid mutating the connection on
393 : : * failure. We have tests in s2n_resume_test.c that prove this level of copy
394 : : * is sufficient.
395 : : */
396 : 13 : struct s2n_crypto_parameters *secure = conn->secure;
397 [ - + ][ # # ]: 13 : RESULT_ENSURE_REF(secure);
398 : 13 : struct s2n_connection temp_conn = *conn;
399 : 13 : struct s2n_crypto_parameters temp_secure = *secure;
400 : 13 : temp_conn.secure = &temp_secure;
401 : :
402 : 13 : temp_conn.resume_protocol_version = state->condensed_protocol_version;
403 [ - + ]: 13 : RESULT_GUARD_POSIX(s2n_set_cipher_as_client(&temp_conn, state->cipher_id));
404 : :
405 [ - + ][ # # ]: 13 : RESULT_CHECKED_MEMCPY(temp_conn.secrets.version.tls12.master_secret, state->master_secret,
[ + - ]
406 : 13 : sizeof(state->master_secret));
407 : :
408 : 13 : temp_conn.ems_negotiated = state->ems_negotiated;
409 : :
410 : 13 : DEFER_CLEANUP(struct s2n_blob client_ticket = { 0 }, s2n_free);
411 [ + + ]: 13 : if (opaque_ticket) {
412 [ - + ]: 8 : RESULT_GUARD_POSIX(s2n_dup(opaque_ticket, &client_ticket));
413 : 8 : }
414 : :
415 : : /* Finally, actually update the connection */
416 [ - + ]: 13 : RESULT_GUARD_POSIX(s2n_free(&conn->client_ticket));
417 : 13 : *secure = temp_secure;
418 : 13 : *conn = temp_conn;
419 : 13 : conn->secure = secure;
420 : 13 : conn->client_ticket = client_ticket;
421 : 13 : ZERO_TO_DISABLE_DEFER_CLEANUP(client_ticket);
422 : :
423 : 13 : return S2N_RESULT_OK;
424 : 13 : }
425 : :
426 : : /* `s2n_validate_ticket_age` is a best effort check that the session ticket is
427 : : * less than one week old.
428 : : *
429 : : * Clock skew between hosts or the possibility of a clock jump prevent this from
430 : : * being a precise check.
431 : : */
432 : : static S2N_RESULT s2n_validate_ticket_age(uint64_t current_time, uint64_t ticket_issue_time)
433 : 18 : {
434 : : /* If the `ticket_issue_time` is in the future, then we are observing clock skew.
435 : : * We shouldn't fully reject the ticket, but we assert that the clock skew is
436 : : * less than some MAX_ALLOWED_CLOCK_SKEW_SEC
437 : : */
438 [ - + ]: 18 : if (current_time < ticket_issue_time) {
439 : 0 : uint64_t clock_skew_in_nanos = ticket_issue_time - current_time;
440 : 0 : uint64_t clock_skew_in_seconds = clock_skew_in_nanos / ONE_SEC_IN_NANOS;
441 [ # # ][ # # ]: 0 : RESULT_ENSURE(clock_skew_in_seconds <= MAX_ALLOWED_CLOCK_SKEW_SEC, S2N_ERR_INVALID_SESSION_TICKET);
442 : 18 : } else {
443 : 18 : uint64_t ticket_age_in_nanos = current_time - ticket_issue_time;
444 : 18 : uint64_t ticket_age_in_sec = ticket_age_in_nanos / ONE_SEC_IN_NANOS;
445 [ # # ][ - + ]: 18 : RESULT_ENSURE(ticket_age_in_sec <= ONE_WEEK_IN_SEC, S2N_ERR_INVALID_SESSION_TICKET);
446 : 18 : }
447 : 18 : return S2N_RESULT_OK;
448 : 18 : }
449 : :
450 : : /* Parses a TLS1.3 resumption state off the wire into `state`.
451 : : *
452 : : * Pure parsing: no validation against the connection and no mutation. Shared by
453 : : * client and server. Variable-length fields alias into `from` (see the struct
454 : : * doc comment). The format identifier is consumed here, mirroring encode.
455 : : */
456 : : static S2N_RESULT s2n_resumption_state_tls13_decode(struct s2n_resumption_state_tls13 *state, struct s2n_stuffer *from)
457 : 18 : {
458 [ # # ][ - + ]: 18 : RESULT_ENSURE_REF(state);
459 [ - + ][ # # ]: 18 : RESULT_ENSURE_REF(from);
460 : :
461 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(from, &state->version));
462 [ - + ][ # # ]: 18 : RESULT_ENSURE(state->version == S2N_SERIALIZED_FORMAT_TLS13_V2, S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
463 : :
464 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(from, &state->condensed_protocol_version));
465 [ # # ][ - + ]: 18 : RESULT_ENSURE_GTE(state->condensed_protocol_version, S2N_TLS13);
466 : :
467 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(from, state->cipher_id, sizeof(state->cipher_id)));
468 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint64(from, &state->ticket_issue_unix_ns));
469 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint32(from, &state->ticket_age_add));
470 : :
471 : 18 : uint8_t secret_len = 0;
472 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(from, &secret_len));
473 [ - + ][ # # ]: 18 : RESULT_ENSURE_LTE(secret_len, S2N_TLS_SECRET_LEN);
474 : 18 : uint8_t *secret_data = s2n_stuffer_raw_read(from, secret_len);
475 [ - + ][ # # ]: 18 : RESULT_ENSURE_REF(secret_data);
476 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_blob_init(&state->resumption_secret, secret_data, secret_len));
477 : :
478 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint64(from, &state->keying_material_expiration));
479 : :
480 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint32(from, &state->max_early_data_size));
481 [ - + ]: 18 : if (state->max_early_data_size > 0) {
482 : 0 : uint8_t app_proto_size = 0;
483 [ # # ]: 0 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(from, &app_proto_size));
484 : 0 : uint8_t *app_proto_data = s2n_stuffer_raw_read(from, app_proto_size);
485 [ # # ][ # # ]: 0 : RESULT_ENSURE_REF(app_proto_data);
486 [ # # ]: 0 : RESULT_GUARD_POSIX(s2n_blob_init(&state->app_protocol, app_proto_data, app_proto_size));
487 : :
488 : 0 : uint16_t early_data_context_size = 0;
489 [ # # ]: 0 : RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(from, &early_data_context_size));
490 : 0 : uint8_t *early_data_context_data = s2n_stuffer_raw_read(from, early_data_context_size);
491 [ # # ][ # # ]: 0 : RESULT_ENSURE_REF(early_data_context_data);
492 [ # # ]: 0 : RESULT_GUARD_POSIX(s2n_blob_init(&state->early_data_context, early_data_context_data, early_data_context_size));
493 : 0 : }
494 : :
495 : 18 : return S2N_RESULT_OK;
496 : 18 : }
497 : :
498 : : /* Validates a decoded TLS1.3 resumption state and applies it to the connection
499 : : * as a resumption PSK.
500 : : *
501 : : * Called by the server when it attempts to use a client-supplied PSK in the ClientHello.
502 : : *
503 : : * Called by the client as part of connection setup, e.g. `s2n_connection_set_session`.
504 : : */
505 : : static S2N_RESULT s2n_resumption_state_tls13_apply(struct s2n_resumption_state_tls13 *state,
506 : : struct s2n_connection *conn, struct s2n_blob *psk_identity)
507 : 18 : {
508 [ # # ][ - + ]: 18 : RESULT_ENSURE_REF(state);
509 [ - + ][ # # ]: 18 : RESULT_ENSURE_REF(conn);
510 [ - + ][ # # ]: 18 : RESULT_ENSURE_REF(psk_identity);
511 : :
512 : : /* Clients don't know which protocol version will be negotiated at this stage */
513 [ + + ]: 18 : if (conn->mode == S2N_SERVER) {
514 [ - + ][ # # ]: 11 : RESULT_ENSURE(state->condensed_protocol_version == conn->actual_protocol_version, S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
515 : 11 : }
516 : :
517 : 18 : struct s2n_cipher_suite *cipher_suite = NULL;
518 [ - + ]: 18 : RESULT_GUARD(s2n_cipher_suite_from_iana(state->cipher_id, sizeof(state->cipher_id), &cipher_suite));
519 [ - + ][ # # ]: 18 : RESULT_ENSURE_REF(cipher_suite);
520 : :
521 : : /**
522 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-4.6.1
523 : : *# Clients MUST NOT cache
524 : : *# tickets for longer than 7 days, regardless of the ticket_lifetime,
525 : : *# and MAY delete tickets earlier based on local policy.
526 : : */
527 : 18 : uint64_t current_time = 0;
528 [ - + ]: 18 : RESULT_GUARD(s2n_config_wall_clock(conn->config, ¤t_time));
529 [ - + ]: 18 : RESULT_GUARD(s2n_validate_ticket_age(current_time, state->ticket_issue_unix_ns));
530 : :
531 : 18 : DEFER_CLEANUP(struct s2n_psk psk = { 0 }, s2n_psk_wipe);
532 [ - + ]: 18 : RESULT_GUARD(s2n_psk_init(&psk, S2N_PSK_TYPE_RESUMPTION));
533 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_psk_set_identity(&psk, psk_identity->data, psk_identity->size));
534 : 18 : psk.hmac_alg = cipher_suite->prf_alg;
535 : 18 : psk.ticket_issue_time = state->ticket_issue_unix_ns;
536 : 18 : psk.ticket_age_add = state->ticket_age_add;
537 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_psk_set_secret(&psk, state->resumption_secret.data, state->resumption_secret.size));
538 : :
539 : 18 : psk.keying_material_expiration = state->keying_material_expiration;
540 [ + + ]: 18 : if (conn->mode == S2N_SERVER) {
541 [ - + ][ # # ]: 11 : RESULT_ENSURE(psk.keying_material_expiration > current_time, S2N_ERR_KEYING_MATERIAL_EXPIRED);
542 : 11 : }
543 : :
544 [ - + ]: 18 : if (state->max_early_data_size > 0) {
545 [ # # ]: 0 : RESULT_GUARD_POSIX(s2n_psk_configure_early_data(&psk, state->max_early_data_size,
546 : 0 : state->cipher_id[0], state->cipher_id[1]));
547 [ # # ]: 0 : RESULT_GUARD_POSIX(s2n_psk_set_application_protocol(&psk, state->app_protocol.data, state->app_protocol.size));
548 [ # # ]: 0 : RESULT_GUARD_POSIX(s2n_psk_set_early_data_context(&psk, state->early_data_context.data,
549 : 0 : state->early_data_context.size));
550 : 0 : }
551 : :
552 : : /* Make sure that this connection is configured for resumption PSKs, not external PSKs */
553 [ - + ]: 18 : RESULT_GUARD(s2n_connection_set_psk_type(conn, S2N_PSK_TYPE_RESUMPTION));
554 : : /* Remove all previously-set PSKs. To keep the session ticket API behavior consistent
555 : : * across protocol versions, we currently only support setting a single resumption PSK. */
556 [ - + ]: 18 : RESULT_GUARD(s2n_psk_parameters_wipe(&conn->psk_params));
557 [ - + ]: 18 : RESULT_GUARD_POSIX(s2n_connection_append_psk(conn, &psk));
558 : :
559 : 18 : return S2N_RESULT_OK;
560 : 18 : }
561 : :
562 : : S2N_RESULT s2n_deserialize_resumption_state(struct s2n_connection *conn,
563 : : struct s2n_blob *ticket, struct s2n_stuffer *from)
564 : 329 : {
565 [ # # ][ - + ]: 329 : RESULT_ENSURE_REF(conn);
566 [ - + ][ # # ]: 329 : RESULT_ENSURE_REF(from);
567 : :
568 : : /* Peek the serialized format identifier to select a decoder */
569 : 329 : uint8_t format = 0;
570 [ - + ]: 329 : RESULT_GUARD_POSIX(s2n_stuffer_peek_char(from, (char *) &format));
571 : :
572 [ + + ]: 329 : if (format == S2N_SERIALIZED_FORMAT_TLS12_V3) {
573 : 82 : struct s2n_resumption_state_tls12 state = { 0 };
574 [ + + ]: 82 : RESULT_GUARD(s2n_resumption_state_tls12_decode(&state, from));
575 : :
576 [ + + ]: 81 : if (conn->mode == S2N_SERVER) {
577 [ + + ]: 37 : RESULT_GUARD(s2n_resumption_state_tls12_apply_server(&state, conn));
578 : 44 : } else {
579 [ + + ]: 44 : RESULT_GUARD(s2n_resumption_state_tls12_apply_client(&state, conn, ticket));
580 : 44 : }
581 [ + + ]: 247 : } else if (format == S2N_SERIALIZED_FORMAT_TLS13_V2) {
582 : 245 : struct s2n_resumption_state_tls13 state = { 0 };
583 [ + + ]: 245 : RESULT_GUARD(s2n_resumption_state_tls13_decode(&state, from));
584 [ + + ]: 244 : RESULT_GUARD(s2n_resumption_state_tls13_apply(&state, conn, ticket));
585 : 244 : } else {
586 [ + - ]: 2 : RESULT_BAIL(S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
587 : 2 : }
588 : 316 : conn->set_session = true;
589 : 316 : return S2N_RESULT_OK;
590 : 329 : }
591 : :
592 : : static int s2n_client_deserialize_with_session_id(struct s2n_connection *conn, struct s2n_stuffer *from)
593 : 6 : {
594 : 6 : uint8_t session_id_len = 0;
595 [ - + ]: 6 : POSIX_GUARD(s2n_stuffer_read_uint8(from, &session_id_len));
596 : :
597 [ + + ][ - + ]: 6 : if (session_id_len == 0 || session_id_len > S2N_TLS_SESSION_ID_MAX_LEN
598 [ - + ]: 6 : || session_id_len > s2n_stuffer_data_available(from)) {
599 [ + - ]: 1 : POSIX_BAIL(S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
600 : 1 : }
601 : :
602 : 5 : conn->session_id_len = session_id_len;
603 [ - + ]: 5 : POSIX_GUARD(s2n_stuffer_read_bytes(from, conn->session_id, session_id_len));
604 : :
605 [ - + ]: 5 : POSIX_GUARD_RESULT(s2n_deserialize_resumption_state(conn, NULL, from));
606 : :
607 : 5 : return 0;
608 : 5 : }
609 : :
610 : : static int s2n_client_deserialize_with_session_ticket(struct s2n_connection *conn, struct s2n_stuffer *from)
611 : 14 : {
612 : 14 : uint16_t session_ticket_len = 0;
613 [ - + ]: 14 : POSIX_GUARD(s2n_stuffer_read_uint16(from, &session_ticket_len));
614 : :
615 [ - + ][ - + ]: 14 : if (session_ticket_len == 0 || session_ticket_len > s2n_stuffer_data_available(from)) {
616 [ # # ]: 0 : POSIX_BAIL(S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
617 : 0 : }
618 : :
619 : 14 : struct s2n_blob session_ticket = { 0 };
620 : 14 : uint8_t *session_ticket_bytes = s2n_stuffer_raw_read(from, session_ticket_len);
621 [ - + ][ # # ]: 14 : POSIX_ENSURE_REF(session_ticket_bytes);
622 [ - + ]: 14 : POSIX_GUARD(s2n_blob_init(&session_ticket, session_ticket_bytes, session_ticket_len));
623 : :
624 [ - + ]: 14 : POSIX_GUARD_RESULT(s2n_deserialize_resumption_state(conn, &session_ticket, from));
625 : 14 : return 0;
626 : 14 : }
627 : :
628 : : static int s2n_client_deserialize_resumption_state(struct s2n_connection *conn, struct s2n_stuffer *from)
629 : 21 : {
630 : 21 : uint8_t format = 0;
631 [ - + ]: 21 : POSIX_GUARD(s2n_stuffer_read_uint8(from, &format));
632 : :
633 : 21 : switch (format) {
634 [ + + ]: 6 : case S2N_STATE_WITH_SESSION_ID:
635 [ + + ]: 6 : POSIX_GUARD(s2n_client_deserialize_with_session_id(conn, from));
636 : 5 : break;
637 [ + + ]: 14 : case S2N_STATE_WITH_SESSION_TICKET:
638 [ - + ]: 14 : POSIX_GUARD(s2n_client_deserialize_with_session_ticket(conn, from));
639 : 14 : break;
640 [ + + ]: 14 : default:
641 [ + - ]: 1 : POSIX_BAIL(S2N_ERR_INVALID_SERIALIZED_SESSION_STATE);
642 : 21 : }
643 : :
644 : 19 : return 0;
645 : 21 : }
646 : :
647 : : int s2n_resume_from_cache(struct s2n_connection *conn)
648 : 16 : {
649 [ + + ][ + - ]: 16 : S2N_ERROR_IF(conn->session_id_len == 0, S2N_ERR_SESSION_ID_TOO_SHORT);
650 [ - + ][ # # ]: 13 : S2N_ERROR_IF(conn->session_id_len > S2N_TLS_SESSION_ID_MAX_LEN, S2N_ERR_SESSION_ID_TOO_LONG);
651 : :
652 : 13 : uint8_t data[S2N_TLS12_TICKET_SIZE_IN_BYTES] = { 0 };
653 : 13 : struct s2n_blob entry = { 0 };
654 [ - + ]: 13 : POSIX_GUARD(s2n_blob_init(&entry, data, S2N_TLS12_TICKET_SIZE_IN_BYTES));
655 : 13 : uint64_t size = entry.size;
656 : 13 : int result = conn->config->cache_retrieve(conn, conn->config->cache_retrieve_data, conn->session_id, conn->session_id_len, entry.data, &size);
657 [ + + ]: 13 : if (result == S2N_CALLBACK_BLOCKED) {
658 [ + - ]: 6 : POSIX_BAIL(S2N_ERR_ASYNC_BLOCKED);
659 : 6 : }
660 [ + + ][ + - ]: 7 : POSIX_ENSURE(result >= S2N_SUCCESS, S2N_ERR_CANCELLED);
661 : :
662 [ - + ][ # # ]: 6 : S2N_ERROR_IF(size != entry.size, S2N_ERR_SIZE_MISMATCH);
663 : :
664 : 6 : struct s2n_stuffer from = { 0 };
665 [ - + ]: 6 : POSIX_GUARD(s2n_stuffer_init(&from, &entry));
666 [ - + ]: 6 : POSIX_GUARD(s2n_stuffer_write(&from, &entry));
667 [ + + ]: 6 : POSIX_GUARD_RESULT(s2n_resume_decrypt_session(conn, &from));
668 : :
669 : 3 : return 0;
670 : 6 : }
671 : :
672 : : S2N_RESULT s2n_store_to_cache(struct s2n_connection *conn)
673 : 7 : {
674 : 7 : uint8_t data[S2N_TLS12_TICKET_SIZE_IN_BYTES] = { 0 };
675 : 7 : struct s2n_blob entry = { 0 };
676 [ - + ]: 7 : RESULT_GUARD_POSIX(s2n_blob_init(&entry, data, S2N_TLS12_TICKET_SIZE_IN_BYTES));
677 : 7 : struct s2n_stuffer to = { 0 };
678 : :
679 : : /* session_id_len should always be >0 since either the Client provided a SessionId or the Server generated a new
680 : : * one for the Client */
681 [ # # ][ - + ]: 7 : RESULT_ENSURE(conn->session_id_len > 0, S2N_ERR_SESSION_ID_TOO_SHORT);
682 [ # # ][ - + ]: 7 : RESULT_ENSURE(conn->session_id_len <= S2N_TLS_SESSION_ID_MAX_LEN, S2N_ERR_SESSION_ID_TOO_LONG);
683 : :
684 [ - + ]: 7 : RESULT_GUARD_POSIX(s2n_stuffer_init(&to, &entry));
685 : :
686 : : /* The lookup returns a pointer into the shared, unsynchronized config->ticket_keys array.
687 : : * A concurrent call to s2n_config_add_ticket_crypto_key (key rotation) can reallocate,
688 : : * shift, or overwrite that array, invalidating the pointer. Copy the key to the stack
689 : : * immediately so later operations never dereference memory another thread might change. */
690 : 7 : struct s2n_ticket_key *key_ptr = s2n_get_ticket_encrypt_decrypt_key(conn->config);
691 [ + - ][ + + ]: 7 : RESULT_ENSURE(key_ptr != NULL, S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY);
692 : 5 : struct s2n_ticket_key key = *key_ptr;
693 [ - + ]: 5 : RESULT_GUARD(s2n_resume_encrypt_session_ticket(conn, &key, &to));
694 : :
695 : : /* Store to the cache */
696 : 5 : conn->config->cache_store(conn, conn->config->cache_store_data, S2N_TLS_SESSION_CACHE_TTL, conn->session_id, conn->session_id_len, entry.data, entry.size);
697 : :
698 : 5 : return S2N_RESULT_OK;
699 : 5 : }
700 : :
701 : : int s2n_connection_set_session(struct s2n_connection *conn, const uint8_t *session, size_t length)
702 : 159 : {
703 [ - + ][ # # ]: 159 : POSIX_ENSURE_REF(conn);
704 [ - + ][ # # ]: 159 : POSIX_ENSURE_REF(session);
705 : :
706 : 159 : DEFER_CLEANUP(struct s2n_blob session_data = { 0 }, s2n_free);
707 : : /* size_t is 64-bit integer on 64-bit system, while s2n_alloc's length parameter is a 32-bit integer */
708 [ + - ][ + + ]: 159 : POSIX_ENSURE(length <= UINT32_MAX, S2N_ERR_INVALID_ARGUMENT);
709 [ - + ]: 158 : POSIX_GUARD(s2n_alloc(&session_data, length));
710 [ # # ][ - + ]: 158 : POSIX_CHECKED_MEMCPY(session_data.data, session, length);
[ + - ]
711 : :
712 : 158 : struct s2n_stuffer from = { 0 };
713 [ - + ]: 158 : POSIX_GUARD(s2n_stuffer_init(&from, &session_data));
714 [ - + ]: 158 : POSIX_GUARD(s2n_stuffer_write(&from, &session_data));
715 [ + + ]: 158 : POSIX_GUARD(s2n_client_deserialize_resumption_state(conn, &from));
716 : 153 : return 0;
717 : 158 : }
718 : :
719 : : int s2n_connection_get_session(struct s2n_connection *conn, uint8_t *session, size_t max_length)
720 : 481 : {
721 [ # # ][ - + ]: 481 : POSIX_ENSURE_REF(conn);
722 [ - + ][ # # ]: 481 : POSIX_ENSURE_REF(session);
723 : :
724 : 481 : const int len = s2n_connection_get_session_length(conn);
725 [ - + ]: 481 : POSIX_GUARD(len);
726 : :
727 [ + + ]: 481 : if (len == 0) {
728 : 2 : return 0;
729 : 2 : }
730 : :
731 [ + + ][ + - ]: 479 : POSIX_ENSURE((size_t) len <= max_length, S2N_ERR_SERIALIZED_SESSION_STATE_TOO_LONG);
732 : :
733 : 478 : struct s2n_blob serialized_data = { 0 };
734 [ - + ]: 478 : POSIX_GUARD(s2n_blob_init(&serialized_data, session, len));
735 [ - + ]: 478 : POSIX_GUARD(s2n_blob_zero(&serialized_data));
736 : :
737 : 478 : struct s2n_stuffer to = { 0 };
738 [ - + ]: 478 : POSIX_GUARD(s2n_stuffer_init(&to, &serialized_data));
739 [ - + ]: 478 : POSIX_GUARD(s2n_client_serialize_resumption_state(conn, &to));
740 : :
741 : 478 : return len;
742 : 478 : }
743 : :
744 : : int s2n_connection_get_session_ticket_lifetime_hint(struct s2n_connection *conn)
745 : 19 : {
746 [ - + ][ # # ]: 19 : POSIX_ENSURE_REF(conn);
747 [ + - ][ + + ]: 19 : S2N_ERROR_IF(!(conn->config->use_tickets && conn->client_ticket.size > 0), S2N_ERR_SESSION_TICKET_NOT_SUPPORTED);
[ + - ]
748 : :
749 : : /* Session resumption using session ticket */
750 : 18 : return conn->ticket_lifetime_hint;
751 : 19 : }
752 : :
753 : : S2N_RESULT s2n_connection_get_session_state_size(struct s2n_connection *conn, size_t *state_size)
754 : 1252 : {
755 [ + - ][ + + ]: 1252 : RESULT_ENSURE_REF(conn);
756 [ - + ][ # # ]: 1251 : RESULT_ENSURE_REF(conn->secure);
757 [ + + ][ + - ]: 1251 : RESULT_ENSURE_REF(state_size);
758 : :
759 [ + + ]: 1250 : if (s2n_resume_protocol_version(conn) < S2N_TLS13) {
760 : 64 : *state_size = S2N_TLS12_STATE_SIZE_IN_BYTES;
761 : 64 : return S2N_RESULT_OK;
762 : 64 : }
763 : :
764 : 1186 : *state_size = S2N_TLS13_FIXED_STATE_SIZE;
765 : :
766 : 1186 : uint8_t secret_size = 0;
767 [ # # ][ - + ]: 1186 : RESULT_ENSURE_REF(conn->secure->cipher_suite);
768 [ - + ]: 1186 : RESULT_GUARD_POSIX(s2n_hmac_digest_size(conn->secure->cipher_suite->prf_alg, &secret_size));
769 : 1186 : *state_size += secret_size;
770 : :
771 : 1186 : uint32_t server_max_early_data = 0;
772 [ - + ]: 1186 : RESULT_GUARD(s2n_early_data_get_server_max_size(conn, &server_max_early_data));
773 [ + + ]: 1186 : if (server_max_early_data > 0) {
774 : 386 : *state_size += S2N_TLS13_FIXED_EARLY_DATA_STATE_SIZE
775 : 386 : + strlen(conn->application_protocol)
776 : 386 : + conn->server_early_data_context.size;
777 : 386 : }
778 : :
779 : 1186 : return S2N_RESULT_OK;
780 : 1186 : }
781 : :
782 : : static S2N_RESULT s2n_connection_get_session_length_impl(struct s2n_connection *conn, size_t *length)
783 : 455 : {
784 [ - + ][ # # ]: 455 : RESULT_ENSURE_REF(conn);
785 [ - + ][ # # ]: 455 : RESULT_ENSURE_REF(conn->config);
786 [ # # ][ - + ]: 455 : RESULT_ENSURE_REF(length);
787 : 455 : *length = 0;
788 : :
789 [ + + ][ + + ]: 455 : if (conn->config->use_tickets && conn->client_ticket.size > 0) {
790 : 441 : size_t session_state_size = 0;
791 [ - + ]: 441 : RESULT_GUARD(s2n_connection_get_session_state_size(conn, &session_state_size));
792 : 441 : *length = S2N_STATE_FORMAT_LEN + S2N_SESSION_TICKET_SIZE_LEN + conn->client_ticket.size + session_state_size;
793 [ + + ][ + + ]: 441 : } else if (conn->session_id_len > 0 && conn->actual_protocol_version < S2N_TLS13) {
794 : 10 : *length = S2N_STATE_FORMAT_LEN + sizeof(conn->session_id_len) + conn->session_id_len + S2N_TLS12_STATE_SIZE_IN_BYTES;
795 : 10 : }
796 : 455 : return S2N_RESULT_OK;
797 : 455 : }
798 : :
799 : : int s2n_connection_get_session_length(struct s2n_connection *conn)
800 : 970 : {
801 : 970 : size_t length = 0;
802 [ + + ]: 970 : if (s2n_result_is_ok(s2n_connection_get_session_length_impl(conn, &length))) {
803 : 969 : return length;
804 : 969 : }
805 : 1 : return 0;
806 : 970 : }
807 : :
808 : : int s2n_connection_is_session_resumed(struct s2n_connection *conn)
809 : 3746 : {
810 [ + + ][ + + ]: 3746 : return conn && IS_RESUMPTION_HANDSHAKE(conn)
[ + - ]
811 [ + + ][ + + ]: 3746 : && (conn->actual_protocol_version < S2N_TLS13 || conn->psk_params.type == S2N_PSK_TYPE_RESUMPTION);
812 : 3746 : }
813 : :
814 : : int s2n_connection_is_ocsp_stapled(struct s2n_connection *conn)
815 : 18 : {
816 [ # # ][ - + ]: 18 : POSIX_ENSURE_REF(conn);
817 : :
818 [ + + ]: 18 : if (conn->actual_protocol_version >= S2N_TLS13) {
819 [ + + ][ + - ]: 10 : return (s2n_server_can_send_ocsp(conn) || s2n_server_sent_ocsp(conn));
[ + - ][ + + ]
[ + + ][ + + ]
820 : 10 : } else {
821 : 8 : return IS_OCSP_STAPLED(conn);
822 : 8 : }
823 : 18 : }
824 : :
825 : : S2N_RESULT s2n_config_is_encrypt_key_available(struct s2n_config *config)
826 : 28 : {
827 [ - + ][ # # ]: 28 : RESULT_ENSURE_REF(config);
828 : :
829 : 28 : uint64_t now = 0;
830 : 28 : struct s2n_ticket_key *ticket_key = NULL;
831 [ - + ]: 28 : RESULT_GUARD(s2n_config_wall_clock(config, &now));
832 [ # # ][ - + ]: 28 : RESULT_ENSURE_REF(config->ticket_keys);
833 : :
834 : 28 : uint32_t ticket_keys_len = 0;
835 [ - + ]: 28 : RESULT_GUARD(s2n_array_num_elements(config->ticket_keys, &ticket_keys_len));
836 : :
837 [ + + ]: 29 : for (uint32_t i = ticket_keys_len; i > 0; i--) {
838 : 23 : uint32_t idx = i - 1;
839 [ - + ]: 23 : RESULT_GUARD(s2n_array_get(config->ticket_keys, idx, (void **) &ticket_key));
840 : 23 : uint64_t key_intro_time = ticket_key->intro_timestamp;
841 : :
842 [ + + ]: 23 : if (key_intro_time <= now
843 [ + - ]: 23 : && now < key_intro_time + config->encrypt_decrypt_key_lifetime_in_nanos) {
844 : 22 : return S2N_RESULT_OK;
845 : 22 : }
846 : 23 : }
847 : :
848 [ + - ]: 6 : RESULT_BAIL(S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY);
849 : 6 : }
850 : :
851 : : /* This function is used in s2n_get_ticket_encrypt_decrypt_key to compute the weight
852 : : * of the keys and to choose a single key from all of the encrypt-decrypt keys.
853 : : * Higher the weight of the key, higher the probability of being picked.
854 : : */
855 : : int s2n_compute_weight_of_encrypt_decrypt_keys(struct s2n_config *config,
856 : : uint8_t *encrypt_decrypt_keys_index,
857 : : uint8_t num_encrypt_decrypt_keys,
858 : : uint64_t now)
859 : 3 : {
860 : 3 : double total_weight = 0;
861 : 3 : struct s2n_ticket_key_weight ticket_keys_weight[S2N_MAX_TICKET_KEYS] = { 0 };
862 : 3 : struct s2n_ticket_key *ticket_key = NULL;
863 : :
864 : : /* Compute weight of encrypt-decrypt keys */
865 [ + + ]: 11 : for (int i = 0; i < num_encrypt_decrypt_keys; i++) {
866 [ - + ]: 8 : POSIX_GUARD_RESULT(s2n_array_get(config->ticket_keys, encrypt_decrypt_keys_index[i], (void **) &ticket_key));
867 : :
868 : 8 : uint64_t key_intro_time = ticket_key->intro_timestamp;
869 : 8 : uint64_t key_encryption_peak_time = key_intro_time + (config->encrypt_decrypt_key_lifetime_in_nanos / 2);
870 : :
871 : : /* The % of encryption using this key is linearly increasing */
872 [ + + ]: 8 : if (now < key_encryption_peak_time) {
873 : 4 : ticket_keys_weight[i].key_weight = now - key_intro_time;
874 : 4 : } else {
875 : : /* The % of encryption using this key is linearly decreasing */
876 : 4 : ticket_keys_weight[i].key_weight = (config->encrypt_decrypt_key_lifetime_in_nanos / 2) - (now - key_encryption_peak_time);
877 : 4 : }
878 : :
879 : 8 : ticket_keys_weight[i].key_index = encrypt_decrypt_keys_index[i];
880 : 8 : total_weight += ticket_keys_weight[i].key_weight;
881 : 8 : }
882 : :
883 : : /* Pick a random number in [0, 1). Using 53 bits (IEEE 754 double-precision floats). */
884 : 3 : uint64_t random_int = 0;
885 [ - + ]: 3 : POSIX_GUARD_RESULT(s2n_public_random(pow(2, 53), &random_int));
886 : 3 : double random = (double) random_int / (double) pow(2, 53);
887 : :
888 : : /* Compute cumulative weight of encrypt-decrypt keys */
889 [ + - ]: 7 : for (int i = 0; i < num_encrypt_decrypt_keys; i++) {
890 : 7 : ticket_keys_weight[i].key_weight = ticket_keys_weight[i].key_weight / total_weight;
891 : :
892 [ + + ]: 7 : if (i > 0) {
893 : 4 : ticket_keys_weight[i].key_weight += ticket_keys_weight[i - 1].key_weight;
894 : 4 : }
895 : :
896 [ + + ]: 7 : if (ticket_keys_weight[i].key_weight > random) {
897 : 3 : return ticket_keys_weight[i].key_index;
898 : 3 : }
899 : 7 : }
900 : :
901 [ # # ]: 0 : POSIX_BAIL(S2N_ERR_ENCRYPT_DECRYPT_KEY_SELECTION_FAILED);
902 : 0 : }
903 : :
904 : : /* This function is used in s2n_resume_encrypt_session_ticket in order for s2n to
905 : : * choose a key in encrypt-decrypt state from all of the keys added to config
906 : : */
907 : : struct s2n_ticket_key *s2n_get_ticket_encrypt_decrypt_key(struct s2n_config *config)
908 : 474 : {
909 : 474 : uint8_t num_encrypt_decrypt_keys = 0;
910 : 474 : uint8_t encrypt_decrypt_keys_index[S2N_MAX_TICKET_KEYS] = { 0 };
911 : 474 : struct s2n_ticket_key *ticket_key = NULL;
912 : :
913 : 474 : uint64_t now = 0;
914 [ - + ]: 474 : PTR_GUARD_RESULT(s2n_config_wall_clock(config, &now));
915 [ + + ][ + - ]: 474 : PTR_ENSURE_REF(config->ticket_keys);
916 : :
917 : 473 : uint32_t ticket_keys_len = 0;
918 [ - + ]: 473 : PTR_GUARD_RESULT(s2n_array_num_elements(config->ticket_keys, &ticket_keys_len));
919 : :
920 [ + + ]: 949 : for (uint32_t i = ticket_keys_len; i > 0; i--) {
921 : 476 : uint32_t idx = i - 1;
922 [ - + ]: 476 : PTR_GUARD_RESULT(s2n_array_get(config->ticket_keys, idx, (void **) &ticket_key));
923 : 476 : uint64_t key_intro_time = ticket_key->intro_timestamp;
924 : :
925 : : /* A key can be used at its intro time (<=) and it can be used up to (<)
926 : : * its expiration time.
927 : : */
928 [ + + ]: 476 : if (key_intro_time <= now
929 [ + + ]: 476 : && now < key_intro_time + config->encrypt_decrypt_key_lifetime_in_nanos) {
930 : 471 : encrypt_decrypt_keys_index[num_encrypt_decrypt_keys] = idx;
931 : 471 : num_encrypt_decrypt_keys++;
932 : 471 : }
933 : 476 : }
934 : :
935 [ + + ]: 473 : if (num_encrypt_decrypt_keys == 0) {
936 [ + - ]: 7 : PTR_BAIL(S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY);
937 : 7 : }
938 : :
939 [ + + ]: 466 : if (num_encrypt_decrypt_keys == 1) {
940 [ - + ]: 463 : PTR_GUARD_RESULT(s2n_array_get(config->ticket_keys, encrypt_decrypt_keys_index[0], (void **) &ticket_key));
941 : 463 : return ticket_key;
942 : 463 : }
943 : :
944 : 3 : int8_t idx = 0;
945 [ - + ]: 3 : PTR_GUARD_POSIX(idx = s2n_compute_weight_of_encrypt_decrypt_keys(config, encrypt_decrypt_keys_index, num_encrypt_decrypt_keys, now));
946 : :
947 [ - + ]: 3 : PTR_GUARD_RESULT(s2n_array_get(config->ticket_keys, idx, (void **) &ticket_key));
948 : 3 : return ticket_key;
949 : 3 : }
950 : :
951 : : /* This function is used in s2n_resume_decrypt_session in order for s2n to
952 : : * find the matching key that was used for encryption.
953 : : */
954 : : struct s2n_ticket_key *s2n_find_ticket_key(struct s2n_config *config, const uint8_t name[S2N_TICKET_KEY_NAME_LEN])
955 : 162 : {
956 : 162 : uint64_t now = 0;
957 : 162 : struct s2n_ticket_key *ticket_key = NULL;
958 [ - + ]: 162 : PTR_GUARD_RESULT(s2n_config_wall_clock(config, &now));
959 [ # # ][ - + ]: 162 : PTR_ENSURE_REF(config->ticket_keys);
960 : :
961 : 162 : uint32_t ticket_keys_len = 0;
962 [ - + ]: 162 : PTR_GUARD_RESULT(s2n_array_num_elements(config->ticket_keys, &ticket_keys_len));
963 : :
964 [ + + ]: 165 : for (uint32_t i = 0; i < ticket_keys_len; i++) {
965 [ - + ]: 158 : PTR_GUARD_RESULT(s2n_array_get(config->ticket_keys, i, (void **) &ticket_key));
966 : :
967 [ + + ]: 158 : if (s2n_constant_time_equals(ticket_key->key_name, name, S2N_TICKET_KEY_NAME_LEN)) {
968 : : /* Check to see if the key has expired */
969 [ - + ]: 155 : if (now >= ticket_key->intro_timestamp
970 : 155 : + config->encrypt_decrypt_key_lifetime_in_nanos
971 : 155 : + config->decrypt_key_lifetime_in_nanos) {
972 : 0 : return NULL;
973 : 0 : }
974 : :
975 : 155 : return ticket_key;
976 : 155 : }
977 : 158 : }
978 : :
979 : 7 : return NULL;
980 : 162 : }
981 : :
982 : : struct s2n_unique_ticket_key {
983 : : struct s2n_blob initial_key;
984 : : uint8_t info[S2N_AES256_KEY_LEN];
985 : : uint8_t output_key[S2N_AES256_KEY_LEN];
986 : : };
987 : :
988 : : /* Ensures that a session ticket encryption key is used only once per ticket.
989 : : *
990 : : * The AES-GCM encryption scheme breaks if the same nonce is used with the same key more than once.
991 : : * As the number of TLS connections increases per second, it becomes more probable that the same
992 : : * random nonce will be generated twice and used with the same ticket key.
993 : : * To avoid this we generate a unique session ticket encryption key for each ticket.
994 : : **/
995 : : static S2N_RESULT s2n_resume_generate_unique_ticket_key(struct s2n_unique_ticket_key *key)
996 : 235 : {
997 [ - + ][ # # ]: 235 : RESULT_ENSURE_REF(key);
998 : :
999 : 235 : struct s2n_blob out_key_blob = { 0 };
1000 [ - + ]: 235 : RESULT_GUARD_POSIX(s2n_blob_init(&out_key_blob, key->output_key, sizeof(key->output_key)));
1001 : 235 : struct s2n_blob info_blob = { 0 };
1002 [ - + ]: 235 : RESULT_GUARD_POSIX(s2n_blob_init(&info_blob, key->info, sizeof(key->info)));
1003 : 235 : struct s2n_blob salt = { 0 };
1004 [ - + ]: 235 : RESULT_GUARD_POSIX(s2n_blob_init(&salt, NULL, 0));
1005 : :
1006 : 235 : DEFER_CLEANUP(struct s2n_hmac_state hmac = { 0 }, s2n_hmac_free);
1007 : : /* TODO: There may be an optimization here to reuse existing hmac memory instead of
1008 : : * creating an entirely new hmac. See: https://github.com/aws/s2n-tls/issues/3206 */
1009 [ - + ]: 235 : RESULT_GUARD_POSIX(s2n_hmac_new(&hmac));
1010 [ - + ]: 235 : RESULT_GUARD_POSIX(s2n_hkdf(&hmac, S2N_HMAC_SHA256, &salt, &key->initial_key, &info_blob, &out_key_blob));
1011 : :
1012 : 235 : return S2N_RESULT_OK;
1013 : 235 : }
1014 : :
1015 : : S2N_RESULT s2n_resume_encrypt_session_ticket(struct s2n_connection *conn,
1016 : : struct s2n_ticket_key *key, struct s2n_stuffer *to)
1017 : 466 : {
1018 [ # # ][ - + ]: 466 : RESULT_ENSURE_REF(conn);
1019 [ - + ][ # # ]: 466 : RESULT_ENSURE_REF(to);
1020 : :
1021 [ + - ][ + + ]: 466 : RESULT_ENSURE(key != NULL, S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY);
1022 : :
1023 : : /* Build the plaintext header: version, key name, random nonce, random IV. */
1024 : 465 : struct s2n_ticket_header header = { 0 };
1025 [ - + ]: 465 : RESULT_GUARD(s2n_ticket_header_init(&header, key));
1026 : :
1027 : : /* Generate unique per-ticket encryption key from the header nonce */
1028 : 465 : struct s2n_unique_ticket_key ticket_key = { 0 };
1029 [ - + ]: 465 : RESULT_GUARD_POSIX(s2n_blob_init(&ticket_key.initial_key, key->aes_key, sizeof(key->aes_key)));
1030 [ - + ][ # # ]: 465 : RESULT_CHECKED_MEMCPY(ticket_key.info, header.nonce, sizeof(ticket_key.info));
[ + - ]
1031 [ - + ]: 465 : RESULT_GUARD(s2n_resume_generate_unique_ticket_key(&ticket_key));
1032 : :
1033 : : /* Initialize AES key */
1034 : 465 : struct s2n_blob aes_key_blob = { 0 };
1035 [ - + ]: 465 : RESULT_GUARD_POSIX(s2n_blob_init(&aes_key_blob, ticket_key.output_key, sizeof(ticket_key.output_key)));
1036 : 465 : DEFER_CLEANUP(struct s2n_session_key aes_ticket_key = { 0 }, s2n_session_key_free);
1037 [ - + ]: 465 : RESULT_GUARD_POSIX(s2n_session_key_alloc(&aes_ticket_key));
1038 [ - + ]: 465 : RESULT_GUARD(s2n_aes256_gcm.init(&aes_ticket_key));
1039 [ - + ]: 465 : RESULT_GUARD(s2n_aes256_gcm.set_encryption_key(&aes_ticket_key, &aes_key_blob));
1040 : :
1041 : : /* Ensure we never encrypt with a zero-filled key */
1042 : 465 : uint8_t zero_block[S2N_AES256_KEY_LEN] = { 0 };
1043 [ + + ][ + - ]: 465 : RESULT_ENSURE(!s2n_constant_time_equals(key->aes_key, zero_block, S2N_AES256_KEY_LEN),
1044 : 464 : S2N_ERR_KEY_CHECK);
1045 : :
1046 : : /* Initialize Additional Authenticated Data */
1047 : 464 : uint8_t aad_data[S2N_TICKET_AAD_LEN] = { 0 };
1048 : 464 : struct s2n_blob aad_blob = { 0 };
1049 [ - + ]: 464 : RESULT_GUARD_POSIX(s2n_blob_init(&aad_blob, aad_data, sizeof(aad_data)));
1050 : 464 : struct s2n_stuffer aad = { 0 };
1051 [ - + ]: 464 : RESULT_GUARD_POSIX(s2n_stuffer_init(&aad, &aad_blob));
1052 [ - + ]: 464 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&aad, key->implicit_aad, sizeof(key->implicit_aad)));
1053 [ - + ]: 464 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&aad, key->key_name, sizeof(key->key_name)));
1054 : :
1055 : : /* Write the plaintext header */
1056 [ + + ]: 464 : RESULT_GUARD(s2n_ticket_header_encode(&header, to));
1057 : :
1058 : 463 : struct s2n_blob iv = { 0 };
1059 [ - + ]: 463 : RESULT_GUARD_POSIX(s2n_blob_init(&iv, header.iv, sizeof(header.iv)));
1060 : :
1061 : : /* Write serialized session state */
1062 : 463 : uint32_t plaintext_state_size = s2n_stuffer_data_available(to);
1063 [ - + ]: 463 : RESULT_GUARD(s2n_serialize_resumption_state(conn, to));
1064 [ - + ]: 463 : RESULT_GUARD_POSIX(s2n_stuffer_skip_write(to, S2N_TLS_GCM_TAG_LEN));
1065 : :
1066 : : /* Initialize blob to be encrypted */
1067 : 463 : struct s2n_blob state_blob = { 0 };
1068 : 463 : struct s2n_stuffer copy_for_encryption = *to;
1069 [ - + ]: 463 : RESULT_GUARD_POSIX(s2n_stuffer_skip_read(©_for_encryption, plaintext_state_size));
1070 : 463 : uint32_t state_blob_size = s2n_stuffer_data_available(©_for_encryption);
1071 : 463 : uint8_t *state_blob_data = s2n_stuffer_raw_read(©_for_encryption, state_blob_size);
1072 [ # # ][ - + ]: 463 : RESULT_ENSURE_REF(state_blob_data);
1073 [ - + ]: 463 : RESULT_GUARD_POSIX(s2n_blob_init(&state_blob, state_blob_data, state_blob_size));
1074 : :
1075 [ - + ]: 463 : RESULT_GUARD_POSIX(s2n_aes256_gcm.io.aead.encrypt(&aes_ticket_key, &iv, &aad_blob, &state_blob, &state_blob));
1076 : :
1077 : 463 : return S2N_RESULT_OK;
1078 : 463 : }
1079 : :
1080 : : S2N_RESULT s2n_resume_decrypt_session(struct s2n_connection *conn, struct s2n_stuffer *from)
1081 : 171 : {
1082 [ # # ][ - + ]: 171 : RESULT_ENSURE_REF(conn);
1083 [ - + ][ # # ]: 171 : RESULT_ENSURE_REF(from);
1084 [ - + ][ # # ]: 171 : RESULT_ENSURE_REF(conn->config);
1085 : :
1086 : 171 : struct s2n_ticket_header header = { 0 };
1087 [ + + ]: 171 : RESULT_GUARD(s2n_ticket_header_decode(&header, from));
1088 : :
1089 : : /* The encrypted resumption state is the remainder of the ticket, following
1090 : : * the header. */
1091 : 162 : struct s2n_blob encrypted_state = { 0 };
1092 : 162 : uint32_t encrypted_size = s2n_stuffer_data_available(from);
1093 : 162 : uint8_t *encrypted_data = s2n_stuffer_raw_read(from, encrypted_size);
1094 [ - + ][ # # ]: 162 : RESULT_ENSURE_REF(encrypted_data);
1095 [ - + ]: 162 : RESULT_GUARD_POSIX(s2n_blob_init(&encrypted_state, encrypted_data, encrypted_size));
1096 : :
1097 : : /* The lookup returns a pointer into the shared, unsynchronized config->ticket_keys array.
1098 : : * A concurrent call to s2n_config_add_ticket_crypto_key (key rotation) can reallocate,
1099 : : * shift, or overwrite that array, invalidating the pointer. Copy the key to the stack
1100 : : * immediately so later operations never dereference memory another thread might change. */
1101 : 162 : struct s2n_ticket_key *key_ptr = s2n_find_ticket_key(conn->config, header.key_name);
1102 : : /* Key has expired; do full handshake */
1103 [ + + ][ + - ]: 162 : RESULT_ENSURE(key_ptr != NULL, S2N_ERR_KEY_USED_IN_SESSION_TICKET_NOT_FOUND);
1104 : 155 : struct s2n_ticket_key key = *key_ptr;
1105 : :
1106 : : /* A concurrent rotation can zero or overwrite the slot between the lookup and the copy.
1107 : : * Re-validate the copy so a ticket is never decrypted under a zeroed or different key. */
1108 [ # # ][ - + ]: 155 : RESULT_ENSURE(s2n_constant_time_equals(key.key_name, header.key_name, sizeof(header.key_name)),
1109 : 155 : S2N_ERR_KEY_USED_IN_SESSION_TICKET_NOT_FOUND);
1110 : 155 : uint8_t zero_block[S2N_AES256_KEY_LEN] = { 0 };
1111 [ - + ][ # # ]: 155 : RESULT_ENSURE(!s2n_constant_time_equals(key.aes_key, zero_block, sizeof(zero_block)),
1112 : 155 : S2N_ERR_KEY_CHECK);
1113 : :
1114 : 155 : struct s2n_unique_ticket_key ticket_key = { 0 };
1115 [ - + ]: 155 : RESULT_GUARD_POSIX(s2n_blob_init(&ticket_key.initial_key, key.aes_key, sizeof(key.aes_key)));
1116 [ - + ][ # # ]: 155 : RESULT_CHECKED_MEMCPY(ticket_key.info, header.nonce, sizeof(ticket_key.info));
[ + - ]
1117 [ - + ]: 155 : RESULT_GUARD(s2n_resume_generate_unique_ticket_key(&ticket_key));
1118 : :
1119 : : /* Initialize AES key */
1120 : 155 : struct s2n_blob aes_key_blob = { 0 };
1121 [ - + ]: 155 : RESULT_GUARD_POSIX(s2n_blob_init(&aes_key_blob, ticket_key.output_key, sizeof(ticket_key.output_key)));
1122 : 155 : DEFER_CLEANUP(struct s2n_session_key aes_ticket_key = { 0 }, s2n_session_key_free);
1123 [ - + ]: 155 : RESULT_GUARD_POSIX(s2n_session_key_alloc(&aes_ticket_key));
1124 [ - + ]: 155 : RESULT_GUARD(s2n_aes256_gcm.init(&aes_ticket_key));
1125 [ - + ]: 155 : RESULT_GUARD(s2n_aes256_gcm.set_decryption_key(&aes_ticket_key, &aes_key_blob));
1126 : :
1127 : : /* Initialize Additional Authenticated Data */
1128 : 155 : uint8_t aad_data[S2N_TICKET_AAD_LEN] = { 0 };
1129 : 155 : struct s2n_blob aad_blob = { 0 };
1130 [ - + ]: 155 : RESULT_GUARD_POSIX(s2n_blob_init(&aad_blob, aad_data, sizeof(aad_data)));
1131 : 155 : struct s2n_stuffer aad = { 0 };
1132 [ - + ]: 155 : RESULT_GUARD_POSIX(s2n_stuffer_init(&aad, &aad_blob));
1133 [ - + ]: 155 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&aad, key.implicit_aad, sizeof(key.implicit_aad)));
1134 [ - + ]: 155 : RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&aad, key.key_name, sizeof(key.key_name)));
1135 : :
1136 : 155 : struct s2n_blob iv = { 0 };
1137 [ - + ]: 155 : RESULT_GUARD_POSIX(s2n_blob_init(&iv, header.iv, sizeof(header.iv)));
1138 : :
1139 [ + + ]: 155 : RESULT_GUARD_POSIX(s2n_aes256_gcm.io.aead.decrypt(&aes_ticket_key, &iv, &aad_blob, &encrypted_state, &encrypted_state));
1140 : :
1141 : : /* Parse decrypted state */
1142 : 152 : struct s2n_blob state_blob = { 0 };
1143 : 152 : uint32_t state_blob_size = encrypted_state.size - S2N_TLS_GCM_TAG_LEN;
1144 [ - + ]: 152 : RESULT_GUARD_POSIX(s2n_blob_init(&state_blob, encrypted_state.data, state_blob_size));
1145 : 152 : struct s2n_stuffer state_stuffer = { 0 };
1146 [ - + ]: 152 : RESULT_GUARD_POSIX(s2n_stuffer_init(&state_stuffer, &state_blob));
1147 [ - + ]: 152 : RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&state_stuffer, state_blob_size));
1148 [ + + ]: 152 : RESULT_GUARD(s2n_deserialize_resumption_state(conn, &from->blob, &state_stuffer));
1149 : :
1150 : 148 : return S2N_RESULT_OK;
1151 : 152 : }
1152 : :
1153 : : /* This function is used to remove all or just one expired key from server config */
1154 : : int s2n_config_wipe_expired_ticket_crypto_keys(struct s2n_config *config, int8_t expired_key_index)
1155 : 145 : {
1156 : 145 : int num_of_expired_keys = 0;
1157 : 145 : int expired_keys_index[S2N_MAX_TICKET_KEYS] = { 0 };
1158 : 145 : struct s2n_ticket_key *ticket_key = NULL;
1159 : :
1160 [ - + ]: 145 : if (expired_key_index != -1) {
1161 : 0 : expired_keys_index[num_of_expired_keys] = expired_key_index;
1162 : 0 : num_of_expired_keys++;
1163 : :
1164 : 0 : goto end;
1165 : 0 : }
1166 : :
1167 : 145 : uint64_t now = 0;
1168 [ - + ]: 145 : POSIX_GUARD_RESULT(s2n_config_wall_clock(config, &now));
1169 [ - + ][ # # ]: 145 : POSIX_ENSURE_REF(config->ticket_keys);
1170 : :
1171 : 145 : uint32_t ticket_keys_len = 0;
1172 [ - + ]: 145 : POSIX_GUARD_RESULT(s2n_array_num_elements(config->ticket_keys, &ticket_keys_len));
1173 [ + + ]: 1347 : for (uint32_t i = 0; i < ticket_keys_len; i++) {
1174 [ - + ]: 1202 : POSIX_GUARD_RESULT(s2n_array_get(config->ticket_keys, i, (void **) &ticket_key));
1175 [ + + ]: 1202 : if (now >= ticket_key->intro_timestamp
1176 : 1202 : + config->encrypt_decrypt_key_lifetime_in_nanos
1177 : 1202 : + config->decrypt_key_lifetime_in_nanos) {
1178 : 3 : expired_keys_index[num_of_expired_keys] = i;
1179 : 3 : num_of_expired_keys++;
1180 : 3 : }
1181 : 1202 : }
1182 : :
1183 : 145 : end:
1184 [ + + ]: 148 : for (int j = 0; j < num_of_expired_keys; j++) {
1185 [ - + ]: 3 : POSIX_GUARD_RESULT(s2n_array_remove(config->ticket_keys, expired_keys_index[j] - j));
1186 : 3 : }
1187 : :
1188 : 145 : return 0;
1189 : 145 : }
1190 : :
1191 : : int s2n_config_store_ticket_key(struct s2n_config *config, struct s2n_ticket_key *key)
1192 : 141 : {
1193 : 141 : uint32_t ticket_keys_len = 0;
1194 [ - + ]: 141 : POSIX_GUARD_RESULT(s2n_array_num_elements(config->ticket_keys, &ticket_keys_len));
1195 : :
1196 : : /* The ticket key name and secret must both be unique. */
1197 [ + + ]: 1287 : for (uint32_t i = 0; i < ticket_keys_len; i++) {
1198 : 1150 : struct s2n_ticket_key *other_key = NULL;
1199 [ - + ]: 1150 : POSIX_GUARD_RESULT(s2n_array_get(config->ticket_keys, i, (void **) &other_key));
1200 [ + + ][ + - ]: 1150 : POSIX_ENSURE(!s2n_constant_time_equals(key->key_name, other_key->key_name, s2n_array_len(key->key_name)),
1201 : 1147 : S2N_ERR_INVALID_TICKET_KEY_NAME_OR_NAME_LENGTH);
1202 [ + + ][ + - ]: 1147 : POSIX_ENSURE(!s2n_constant_time_equals(key->aes_key, other_key->aes_key, s2n_array_len(key->aes_key)),
1203 : 1147 : S2N_ERR_TICKET_KEY_NOT_UNIQUE);
1204 : 1147 : }
1205 : :
1206 [ - + ]: 137 : POSIX_GUARD_RESULT(s2n_array_insert_and_copy(config->ticket_keys, ticket_keys_len, key));
1207 : 137 : return S2N_SUCCESS;
1208 : 137 : }
1209 : :
1210 : : int s2n_config_set_initial_ticket_count(struct s2n_config *config, uint8_t num)
1211 : 4 : {
1212 [ - + ][ # # ]: 4 : POSIX_ENSURE_REF(config);
1213 : :
1214 : 4 : config->initial_tickets_to_send = num;
1215 [ - + ]: 4 : POSIX_GUARD(s2n_config_set_session_tickets_onoff(config, true));
1216 : :
1217 : 4 : return S2N_SUCCESS;
1218 : 4 : }
1219 : :
1220 : : int s2n_connection_add_new_tickets_to_send(struct s2n_connection *conn, uint8_t num)
1221 : 123 : {
1222 [ - + ][ # # ]: 123 : POSIX_ENSURE_REF(conn);
1223 [ + + ]: 123 : POSIX_GUARD_RESULT(s2n_psk_validate_keying_material(conn));
1224 : :
1225 : 121 : uint32_t out = conn->tickets_to_send + num;
1226 [ + + ][ + - ]: 121 : POSIX_ENSURE(out <= UINT16_MAX, S2N_ERR_INTEGER_OVERFLOW);
1227 : 120 : conn->tickets_to_send = out;
1228 : :
1229 : 120 : return S2N_SUCCESS;
1230 : 121 : }
1231 : :
1232 : : int s2n_connection_get_tickets_sent(struct s2n_connection *conn, uint16_t *num)
1233 : 34 : {
1234 [ - + ][ # # ]: 34 : POSIX_ENSURE_REF(conn);
1235 [ # # ][ - + ]: 34 : POSIX_ENSURE_REF(num);
1236 [ + - ][ + + ]: 34 : POSIX_ENSURE(conn->mode == S2N_SERVER, S2N_ERR_CLIENT_MODE);
1237 : 33 : *num = conn->tickets_sent;
1238 : 33 : return S2N_SUCCESS;
1239 : 34 : }
1240 : :
1241 : : int s2n_connection_set_server_keying_material_lifetime(struct s2n_connection *conn, uint32_t lifetime_in_secs)
1242 : 9 : {
1243 [ + - ][ + + ]: 9 : POSIX_ENSURE_REF(conn);
1244 : 8 : conn->server_keying_material_lifetime = lifetime_in_secs;
1245 : 8 : return S2N_SUCCESS;
1246 : 9 : }
1247 : :
1248 : : int s2n_config_set_session_ticket_cb(struct s2n_config *config, s2n_session_ticket_fn callback, void *ctx)
1249 : 39 : {
1250 [ + + ][ + - ]: 39 : POSIX_ENSURE_MUT(config);
1251 : :
1252 : 38 : config->session_ticket_cb = callback;
1253 : 38 : config->session_ticket_ctx = ctx;
1254 : 38 : return S2N_SUCCESS;
1255 : 39 : }
1256 : :
1257 : : int s2n_session_ticket_get_data_len(struct s2n_session_ticket *ticket, size_t *data_len)
1258 : 259 : {
1259 [ + - ][ + + ]: 259 : POSIX_ENSURE_REF(ticket);
1260 [ + - ][ + + ]: 258 : POSIX_ENSURE_MUT(data_len);
1261 : :
1262 : 257 : *data_len = ticket->ticket_data.size;
1263 : 257 : return S2N_SUCCESS;
1264 : 258 : }
1265 : :
1266 : : int s2n_session_ticket_get_data(struct s2n_session_ticket *ticket, size_t max_data_len, uint8_t *data)
1267 : 259 : {
1268 [ + - ][ + + ]: 259 : POSIX_ENSURE_REF(ticket);
1269 [ + - ][ + + ]: 258 : POSIX_ENSURE_MUT(data);
1270 : :
1271 [ + + ][ + - ]: 257 : POSIX_ENSURE(ticket->ticket_data.size <= max_data_len, S2N_ERR_SERIALIZED_SESSION_STATE_TOO_LONG);
1272 [ # # ][ - + ]: 256 : POSIX_CHECKED_MEMCPY(data, ticket->ticket_data.data, ticket->ticket_data.size);
[ + - ]
1273 : :
1274 : 256 : return S2N_SUCCESS;
1275 : 256 : }
1276 : :
1277 : : int s2n_session_ticket_get_lifetime(struct s2n_session_ticket *ticket, uint32_t *session_lifetime)
1278 : 22 : {
1279 [ + + ][ + - ]: 22 : POSIX_ENSURE_REF(ticket);
1280 [ + - ][ + + ]: 21 : POSIX_ENSURE_REF(session_lifetime);
1281 : :
1282 : 20 : *session_lifetime = ticket->session_lifetime;
1283 : :
1284 : 20 : return S2N_SUCCESS;
1285 : 21 : }
|