Debian Codegraph @

crt/s2n/tls/s2n_connection_serialize.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "tls/s2n_connection_serialize.h"
17
18 #include "crypto/s2n_sequence.h"
19 #include "tls/s2n_connection.h"
20 #include "tls/s2n_tls.h"
21 #include "tls/s2n_tls13_key_schedule.h"
22
23 static bool s2n_libcrypto_supports_evp_aead_tls(void)
24 {
25 #ifdef S2N_LIBCRYPTO_SUPPORTS_EVP_AEAD_TLS
26 return true;
27 #else
28 return false;
29 #endif
30 }
31
32 /* True if a serialized blob carries the CBC implicit IV state. Only TLS1.0
33 * and SSLv3 chain the implicit IV across records (TLS1.1+ uses an explicit
34 * per-record IV), and only for CBC and composite (CBC + HMAC fused) record
35 * algorithms. */
36 static bool s2n_serialization_includes_implicit_iv(uint8_t protocol_version,
37 const struct s2n_cipher_suite *cipher_suite)
38 {
39 if (protocol_version >= S2N_TLS11) {
40 return false;
41 }
42 int type = cipher_suite->record_alg->cipher->type;
43 return type == S2N_CBC || type == S2N_COMPOSITE;
44 }
45
46 int s2n_connection_serialization_length(struct s2n_connection *conn, uint32_t *length)
47 {
48 POSIX_ENSURE_REF(conn);
49 POSIX_ENSURE_REF(conn->config);
50 POSIX_ENSURE_REF(conn->secure);
51 POSIX_ENSURE_REF(conn->secure->cipher_suite);
52 POSIX_ENSURE_REF(length);
53
54 POSIX_ENSURE(conn->config->serialized_connection_version != S2N_SERIALIZED_CONN_NONE,
55 S2N_ERR_INVALID_STATE);
56
57 if (conn->actual_protocol_version >= S2N_TLS13) {
58 uint8_t secret_size = 0;
59 POSIX_GUARD(s2n_hmac_digest_size(conn->secure->cipher_suite->prf_alg, &secret_size));
60 *length = S2N_SERIALIZED_CONN_FIXED_SIZE + (secret_size * 3);
61 } else if (s2n_serialization_includes_implicit_iv(conn->actual_protocol_version,
62 conn->secure->cipher_suite)) {
63 *length = S2N_SERIALIZED_CONN_TLS10_SIZE;
64 } else {
65 *length = S2N_SERIALIZED_CONN_TLS12_SIZE;
66 }
67
68 return S2N_SUCCESS;
69 }
70
71 static S2N_RESULT s2n_connection_serialize_tls13_secrets(struct s2n_connection *conn, struct s2n_stuffer *output)
72 {
73 RESULT_ENSURE_REF(conn);
74 RESULT_ENSURE_REF(conn->secure);
75 RESULT_ENSURE_REF(conn->secure->cipher_suite);
76
77 uint8_t secret_size = 0;
78 RESULT_GUARD_POSIX(s2n_hmac_digest_size(conn->secure->cipher_suite->prf_alg, &secret_size));
79
80 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, conn->secrets.version.tls13.client_app_secret,
81 secret_size));
82 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, conn->secrets.version.tls13.server_app_secret,
83 secret_size));
84 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, conn->secrets.version.tls13.resumption_master_secret,
85 secret_size));
86 return S2N_RESULT_OK;
87 }
88
89 static S2N_RESULT s2n_connection_serialize_secrets(struct s2n_connection *conn, struct s2n_stuffer *output)
90 {
91 RESULT_ENSURE_REF(conn);
92 RESULT_ENSURE_REF(conn->secure);
93
94 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, conn->secrets.version.tls12.master_secret,
95 S2N_TLS_SECRET_LEN));
96 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, conn->client_hello.random,
97 S2N_TLS_RANDOM_DATA_LEN));
98 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, conn->handshake_params.server_random,
99 S2N_TLS_RANDOM_DATA_LEN));
100
101 /* TLS1.0 and SSLv3 CBC chain the implicit IV across records; capture it so
102 * the deserialized connection can keep talking to its peer.
103 */
104 if (s2n_serialization_includes_implicit_iv(conn->actual_protocol_version,
105 conn->secure->cipher_suite)) {
106 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, conn->secure->client_implicit_iv,
107 S2N_TLS_MAX_IV_LEN));
108 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(output, conn->secure->server_implicit_iv,
109 S2N_TLS_MAX_IV_LEN));
110 }
111 return S2N_RESULT_OK;
112 }
113
114 int s2n_connection_serialize(struct s2n_connection *conn, uint8_t *buffer, uint32_t buffer_length)
115 {
116 POSIX_ENSURE_REF(conn);
117 POSIX_ENSURE_REF(conn->secure);
118 POSIX_ENSURE_REF(conn->secure->cipher_suite);
119 POSIX_ENSURE_REF(conn->config);
120 POSIX_ENSURE_REF(buffer);
121
122 POSIX_ENSURE(conn->config->serialized_connection_version != S2N_SERIALIZED_CONN_NONE,
123 S2N_ERR_INVALID_STATE);
124
125 /* This method must be called after negotiation */
126 POSIX_ENSURE(s2n_handshake_is_complete(conn), S2N_ERR_HANDSHAKE_NOT_COMPLETE);
127
128 /* The connection must not be closed already. Otherwise, we might have an alert
129 * queued up that would be sent in cleartext after we disable encryption. */
130 s2n_io_status status = S2N_IO_FULL_DUPLEX;
131 POSIX_ENSURE(s2n_connection_check_io_status(conn, status), S2N_ERR_CLOSED);
132
133 /* Best effort check for pending input or output data.
134 * This method should not be called until the application has stopped sending and receiving.
135 * Saving partial read or partial write state would complicate this problem.
136 */
137 POSIX_ENSURE(s2n_stuffer_data_available(&conn->header_in) == 0, S2N_ERR_INVALID_STATE);
138 POSIX_ENSURE(s2n_stuffer_data_available(&conn->in) == 0, S2N_ERR_INVALID_STATE);
139 POSIX_ENSURE(s2n_stuffer_data_available(&conn->out) == 0, S2N_ERR_INVALID_STATE);
140
141 uint32_t context_length = 0;
142 POSIX_GUARD(s2n_connection_serialization_length(conn, &context_length));
143 POSIX_ENSURE(buffer_length >= context_length, S2N_ERR_INSUFFICIENT_MEM_SIZE);
144
145 /* Stream ciphers (RC4) hold keystream position in libcrypto state that
146 * this API does not capture, so a deserialized stream-cipher connection
147 * cannot decrypt records from its peer. Reject rather than producing a
148 * blob that can't be used.
149 */
150 POSIX_ENSURE_REF(conn->secure->cipher_suite->record_alg);
151 POSIX_ENSURE_REF(conn->secure->cipher_suite->record_alg->cipher);
152 POSIX_ENSURE(conn->secure->cipher_suite->record_alg->cipher->type != S2N_STREAM,
153 S2N_ERR_INVALID_STATE);
154
155 struct s2n_blob context_blob = { 0 };
156 POSIX_GUARD(s2n_blob_init(&context_blob, buffer, buffer_length));
157 struct s2n_stuffer output = { 0 };
158 POSIX_GUARD(s2n_stuffer_init(&output, &context_blob));
159
160 POSIX_GUARD(s2n_stuffer_write_uint64(&output, S2N_SERIALIZED_CONN_V1));
161
162 POSIX_GUARD(s2n_stuffer_write_uint8(&output, conn->actual_protocol_version / 10));
163 POSIX_GUARD(s2n_stuffer_write_uint8(&output, conn->actual_protocol_version % 10));
164 POSIX_GUARD(s2n_stuffer_write_bytes(&output, conn->secure->cipher_suite->iana_value, S2N_TLS_CIPHER_SUITE_LEN));
165
166 POSIX_GUARD(s2n_stuffer_write_bytes(&output, conn->secure->client_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
167 POSIX_GUARD(s2n_stuffer_write_bytes(&output, conn->secure->server_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
168
169 POSIX_GUARD(s2n_stuffer_write_uint16(&output, conn->max_outgoing_fragment_length));
170
171 if (conn->actual_protocol_version >= S2N_TLS13) {
172 POSIX_GUARD_RESULT(s2n_connection_serialize_tls13_secrets(conn, &output));
173 } else {
174 POSIX_GUARD_RESULT(s2n_connection_serialize_secrets(conn, &output));
175 }
176
177 /* Users should not be able to send/recv on the connection after serialization as that
178 * could lead to nonce reuse. We close the connection to prevent the application from sending
179 * more application data. However, the application could still send a close_notify alert record
180 * to shutdown the connection, so we also intentionally wipe keys and disable encryption.
181 *
182 * A plaintext close_notify alert is not a security concern, although the peer will likely consider
183 * it an error.
184 */
185 POSIX_GUARD_RESULT(s2n_connection_set_closed(conn));
186 POSIX_GUARD_RESULT(s2n_crypto_parameters_wipe(conn->secure));
187
188 return S2N_SUCCESS;
189 }
190
191 struct s2n_connection_deserialize {
192 uint8_t protocol_version;
193 struct s2n_cipher_suite *cipher_suite;
194 uint8_t client_sequence_number[S2N_TLS_SEQUENCE_NUM_LEN];
195 uint8_t server_sequence_number[S2N_TLS_SEQUENCE_NUM_LEN];
196 uint16_t max_fragment_len;
197 union {
198 struct {
199 uint8_t master_secret[S2N_TLS_SECRET_LEN];
200 uint8_t client_random[S2N_TLS_RANDOM_DATA_LEN];
201 uint8_t server_random[S2N_TLS_RANDOM_DATA_LEN];
202 /* Only set for blobs with protocol_version < S2N_TLS11 + CBC/composite cipher */
203 uint8_t client_implicit_iv[S2N_TLS_MAX_IV_LEN];
204 uint8_t server_implicit_iv[S2N_TLS_MAX_IV_LEN];
205 } tls12;
206 struct {
207 uint8_t secret_size;
208 uint8_t client_application_secret[S2N_TLS_SECRET_LEN];
209 uint8_t server_application_secret[S2N_TLS_SECRET_LEN];
210 uint8_t resumption_master_secret[S2N_TLS_SECRET_LEN];
211 } tls13;
212 } version;
213 };
214
215 static S2N_RESULT s2n_connection_deserialize_tls13_secrets(struct s2n_stuffer *input,
216 struct s2n_connection_deserialize *parsed_values)
217 {
218 RESULT_ENSURE_REF(input);
219 RESULT_ENSURE_REF(parsed_values);
220
221 RESULT_GUARD_POSIX(s2n_hmac_digest_size(parsed_values->cipher_suite->prf_alg,
222 &parsed_values->version.tls13.secret_size));
223 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, parsed_values->version.tls13.client_application_secret,
224 parsed_values->version.tls13.secret_size));
225 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, parsed_values->version.tls13.server_application_secret,
226 parsed_values->version.tls13.secret_size));
227 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, parsed_values->version.tls13.resumption_master_secret,
228 parsed_values->version.tls13.secret_size));
229
230 return S2N_RESULT_OK;
231 }
232
233 static S2N_RESULT s2n_connection_deserialize_secrets(struct s2n_stuffer *input,
234 struct s2n_connection_deserialize *parsed_values)
235 {
236 RESULT_ENSURE_REF(input);
237 RESULT_ENSURE_REF(parsed_values);
238 RESULT_ENSURE_REF(parsed_values->cipher_suite);
239 RESULT_ENSURE_REF(parsed_values->cipher_suite->record_alg);
240 RESULT_ENSURE_REF(parsed_values->cipher_suite->record_alg->cipher);
241
242 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, parsed_values->version.tls12.master_secret, S2N_TLS_SECRET_LEN));
243 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, parsed_values->version.tls12.client_random, S2N_TLS_RANDOM_DATA_LEN));
244 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, parsed_values->version.tls12.server_random, S2N_TLS_RANDOM_DATA_LEN));
245
246 /* TLS1.0 and SSLv3 CBC blobs trail the implicit IVs. See
247 * s2n_connection_serialize_secrets. Other blobs end at server_random. */
248 if (s2n_serialization_includes_implicit_iv(parsed_values->protocol_version,
249 parsed_values->cipher_suite)) {
250 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, parsed_values->version.tls12.client_implicit_iv,
251 S2N_TLS_MAX_IV_LEN));
252 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(input, parsed_values->version.tls12.server_implicit_iv,
253 S2N_TLS_MAX_IV_LEN));
254 }
255
256 return S2N_RESULT_OK;
257 }
258
259 static S2N_RESULT s2n_connection_deserialize_parse(uint8_t *buffer, uint32_t buffer_length,
260 struct s2n_connection_deserialize *parsed_values)
261 {
262 RESULT_ENSURE_REF(parsed_values);
263
264 struct s2n_blob context_blob = { 0 };
265 RESULT_GUARD_POSIX(s2n_blob_init(&context_blob, buffer, buffer_length));
266 struct s2n_stuffer input = { 0 };
267 RESULT_GUARD_POSIX(s2n_stuffer_init_written(&input, &context_blob));
268
269 uint64_t serialized_version = 0;
270 RESULT_GUARD_POSIX(s2n_stuffer_read_uint64(&input, &serialized_version));
271 /* No other version is supported currently */
272 RESULT_ENSURE_EQ(serialized_version, S2N_SERIALIZED_CONN_V1);
273
274 uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN] = { 0 };
275 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(&input, protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN));
276 parsed_values->protocol_version = (protocol_version[0] * 10) + protocol_version[1];
277
278 RESULT_ENSURE(parsed_values->protocol_version >= S2N_SSLv3 && parsed_values->protocol_version <= s2n_highest_protocol_version,
279 S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
280
281 uint8_t cipher_suite[S2N_TLS_CIPHER_SUITE_LEN] = { 0 };
282 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(&input, cipher_suite, S2N_TLS_CIPHER_SUITE_LEN));
283 RESULT_GUARD(s2n_cipher_suite_from_iana(cipher_suite, S2N_TLS_CIPHER_SUITE_LEN, &parsed_values->cipher_suite));
284
285 /* SSLv3 uses a cloned cipher suite variant with the SSLv3-specific
286 * record_alg. s2n_cipher_suite_from_iana returns the base; swap to the
287 * variant. Same lookup-then-swap pattern as s2n_set_cipher_as_client and
288 * s2n_set_cipher_as_server during handshake.
289 */
290 if (parsed_values->protocol_version == S2N_SSLv3) {
291 RESULT_ENSURE_REF(parsed_values->cipher_suite->sslv3_cipher_suite);
292 parsed_values->cipher_suite = parsed_values->cipher_suite->sslv3_cipher_suite;
293 }
294
295 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(&input, parsed_values->client_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
296 RESULT_GUARD_POSIX(s2n_stuffer_read_bytes(&input, parsed_values->server_sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
297
298 RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&input, &parsed_values->max_fragment_len));
299
300 if (parsed_values->protocol_version >= S2N_TLS13) {
301 RESULT_GUARD(s2n_connection_deserialize_tls13_secrets(&input, parsed_values));
302 } else {
303 RESULT_GUARD(s2n_connection_deserialize_secrets(&input, parsed_values));
304 }
305
306 return S2N_RESULT_OK;
307 }
308
309 /* Boringssl and AWS-LC do a special check in tls13 during the first call to encrypt after
310 * initialization. In the first call they assume that the sequence number will be 0, and therefore
311 * the provided nonce is equivalent to the implicit IV because 0 ^ iv = iv. The recovered implicit IV
312 * is stored and used later on to ensure the monotonicity of sequence numbers.
313 *
314 * In the case of deserialization, in the first call the sequence number may not be 0.
315 * Therefore the provided nonce cannot be considered to be the implicit IV because n ^ iv != iv.
316 * This inability to get the correct implicit IV causes issues with encryption later on.
317 *
318 * To resolve this we perform one throwaway encryption call with a zero sequence number after
319 * deserialization. This allows the libcrypto to recover the implicit IV correctly.
320 */
321 static S2N_RESULT s2n_initialize_implicit_iv(struct s2n_connection *conn, struct s2n_connection_deserialize *parsed_values)
322 {
323 RESULT_ENSURE_REF(conn);
324 RESULT_ENSURE_REF(parsed_values);
325 RESULT_ENSURE_REF(conn->secure);
326 RESULT_ENSURE_REF(conn->server);
327 RESULT_ENSURE_REF(conn->client);
328
329 if (!s2n_libcrypto_supports_evp_aead_tls()) {
330 return S2N_RESULT_OK;
331 }
332
333 uint8_t *seq_num = parsed_values->server_sequence_number;
334 uint8_t *implicit_iv = conn->server->server_implicit_iv;
335 struct s2n_session_key key = conn->server->server_key;
336 if (conn->mode == S2N_CLIENT) {
337 seq_num = parsed_values->client_sequence_number;
338 implicit_iv = conn->client->client_implicit_iv;
339 key = conn->client->client_key;
340 }
341
342 uint64_t parsed_sequence_num = 0;
343 struct s2n_blob seq_num_blob = { 0 };
344 RESULT_GUARD_POSIX(s2n_blob_init(&seq_num_blob, seq_num, S2N_TLS_SEQUENCE_NUM_LEN));
345 RESULT_GUARD_POSIX(s2n_sequence_number_to_uint64(&seq_num_blob, &parsed_sequence_num));
346
347 /* we don't need to initialize the context when the sequence number is 0 */
348 if (parsed_sequence_num == 0) {
349 return S2N_RESULT_OK;
350 }
351
352 uint8_t in_data[S2N_TLS_GCM_TAG_LEN] = { 0 };
353 struct s2n_blob in_blob = { 0 };
354 RESULT_GUARD_POSIX(s2n_blob_init(&in_blob, in_data, sizeof(in_data)));
355
356 struct s2n_blob iv_blob = { 0 };
357 RESULT_GUARD_POSIX(s2n_blob_init(&iv_blob, implicit_iv, S2N_TLS13_FIXED_IV_LEN));
358
359 struct s2n_blob aad_blob = { 0 };
360 RESULT_GUARD_POSIX(s2n_blob_init(&aad_blob, NULL, 0));
361
362 RESULT_ENSURE_REF(conn->secure->cipher_suite);
363 RESULT_ENSURE_REF(conn->secure->cipher_suite->record_alg);
364 RESULT_ENSURE_REF(conn->secure->cipher_suite->record_alg->cipher);
365 RESULT_GUARD_POSIX(conn->secure->cipher_suite->record_alg->cipher->io.aead.encrypt(&key,
366 &iv_blob, &aad_blob, &in_blob, &in_blob));
367
368 return S2N_RESULT_OK;
369 }
370
371 static S2N_RESULT s2n_restore_tls13_secrets(struct s2n_connection *conn, struct s2n_connection_deserialize *parsed_values)
372 {
373 RESULT_ENSURE_REF(conn);
374 RESULT_ENSURE_REF(parsed_values);
375
376 RESULT_CHECKED_MEMCPY(conn->secrets.version.tls13.client_app_secret,
377 parsed_values->version.tls13.client_application_secret, parsed_values->version.tls13.secret_size);
378 RESULT_CHECKED_MEMCPY(conn->secrets.version.tls13.server_app_secret,
379 parsed_values->version.tls13.server_application_secret, parsed_values->version.tls13.secret_size);
380 RESULT_CHECKED_MEMCPY(conn->secrets.version.tls13.resumption_master_secret,
381 parsed_values->version.tls13.resumption_master_secret, parsed_values->version.tls13.secret_size);
382
383 RESULT_GUARD(s2n_tls13_key_schedule_set_key(conn, S2N_MASTER_SECRET, S2N_SERVER));
384 RESULT_GUARD(s2n_tls13_key_schedule_set_key(conn, S2N_MASTER_SECRET, S2N_CLIENT));
385
386 RESULT_GUARD(s2n_initialize_implicit_iv(conn, parsed_values));
387
388 return S2N_RESULT_OK;
389 }
390
391 static S2N_RESULT s2n_restore_secrets(struct s2n_connection *conn, struct s2n_connection_deserialize *parsed_values)
392 {
393 RESULT_ENSURE_REF(conn);
394 RESULT_ENSURE_REF(parsed_values);
395 RESULT_ENSURE_REF(conn->secure);
396
397 RESULT_CHECKED_MEMCPY(conn->secrets.version.tls12.master_secret, parsed_values->version.tls12.master_secret,
398 S2N_TLS_SECRET_LEN);
399 RESULT_CHECKED_MEMCPY(conn->client_hello.random, parsed_values->version.tls12.client_random,
400 S2N_TLS_RANDOM_DATA_LEN);
401 RESULT_CHECKED_MEMCPY(conn->handshake_params.server_random, parsed_values->version.tls12.server_random,
402 S2N_TLS_RANDOM_DATA_LEN);
403 RESULT_GUARD_POSIX(s2n_prf_key_expansion(conn));
404
405 /* PRF key expansion seeds the implicit IVs from the master secret, which is
406 * only correct at record 0. For TLS1.0 and SSLv3 CBC the blob ships the
407 * current IV state so the connection can continue where it left off.
408 */
409 if (s2n_serialization_includes_implicit_iv(parsed_values->protocol_version,
410 parsed_values->cipher_suite)) {
411 RESULT_CHECKED_MEMCPY(conn->secure->client_implicit_iv,
412 parsed_values->version.tls12.client_implicit_iv, S2N_TLS_MAX_IV_LEN);
413 RESULT_CHECKED_MEMCPY(conn->secure->server_implicit_iv,
414 parsed_values->version.tls12.server_implicit_iv, S2N_TLS_MAX_IV_LEN);
415 }
416
417 return S2N_RESULT_OK;
418 }
419
420 int s2n_connection_deserialize(struct s2n_connection *conn, uint8_t *buffer, uint32_t buffer_length)
421 {
422 POSIX_ENSURE_REF(conn);
423 POSIX_ENSURE_REF(conn->secure);
424 POSIX_ENSURE_REF(buffer);
425
426 /* The serialized blob does not include a MAC or signature. All values from the buffer
427 * are trusted after basic format validation. Callers must verify the integrity of the
428 * buffer before calling this function. See the s2n_connection_deserialize API docs.
429 */
430
431 /* Read parsed values into a temporary struct so that the connection is unaltered if parsing fails */
432 struct s2n_connection_deserialize parsed_values = { 0 };
433 POSIX_ENSURE(s2n_result_is_ok(s2n_connection_deserialize_parse(buffer, buffer_length, &parsed_values)),
434 S2N_ERR_INVALID_SERIALIZED_CONNECTION);
435
436 /* Rehydrate fields now that parsing has completed successfully */
437 conn->actual_protocol_version = parsed_values.protocol_version;
438 conn->secure->cipher_suite = parsed_values.cipher_suite;
439 POSIX_GUARD_RESULT(s2n_connection_set_max_fragment_length(conn, parsed_values.max_fragment_len));
440
441 /* Mark the connection as having been deserialized */
442 conn->deserialized_conn = true;
443
444 /* Key expansion */
445 if (parsed_values.protocol_version >= S2N_TLS13) {
446 POSIX_GUARD_RESULT(s2n_restore_tls13_secrets(conn, &parsed_values));
447 } else {
448 POSIX_GUARD_RESULT(s2n_restore_secrets(conn, &parsed_values));
449 }
450
451 /* Wait until after key generation to restore sequence numbers since they get zeroed during
452 * key expansion */
453 POSIX_CHECKED_MEMCPY(conn->secure->client_sequence_number, parsed_values.client_sequence_number,
454 S2N_TLS_SEQUENCE_NUM_LEN);
455 POSIX_CHECKED_MEMCPY(conn->secure->server_sequence_number, parsed_values.server_sequence_number,
456 S2N_TLS_SEQUENCE_NUM_LEN);
457
458 conn->client = conn->secure;
459 conn->server = conn->secure;
460
461 return S2N_SUCCESS;
462 }