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