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