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