]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
fecb3aae | 2 | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
c51ae173 | 3 | * |
2c18d164 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
c51ae173 | 8 | */ |
d02b48c6 RE |
9 | |
10 | #include <stdio.h> | |
339da43d | 11 | #include <limits.h> |
d02b48c6 | 12 | #include <errno.h> |
706457b7 | 13 | #include "../ssl_local.h" |
ec577822 BM |
14 | #include <openssl/evp.h> |
15 | #include <openssl/buffer.h> | |
637f374a | 16 | #include <openssl/rand.h> |
706457b7 | 17 | #include "record_local.h" |
0d345f0e | 18 | #include "internal/packet.h" |
d02b48c6 | 19 | |
0f113f3e MC |
20 | #if defined(OPENSSL_SMALL_FOOTPRINT) || \ |
21 | !( defined(AES_ASM) && ( \ | |
22 | defined(__x86_64) || defined(__x86_64__) || \ | |
fbaf30d0 | 23 | defined(_M_AMD64) || defined(_M_X64) ) \ |
0f113f3e | 24 | ) |
a9c6edcd AP |
25 | # undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK |
26 | # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0 | |
27 | #endif | |
28 | ||
38b051a1 | 29 | void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s) |
c036e210 MC |
30 | { |
31 | rl->s = s; | |
78fcddbb | 32 | RECORD_LAYER_set_first_record(&s->rlayer); |
94777c9c | 33 | SSL3_RECORD_clear(rl->rrec, SSL_MAX_PIPELINES); |
c036e210 MC |
34 | } |
35 | ||
af9752e5 MC |
36 | void RECORD_LAYER_clear(RECORD_LAYER *rl) |
37 | { | |
6b41b3f5 | 38 | rl->rstate = SSL_ST_READ_HEADER; |
af9752e5 | 39 | |
a230b26e EK |
40 | /* |
41 | * Do I need to clear read_ahead? As far as I can tell read_ahead did not | |
af9752e5 MC |
42 | * previously get reset by SSL_clear...so I'll keep it that way..but is |
43 | * that right? | |
44 | */ | |
6b41b3f5 MC |
45 | |
46 | rl->packet = NULL; | |
47 | rl->packet_length = 0; | |
48 | rl->wnum = 0; | |
6b41b3f5 MC |
49 | memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment)); |
50 | rl->handshake_fragment_len = 0; | |
51 | rl->wpend_tot = 0; | |
52 | rl->wpend_type = 0; | |
53 | rl->wpend_ret = 0; | |
54 | rl->wpend_buf = NULL; | |
55 | ||
56 | SSL3_BUFFER_clear(&rl->rbuf); | |
4bf08600 | 57 | ssl3_release_write_buffer(rl->s); |
0aac3a6b | 58 | rl->numrpipes = 0; |
94777c9c | 59 | SSL3_RECORD_clear(rl->rrec, SSL_MAX_PIPELINES); |
6b41b3f5 | 60 | |
95cdad63 MC |
61 | RECORD_LAYER_reset_read_sequence(rl); |
62 | RECORD_LAYER_reset_write_sequence(rl); | |
0485d540 | 63 | |
6b41b3f5 | 64 | if (rl->d) |
5fb6f80c | 65 | DTLS_RECORD_LAYER_clear(rl); |
af9752e5 MC |
66 | } |
67 | ||
f161995e MC |
68 | void RECORD_LAYER_release(RECORD_LAYER *rl) |
69 | { | |
70 | if (SSL3_BUFFER_is_initialised(&rl->rbuf)) | |
71 | ssl3_release_read_buffer(rl->s); | |
d102d9df | 72 | if (rl->numwpipes > 0) |
f161995e | 73 | ssl3_release_write_buffer(rl->s); |
94777c9c | 74 | SSL3_RECORD_release(rl->rrec, SSL_MAX_PIPELINES); |
f161995e MC |
75 | } |
76 | ||
b8c49611 | 77 | /* Checks if we have unprocessed read ahead data pending */ |
49580f25 | 78 | int RECORD_LAYER_read_pending(const RECORD_LAYER *rl) |
f161995e MC |
79 | { |
80 | return SSL3_BUFFER_get_left(&rl->rbuf) != 0; | |
81 | } | |
82 | ||
b8c49611 MC |
83 | /* Checks if we have decrypted unread record data pending */ |
84 | int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl) | |
85 | { | |
86 | size_t curr_rec = 0, num_recs = RECORD_LAYER_get_numrpipes(rl); | |
87 | const SSL3_RECORD *rr = rl->rrec; | |
88 | ||
89 | while (curr_rec < num_recs && SSL3_RECORD_is_read(&rr[curr_rec])) | |
90 | curr_rec++; | |
91 | ||
92 | return curr_rec < num_recs; | |
93 | } | |
94 | ||
49580f25 | 95 | int RECORD_LAYER_write_pending(const RECORD_LAYER *rl) |
f161995e | 96 | { |
d102d9df | 97 | return (rl->numwpipes > 0) |
a230b26e | 98 | && SSL3_BUFFER_get_left(&rl->wbuf[rl->numwpipes - 1]) != 0; |
f161995e MC |
99 | } |
100 | ||
de07f311 MC |
101 | void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl) |
102 | { | |
95cdad63 | 103 | memset(rl->read_sequence, 0, sizeof(rl->read_sequence)); |
de07f311 MC |
104 | } |
105 | ||
106 | void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl) | |
107 | { | |
95cdad63 | 108 | memset(rl->write_sequence, 0, sizeof(rl->write_sequence)); |
de07f311 MC |
109 | } |
110 | ||
8b0e934a | 111 | size_t ssl3_pending(const SSL *s) |
d5a25ae0 | 112 | { |
8b0e934a | 113 | size_t i, num = 0; |
38b051a1 | 114 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
94777c9c | 115 | |
38b051a1 | 116 | if (sc == NULL) |
d5a25ae0 MC |
117 | return 0; |
118 | ||
38b051a1 TM |
119 | if (sc->rlayer.rstate == SSL_ST_READ_BODY) |
120 | return 0; | |
121 | ||
6d6b295a MC |
122 | /* Take into account DTLS buffered app data */ |
123 | if (SSL_CONNECTION_IS_DTLS(sc)) { | |
124 | DTLS1_RECORD_DATA *rdata; | |
125 | pitem *item, *iter; | |
126 | ||
127 | iter = pqueue_iterator(sc->rlayer.d->buffered_app_data.q); | |
128 | while ((item = pqueue_next(&iter)) != NULL) { | |
129 | rdata = item->data; | |
130 | num += rdata->rrec.length; | |
131 | } | |
132 | } | |
133 | ||
38b051a1 TM |
134 | for (i = 0; i < RECORD_LAYER_get_numrpipes(&sc->rlayer); i++) { |
135 | if (SSL3_RECORD_get_type(&sc->rlayer.rrec[i]) | |
a230b26e | 136 | != SSL3_RT_APPLICATION_DATA) |
6d6b295a | 137 | return num; |
38b051a1 | 138 | num += SSL3_RECORD_get_length(&sc->rlayer.rrec[i]); |
94777c9c MC |
139 | } |
140 | ||
141 | return num; | |
d5a25ae0 MC |
142 | } |
143 | ||
dad78fb1 MC |
144 | void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len) |
145 | { | |
146 | ctx->default_read_buf_len = len; | |
147 | } | |
148 | ||
149 | void SSL_set_default_read_buffer_len(SSL *s, size_t len) | |
150 | { | |
38b051a1 TM |
151 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
152 | ||
153 | if (sc == NULL) | |
154 | return; | |
e2d5742b | 155 | SSL3_BUFFER_set_default_len(sc->rrlmethod->get0_rbuf(sc->rrl), len); |
dad78fb1 MC |
156 | } |
157 | ||
295c3f41 MC |
158 | const char *SSL_rstate_string_long(const SSL *s) |
159 | { | |
38b051a1 TM |
160 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
161 | ||
162 | if (sc == NULL) | |
163 | return NULL; | |
164 | ||
165 | switch (sc->rlayer.rstate) { | |
295c3f41 | 166 | case SSL_ST_READ_HEADER: |
475965f2 | 167 | return "read header"; |
295c3f41 | 168 | case SSL_ST_READ_BODY: |
475965f2 | 169 | return "read body"; |
295c3f41 | 170 | case SSL_ST_READ_DONE: |
475965f2 | 171 | return "read done"; |
295c3f41 | 172 | default: |
475965f2 | 173 | return "unknown"; |
295c3f41 | 174 | } |
295c3f41 MC |
175 | } |
176 | ||
177 | const char *SSL_rstate_string(const SSL *s) | |
178 | { | |
38b051a1 TM |
179 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
180 | ||
181 | if (sc == NULL) | |
182 | return NULL; | |
183 | ||
184 | switch (sc->rlayer.rstate) { | |
295c3f41 | 185 | case SSL_ST_READ_HEADER: |
475965f2 | 186 | return "RH"; |
295c3f41 | 187 | case SSL_ST_READ_BODY: |
475965f2 | 188 | return "RB"; |
295c3f41 | 189 | case SSL_ST_READ_DONE: |
475965f2 | 190 | return "RD"; |
295c3f41 | 191 | default: |
475965f2 | 192 | return "unknown"; |
295c3f41 | 193 | } |
295c3f41 MC |
194 | } |
195 | ||
0f113f3e | 196 | |
0f113f3e MC |
197 | /* |
198 | * Call this to write data in records of type 'type' It will return <= 0 if | |
199 | * not all data has been sent or non-blocking IO. | |
d02b48c6 | 200 | */ |
38b051a1 | 201 | int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, size_t len, |
7ee8627f | 202 | size_t *written) |
0f113f3e MC |
203 | { |
204 | const unsigned char *buf = buf_; | |
7ee8627f | 205 | size_t tot; |
cf72c757 | 206 | size_t n, max_send_fragment, split_send_fragment, maxpipes; |
f1f7598c | 207 | #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK |
cf72c757 | 208 | size_t nw; |
f1f7598c | 209 | #endif |
38b051a1 | 210 | SSL3_BUFFER *wb; |
0f113f3e | 211 | int i; |
7ee8627f | 212 | size_t tmpwrit; |
38b051a1 | 213 | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); |
0f113f3e | 214 | |
38b051a1 TM |
215 | if (s == NULL) |
216 | return -1; | |
217 | ||
218 | wb = &s->rlayer.wbuf[0]; | |
0f113f3e | 219 | s->rwstate = SSL_NOTHING; |
e2228d31 | 220 | tot = s->rlayer.wnum; |
0f113f3e MC |
221 | /* |
222 | * ensure that if we end up with a smaller value of data to write out | |
3519bae5 | 223 | * than the original len from a write which didn't complete for |
0f113f3e MC |
224 | * non-blocking I/O and also somehow ended up avoiding the check for |
225 | * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be | |
226 | * possible to end up with (len-tot) as a large number that will then | |
227 | * promptly send beyond the end of the users buffer ... so we trap and | |
228 | * report the error in a way the user will notice | |
229 | */ | |
bd91e3c8 | 230 | if ((len < s->rlayer.wnum) |
cbbe9186 | 231 | || ((wb->left != 0) && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) { |
c48ffbcc | 232 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH); |
1c2e5d56 MC |
233 | return -1; |
234 | } | |
235 | ||
7daf7156 | 236 | if (s->early_data_state == SSL_EARLY_DATA_WRITING |
38b051a1 | 237 | && !ossl_early_data_count_ok(s, len, 0, 1)) { |
196f2cbb | 238 | /* SSLfatal() already called */ |
7daf7156 | 239 | return -1; |
196f2cbb | 240 | } |
7daf7156 | 241 | |
1c2e5d56 MC |
242 | s->rlayer.wnum = 0; |
243 | ||
feb9e31c | 244 | /* |
3bfacb5f BK |
245 | * If we are supposed to be sending a KeyUpdate or NewSessionTicket then go |
246 | * into init unless we have writes pending - in which case we should finish | |
247 | * doing that first. | |
feb9e31c | 248 | */ |
3bfacb5f BK |
249 | if (wb->left == 0 && (s->key_update != SSL_KEY_UPDATE_NONE |
250 | || s->ext.extra_tickets_expected > 0)) | |
feb9e31c MC |
251 | ossl_statem_set_in_init(s, 1); |
252 | ||
59cebcf9 MC |
253 | /* |
254 | * When writing early data on the server side we could be "in_init" in | |
255 | * between receiving the EoED and the CF - but we don't want to handle those | |
256 | * messages yet. | |
257 | */ | |
38b051a1 | 258 | if (SSL_in_init(ssl) && !ossl_statem_get_in_handshake(s) |
59cebcf9 | 259 | && s->early_data_state != SSL_EARLY_DATA_UNAUTH_WRITING) { |
38b051a1 | 260 | i = s->handshake_func(ssl); |
c2853382 | 261 | /* SSLfatal() already called */ |
1c2e5d56 | 262 | if (i < 0) |
7ee8627f | 263 | return i; |
1c2e5d56 | 264 | if (i == 0) { |
1c2e5d56 MC |
265 | return -1; |
266 | } | |
0f113f3e MC |
267 | } |
268 | ||
269 | /* | |
270 | * first check if there is a SSL3_BUFFER still being written out. This | |
271 | * will happen with non blocking IO | |
272 | */ | |
273 | if (wb->left != 0) { | |
c2853382 | 274 | /* SSLfatal() already called if appropriate */ |
7ee8627f MC |
275 | i = ssl3_write_pending(s, type, &buf[tot], s->rlayer.wpend_tot, |
276 | &tmpwrit); | |
0f113f3e MC |
277 | if (i <= 0) { |
278 | /* XXX should we ssl3_release_write_buffer if i<0? */ | |
e2228d31 | 279 | s->rlayer.wnum = tot; |
0f113f3e MC |
280 | return i; |
281 | } | |
7ee8627f | 282 | tot += tmpwrit; /* this might be last fragment */ |
0f113f3e | 283 | } |
a9c6edcd | 284 | #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK |
0f113f3e MC |
285 | /* |
286 | * Depending on platform multi-block can deliver several *times* | |
287 | * better performance. Downside is that it has to allocate | |
8483a003 | 288 | * jumbo buffer to accommodate up to 8 records, but the |
0f113f3e MC |
289 | * compromise is considered worthy. |
290 | */ | |
f6c95e46 RS |
291 | if (type == SSL3_RT_APPLICATION_DATA |
292 | && len >= 4 * (max_send_fragment = ssl_get_max_send_fragment(s)) | |
293 | && s->compress == NULL | |
294 | && s->msg_callback == NULL | |
295 | && !SSL_WRITE_ETM(s) | |
296 | && SSL_USE_EXPLICIT_IV(s) | |
ad2f4cdc | 297 | && !BIO_get_ktls_send(s->wbio) |
ed576acd | 298 | && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx)) |
f6c95e46 | 299 | & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) != 0) { |
0f113f3e MC |
300 | unsigned char aad[13]; |
301 | EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param; | |
8e6d03ca | 302 | size_t packlen; |
e3c9727f | 303 | int packleni; |
0f113f3e MC |
304 | |
305 | /* minimize address aliasing conflicts */ | |
306 | if ((max_send_fragment & 0xfff) == 0) | |
307 | max_send_fragment -= 512; | |
308 | ||
309 | if (tot == 0 || wb->buf == NULL) { /* allocate jumbo buffer */ | |
310 | ssl3_release_write_buffer(s); | |
311 | ||
312 | packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx, | |
313 | EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE, | |
8b0e934a | 314 | (int)max_send_fragment, NULL); |
0f113f3e | 315 | |
7ee8627f | 316 | if (len >= 8 * max_send_fragment) |
0f113f3e MC |
317 | packlen *= 8; |
318 | else | |
319 | packlen *= 4; | |
320 | ||
58c27c20 | 321 | if (!ssl3_setup_write_buffer(s, 1, packlen)) { |
c2853382 | 322 | /* SSLfatal() already called */ |
918bb865 MC |
323 | return -1; |
324 | } | |
0f113f3e | 325 | } else if (tot == len) { /* done? */ |
58c27c20 MC |
326 | /* free jumbo buffer */ |
327 | ssl3_release_write_buffer(s); | |
8b0e934a MC |
328 | *written = tot; |
329 | return 1; | |
0f113f3e MC |
330 | } |
331 | ||
332 | n = (len - tot); | |
333 | for (;;) { | |
334 | if (n < 4 * max_send_fragment) { | |
58c27c20 MC |
335 | /* free jumbo buffer */ |
336 | ssl3_release_write_buffer(s); | |
0f113f3e MC |
337 | break; |
338 | } | |
339 | ||
555cbb32 | 340 | if (s->s3.alert_dispatch) { |
38b051a1 | 341 | i = ssl->method->ssl_dispatch_alert(ssl); |
0f113f3e | 342 | if (i <= 0) { |
c2853382 | 343 | /* SSLfatal() already called if appropriate */ |
e2228d31 | 344 | s->rlayer.wnum = tot; |
0f113f3e MC |
345 | return i; |
346 | } | |
347 | } | |
348 | ||
349 | if (n >= 8 * max_send_fragment) | |
350 | nw = max_send_fragment * (mb_param.interleave = 8); | |
351 | else | |
352 | nw = max_send_fragment * (mb_param.interleave = 4); | |
353 | ||
de07f311 | 354 | memcpy(aad, s->rlayer.write_sequence, 8); |
0f113f3e MC |
355 | aad[8] = type; |
356 | aad[9] = (unsigned char)(s->version >> 8); | |
357 | aad[10] = (unsigned char)(s->version); | |
358 | aad[11] = 0; | |
359 | aad[12] = 0; | |
360 | mb_param.out = NULL; | |
361 | mb_param.inp = aad; | |
362 | mb_param.len = nw; | |
363 | ||
e3c9727f | 364 | packleni = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx, |
0f113f3e MC |
365 | EVP_CTRL_TLS1_1_MULTIBLOCK_AAD, |
366 | sizeof(mb_param), &mb_param); | |
e3c9727f MC |
367 | packlen = (size_t)packleni; |
368 | if (packleni <= 0 || packlen > wb->len) { /* never happens */ | |
58c27c20 MC |
369 | /* free jumbo buffer */ |
370 | ssl3_release_write_buffer(s); | |
0f113f3e MC |
371 | break; |
372 | } | |
373 | ||
374 | mb_param.out = wb->buf; | |
375 | mb_param.inp = &buf[tot]; | |
376 | mb_param.len = nw; | |
377 | ||
378 | if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx, | |
379 | EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT, | |
380 | sizeof(mb_param), &mb_param) <= 0) | |
381 | return -1; | |
382 | ||
de07f311 MC |
383 | s->rlayer.write_sequence[7] += mb_param.interleave; |
384 | if (s->rlayer.write_sequence[7] < mb_param.interleave) { | |
0f113f3e | 385 | int j = 6; |
de07f311 | 386 | while (j >= 0 && (++s->rlayer.write_sequence[j--]) == 0) ; |
0f113f3e MC |
387 | } |
388 | ||
389 | wb->offset = 0; | |
390 | wb->left = packlen; | |
391 | ||
f8caa3c8 MC |
392 | s->rlayer.wpend_tot = nw; |
393 | s->rlayer.wpend_buf = &buf[tot]; | |
394 | s->rlayer.wpend_type = type; | |
395 | s->rlayer.wpend_ret = nw; | |
0f113f3e | 396 | |
7ee8627f | 397 | i = ssl3_write_pending(s, type, &buf[tot], nw, &tmpwrit); |
0f113f3e | 398 | if (i <= 0) { |
c2853382 | 399 | /* SSLfatal() already called if appropriate */ |
1d2a18dc | 400 | if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) { |
58c27c20 MC |
401 | /* free jumbo buffer */ |
402 | ssl3_release_write_buffer(s); | |
0f113f3e | 403 | } |
e2228d31 | 404 | s->rlayer.wnum = tot; |
0f113f3e MC |
405 | return i; |
406 | } | |
7ee8627f | 407 | if (tmpwrit == n) { |
58c27c20 MC |
408 | /* free jumbo buffer */ |
409 | ssl3_release_write_buffer(s); | |
7ee8627f MC |
410 | *written = tot + tmpwrit; |
411 | return 1; | |
0f113f3e | 412 | } |
7ee8627f MC |
413 | n -= tmpwrit; |
414 | tot += tmpwrit; | |
0f113f3e MC |
415 | } |
416 | } else | |
cf72c757 | 417 | #endif /* !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK */ |
0f113f3e | 418 | if (tot == len) { /* done? */ |
38b051a1 | 419 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_CONNECTION_IS_DTLS(s)) |
0f113f3e MC |
420 | ssl3_release_write_buffer(s); |
421 | ||
7ee8627f MC |
422 | *written = tot; |
423 | return 1; | |
0f113f3e MC |
424 | } |
425 | ||
426 | n = (len - tot); | |
d102d9df | 427 | |
cf72c757 F |
428 | max_send_fragment = ssl_get_max_send_fragment(s); |
429 | split_send_fragment = ssl_get_split_send_fragment(s); | |
d102d9df MC |
430 | /* |
431 | * If max_pipelines is 0 then this means "undefined" and we default to | |
8483a003 | 432 | * 1 pipeline. Similarly if the cipher does not support pipelined |
d102d9df MC |
433 | * processing then we also only use 1 pipeline, or if we're not using |
434 | * explicit IVs | |
435 | */ | |
436 | maxpipes = s->max_pipelines; | |
437 | if (maxpipes > SSL_MAX_PIPELINES) { | |
438 | /* | |
439 | * We should have prevented this when we set max_pipelines so we | |
440 | * shouldn't get here | |
a230b26e | 441 | */ |
c48ffbcc | 442 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
d102d9df MC |
443 | return -1; |
444 | } | |
445 | if (maxpipes == 0 | |
a230b26e | 446 | || s->enc_write_ctx == NULL |
ed576acd | 447 | || (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx)) |
f6c95e46 | 448 | & EVP_CIPH_FLAG_PIPELINE) == 0 |
a230b26e | 449 | || !SSL_USE_EXPLICIT_IV(s)) |
d102d9df | 450 | maxpipes = 1; |
f6c95e46 RS |
451 | if (max_send_fragment == 0 |
452 | || split_send_fragment == 0 | |
453 | || split_send_fragment > max_send_fragment) { | |
d102d9df | 454 | /* |
cf72c757 | 455 | * We should have prevented this when we set/get the split and max send |
d102d9df | 456 | * fragments so we shouldn't get here |
a230b26e | 457 | */ |
c48ffbcc | 458 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
d102d9df MC |
459 | return -1; |
460 | } | |
461 | ||
0f113f3e | 462 | for (;;) { |
7ee8627f MC |
463 | size_t pipelens[SSL_MAX_PIPELINES], tmppipelen, remain; |
464 | size_t numpipes, j; | |
d102d9df MC |
465 | |
466 | if (n == 0) | |
467 | numpipes = 1; | |
0f113f3e | 468 | else |
d102d9df MC |
469 | numpipes = ((n - 1) / split_send_fragment) + 1; |
470 | if (numpipes > maxpipes) | |
471 | numpipes = maxpipes; | |
472 | ||
cf72c757 | 473 | if (n / numpipes >= max_send_fragment) { |
d102d9df MC |
474 | /* |
475 | * We have enough data to completely fill all available | |
476 | * pipelines | |
477 | */ | |
478 | for (j = 0; j < numpipes; j++) { | |
cf72c757 | 479 | pipelens[j] = max_send_fragment; |
d102d9df MC |
480 | } |
481 | } else { | |
482 | /* We can partially fill all available pipelines */ | |
483 | tmppipelen = n / numpipes; | |
484 | remain = n % numpipes; | |
485 | for (j = 0; j < numpipes; j++) { | |
486 | pipelens[j] = tmppipelen; | |
487 | if (j < remain) | |
488 | pipelens[j]++; | |
489 | } | |
490 | } | |
0f113f3e | 491 | |
7ee8627f MC |
492 | i = do_ssl3_write(s, type, &(buf[tot]), pipelens, numpipes, 0, |
493 | &tmpwrit); | |
0f113f3e | 494 | if (i <= 0) { |
c2853382 | 495 | /* SSLfatal() already called if appropriate */ |
0f113f3e | 496 | /* XXX should we ssl3_release_write_buffer if i<0? */ |
e2228d31 | 497 | s->rlayer.wnum = tot; |
0f113f3e MC |
498 | return i; |
499 | } | |
500 | ||
ff04799d | 501 | if (tmpwrit == n || |
0f113f3e MC |
502 | (type == SSL3_RT_APPLICATION_DATA && |
503 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) { | |
504 | /* | |
505 | * next chunk of data should get another prepended empty fragment | |
506 | * in ciphersuites with known-IV weakness: | |
507 | */ | |
555cbb32 | 508 | s->s3.empty_fragment_done = 0; |
0f113f3e | 509 | |
8bbf63e4 MC |
510 | if (tmpwrit == n |
511 | && (s->mode & SSL_MODE_RELEASE_BUFFERS) != 0 | |
38b051a1 | 512 | && !SSL_CONNECTION_IS_DTLS(s)) |
0f113f3e MC |
513 | ssl3_release_write_buffer(s); |
514 | ||
7ee8627f MC |
515 | *written = tot + tmpwrit; |
516 | return 1; | |
0f113f3e MC |
517 | } |
518 | ||
7ee8627f MC |
519 | n -= tmpwrit; |
520 | tot += tmpwrit; | |
0f113f3e MC |
521 | } |
522 | } | |
d02b48c6 | 523 | |
38b051a1 | 524 | int do_ssl3_write(SSL_CONNECTION *s, int type, const unsigned char *buf, |
7ee8627f MC |
525 | size_t *pipelens, size_t numpipes, |
526 | int create_empty_fragment, size_t *written) | |
0f113f3e | 527 | { |
c7c42022 | 528 | WPACKET pkt[SSL_MAX_PIPELINES]; |
d102d9df | 529 | SSL3_RECORD wr[SSL_MAX_PIPELINES]; |
44e58f3b MC |
530 | WPACKET *thispkt; |
531 | SSL3_RECORD *thiswr; | |
c7c42022 | 532 | unsigned char *recordstart; |
0f113f3e | 533 | int i, mac_size, clear = 0; |
7ee8627f | 534 | size_t prefix_len = 0; |
829754a6 | 535 | int eivlen = 0; |
f4bd5de5 | 536 | size_t align = 0; |
d102d9df | 537 | SSL3_BUFFER *wb; |
0f113f3e | 538 | SSL_SESSION *sess; |
c7c42022 | 539 | size_t totlen = 0, len, wpinited = 0; |
7ee8627f | 540 | size_t j; |
ad2f4cdc | 541 | int using_ktls; |
38b051a1 | 542 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
0f113f3e | 543 | |
d102d9df MC |
544 | for (j = 0; j < numpipes; j++) |
545 | totlen += pipelens[j]; | |
0f113f3e MC |
546 | /* |
547 | * first check if there is a SSL3_BUFFER still being written out. This | |
548 | * will happen with non blocking IO | |
549 | */ | |
c2853382 MC |
550 | if (RECORD_LAYER_write_pending(&s->rlayer)) { |
551 | /* Calls SSLfatal() as required */ | |
7ee8627f | 552 | return ssl3_write_pending(s, type, buf, totlen, written); |
c2853382 | 553 | } |
0f113f3e MC |
554 | |
555 | /* If we have an alert to send, lets send it */ | |
555cbb32 | 556 | if (s->s3.alert_dispatch) { |
38b051a1 | 557 | i = ssl->method->ssl_dispatch_alert(ssl); |
c2853382 MC |
558 | if (i <= 0) { |
559 | /* SSLfatal() already called if appropriate */ | |
26a7d938 | 560 | return i; |
c2853382 | 561 | } |
0f113f3e MC |
562 | /* if it went, fall through and send more stuff */ |
563 | } | |
564 | ||
c2853382 MC |
565 | if (s->rlayer.numwpipes < numpipes) { |
566 | if (!ssl3_setup_write_buffer(s, numpipes, 0)) { | |
567 | /* SSLfatal() already called */ | |
0f113f3e | 568 | return -1; |
c2853382 MC |
569 | } |
570 | } | |
0f113f3e | 571 | |
d102d9df | 572 | if (totlen == 0 && !create_empty_fragment) |
0f113f3e MC |
573 | return 0; |
574 | ||
0f113f3e MC |
575 | sess = s->session; |
576 | ||
f6c95e46 RS |
577 | if ((sess == NULL) |
578 | || (s->enc_write_ctx == NULL) | |
579 | || (EVP_MD_CTX_get0_md(s->write_hash) == NULL)) { | |
0f113f3e | 580 | clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */ |
0f113f3e MC |
581 | mac_size = 0; |
582 | } else { | |
ed576acd | 583 | mac_size = EVP_MD_CTX_get_size(s->write_hash); |
c2853382 | 584 | if (mac_size < 0) { |
c48ffbcc | 585 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
0f113f3e | 586 | goto err; |
c2853382 | 587 | } |
0f113f3e | 588 | } |
d02b48c6 | 589 | |
0f113f3e MC |
590 | /* |
591 | * 'create_empty_fragment' is true only when this function calls itself | |
592 | */ | |
555cbb32 | 593 | if (!clear && !create_empty_fragment && !s->s3.empty_fragment_done) { |
0f113f3e MC |
594 | /* |
595 | * countermeasure against known-IV weakness in CBC ciphersuites (see | |
596 | * http://www.openssl.org/~bodo/tls-cbc.txt) | |
597 | */ | |
598 | ||
555cbb32 | 599 | if (s->s3.need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) { |
0f113f3e MC |
600 | /* |
601 | * recursive function call with 'create_empty_fragment' set; this | |
602 | * prepares and buffers the data for an empty fragment (these | |
603 | * 'prefix_len' bytes are sent out later together with the actual | |
604 | * payload) | |
605 | */ | |
7ee8627f MC |
606 | size_t tmppipelen = 0; |
607 | int ret; | |
d102d9df | 608 | |
7ee8627f | 609 | ret = do_ssl3_write(s, type, buf, &tmppipelen, 1, 1, &prefix_len); |
c2853382 MC |
610 | if (ret <= 0) { |
611 | /* SSLfatal() already called if appropriate */ | |
0f113f3e | 612 | goto err; |
c2853382 | 613 | } |
0f113f3e MC |
614 | |
615 | if (prefix_len > | |
a230b26e | 616 | (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) { |
0f113f3e | 617 | /* insufficient space */ |
c48ffbcc | 618 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
0f113f3e MC |
619 | goto err; |
620 | } | |
621 | } | |
622 | ||
555cbb32 | 623 | s->s3.empty_fragment_done = 1; |
0f113f3e MC |
624 | } |
625 | ||
ad2f4cdc BE |
626 | using_ktls = BIO_get_ktls_send(s->wbio); |
627 | if (using_ktls) { | |
50ec7505 BP |
628 | /* |
629 | * ktls doesn't modify the buffer, but to avoid a warning we need to | |
630 | * discard the const qualifier. | |
631 | * This doesn't leak memory because the buffers have been released when | |
632 | * switching to ktls. | |
633 | */ | |
634 | SSL3_BUFFER_set_buf(&s->rlayer.wbuf[0], (unsigned char *)buf); | |
635 | SSL3_BUFFER_set_offset(&s->rlayer.wbuf[0], 0); | |
074a6e86 | 636 | SSL3_BUFFER_set_app_buffer(&s->rlayer.wbuf[0], 1); |
50ec7505 BP |
637 | goto wpacket_init_complete; |
638 | } | |
639 | ||
0f113f3e | 640 | if (create_empty_fragment) { |
d102d9df | 641 | wb = &s->rlayer.wbuf[0]; |
a4d64c7f | 642 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0 |
0f113f3e MC |
643 | /* |
644 | * extra fragment would be couple of cipher blocks, which would be | |
645 | * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real | |
8483a003 | 646 | * payload, then we can just pretend we simply have two headers. |
0f113f3e | 647 | */ |
f4bd5de5 | 648 | align = (size_t)SSL3_BUFFER_get_buf(wb) + 2 * SSL3_RT_HEADER_LENGTH; |
753be41d | 649 | align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD); |
a4d64c7f | 650 | #endif |
747e1639 | 651 | SSL3_BUFFER_set_offset(wb, align); |
c7c42022 MC |
652 | if (!WPACKET_init_static_len(&pkt[0], SSL3_BUFFER_get_buf(wb), |
653 | SSL3_BUFFER_get_len(wb), 0) | |
654 | || !WPACKET_allocate_bytes(&pkt[0], align, NULL)) { | |
c48ffbcc | 655 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c7c42022 MC |
656 | goto err; |
657 | } | |
658 | wpinited = 1; | |
0f113f3e | 659 | } else if (prefix_len) { |
d102d9df | 660 | wb = &s->rlayer.wbuf[0]; |
44e58f3b MC |
661 | if (!WPACKET_init_static_len(&pkt[0], |
662 | SSL3_BUFFER_get_buf(wb), | |
c7c42022 MC |
663 | SSL3_BUFFER_get_len(wb), 0) |
664 | || !WPACKET_allocate_bytes(&pkt[0], SSL3_BUFFER_get_offset(wb) | |
665 | + prefix_len, NULL)) { | |
c48ffbcc | 666 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c7c42022 MC |
667 | goto err; |
668 | } | |
669 | wpinited = 1; | |
0f113f3e | 670 | } else { |
a230b26e | 671 | for (j = 0; j < numpipes; j++) { |
44e58f3b MC |
672 | thispkt = &pkt[j]; |
673 | ||
d102d9df | 674 | wb = &s->rlayer.wbuf[j]; |
829754a6 | 675 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0 |
d102d9df | 676 | align = (size_t)SSL3_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH; |
753be41d | 677 | align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD); |
a4d64c7f | 678 | #endif |
d102d9df | 679 | SSL3_BUFFER_set_offset(wb, align); |
44e58f3b | 680 | if (!WPACKET_init_static_len(thispkt, SSL3_BUFFER_get_buf(wb), |
c7c42022 | 681 | SSL3_BUFFER_get_len(wb), 0) |
44e58f3b | 682 | || !WPACKET_allocate_bytes(thispkt, align, NULL)) { |
c48ffbcc | 683 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c7c42022 MC |
684 | goto err; |
685 | } | |
686 | wpinited++; | |
d102d9df | 687 | } |
0f113f3e MC |
688 | } |
689 | ||
0f113f3e | 690 | /* Explicit IV length, block ciphers appropriate version flag */ |
38b051a1 TM |
691 | if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s) |
692 | && !SSL_CONNECTION_TREAT_AS_TLS13(s)) { | |
ed576acd | 693 | int mode = EVP_CIPHER_CTX_get_mode(s->enc_write_ctx); |
0f113f3e | 694 | if (mode == EVP_CIPH_CBC_MODE) { |
ed576acd | 695 | eivlen = EVP_CIPHER_CTX_get_iv_length(s->enc_write_ctx); |
83ab43da DB |
696 | if (eivlen < 0) { |
697 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); | |
771fef77 | 698 | goto err; |
83ab43da | 699 | } |
0f113f3e MC |
700 | if (eivlen <= 1) |
701 | eivlen = 0; | |
c7c42022 MC |
702 | } else if (mode == EVP_CIPH_GCM_MODE) { |
703 | /* Need explicit part of IV for GCM mode */ | |
0f113f3e | 704 | eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN; |
c7c42022 | 705 | } else if (mode == EVP_CIPH_CCM_MODE) { |
e75c5a79 | 706 | eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN; |
c7c42022 | 707 | } |
c7c42022 | 708 | } |
0f113f3e | 709 | |
50ec7505 BP |
710 | wpacket_init_complete: |
711 | ||
d102d9df MC |
712 | totlen = 0; |
713 | /* Clear our SSL3_RECORD structures */ | |
cbe29648 | 714 | memset(wr, 0, sizeof(wr)); |
a230b26e | 715 | for (j = 0; j < numpipes; j++) { |
de9e884b MC |
716 | unsigned int version = (s->version == TLS1_3_VERSION) ? TLS1_2_VERSION |
717 | : s->version; | |
e8eb224b | 718 | unsigned char *compressdata = NULL; |
c7c42022 | 719 | size_t maxcomplen; |
e60ce9c4 | 720 | unsigned int rectype; |
0f113f3e | 721 | |
44e58f3b MC |
722 | thispkt = &pkt[j]; |
723 | thiswr = &wr[j]; | |
724 | ||
e60ce9c4 MC |
725 | /* |
726 | * In TLSv1.3, once encrypting, we always use application data for the | |
727 | * record type | |
728 | */ | |
38b051a1 | 729 | if (SSL_CONNECTION_TREAT_AS_TLS13(s) |
7426cd34 MC |
730 | && s->enc_write_ctx != NULL |
731 | && (s->statem.enc_write_state != ENC_WRITE_STATE_WRITE_PLAIN_ALERTS | |
732 | || type != SSL3_RT_ALERT)) | |
e60ce9c4 MC |
733 | rectype = SSL3_RT_APPLICATION_DATA; |
734 | else | |
735 | rectype = type; | |
3295d242 MC |
736 | SSL3_RECORD_set_type(thiswr, rectype); |
737 | ||
d102d9df | 738 | /* |
8483a003 | 739 | * Some servers hang if initial client hello is larger than 256 bytes |
d102d9df MC |
740 | * and record version number > TLS 1.0 |
741 | */ | |
38b051a1 | 742 | if (SSL_get_state(ssl) == TLS_ST_CW_CLNT_HELLO |
5f7470df | 743 | && !s->renegotiate |
38b051a1 | 744 | && TLS1_get_version(ssl) > TLS1_VERSION |
5f7470df | 745 | && s->hello_retry_request == SSL_HRR_NONE) |
c7c42022 | 746 | version = TLS1_VERSION; |
3295d242 | 747 | SSL3_RECORD_set_rec_version(thiswr, version); |
0f113f3e | 748 | |
44e58f3b MC |
749 | maxcomplen = pipelens[j]; |
750 | if (s->compress != NULL) | |
f33f9dde | 751 | maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD; |
44e58f3b | 752 | |
50ec7505 BP |
753 | /* |
754 | * When using offload kernel will write the header. | |
755 | * Otherwise write the header now | |
756 | */ | |
ad2f4cdc | 757 | if (!using_ktls |
50ec7505 | 758 | && (!WPACKET_put_bytes_u8(thispkt, rectype) |
44e58f3b MC |
759 | || !WPACKET_put_bytes_u16(thispkt, version) |
760 | || !WPACKET_start_sub_packet_u16(thispkt) | |
c7c42022 | 761 | || (eivlen > 0 |
44e58f3b | 762 | && !WPACKET_allocate_bytes(thispkt, eivlen, NULL)) |
c7c42022 | 763 | || (maxcomplen > 0 |
44e58f3b | 764 | && !WPACKET_reserve_bytes(thispkt, maxcomplen, |
50ec7505 | 765 | &compressdata)))) { |
c48ffbcc | 766 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c7c42022 MC |
767 | goto err; |
768 | } | |
0f113f3e | 769 | |
d102d9df | 770 | /* lets setup the record stuff. */ |
44e58f3b MC |
771 | SSL3_RECORD_set_data(thiswr, compressdata); |
772 | SSL3_RECORD_set_length(thiswr, pipelens[j]); | |
773 | SSL3_RECORD_set_input(thiswr, (unsigned char *)&buf[totlen]); | |
d102d9df | 774 | totlen += pipelens[j]; |
0f113f3e | 775 | |
d102d9df | 776 | /* |
44e58f3b MC |
777 | * we now 'read' from thiswr->input, thiswr->length bytes into |
778 | * thiswr->data | |
d102d9df | 779 | */ |
0f113f3e | 780 | |
d102d9df MC |
781 | /* first we compress */ |
782 | if (s->compress != NULL) { | |
44e58f3b MC |
783 | if (!ssl3_do_compress(s, thiswr) |
784 | || !WPACKET_allocate_bytes(thispkt, thiswr->length, NULL)) { | |
c48ffbcc | 785 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE); |
d102d9df MC |
786 | goto err; |
787 | } | |
788 | } else { | |
ad2f4cdc | 789 | if (using_ktls) { |
50ec7505 BP |
790 | SSL3_RECORD_reset_data(&wr[j]); |
791 | } else { | |
792 | if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) { | |
c48ffbcc | 793 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
50ec7505 BP |
794 | goto err; |
795 | } | |
796 | SSL3_RECORD_reset_input(&wr[j]); | |
c7c42022 | 797 | } |
d102d9df | 798 | } |
0f113f3e | 799 | |
38b051a1 | 800 | if (SSL_CONNECTION_TREAT_AS_TLS13(s) |
ad2f4cdc | 801 | && !using_ktls |
7426cd34 MC |
802 | && s->enc_write_ctx != NULL |
803 | && (s->statem.enc_write_state != ENC_WRITE_STATE_WRITE_PLAIN_ALERTS | |
804 | || type != SSL3_RT_ALERT)) { | |
cf72c757 | 805 | size_t rlen, max_send_fragment; |
c649d10d | 806 | |
44e58f3b | 807 | if (!WPACKET_put_bytes_u8(thispkt, type)) { |
c48ffbcc | 808 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
e60ce9c4 MC |
809 | goto err; |
810 | } | |
44e58f3b | 811 | SSL3_RECORD_add_length(thiswr, 1); |
c649d10d TS |
812 | |
813 | /* Add TLS1.3 padding */ | |
cf72c757 | 814 | max_send_fragment = ssl_get_max_send_fragment(s); |
96c9aee2 | 815 | rlen = SSL3_RECORD_get_length(thiswr); |
cf72c757 | 816 | if (rlen < max_send_fragment) { |
96c9aee2 | 817 | size_t padding = 0; |
cf72c757 | 818 | size_t max_padding = max_send_fragment - rlen; |
96c9aee2 | 819 | if (s->record_padding_cb != NULL) { |
38b051a1 | 820 | padding = s->record_padding_cb(ssl, type, rlen, s->record_padding_arg); |
96c9aee2 TS |
821 | } else if (s->block_padding > 0) { |
822 | size_t mask = s->block_padding - 1; | |
823 | size_t remainder; | |
824 | ||
825 | /* optimize for power of 2 */ | |
826 | if ((s->block_padding & mask) == 0) | |
827 | remainder = rlen & mask; | |
828 | else | |
829 | remainder = rlen % s->block_padding; | |
830 | /* don't want to add a block of padding if we don't have to */ | |
831 | if (remainder == 0) | |
832 | padding = 0; | |
833 | else | |
834 | padding = s->block_padding - remainder; | |
835 | } | |
836 | if (padding > 0) { | |
837 | /* do not allow the record to exceed max plaintext length */ | |
838 | if (padding > max_padding) | |
839 | padding = max_padding; | |
840 | if (!WPACKET_memset(thispkt, 0, padding)) { | |
c48ffbcc | 841 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
c2853382 | 842 | ERR_R_INTERNAL_ERROR); |
96c9aee2 TS |
843 | goto err; |
844 | } | |
845 | SSL3_RECORD_add_length(thiswr, padding); | |
c649d10d | 846 | } |
c649d10d | 847 | } |
e60ce9c4 MC |
848 | } |
849 | ||
0f113f3e | 850 | /* |
44e58f3b MC |
851 | * we should still have the output to thiswr->data and the input from |
852 | * wr->input. Length should be thiswr->length. thiswr->data still points | |
853 | * in the wb->buf | |
0f113f3e | 854 | */ |
0f113f3e | 855 | |
ad2f4cdc | 856 | if (!using_ktls && !SSL_WRITE_ETM(s) && mac_size != 0) { |
c7c42022 MC |
857 | unsigned char *mac; |
858 | ||
44e58f3b | 859 | if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac) |
38b051a1 | 860 | || !ssl->method->ssl3_enc->mac(s, thiswr, mac, 1)) { |
c48ffbcc | 861 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
d102d9df | 862 | goto err; |
c7c42022 MC |
863 | } |
864 | } | |
865 | ||
866 | /* | |
867 | * Reserve some bytes for any growth that may occur during encryption. | |
868 | * This will be at most one cipher block or the tag length if using | |
869 | * AEAD. SSL_RT_MAX_CIPHER_BLOCK_SIZE covers either case. | |
870 | */ | |
ad2f4cdc | 871 | if (!using_ktls) { |
50ec7505 BP |
872 | if (!WPACKET_reserve_bytes(thispkt, |
873 | SSL_RT_MAX_CIPHER_BLOCK_SIZE, | |
874 | NULL) | |
875 | /* | |
876 | * We also need next the amount of bytes written to this | |
877 | * sub-packet | |
878 | */ | |
44e58f3b | 879 | || !WPACKET_get_length(thispkt, &len)) { |
c48ffbcc | 880 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c7c42022 | 881 | goto err; |
50ec7505 | 882 | } |
c7c42022 | 883 | |
50ec7505 BP |
884 | /* Get a pointer to the start of this record excluding header */ |
885 | recordstart = WPACKET_get_curr(thispkt) - len; | |
886 | SSL3_RECORD_set_data(thiswr, recordstart); | |
887 | SSL3_RECORD_reset_input(thiswr); | |
888 | SSL3_RECORD_set_length(thiswr, len); | |
889 | } | |
0f113f3e MC |
890 | } |
891 | ||
7426cd34 | 892 | if (s->statem.enc_write_state == ENC_WRITE_STATE_WRITE_PLAIN_ALERTS) { |
49e7fe12 MC |
893 | /* |
894 | * We haven't actually negotiated the version yet, but we're trying to | |
3519bae5 | 895 | * send early data - so we need to use the tls13enc function. |
49e7fe12 | 896 | */ |
ec27e619 | 897 | if (tls13_enc(s, wr, numpipes, 1, NULL, mac_size) < 1) { |
921d84a0 | 898 | if (!ossl_statem_in_error(s)) { |
c48ffbcc | 899 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
921d84a0 | 900 | } |
49e7fe12 | 901 | goto err; |
c2853382 | 902 | } |
49e7fe12 | 903 | } else { |
ad2f4cdc | 904 | if (!using_ktls) { |
38b051a1 | 905 | if (ssl->method->ssl3_enc->enc(s, wr, numpipes, 1, NULL, |
ec27e619 | 906 | mac_size) < 1) { |
50ec7505 | 907 | if (!ossl_statem_in_error(s)) { |
c48ffbcc | 908 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
50ec7505 BP |
909 | } |
910 | goto err; | |
921d84a0 | 911 | } |
c2853382 | 912 | } |
49e7fe12 | 913 | } |
0f113f3e | 914 | |
a230b26e | 915 | for (j = 0; j < numpipes; j++) { |
c7c42022 MC |
916 | size_t origlen; |
917 | ||
44e58f3b MC |
918 | thispkt = &pkt[j]; |
919 | thiswr = &wr[j]; | |
920 | ||
ad2f4cdc | 921 | if (using_ktls) |
50ec7505 BP |
922 | goto mac_done; |
923 | ||
c7c42022 | 924 | /* Allocate bytes for the encryption overhead */ |
44e58f3b | 925 | if (!WPACKET_get_length(thispkt, &origlen) |
c7c42022 | 926 | /* Encryption should never shrink the data! */ |
44e58f3b MC |
927 | || origlen > thiswr->length |
928 | || (thiswr->length > origlen | |
929 | && !WPACKET_allocate_bytes(thispkt, | |
50ec7505 BP |
930 | thiswr->length - origlen, |
931 | NULL))) { | |
c48ffbcc | 932 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c7c42022 MC |
933 | goto err; |
934 | } | |
28a31a0a | 935 | if (SSL_WRITE_ETM(s) && mac_size != 0) { |
c7c42022 MC |
936 | unsigned char *mac; |
937 | ||
44e58f3b | 938 | if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac) |
38b051a1 | 939 | || !ssl->method->ssl3_enc->mac(s, thiswr, mac, 1)) { |
c48ffbcc | 940 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
d102d9df | 941 | goto err; |
c7c42022 | 942 | } |
44e58f3b | 943 | SSL3_RECORD_add_length(thiswr, mac_size); |
d102d9df | 944 | } |
0f113f3e | 945 | |
44e58f3b MC |
946 | if (!WPACKET_get_length(thispkt, &len) |
947 | || !WPACKET_close(thispkt)) { | |
c48ffbcc | 948 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c7c42022 MC |
949 | goto err; |
950 | } | |
d102d9df | 951 | |
c7c42022 | 952 | if (s->msg_callback) { |
44e58f3b | 953 | recordstart = WPACKET_get_curr(thispkt) - len |
c7c42022 | 954 | - SSL3_RT_HEADER_LENGTH; |
fcc3a520 | 955 | s->msg_callback(1, thiswr->rec_version, SSL3_RT_HEADER, recordstart, |
38b051a1 | 956 | SSL3_RT_HEADER_LENGTH, ssl, |
d102d9df | 957 | s->msg_callback_arg); |
ad5100bc | 958 | |
38b051a1 | 959 | if (SSL_CONNECTION_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL) { |
ad5100bc MC |
960 | unsigned char ctype = type; |
961 | ||
fcc3a520 | 962 | s->msg_callback(1, thiswr->rec_version, SSL3_RT_INNER_CONTENT_TYPE, |
38b051a1 | 963 | &ctype, 1, ssl, s->msg_callback_arg); |
ad5100bc | 964 | } |
c7c42022 MC |
965 | } |
966 | ||
44e58f3b | 967 | if (!WPACKET_finish(thispkt)) { |
c48ffbcc | 968 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
c7c42022 MC |
969 | goto err; |
970 | } | |
0f113f3e | 971 | |
50ec7505 | 972 | /* header is added by the kernel when using offload */ |
b375a8ac | 973 | SSL3_RECORD_add_length(thiswr, SSL3_RT_HEADER_LENGTH); |
d102d9df MC |
974 | |
975 | if (create_empty_fragment) { | |
976 | /* | |
977 | * we are in a recursive call; just return the length, don't write | |
978 | * out anything here | |
979 | */ | |
980 | if (j > 0) { | |
981 | /* We should never be pipelining an empty fragment!! */ | |
c48ffbcc | 982 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
d102d9df MC |
983 | goto err; |
984 | } | |
44e58f3b | 985 | *written = SSL3_RECORD_get_length(thiswr); |
7ee8627f | 986 | return 1; |
d102d9df MC |
987 | } |
988 | ||
50ec7505 BP |
989 | mac_done: |
990 | /* | |
991 | * we should now have thiswr->data pointing to the encrypted data, which | |
992 | * is thiswr->length long | |
993 | */ | |
994 | SSL3_RECORD_set_type(thiswr, type); /* not needed but helps for | |
995 | * debugging */ | |
996 | ||
d102d9df MC |
997 | /* now let's set up wb */ |
998 | SSL3_BUFFER_set_left(&s->rlayer.wbuf[j], | |
44e58f3b | 999 | prefix_len + SSL3_RECORD_get_length(thiswr)); |
0f113f3e MC |
1000 | } |
1001 | ||
0f113f3e MC |
1002 | /* |
1003 | * memorize arguments so that ssl3_write_pending can detect bad write | |
1004 | * retries later | |
1005 | */ | |
d102d9df | 1006 | s->rlayer.wpend_tot = totlen; |
f8caa3c8 MC |
1007 | s->rlayer.wpend_buf = buf; |
1008 | s->rlayer.wpend_type = type; | |
d102d9df | 1009 | s->rlayer.wpend_ret = totlen; |
0f113f3e MC |
1010 | |
1011 | /* we now just need to write the buffer */ | |
7ee8627f | 1012 | return ssl3_write_pending(s, type, buf, totlen, written); |
0f113f3e | 1013 | err: |
c7c42022 MC |
1014 | for (j = 0; j < wpinited; j++) |
1015 | WPACKET_cleanup(&pkt[j]); | |
0f113f3e MC |
1016 | return -1; |
1017 | } | |
d02b48c6 | 1018 | |
7c7c3561 | 1019 | /* if SSL3_BUFFER_get_left() != 0, we need to call this |
4880672a | 1020 | * |
beacb0f0 | 1021 | * Return values are as per SSL_write() |
4880672a | 1022 | */ |
38b051a1 TM |
1023 | int ssl3_write_pending(SSL_CONNECTION *s, int type, const unsigned char *buf, |
1024 | size_t len, size_t *written) | |
0f113f3e MC |
1025 | { |
1026 | int i; | |
d102d9df | 1027 | SSL3_BUFFER *wb = s->rlayer.wbuf; |
7ee8627f | 1028 | size_t currbuf = 0; |
f0ca8f89 | 1029 | size_t tmpwrit = 0; |
d02b48c6 | 1030 | |
7ee8627f | 1031 | if ((s->rlayer.wpend_tot > len) |
ebc20cfa BE |
1032 | || (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) |
1033 | && (s->rlayer.wpend_buf != buf)) | |
f8caa3c8 | 1034 | || (s->rlayer.wpend_type != type)) { |
c48ffbcc | 1035 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_WRITE_RETRY); |
7ee8627f | 1036 | return -1; |
0f113f3e MC |
1037 | } |
1038 | ||
1039 | for (;;) { | |
d102d9df MC |
1040 | /* Loop until we find a buffer we haven't written out yet */ |
1041 | if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0 | |
a230b26e | 1042 | && currbuf < s->rlayer.numwpipes - 1) { |
d102d9df MC |
1043 | currbuf++; |
1044 | continue; | |
1045 | } | |
0f113f3e MC |
1046 | clear_sys_error(); |
1047 | if (s->wbio != NULL) { | |
1048 | s->rwstate = SSL_WRITING; | |
50ec7505 BP |
1049 | |
1050 | /* | |
1051 | * To prevent coalescing of control and data messages, | |
1052 | * such as in buffer_write, we flush the BIO | |
1053 | */ | |
1054 | if (BIO_get_ktls_send(s->wbio) && type != SSL3_RT_APPLICATION_DATA) { | |
1055 | i = BIO_flush(s->wbio); | |
1056 | if (i <= 0) | |
1057 | return i; | |
50ec7505 BP |
1058 | BIO_set_ktls_ctrl_msg(s->wbio, type); |
1059 | } | |
a230b26e EK |
1060 | i = BIO_write(s->wbio, (char *) |
1061 | &(SSL3_BUFFER_get_buf(&wb[currbuf]) | |
1062 | [SSL3_BUFFER_get_offset(&wb[currbuf])]), | |
1063 | (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf])); | |
7ee8627f MC |
1064 | if (i >= 0) |
1065 | tmpwrit = i; | |
0f113f3e | 1066 | } else { |
c48ffbcc | 1067 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BIO_NOT_SET); |
0f113f3e MC |
1068 | i = -1; |
1069 | } | |
410f5bb1 | 1070 | |
5ea4d764 P |
1071 | /* |
1072 | * When an empty fragment is sent on a connection using KTLS, | |
1073 | * it is sent as a write of zero bytes. If this zero byte | |
1074 | * write succeeds, i will be 0 rather than a non-zero value. | |
1075 | * Treat i == 0 as success rather than an error for zero byte | |
1076 | * writes to permit this case. | |
1077 | */ | |
410f5bb1 | 1078 | if (i >= 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) { |
d102d9df | 1079 | SSL3_BUFFER_set_left(&wb[currbuf], 0); |
7ee8627f | 1080 | SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit); |
d102d9df MC |
1081 | if (currbuf + 1 < s->rlayer.numwpipes) |
1082 | continue; | |
0f113f3e | 1083 | s->rwstate = SSL_NOTHING; |
7ee8627f MC |
1084 | *written = s->rlayer.wpend_ret; |
1085 | return 1; | |
0f113f3e | 1086 | } else if (i <= 0) { |
38b051a1 | 1087 | if (SSL_CONNECTION_IS_DTLS(s)) { |
0f113f3e MC |
1088 | /* |
1089 | * For DTLS, just drop it. That's kind of the whole point in | |
1090 | * using a datagram service | |
1091 | */ | |
d102d9df | 1092 | SSL3_BUFFER_set_left(&wb[currbuf], 0); |
0f113f3e | 1093 | } |
26a7d938 | 1094 | return i; |
0f113f3e | 1095 | } |
7ee8627f MC |
1096 | SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit); |
1097 | SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit); | |
0f113f3e MC |
1098 | } |
1099 | } | |
d02b48c6 | 1100 | |
1d97c843 TH |
1101 | /*- |
1102 | * Return up to 'len' payload bytes received in 'type' records. | |
b35e9050 BM |
1103 | * 'type' is one of the following: |
1104 | * | |
1105 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) | |
1106 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) | |
1107 | * - 0 (during a shutdown, no data has to be returned) | |
1108 | * | |
1109 | * If we don't have stored data to work from, read a SSL/TLS record first | |
1110 | * (possibly multiple records if we still don't have anything to return). | |
1111 | * | |
1112 | * This function must handle any surprises the peer may have for us, such as | |
657da85e | 1113 | * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec |
105af0ad | 1114 | * messages are treated as if they were handshake messages *if* the |recvd_type| |
657da85e | 1115 | * argument is non NULL. |
b35e9050 BM |
1116 | * Also if record payloads contain fragments too small to process, we store |
1117 | * them until there is enough for the respective protocol (the record protocol | |
1118 | * may use arbitrary fragmentation and even interleaving): | |
1119 | * Change cipher spec protocol | |
1120 | * just 1 byte needed, no need for keeping anything stored | |
1121 | * Alert protocol | |
1122 | * 2 bytes needed (AlertLevel, AlertDescription) | |
1123 | * Handshake protocol | |
1124 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have | |
1125 | * to detect unexpected Client Hello and Hello Request messages | |
1126 | * here, anything else is handled by higher layers | |
1127 | * Application data protocol | |
1128 | * none of our business | |
1129 | */ | |
38b051a1 | 1130 | int ssl3_read_bytes(SSL *ssl, int type, int *recvd_type, unsigned char *buf, |
54105ddd | 1131 | size_t len, int peek, size_t *readbytes) |
0f113f3e | 1132 | { |
99dd3740 | 1133 | int i, j, ret; |
54105ddd | 1134 | size_t n, curr_rec, num_recs, totalbytes; |
0f113f3e | 1135 | SSL3_RECORD *rr; |
94777c9c | 1136 | SSL3_BUFFER *rbuf; |
0f113f3e | 1137 | void (*cb) (const SSL *ssl, int type2, int val) = NULL; |
38b051a1 TM |
1138 | int is_tls13; |
1139 | SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl); | |
1140 | ||
1141 | is_tls13 = SSL_CONNECTION_IS_TLS13(s); | |
0f113f3e | 1142 | |
e2d5742b | 1143 | rbuf = s->rrlmethod->get0_rbuf(s->rrl); |
94777c9c MC |
1144 | |
1145 | if (!SSL3_BUFFER_is_initialised(rbuf)) { | |
28d59af8 | 1146 | /* Not initialized yet */ |
196f2cbb MC |
1147 | if (!ssl3_setup_read_buffer(s)) { |
1148 | /* SSLfatal() already called */ | |
eda75751 | 1149 | return -1; |
196f2cbb | 1150 | } |
28d59af8 | 1151 | } |
0f113f3e MC |
1152 | |
1153 | if ((type && (type != SSL3_RT_APPLICATION_DATA) | |
1154 | && (type != SSL3_RT_HANDSHAKE)) || (peek | |
1155 | && (type != | |
1156 | SSL3_RT_APPLICATION_DATA))) { | |
c48ffbcc | 1157 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
0f113f3e MC |
1158 | return -1; |
1159 | } | |
1160 | ||
4aa7389e | 1161 | if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0)) |
0f113f3e MC |
1162 | /* (partially) satisfy request from storage */ |
1163 | { | |
4aa7389e | 1164 | unsigned char *src = s->rlayer.handshake_fragment; |
0f113f3e MC |
1165 | unsigned char *dst = buf; |
1166 | unsigned int k; | |
1167 | ||
1168 | /* peek == 0 */ | |
1169 | n = 0; | |
4aa7389e | 1170 | while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) { |
0f113f3e MC |
1171 | *dst++ = *src++; |
1172 | len--; | |
4aa7389e | 1173 | s->rlayer.handshake_fragment_len--; |
0f113f3e MC |
1174 | n++; |
1175 | } | |
1176 | /* move any remaining fragment bytes: */ | |
4aa7389e MC |
1177 | for (k = 0; k < s->rlayer.handshake_fragment_len; k++) |
1178 | s->rlayer.handshake_fragment[k] = *src++; | |
e9f6b9a1 MC |
1179 | |
1180 | if (recvd_type != NULL) | |
1181 | *recvd_type = SSL3_RT_HANDSHAKE; | |
1182 | ||
54105ddd | 1183 | *readbytes = n; |
eda75751 | 1184 | return 1; |
0f113f3e MC |
1185 | } |
1186 | ||
1187 | /* | |
4aa7389e | 1188 | * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. |
0f113f3e MC |
1189 | */ |
1190 | ||
38b051a1 | 1191 | if (!ossl_statem_get_in_handshake(s) && SSL_in_init(ssl)) { |
0f113f3e | 1192 | /* type == SSL3_RT_APPLICATION_DATA */ |
38b051a1 | 1193 | i = s->handshake_func(ssl); |
99dd3740 | 1194 | /* SSLfatal() already called */ |
0f113f3e | 1195 | if (i < 0) |
eda75751 | 1196 | return i; |
99dd3740 | 1197 | if (i == 0) |
eda75751 | 1198 | return -1; |
0f113f3e MC |
1199 | } |
1200 | start: | |
1201 | s->rwstate = SSL_NOTHING; | |
1202 | ||
50e735f9 | 1203 | /*- |
94777c9c MC |
1204 | * For each record 'i' up to |num_recs] |
1205 | * rr[i].type - is the type of record | |
1206 | * rr[i].data, - data | |
1207 | * rr[i].off, - offset into 'data' for next read | |
1208 | * rr[i].length, - number of bytes. | |
50e735f9 | 1209 | */ |
94777c9c MC |
1210 | rr = s->rlayer.rrec; |
1211 | num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer); | |
1212 | ||
1213 | do { | |
1214 | /* get new records if necessary */ | |
1215 | if (num_recs == 0) { | |
1216 | ret = ssl3_get_record(s); | |
99dd3740 MC |
1217 | if (ret <= 0) { |
1218 | /* SSLfatal() already called if appropriate */ | |
eda75751 | 1219 | return ret; |
99dd3740 | 1220 | } |
94777c9c MC |
1221 | num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer); |
1222 | if (num_recs == 0) { | |
1223 | /* Shouldn't happen */ | |
c48ffbcc | 1224 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
99dd3740 | 1225 | return -1; |
94777c9c MC |
1226 | } |
1227 | } | |
255cfeac | 1228 | /* Skip over any records we have already read */ |
94777c9c | 1229 | for (curr_rec = 0; |
255cfeac | 1230 | curr_rec < num_recs && SSL3_RECORD_is_read(&rr[curr_rec]); |
a230b26e | 1231 | curr_rec++) ; |
94777c9c MC |
1232 | if (curr_rec == num_recs) { |
1233 | RECORD_LAYER_set_numrpipes(&s->rlayer, 0); | |
1234 | num_recs = 0; | |
1235 | curr_rec = 0; | |
1236 | } | |
1237 | } while (num_recs == 0); | |
1238 | rr = &rr[curr_rec]; | |
0f113f3e | 1239 | |
3d35e3a2 MC |
1240 | if (s->rlayer.handshake_fragment_len > 0 |
1241 | && SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE | |
38b051a1 | 1242 | && SSL_CONNECTION_IS_TLS13(s)) { |
c48ffbcc | 1243 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
3d35e3a2 MC |
1244 | SSL_R_MIXED_HANDSHAKE_AND_NON_HANDSHAKE_DATA); |
1245 | return -1; | |
1246 | } | |
1247 | ||
af58be76 MC |
1248 | /* |
1249 | * Reset the count of consecutive warning alerts if we've got a non-empty | |
1250 | * record that isn't an alert. | |
1251 | */ | |
1252 | if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT | |
1253 | && SSL3_RECORD_get_length(rr) != 0) | |
1254 | s->rlayer.alert_count = 0; | |
1255 | ||
0f113f3e MC |
1256 | /* we now have a packet which can be read and processed */ |
1257 | ||
555cbb32 TS |
1258 | if (s->s3.change_cipher_spec /* set when we receive ChangeCipherSpec, |
1259 | * reset by ssl3_get_finished */ | |
747e1639 | 1260 | && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) { |
c48ffbcc | 1261 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
99dd3740 MC |
1262 | SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); |
1263 | return -1; | |
0f113f3e MC |
1264 | } |
1265 | ||
1266 | /* | |
1267 | * If the other end has shut down, throw anything we read away (even in | |
1268 | * 'peek' mode) | |
1269 | */ | |
1270 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | |
747e1639 | 1271 | SSL3_RECORD_set_length(rr, 0); |
0f113f3e | 1272 | s->rwstate = SSL_NOTHING; |
eda75751 | 1273 | return 0; |
0f113f3e MC |
1274 | } |
1275 | ||
657da85e | 1276 | if (type == SSL3_RECORD_get_type(rr) |
a230b26e | 1277 | || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC |
97997489 | 1278 | && type == SSL3_RT_HANDSHAKE && recvd_type != NULL |
bcf2907c | 1279 | && !is_tls13)) { |
657da85e MC |
1280 | /* |
1281 | * SSL3_RT_APPLICATION_DATA or | |
1282 | * SSL3_RT_HANDSHAKE or | |
1283 | * SSL3_RT_CHANGE_CIPHER_SPEC | |
1284 | */ | |
0f113f3e MC |
1285 | /* |
1286 | * make sure that we are not getting application data when we are | |
1287 | * doing a handshake for the first time | |
1288 | */ | |
38b051a1 TM |
1289 | if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA |
1290 | && s->enc_read_ctx == NULL) { | |
c48ffbcc | 1291 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_APP_DATA_IN_HANDSHAKE); |
99dd3740 | 1292 | return -1; |
0f113f3e MC |
1293 | } |
1294 | ||
657da85e | 1295 | if (type == SSL3_RT_HANDSHAKE |
a230b26e EK |
1296 | && SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC |
1297 | && s->rlayer.handshake_fragment_len > 0) { | |
c48ffbcc | 1298 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY); |
99dd3740 | 1299 | return -1; |
657da85e MC |
1300 | } |
1301 | ||
1302 | if (recvd_type != NULL) | |
1303 | *recvd_type = SSL3_RECORD_get_type(rr); | |
1304 | ||
1c47d35a MC |
1305 | if (len == 0) { |
1306 | /* | |
1307 | * Mark a zero length record as read. This ensures multiple calls to | |
1308 | * SSL_read() with a zero length buffer will eventually cause | |
1309 | * SSL_pending() to report data as being available. | |
1310 | */ | |
1311 | if (SSL3_RECORD_get_length(rr) == 0) | |
1312 | SSL3_RECORD_set_read(rr); | |
eda75751 | 1313 | return 0; |
1c47d35a | 1314 | } |
0f113f3e | 1315 | |
54105ddd | 1316 | totalbytes = 0; |
94777c9c | 1317 | do { |
54105ddd | 1318 | if (len - totalbytes > SSL3_RECORD_get_length(rr)) |
94777c9c MC |
1319 | n = SSL3_RECORD_get_length(rr); |
1320 | else | |
54105ddd | 1321 | n = len - totalbytes; |
94777c9c MC |
1322 | |
1323 | memcpy(buf, &(rr->data[rr->off]), n); | |
1324 | buf += n; | |
b8d24395 MC |
1325 | if (peek) { |
1326 | /* Mark any zero length record as consumed CVE-2016-6305 */ | |
1327 | if (SSL3_RECORD_get_length(rr) == 0) | |
1328 | SSL3_RECORD_set_read(rr); | |
1329 | } else { | |
163b8016 ME |
1330 | if (s->options & SSL_OP_CLEANSE_PLAINTEXT) |
1331 | OPENSSL_cleanse(&(rr->data[rr->off]), n); | |
753be41d | 1332 | SSL3_RECORD_sub_length(rr, n); |
94777c9c MC |
1333 | SSL3_RECORD_add_off(rr, n); |
1334 | if (SSL3_RECORD_get_length(rr) == 0) { | |
1335 | s->rlayer.rstate = SSL_ST_READ_HEADER; | |
1336 | SSL3_RECORD_set_off(rr, 0); | |
255cfeac | 1337 | SSL3_RECORD_set_read(rr); |
94777c9c | 1338 | } |
0f113f3e | 1339 | } |
94777c9c MC |
1340 | if (SSL3_RECORD_get_length(rr) == 0 |
1341 | || (peek && n == SSL3_RECORD_get_length(rr))) { | |
1342 | curr_rec++; | |
1343 | rr++; | |
1344 | } | |
54105ddd | 1345 | totalbytes += n; |
94777c9c | 1346 | } while (type == SSL3_RT_APPLICATION_DATA && curr_rec < num_recs |
54105ddd MC |
1347 | && totalbytes < len); |
1348 | if (totalbytes == 0) { | |
255cfeac MC |
1349 | /* We must have read empty records. Get more data */ |
1350 | goto start; | |
1351 | } | |
94777c9c | 1352 | if (!peek && curr_rec == num_recs |
a230b26e EK |
1353 | && (s->mode & SSL_MODE_RELEASE_BUFFERS) |
1354 | && SSL3_BUFFER_get_left(rbuf) == 0) | |
94777c9c | 1355 | ssl3_release_read_buffer(s); |
54105ddd | 1356 | *readbytes = totalbytes; |
eda75751 | 1357 | return 1; |
0f113f3e MC |
1358 | } |
1359 | ||
1360 | /* | |
1361 | * If we get here, then type != rr->type; if we have a handshake message, | |
657da85e MC |
1362 | * then it was unexpected (Hello Request or Client Hello) or invalid (we |
1363 | * were actually expecting a CCS). | |
0f113f3e MC |
1364 | */ |
1365 | ||
32ec4153 MC |
1366 | /* |
1367 | * Lets just double check that we've not got an SSLv2 record | |
1368 | */ | |
1369 | if (rr->rec_version == SSL2_VERSION) { | |
1370 | /* | |
1371 | * Should never happen. ssl3_get_record() should only give us an SSLv2 | |
1372 | * record back if this is the first packet and we are looking for an | |
1373 | * initial ClientHello. Therefore |type| should always be equal to | |
1374 | * |rr->type|. If not then something has gone horribly wrong | |
1375 | */ | |
c48ffbcc | 1376 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
99dd3740 | 1377 | return -1; |
32ec4153 MC |
1378 | } |
1379 | ||
38b051a1 | 1380 | if (ssl->method->version == TLS_ANY_VERSION |
a230b26e | 1381 | && (s->server || rr->type != SSL3_RT_ALERT)) { |
13c9bb3e MC |
1382 | /* |
1383 | * If we've got this far and still haven't decided on what version | |
558ea847 RL |
1384 | * we're using then this must be a client side alert we're dealing |
1385 | * with. We shouldn't be receiving anything other than a ClientHello | |
1386 | * if we are a server. | |
13c9bb3e MC |
1387 | */ |
1388 | s->version = rr->rec_version; | |
c48ffbcc | 1389 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
99dd3740 | 1390 | return -1; |
13c9bb3e MC |
1391 | } |
1392 | ||
50e735f9 | 1393 | /*- |
4aa7389e | 1394 | * s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE; |
50e735f9 MC |
1395 | * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) |
1396 | */ | |
0f113f3e | 1397 | |
bd990e25 MC |
1398 | if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) { |
1399 | unsigned int alert_level, alert_descr; | |
1400 | unsigned char *alert_bytes = SSL3_RECORD_get_data(rr) | |
1401 | + SSL3_RECORD_get_off(rr); | |
1402 | PACKET alert; | |
1403 | ||
1404 | if (!PACKET_buf_init(&alert, alert_bytes, SSL3_RECORD_get_length(rr)) | |
1405 | || !PACKET_get_1(&alert, &alert_level) | |
1406 | || !PACKET_get_1(&alert, &alert_descr) | |
1407 | || PACKET_remaining(&alert) != 0) { | |
c48ffbcc | 1408 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT); |
99dd3740 | 1409 | return -1; |
bd990e25 | 1410 | } |
0f113f3e MC |
1411 | |
1412 | if (s->msg_callback) | |
38b051a1 | 1413 | s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, ssl, |
4aa7389e | 1414 | s->msg_callback_arg); |
0f113f3e MC |
1415 | |
1416 | if (s->info_callback != NULL) | |
1417 | cb = s->info_callback; | |
38b051a1 TM |
1418 | else if (ssl->ctx->info_callback != NULL) |
1419 | cb = ssl->ctx->info_callback; | |
0f113f3e MC |
1420 | |
1421 | if (cb != NULL) { | |
1422 | j = (alert_level << 8) | alert_descr; | |
38b051a1 | 1423 | cb(ssl, SSL_CB_READ_ALERT, j); |
0f113f3e MC |
1424 | } |
1425 | ||
bcf2907c MC |
1426 | if (alert_level == SSL3_AL_WARNING |
1427 | || (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED)) { | |
555cbb32 | 1428 | s->s3.warn_alert = alert_descr; |
63916e9a | 1429 | SSL3_RECORD_set_read(rr); |
af58be76 MC |
1430 | |
1431 | s->rlayer.alert_count++; | |
1432 | if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) { | |
c48ffbcc | 1433 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
99dd3740 MC |
1434 | SSL_R_TOO_MANY_WARN_ALERTS); |
1435 | return -1; | |
af58be76 | 1436 | } |
4aa5a566 MC |
1437 | } |
1438 | ||
1439 | /* | |
bcf2907c MC |
1440 | * Apart from close_notify the only other warning alert in TLSv1.3 |
1441 | * is user_cancelled - which we just ignore. | |
4aa5a566 | 1442 | */ |
bcf2907c MC |
1443 | if (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED) { |
1444 | goto start; | |
1445 | } else if (alert_descr == SSL_AD_CLOSE_NOTIFY | |
1446 | && (is_tls13 || alert_level == SSL3_AL_WARNING)) { | |
1447 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; | |
1448 | return 0; | |
1449 | } else if (alert_level == SSL3_AL_FATAL || is_tls13) { | |
0f113f3e | 1450 | s->rwstate = SSL_NOTHING; |
555cbb32 | 1451 | s->s3.fatal_alert = alert_descr; |
c48ffbcc RL |
1452 | SSLfatal_data(s, SSL_AD_NO_ALERT, |
1453 | SSL_AD_REASON_OFFSET + alert_descr, | |
1454 | "SSL alert number %d", alert_descr); | |
0f113f3e | 1455 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
63916e9a | 1456 | SSL3_RECORD_set_read(rr); |
e2bb9b9b | 1457 | SSL_CTX_remove_session(s->session_ctx, s->session); |
eda75751 | 1458 | return 0; |
bcf2907c MC |
1459 | } else if (alert_descr == SSL_AD_NO_RENEGOTIATION) { |
1460 | /* | |
1461 | * This is a warning but we receive it if we requested | |
1462 | * renegotiation and the peer denied it. Terminate with a fatal | |
1463 | * alert because if application tried to renegotiate it | |
1464 | * presumably had a good reason and expects it to succeed. In | |
1465 | * future we might have a renegotiation where we don't care if | |
1466 | * the peer refused it where we carry on. | |
1467 | */ | |
c48ffbcc | 1468 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_RENEGOTIATION); |
99dd3740 | 1469 | return -1; |
fee33643 MC |
1470 | } else if (alert_level == SSL3_AL_WARNING) { |
1471 | /* We ignore any other warning alert in TLSv1.2 and below */ | |
1472 | goto start; | |
0f113f3e | 1473 | } |
bcf2907c | 1474 | |
c48ffbcc | 1475 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE); |
bcf2907c | 1476 | return -1; |
0f113f3e MC |
1477 | } |
1478 | ||
ba709049 | 1479 | if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) { |
ba709049 MC |
1480 | if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) { |
1481 | BIO *rbio; | |
1482 | ||
1bf4cb0f MC |
1483 | /* |
1484 | * We ignore any handshake messages sent to us unless they are | |
1485 | * TLSv1.3 in which case we want to process them. For all other | |
1486 | * handshake messages we can't do anything reasonable with them | |
1487 | * because we are unable to write any response due to having already | |
1488 | * sent close_notify. | |
1489 | */ | |
38b051a1 | 1490 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
1bf4cb0f MC |
1491 | SSL3_RECORD_set_length(rr, 0); |
1492 | SSL3_RECORD_set_read(rr); | |
1493 | ||
1494 | if ((s->mode & SSL_MODE_AUTO_RETRY) != 0) | |
1495 | goto start; | |
ba709049 | 1496 | |
1bf4cb0f | 1497 | s->rwstate = SSL_READING; |
38b051a1 | 1498 | rbio = SSL_get_rbio(ssl); |
1bf4cb0f MC |
1499 | BIO_clear_retry_flags(rbio); |
1500 | BIO_set_retry_read(rbio); | |
1501 | return -1; | |
1502 | } | |
358ffa05 MC |
1503 | } else { |
1504 | /* | |
1505 | * The peer is continuing to send application data, but we have | |
1506 | * already sent close_notify. If this was expected we should have | |
1507 | * been called via SSL_read() and this would have been handled | |
1508 | * above. | |
1509 | * No alert sent because we already sent close_notify | |
1510 | */ | |
1bf4cb0f MC |
1511 | SSL3_RECORD_set_length(rr, 0); |
1512 | SSL3_RECORD_set_read(rr); | |
c48ffbcc | 1513 | SSLfatal(s, SSL_AD_NO_ALERT, |
358ffa05 | 1514 | SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY); |
1bf4cb0f | 1515 | return -1; |
ba709049 | 1516 | } |
0f113f3e MC |
1517 | } |
1518 | ||
93f528f3 MC |
1519 | /* |
1520 | * For handshake data we have 'fragment' storage, so fill that so that we | |
1521 | * can process the header at a fixed place. This is done after the | |
1522 | * "SHUTDOWN" code above to avoid filling the fragment storage with data | |
1523 | * that we're just going to discard. | |
1524 | */ | |
1525 | if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) { | |
1526 | size_t dest_maxlen = sizeof(s->rlayer.handshake_fragment); | |
1527 | unsigned char *dest = s->rlayer.handshake_fragment; | |
1528 | size_t *dest_len = &s->rlayer.handshake_fragment_len; | |
1529 | ||
1530 | n = dest_maxlen - *dest_len; /* available space in 'dest' */ | |
1531 | if (SSL3_RECORD_get_length(rr) < n) | |
1532 | n = SSL3_RECORD_get_length(rr); /* available bytes */ | |
1533 | ||
1534 | /* now move 'n' bytes: */ | |
1535 | memcpy(dest + *dest_len, | |
1536 | SSL3_RECORD_get_data(rr) + SSL3_RECORD_get_off(rr), n); | |
1537 | SSL3_RECORD_add_off(rr, n); | |
1538 | SSL3_RECORD_sub_length(rr, n); | |
1539 | *dest_len += n; | |
1540 | if (SSL3_RECORD_get_length(rr) == 0) | |
1541 | SSL3_RECORD_set_read(rr); | |
1542 | ||
1543 | if (*dest_len < dest_maxlen) | |
1544 | goto start; /* fragment was too small */ | |
1545 | } | |
1546 | ||
747e1639 | 1547 | if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) { |
c48ffbcc | 1548 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY); |
99dd3740 | 1549 | return -1; |
0f113f3e MC |
1550 | } |
1551 | ||
1552 | /* | |
c7f47786 | 1553 | * Unexpected handshake message (ClientHello, NewSessionTicket (TLS1.3) or |
0386aad1 | 1554 | * protocol violation) |
0f113f3e | 1555 | */ |
024f543c | 1556 | if ((s->rlayer.handshake_fragment_len >= 4) |
c7f47786 | 1557 | && !ossl_statem_get_in_handshake(s)) { |
39ef7821 MC |
1558 | int ined = (s->early_data_state == SSL_EARLY_DATA_READING); |
1559 | ||
c7f47786 MC |
1560 | /* We found handshake data, so we're going back into init */ |
1561 | ossl_statem_set_in_init(s, 1); | |
1562 | ||
38b051a1 | 1563 | i = s->handshake_func(ssl); |
99dd3740 | 1564 | /* SSLfatal() already called if appropriate */ |
0f113f3e | 1565 | if (i < 0) |
eda75751 | 1566 | return i; |
0f113f3e | 1567 | if (i == 0) { |
eda75751 | 1568 | return -1; |
0f113f3e MC |
1569 | } |
1570 | ||
39ef7821 MC |
1571 | /* |
1572 | * If we were actually trying to read early data and we found a | |
1573 | * handshake message, then we don't want to continue to try and read | |
1574 | * the application data any more. It won't be "early" now. | |
1575 | */ | |
1576 | if (ined) | |
1577 | return -1; | |
1578 | ||
0f113f3e | 1579 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) { |
94777c9c | 1580 | if (SSL3_BUFFER_get_left(rbuf) == 0) { |
28d59af8 | 1581 | /* no read-ahead left? */ |
0f113f3e MC |
1582 | BIO *bio; |
1583 | /* | |
1584 | * In the case where we try to read application data, but we | |
1585 | * trigger an SSL handshake, we return -1 with the retry | |
1586 | * option set. Otherwise renegotiation may cause nasty | |
1587 | * problems in the blocking world | |
1588 | */ | |
1589 | s->rwstate = SSL_READING; | |
38b051a1 | 1590 | bio = SSL_get_rbio(ssl); |
0f113f3e MC |
1591 | BIO_clear_retry_flags(bio); |
1592 | BIO_set_retry_read(bio); | |
eda75751 | 1593 | return -1; |
0f113f3e MC |
1594 | } |
1595 | } | |
1596 | goto start; | |
1597 | } | |
1598 | ||
747e1639 | 1599 | switch (SSL3_RECORD_get_type(rr)) { |
0f113f3e | 1600 | default: |
0f113f3e | 1601 | /* |
436a2a01 MC |
1602 | * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but |
1603 | * TLS 1.2 says you MUST send an unexpected message alert. We use the | |
1604 | * TLS 1.2 behaviour for all protocol versions to prevent issues where | |
1605 | * no progress is being made and the peer continually sends unrecognised | |
1606 | * record types, using up resources processing them. | |
0f113f3e | 1607 | */ |
c48ffbcc | 1608 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD); |
99dd3740 | 1609 | return -1; |
0f113f3e MC |
1610 | case SSL3_RT_CHANGE_CIPHER_SPEC: |
1611 | case SSL3_RT_ALERT: | |
1612 | case SSL3_RT_HANDSHAKE: | |
1613 | /* | |
1614 | * we already handled all of these, with the possible exception of | |
024f543c MC |
1615 | * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but |
1616 | * that should not happen when type != rr->type | |
0f113f3e | 1617 | */ |
c48ffbcc | 1618 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR); |
99dd3740 | 1619 | return -1; |
0f113f3e MC |
1620 | case SSL3_RT_APPLICATION_DATA: |
1621 | /* | |
1622 | * At this point, we were expecting handshake data, but have | |
1623 | * application data. If the library was running inside ssl3_read() | |
1624 | * (i.e. in_read_app_data is set) and it makes sense to read | |
1625 | * application data at this point (session renegotiation not yet | |
1626 | * started), we will indulge it. | |
1627 | */ | |
fe3a3291 | 1628 | if (ossl_statem_app_data_allowed(s)) { |
555cbb32 | 1629 | s->s3.in_read_app_data = 2; |
eda75751 | 1630 | return -1; |
a832b5ef MC |
1631 | } else if (ossl_statem_skip_early_data(s)) { |
1632 | /* | |
1633 | * This can happen after a client sends a CH followed by early_data, | |
1634 | * but the server responds with a HelloRetryRequest. The server | |
1635 | * reads the next record from the client expecting to find a | |
1636 | * plaintext ClientHello but gets a record which appears to be | |
1637 | * application data. The trial decrypt "works" because null | |
1638 | * decryption was applied. We just skip it and move on to the next | |
1639 | * record. | |
1640 | */ | |
38b051a1 TM |
1641 | if (!ossl_early_data_count_ok(s, rr->length, |
1642 | EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) { | |
196f2cbb | 1643 | /* SSLfatal() already called */ |
99dd3740 | 1644 | return -1; |
196f2cbb | 1645 | } |
a832b5ef MC |
1646 | SSL3_RECORD_set_read(rr); |
1647 | goto start; | |
0f113f3e | 1648 | } else { |
c48ffbcc | 1649 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD); |
99dd3740 | 1650 | return -1; |
0f113f3e MC |
1651 | } |
1652 | } | |
0f113f3e | 1653 | } |
d02b48c6 | 1654 | |
14daae5a MC |
1655 | void ssl3_record_sequence_update(unsigned char *seq) |
1656 | { | |
1657 | int i; | |
1658 | ||
1659 | for (i = 7; i >= 0; i--) { | |
1660 | ++seq[i]; | |
1661 | if (seq[i] != 0) | |
1662 | break; | |
1663 | } | |
1664 | } | |
1665 | ||
d45ba43d MC |
1666 | /* |
1667 | * Returns true if the current rrec was sent in SSLv2 backwards compatible | |
1668 | * format and false otherwise. | |
1669 | */ | |
32ec4153 MC |
1670 | int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl) |
1671 | { | |
94777c9c | 1672 | return SSL3_RECORD_is_sslv2_record(&rl->rrec[0]); |
32ec4153 | 1673 | } |
0f113f3e | 1674 | |
d45ba43d MC |
1675 | /* |
1676 | * Returns the length in bytes of the current rrec | |
1677 | */ | |
eda75751 | 1678 | size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl) |
32ec4153 | 1679 | { |
94777c9c | 1680 | return SSL3_RECORD_get_length(&rl->rrec[0]); |
32ec4153 | 1681 | } |