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