1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
115 #include "../ssl_locl.h"
116 #include <openssl/evp.h>
117 #include <openssl/buffer.h>
118 #include <openssl/rand.h>
119 #include "record_locl.h"
121 #ifndef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
122 # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
125 #if defined(OPENSSL_SMALL_FOOTPRINT) || \
126 !( defined(AES_ASM) && ( \
127 defined(__x86_64) || defined(__x86_64__) || \
128 defined(_M_AMD64) || defined(_M_X64) || \
129 defined(__INTEL__) ) \
131 # undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
132 # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
135 void RECORD_LAYER_init(RECORD_LAYER
*rl
, SSL
*s
)
138 SSL3_RECORD_clear(&rl
->rrec
);
139 SSL3_RECORD_clear(&rl
->wrec
);
142 void RECORD_LAYER_clear(RECORD_LAYER
*rl
)
144 rl
->rstate
= SSL_ST_READ_HEADER
;
146 /* Do I need to clear read_ahead? As far as I can tell read_ahead did not
147 * previously get reset by SSL_clear...so I'll keep it that way..but is
152 rl
->packet_length
= 0;
154 memset(rl
->alert_fragment
, 0, sizeof(rl
->alert_fragment
));
155 rl
->alert_fragment_len
= 0;
156 memset(rl
->handshake_fragment
, 0, sizeof(rl
->handshake_fragment
));
157 rl
->handshake_fragment_len
= 0;
161 rl
->wpend_buf
= NULL
;
163 SSL3_BUFFER_clear(&rl
->rbuf
);
164 SSL3_BUFFER_clear(&rl
->wbuf
);
165 SSL3_RECORD_clear(&rl
->rrec
);
166 SSL3_RECORD_clear(&rl
->wrec
);
168 RECORD_LAYER_reset_read_sequence(rl
);
169 RECORD_LAYER_reset_write_sequence(rl
);
172 DTLS_RECORD_LAYER_clear(rl
);
175 void RECORD_LAYER_release(RECORD_LAYER
*rl
)
177 if (SSL3_BUFFER_is_initialised(&rl
->rbuf
))
178 ssl3_release_read_buffer(rl
->s
);
179 if (SSL3_BUFFER_is_initialised(&rl
->wbuf
))
180 ssl3_release_write_buffer(rl
->s
);
181 SSL3_RECORD_release(&rl
->rrec
);
184 int RECORD_LAYER_read_pending(RECORD_LAYER
*rl
)
186 return SSL3_BUFFER_get_left(&rl
->rbuf
) != 0;
189 int RECORD_LAYER_write_pending(RECORD_LAYER
*rl
)
191 return SSL3_BUFFER_get_left(&rl
->wbuf
) != 0;
194 int RECORD_LAYER_set_data(RECORD_LAYER
*rl
, const unsigned char *buf
, int len
)
196 rl
->packet_length
= len
;
198 rl
->rstate
= SSL_ST_READ_HEADER
;
199 if (!SSL3_BUFFER_is_initialised(&rl
->rbuf
))
200 if (!ssl3_setup_read_buffer(rl
->s
))
204 rl
->packet
= SSL3_BUFFER_get_buf(&rl
->rbuf
);
205 SSL3_BUFFER_set_data(&rl
->rbuf
, buf
, len
);
210 void RECORD_LAYER_reset_read_sequence(RECORD_LAYER
*rl
)
212 memset(rl
->read_sequence
, 0, sizeof(rl
->read_sequence
));
215 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER
*rl
)
217 memset(rl
->write_sequence
, 0, sizeof(rl
->write_sequence
));
220 int RECORD_LAYER_setup_comp_buffer(RECORD_LAYER
*rl
)
222 return SSL3_RECORD_setup(&(rl
)->rrec
);
225 int ssl3_pending(const SSL
*s
)
227 if (s
->rlayer
.rstate
== SSL_ST_READ_BODY
)
230 return (SSL3_RECORD_get_type(&s
->rlayer
.rrec
) == SSL3_RT_APPLICATION_DATA
)
231 ? SSL3_RECORD_get_length(&s
->rlayer
.rrec
) : 0;
234 const char *SSL_rstate_string_long(const SSL
*s
)
238 switch (s
->rlayer
.rstate
) {
239 case SSL_ST_READ_HEADER
:
242 case SSL_ST_READ_BODY
:
245 case SSL_ST_READ_DONE
:
255 const char *SSL_rstate_string(const SSL
*s
)
259 switch (s
->rlayer
.rstate
) {
260 case SSL_ST_READ_HEADER
:
263 case SSL_ST_READ_BODY
:
266 case SSL_ST_READ_DONE
:
276 int ssl3_read_n(SSL
*s
, int n
, int max
, int extend
)
279 * If extend == 0, obtain new n-byte packet; if extend == 1, increase
280 * packet by another n bytes. The packet will be in the sub-array of
281 * s->s3->rbuf.buf specified by s->packet and s->packet_length. (If
282 * s->rlayer.read_ahead is set, 'max' bytes may be stored in rbuf [plus
283 * s->packet_length bytes if extend == 1].)
293 rb
= &s
->rlayer
.rbuf
;
295 if (!ssl3_setup_read_buffer(s
))
299 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
300 align
= (size_t)rb
->buf
+ SSL3_RT_HEADER_LENGTH
;
301 align
= (0-align
) & (SSL3_ALIGN_PAYLOAD
- 1);
305 /* start with empty packet ... */
308 else if (align
!= 0 && left
>= SSL3_RT_HEADER_LENGTH
) {
310 * check if next packet length is large enough to justify payload
313 pkt
= rb
->buf
+ rb
->offset
;
314 if (pkt
[0] == SSL3_RT_APPLICATION_DATA
315 && (pkt
[3] << 8 | pkt
[4]) >= 128) {
317 * Note that even if packet is corrupted and its length field
318 * is insane, we can only be led to wrong decision about
319 * whether memmove will occur or not. Header values has no
320 * effect on memmove arguments and therefore no buffer
321 * overrun can be triggered.
323 memmove(rb
->buf
+ align
, pkt
, left
);
327 s
->rlayer
.packet
= rb
->buf
+ rb
->offset
;
328 s
->rlayer
.packet_length
= 0;
329 /* ... now we can act as if 'extend' was set */
333 * For DTLS/UDP reads should not span multiple packets because the read
334 * operation returns the whole packet at once (as long as it fits into
337 if (SSL_IS_DTLS(s
)) {
338 if (left
== 0 && extend
)
340 if (left
> 0 && n
> left
)
344 /* if there is enough in the buffer from a previous read, take some */
346 s
->rlayer
.packet_length
+= n
;
352 /* else we need to read more data */
354 len
= s
->rlayer
.packet_length
;
355 pkt
= rb
->buf
+ align
;
357 * Move any available bytes to front of buffer: 'len' bytes already
358 * pointed to by 'packet', 'left' extra ones at the end
360 if (s
->rlayer
.packet
!= pkt
) { /* len > 0 */
361 memmove(pkt
, s
->rlayer
.packet
, len
+ left
);
362 s
->rlayer
.packet
= pkt
;
363 rb
->offset
= len
+ align
;
366 if (n
> (int)(rb
->len
- rb
->offset
)) { /* does not happen */
367 SSLerr(SSL_F_SSL3_READ_N
, ERR_R_INTERNAL_ERROR
);
371 /* We always act like read_ahead is set for DTLS */
372 if (!s
->rlayer
.read_ahead
&& !SSL_IS_DTLS(s
))
373 /* ignore max parameter */
378 if (max
> (int)(rb
->len
- rb
->offset
))
379 max
= rb
->len
- rb
->offset
;
384 * Now we have len+left bytes at the front of s->s3->rbuf.buf and
385 * need to read in more until we have len+n (up to len+max if
390 if (s
->rbio
!= NULL
) {
391 s
->rwstate
= SSL_READING
;
392 i
= BIO_read(s
->rbio
, pkt
+ len
+ left
, max
- left
);
394 SSLerr(SSL_F_SSL3_READ_N
, SSL_R_READ_BIO_NOT_SET
);
400 if (s
->mode
& SSL_MODE_RELEASE_BUFFERS
&& !SSL_IS_DTLS(s
))
402 ssl3_release_read_buffer(s
);
407 * reads should *never* span multiple packets for DTLS because the
408 * underlying transport protocol is message oriented as opposed to
409 * byte oriented as in the TLS case.
411 if (SSL_IS_DTLS(s
)) {
413 n
= left
; /* makes the while condition false */
417 /* done reading, now the book-keeping */
420 s
->rlayer
.packet_length
+= n
;
421 s
->rwstate
= SSL_NOTHING
;
427 * Call this to write data in records of type 'type' It will return <= 0 if
428 * not all data has been sent or non-blocking IO.
430 int ssl3_write_bytes(SSL
*s
, int type
, const void *buf_
, int len
)
432 const unsigned char *buf
= buf_
;
435 #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
436 unsigned int max_send_fragment
;
437 unsigned int u_len
= (unsigned int)len
;
439 SSL3_BUFFER
*wb
= &s
->rlayer
.wbuf
;
443 SSLerr(SSL_F_SSL3_WRITE_BYTES
, SSL_R_SSL_NEGATIVE_LENGTH
);
447 s
->rwstate
= SSL_NOTHING
;
448 tot
= s
->rlayer
.wnum
;
450 * ensure that if we end up with a smaller value of data to write out
451 * than the the original len from a write which didn't complete for
452 * non-blocking I/O and also somehow ended up avoiding the check for
453 * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
454 * possible to end up with (len-tot) as a large number that will then
455 * promptly send beyond the end of the users buffer ... so we trap and
456 * report the error in a way the user will notice
458 if ((unsigned int)len
< s
->rlayer
.wnum
) {
459 SSLerr(SSL_F_SSL3_WRITE_BYTES
, SSL_R_BAD_LENGTH
);
466 if (SSL_in_init(s
) && !ossl_statem_get_in_handshake(s
)) {
467 i
= s
->handshake_func(s
);
471 SSLerr(SSL_F_SSL3_WRITE_BYTES
, SSL_R_SSL_HANDSHAKE_FAILURE
);
477 * first check if there is a SSL3_BUFFER still being written out. This
478 * will happen with non blocking IO
481 i
= ssl3_write_pending(s
, type
, &buf
[tot
], s
->rlayer
.wpend_tot
);
483 /* XXX should we ssl3_release_write_buffer if i<0? */
484 s
->rlayer
.wnum
= tot
;
487 tot
+= i
; /* this might be last fragment */
489 #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
491 * Depending on platform multi-block can deliver several *times*
492 * better performance. Downside is that it has to allocate
493 * jumbo buffer to accomodate up to 8 records, but the
494 * compromise is considered worthy.
496 if (type
== SSL3_RT_APPLICATION_DATA
&&
497 u_len
>= 4 * (max_send_fragment
= s
->max_send_fragment
) &&
498 s
->compress
== NULL
&& s
->msg_callback
== NULL
&&
499 !SSL_USE_ETM(s
) && SSL_USE_EXPLICIT_IV(s
) &&
500 EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s
->enc_write_ctx
)) &
501 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
) {
502 unsigned char aad
[13];
503 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param
;
506 /* minimize address aliasing conflicts */
507 if ((max_send_fragment
& 0xfff) == 0)
508 max_send_fragment
-= 512;
510 if (tot
== 0 || wb
->buf
== NULL
) { /* allocate jumbo buffer */
511 ssl3_release_write_buffer(s
);
513 packlen
= EVP_CIPHER_CTX_ctrl(s
->enc_write_ctx
,
514 EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
,
515 max_send_fragment
, NULL
);
517 if (u_len
>= 8 * max_send_fragment
)
522 wb
->buf
= OPENSSL_malloc(packlen
);
523 if (wb
->buf
== NULL
) {
524 SSLerr(SSL_F_SSL3_WRITE_BYTES
, ERR_R_MALLOC_FAILURE
);
528 } else if (tot
== len
) { /* done? */
529 OPENSSL_free(wb
->buf
); /* free jumbo buffer */
536 if (n
< 4 * max_send_fragment
) {
537 OPENSSL_free(wb
->buf
); /* free jumbo buffer */
542 if (s
->s3
->alert_dispatch
) {
543 i
= s
->method
->ssl_dispatch_alert(s
);
545 s
->rlayer
.wnum
= tot
;
550 if (n
>= 8 * max_send_fragment
)
551 nw
= max_send_fragment
* (mb_param
.interleave
= 8);
553 nw
= max_send_fragment
* (mb_param
.interleave
= 4);
555 memcpy(aad
, s
->rlayer
.write_sequence
, 8);
557 aad
[9] = (unsigned char)(s
->version
>> 8);
558 aad
[10] = (unsigned char)(s
->version
);
565 packlen
= EVP_CIPHER_CTX_ctrl(s
->enc_write_ctx
,
566 EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
,
567 sizeof(mb_param
), &mb_param
);
569 if (packlen
<= 0 || packlen
> (int)wb
->len
) { /* never happens */
570 OPENSSL_free(wb
->buf
); /* free jumbo buffer */
575 mb_param
.out
= wb
->buf
;
576 mb_param
.inp
= &buf
[tot
];
579 if (EVP_CIPHER_CTX_ctrl(s
->enc_write_ctx
,
580 EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
,
581 sizeof(mb_param
), &mb_param
) <= 0)
584 s
->rlayer
.write_sequence
[7] += mb_param
.interleave
;
585 if (s
->rlayer
.write_sequence
[7] < mb_param
.interleave
) {
587 while (j
>= 0 && (++s
->rlayer
.write_sequence
[j
--]) == 0) ;
593 s
->rlayer
.wpend_tot
= nw
;
594 s
->rlayer
.wpend_buf
= &buf
[tot
];
595 s
->rlayer
.wpend_type
= type
;
596 s
->rlayer
.wpend_ret
= nw
;
598 i
= ssl3_write_pending(s
, type
, &buf
[tot
], nw
);
600 if (i
< 0 && (!s
->wbio
|| !BIO_should_retry(s
->wbio
))) {
601 OPENSSL_free(wb
->buf
);
604 s
->rlayer
.wnum
= tot
;
608 OPENSSL_free(wb
->buf
); /* free jumbo buffer */
617 if (tot
== len
) { /* done? */
618 if (s
->mode
& SSL_MODE_RELEASE_BUFFERS
&& !SSL_IS_DTLS(s
))
619 ssl3_release_write_buffer(s
);
626 if (n
> s
->max_send_fragment
)
627 nw
= s
->max_send_fragment
;
631 i
= do_ssl3_write(s
, type
, &(buf
[tot
]), nw
, 0);
633 /* XXX should we ssl3_release_write_buffer if i<0? */
634 s
->rlayer
.wnum
= tot
;
639 (type
== SSL3_RT_APPLICATION_DATA
&&
640 (s
->mode
& SSL_MODE_ENABLE_PARTIAL_WRITE
))) {
642 * next chunk of data should get another prepended empty fragment
643 * in ciphersuites with known-IV weakness:
645 s
->s3
->empty_fragment_done
= 0;
647 if ((i
== (int)n
) && s
->mode
& SSL_MODE_RELEASE_BUFFERS
&&
649 ssl3_release_write_buffer(s
);
659 int do_ssl3_write(SSL
*s
, int type
, const unsigned char *buf
,
660 unsigned int len
, int create_empty_fragment
)
662 unsigned char *p
, *plen
;
663 int i
, mac_size
, clear
= 0;
668 SSL3_BUFFER
*wb
= &s
->rlayer
.wbuf
;
672 * first check if there is a SSL3_BUFFER still being written out. This
673 * will happen with non blocking IO
675 if (SSL3_BUFFER_get_left(wb
) != 0)
676 return (ssl3_write_pending(s
, type
, buf
, len
));
678 /* If we have an alert to send, lets send it */
679 if (s
->s3
->alert_dispatch
) {
680 i
= s
->method
->ssl_dispatch_alert(s
);
683 /* if it went, fall through and send more stuff */
686 if (!SSL3_BUFFER_is_initialised(wb
))
687 if (!ssl3_setup_write_buffer(s
))
690 if (len
== 0 && !create_empty_fragment
)
693 wr
= &s
->rlayer
.wrec
;
696 if ((sess
== NULL
) ||
697 (s
->enc_write_ctx
== NULL
) ||
698 (EVP_MD_CTX_md(s
->write_hash
) == NULL
)) {
699 clear
= s
->enc_write_ctx
? 0 : 1; /* must be AEAD cipher */
702 mac_size
= EVP_MD_CTX_size(s
->write_hash
);
708 * 'create_empty_fragment' is true only when this function calls itself
710 if (!clear
&& !create_empty_fragment
&& !s
->s3
->empty_fragment_done
) {
712 * countermeasure against known-IV weakness in CBC ciphersuites (see
713 * http://www.openssl.org/~bodo/tls-cbc.txt)
716 if (s
->s3
->need_empty_fragments
&& type
== SSL3_RT_APPLICATION_DATA
) {
718 * recursive function call with 'create_empty_fragment' set; this
719 * prepares and buffers the data for an empty fragment (these
720 * 'prefix_len' bytes are sent out later together with the actual
723 prefix_len
= do_ssl3_write(s
, type
, buf
, 0, 1);
728 (SSL3_RT_HEADER_LENGTH
+ SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD
))
730 /* insufficient space */
731 SSLerr(SSL_F_DO_SSL3_WRITE
, ERR_R_INTERNAL_ERROR
);
736 s
->s3
->empty_fragment_done
= 1;
739 if (create_empty_fragment
) {
740 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
742 * extra fragment would be couple of cipher blocks, which would be
743 * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real
744 * payload, then we can just pretent we simply have two headers.
746 align
= (size_t)SSL3_BUFFER_get_buf(wb
) + 2 * SSL3_RT_HEADER_LENGTH
;
747 align
= (0-align
) & (SSL3_ALIGN_PAYLOAD
- 1);
749 p
= SSL3_BUFFER_get_buf(wb
) + align
;
750 SSL3_BUFFER_set_offset(wb
, align
);
751 } else if (prefix_len
) {
752 p
= SSL3_BUFFER_get_buf(wb
) + SSL3_BUFFER_get_offset(wb
) + prefix_len
;
754 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
755 align
= (size_t)SSL3_BUFFER_get_buf(wb
) + SSL3_RT_HEADER_LENGTH
;
756 align
= (0-align
) & (SSL3_ALIGN_PAYLOAD
- 1);
758 p
= SSL3_BUFFER_get_buf(wb
) + align
;
759 SSL3_BUFFER_set_offset(wb
, align
);
762 /* write the header */
764 *(p
++) = type
& 0xff;
765 SSL3_RECORD_set_type(wr
, type
);
767 *(p
++) = (s
->version
>> 8);
769 * Some servers hang if iniatial client hello is larger than 256 bytes
770 * and record version number > TLS 1.0
772 if (SSL_get_state(s
) == TLS_ST_CW_CLNT_HELLO
773 && !s
->renegotiate
&& TLS1_get_version(s
) > TLS1_VERSION
)
776 *(p
++) = s
->version
& 0xff;
778 /* field where we are to write out packet length */
781 /* Explicit IV length, block ciphers appropriate version flag */
782 if (s
->enc_write_ctx
&& SSL_USE_EXPLICIT_IV(s
)) {
783 int mode
= EVP_CIPHER_CTX_mode(s
->enc_write_ctx
);
784 if (mode
== EVP_CIPH_CBC_MODE
) {
785 eivlen
= EVP_CIPHER_CTX_iv_length(s
->enc_write_ctx
);
789 /* Need explicit part of IV for GCM mode */
790 else if (mode
== EVP_CIPH_GCM_MODE
)
791 eivlen
= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
792 else if (mode
== EVP_CIPH_CCM_MODE
)
793 eivlen
= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
799 /* lets setup the record stuff. */
800 SSL3_RECORD_set_data(wr
, p
+ eivlen
);
801 SSL3_RECORD_set_length(wr
, (int)len
);
802 SSL3_RECORD_set_input(wr
, (unsigned char *)buf
);
806 * we now 'read' from wr->input, wr->length bytes into wr->data
809 /* first we compress */
810 if (s
->compress
!= NULL
) {
811 if (!ssl3_do_compress(s
)) {
812 SSLerr(SSL_F_DO_SSL3_WRITE
, SSL_R_COMPRESSION_FAILURE
);
816 memcpy(wr
->data
, wr
->input
, wr
->length
);
817 SSL3_RECORD_reset_input(wr
);
821 * we should still have the output to wr->data and the input from
822 * wr->input. Length should be wr->length. wr->data still points in the
826 if (!SSL_USE_ETM(s
) && mac_size
!= 0) {
827 if (s
->method
->ssl3_enc
->mac(s
, &(p
[wr
->length
+ eivlen
]), 1) < 0)
829 SSL3_RECORD_add_length(wr
, mac_size
);
832 SSL3_RECORD_set_data(wr
, p
);
833 SSL3_RECORD_reset_input(wr
);
837 * if (RAND_pseudo_bytes(p, eivlen) <= 0) goto err;
839 SSL3_RECORD_add_length(wr
, eivlen
);
842 if (s
->method
->ssl3_enc
->enc(s
, 1) < 1)
845 if (SSL_USE_ETM(s
) && mac_size
!= 0) {
846 if (s
->method
->ssl3_enc
->mac(s
, p
+ wr
->length
, 1) < 0)
848 SSL3_RECORD_add_length(wr
, mac_size
);
851 /* record length after mac and block padding */
852 s2n(SSL3_RECORD_get_length(wr
), plen
);
855 s
->msg_callback(1, 0, SSL3_RT_HEADER
, plen
- 5, 5, s
,
856 s
->msg_callback_arg
);
859 * we should now have wr->data pointing to the encrypted data, which is
862 SSL3_RECORD_set_type(wr
, type
); /* not needed but helps for debugging */
863 SSL3_RECORD_add_length(wr
, SSL3_RT_HEADER_LENGTH
);
865 if (create_empty_fragment
) {
867 * we are in a recursive call; just return the length, don't write
870 return SSL3_RECORD_get_length(wr
);
873 /* now let's set up wb */
874 SSL3_BUFFER_set_left(wb
, prefix_len
+ SSL3_RECORD_get_length(wr
));
877 * memorize arguments so that ssl3_write_pending can detect bad write
880 s
->rlayer
.wpend_tot
= len
;
881 s
->rlayer
.wpend_buf
= buf
;
882 s
->rlayer
.wpend_type
= type
;
883 s
->rlayer
.wpend_ret
= len
;
885 /* we now just need to write the buffer */
886 return ssl3_write_pending(s
, type
, buf
, len
);
891 /* if s->s3->wbuf.left != 0, we need to call this */
892 int ssl3_write_pending(SSL
*s
, int type
, const unsigned char *buf
,
896 SSL3_BUFFER
*wb
= &s
->rlayer
.wbuf
;
899 if ((s
->rlayer
.wpend_tot
> (int)len
)
900 || ((s
->rlayer
.wpend_buf
!= buf
) &&
901 !(s
->mode
& SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
))
902 || (s
->rlayer
.wpend_type
!= type
)) {
903 SSLerr(SSL_F_SSL3_WRITE_PENDING
, SSL_R_BAD_WRITE_RETRY
);
909 if (s
->wbio
!= NULL
) {
910 s
->rwstate
= SSL_WRITING
;
911 i
= BIO_write(s
->wbio
,
912 (char *)&(SSL3_BUFFER_get_buf(wb
)[SSL3_BUFFER_get_offset(wb
)]),
913 (unsigned int)SSL3_BUFFER_get_left(wb
));
915 SSLerr(SSL_F_SSL3_WRITE_PENDING
, SSL_R_BIO_NOT_SET
);
918 if (i
== SSL3_BUFFER_get_left(wb
)) {
919 SSL3_BUFFER_set_left(wb
, 0);
920 SSL3_BUFFER_add_offset(wb
, i
);
921 s
->rwstate
= SSL_NOTHING
;
922 return (s
->rlayer
.wpend_ret
);
924 if (SSL_IS_DTLS(s
)) {
926 * For DTLS, just drop it. That's kind of the whole point in
927 * using a datagram service
929 SSL3_BUFFER_set_left(wb
, 0);
933 SSL3_BUFFER_add_offset(wb
, i
);
934 SSL3_BUFFER_add_left(wb
, -i
);
939 * Return up to 'len' payload bytes received in 'type' records.
940 * 'type' is one of the following:
942 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
943 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
944 * - 0 (during a shutdown, no data has to be returned)
946 * If we don't have stored data to work from, read a SSL/TLS record first
947 * (possibly multiple records if we still don't have anything to return).
949 * This function must handle any surprises the peer may have for us, such as
950 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
951 * messages are treated as if they were handshake messages *if* the |recd_type|
952 * argument is non NULL.
953 * Also if record payloads contain fragments too small to process, we store
954 * them until there is enough for the respective protocol (the record protocol
955 * may use arbitrary fragmentation and even interleaving):
956 * Change cipher spec protocol
957 * just 1 byte needed, no need for keeping anything stored
959 * 2 bytes needed (AlertLevel, AlertDescription)
961 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
962 * to detect unexpected Client Hello and Hello Request messages
963 * here, anything else is handled by higher layers
964 * Application data protocol
965 * none of our business
967 int ssl3_read_bytes(SSL
*s
, int type
, int *recvd_type
, unsigned char *buf
,
973 void (*cb
) (const SSL
*ssl
, int type2
, int val
) = NULL
;
975 if (!SSL3_BUFFER_is_initialised(&s
->rlayer
.rbuf
)) {
976 /* Not initialized yet */
977 if (!ssl3_setup_read_buffer(s
))
981 if ((type
&& (type
!= SSL3_RT_APPLICATION_DATA
)
982 && (type
!= SSL3_RT_HANDSHAKE
)) || (peek
984 SSL3_RT_APPLICATION_DATA
))) {
985 SSLerr(SSL_F_SSL3_READ_BYTES
, ERR_R_INTERNAL_ERROR
);
989 if ((type
== SSL3_RT_HANDSHAKE
) && (s
->rlayer
.handshake_fragment_len
> 0))
990 /* (partially) satisfy request from storage */
992 unsigned char *src
= s
->rlayer
.handshake_fragment
;
993 unsigned char *dst
= buf
;
998 while ((len
> 0) && (s
->rlayer
.handshake_fragment_len
> 0)) {
1001 s
->rlayer
.handshake_fragment_len
--;
1004 /* move any remaining fragment bytes: */
1005 for (k
= 0; k
< s
->rlayer
.handshake_fragment_len
; k
++)
1006 s
->rlayer
.handshake_fragment
[k
] = *src
++;
1008 if (recvd_type
!= NULL
)
1009 *recvd_type
= SSL3_RT_HANDSHAKE
;
1015 * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
1018 if (!ossl_statem_get_in_handshake(s
) && SSL_in_init(s
)) {
1019 /* type == SSL3_RT_APPLICATION_DATA */
1020 i
= s
->handshake_func(s
);
1024 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_SSL_HANDSHAKE_FAILURE
);
1029 s
->rwstate
= SSL_NOTHING
;
1032 * s->s3->rrec.type - is the type of record
1033 * s->s3->rrec.data, - data
1034 * s->s3->rrec.off, - offset into 'data' for next read
1035 * s->s3->rrec.length, - number of bytes.
1037 rr
= &s
->rlayer
.rrec
;
1039 /* get new packet if necessary */
1040 if ((SSL3_RECORD_get_length(rr
) == 0)
1041 || (s
->rlayer
.rstate
== SSL_ST_READ_BODY
)) {
1042 ret
= ssl3_get_record(s
);
1047 /* we now have a packet which can be read and processed */
1049 if (s
->s3
->change_cipher_spec
/* set when we receive ChangeCipherSpec,
1050 * reset by ssl3_get_finished */
1051 && (SSL3_RECORD_get_type(rr
) != SSL3_RT_HANDSHAKE
)) {
1052 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1053 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED
);
1058 * If the other end has shut down, throw anything we read away (even in
1061 if (s
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
1062 SSL3_RECORD_set_length(rr
, 0);
1063 s
->rwstate
= SSL_NOTHING
;
1067 if (type
== SSL3_RECORD_get_type(rr
)
1068 || (SSL3_RECORD_get_type(rr
) == SSL3_RT_CHANGE_CIPHER_SPEC
1069 && type
== SSL3_RT_HANDSHAKE
&& recvd_type
!= NULL
)) {
1071 * SSL3_RT_APPLICATION_DATA or
1072 * SSL3_RT_HANDSHAKE or
1073 * SSL3_RT_CHANGE_CIPHER_SPEC
1076 * make sure that we are not getting application data when we are
1077 * doing a handshake for the first time
1079 if (SSL_in_init(s
) && (type
== SSL3_RT_APPLICATION_DATA
) &&
1080 (s
->enc_read_ctx
== NULL
)) {
1081 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1082 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_APP_DATA_IN_HANDSHAKE
);
1086 if (type
== SSL3_RT_HANDSHAKE
1087 && SSL3_RECORD_get_type(rr
) == SSL3_RT_CHANGE_CIPHER_SPEC
1088 && s
->rlayer
.handshake_fragment_len
> 0) {
1089 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1090 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_CCS_RECEIVED_EARLY
);
1094 if (recvd_type
!= NULL
)
1095 *recvd_type
= SSL3_RECORD_get_type(rr
);
1100 if ((unsigned int)len
> SSL3_RECORD_get_length(rr
))
1101 n
= SSL3_RECORD_get_length(rr
);
1103 n
= (unsigned int)len
;
1105 memcpy(buf
, &(rr
->data
[rr
->off
]), n
);
1107 SSL3_RECORD_add_length(rr
, -n
);
1108 SSL3_RECORD_add_off(rr
, n
);
1109 if (SSL3_RECORD_get_length(rr
) == 0) {
1110 s
->rlayer
.rstate
= SSL_ST_READ_HEADER
;
1111 SSL3_RECORD_set_off(rr
, 0);
1112 if (s
->mode
& SSL_MODE_RELEASE_BUFFERS
1113 && SSL3_BUFFER_get_left(&s
->rlayer
.rbuf
) == 0)
1114 ssl3_release_read_buffer(s
);
1121 * If we get here, then type != rr->type; if we have a handshake message,
1122 * then it was unexpected (Hello Request or Client Hello) or invalid (we
1123 * were actually expecting a CCS).
1126 if (rr
->type
== SSL3_RT_HANDSHAKE
&& type
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
1127 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1128 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_UNEXPECTED_MESSAGE
);
1133 * Lets just double check that we've not got an SSLv2 record
1135 if (rr
->rec_version
== SSL2_VERSION
) {
1137 * Should never happen. ssl3_get_record() should only give us an SSLv2
1138 * record back if this is the first packet and we are looking for an
1139 * initial ClientHello. Therefore |type| should always be equal to
1140 * |rr->type|. If not then something has gone horribly wrong
1142 al
= SSL_AD_INTERNAL_ERROR
;
1143 SSLerr(SSL_F_SSL3_READ_BYTES
, ERR_R_INTERNAL_ERROR
);
1147 if(s
->method
->version
== TLS_ANY_VERSION
1148 && (s
->server
|| rr
->type
!= SSL3_RT_ALERT
)) {
1150 * If we've got this far and still haven't decided on what version
1151 * we're using then this must be a client side alert we're dealing with
1152 * (we don't allow heartbeats yet). We shouldn't be receiving anything
1153 * other than a ClientHello if we are a server.
1155 s
->version
= rr
->rec_version
;
1156 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1157 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_UNEXPECTED_MESSAGE
);
1162 * In case of record types for which we have 'fragment' storage, fill
1163 * that so that we can process the data at a fixed place.
1166 unsigned int dest_maxlen
= 0;
1167 unsigned char *dest
= NULL
;
1168 unsigned int *dest_len
= NULL
;
1170 if (SSL3_RECORD_get_type(rr
) == SSL3_RT_HANDSHAKE
) {
1171 dest_maxlen
= sizeof s
->rlayer
.handshake_fragment
;
1172 dest
= s
->rlayer
.handshake_fragment
;
1173 dest_len
= &s
->rlayer
.handshake_fragment_len
;
1174 } else if (SSL3_RECORD_get_type(rr
) == SSL3_RT_ALERT
) {
1175 dest_maxlen
= sizeof s
->rlayer
.alert_fragment
;
1176 dest
= s
->rlayer
.alert_fragment
;
1177 dest_len
= &s
->rlayer
.alert_fragment_len
;
1179 #ifndef OPENSSL_NO_HEARTBEATS
1180 else if (SSL3_RECORD_get_type(rr
)== TLS1_RT_HEARTBEAT
) {
1181 /* We can ignore 0 return values */
1182 if (tls1_process_heartbeat(s
, SSL3_RECORD_get_data(rr
),
1183 SSL3_RECORD_get_length(rr
)) < 0) {
1187 /* Exit and notify application to read again */
1188 SSL3_RECORD_set_length(rr
, 0);
1189 s
->rwstate
= SSL_READING
;
1190 BIO_clear_retry_flags(SSL_get_rbio(s
));
1191 BIO_set_retry_read(SSL_get_rbio(s
));
1196 if (dest_maxlen
> 0) {
1197 n
= dest_maxlen
- *dest_len
; /* available space in 'dest' */
1198 if (SSL3_RECORD_get_length(rr
) < n
)
1199 n
= SSL3_RECORD_get_length(rr
); /* available bytes */
1201 /* now move 'n' bytes: */
1203 dest
[(*dest_len
)++] =
1204 SSL3_RECORD_get_data(rr
)[SSL3_RECORD_get_off(rr
)];
1205 SSL3_RECORD_add_off(rr
, 1);
1206 SSL3_RECORD_add_length(rr
, -1);
1209 if (*dest_len
< dest_maxlen
)
1210 goto start
; /* fragment was too small */
1215 * s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
1216 * s->rlayer.alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
1217 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
1220 /* If we are a client, check for an incoming 'Hello Request': */
1222 (s
->rlayer
.handshake_fragment_len
>= 4) &&
1223 (s
->rlayer
.handshake_fragment
[0] == SSL3_MT_HELLO_REQUEST
) &&
1224 (s
->session
!= NULL
) && (s
->session
->cipher
!= NULL
)) {
1225 s
->rlayer
.handshake_fragment_len
= 0;
1227 if ((s
->rlayer
.handshake_fragment
[1] != 0) ||
1228 (s
->rlayer
.handshake_fragment
[2] != 0) ||
1229 (s
->rlayer
.handshake_fragment
[3] != 0)) {
1230 al
= SSL_AD_DECODE_ERROR
;
1231 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_BAD_HELLO_REQUEST
);
1235 if (s
->msg_callback
)
1236 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
1237 s
->rlayer
.handshake_fragment
, 4, s
,
1238 s
->msg_callback_arg
);
1240 if (SSL_is_init_finished(s
) &&
1241 !(s
->s3
->flags
& SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
) &&
1242 !s
->s3
->renegotiate
) {
1243 ssl3_renegotiate(s
);
1244 if (ssl3_renegotiate_check(s
)) {
1245 i
= s
->handshake_func(s
);
1249 SSLerr(SSL_F_SSL3_READ_BYTES
,
1250 SSL_R_SSL_HANDSHAKE_FAILURE
);
1254 if (!(s
->mode
& SSL_MODE_AUTO_RETRY
)) {
1255 if (SSL3_BUFFER_get_left(&s
->rlayer
.rbuf
) == 0) {
1256 /* no read-ahead left? */
1259 * In the case where we try to read application data,
1260 * but we trigger an SSL handshake, we return -1 with
1261 * the retry option set. Otherwise renegotiation may
1262 * cause nasty problems in the blocking world
1264 s
->rwstate
= SSL_READING
;
1265 bio
= SSL_get_rbio(s
);
1266 BIO_clear_retry_flags(bio
);
1267 BIO_set_retry_read(bio
);
1274 * we either finished a handshake or ignored the request, now try
1275 * again to obtain the (application) data we were asked for
1280 * If we are a server and get a client hello when renegotiation isn't
1281 * allowed send back a no renegotiation alert and carry on. WARNING:
1282 * experimental code, needs reviewing (steve)
1285 SSL_is_init_finished(s
) &&
1286 !s
->s3
->send_connection_binding
&&
1287 (s
->version
> SSL3_VERSION
) &&
1288 (s
->rlayer
.handshake_fragment_len
>= 4) &&
1289 (s
->rlayer
.handshake_fragment
[0] == SSL3_MT_CLIENT_HELLO
) &&
1290 (s
->session
!= NULL
) && (s
->session
->cipher
!= NULL
) &&
1291 !(s
->ctx
->options
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)) {
1292 SSL3_RECORD_set_length(rr
, 0);
1293 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
1296 if (s
->rlayer
.alert_fragment_len
>= 2) {
1297 int alert_level
= s
->rlayer
.alert_fragment
[0];
1298 int alert_descr
= s
->rlayer
.alert_fragment
[1];
1300 s
->rlayer
.alert_fragment_len
= 0;
1302 if (s
->msg_callback
)
1303 s
->msg_callback(0, s
->version
, SSL3_RT_ALERT
,
1304 s
->rlayer
.alert_fragment
, 2, s
,
1305 s
->msg_callback_arg
);
1307 if (s
->info_callback
!= NULL
)
1308 cb
= s
->info_callback
;
1309 else if (s
->ctx
->info_callback
!= NULL
)
1310 cb
= s
->ctx
->info_callback
;
1313 j
= (alert_level
<< 8) | alert_descr
;
1314 cb(s
, SSL_CB_READ_ALERT
, j
);
1317 if (alert_level
== SSL3_AL_WARNING
) {
1318 s
->s3
->warn_alert
= alert_descr
;
1319 if (alert_descr
== SSL_AD_CLOSE_NOTIFY
) {
1320 s
->shutdown
|= SSL_RECEIVED_SHUTDOWN
;
1324 * This is a warning but we receive it if we requested
1325 * renegotiation and the peer denied it. Terminate with a fatal
1326 * alert because if application tried to renegotiatie it
1327 * presumably had a good reason and expects it to succeed. In
1328 * future we might have a renegotiation where we don't care if
1329 * the peer refused it where we carry on.
1331 else if (alert_descr
== SSL_AD_NO_RENEGOTIATION
) {
1332 al
= SSL_AD_HANDSHAKE_FAILURE
;
1333 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_NO_RENEGOTIATION
);
1336 #ifdef SSL_AD_MISSING_SRP_USERNAME
1337 else if (alert_descr
== SSL_AD_MISSING_SRP_USERNAME
)
1340 } else if (alert_level
== SSL3_AL_FATAL
) {
1343 s
->rwstate
= SSL_NOTHING
;
1344 s
->s3
->fatal_alert
= alert_descr
;
1345 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_AD_REASON_OFFSET
+ alert_descr
);
1346 BIO_snprintf(tmp
, sizeof tmp
, "%d", alert_descr
);
1347 ERR_add_error_data(2, "SSL alert number ", tmp
);
1348 s
->shutdown
|= SSL_RECEIVED_SHUTDOWN
;
1349 SSL_CTX_remove_session(s
->ctx
, s
->session
);
1352 al
= SSL_AD_ILLEGAL_PARAMETER
;
1353 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_UNKNOWN_ALERT_TYPE
);
1360 if (s
->shutdown
& SSL_SENT_SHUTDOWN
) { /* but we have not received a
1362 s
->rwstate
= SSL_NOTHING
;
1363 SSL3_RECORD_set_length(rr
, 0);
1367 if (SSL3_RECORD_get_type(rr
) == SSL3_RT_CHANGE_CIPHER_SPEC
) {
1368 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1369 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_CCS_RECEIVED_EARLY
);
1374 * Unexpected handshake message (Client Hello, or protocol violation)
1376 if ((s
->rlayer
.handshake_fragment_len
>= 4)
1377 && !ossl_statem_get_in_handshake(s
)) {
1378 if (SSL_is_init_finished(s
) &&
1379 !(s
->s3
->flags
& SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS
)) {
1380 ossl_statem_set_in_init(s
, 1);
1384 i
= s
->handshake_func(s
);
1388 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_SSL_HANDSHAKE_FAILURE
);
1392 if (!(s
->mode
& SSL_MODE_AUTO_RETRY
)) {
1393 if (SSL3_BUFFER_get_left(&s
->rlayer
.rbuf
) == 0) {
1394 /* no read-ahead left? */
1397 * In the case where we try to read application data, but we
1398 * trigger an SSL handshake, we return -1 with the retry
1399 * option set. Otherwise renegotiation may cause nasty
1400 * problems in the blocking world
1402 s
->rwstate
= SSL_READING
;
1403 bio
= SSL_get_rbio(s
);
1404 BIO_clear_retry_flags(bio
);
1405 BIO_set_retry_read(bio
);
1412 switch (SSL3_RECORD_get_type(rr
)) {
1415 * TLS up to v1.1 just ignores unknown message types: TLS v1.2 give
1416 * an unexpected message alert.
1418 if (s
->version
>= TLS1_VERSION
&& s
->version
<= TLS1_1_VERSION
) {
1419 SSL3_RECORD_set_length(rr
, 0);
1422 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1423 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_UNEXPECTED_RECORD
);
1425 case SSL3_RT_CHANGE_CIPHER_SPEC
:
1427 case SSL3_RT_HANDSHAKE
:
1429 * we already handled all of these, with the possible exception of
1430 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
1431 * that should not happen when type != rr->type
1433 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1434 SSLerr(SSL_F_SSL3_READ_BYTES
, ERR_R_INTERNAL_ERROR
);
1436 case SSL3_RT_APPLICATION_DATA
:
1438 * At this point, we were expecting handshake data, but have
1439 * application data. If the library was running inside ssl3_read()
1440 * (i.e. in_read_app_data is set) and it makes sense to read
1441 * application data at this point (session renegotiation not yet
1442 * started), we will indulge it.
1444 if (ossl_statem_app_data_allowed(s
)) {
1445 s
->s3
->in_read_app_data
= 2;
1448 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1449 SSLerr(SSL_F_SSL3_READ_BYTES
, SSL_R_UNEXPECTED_RECORD
);
1456 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1460 void ssl3_record_sequence_update(unsigned char *seq
)
1464 for (i
= 7; i
>= 0; i
--) {
1472 * Returns true if the current rrec was sent in SSLv2 backwards compatible
1473 * format and false otherwise.
1475 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER
*rl
)
1477 return SSL3_RECORD_is_sslv2_record(&rl
->rrec
);
1481 * Returns the length in bytes of the current rrec
1483 unsigned int RECORD_LAYER_get_rrec_length(RECORD_LAYER
*rl
)
1485 return SSL3_RECORD_get_length(&rl
->rrec
);