Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/s3_pkt.cc

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected])
2 // Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/ssl.h>
6
7 #include <assert.h>
8 #include <limits.h>
9 #include <string.h>
10
11 #include <algorithm>
12
13 #include <openssl/err.h>
14 #include <openssl/evp.h>
15 #include <openssl/mem.h>
16 #include <openssl/rand.h>
17
18 #include "../crypto/err/internal.h"
19 #include "../crypto/internal.h"
20 #include "internal.h"
21
22
23 BSSL_NAMESPACE_BEGIN
24
25 static int do_tls_write(SSL *ssl, size_t *out_bytes_written, uint8_t type,
26 Span<const uint8_t> in);
27
28 int tls_write_app_data(SSL *ssl, bool *out_needs_handshake,
29 size_t *out_bytes_written, Span<const uint8_t> in) {
30 assert(ssl_can_write(ssl));
31 assert(!ssl->s3->aead_write_ctx->is_null_cipher());
32
33 *out_needs_handshake = false;
34
35 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
36 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
37 return -1;
38 }
39
40 size_t total_bytes_written = ssl->s3->unreported_bytes_written;
41 if (in.size() < total_bytes_written) {
42 // This can happen if the caller disables |SSL_MODE_ENABLE_PARTIAL_WRITE|,
43 // asks us to write some input of length N, we successfully encrypt M bytes
44 // and write it, but fail to write the rest. We will report
45 // |SSL_ERROR_WANT_WRITE|. If the caller then retries with fewer than M
46 // bytes, we cannot satisfy that request. The caller is required to always
47 // retry with at least as many bytes as the previous attempt.
48 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
49 return -1;
50 }
51
52 in = in.subspan(total_bytes_written);
53
54 const bool is_early_data_write =
55 !ssl->server && SSL_in_early_data(ssl) && ssl->s3->hs->can_early_write;
56 for (;;) {
57 size_t max_send_fragment = ssl->max_send_fragment;
58 if (is_early_data_write) {
59 SSL_HANDSHAKE *hs = ssl->s3->hs.get();
60 if (hs->early_data_written >= hs->early_session->ticket_max_early_data) {
61 ssl->s3->unreported_bytes_written = total_bytes_written;
62 hs->can_early_write = false;
63 *out_needs_handshake = true;
64 return -1;
65 }
66 max_send_fragment = std::min(
67 max_send_fragment, size_t{hs->early_session->ticket_max_early_data -
68 hs->early_data_written});
69 }
70
71 const size_t to_write = std::min(max_send_fragment, in.size());
72 size_t bytes_written;
73 int ret = do_tls_write(ssl, &bytes_written, SSL3_RT_APPLICATION_DATA,
74 in.subspan(0, to_write));
75 if (ret <= 0) {
76 ssl->s3->unreported_bytes_written = total_bytes_written;
77 return ret;
78 }
79
80 // Note |bytes_written| may be less than |to_write| if there was a pending
81 // record from a smaller write attempt.
82 assert(bytes_written <= to_write);
83 total_bytes_written += bytes_written;
84 in = in.subspan(bytes_written);
85 if (is_early_data_write) {
86 ssl->s3->hs->early_data_written += bytes_written;
87 }
88
89 if (in.empty() || (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) {
90 ssl->s3->unreported_bytes_written = 0;
91 *out_bytes_written = total_bytes_written;
92 return 1;
93 }
94 }
95 }
96
97 // do_tls_write writes an SSL record of the given type. On success, it sets
98 // |*out_bytes_written| to number of bytes successfully written and returns one.
99 // On error, it returns a value <= 0 from the underlying |BIO|.
100 static int do_tls_write(SSL *ssl, size_t *out_bytes_written, uint8_t type,
101 Span<const uint8_t> in) {
102 // If there is a pending write, the retry must be consistent.
103 if (!ssl->s3->pending_write.empty() &&
104 (ssl->s3->pending_write.size() > in.size() ||
105 (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) &&
106 ssl->s3->pending_write.data() != in.data()) ||
107 ssl->s3->pending_write_type != type)) {
108 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
109 return -1;
110 }
111
112 // Flush any unwritten data to the transport. There may be data to flush even
113 // if |wpend_tot| is zero.
114 int ret = ssl_write_buffer_flush(ssl);
115 if (ret <= 0) {
116 return ret;
117 }
118
119 // If there is a pending write, we just completed it. Report it to the caller.
120 if (!ssl->s3->pending_write.empty()) {
121 *out_bytes_written = ssl->s3->pending_write.size();
122 ssl->s3->pending_write = {};
123 return 1;
124 }
125
126 SSLBuffer *buf = &ssl->s3->write_buffer;
127 if (in.size() > SSL3_RT_MAX_PLAIN_LENGTH || buf->size() > 0) {
128 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
129 return -1;
130 }
131
132 if (!tls_flush_pending_hs_data(ssl)) {
133 return -1;
134 }
135
136 // We may have unflushed handshake data that must be written before |in|. This
137 // may be a KeyUpdate acknowledgment, 0-RTT key change messages, or a
138 // NewSessionTicket.
139 Span<const uint8_t> pending_flight;
140 if (ssl->s3->pending_flight != nullptr) {
141 pending_flight = MakeConstSpan(
142 reinterpret_cast<const uint8_t *>(ssl->s3->pending_flight->data),
143 ssl->s3->pending_flight->length);
144 pending_flight = pending_flight.subspan(ssl->s3->pending_flight_offset);
145 }
146
147 size_t max_out = pending_flight.size();
148 if (!in.empty()) {
149 const size_t max_ciphertext_len = in.size() + SSL_max_seal_overhead(ssl);
150 if (max_ciphertext_len < in.size() ||
151 max_out + max_ciphertext_len < max_out) {
152 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
153 return -1;
154 }
155 max_out += max_ciphertext_len;
156 }
157
158 if (max_out == 0) {
159 // Nothing to write.
160 *out_bytes_written = 0;
161 return 1;
162 }
163
164 if (!buf->EnsureCap(pending_flight.size() + ssl_seal_align_prefix_len(ssl),
165 max_out)) {
166 return -1;
167 }
168
169 // Copy |pending_flight| to the output.
170 if (!pending_flight.empty()) {
171 OPENSSL_memcpy(buf->remaining().data(), pending_flight.data(),
172 pending_flight.size());
173 ssl->s3->pending_flight.reset();
174 ssl->s3->pending_flight_offset = 0;
175 buf->DidWrite(pending_flight.size());
176 }
177
178 if (!in.empty()) {
179 size_t ciphertext_len;
180 if (!tls_seal_record(ssl, buf->remaining().data(), &ciphertext_len,
181 buf->remaining().size(), type, in.data(), in.size())) {
182 return -1;
183 }
184 buf->DidWrite(ciphertext_len);
185 }
186
187 // Now that we've made progress on the connection, uncork KeyUpdate
188 // acknowledgments.
189 ssl->s3->key_update_pending = SSL_KEY_UPDATE_NONE;
190
191 // Flush the write buffer.
192 ret = ssl_write_buffer_flush(ssl);
193 if (ret <= 0) {
194 // Track the unfinished write.
195 if (!in.empty()) {
196 ssl->s3->pending_write = in;
197 ssl->s3->pending_write_type = type;
198 }
199 return ret;
200 }
201
202 *out_bytes_written = in.size();
203 return 1;
204 }
205
206 ssl_open_record_t tls_open_app_data(SSL *ssl, Span<uint8_t> *out,
207 size_t *out_consumed, uint8_t *out_alert,
208 Span<uint8_t> in) {
209 assert(ssl_can_read(ssl));
210 assert(!ssl->s3->aead_read_ctx->is_null_cipher());
211
212 uint8_t type;
213 Span<uint8_t> body;
214 auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in);
215 if (ret != ssl_open_record_success) {
216 return ret;
217 }
218
219 const bool is_early_data_read = ssl->server && SSL_in_early_data(ssl);
220
221 if (type == SSL3_RT_HANDSHAKE) {
222 // Post-handshake data prior to TLS 1.3 is always renegotiation, which we
223 // never accept as a server. Otherwise |tls_get_message| will send
224 // |SSL_R_EXCESSIVE_MESSAGE_SIZE|.
225 if (ssl->server && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
226 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
227 *out_alert = SSL_AD_NO_RENEGOTIATION;
228 return ssl_open_record_error;
229 }
230
231 if (!tls_append_handshake_data(ssl, body)) {
232 *out_alert = SSL_AD_INTERNAL_ERROR;
233 return ssl_open_record_error;
234 }
235 return ssl_open_record_discard;
236 }
237
238 if (type != SSL3_RT_APPLICATION_DATA) {
239 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
240 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
241 return ssl_open_record_error;
242 }
243
244 if (is_early_data_read) {
245 if (body.size() > kMaxEarlyDataAccepted - ssl->s3->hs->early_data_read) {
246 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_READ_EARLY_DATA);
247 *out_alert = SSL3_AD_UNEXPECTED_MESSAGE;
248 return ssl_open_record_error;
249 }
250
251 ssl->s3->hs->early_data_read += body.size();
252 }
253
254 if (body.empty()) {
255 return ssl_open_record_discard;
256 }
257
258 *out = body;
259 return ssl_open_record_success;
260 }
261
262 ssl_open_record_t tls_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
263 uint8_t *out_alert,
264 Span<uint8_t> in) {
265 uint8_t type;
266 Span<uint8_t> body;
267 auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in);
268 if (ret != ssl_open_record_success) {
269 return ret;
270 }
271
272 if (type != SSL3_RT_CHANGE_CIPHER_SPEC) {
273 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
274 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
275 return ssl_open_record_error;
276 }
277
278 if (body.size() != 1 || body[0] != SSL3_MT_CCS) {
279 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
280 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
281 return ssl_open_record_error;
282 }
283
284 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, body);
285 return ssl_open_record_success;
286 }
287
288 void ssl_send_alert(SSL *ssl, int level, int desc) {
289 // This function is called in response to a fatal error from the peer. Ignore
290 // any failures writing the alert and report only the original error. In
291 // particular, if the transport uses |SSL_write|, our existing error will be
292 // clobbered so we must save and restore the error queue. See
293 // https://crbug.com/959305.
294 //
295 // TODO(davidben): Return the alert out of the handshake, rather than calling
296 // this function internally everywhere.
297 //
298 // TODO(davidben): This does not allow retrying if the alert hit EAGAIN. See
299 // https://crbug.com/boringssl/130.
300 UniquePtr<ERR_SAVE_STATE> err_state(ERR_save_state());
301 ssl_send_alert_impl(ssl, level, desc);
302 ERR_restore_state(err_state.get());
303 }
304
305 int ssl_send_alert_impl(SSL *ssl, int level, int desc) {
306 // It is illegal to send an alert when we've already sent a closing one.
307 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
308 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
309 return -1;
310 }
311
312 if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) {
313 ssl->s3->write_shutdown = ssl_shutdown_close_notify;
314 } else {
315 assert(level == SSL3_AL_FATAL);
316 assert(desc != SSL_AD_CLOSE_NOTIFY);
317 ssl->s3->write_shutdown = ssl_shutdown_error;
318 }
319
320 ssl->s3->alert_dispatch = true;
321 ssl->s3->send_alert[0] = level;
322 ssl->s3->send_alert[1] = desc;
323 if (ssl->s3->write_buffer.empty()) {
324 // Nothing is being written out, so the alert may be dispatched
325 // immediately.
326 return ssl->method->dispatch_alert(ssl);
327 }
328
329 // The alert will be dispatched later.
330 return -1;
331 }
332
333 int tls_dispatch_alert(SSL *ssl) {
334 if (ssl->quic_method) {
335 if (!ssl->quic_method->send_alert(ssl, ssl->s3->write_level,
336 ssl->s3->send_alert[1])) {
337 OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR);
338 return 0;
339 }
340 } else {
341 size_t bytes_written;
342 int ret =
343 do_tls_write(ssl, &bytes_written, SSL3_RT_ALERT, ssl->s3->send_alert);
344 if (ret <= 0) {
345 return ret;
346 }
347 assert(bytes_written == 2);
348 }
349
350 ssl->s3->alert_dispatch = false;
351
352 // If the alert is fatal, flush the BIO now.
353 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
354 BIO_flush(ssl->wbio.get());
355 }
356
357 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert);
358
359 int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
360 ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
361
362 return 1;
363 }
364
365 BSSL_NAMESPACE_END