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