2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
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
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"
20 #if defined(OPENSSL_SMALL_FOOTPRINT) || \
21 !( defined(AES_ASM) && ( \
22 defined(__x86_64) || defined(__x86_64__) || \
23 defined(_M_AMD64) || defined(_M_X64) ) \
25 # undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
26 # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
29 void RECORD_LAYER_init(RECORD_LAYER
*rl
, SSL_CONNECTION
*s
)
32 RECORD_LAYER_set_first_record(&s
->rlayer
);
33 SSL3_RECORD_clear(rl
->rrec
, SSL_MAX_PIPELINES
);
36 void RECORD_LAYER_clear(RECORD_LAYER
*rl
)
38 rl
->rstate
= SSL_ST_READ_HEADER
;
41 * Do I need to clear read_ahead? As far as I can tell read_ahead did not
42 * previously get reset by SSL_clear...so I'll keep it that way..but is
47 rl
->packet_length
= 0;
49 memset(rl
->handshake_fragment
, 0, sizeof(rl
->handshake_fragment
));
50 rl
->handshake_fragment_len
= 0;
56 SSL3_BUFFER_clear(&rl
->rbuf
);
57 ssl3_release_write_buffer(rl
->s
);
59 SSL3_RECORD_clear(rl
->rrec
, SSL_MAX_PIPELINES
);
61 RECORD_LAYER_reset_read_sequence(rl
);
62 RECORD_LAYER_reset_write_sequence(rl
);
65 DTLS_RECORD_LAYER_clear(rl
);
68 void RECORD_LAYER_release(RECORD_LAYER
*rl
)
70 if (SSL3_BUFFER_is_initialised(&rl
->rbuf
))
71 ssl3_release_read_buffer(rl
->s
);
72 if (rl
->numwpipes
> 0)
73 ssl3_release_write_buffer(rl
->s
);
74 SSL3_RECORD_release(rl
->rrec
, SSL_MAX_PIPELINES
);
77 /* Checks if we have unprocessed read ahead data pending */
78 int RECORD_LAYER_read_pending(const RECORD_LAYER
*rl
)
80 return SSL3_BUFFER_get_left(&rl
->rbuf
) != 0;
83 /* Checks if we have decrypted unread record data pending */
84 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER
*rl
)
86 size_t curr_rec
= 0, num_recs
= RECORD_LAYER_get_numrpipes(rl
);
87 const SSL3_RECORD
*rr
= rl
->rrec
;
89 while (curr_rec
< num_recs
&& SSL3_RECORD_is_read(&rr
[curr_rec
]))
92 return curr_rec
< num_recs
;
95 int RECORD_LAYER_write_pending(const RECORD_LAYER
*rl
)
97 return (rl
->numwpipes
> 0)
98 && SSL3_BUFFER_get_left(&rl
->wbuf
[rl
->numwpipes
- 1]) != 0;
101 void RECORD_LAYER_reset_read_sequence(RECORD_LAYER
*rl
)
103 memset(rl
->read_sequence
, 0, sizeof(rl
->read_sequence
));
106 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER
*rl
)
108 memset(rl
->write_sequence
, 0, sizeof(rl
->write_sequence
));
111 size_t ssl3_pending(const SSL
*s
)
114 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
119 if (sc
->rlayer
.rstate
== SSL_ST_READ_BODY
)
122 for (i
= 0; i
< RECORD_LAYER_get_numrpipes(&sc
->rlayer
); i
++) {
123 if (SSL3_RECORD_get_type(&sc
->rlayer
.rrec
[i
])
124 != SSL3_RT_APPLICATION_DATA
)
126 num
+= SSL3_RECORD_get_length(&sc
->rlayer
.rrec
[i
]);
132 void SSL_CTX_set_default_read_buffer_len(SSL_CTX
*ctx
, size_t len
)
134 ctx
->default_read_buf_len
= len
;
137 void SSL_set_default_read_buffer_len(SSL
*s
, size_t len
)
139 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
144 SSL3_BUFFER_set_default_len(RECORD_LAYER_get_rbuf(&sc
->rlayer
), len
);
147 const char *SSL_rstate_string_long(const SSL
*s
)
149 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
154 switch (sc
->rlayer
.rstate
) {
155 case SSL_ST_READ_HEADER
:
156 return "read header";
157 case SSL_ST_READ_BODY
:
159 case SSL_ST_READ_DONE
:
166 const char *SSL_rstate_string(const SSL
*s
)
168 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
173 switch (sc
->rlayer
.rstate
) {
174 case SSL_ST_READ_HEADER
:
176 case SSL_ST_READ_BODY
:
178 case SSL_ST_READ_DONE
:
186 * Return values are as per SSL_read()
188 int ssl3_read_n(SSL_CONNECTION
*s
, size_t n
, size_t max
, int extend
,
189 int clearold
, size_t *readbytes
)
192 * If extend == 0, obtain new n-byte packet; if extend == 1, increase
193 * packet by another n bytes. The packet will be in the sub-array of
194 * s->rlayer.rbuf.buf specified by s->rlayer.packet and
195 * s->rlayer.packet_length. (If s->rlayer.read_ahead is set, 'max' bytes may
196 * be stored in rbuf [plus s->rlayer.packet_length bytes if extend == 1].)
197 * if clearold == 1, move the packet to the start of the buffer; if
198 * clearold == 0 then leave any old packets where they were
200 size_t len
, left
, align
= 0;
207 rb
= &s
->rlayer
.rbuf
;
209 if (!ssl3_setup_read_buffer(s
)) {
210 /* SSLfatal() already called */
215 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
216 align
= (size_t)rb
->buf
+ SSL3_RT_HEADER_LENGTH
;
217 align
= SSL3_ALIGN_PAYLOAD
- 1 - ((align
- 1) % SSL3_ALIGN_PAYLOAD
);
221 /* start with empty packet ... */
224 else if (align
!= 0 && left
>= SSL3_RT_HEADER_LENGTH
) {
226 * check if next packet length is large enough to justify payload
229 pkt
= rb
->buf
+ rb
->offset
;
230 if (pkt
[0] == SSL3_RT_APPLICATION_DATA
231 && (pkt
[3] << 8 | pkt
[4]) >= 128) {
233 * Note that even if packet is corrupted and its length field
234 * is insane, we can only be led to wrong decision about
235 * whether memmove will occur or not. Header values has no
236 * effect on memmove arguments and therefore no buffer
237 * overrun can be triggered.
239 memmove(rb
->buf
+ align
, pkt
, left
);
243 s
->rlayer
.packet
= rb
->buf
+ rb
->offset
;
244 s
->rlayer
.packet_length
= 0;
245 /* ... now we can act as if 'extend' was set */
248 len
= s
->rlayer
.packet_length
;
249 pkt
= rb
->buf
+ align
;
251 * Move any available bytes to front of buffer: 'len' bytes already
252 * pointed to by 'packet', 'left' extra ones at the end
254 if (s
->rlayer
.packet
!= pkt
&& clearold
== 1) {
255 memmove(pkt
, s
->rlayer
.packet
, len
+ left
);
256 s
->rlayer
.packet
= pkt
;
257 rb
->offset
= len
+ align
;
261 * For DTLS/UDP reads should not span multiple packets because the read
262 * operation returns the whole packet at once (as long as it fits into
265 if (SSL_CONNECTION_IS_DTLS(s
)) {
266 if (left
== 0 && extend
)
268 if (left
> 0 && n
> left
)
272 /* if there is enough in the buffer from a previous read, take some */
274 s
->rlayer
.packet_length
+= n
;
281 /* else we need to read more data */
283 if (n
> rb
->len
- rb
->offset
) {
284 /* does not happen */
285 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
290 * Ktls always reads full records.
291 * Also, we always act like read_ahead is set for DTLS.
293 if (!BIO_get_ktls_recv(s
->rbio
) && !s
->rlayer
.read_ahead
294 && !SSL_CONNECTION_IS_DTLS(s
)) {
295 /* ignore max parameter */
300 if (max
> rb
->len
- rb
->offset
)
301 max
= rb
->len
- rb
->offset
;
309 * Now we have len+left bytes at the front of s->s3.rbuf.buf and
310 * need to read in more until we have len+n (up to len+max if
315 if (s
->rbio
!= NULL
) {
316 s
->rwstate
= SSL_READING
;
317 ret
= BIO_read(s
->rbio
, pkt
+ len
+ left
, max
- left
);
321 && !BIO_should_retry(s
->rbio
)
322 && BIO_eof(s
->rbio
)) {
323 if (s
->options
& SSL_OP_IGNORE_UNEXPECTED_EOF
) {
324 SSL_set_shutdown(SSL_CONNECTION_GET_SSL(s
),
325 SSL_RECEIVED_SHUTDOWN
);
326 s
->s3
.warn_alert
= SSL_AD_CLOSE_NOTIFY
;
328 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
329 SSL_R_UNEXPECTED_EOF_WHILE_READING
);
333 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_READ_BIO_NOT_SET
);
339 if ((s
->mode
& SSL_MODE_RELEASE_BUFFERS
) != 0
340 && !SSL_CONNECTION_IS_DTLS(s
))
342 ssl3_release_read_buffer(s
);
347 * reads should *never* span multiple packets for DTLS because the
348 * underlying transport protocol is message oriented as opposed to
349 * byte oriented as in the TLS case.
351 if (SSL_CONNECTION_IS_DTLS(s
)) {
353 n
= left
; /* makes the while condition false */
357 /* done reading, now the book-keeping */
360 s
->rlayer
.packet_length
+= n
;
361 s
->rwstate
= SSL_NOTHING
;
367 * Call this to write data in records of type 'type' It will return <= 0 if
368 * not all data has been sent or non-blocking IO.
370 int ssl3_write_bytes(SSL
*ssl
, int type
, const void *buf_
, size_t len
,
373 const unsigned char *buf
= buf_
;
375 size_t n
, max_send_fragment
, split_send_fragment
, maxpipes
;
376 #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
382 SSL_CONNECTION
*s
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
387 wb
= &s
->rlayer
.wbuf
[0];
388 s
->rwstate
= SSL_NOTHING
;
389 tot
= s
->rlayer
.wnum
;
391 * ensure that if we end up with a smaller value of data to write out
392 * than the original len from a write which didn't complete for
393 * non-blocking I/O and also somehow ended up avoiding the check for
394 * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
395 * possible to end up with (len-tot) as a large number that will then
396 * promptly send beyond the end of the users buffer ... so we trap and
397 * report the error in a way the user will notice
399 if ((len
< s
->rlayer
.wnum
)
400 || ((wb
->left
!= 0) && (len
< (s
->rlayer
.wnum
+ s
->rlayer
.wpend_tot
)))) {
401 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_LENGTH
);
405 if (s
->early_data_state
== SSL_EARLY_DATA_WRITING
406 && !ossl_early_data_count_ok(s
, len
, 0, 1)) {
407 /* SSLfatal() already called */
414 * If we are supposed to be sending a KeyUpdate or NewSessionTicket then go
415 * into init unless we have writes pending - in which case we should finish
418 if (wb
->left
== 0 && (s
->key_update
!= SSL_KEY_UPDATE_NONE
419 || s
->ext
.extra_tickets_expected
> 0))
420 ossl_statem_set_in_init(s
, 1);
423 * When writing early data on the server side we could be "in_init" in
424 * between receiving the EoED and the CF - but we don't want to handle those
427 if (SSL_in_init(ssl
) && !ossl_statem_get_in_handshake(s
)
428 && s
->early_data_state
!= SSL_EARLY_DATA_UNAUTH_WRITING
) {
429 i
= s
->handshake_func(ssl
);
430 /* SSLfatal() already called */
439 * first check if there is a SSL3_BUFFER still being written out. This
440 * will happen with non blocking IO
443 /* SSLfatal() already called if appropriate */
444 i
= ssl3_write_pending(s
, type
, &buf
[tot
], s
->rlayer
.wpend_tot
,
447 /* XXX should we ssl3_release_write_buffer if i<0? */
448 s
->rlayer
.wnum
= tot
;
451 tot
+= tmpwrit
; /* this might be last fragment */
453 #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
455 * Depending on platform multi-block can deliver several *times*
456 * better performance. Downside is that it has to allocate
457 * jumbo buffer to accommodate up to 8 records, but the
458 * compromise is considered worthy.
460 if (type
== SSL3_RT_APPLICATION_DATA
461 && len
>= 4 * (max_send_fragment
= ssl_get_max_send_fragment(s
))
462 && s
->compress
== NULL
463 && s
->msg_callback
== NULL
465 && SSL_USE_EXPLICIT_IV(s
)
466 && !BIO_get_ktls_send(s
->wbio
)
467 && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s
->enc_write_ctx
))
468 & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
) != 0) {
469 unsigned char aad
[13];
470 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param
;
474 /* minimize address aliasing conflicts */
475 if ((max_send_fragment
& 0xfff) == 0)
476 max_send_fragment
-= 512;
478 if (tot
== 0 || wb
->buf
== NULL
) { /* allocate jumbo buffer */
479 ssl3_release_write_buffer(s
);
481 packlen
= EVP_CIPHER_CTX_ctrl(s
->enc_write_ctx
,
482 EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
,
483 (int)max_send_fragment
, NULL
);
485 if (len
>= 8 * max_send_fragment
)
490 if (!ssl3_setup_write_buffer(s
, 1, packlen
)) {
491 /* SSLfatal() already called */
494 } else if (tot
== len
) { /* done? */
495 /* free jumbo buffer */
496 ssl3_release_write_buffer(s
);
503 if (n
< 4 * max_send_fragment
) {
504 /* free jumbo buffer */
505 ssl3_release_write_buffer(s
);
509 if (s
->s3
.alert_dispatch
) {
510 i
= ssl
->method
->ssl_dispatch_alert(ssl
);
512 /* SSLfatal() already called if appropriate */
513 s
->rlayer
.wnum
= tot
;
518 if (n
>= 8 * max_send_fragment
)
519 nw
= max_send_fragment
* (mb_param
.interleave
= 8);
521 nw
= max_send_fragment
* (mb_param
.interleave
= 4);
523 memcpy(aad
, s
->rlayer
.write_sequence
, 8);
525 aad
[9] = (unsigned char)(s
->version
>> 8);
526 aad
[10] = (unsigned char)(s
->version
);
533 packleni
= EVP_CIPHER_CTX_ctrl(s
->enc_write_ctx
,
534 EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
,
535 sizeof(mb_param
), &mb_param
);
536 packlen
= (size_t)packleni
;
537 if (packleni
<= 0 || packlen
> wb
->len
) { /* never happens */
538 /* free jumbo buffer */
539 ssl3_release_write_buffer(s
);
543 mb_param
.out
= wb
->buf
;
544 mb_param
.inp
= &buf
[tot
];
547 if (EVP_CIPHER_CTX_ctrl(s
->enc_write_ctx
,
548 EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
,
549 sizeof(mb_param
), &mb_param
) <= 0)
552 s
->rlayer
.write_sequence
[7] += mb_param
.interleave
;
553 if (s
->rlayer
.write_sequence
[7] < mb_param
.interleave
) {
555 while (j
>= 0 && (++s
->rlayer
.write_sequence
[j
--]) == 0) ;
561 s
->rlayer
.wpend_tot
= nw
;
562 s
->rlayer
.wpend_buf
= &buf
[tot
];
563 s
->rlayer
.wpend_type
= type
;
564 s
->rlayer
.wpend_ret
= nw
;
566 i
= ssl3_write_pending(s
, type
, &buf
[tot
], nw
, &tmpwrit
);
568 /* SSLfatal() already called if appropriate */
569 if (i
< 0 && (!s
->wbio
|| !BIO_should_retry(s
->wbio
))) {
570 /* free jumbo buffer */
571 ssl3_release_write_buffer(s
);
573 s
->rlayer
.wnum
= tot
;
577 /* free jumbo buffer */
578 ssl3_release_write_buffer(s
);
579 *written
= tot
+ tmpwrit
;
586 #endif /* !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK */
587 if (tot
== len
) { /* done? */
588 if (s
->mode
& SSL_MODE_RELEASE_BUFFERS
&& !SSL_CONNECTION_IS_DTLS(s
))
589 ssl3_release_write_buffer(s
);
597 max_send_fragment
= ssl_get_max_send_fragment(s
);
598 split_send_fragment
= ssl_get_split_send_fragment(s
);
600 * If max_pipelines is 0 then this means "undefined" and we default to
601 * 1 pipeline. Similarly if the cipher does not support pipelined
602 * processing then we also only use 1 pipeline, or if we're not using
605 maxpipes
= s
->max_pipelines
;
606 if (maxpipes
> SSL_MAX_PIPELINES
) {
608 * We should have prevented this when we set max_pipelines so we
611 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
615 || s
->enc_write_ctx
== NULL
616 || (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s
->enc_write_ctx
))
617 & EVP_CIPH_FLAG_PIPELINE
) == 0
618 || !SSL_USE_EXPLICIT_IV(s
))
620 if (max_send_fragment
== 0
621 || split_send_fragment
== 0
622 || split_send_fragment
> max_send_fragment
) {
624 * We should have prevented this when we set/get the split and max send
625 * fragments so we shouldn't get here
627 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
632 size_t pipelens
[SSL_MAX_PIPELINES
], tmppipelen
, remain
;
638 numpipes
= ((n
- 1) / split_send_fragment
) + 1;
639 if (numpipes
> maxpipes
)
642 if (n
/ numpipes
>= max_send_fragment
) {
644 * We have enough data to completely fill all available
647 for (j
= 0; j
< numpipes
; j
++) {
648 pipelens
[j
] = max_send_fragment
;
651 /* We can partially fill all available pipelines */
652 tmppipelen
= n
/ numpipes
;
653 remain
= n
% numpipes
;
654 for (j
= 0; j
< numpipes
; j
++) {
655 pipelens
[j
] = tmppipelen
;
661 i
= do_ssl3_write(s
, type
, &(buf
[tot
]), pipelens
, numpipes
, 0,
664 /* SSLfatal() already called if appropriate */
665 /* XXX should we ssl3_release_write_buffer if i<0? */
666 s
->rlayer
.wnum
= tot
;
671 (type
== SSL3_RT_APPLICATION_DATA
&&
672 (s
->mode
& SSL_MODE_ENABLE_PARTIAL_WRITE
))) {
674 * next chunk of data should get another prepended empty fragment
675 * in ciphersuites with known-IV weakness:
677 s
->s3
.empty_fragment_done
= 0;
680 && (s
->mode
& SSL_MODE_RELEASE_BUFFERS
) != 0
681 && !SSL_CONNECTION_IS_DTLS(s
))
682 ssl3_release_write_buffer(s
);
684 *written
= tot
+ tmpwrit
;
693 int do_ssl3_write(SSL_CONNECTION
*s
, int type
, const unsigned char *buf
,
694 size_t *pipelens
, size_t numpipes
,
695 int create_empty_fragment
, size_t *written
)
697 WPACKET pkt
[SSL_MAX_PIPELINES
];
698 SSL3_RECORD wr
[SSL_MAX_PIPELINES
];
701 unsigned char *recordstart
;
702 int i
, mac_size
, clear
= 0;
703 size_t prefix_len
= 0;
708 size_t totlen
= 0, len
, wpinited
= 0;
711 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
713 for (j
= 0; j
< numpipes
; j
++)
714 totlen
+= pipelens
[j
];
716 * first check if there is a SSL3_BUFFER still being written out. This
717 * will happen with non blocking IO
719 if (RECORD_LAYER_write_pending(&s
->rlayer
)) {
720 /* Calls SSLfatal() as required */
721 return ssl3_write_pending(s
, type
, buf
, totlen
, written
);
724 /* If we have an alert to send, lets send it */
725 if (s
->s3
.alert_dispatch
) {
726 i
= ssl
->method
->ssl_dispatch_alert(ssl
);
728 /* SSLfatal() already called if appropriate */
731 /* if it went, fall through and send more stuff */
734 if (s
->rlayer
.numwpipes
< numpipes
) {
735 if (!ssl3_setup_write_buffer(s
, numpipes
, 0)) {
736 /* SSLfatal() already called */
741 if (totlen
== 0 && !create_empty_fragment
)
747 || (s
->enc_write_ctx
== NULL
)
748 || (EVP_MD_CTX_get0_md(s
->write_hash
) == NULL
)) {
749 clear
= s
->enc_write_ctx
? 0 : 1; /* must be AEAD cipher */
752 mac_size
= EVP_MD_CTX_get_size(s
->write_hash
);
754 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
760 * 'create_empty_fragment' is true only when this function calls itself
762 if (!clear
&& !create_empty_fragment
&& !s
->s3
.empty_fragment_done
) {
764 * countermeasure against known-IV weakness in CBC ciphersuites (see
765 * http://www.openssl.org/~bodo/tls-cbc.txt)
768 if (s
->s3
.need_empty_fragments
&& type
== SSL3_RT_APPLICATION_DATA
) {
770 * recursive function call with 'create_empty_fragment' set; this
771 * prepares and buffers the data for an empty fragment (these
772 * 'prefix_len' bytes are sent out later together with the actual
775 size_t tmppipelen
= 0;
778 ret
= do_ssl3_write(s
, type
, buf
, &tmppipelen
, 1, 1, &prefix_len
);
780 /* SSLfatal() already called if appropriate */
785 (SSL3_RT_HEADER_LENGTH
+ SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
)) {
786 /* insufficient space */
787 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
792 s
->s3
.empty_fragment_done
= 1;
795 using_ktls
= BIO_get_ktls_send(s
->wbio
);
798 * ktls doesn't modify the buffer, but to avoid a warning we need to
799 * discard the const qualifier.
800 * This doesn't leak memory because the buffers have been released when
803 SSL3_BUFFER_set_buf(&s
->rlayer
.wbuf
[0], (unsigned char *)buf
);
804 SSL3_BUFFER_set_offset(&s
->rlayer
.wbuf
[0], 0);
805 SSL3_BUFFER_set_app_buffer(&s
->rlayer
.wbuf
[0], 1);
806 goto wpacket_init_complete
;
809 if (create_empty_fragment
) {
810 wb
= &s
->rlayer
.wbuf
[0];
811 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
813 * extra fragment would be couple of cipher blocks, which would be
814 * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real
815 * payload, then we can just pretend we simply have two headers.
817 align
= (size_t)SSL3_BUFFER_get_buf(wb
) + 2 * SSL3_RT_HEADER_LENGTH
;
818 align
= SSL3_ALIGN_PAYLOAD
- 1 - ((align
- 1) % SSL3_ALIGN_PAYLOAD
);
820 SSL3_BUFFER_set_offset(wb
, align
);
821 if (!WPACKET_init_static_len(&pkt
[0], SSL3_BUFFER_get_buf(wb
),
822 SSL3_BUFFER_get_len(wb
), 0)
823 || !WPACKET_allocate_bytes(&pkt
[0], align
, NULL
)) {
824 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
828 } else if (prefix_len
) {
829 wb
= &s
->rlayer
.wbuf
[0];
830 if (!WPACKET_init_static_len(&pkt
[0],
831 SSL3_BUFFER_get_buf(wb
),
832 SSL3_BUFFER_get_len(wb
), 0)
833 || !WPACKET_allocate_bytes(&pkt
[0], SSL3_BUFFER_get_offset(wb
)
834 + prefix_len
, NULL
)) {
835 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
840 for (j
= 0; j
< numpipes
; j
++) {
843 wb
= &s
->rlayer
.wbuf
[j
];
844 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
845 align
= (size_t)SSL3_BUFFER_get_buf(wb
) + SSL3_RT_HEADER_LENGTH
;
846 align
= SSL3_ALIGN_PAYLOAD
- 1 - ((align
- 1) % SSL3_ALIGN_PAYLOAD
);
848 SSL3_BUFFER_set_offset(wb
, align
);
849 if (!WPACKET_init_static_len(thispkt
, SSL3_BUFFER_get_buf(wb
),
850 SSL3_BUFFER_get_len(wb
), 0)
851 || !WPACKET_allocate_bytes(thispkt
, align
, NULL
)) {
852 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
859 /* Explicit IV length, block ciphers appropriate version flag */
860 if (s
->enc_write_ctx
&& SSL_USE_EXPLICIT_IV(s
)
861 && !SSL_CONNECTION_TREAT_AS_TLS13(s
)) {
862 int mode
= EVP_CIPHER_CTX_get_mode(s
->enc_write_ctx
);
863 if (mode
== EVP_CIPH_CBC_MODE
) {
864 eivlen
= EVP_CIPHER_CTX_get_iv_length(s
->enc_write_ctx
);
867 } else if (mode
== EVP_CIPH_GCM_MODE
) {
868 /* Need explicit part of IV for GCM mode */
869 eivlen
= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
870 } else if (mode
== EVP_CIPH_CCM_MODE
) {
871 eivlen
= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
875 wpacket_init_complete
:
878 /* Clear our SSL3_RECORD structures */
879 memset(wr
, 0, sizeof(wr
));
880 for (j
= 0; j
< numpipes
; j
++) {
881 unsigned int version
= (s
->version
== TLS1_3_VERSION
) ? TLS1_2_VERSION
883 unsigned char *compressdata
= NULL
;
885 unsigned int rectype
;
891 * In TLSv1.3, once encrypting, we always use application data for the
894 if (SSL_CONNECTION_TREAT_AS_TLS13(s
)
895 && s
->enc_write_ctx
!= NULL
896 && (s
->statem
.enc_write_state
!= ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
897 || type
!= SSL3_RT_ALERT
))
898 rectype
= SSL3_RT_APPLICATION_DATA
;
901 SSL3_RECORD_set_type(thiswr
, rectype
);
904 * Some servers hang if initial client hello is larger than 256 bytes
905 * and record version number > TLS 1.0
907 if (SSL_get_state(ssl
) == TLS_ST_CW_CLNT_HELLO
909 && TLS1_get_version(ssl
) > TLS1_VERSION
910 && s
->hello_retry_request
== SSL_HRR_NONE
)
911 version
= TLS1_VERSION
;
912 SSL3_RECORD_set_rec_version(thiswr
, version
);
914 maxcomplen
= pipelens
[j
];
915 if (s
->compress
!= NULL
)
916 maxcomplen
+= SSL3_RT_MAX_COMPRESSED_OVERHEAD
;
919 * When using offload kernel will write the header.
920 * Otherwise write the header now
923 && (!WPACKET_put_bytes_u8(thispkt
, rectype
)
924 || !WPACKET_put_bytes_u16(thispkt
, version
)
925 || !WPACKET_start_sub_packet_u16(thispkt
)
927 && !WPACKET_allocate_bytes(thispkt
, eivlen
, NULL
))
929 && !WPACKET_reserve_bytes(thispkt
, maxcomplen
,
931 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
935 /* lets setup the record stuff. */
936 SSL3_RECORD_set_data(thiswr
, compressdata
);
937 SSL3_RECORD_set_length(thiswr
, pipelens
[j
]);
938 SSL3_RECORD_set_input(thiswr
, (unsigned char *)&buf
[totlen
]);
939 totlen
+= pipelens
[j
];
942 * we now 'read' from thiswr->input, thiswr->length bytes into
946 /* first we compress */
947 if (s
->compress
!= NULL
) {
948 if (!ssl3_do_compress(s
, thiswr
)
949 || !WPACKET_allocate_bytes(thispkt
, thiswr
->length
, NULL
)) {
950 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_COMPRESSION_FAILURE
);
955 SSL3_RECORD_reset_data(&wr
[j
]);
957 if (!WPACKET_memcpy(thispkt
, thiswr
->input
, thiswr
->length
)) {
958 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
961 SSL3_RECORD_reset_input(&wr
[j
]);
965 if (SSL_CONNECTION_TREAT_AS_TLS13(s
)
967 && s
->enc_write_ctx
!= NULL
968 && (s
->statem
.enc_write_state
!= ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
969 || type
!= SSL3_RT_ALERT
)) {
970 size_t rlen
, max_send_fragment
;
972 if (!WPACKET_put_bytes_u8(thispkt
, type
)) {
973 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
976 SSL3_RECORD_add_length(thiswr
, 1);
978 /* Add TLS1.3 padding */
979 max_send_fragment
= ssl_get_max_send_fragment(s
);
980 rlen
= SSL3_RECORD_get_length(thiswr
);
981 if (rlen
< max_send_fragment
) {
983 size_t max_padding
= max_send_fragment
- rlen
;
984 if (s
->record_padding_cb
!= NULL
) {
985 padding
= s
->record_padding_cb(ssl
, type
, rlen
, s
->record_padding_arg
);
986 } else if (s
->block_padding
> 0) {
987 size_t mask
= s
->block_padding
- 1;
990 /* optimize for power of 2 */
991 if ((s
->block_padding
& mask
) == 0)
992 remainder
= rlen
& mask
;
994 remainder
= rlen
% s
->block_padding
;
995 /* don't want to add a block of padding if we don't have to */
999 padding
= s
->block_padding
- remainder
;
1002 /* do not allow the record to exceed max plaintext length */
1003 if (padding
> max_padding
)
1004 padding
= max_padding
;
1005 if (!WPACKET_memset(thispkt
, 0, padding
)) {
1006 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1007 ERR_R_INTERNAL_ERROR
);
1010 SSL3_RECORD_add_length(thiswr
, padding
);
1016 * we should still have the output to thiswr->data and the input from
1017 * wr->input. Length should be thiswr->length. thiswr->data still points
1021 if (!using_ktls
&& !SSL_WRITE_ETM(s
) && mac_size
!= 0) {
1024 if (!WPACKET_allocate_bytes(thispkt
, mac_size
, &mac
)
1025 || !ssl
->method
->ssl3_enc
->mac(s
, thiswr
, mac
, 1)) {
1026 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1032 * Reserve some bytes for any growth that may occur during encryption.
1033 * This will be at most one cipher block or the tag length if using
1034 * AEAD. SSL_RT_MAX_CIPHER_BLOCK_SIZE covers either case.
1037 if (!WPACKET_reserve_bytes(thispkt
,
1038 SSL_RT_MAX_CIPHER_BLOCK_SIZE
,
1041 * We also need next the amount of bytes written to this
1044 || !WPACKET_get_length(thispkt
, &len
)) {
1045 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1049 /* Get a pointer to the start of this record excluding header */
1050 recordstart
= WPACKET_get_curr(thispkt
) - len
;
1051 SSL3_RECORD_set_data(thiswr
, recordstart
);
1052 SSL3_RECORD_reset_input(thiswr
);
1053 SSL3_RECORD_set_length(thiswr
, len
);
1057 if (s
->statem
.enc_write_state
== ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
) {
1059 * We haven't actually negotiated the version yet, but we're trying to
1060 * send early data - so we need to use the tls13enc function.
1062 if (tls13_enc(s
, wr
, numpipes
, 1, NULL
, mac_size
) < 1) {
1063 if (!ossl_statem_in_error(s
)) {
1064 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1070 if (ssl
->method
->ssl3_enc
->enc(s
, wr
, numpipes
, 1, NULL
,
1072 if (!ossl_statem_in_error(s
)) {
1073 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1080 for (j
= 0; j
< numpipes
; j
++) {
1089 /* Allocate bytes for the encryption overhead */
1090 if (!WPACKET_get_length(thispkt
, &origlen
)
1091 /* Encryption should never shrink the data! */
1092 || origlen
> thiswr
->length
1093 || (thiswr
->length
> origlen
1094 && !WPACKET_allocate_bytes(thispkt
,
1095 thiswr
->length
- origlen
,
1097 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1100 if (SSL_WRITE_ETM(s
) && mac_size
!= 0) {
1103 if (!WPACKET_allocate_bytes(thispkt
, mac_size
, &mac
)
1104 || !ssl
->method
->ssl3_enc
->mac(s
, thiswr
, mac
, 1)) {
1105 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1108 SSL3_RECORD_add_length(thiswr
, mac_size
);
1111 if (!WPACKET_get_length(thispkt
, &len
)
1112 || !WPACKET_close(thispkt
)) {
1113 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1117 if (s
->msg_callback
) {
1118 recordstart
= WPACKET_get_curr(thispkt
) - len
1119 - SSL3_RT_HEADER_LENGTH
;
1120 s
->msg_callback(1, thiswr
->rec_version
, SSL3_RT_HEADER
, recordstart
,
1121 SSL3_RT_HEADER_LENGTH
, ssl
,
1122 s
->msg_callback_arg
);
1124 if (SSL_CONNECTION_TREAT_AS_TLS13(s
) && s
->enc_write_ctx
!= NULL
) {
1125 unsigned char ctype
= type
;
1127 s
->msg_callback(1, thiswr
->rec_version
, SSL3_RT_INNER_CONTENT_TYPE
,
1128 &ctype
, 1, ssl
, s
->msg_callback_arg
);
1132 if (!WPACKET_finish(thispkt
)) {
1133 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1137 /* header is added by the kernel when using offload */
1138 SSL3_RECORD_add_length(thiswr
, SSL3_RT_HEADER_LENGTH
);
1140 if (create_empty_fragment
) {
1142 * we are in a recursive call; just return the length, don't write
1146 /* We should never be pipelining an empty fragment!! */
1147 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1150 *written
= SSL3_RECORD_get_length(thiswr
);
1156 * we should now have thiswr->data pointing to the encrypted data, which
1157 * is thiswr->length long
1159 SSL3_RECORD_set_type(thiswr
, type
); /* not needed but helps for
1162 /* now let's set up wb */
1163 SSL3_BUFFER_set_left(&s
->rlayer
.wbuf
[j
],
1164 prefix_len
+ SSL3_RECORD_get_length(thiswr
));
1168 * memorize arguments so that ssl3_write_pending can detect bad write
1171 s
->rlayer
.wpend_tot
= totlen
;
1172 s
->rlayer
.wpend_buf
= buf
;
1173 s
->rlayer
.wpend_type
= type
;
1174 s
->rlayer
.wpend_ret
= totlen
;
1176 /* we now just need to write the buffer */
1177 return ssl3_write_pending(s
, type
, buf
, totlen
, written
);
1179 for (j
= 0; j
< wpinited
; j
++)
1180 WPACKET_cleanup(&pkt
[j
]);
1184 /* if SSL3_BUFFER_get_left() != 0, we need to call this
1186 * Return values are as per SSL_write()
1188 int ssl3_write_pending(SSL_CONNECTION
*s
, int type
, const unsigned char *buf
,
1189 size_t len
, size_t *written
)
1192 SSL3_BUFFER
*wb
= s
->rlayer
.wbuf
;
1196 if ((s
->rlayer
.wpend_tot
> len
)
1197 || (!(s
->mode
& SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
)
1198 && (s
->rlayer
.wpend_buf
!= buf
))
1199 || (s
->rlayer
.wpend_type
!= type
)) {
1200 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_WRITE_RETRY
);
1205 /* Loop until we find a buffer we haven't written out yet */
1206 if (SSL3_BUFFER_get_left(&wb
[currbuf
]) == 0
1207 && currbuf
< s
->rlayer
.numwpipes
- 1) {
1212 if (s
->wbio
!= NULL
) {
1213 s
->rwstate
= SSL_WRITING
;
1216 * To prevent coalescing of control and data messages,
1217 * such as in buffer_write, we flush the BIO
1219 if (BIO_get_ktls_send(s
->wbio
) && type
!= SSL3_RT_APPLICATION_DATA
) {
1220 i
= BIO_flush(s
->wbio
);
1223 BIO_set_ktls_ctrl_msg(s
->wbio
, type
);
1225 i
= BIO_write(s
->wbio
, (char *)
1226 &(SSL3_BUFFER_get_buf(&wb
[currbuf
])
1227 [SSL3_BUFFER_get_offset(&wb
[currbuf
])]),
1228 (unsigned int)SSL3_BUFFER_get_left(&wb
[currbuf
]));
1232 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BIO_NOT_SET
);
1237 * When an empty fragment is sent on a connection using KTLS,
1238 * it is sent as a write of zero bytes. If this zero byte
1239 * write succeeds, i will be 0 rather than a non-zero value.
1240 * Treat i == 0 as success rather than an error for zero byte
1241 * writes to permit this case.
1243 if (i
>= 0 && tmpwrit
== SSL3_BUFFER_get_left(&wb
[currbuf
])) {
1244 SSL3_BUFFER_set_left(&wb
[currbuf
], 0);
1245 SSL3_BUFFER_add_offset(&wb
[currbuf
], tmpwrit
);
1246 if (currbuf
+ 1 < s
->rlayer
.numwpipes
)
1248 s
->rwstate
= SSL_NOTHING
;
1249 *written
= s
->rlayer
.wpend_ret
;
1251 } else if (i
<= 0) {
1252 if (SSL_CONNECTION_IS_DTLS(s
)) {
1254 * For DTLS, just drop it. That's kind of the whole point in
1255 * using a datagram service
1257 SSL3_BUFFER_set_left(&wb
[currbuf
], 0);
1261 SSL3_BUFFER_add_offset(&wb
[currbuf
], tmpwrit
);
1262 SSL3_BUFFER_sub_left(&wb
[currbuf
], tmpwrit
);
1267 * Return up to 'len' payload bytes received in 'type' records.
1268 * 'type' is one of the following:
1270 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
1271 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
1272 * - 0 (during a shutdown, no data has to be returned)
1274 * If we don't have stored data to work from, read a SSL/TLS record first
1275 * (possibly multiple records if we still don't have anything to return).
1277 * This function must handle any surprises the peer may have for us, such as
1278 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
1279 * messages are treated as if they were handshake messages *if* the |recvd_type|
1280 * argument is non NULL.
1281 * Also if record payloads contain fragments too small to process, we store
1282 * them until there is enough for the respective protocol (the record protocol
1283 * may use arbitrary fragmentation and even interleaving):
1284 * Change cipher spec protocol
1285 * just 1 byte needed, no need for keeping anything stored
1287 * 2 bytes needed (AlertLevel, AlertDescription)
1288 * Handshake protocol
1289 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
1290 * to detect unexpected Client Hello and Hello Request messages
1291 * here, anything else is handled by higher layers
1292 * Application data protocol
1293 * none of our business
1295 int ssl3_read_bytes(SSL
*ssl
, int type
, int *recvd_type
, unsigned char *buf
,
1296 size_t len
, int peek
, size_t *readbytes
)
1299 size_t n
, curr_rec
, num_recs
, totalbytes
;
1302 void (*cb
) (const SSL
*ssl
, int type2
, int val
) = NULL
;
1304 SSL_CONNECTION
*s
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
1306 is_tls13
= SSL_CONNECTION_IS_TLS13(s
);
1308 rbuf
= &s
->rlayer
.rbuf
;
1310 if (!SSL3_BUFFER_is_initialised(rbuf
)) {
1311 /* Not initialized yet */
1312 if (!ssl3_setup_read_buffer(s
)) {
1313 /* SSLfatal() already called */
1318 if ((type
&& (type
!= SSL3_RT_APPLICATION_DATA
)
1319 && (type
!= SSL3_RT_HANDSHAKE
)) || (peek
1321 SSL3_RT_APPLICATION_DATA
))) {
1322 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1326 if ((type
== SSL3_RT_HANDSHAKE
) && (s
->rlayer
.handshake_fragment_len
> 0))
1327 /* (partially) satisfy request from storage */
1329 unsigned char *src
= s
->rlayer
.handshake_fragment
;
1330 unsigned char *dst
= buf
;
1335 while ((len
> 0) && (s
->rlayer
.handshake_fragment_len
> 0)) {
1338 s
->rlayer
.handshake_fragment_len
--;
1341 /* move any remaining fragment bytes: */
1342 for (k
= 0; k
< s
->rlayer
.handshake_fragment_len
; k
++)
1343 s
->rlayer
.handshake_fragment
[k
] = *src
++;
1345 if (recvd_type
!= NULL
)
1346 *recvd_type
= SSL3_RT_HANDSHAKE
;
1353 * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
1356 if (!ossl_statem_get_in_handshake(s
) && SSL_in_init(ssl
)) {
1357 /* type == SSL3_RT_APPLICATION_DATA */
1358 i
= s
->handshake_func(ssl
);
1359 /* SSLfatal() already called */
1366 s
->rwstate
= SSL_NOTHING
;
1369 * For each record 'i' up to |num_recs]
1370 * rr[i].type - is the type of record
1371 * rr[i].data, - data
1372 * rr[i].off, - offset into 'data' for next read
1373 * rr[i].length, - number of bytes.
1375 rr
= s
->rlayer
.rrec
;
1376 num_recs
= RECORD_LAYER_get_numrpipes(&s
->rlayer
);
1379 /* get new records if necessary */
1380 if (num_recs
== 0) {
1381 ret
= ssl3_get_record(s
);
1383 /* SSLfatal() already called if appropriate */
1386 num_recs
= RECORD_LAYER_get_numrpipes(&s
->rlayer
);
1387 if (num_recs
== 0) {
1388 /* Shouldn't happen */
1389 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1393 /* Skip over any records we have already read */
1395 curr_rec
< num_recs
&& SSL3_RECORD_is_read(&rr
[curr_rec
]);
1397 if (curr_rec
== num_recs
) {
1398 RECORD_LAYER_set_numrpipes(&s
->rlayer
, 0);
1402 } while (num_recs
== 0);
1405 if (s
->rlayer
.handshake_fragment_len
> 0
1406 && SSL3_RECORD_get_type(rr
) != SSL3_RT_HANDSHAKE
1407 && SSL_CONNECTION_IS_TLS13(s
)) {
1408 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1409 SSL_R_MIXED_HANDSHAKE_AND_NON_HANDSHAKE_DATA
);
1414 * Reset the count of consecutive warning alerts if we've got a non-empty
1415 * record that isn't an alert.
1417 if (SSL3_RECORD_get_type(rr
) != SSL3_RT_ALERT
1418 && SSL3_RECORD_get_length(rr
) != 0)
1419 s
->rlayer
.alert_count
= 0;
1421 /* we now have a packet which can be read and processed */
1423 if (s
->s3
.change_cipher_spec
/* set when we receive ChangeCipherSpec,
1424 * reset by ssl3_get_finished */
1425 && (SSL3_RECORD_get_type(rr
) != SSL3_RT_HANDSHAKE
)) {
1426 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1427 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED
);
1432 * If the other end has shut down, throw anything we read away (even in
1435 if (s
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
1436 SSL3_RECORD_set_length(rr
, 0);
1437 s
->rwstate
= SSL_NOTHING
;
1441 if (type
== SSL3_RECORD_get_type(rr
)
1442 || (SSL3_RECORD_get_type(rr
) == SSL3_RT_CHANGE_CIPHER_SPEC
1443 && type
== SSL3_RT_HANDSHAKE
&& recvd_type
!= NULL
1446 * SSL3_RT_APPLICATION_DATA or
1447 * SSL3_RT_HANDSHAKE or
1448 * SSL3_RT_CHANGE_CIPHER_SPEC
1451 * make sure that we are not getting application data when we are
1452 * doing a handshake for the first time
1454 if (SSL_in_init(ssl
) && type
== SSL3_RT_APPLICATION_DATA
1455 && s
->enc_read_ctx
== NULL
) {
1456 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_APP_DATA_IN_HANDSHAKE
);
1460 if (type
== SSL3_RT_HANDSHAKE
1461 && SSL3_RECORD_get_type(rr
) == SSL3_RT_CHANGE_CIPHER_SPEC
1462 && s
->rlayer
.handshake_fragment_len
> 0) {
1463 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_CCS_RECEIVED_EARLY
);
1467 if (recvd_type
!= NULL
)
1468 *recvd_type
= SSL3_RECORD_get_type(rr
);
1472 * Mark a zero length record as read. This ensures multiple calls to
1473 * SSL_read() with a zero length buffer will eventually cause
1474 * SSL_pending() to report data as being available.
1476 if (SSL3_RECORD_get_length(rr
) == 0)
1477 SSL3_RECORD_set_read(rr
);
1483 if (len
- totalbytes
> SSL3_RECORD_get_length(rr
))
1484 n
= SSL3_RECORD_get_length(rr
);
1486 n
= len
- totalbytes
;
1488 memcpy(buf
, &(rr
->data
[rr
->off
]), n
);
1491 /* Mark any zero length record as consumed CVE-2016-6305 */
1492 if (SSL3_RECORD_get_length(rr
) == 0)
1493 SSL3_RECORD_set_read(rr
);
1495 if (s
->options
& SSL_OP_CLEANSE_PLAINTEXT
)
1496 OPENSSL_cleanse(&(rr
->data
[rr
->off
]), n
);
1497 SSL3_RECORD_sub_length(rr
, n
);
1498 SSL3_RECORD_add_off(rr
, n
);
1499 if (SSL3_RECORD_get_length(rr
) == 0) {
1500 s
->rlayer
.rstate
= SSL_ST_READ_HEADER
;
1501 SSL3_RECORD_set_off(rr
, 0);
1502 SSL3_RECORD_set_read(rr
);
1505 if (SSL3_RECORD_get_length(rr
) == 0
1506 || (peek
&& n
== SSL3_RECORD_get_length(rr
))) {
1511 } while (type
== SSL3_RT_APPLICATION_DATA
&& curr_rec
< num_recs
1512 && totalbytes
< len
);
1513 if (totalbytes
== 0) {
1514 /* We must have read empty records. Get more data */
1517 if (!peek
&& curr_rec
== num_recs
1518 && (s
->mode
& SSL_MODE_RELEASE_BUFFERS
)
1519 && SSL3_BUFFER_get_left(rbuf
) == 0)
1520 ssl3_release_read_buffer(s
);
1521 *readbytes
= totalbytes
;
1526 * If we get here, then type != rr->type; if we have a handshake message,
1527 * then it was unexpected (Hello Request or Client Hello) or invalid (we
1528 * were actually expecting a CCS).
1532 * Lets just double check that we've not got an SSLv2 record
1534 if (rr
->rec_version
== SSL2_VERSION
) {
1536 * Should never happen. ssl3_get_record() should only give us an SSLv2
1537 * record back if this is the first packet and we are looking for an
1538 * initial ClientHello. Therefore |type| should always be equal to
1539 * |rr->type|. If not then something has gone horribly wrong
1541 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1545 if (ssl
->method
->version
== TLS_ANY_VERSION
1546 && (s
->server
|| rr
->type
!= SSL3_RT_ALERT
)) {
1548 * If we've got this far and still haven't decided on what version
1549 * we're using then this must be a client side alert we're dealing
1550 * with. We shouldn't be receiving anything other than a ClientHello
1551 * if we are a server.
1553 s
->version
= rr
->rec_version
;
1554 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
1559 * s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
1560 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
1563 if (SSL3_RECORD_get_type(rr
) == SSL3_RT_ALERT
) {
1564 unsigned int alert_level
, alert_descr
;
1565 unsigned char *alert_bytes
= SSL3_RECORD_get_data(rr
)
1566 + SSL3_RECORD_get_off(rr
);
1569 if (!PACKET_buf_init(&alert
, alert_bytes
, SSL3_RECORD_get_length(rr
))
1570 || !PACKET_get_1(&alert
, &alert_level
)
1571 || !PACKET_get_1(&alert
, &alert_descr
)
1572 || PACKET_remaining(&alert
) != 0) {
1573 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_INVALID_ALERT
);
1577 if (s
->msg_callback
)
1578 s
->msg_callback(0, s
->version
, SSL3_RT_ALERT
, alert_bytes
, 2, ssl
,
1579 s
->msg_callback_arg
);
1581 if (s
->info_callback
!= NULL
)
1582 cb
= s
->info_callback
;
1583 else if (ssl
->ctx
->info_callback
!= NULL
)
1584 cb
= ssl
->ctx
->info_callback
;
1587 j
= (alert_level
<< 8) | alert_descr
;
1588 cb(ssl
, SSL_CB_READ_ALERT
, j
);
1591 if (alert_level
== SSL3_AL_WARNING
1592 || (is_tls13
&& alert_descr
== SSL_AD_USER_CANCELLED
)) {
1593 s
->s3
.warn_alert
= alert_descr
;
1594 SSL3_RECORD_set_read(rr
);
1596 s
->rlayer
.alert_count
++;
1597 if (s
->rlayer
.alert_count
== MAX_WARN_ALERT_COUNT
) {
1598 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1599 SSL_R_TOO_MANY_WARN_ALERTS
);
1605 * Apart from close_notify the only other warning alert in TLSv1.3
1606 * is user_cancelled - which we just ignore.
1608 if (is_tls13
&& alert_descr
== SSL_AD_USER_CANCELLED
) {
1610 } else if (alert_descr
== SSL_AD_CLOSE_NOTIFY
1611 && (is_tls13
|| alert_level
== SSL3_AL_WARNING
)) {
1612 s
->shutdown
|= SSL_RECEIVED_SHUTDOWN
;
1614 } else if (alert_level
== SSL3_AL_FATAL
|| is_tls13
) {
1615 s
->rwstate
= SSL_NOTHING
;
1616 s
->s3
.fatal_alert
= alert_descr
;
1617 SSLfatal_data(s
, SSL_AD_NO_ALERT
,
1618 SSL_AD_REASON_OFFSET
+ alert_descr
,
1619 "SSL alert number %d", alert_descr
);
1620 s
->shutdown
|= SSL_RECEIVED_SHUTDOWN
;
1621 SSL3_RECORD_set_read(rr
);
1622 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
1624 } else if (alert_descr
== SSL_AD_NO_RENEGOTIATION
) {
1626 * This is a warning but we receive it if we requested
1627 * renegotiation and the peer denied it. Terminate with a fatal
1628 * alert because if application tried to renegotiate it
1629 * presumably had a good reason and expects it to succeed. In
1630 * future we might have a renegotiation where we don't care if
1631 * the peer refused it where we carry on.
1633 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_NO_RENEGOTIATION
);
1635 } else if (alert_level
== SSL3_AL_WARNING
) {
1636 /* We ignore any other warning alert in TLSv1.2 and below */
1640 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_UNKNOWN_ALERT_TYPE
);
1644 if ((s
->shutdown
& SSL_SENT_SHUTDOWN
) != 0) {
1645 if (SSL3_RECORD_get_type(rr
) == SSL3_RT_HANDSHAKE
) {
1649 * We ignore any handshake messages sent to us unless they are
1650 * TLSv1.3 in which case we want to process them. For all other
1651 * handshake messages we can't do anything reasonable with them
1652 * because we are unable to write any response due to having already
1653 * sent close_notify.
1655 if (!SSL_CONNECTION_IS_TLS13(s
)) {
1656 SSL3_RECORD_set_length(rr
, 0);
1657 SSL3_RECORD_set_read(rr
);
1659 if ((s
->mode
& SSL_MODE_AUTO_RETRY
) != 0)
1662 s
->rwstate
= SSL_READING
;
1663 rbio
= SSL_get_rbio(ssl
);
1664 BIO_clear_retry_flags(rbio
);
1665 BIO_set_retry_read(rbio
);
1670 * The peer is continuing to send application data, but we have
1671 * already sent close_notify. If this was expected we should have
1672 * been called via SSL_read() and this would have been handled
1674 * No alert sent because we already sent close_notify
1676 SSL3_RECORD_set_length(rr
, 0);
1677 SSL3_RECORD_set_read(rr
);
1678 SSLfatal(s
, SSL_AD_NO_ALERT
,
1679 SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY
);
1685 * For handshake data we have 'fragment' storage, so fill that so that we
1686 * can process the header at a fixed place. This is done after the
1687 * "SHUTDOWN" code above to avoid filling the fragment storage with data
1688 * that we're just going to discard.
1690 if (SSL3_RECORD_get_type(rr
) == SSL3_RT_HANDSHAKE
) {
1691 size_t dest_maxlen
= sizeof(s
->rlayer
.handshake_fragment
);
1692 unsigned char *dest
= s
->rlayer
.handshake_fragment
;
1693 size_t *dest_len
= &s
->rlayer
.handshake_fragment_len
;
1695 n
= dest_maxlen
- *dest_len
; /* available space in 'dest' */
1696 if (SSL3_RECORD_get_length(rr
) < n
)
1697 n
= SSL3_RECORD_get_length(rr
); /* available bytes */
1699 /* now move 'n' bytes: */
1700 memcpy(dest
+ *dest_len
,
1701 SSL3_RECORD_get_data(rr
) + SSL3_RECORD_get_off(rr
), n
);
1702 SSL3_RECORD_add_off(rr
, n
);
1703 SSL3_RECORD_sub_length(rr
, n
);
1705 if (SSL3_RECORD_get_length(rr
) == 0)
1706 SSL3_RECORD_set_read(rr
);
1708 if (*dest_len
< dest_maxlen
)
1709 goto start
; /* fragment was too small */
1712 if (SSL3_RECORD_get_type(rr
) == SSL3_RT_CHANGE_CIPHER_SPEC
) {
1713 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_CCS_RECEIVED_EARLY
);
1718 * Unexpected handshake message (ClientHello, NewSessionTicket (TLS1.3) or
1719 * protocol violation)
1721 if ((s
->rlayer
.handshake_fragment_len
>= 4)
1722 && !ossl_statem_get_in_handshake(s
)) {
1723 int ined
= (s
->early_data_state
== SSL_EARLY_DATA_READING
);
1725 /* We found handshake data, so we're going back into init */
1726 ossl_statem_set_in_init(s
, 1);
1728 i
= s
->handshake_func(ssl
);
1729 /* SSLfatal() already called if appropriate */
1737 * If we were actually trying to read early data and we found a
1738 * handshake message, then we don't want to continue to try and read
1739 * the application data any more. It won't be "early" now.
1744 if (!(s
->mode
& SSL_MODE_AUTO_RETRY
)) {
1745 if (SSL3_BUFFER_get_left(rbuf
) == 0) {
1746 /* no read-ahead left? */
1749 * In the case where we try to read application data, but we
1750 * trigger an SSL handshake, we return -1 with the retry
1751 * option set. Otherwise renegotiation may cause nasty
1752 * problems in the blocking world
1754 s
->rwstate
= SSL_READING
;
1755 bio
= SSL_get_rbio(ssl
);
1756 BIO_clear_retry_flags(bio
);
1757 BIO_set_retry_read(bio
);
1764 switch (SSL3_RECORD_get_type(rr
)) {
1767 * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but
1768 * TLS 1.2 says you MUST send an unexpected message alert. We use the
1769 * TLS 1.2 behaviour for all protocol versions to prevent issues where
1770 * no progress is being made and the peer continually sends unrecognised
1771 * record types, using up resources processing them.
1773 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_RECORD
);
1775 case SSL3_RT_CHANGE_CIPHER_SPEC
:
1777 case SSL3_RT_HANDSHAKE
:
1779 * we already handled all of these, with the possible exception of
1780 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
1781 * that should not happen when type != rr->type
1783 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, ERR_R_INTERNAL_ERROR
);
1785 case SSL3_RT_APPLICATION_DATA
:
1787 * At this point, we were expecting handshake data, but have
1788 * application data. If the library was running inside ssl3_read()
1789 * (i.e. in_read_app_data is set) and it makes sense to read
1790 * application data at this point (session renegotiation not yet
1791 * started), we will indulge it.
1793 if (ossl_statem_app_data_allowed(s
)) {
1794 s
->s3
.in_read_app_data
= 2;
1796 } else if (ossl_statem_skip_early_data(s
)) {
1798 * This can happen after a client sends a CH followed by early_data,
1799 * but the server responds with a HelloRetryRequest. The server
1800 * reads the next record from the client expecting to find a
1801 * plaintext ClientHello but gets a record which appears to be
1802 * application data. The trial decrypt "works" because null
1803 * decryption was applied. We just skip it and move on to the next
1806 if (!ossl_early_data_count_ok(s
, rr
->length
,
1807 EARLY_DATA_CIPHERTEXT_OVERHEAD
, 0)) {
1808 /* SSLfatal() already called */
1811 SSL3_RECORD_set_read(rr
);
1814 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_RECORD
);
1820 void ssl3_record_sequence_update(unsigned char *seq
)
1824 for (i
= 7; i
>= 0; i
--) {
1832 * Returns true if the current rrec was sent in SSLv2 backwards compatible
1833 * format and false otherwise.
1835 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER
*rl
)
1837 return SSL3_RECORD_is_sslv2_record(&rl
->rrec
[0]);
1841 * Returns the length in bytes of the current rrec
1843 size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER
*rl
)
1845 return SSL3_RECORD_get_length(&rl
->rrec
[0]);