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 <stdint.h>
17 : : #include <sys/param.h>
18 : :
19 : : #include "crypto/s2n_cipher.h"
20 : : #include "crypto/s2n_hmac.h"
21 : : #include "crypto/s2n_sequence.h"
22 : : #include "error/s2n_errno.h"
23 : : #include "stuffer/s2n_stuffer.h"
24 : : #include "tls/s2n_cipher_suites.h"
25 : : #include "tls/s2n_connection.h"
26 : : #include "tls/s2n_crypto.h"
27 : : #include "tls/s2n_ktls.h"
28 : : #include "tls/s2n_record.h"
29 : : #include "utils/s2n_blob.h"
30 : : #include "utils/s2n_random.h"
31 : : #include "utils/s2n_safety.h"
32 : :
33 : : extern uint8_t s2n_unknown_protocol_version;
34 : :
35 : : /* In TLS1.3 the record type is obfuscated as APPLICATION_DATA once the handshake begins to be encrypted.
36 : : * The real record type is encrypted and written in the final byte of the record.
37 : : * In TLS1.2 the record type is always cleartext. */
38 : 3603031 : #define RECORD_TYPE(is_tls13_record, content_type) (is_tls13_record ? TLS_APPLICATION_DATA : content_type)
39 : :
40 : : /* How much overhead does the IV, MAC, TAG and padding bytes introduce ? */
41 : : static S2N_RESULT s2n_tls_record_overhead(struct s2n_connection *conn, uint16_t *out)
42 : 3603356 : {
43 [ - + ][ # # ]: 3603356 : RESULT_ENSURE_REF(conn);
44 [ # # ][ - + ]: 3603356 : RESULT_ENSURE_MUT(out);
45 : 3603356 : struct s2n_crypto_parameters *active = conn->server;
46 : :
47 [ + + ]: 3603356 : if (conn->mode == S2N_CLIENT) {
48 : 83095 : active = conn->client;
49 : 83095 : }
50 : :
51 : 3603356 : uint8_t extra = 0;
52 [ - + ]: 3603356 : RESULT_GUARD_POSIX(s2n_hmac_digest_size(active->cipher_suite->record_alg->hmac_alg, &extra));
53 : :
54 [ + + ]: 3603356 : if (active->cipher_suite->record_alg->cipher->type == S2N_CBC) {
55 : : /* Subtract one for the padding length byte */
56 : 41773 : extra += 1;
57 : :
58 [ + + ]: 41773 : if (conn->actual_protocol_version > S2N_TLS10) {
59 : 32918 : extra += active->cipher_suite->record_alg->cipher->io.cbc.record_iv_size;
60 : 32918 : }
61 [ + + ]: 3561583 : } else if (active->cipher_suite->record_alg->cipher->type == S2N_AEAD) {
62 : 3344832 : extra += active->cipher_suite->record_alg->cipher->io.aead.tag_size;
63 : 3344832 : extra += active->cipher_suite->record_alg->cipher->io.aead.record_iv_size;
64 [ + + ][ + + ]: 3344832 : } else if (active->cipher_suite->record_alg->cipher->type == S2N_COMPOSITE && conn->actual_protocol_version > S2N_TLS10) {
65 : 131167 : extra += active->cipher_suite->record_alg->cipher->io.comp.record_iv_size;
66 : 131167 : }
67 : :
68 : 3603356 : *out = extra;
69 : :
70 : 3603356 : return S2N_RESULT_OK;
71 : 3603356 : }
72 : :
73 : : /* This function returns maximum size of plaintext data to write for the payload.
74 : : * Record overheads are not included here.
75 : : */
76 : : S2N_RESULT s2n_record_max_write_payload_size(struct s2n_connection *conn, uint16_t *max_fragment_size)
77 : 3626796 : {
78 [ - + ][ # # ]: 3626796 : RESULT_ENSURE_REF(conn);
79 [ # # ][ - + ]: 3626796 : RESULT_ENSURE_REF(conn->config);
80 [ # # ][ - + ]: 3626796 : RESULT_ENSURE_MUT(max_fragment_size);
81 [ + + ][ + - ]: 3626796 : RESULT_ENSURE(conn->max_outgoing_fragment_length > 0, S2N_ERR_FRAGMENT_LENGTH_TOO_SMALL);
82 : :
83 : 3626795 : *max_fragment_size = MIN(conn->max_outgoing_fragment_length, S2N_TLS_MAXIMUM_FRAGMENT_LENGTH);
84 : :
85 : : /* If a custom send buffer is configured, ensure it will be large enough for the payload.
86 : : * That may mean we need a smaller fragment size.
87 : : */
88 : 3626795 : uint32_t send_buffer_override = conn->config->send_buffer_size_override;
89 [ + + ]: 3626795 : if (send_buffer_override) {
90 : 70 : uint16_t max_record_size = 0;
91 [ - + ]: 70 : RESULT_GUARD(s2n_record_max_write_size(conn, *max_fragment_size, &max_record_size));
92 [ + + ]: 70 : if (send_buffer_override < max_record_size) {
93 : 10 : size_t overhead = (max_record_size - *max_fragment_size);
94 [ - + ][ # # ]: 10 : RESULT_ENSURE_GT(send_buffer_override, overhead);
95 : 10 : *max_fragment_size = send_buffer_override - overhead;
96 : 10 : }
97 : 70 : }
98 : :
99 : 3626795 : return S2N_RESULT_OK;
100 : 3626795 : }
101 : :
102 : : S2N_RESULT s2n_record_max_write_size(struct s2n_connection *conn, uint16_t max_fragment_size, uint16_t *max_record_size)
103 : 3306818 : {
104 [ + - ][ + + ]: 3306818 : RESULT_ENSURE_REF(conn);
105 [ + + ][ + - ]: 3306817 : RESULT_ENSURE_MUT(max_record_size);
106 : :
107 [ + + ]: 3306816 : if (!IS_NEGOTIATED(conn)) {
108 : 3300208 : *max_record_size = S2N_TLS_MAX_RECORD_LEN_FOR(max_fragment_size);
109 [ + + ]: 3300208 : } else if (conn->actual_protocol_version < S2N_TLS13) {
110 : 2353 : *max_record_size = S2N_TLS12_MAX_RECORD_LEN_FOR(max_fragment_size);
111 : 4255 : } else {
112 : 4255 : *max_record_size = S2N_TLS13_MAX_RECORD_LEN_FOR(max_fragment_size);
113 : 4255 : }
114 : 3306816 : return S2N_RESULT_OK;
115 : 3306817 : }
116 : :
117 : : /* Find the largest size that will fit within an ethernet frame for a "small" payload */
118 : : S2N_RESULT s2n_record_min_write_payload_size(struct s2n_connection *conn, uint16_t *payload_size)
119 : 325 : {
120 [ - + ][ # # ]: 325 : RESULT_ENSURE_REF(conn);
121 [ - + ][ # # ]: 325 : RESULT_ENSURE_MUT(payload_size);
122 : :
123 : : /* remove ethernet, TCP/IP and TLS header overheads */
124 [ - + ]: 325 : const uint16_t min_outgoing_fragment_length = ETH_MTU - (conn->ipv6 ? IP_V6_HEADER_LENGTH : IP_V4_HEADER_LENGTH)
125 : 325 : - TCP_HEADER_LENGTH - TCP_OPTIONS_LENGTH - S2N_TLS_RECORD_HEADER_LENGTH;
126 : :
127 [ - + ][ # # ]: 325 : RESULT_ENSURE(min_outgoing_fragment_length <= S2N_TLS_MAXIMUM_FRAGMENT_LENGTH, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
128 : 325 : uint16_t size = min_outgoing_fragment_length;
129 : :
130 [ + + ]: 325 : const struct s2n_crypto_parameters *active = conn->mode == S2N_CLIENT ? conn->client : conn->server;
131 : :
132 : : /* Round the fragment size down to be block aligned */
133 [ + + ]: 325 : if (active->cipher_suite->record_alg->cipher->type == S2N_CBC) {
134 : 1 : size -= size % active->cipher_suite->record_alg->cipher->io.cbc.block_size;
135 [ + + ]: 324 : } else if (active->cipher_suite->record_alg->cipher->type == S2N_COMPOSITE) {
136 : 1 : size -= size % active->cipher_suite->record_alg->cipher->io.comp.block_size;
137 : : /* Composite digest length */
138 : 1 : size -= active->cipher_suite->record_alg->cipher->io.comp.mac_key_size;
139 : : /* Padding length byte */
140 : 1 : size -= 1;
141 : 1 : }
142 : :
143 : : /* If TLS1.3, remove content type */
144 [ + + ]: 325 : if (conn->actual_protocol_version >= S2N_TLS13) {
145 [ # # ][ - + ]: 68 : RESULT_ENSURE(size > S2N_TLS_CONTENT_TYPE_LENGTH, S2N_ERR_FRAGMENT_LENGTH_TOO_SMALL);
146 : 68 : size -= S2N_TLS_CONTENT_TYPE_LENGTH;
147 : 68 : }
148 : :
149 : : /* subtract overheads of a TLS record */
150 : 325 : uint16_t overhead = 0;
151 [ - + ]: 325 : RESULT_GUARD(s2n_tls_record_overhead(conn, &overhead));
152 [ # # ][ - + ]: 325 : RESULT_ENSURE(size > overhead, S2N_ERR_FRAGMENT_LENGTH_TOO_SMALL);
153 : 325 : size -= overhead;
154 : :
155 [ - + ][ # # ]: 325 : RESULT_ENSURE(size > 0, S2N_ERR_FRAGMENT_LENGTH_TOO_SMALL);
156 [ - + ][ # # ]: 325 : RESULT_ENSURE(size <= ETH_MTU, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
157 : :
158 : 325 : *payload_size = size;
159 : :
160 : 325 : return S2N_RESULT_OK;
161 : 325 : }
162 : :
163 : : int s2n_record_write_protocol_version(struct s2n_connection *conn, uint8_t record_type, struct s2n_stuffer *out)
164 : 3603031 : {
165 : 3603031 : uint8_t record_protocol_version = conn->actual_protocol_version;
166 : :
167 : : /**
168 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-5.1
169 : : *# This version value is historical, deriving from the use of 0x0301 for
170 : : *# TLS 1.0 and 0x0300 for SSL 3.0. In order to maximize backward
171 : : *# compatibility, a record containing an initial ClientHello SHOULD have
172 : : *# version 0x0301 (reflecting TLS 1.0)
173 : : *
174 : : * We set actual_protocol_version early for clients, but we do not
175 : : * use that assumed value here in case we are talking to a legacy
176 : : * server that expects TLS1.0.
177 : : *
178 : : * Both TLS 1.3 early data and a deserialized connection will
179 : : * send data without the server_protocol_version being known. However,
180 : : * the record type would be set to APPLICATION_DATA in their cases
181 : : * so this check is avoided.
182 : : **/
183 [ + + ]: 3603031 : if (conn->server_protocol_version == s2n_unknown_protocol_version
184 [ + + ]: 3603031 : && record_type == TLS_HANDSHAKE) {
185 : 7814 : record_protocol_version = MIN(record_protocol_version, S2N_TLS10);
186 : 7814 : }
187 : :
188 : : /**
189 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-5.1
190 : : *# legacy_record_version: MUST be set to 0x0303 for all records
191 : : *# generated by a TLS 1.3 implementation other than an initial
192 : : *# ClientHello (i.e., one not generated after a HelloRetryRequest),
193 : : *# where it MAY also be 0x0301 for compatibility purposes.
194 : : **/
195 : 3603031 : record_protocol_version = MIN(record_protocol_version, S2N_TLS12);
196 : :
197 : : /* Never send an empty protocol version.
198 : : * If the protocol version is unknown, default to TLS1.0 like we do for initial ClientHellos.
199 : : */
200 [ + + ]: 3603031 : if (record_protocol_version == s2n_unknown_protocol_version) {
201 : 18 : record_protocol_version = S2N_TLS10;
202 : 18 : }
203 : :
204 : 3603031 : uint8_t protocol_version[S2N_TLS_PROTOCOL_VERSION_LEN];
205 : 3603031 : protocol_version[0] = record_protocol_version / 10;
206 : 3603031 : protocol_version[1] = record_protocol_version % 10;
207 : :
208 [ - + ]: 3603031 : POSIX_GUARD(s2n_stuffer_write_bytes(out, protocol_version, S2N_TLS_PROTOCOL_VERSION_LEN));
209 : :
210 : 3603031 : return 0;
211 : 3603031 : }
212 : :
213 : : static inline int s2n_record_encrypt(
214 : : struct s2n_connection *conn,
215 : : const struct s2n_cipher_suite *cipher_suite,
216 : : struct s2n_session_key *session_key,
217 : : struct s2n_blob *iv,
218 : : struct s2n_blob *aad,
219 : : struct s2n_blob *en,
220 : : uint8_t *implicit_iv, uint16_t block_size)
221 : 3603030 : {
222 [ - + ][ # # ]: 3603030 : POSIX_ENSURE_REF(en->data);
223 : :
224 : 3603030 : switch (cipher_suite->record_alg->cipher->type) {
225 [ + + ]: 52793 : case S2N_STREAM:
226 [ - + ]: 52793 : POSIX_GUARD(cipher_suite->record_alg->cipher->io.stream.encrypt(session_key, en, en));
227 : 52793 : break;
228 [ + + ]: 52793 : case S2N_CBC:
229 [ - + ]: 41772 : POSIX_GUARD(cipher_suite->record_alg->cipher->io.cbc.encrypt(session_key, iv, en, en));
230 : :
231 : : /* Copy the last encrypted block to be the next IV */
232 [ + + ]: 41772 : if (conn->actual_protocol_version < S2N_TLS11) {
233 [ - + ][ # # ]: 8855 : POSIX_ENSURE_GTE(en->size, block_size);
234 [ - + ][ # # ]: 8855 : POSIX_CHECKED_MEMCPY(implicit_iv, en->data + en->size - block_size, block_size);
[ + - ]
235 : 8855 : }
236 : 41772 : break;
237 [ + + ]: 3344510 : case S2N_AEAD:
238 [ - + ]: 3344510 : POSIX_GUARD(cipher_suite->record_alg->cipher->io.aead.encrypt(session_key, iv, aad, en, en));
239 : 3344510 : break;
240 [ + + ]: 3344510 : case S2N_COMPOSITE:
241 : : /* This will: compute mac, append padding, append padding length, and encrypt */
242 [ - + ]: 163955 : POSIX_GUARD(cipher_suite->record_alg->cipher->io.comp.encrypt(session_key, iv, en, en));
243 : :
244 : : /* Copy the last encrypted block to be the next IV */
245 [ # # ][ - + ]: 163955 : POSIX_ENSURE_GTE(en->size, block_size);
246 [ - + ][ # # ]: 163955 : POSIX_CHECKED_MEMCPY(implicit_iv, en->data + en->size - block_size, block_size);
[ + - ]
247 : 163955 : break;
248 [ - + ]: 163955 : default:
249 [ # # ]: 0 : POSIX_BAIL(S2N_ERR_CIPHER_TYPE);
250 : 0 : break;
251 : 3603030 : }
252 : :
253 : 3603030 : return 0;
254 : 3603030 : }
255 : :
256 : : static S2N_RESULT s2n_record_write_mac(struct s2n_connection *conn, struct s2n_blob *record_header,
257 : : struct s2n_blob *plaintext, struct s2n_stuffer *out, uint32_t *bytes_written)
258 : 3603031 : {
259 [ - + ][ # # ]: 3603031 : RESULT_ENSURE_REF(conn);
260 [ # # ][ - + ]: 3603031 : RESULT_ENSURE_REF(conn->server);
261 [ - + ][ # # ]: 3603031 : RESULT_ENSURE_REF(conn->client);
262 [ # # ][ - + ]: 3603031 : RESULT_ENSURE_REF(record_header);
263 [ - + ][ # # ]: 3603031 : RESULT_ENSURE_REF(plaintext);
264 [ # # ][ - + ]: 3603031 : RESULT_ENSURE_REF(out);
265 [ - + ][ # # ]: 3603031 : RESULT_ENSURE_REF(bytes_written);
266 : 3603031 : *bytes_written = 0;
267 : :
268 : 3603031 : struct s2n_hmac_state *mac = &conn->server->server_record_mac;
269 : 3603031 : const struct s2n_cipher_suite *cipher_suite = conn->server->cipher_suite;
270 : 3603031 : uint8_t *sequence_number = conn->server->server_sequence_number;
271 : :
272 [ + + ]: 3603031 : if (conn->mode == S2N_CLIENT) {
273 : 82777 : mac = &conn->client->client_record_mac;
274 : 82777 : cipher_suite = conn->client->cipher_suite;
275 : 82777 : sequence_number = conn->client->client_sequence_number;
276 : 82777 : }
277 : :
278 [ # # ][ - + ]: 3603031 : RESULT_ENSURE_REF(cipher_suite);
279 [ - + ][ # # ]: 3603031 : RESULT_ENSURE_REF(cipher_suite->record_alg);
280 : :
281 [ + + ]: 3603031 : if (cipher_suite->record_alg->hmac_alg == S2N_HMAC_NONE) {
282 : : /* If the S2N_HMAC_NONE algorithm is specified, a MAC should not be explicitly written.
283 : : * This is the case for AEAD and Composite cipher types, where the MAC is written as part
284 : : * of encryption. This is also the case for plaintext handshake records, where the null
285 : : * stream cipher is used.
286 : : */
287 : 3553168 : return S2N_RESULT_OK;
288 : 3553168 : }
289 : :
290 : : /**
291 : : *= https://www.rfc-editor.org/rfc/rfc5246#section-6.2.3.1
292 : : *# The MAC is generated as:
293 : : *#
294 : : *# MAC(MAC_write_key, seq_num +
295 : : */
296 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_hmac_update(mac, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
297 : :
298 : 49863 : struct s2n_stuffer header_stuffer = { 0 };
299 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_stuffer_init_written(&header_stuffer, record_header));
300 : :
301 : : /**
302 : : *= https://www.rfc-editor.org/rfc/rfc5246#section-6.2.3.1
303 : : *# TLSCompressed.type +
304 : : */
305 : 49863 : void *record_type_byte = s2n_stuffer_raw_read(&header_stuffer, sizeof(uint8_t));
306 [ - + ][ # # ]: 49863 : RESULT_ENSURE_REF(record_type_byte);
307 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_hmac_update(mac, record_type_byte, sizeof(uint8_t)));
308 : :
309 : : /**
310 : : *= https://www.rfc-editor.org/rfc/rfc5246#section-6.2.3.1
311 : : *# TLSCompressed.version +
312 : : */
313 : 49863 : void *protocol_version_bytes = s2n_stuffer_raw_read(&header_stuffer, S2N_TLS_PROTOCOL_VERSION_LEN);
314 [ - + ][ # # ]: 49863 : RESULT_ENSURE_REF(protocol_version_bytes);
315 [ + + ]: 49863 : if (conn->actual_protocol_version > S2N_SSLv3) {
316 : : /* SSLv3 doesn't include the protocol version in the MAC. */
317 [ - + ]: 49238 : RESULT_GUARD_POSIX(s2n_hmac_update(mac, protocol_version_bytes, S2N_TLS_PROTOCOL_VERSION_LEN));
318 : 49238 : }
319 : :
320 : : /**
321 : : *= https://www.rfc-editor.org/rfc/rfc5246#section-6.2.3.1
322 : : *# TLSCompressed.length +
323 : : *
324 : : * Note that the length field refers to the length of the plaintext content, not the length of
325 : : * TLSCiphertext fragment written to the record header, which accounts for additional fields
326 : : * such as the padding and MAC.
327 : : */
328 : 49863 : uint8_t content_length_bytes[sizeof(uint16_t)] = { 0 };
329 : 49863 : struct s2n_blob content_length_blob = { 0 };
330 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_blob_init(&content_length_blob, content_length_bytes, sizeof(content_length_bytes)));
331 : 49863 : struct s2n_stuffer content_length_stuffer = { 0 };
332 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_stuffer_init(&content_length_stuffer, &content_length_blob));
333 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(&content_length_stuffer, plaintext->size));
334 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_hmac_update(mac, content_length_bytes, sizeof(content_length_bytes)));
335 : :
336 : : /**
337 : : *= https://www.rfc-editor.org/rfc/rfc5246#section-6.2.3.1
338 : : *# TLSCompressed.fragment);
339 : : *#
340 : : *# where "+" denotes concatenation.
341 : : */
342 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_hmac_update(mac, plaintext->data, plaintext->size));
343 : :
344 : 49863 : uint8_t mac_digest_size = 0;
345 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_hmac_digest_size(mac->alg, &mac_digest_size));
346 : 49863 : uint8_t *digest = s2n_stuffer_raw_write(out, mac_digest_size);
347 [ - + ][ # # ]: 49863 : RESULT_ENSURE_REF(digest);
348 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_hmac_digest(mac, digest, mac_digest_size));
349 : 49863 : *bytes_written = mac_digest_size;
350 : :
351 [ - + ]: 49863 : RESULT_GUARD_POSIX(s2n_hmac_reset(mac));
352 : :
353 : 49863 : return S2N_RESULT_OK;
354 : 49863 : }
355 : :
356 : : int s2n_record_writev(struct s2n_connection *conn, uint8_t content_type, const struct iovec *in, int in_count, size_t offs, size_t to_write)
357 : 3603041 : {
358 [ + + ]: 3603041 : if (conn->ktls_send_enabled) {
359 : 8 : return s2n_ktls_record_writev(conn, content_type, in, in_count, offs, to_write);
360 : 8 : }
361 : :
362 : 3603033 : struct s2n_blob iv = { 0 };
363 : 3603033 : uint8_t padding = 0;
364 : 3603033 : uint16_t block_size = 0;
365 : 3603033 : uint8_t aad_iv[S2N_TLS_MAX_IV_LEN] = { 0 };
366 : :
367 : : /* In TLS 1.3, handle CCS message as unprotected records */
368 : 3603033 : struct s2n_crypto_parameters *current_client_crypto = conn->client;
369 : 3603033 : struct s2n_crypto_parameters *current_server_crypto = conn->server;
370 [ + + ][ + + ]: 3603033 : if (conn->actual_protocol_version == S2N_TLS13 && content_type == TLS_CHANGE_CIPHER_SPEC) {
371 [ - + ][ # # ]: 6915 : POSIX_ENSURE_REF(conn->initial);
372 : 6915 : conn->client = conn->initial;
373 : 6915 : conn->server = conn->initial;
374 : 6915 : }
375 : :
376 : 3603033 : uint8_t *sequence_number = conn->server->server_sequence_number;
377 : 3603033 : struct s2n_session_key *session_key = &conn->server->server_key;
378 : 3603033 : const struct s2n_cipher_suite *cipher_suite = conn->server->cipher_suite;
379 : 3603033 : uint8_t *implicit_iv = conn->server->server_implicit_iv;
380 : :
381 [ + + ]: 3603033 : if (conn->mode == S2N_CLIENT) {
382 : 82777 : sequence_number = conn->client->client_sequence_number;
383 : 82777 : session_key = &conn->client->client_key;
384 : 82777 : cipher_suite = conn->client->cipher_suite;
385 : 82777 : implicit_iv = conn->client->client_implicit_iv;
386 : 82777 : }
387 : :
388 : : /* The NULL stream cipher MUST NEVER be used for ApplicationData.
389 : : * Writing ApplicationData unencrypted defeats the purpose of TLS. */
390 [ + + ]: 3603033 : if (cipher_suite->record_alg->cipher == &s2n_null_cipher) {
391 [ + + ][ + - ]: 52795 : POSIX_ENSURE(content_type != TLS_APPLICATION_DATA, S2N_ERR_ENCRYPT);
392 : 52795 : }
393 : :
394 : 3603032 : const int is_tls13_record = cipher_suite->record_alg->flags & S2N_TLS13_RECORD_AEAD_NONCE;
395 [ - + ][ - + ]: 3603032 : s2n_stack_blob(aad, is_tls13_record ? S2N_TLS13_AAD_LEN : S2N_TLS_MAX_AAD_LEN, S2N_TLS_MAX_AAD_LEN);
[ # # ][ + + ]
396 : :
397 : : /* If we aren't buffering multiple records, then the output stuffer should be empty. */
398 [ + + ]: 3603032 : if (!conn->multirecord_send) {
399 [ + - ][ + + ]: 3603002 : POSIX_ENSURE(s2n_stuffer_data_available(&conn->out) == 0, S2N_ERR_RECORD_STUFFER_NEEDS_DRAINING);
400 : 3603002 : }
401 : :
402 : : /* Before we do anything, we need to figure out what the length of the
403 : : * fragment is going to be.
404 : : */
405 : 3603031 : uint16_t max_write_payload_size = 0;
406 [ - + ]: 3603031 : POSIX_GUARD_RESULT(s2n_record_max_write_payload_size(conn, &max_write_payload_size));
407 : 3603031 : const uint16_t data_bytes_to_take = MIN(to_write, max_write_payload_size);
408 : :
409 : 3603031 : uint16_t extra = 0;
410 [ - + ]: 3603031 : POSIX_GUARD_RESULT(s2n_tls_record_overhead(conn, &extra));
411 : :
412 : : /* If we have padding to worry about, figure that out too */
413 [ + + ]: 3603031 : if (cipher_suite->record_alg->cipher->type == S2N_CBC) {
414 : 41772 : block_size = cipher_suite->record_alg->cipher->io.cbc.block_size;
415 [ + + ]: 41772 : if (((data_bytes_to_take + extra) % block_size)) {
416 : 38732 : padding = block_size - ((data_bytes_to_take + extra) % block_size);
417 : 38732 : }
418 [ + + ]: 3561259 : } else if (cipher_suite->record_alg->cipher->type == S2N_COMPOSITE) {
419 : 163955 : block_size = cipher_suite->record_alg->cipher->io.comp.block_size;
420 : 163955 : }
421 : :
422 [ + + ]: 3603031 : if (s2n_stuffer_is_freed(&conn->out)) {
423 : : /* If the output buffer has not been allocated yet, allocate
424 : : * at least enough memory to hold a record with the local maximum fragment length.
425 : : *
426 : : * The local maximum fragment length is:
427 : : * 1) The local default configured for new connections
428 : : * 2) The local value set by the user via s2n_connection_prefer_throughput()
429 : : * or s2n_connection_prefer_low_latency()
430 : : * 3) On the server, the minimum of the local value and the value negotiated with the
431 : : * client via the max_fragment_length extension
432 : : *
433 : : * Because this only occurs if the output buffer has not been allocated,
434 : : * it does NOT resize existing buffers.
435 : : */
436 : 3306330 : uint16_t max_wire_record_size = 0;
437 [ - + ]: 3306330 : POSIX_GUARD_RESULT(s2n_record_max_write_size(conn, max_write_payload_size, &max_wire_record_size));
438 : :
439 : 3306330 : uint32_t buffer_size = MAX(conn->config->send_buffer_size_override, max_wire_record_size);
440 [ - + ]: 3306330 : POSIX_GUARD(s2n_stuffer_growable_alloc(&conn->out, buffer_size));
441 : 3306330 : }
442 : :
443 : : /* A record only local stuffer used to avoid tainting the conn->out stuffer or overwriting
444 : : * previous records. It should be used to add an individual record to the out stuffer.
445 : : */
446 : 3603031 : struct s2n_blob record_blob = { 0 };
447 : 3603031 : struct s2n_stuffer record_stuffer = { 0 };
448 [ - + ]: 3603031 : POSIX_GUARD(s2n_blob_init(&record_blob,
449 : 3603031 : conn->out.blob.data + conn->out.write_cursor,
450 : 3603031 : s2n_stuffer_space_remaining(&conn->out)));
451 [ - + ]: 3603031 : POSIX_GUARD(s2n_stuffer_init(&record_stuffer, &record_blob));
452 : :
453 : : /* Now that we know the length, start writing the record */
454 [ + + ]: 3603031 : uint8_t record_type = RECORD_TYPE(is_tls13_record, content_type);
455 [ - + ]: 3603031 : POSIX_GUARD(s2n_stuffer_write_uint8(&record_stuffer, record_type));
456 [ - + ]: 3603031 : POSIX_GUARD(s2n_record_write_protocol_version(conn, record_type, &record_stuffer));
457 : :
458 : : /* Compute non-payload parts of the MAC(seq num, type, proto vers, fragment length) for composite ciphers.
459 : : * Composite "encrypt" will MAC the payload data and fill in padding.
460 : : */
461 [ + + ]: 3603031 : if (cipher_suite->record_alg->cipher->type == S2N_COMPOSITE) {
462 : : /* Only fragment length is needed for MAC, but the EVP ctrl function needs fragment length + eiv len. */
463 : 163955 : uint16_t payload_and_eiv_len = data_bytes_to_take;
464 [ + + ]: 163955 : if (conn->actual_protocol_version > S2N_TLS10) {
465 : 131166 : payload_and_eiv_len += block_size;
466 : 131166 : }
467 : :
468 : : /* Outputs number of extra bytes required for MAC and padding */
469 : 163955 : int pad_and_mac_len = 0;
470 [ - + ]: 163955 : POSIX_GUARD(cipher_suite->record_alg->cipher->io.comp.initial_hmac(session_key, sequence_number, content_type, conn->actual_protocol_version,
471 : 163955 : payload_and_eiv_len, &pad_and_mac_len));
472 : 163955 : extra += pad_and_mac_len;
473 : 163955 : }
474 : :
475 : : /* TLS 1.3 protected record occupies one extra byte for content type */
476 [ + + ]: 3603031 : if (is_tls13_record) {
477 : 108271 : extra += S2N_TLS_CONTENT_TYPE_LENGTH;
478 : 108271 : }
479 : :
480 : : /* Rewrite the length to be the actual fragment length */
481 : 3603031 : const uint16_t actual_fragment_length = data_bytes_to_take + padding + extra;
482 : : /* ensure actual_fragment_length + S2N_TLS_RECORD_HEADER_LENGTH <= max record length */
483 [ + + ]: 3603031 : const uint16_t max_record_length = is_tls13_record ? S2N_TLS13_MAXIMUM_RECORD_LENGTH : S2N_TLS_MAXIMUM_RECORD_LENGTH;
484 [ - + ][ # # ]: 3603031 : S2N_ERROR_IF(actual_fragment_length + S2N_TLS_RECORD_HEADER_LENGTH > max_record_length, S2N_ERR_RECORD_LENGTH_TOO_LARGE);
485 [ - + ]: 3603031 : POSIX_GUARD(s2n_stuffer_write_uint16(&record_stuffer, actual_fragment_length));
486 : :
487 : : /* If we're AEAD, write the sequence number as an IV, and generate the AAD */
488 [ + + ]: 3603031 : if (cipher_suite->record_alg->cipher->type == S2N_AEAD) {
489 : 3344510 : struct s2n_stuffer iv_stuffer = { 0 };
490 [ - + ]: 3344510 : POSIX_GUARD(s2n_blob_init(&iv, aad_iv, sizeof(aad_iv)));
491 [ - + ]: 3344510 : POSIX_GUARD(s2n_stuffer_init(&iv_stuffer, &iv));
492 : :
493 [ + + ]: 3344510 : if (cipher_suite->record_alg->flags & S2N_TLS12_AES_GCM_AEAD_NONCE) {
494 : : /* Partially explicit nonce. See RFC 5288 Section 3 */
495 [ - + ]: 2178395 : POSIX_GUARD(s2n_stuffer_write_bytes(&record_stuffer, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
496 [ - + ]: 2178395 : POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, implicit_iv, cipher_suite->record_alg->cipher->io.aead.fixed_iv_size));
497 [ - + ]: 2178395 : POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
498 [ + + ][ + - ]: 2178395 : } else if (cipher_suite->record_alg->flags & S2N_TLS12_CHACHA_POLY_AEAD_NONCE || is_tls13_record) {
499 : : /* Fully implicit nonce. See RFC7905 Section 2 */
500 : 1166115 : uint8_t four_zeroes[4] = { 0 };
501 [ - + ]: 1166115 : POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, four_zeroes, 4));
502 [ - + ]: 1166115 : POSIX_GUARD(s2n_stuffer_write_bytes(&iv_stuffer, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
503 [ + + ]: 15159495 : for (int i = 0; i < cipher_suite->record_alg->cipher->io.aead.fixed_iv_size; i++) {
504 : 13993380 : aad_iv[i] = aad_iv[i] ^ implicit_iv[i];
505 : 13993380 : }
506 : 1166115 : } else {
507 [ # # ]: 0 : POSIX_BAIL(S2N_ERR_INVALID_NONCE_TYPE);
508 : 0 : }
509 : :
510 : : /* Set the IV size to the amount of data written */
511 : 3344510 : iv.size = s2n_stuffer_data_available(&iv_stuffer);
512 [ + + ]: 3344510 : if (is_tls13_record) {
513 [ - + ]: 108271 : POSIX_GUARD_RESULT(s2n_tls13_aead_aad_init(data_bytes_to_take + S2N_TLS_CONTENT_TYPE_LENGTH, cipher_suite->record_alg->cipher->io.aead.tag_size, &aad));
514 : 3236239 : } else {
515 [ - + ]: 3236239 : POSIX_GUARD_RESULT(s2n_aead_aad_init(conn, sequence_number, content_type, data_bytes_to_take, &aad));
516 : 3236239 : }
517 [ + + ][ + + ]: 3344510 : } else if (cipher_suite->record_alg->cipher->type == S2N_CBC || cipher_suite->record_alg->cipher->type == S2N_COMPOSITE) {
518 [ - + ]: 205727 : POSIX_GUARD(s2n_blob_init(&iv, implicit_iv, block_size));
519 : :
520 : : /* For TLS1.1/1.2; write the IV with random data */
521 [ + + ]: 205727 : if (conn->actual_protocol_version > S2N_TLS10) {
522 [ - + ]: 164083 : POSIX_GUARD_RESULT(s2n_get_public_random_data(&iv));
523 [ + + ]: 164083 : if (cipher_suite->record_alg->cipher->type == S2N_COMPOSITE) {
524 : : /* Write a separate random block to the record. This will be used along with the previously generated
525 : : * iv blob to generate the final explicit_iv for this record.
526 : : *
527 : : * How? Openssl's AES-CBC stitched encrypt populates the first block of application data with:
528 : : * AES(Key, XOR(iv, initial_block))
529 : : *
530 : : * If we make initial_block a random block unrelated to random_iv, explicit IV for this record
531 : : * is random value based on the two random blobs we just generated:
532 : : * AES(Key, XOR(random_iv, explicit_iv_placeholder) == AES(Key, XOR(random_iv, random_iv2))
533 : : *
534 : : * NOTE: We can't use the same random IV blob as both the initial block and IV since it will result in:
535 : : * AES(Key, XOR(random_iv, random_iv)) == AES(Key, 0), which will be shared by all records in this session.
536 : : */
537 : 131166 : struct s2n_blob explicit_iv_placeholder = { 0 };
538 : 131166 : uint8_t zero_block[S2N_TLS_MAX_IV_LEN] = { 0 };
539 [ - + ]: 131166 : POSIX_GUARD(s2n_blob_init(&explicit_iv_placeholder, zero_block, block_size));
540 [ - + ]: 131166 : POSIX_GUARD_RESULT(s2n_get_public_random_data(&explicit_iv_placeholder));
541 [ - + ]: 131166 : POSIX_GUARD(s2n_stuffer_write(&record_stuffer, &explicit_iv_placeholder));
542 : 131166 : } else {
543 : : /* We can write the explicit IV directly to the record for non composite CBC because
544 : : * s2n starts AES *after* the explicit IV.
545 : : */
546 [ - + ]: 32917 : POSIX_GUARD(s2n_stuffer_write(&record_stuffer, &iv));
547 : 32917 : }
548 : 164083 : }
549 : 205727 : }
550 : :
551 : : /* Write the plaintext data */
552 [ - + ]: 3603031 : POSIX_GUARD(s2n_stuffer_writev_bytes(&record_stuffer, in, in_count, offs, data_bytes_to_take));
553 : 3603031 : void *orig_write_ptr = record_stuffer.blob.data + record_stuffer.write_cursor - data_bytes_to_take;
554 : :
555 : : /* Write the MAC */
556 : 3603031 : struct s2n_blob header_blob = { 0 };
557 [ - + ]: 3603031 : POSIX_GUARD(s2n_blob_slice(&record_blob, &header_blob, 0, S2N_TLS_RECORD_HEADER_LENGTH));
558 : 3603031 : struct s2n_blob plaintext_blob = { 0 };
559 [ - + ]: 3603031 : POSIX_GUARD(s2n_blob_init(&plaintext_blob, orig_write_ptr, data_bytes_to_take));
560 : 3603031 : uint32_t mac_digest_size = 0;
561 [ - + ]: 3603031 : POSIX_GUARD_RESULT(s2n_record_write_mac(conn, &header_blob, &plaintext_blob, &record_stuffer, &mac_digest_size));
562 : :
563 : : /* We are done with this sequence number, so we can increment it */
564 : 3603031 : struct s2n_blob seq = { 0 };
565 [ - + ]: 3603031 : POSIX_GUARD(s2n_blob_init(&seq, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
566 [ + + ]: 3603031 : POSIX_GUARD(s2n_increment_sequence_number(&seq));
567 : :
568 : : /* Write content type for TLS 1.3 record (RFC 8446 Section 5.2) */
569 [ + + ]: 3603030 : if (is_tls13_record) {
570 [ - + ]: 108271 : POSIX_GUARD(s2n_stuffer_write_uint8(&record_stuffer, content_type));
571 : 108271 : }
572 : :
573 [ + + ]: 3603030 : if (cipher_suite->record_alg->cipher->type == S2N_CBC) {
574 : : /* Include padding bytes, each with the value 'p', and
575 : : * include an extra padding length byte, also with the value 'p'.
576 : : */
577 [ + + ]: 362758 : for (int i = 0; i <= padding; i++) {
578 [ - + ]: 320986 : POSIX_GUARD(s2n_stuffer_write_uint8(&record_stuffer, padding));
579 : 320986 : }
580 : 41772 : }
581 : :
582 : : /* Rewind to rewrite/encrypt the packet */
583 [ - + ]: 3603030 : POSIX_GUARD(s2n_stuffer_rewrite(&record_stuffer));
584 : :
585 : : /* Skip the header */
586 [ - + ]: 3603030 : POSIX_GUARD(s2n_stuffer_skip_write(&record_stuffer, S2N_TLS_RECORD_HEADER_LENGTH));
587 : :
588 : 3603030 : uint16_t encrypted_length = data_bytes_to_take + mac_digest_size;
589 : 3603030 : switch (cipher_suite->record_alg->cipher->type) {
590 [ + + ]: 3344510 : case S2N_AEAD:
591 [ - + ]: 3344510 : POSIX_GUARD(s2n_stuffer_skip_write(&record_stuffer, cipher_suite->record_alg->cipher->io.aead.record_iv_size));
592 : 3344510 : encrypted_length += cipher_suite->record_alg->cipher->io.aead.tag_size;
593 [ + + ]: 3344510 : if (is_tls13_record) {
594 : : /* one extra byte for content type */
595 : 108271 : encrypted_length += S2N_TLS_CONTENT_TYPE_LENGTH;
596 : 108271 : }
597 : 3344510 : break;
598 [ + + ]: 41772 : case S2N_CBC:
599 [ + + ]: 41772 : if (conn->actual_protocol_version > S2N_TLS10) {
600 : : /* Leave the IV alone and unencrypted */
601 [ - + ]: 32917 : POSIX_GUARD(s2n_stuffer_skip_write(&record_stuffer, iv.size));
602 : 32917 : }
603 : : /* Encrypt the padding and the padding length byte too */
604 : 41772 : encrypted_length += padding + 1;
605 : 41772 : break;
606 [ + + ]: 163955 : case S2N_COMPOSITE:
607 : : /* Composite CBC expects a pointer starting at explicit IV: [Explicit IV | fragment | MAC | padding | padding len ]
608 : : * extra will account for the explicit IV len(if applicable), MAC digest len, padding len + padding byte.
609 : : */
610 : 163955 : encrypted_length += extra;
611 : 163955 : break;
612 [ + + ]: 52793 : default:
613 : 52793 : break;
614 : 3603030 : }
615 : :
616 : : /* Check that stuffer have enough space to write encrypted record, because raw_write cannot expand tainted stuffer */
617 [ - + ][ # # ]: 3603030 : S2N_ERROR_IF(s2n_stuffer_space_remaining(&record_stuffer) < encrypted_length, S2N_ERR_RECORD_STUFFER_SIZE);
618 : :
619 : : /* Do the encryption */
620 : 3603030 : struct s2n_blob en = { .size = encrypted_length, .data = s2n_stuffer_raw_write(&record_stuffer, encrypted_length) };
621 [ - + ]: 3603030 : POSIX_GUARD(s2n_record_encrypt(conn, cipher_suite, session_key, &iv, &aad, &en, implicit_iv, block_size));
622 : :
623 : : /* Sync the out stuffer write cursor with the record stuffer. */
624 [ - + ]: 3603030 : POSIX_GUARD(s2n_stuffer_skip_write(&conn->out, s2n_stuffer_data_available(&record_stuffer)));
625 : :
626 [ + + ][ + + ]: 3603030 : if (conn->actual_protocol_version == S2N_TLS13 && content_type == TLS_CHANGE_CIPHER_SPEC) {
627 : 6915 : conn->client = current_client_crypto;
628 : 6915 : conn->server = current_server_crypto;
629 : 6915 : }
630 : :
631 : 3603030 : return data_bytes_to_take;
632 : 3603030 : }
633 : :
634 : : S2N_RESULT s2n_record_write(struct s2n_connection *conn, uint8_t content_type, struct s2n_blob *in)
635 : 3392060 : {
636 : 3392060 : struct iovec iov;
637 : 3392060 : iov.iov_base = in->data;
638 : 3392060 : iov.iov_len = in->size;
639 : 3392060 : int written = s2n_record_writev(conn, content_type, &iov, 1, 0, in->size);
640 [ + + ]: 3392060 : RESULT_GUARD_POSIX(written);
641 [ + - ][ + + ]: 3392057 : RESULT_ENSURE((uint32_t) written == in->size, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
642 : 3392037 : return S2N_RESULT_OK;
643 : 3392057 : }
|