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