1ebfedea0SLionel Sambuc /* ssl/s3_pkt.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
60ebfedea0SLionel Sambuc *
61ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc * are met:
64ebfedea0SLionel Sambuc *
65ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc *
68ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc * distribution.
72ebfedea0SLionel Sambuc *
73ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc * software must display the following acknowledgment:
75ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc *
78ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc * openssl-core@openssl.org.
82ebfedea0SLionel Sambuc *
83ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc *
87ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc * acknowledgment:
89ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc *
92ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc * ====================================================================
105ebfedea0SLionel Sambuc *
106ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
108ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc *
110ebfedea0SLionel Sambuc */
111ebfedea0SLionel Sambuc
112ebfedea0SLionel Sambuc #include <stdio.h>
113*0a6a1f1dSLionel Sambuc #include <limits.h>
114ebfedea0SLionel Sambuc #include <errno.h>
115ebfedea0SLionel Sambuc #define USE_SOCKETS
116ebfedea0SLionel Sambuc #include "ssl_locl.h"
117ebfedea0SLionel Sambuc #include <openssl/evp.h>
118ebfedea0SLionel Sambuc #include <openssl/buffer.h>
119ebfedea0SLionel Sambuc #include <openssl/rand.h>
120ebfedea0SLionel Sambuc
121ebfedea0SLionel Sambuc static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
122ebfedea0SLionel Sambuc unsigned int len, int create_empty_fragment);
123ebfedea0SLionel Sambuc static int ssl3_get_record(SSL *s);
124ebfedea0SLionel Sambuc
ssl3_read_n(SSL * s,int n,int max,int extend)125ebfedea0SLionel Sambuc int ssl3_read_n(SSL *s, int n, int max, int extend)
126ebfedea0SLionel Sambuc {
127*0a6a1f1dSLionel Sambuc /*
128*0a6a1f1dSLionel Sambuc * If extend == 0, obtain new n-byte packet; if extend == 1, increase
129*0a6a1f1dSLionel Sambuc * packet by another n bytes. The packet will be in the sub-array of
130*0a6a1f1dSLionel Sambuc * s->s3->rbuf.buf specified by s->packet and s->packet_length. (If
131*0a6a1f1dSLionel Sambuc * s->read_ahead is set, 'max' bytes may be stored in rbuf [plus
132*0a6a1f1dSLionel Sambuc * s->packet_length bytes if extend == 1].)
133ebfedea0SLionel Sambuc */
134ebfedea0SLionel Sambuc int i, len, left;
135ebfedea0SLionel Sambuc long align = 0;
136ebfedea0SLionel Sambuc unsigned char *pkt;
137ebfedea0SLionel Sambuc SSL3_BUFFER *rb;
138ebfedea0SLionel Sambuc
139*0a6a1f1dSLionel Sambuc if (n <= 0)
140*0a6a1f1dSLionel Sambuc return n;
141ebfedea0SLionel Sambuc
142ebfedea0SLionel Sambuc rb = &(s->s3->rbuf);
143ebfedea0SLionel Sambuc if (rb->buf == NULL)
144ebfedea0SLionel Sambuc if (!ssl3_setup_read_buffer(s))
145ebfedea0SLionel Sambuc return -1;
146ebfedea0SLionel Sambuc
147ebfedea0SLionel Sambuc left = rb->left;
148ebfedea0SLionel Sambuc #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
149ebfedea0SLionel Sambuc align = (long)rb->buf + SSL3_RT_HEADER_LENGTH;
150ebfedea0SLionel Sambuc align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
151ebfedea0SLionel Sambuc #endif
152ebfedea0SLionel Sambuc
153*0a6a1f1dSLionel Sambuc if (!extend) {
154ebfedea0SLionel Sambuc /* start with empty packet ... */
155ebfedea0SLionel Sambuc if (left == 0)
156ebfedea0SLionel Sambuc rb->offset = align;
157*0a6a1f1dSLionel Sambuc else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
158*0a6a1f1dSLionel Sambuc /*
159*0a6a1f1dSLionel Sambuc * check if next packet length is large enough to justify payload
160*0a6a1f1dSLionel Sambuc * alignment...
161*0a6a1f1dSLionel Sambuc */
162ebfedea0SLionel Sambuc pkt = rb->buf + rb->offset;
163ebfedea0SLionel Sambuc if (pkt[0] == SSL3_RT_APPLICATION_DATA
164*0a6a1f1dSLionel Sambuc && (pkt[3] << 8 | pkt[4]) >= 128) {
165*0a6a1f1dSLionel Sambuc /*
166*0a6a1f1dSLionel Sambuc * Note that even if packet is corrupted and its length field
167*0a6a1f1dSLionel Sambuc * is insane, we can only be led to wrong decision about
168*0a6a1f1dSLionel Sambuc * whether memmove will occur or not. Header values has no
169*0a6a1f1dSLionel Sambuc * effect on memmove arguments and therefore no buffer
170*0a6a1f1dSLionel Sambuc * overrun can be triggered.
171*0a6a1f1dSLionel Sambuc */
172ebfedea0SLionel Sambuc memmove(rb->buf + align, pkt, left);
173ebfedea0SLionel Sambuc rb->offset = align;
174ebfedea0SLionel Sambuc }
175ebfedea0SLionel Sambuc }
176ebfedea0SLionel Sambuc s->packet = rb->buf + rb->offset;
177ebfedea0SLionel Sambuc s->packet_length = 0;
178ebfedea0SLionel Sambuc /* ... now we can act as if 'extend' was set */
179ebfedea0SLionel Sambuc }
180ebfedea0SLionel Sambuc
181*0a6a1f1dSLionel Sambuc /*
182*0a6a1f1dSLionel Sambuc * For DTLS/UDP reads should not span multiple packets because the read
183*0a6a1f1dSLionel Sambuc * operation returns the whole packet at once (as long as it fits into
184*0a6a1f1dSLionel Sambuc * the buffer).
185*0a6a1f1dSLionel Sambuc */
186*0a6a1f1dSLionel Sambuc if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER) {
187*0a6a1f1dSLionel Sambuc if (left == 0 && extend)
188*0a6a1f1dSLionel Sambuc return 0;
189ebfedea0SLionel Sambuc if (left > 0 && n > left)
190ebfedea0SLionel Sambuc n = left;
191ebfedea0SLionel Sambuc }
192ebfedea0SLionel Sambuc
193ebfedea0SLionel Sambuc /* if there is enough in the buffer from a previous read, take some */
194*0a6a1f1dSLionel Sambuc if (left >= n) {
195ebfedea0SLionel Sambuc s->packet_length += n;
196ebfedea0SLionel Sambuc rb->left = left - n;
197ebfedea0SLionel Sambuc rb->offset += n;
198ebfedea0SLionel Sambuc return (n);
199ebfedea0SLionel Sambuc }
200ebfedea0SLionel Sambuc
201ebfedea0SLionel Sambuc /* else we need to read more data */
202ebfedea0SLionel Sambuc
203ebfedea0SLionel Sambuc len = s->packet_length;
204ebfedea0SLionel Sambuc pkt = rb->buf + align;
205*0a6a1f1dSLionel Sambuc /*
206*0a6a1f1dSLionel Sambuc * Move any available bytes to front of buffer: 'len' bytes already
207*0a6a1f1dSLionel Sambuc * pointed to by 'packet', 'left' extra ones at the end
208*0a6a1f1dSLionel Sambuc */
209*0a6a1f1dSLionel Sambuc if (s->packet != pkt) { /* len > 0 */
210ebfedea0SLionel Sambuc memmove(pkt, s->packet, len + left);
211ebfedea0SLionel Sambuc s->packet = pkt;
212ebfedea0SLionel Sambuc rb->offset = len + align;
213ebfedea0SLionel Sambuc }
214ebfedea0SLionel Sambuc
215*0a6a1f1dSLionel Sambuc if (n > (int)(rb->len - rb->offset)) { /* does not happen */
216ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_N, ERR_R_INTERNAL_ERROR);
217ebfedea0SLionel Sambuc return -1;
218ebfedea0SLionel Sambuc }
219ebfedea0SLionel Sambuc
220*0a6a1f1dSLionel Sambuc /* We always act like read_ahead is set for DTLS */
221*0a6a1f1dSLionel Sambuc if (!s->read_ahead && !SSL_IS_DTLS(s))
222ebfedea0SLionel Sambuc /* ignore max parameter */
223ebfedea0SLionel Sambuc max = n;
224*0a6a1f1dSLionel Sambuc else {
225ebfedea0SLionel Sambuc if (max < n)
226ebfedea0SLionel Sambuc max = n;
227ebfedea0SLionel Sambuc if (max > (int)(rb->len - rb->offset))
228ebfedea0SLionel Sambuc max = rb->len - rb->offset;
229ebfedea0SLionel Sambuc }
230ebfedea0SLionel Sambuc
231*0a6a1f1dSLionel Sambuc while (left < n) {
232*0a6a1f1dSLionel Sambuc /*
233*0a6a1f1dSLionel Sambuc * Now we have len+left bytes at the front of s->s3->rbuf.buf and
234*0a6a1f1dSLionel Sambuc * need to read in more until we have len+n (up to len+max if
235*0a6a1f1dSLionel Sambuc * possible)
236*0a6a1f1dSLionel Sambuc */
237ebfedea0SLionel Sambuc
238ebfedea0SLionel Sambuc clear_sys_error();
239*0a6a1f1dSLionel Sambuc if (s->rbio != NULL) {
240ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
241ebfedea0SLionel Sambuc i = BIO_read(s->rbio, pkt + len + left, max - left);
242*0a6a1f1dSLionel Sambuc } else {
243ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_N, SSL_R_READ_BIO_NOT_SET);
244ebfedea0SLionel Sambuc i = -1;
245ebfedea0SLionel Sambuc }
246ebfedea0SLionel Sambuc
247*0a6a1f1dSLionel Sambuc if (i <= 0) {
248ebfedea0SLionel Sambuc rb->left = left;
249ebfedea0SLionel Sambuc if (s->mode & SSL_MODE_RELEASE_BUFFERS &&
250*0a6a1f1dSLionel Sambuc SSL_version(s) != DTLS1_VERSION
251*0a6a1f1dSLionel Sambuc && SSL_version(s) != DTLS1_BAD_VER)
252ebfedea0SLionel Sambuc if (len + left == 0)
253ebfedea0SLionel Sambuc ssl3_release_read_buffer(s);
254ebfedea0SLionel Sambuc return (i);
255ebfedea0SLionel Sambuc }
256ebfedea0SLionel Sambuc left += i;
257*0a6a1f1dSLionel Sambuc /*
258*0a6a1f1dSLionel Sambuc * reads should *never* span multiple packets for DTLS because the
259*0a6a1f1dSLionel Sambuc * underlying transport protocol is message oriented as opposed to
260*0a6a1f1dSLionel Sambuc * byte oriented as in the TLS case.
261*0a6a1f1dSLionel Sambuc */
262*0a6a1f1dSLionel Sambuc if (SSL_version(s) == DTLS1_VERSION
263*0a6a1f1dSLionel Sambuc || SSL_version(s) == DTLS1_BAD_VER) {
264ebfedea0SLionel Sambuc if (n > left)
265ebfedea0SLionel Sambuc n = left; /* makes the while condition false */
266ebfedea0SLionel Sambuc }
267ebfedea0SLionel Sambuc }
268ebfedea0SLionel Sambuc
269ebfedea0SLionel Sambuc /* done reading, now the book-keeping */
270ebfedea0SLionel Sambuc rb->offset += n;
271ebfedea0SLionel Sambuc rb->left = left - n;
272ebfedea0SLionel Sambuc s->packet_length += n;
273ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
274ebfedea0SLionel Sambuc return (n);
275ebfedea0SLionel Sambuc }
276ebfedea0SLionel Sambuc
277*0a6a1f1dSLionel Sambuc /*
278*0a6a1f1dSLionel Sambuc * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
279*0a6a1f1dSLionel Sambuc * will be processed per call to ssl3_get_record. Without this limit an
280*0a6a1f1dSLionel Sambuc * attacker could send empty records at a faster rate than we can process and
281*0a6a1f1dSLionel Sambuc * cause ssl3_get_record to loop forever.
282*0a6a1f1dSLionel Sambuc */
283*0a6a1f1dSLionel Sambuc #define MAX_EMPTY_RECORDS 32
284*0a6a1f1dSLionel Sambuc
285*0a6a1f1dSLionel Sambuc /*-
286*0a6a1f1dSLionel Sambuc * Call this to get a new input record.
287ebfedea0SLionel Sambuc * It will return <= 0 if more data is needed, normally due to an error
288ebfedea0SLionel Sambuc * or non-blocking IO.
289ebfedea0SLionel Sambuc * When it finishes, one packet has been decoded and can be found in
290ebfedea0SLionel Sambuc * ssl->s3->rrec.type - is the type of record
291ebfedea0SLionel Sambuc * ssl->s3->rrec.data, - data
292ebfedea0SLionel Sambuc * ssl->s3->rrec.length, - number of bytes
293ebfedea0SLionel Sambuc */
294ebfedea0SLionel Sambuc /* used only by ssl3_read_bytes */
ssl3_get_record(SSL * s)295ebfedea0SLionel Sambuc static int ssl3_get_record(SSL *s)
296ebfedea0SLionel Sambuc {
297ebfedea0SLionel Sambuc int ssl_major, ssl_minor, al;
298ebfedea0SLionel Sambuc int enc_err, n, i, ret = -1;
299ebfedea0SLionel Sambuc SSL3_RECORD *rr;
300ebfedea0SLionel Sambuc SSL_SESSION *sess;
301ebfedea0SLionel Sambuc unsigned char *p;
302ebfedea0SLionel Sambuc unsigned char md[EVP_MAX_MD_SIZE];
303ebfedea0SLionel Sambuc short version;
304ebfedea0SLionel Sambuc unsigned mac_size, orig_len;
305ebfedea0SLionel Sambuc size_t extra;
306*0a6a1f1dSLionel Sambuc unsigned empty_record_count = 0;
307ebfedea0SLionel Sambuc
308ebfedea0SLionel Sambuc rr = &(s->s3->rrec);
309ebfedea0SLionel Sambuc sess = s->session;
310ebfedea0SLionel Sambuc
311ebfedea0SLionel Sambuc if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
312ebfedea0SLionel Sambuc extra = SSL3_RT_MAX_EXTRA;
313ebfedea0SLionel Sambuc else
314ebfedea0SLionel Sambuc extra = 0;
315*0a6a1f1dSLionel Sambuc if (extra && !s->s3->init_extra) {
316*0a6a1f1dSLionel Sambuc /*
317*0a6a1f1dSLionel Sambuc * An application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER set after
318*0a6a1f1dSLionel Sambuc * ssl3_setup_buffers() was done
319*0a6a1f1dSLionel Sambuc */
320ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
321ebfedea0SLionel Sambuc return -1;
322ebfedea0SLionel Sambuc }
323ebfedea0SLionel Sambuc
324ebfedea0SLionel Sambuc again:
325ebfedea0SLionel Sambuc /* check if we have the header */
326ebfedea0SLionel Sambuc if ((s->rstate != SSL_ST_READ_BODY) ||
327*0a6a1f1dSLionel Sambuc (s->packet_length < SSL3_RT_HEADER_LENGTH)) {
328ebfedea0SLionel Sambuc n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
329*0a6a1f1dSLionel Sambuc if (n <= 0)
330*0a6a1f1dSLionel Sambuc return (n); /* error or non-blocking */
331ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_BODY;
332ebfedea0SLionel Sambuc
333ebfedea0SLionel Sambuc p = s->packet;
334ebfedea0SLionel Sambuc
335ebfedea0SLionel Sambuc /* Pull apart the header into the SSL3_RECORD */
336ebfedea0SLionel Sambuc rr->type = *(p++);
337ebfedea0SLionel Sambuc ssl_major = *(p++);
338ebfedea0SLionel Sambuc ssl_minor = *(p++);
339ebfedea0SLionel Sambuc version = (ssl_major << 8) | ssl_minor;
340ebfedea0SLionel Sambuc n2s(p, rr->length);
341ebfedea0SLionel Sambuc #if 0
342ebfedea0SLionel Sambuc fprintf(stderr, "Record type=%d, Length=%d\n", rr->type, rr->length);
343ebfedea0SLionel Sambuc #endif
344ebfedea0SLionel Sambuc
345ebfedea0SLionel Sambuc /* Lets check version */
346*0a6a1f1dSLionel Sambuc if (!s->first_packet) {
347*0a6a1f1dSLionel Sambuc if (version != s->version) {
348ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
349*0a6a1f1dSLionel Sambuc if ((s->version & 0xFF00) == (version & 0xFF00)
350*0a6a1f1dSLionel Sambuc && !s->enc_write_ctx && !s->write_hash) {
351*0a6a1f1dSLionel Sambuc if (rr->type == SSL3_RT_ALERT) {
352*0a6a1f1dSLionel Sambuc /*
353*0a6a1f1dSLionel Sambuc * The record is using an incorrect version number, but
354*0a6a1f1dSLionel Sambuc * what we've got appears to be an alert. We haven't
355*0a6a1f1dSLionel Sambuc * read the body yet to check whether its a fatal or
356*0a6a1f1dSLionel Sambuc * not - but chances are it is. We probably shouldn't
357*0a6a1f1dSLionel Sambuc * send a fatal alert back. We'll just end.
358*0a6a1f1dSLionel Sambuc */
359*0a6a1f1dSLionel Sambuc goto err;
360*0a6a1f1dSLionel Sambuc }
361*0a6a1f1dSLionel Sambuc /*
362*0a6a1f1dSLionel Sambuc * Send back error using their minor version number :-)
363*0a6a1f1dSLionel Sambuc */
364ebfedea0SLionel Sambuc s->version = (unsigned short)version;
365*0a6a1f1dSLionel Sambuc }
366ebfedea0SLionel Sambuc al = SSL_AD_PROTOCOL_VERSION;
367ebfedea0SLionel Sambuc goto f_err;
368ebfedea0SLionel Sambuc }
369ebfedea0SLionel Sambuc }
370ebfedea0SLionel Sambuc
371*0a6a1f1dSLionel Sambuc if ((version >> 8) != SSL3_VERSION_MAJOR) {
372ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
373ebfedea0SLionel Sambuc goto err;
374ebfedea0SLionel Sambuc }
375ebfedea0SLionel Sambuc
376*0a6a1f1dSLionel Sambuc if (rr->length > s->s3->rbuf.len - SSL3_RT_HEADER_LENGTH) {
377ebfedea0SLionel Sambuc al = SSL_AD_RECORD_OVERFLOW;
378ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
379ebfedea0SLionel Sambuc goto f_err;
380ebfedea0SLionel Sambuc }
381ebfedea0SLionel Sambuc
382ebfedea0SLionel Sambuc /* now s->rstate == SSL_ST_READ_BODY */
383ebfedea0SLionel Sambuc }
384ebfedea0SLionel Sambuc
385ebfedea0SLionel Sambuc /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
386ebfedea0SLionel Sambuc
387*0a6a1f1dSLionel Sambuc if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
388ebfedea0SLionel Sambuc /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
389ebfedea0SLionel Sambuc i = rr->length;
390ebfedea0SLionel Sambuc n = ssl3_read_n(s, i, i, 1);
391*0a6a1f1dSLionel Sambuc if (n <= 0)
392*0a6a1f1dSLionel Sambuc return (n); /* error or non-blocking io */
393*0a6a1f1dSLionel Sambuc /*
394*0a6a1f1dSLionel Sambuc * now n == rr->length, and s->packet_length == SSL3_RT_HEADER_LENGTH
395*0a6a1f1dSLionel Sambuc * + rr->length
396*0a6a1f1dSLionel Sambuc */
397ebfedea0SLionel Sambuc }
398ebfedea0SLionel Sambuc
399ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
400ebfedea0SLionel Sambuc
401*0a6a1f1dSLionel Sambuc /*
402*0a6a1f1dSLionel Sambuc * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
403ebfedea0SLionel Sambuc * and we have that many bytes in s->packet
404ebfedea0SLionel Sambuc */
405ebfedea0SLionel Sambuc rr->input = &(s->packet[SSL3_RT_HEADER_LENGTH]);
406ebfedea0SLionel Sambuc
407*0a6a1f1dSLionel Sambuc /*
408*0a6a1f1dSLionel Sambuc * ok, we can now read from 's->packet' data into 'rr' rr->input points
409*0a6a1f1dSLionel Sambuc * at rr->length bytes, which need to be copied into rr->data by either
410*0a6a1f1dSLionel Sambuc * the decryption or by the decompression When the data is 'copied' into
411*0a6a1f1dSLionel Sambuc * the rr->data buffer, rr->input will be pointed at the new buffer
412*0a6a1f1dSLionel Sambuc */
413ebfedea0SLionel Sambuc
414*0a6a1f1dSLionel Sambuc /*
415*0a6a1f1dSLionel Sambuc * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
416*0a6a1f1dSLionel Sambuc * bytes of encrypted compressed stuff.
417*0a6a1f1dSLionel Sambuc */
418ebfedea0SLionel Sambuc
419ebfedea0SLionel Sambuc /* check is not needed I believe */
420*0a6a1f1dSLionel Sambuc if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
421ebfedea0SLionel Sambuc al = SSL_AD_RECORD_OVERFLOW;
422ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
423ebfedea0SLionel Sambuc goto f_err;
424ebfedea0SLionel Sambuc }
425ebfedea0SLionel Sambuc
426ebfedea0SLionel Sambuc /* decrypt in place in 'rr->input' */
427ebfedea0SLionel Sambuc rr->data = rr->input;
428ebfedea0SLionel Sambuc
429ebfedea0SLionel Sambuc enc_err = s->method->ssl3_enc->enc(s, 0);
430*0a6a1f1dSLionel Sambuc /*-
431*0a6a1f1dSLionel Sambuc * enc_err is:
432ebfedea0SLionel Sambuc * 0: (in non-constant time) if the record is publically invalid.
433ebfedea0SLionel Sambuc * 1: if the padding is valid
434*0a6a1f1dSLionel Sambuc * -1: if the padding is invalid
435*0a6a1f1dSLionel Sambuc */
436*0a6a1f1dSLionel Sambuc if (enc_err == 0) {
437ebfedea0SLionel Sambuc al = SSL_AD_DECRYPTION_FAILED;
438ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
439ebfedea0SLionel Sambuc goto f_err;
440ebfedea0SLionel Sambuc }
441ebfedea0SLionel Sambuc #ifdef TLS_DEBUG
442ebfedea0SLionel Sambuc printf("dec %d\n", rr->length);
443*0a6a1f1dSLionel Sambuc {
444*0a6a1f1dSLionel Sambuc unsigned int z;
445*0a6a1f1dSLionel Sambuc for (z = 0; z < rr->length; z++)
446*0a6a1f1dSLionel Sambuc printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
447*0a6a1f1dSLionel Sambuc }
448ebfedea0SLionel Sambuc printf("\n");
449ebfedea0SLionel Sambuc #endif
450ebfedea0SLionel Sambuc
451ebfedea0SLionel Sambuc /* r->length is now the compressed data plus mac */
452ebfedea0SLionel Sambuc if ((sess != NULL) &&
453*0a6a1f1dSLionel Sambuc (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
454ebfedea0SLionel Sambuc /* s->read_hash != NULL => mac_size != -1 */
455ebfedea0SLionel Sambuc unsigned char *mac = NULL;
456ebfedea0SLionel Sambuc unsigned char mac_tmp[EVP_MAX_MD_SIZE];
457ebfedea0SLionel Sambuc mac_size = EVP_MD_CTX_size(s->read_hash);
458ebfedea0SLionel Sambuc OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
459ebfedea0SLionel Sambuc
460*0a6a1f1dSLionel Sambuc /*
461*0a6a1f1dSLionel Sambuc * kludge: *_cbc_remove_padding passes padding length in rr->type
462*0a6a1f1dSLionel Sambuc */
463ebfedea0SLionel Sambuc orig_len = rr->length + ((unsigned int)rr->type >> 8);
464ebfedea0SLionel Sambuc
465*0a6a1f1dSLionel Sambuc /*
466*0a6a1f1dSLionel Sambuc * orig_len is the length of the record before any padding was
467ebfedea0SLionel Sambuc * removed. This is public information, as is the MAC in use,
468*0a6a1f1dSLionel Sambuc * therefore we can safely process the record in a different amount
469*0a6a1f1dSLionel Sambuc * of time if it's too short to possibly contain a MAC.
470ebfedea0SLionel Sambuc */
471ebfedea0SLionel Sambuc if (orig_len < mac_size ||
472ebfedea0SLionel Sambuc /* CBC records must have a padding length byte too. */
473ebfedea0SLionel Sambuc (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
474*0a6a1f1dSLionel Sambuc orig_len < mac_size + 1)) {
475ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
476ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
477ebfedea0SLionel Sambuc goto f_err;
478ebfedea0SLionel Sambuc }
479ebfedea0SLionel Sambuc
480*0a6a1f1dSLionel Sambuc if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
481*0a6a1f1dSLionel Sambuc /*
482*0a6a1f1dSLionel Sambuc * We update the length so that the TLS header bytes can be
483*0a6a1f1dSLionel Sambuc * constructed correctly but we need to extract the MAC in
484*0a6a1f1dSLionel Sambuc * constant time from within the record, without leaking the
485*0a6a1f1dSLionel Sambuc * contents of the padding bytes.
486*0a6a1f1dSLionel Sambuc */
487ebfedea0SLionel Sambuc mac = mac_tmp;
488ebfedea0SLionel Sambuc ssl3_cbc_copy_mac(mac_tmp, rr, mac_size, orig_len);
489ebfedea0SLionel Sambuc rr->length -= mac_size;
490*0a6a1f1dSLionel Sambuc } else {
491*0a6a1f1dSLionel Sambuc /*
492*0a6a1f1dSLionel Sambuc * In this case there's no padding, so |orig_len| equals
493*0a6a1f1dSLionel Sambuc * |rec->length| and we checked that there's enough bytes for
494*0a6a1f1dSLionel Sambuc * |mac_size| above.
495*0a6a1f1dSLionel Sambuc */
496ebfedea0SLionel Sambuc rr->length -= mac_size;
497ebfedea0SLionel Sambuc mac = &rr->data[rr->length];
498ebfedea0SLionel Sambuc }
499ebfedea0SLionel Sambuc
500ebfedea0SLionel Sambuc i = s->method->ssl3_enc->mac(s, md, 0 /* not send */ );
501*0a6a1f1dSLionel Sambuc if (i < 0 || mac == NULL
502*0a6a1f1dSLionel Sambuc || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
503ebfedea0SLionel Sambuc enc_err = -1;
504ebfedea0SLionel Sambuc if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + extra + mac_size)
505ebfedea0SLionel Sambuc enc_err = -1;
506ebfedea0SLionel Sambuc }
507ebfedea0SLionel Sambuc
508*0a6a1f1dSLionel Sambuc if (enc_err < 0) {
509*0a6a1f1dSLionel Sambuc /*
510*0a6a1f1dSLionel Sambuc * A separate 'decryption_failed' alert was introduced with TLS 1.0,
511ebfedea0SLionel Sambuc * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
512*0a6a1f1dSLionel Sambuc * failure is directly visible from the ciphertext anyway, we should
513*0a6a1f1dSLionel Sambuc * not reveal which kind of error occured -- this might become
514*0a6a1f1dSLionel Sambuc * visible to an attacker (e.g. via a logfile)
515*0a6a1f1dSLionel Sambuc */
516ebfedea0SLionel Sambuc al = SSL_AD_BAD_RECORD_MAC;
517*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD,
518*0a6a1f1dSLionel Sambuc SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
519ebfedea0SLionel Sambuc goto f_err;
520ebfedea0SLionel Sambuc }
521ebfedea0SLionel Sambuc
522ebfedea0SLionel Sambuc /* r->length is now just compressed */
523*0a6a1f1dSLionel Sambuc if (s->expand != NULL) {
524*0a6a1f1dSLionel Sambuc if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + extra) {
525ebfedea0SLionel Sambuc al = SSL_AD_RECORD_OVERFLOW;
526ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
527ebfedea0SLionel Sambuc goto f_err;
528ebfedea0SLionel Sambuc }
529*0a6a1f1dSLionel Sambuc if (!ssl3_do_uncompress(s)) {
530ebfedea0SLionel Sambuc al = SSL_AD_DECOMPRESSION_FAILURE;
531ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
532ebfedea0SLionel Sambuc goto f_err;
533ebfedea0SLionel Sambuc }
534ebfedea0SLionel Sambuc }
535ebfedea0SLionel Sambuc
536*0a6a1f1dSLionel Sambuc if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
537ebfedea0SLionel Sambuc al = SSL_AD_RECORD_OVERFLOW;
538ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
539ebfedea0SLionel Sambuc goto f_err;
540ebfedea0SLionel Sambuc }
541ebfedea0SLionel Sambuc
542ebfedea0SLionel Sambuc rr->off = 0;
543*0a6a1f1dSLionel Sambuc /*-
544*0a6a1f1dSLionel Sambuc * So at this point the following is true
545ebfedea0SLionel Sambuc * ssl->s3->rrec.type is the type of record
546ebfedea0SLionel Sambuc * ssl->s3->rrec.length == number of bytes in record
547ebfedea0SLionel Sambuc * ssl->s3->rrec.off == offset to first valid byte
548ebfedea0SLionel Sambuc * ssl->s3->rrec.data == where to take bytes from, increment
549ebfedea0SLionel Sambuc * after use :-).
550ebfedea0SLionel Sambuc */
551ebfedea0SLionel Sambuc
552ebfedea0SLionel Sambuc /* we have pulled in a full packet so zero things */
553ebfedea0SLionel Sambuc s->packet_length = 0;
554ebfedea0SLionel Sambuc
555ebfedea0SLionel Sambuc /* just read a 0 length packet */
556*0a6a1f1dSLionel Sambuc if (rr->length == 0) {
557*0a6a1f1dSLionel Sambuc empty_record_count++;
558*0a6a1f1dSLionel Sambuc if (empty_record_count > MAX_EMPTY_RECORDS) {
559*0a6a1f1dSLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
560*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
561*0a6a1f1dSLionel Sambuc goto f_err;
562*0a6a1f1dSLionel Sambuc }
563*0a6a1f1dSLionel Sambuc goto again;
564*0a6a1f1dSLionel Sambuc }
565ebfedea0SLionel Sambuc #if 0
566*0a6a1f1dSLionel Sambuc fprintf(stderr, "Ultimate Record type=%d, Length=%d\n", rr->type,
567*0a6a1f1dSLionel Sambuc rr->length);
568ebfedea0SLionel Sambuc #endif
569ebfedea0SLionel Sambuc
570ebfedea0SLionel Sambuc return (1);
571ebfedea0SLionel Sambuc
572ebfedea0SLionel Sambuc f_err:
573ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
574ebfedea0SLionel Sambuc err:
575ebfedea0SLionel Sambuc return (ret);
576ebfedea0SLionel Sambuc }
577ebfedea0SLionel Sambuc
ssl3_do_uncompress(SSL * ssl)578ebfedea0SLionel Sambuc int ssl3_do_uncompress(SSL *ssl)
579ebfedea0SLionel Sambuc {
580ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
581ebfedea0SLionel Sambuc int i;
582ebfedea0SLionel Sambuc SSL3_RECORD *rr;
583ebfedea0SLionel Sambuc
584ebfedea0SLionel Sambuc rr = &(ssl->s3->rrec);
585ebfedea0SLionel Sambuc i = COMP_expand_block(ssl->expand, rr->comp,
586*0a6a1f1dSLionel Sambuc SSL3_RT_MAX_PLAIN_LENGTH, rr->data,
587*0a6a1f1dSLionel Sambuc (int)rr->length);
588ebfedea0SLionel Sambuc if (i < 0)
589ebfedea0SLionel Sambuc return (0);
590ebfedea0SLionel Sambuc else
591ebfedea0SLionel Sambuc rr->length = i;
592ebfedea0SLionel Sambuc rr->data = rr->comp;
593ebfedea0SLionel Sambuc #endif
594ebfedea0SLionel Sambuc return (1);
595ebfedea0SLionel Sambuc }
596ebfedea0SLionel Sambuc
ssl3_do_compress(SSL * ssl)597ebfedea0SLionel Sambuc int ssl3_do_compress(SSL *ssl)
598ebfedea0SLionel Sambuc {
599ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
600ebfedea0SLionel Sambuc int i;
601ebfedea0SLionel Sambuc SSL3_RECORD *wr;
602ebfedea0SLionel Sambuc
603ebfedea0SLionel Sambuc wr = &(ssl->s3->wrec);
604ebfedea0SLionel Sambuc i = COMP_compress_block(ssl->compress, wr->data,
605ebfedea0SLionel Sambuc SSL3_RT_MAX_COMPRESSED_LENGTH,
606ebfedea0SLionel Sambuc wr->input, (int)wr->length);
607ebfedea0SLionel Sambuc if (i < 0)
608ebfedea0SLionel Sambuc return (0);
609ebfedea0SLionel Sambuc else
610ebfedea0SLionel Sambuc wr->length = i;
611ebfedea0SLionel Sambuc
612ebfedea0SLionel Sambuc wr->input = wr->data;
613ebfedea0SLionel Sambuc #endif
614ebfedea0SLionel Sambuc return (1);
615ebfedea0SLionel Sambuc }
616ebfedea0SLionel Sambuc
617*0a6a1f1dSLionel Sambuc /*
618*0a6a1f1dSLionel Sambuc * Call this to write data in records of type 'type' It will return <= 0 if
619*0a6a1f1dSLionel Sambuc * not all data has been sent or non-blocking IO.
620ebfedea0SLionel Sambuc */
ssl3_write_bytes(SSL * s,int type,const void * buf_,int len)621ebfedea0SLionel Sambuc int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
622ebfedea0SLionel Sambuc {
623ebfedea0SLionel Sambuc const unsigned char *buf = buf_;
624*0a6a1f1dSLionel Sambuc unsigned int n, nw;
625*0a6a1f1dSLionel Sambuc int i, tot;
626ebfedea0SLionel Sambuc
627ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
628*0a6a1f1dSLionel Sambuc OPENSSL_assert(s->s3->wnum <= INT_MAX);
629ebfedea0SLionel Sambuc tot = s->s3->wnum;
630ebfedea0SLionel Sambuc s->s3->wnum = 0;
631ebfedea0SLionel Sambuc
632*0a6a1f1dSLionel Sambuc if (SSL_in_init(s) && !s->in_handshake) {
633ebfedea0SLionel Sambuc i = s->handshake_func(s);
634*0a6a1f1dSLionel Sambuc if (i < 0)
635*0a6a1f1dSLionel Sambuc return (i);
636*0a6a1f1dSLionel Sambuc if (i == 0) {
637ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
638ebfedea0SLionel Sambuc return -1;
639ebfedea0SLionel Sambuc }
640ebfedea0SLionel Sambuc }
641ebfedea0SLionel Sambuc
642*0a6a1f1dSLionel Sambuc /*
643*0a6a1f1dSLionel Sambuc * ensure that if we end up with a smaller value of data to write out
644*0a6a1f1dSLionel Sambuc * than the the original len from a write which didn't complete for
645*0a6a1f1dSLionel Sambuc * non-blocking I/O and also somehow ended up avoiding the check for
646*0a6a1f1dSLionel Sambuc * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
647*0a6a1f1dSLionel Sambuc * possible to end up with (len-tot) as a large number that will then
648*0a6a1f1dSLionel Sambuc * promptly send beyond the end of the users buffer ... so we trap and
649*0a6a1f1dSLionel Sambuc * report the error in a way the user will notice
650*0a6a1f1dSLionel Sambuc */
651*0a6a1f1dSLionel Sambuc if (len < tot) {
652*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_BAD_LENGTH);
653*0a6a1f1dSLionel Sambuc return (-1);
654*0a6a1f1dSLionel Sambuc }
655*0a6a1f1dSLionel Sambuc
656ebfedea0SLionel Sambuc n = (len - tot);
657*0a6a1f1dSLionel Sambuc for (;;) {
658ebfedea0SLionel Sambuc if (n > s->max_send_fragment)
659ebfedea0SLionel Sambuc nw = s->max_send_fragment;
660ebfedea0SLionel Sambuc else
661ebfedea0SLionel Sambuc nw = n;
662ebfedea0SLionel Sambuc
663ebfedea0SLionel Sambuc i = do_ssl3_write(s, type, &(buf[tot]), nw, 0);
664*0a6a1f1dSLionel Sambuc if (i <= 0) {
665ebfedea0SLionel Sambuc s->s3->wnum = tot;
666ebfedea0SLionel Sambuc return i;
667ebfedea0SLionel Sambuc }
668ebfedea0SLionel Sambuc
669ebfedea0SLionel Sambuc if ((i == (int)n) ||
670ebfedea0SLionel Sambuc (type == SSL3_RT_APPLICATION_DATA &&
671*0a6a1f1dSLionel Sambuc (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
672*0a6a1f1dSLionel Sambuc /*
673*0a6a1f1dSLionel Sambuc * next chunk of data should get another prepended empty fragment
674*0a6a1f1dSLionel Sambuc * in ciphersuites with known-IV weakness:
675*0a6a1f1dSLionel Sambuc */
676ebfedea0SLionel Sambuc s->s3->empty_fragment_done = 0;
677ebfedea0SLionel Sambuc
678ebfedea0SLionel Sambuc return tot + i;
679ebfedea0SLionel Sambuc }
680ebfedea0SLionel Sambuc
681ebfedea0SLionel Sambuc n -= i;
682ebfedea0SLionel Sambuc tot += i;
683ebfedea0SLionel Sambuc }
684ebfedea0SLionel Sambuc }
685ebfedea0SLionel Sambuc
do_ssl3_write(SSL * s,int type,const unsigned char * buf,unsigned int len,int create_empty_fragment)686ebfedea0SLionel Sambuc static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
687ebfedea0SLionel Sambuc unsigned int len, int create_empty_fragment)
688ebfedea0SLionel Sambuc {
689ebfedea0SLionel Sambuc unsigned char *p, *plen;
690ebfedea0SLionel Sambuc int i, mac_size, clear = 0;
691ebfedea0SLionel Sambuc int prefix_len = 0;
692ebfedea0SLionel Sambuc int eivlen;
693ebfedea0SLionel Sambuc long align = 0;
694ebfedea0SLionel Sambuc SSL3_RECORD *wr;
695ebfedea0SLionel Sambuc SSL3_BUFFER *wb = &(s->s3->wbuf);
696ebfedea0SLionel Sambuc SSL_SESSION *sess;
697ebfedea0SLionel Sambuc
698*0a6a1f1dSLionel Sambuc /*
699*0a6a1f1dSLionel Sambuc * first check if there is a SSL3_BUFFER still being written out. This
700*0a6a1f1dSLionel Sambuc * will happen with non blocking IO
701*0a6a1f1dSLionel Sambuc */
702ebfedea0SLionel Sambuc if (wb->left != 0)
703ebfedea0SLionel Sambuc return (ssl3_write_pending(s, type, buf, len));
704ebfedea0SLionel Sambuc
705ebfedea0SLionel Sambuc /* If we have an alert to send, lets send it */
706*0a6a1f1dSLionel Sambuc if (s->s3->alert_dispatch) {
707ebfedea0SLionel Sambuc i = s->method->ssl_dispatch_alert(s);
708ebfedea0SLionel Sambuc if (i <= 0)
709ebfedea0SLionel Sambuc return (i);
710ebfedea0SLionel Sambuc /* if it went, fall through and send more stuff */
711*0a6a1f1dSLionel Sambuc /* we may have released our buffer, so get it again */
712*0a6a1f1dSLionel Sambuc if (wb->buf == NULL)
713*0a6a1f1dSLionel Sambuc if (!ssl3_setup_write_buffer(s))
714*0a6a1f1dSLionel Sambuc return -1;
715ebfedea0SLionel Sambuc }
716ebfedea0SLionel Sambuc
717*0a6a1f1dSLionel Sambuc if (wb->buf == NULL)
718*0a6a1f1dSLionel Sambuc if (!ssl3_setup_write_buffer(s))
719*0a6a1f1dSLionel Sambuc return -1;
720*0a6a1f1dSLionel Sambuc
721ebfedea0SLionel Sambuc if (len == 0 && !create_empty_fragment)
722ebfedea0SLionel Sambuc return 0;
723ebfedea0SLionel Sambuc
724ebfedea0SLionel Sambuc wr = &(s->s3->wrec);
725ebfedea0SLionel Sambuc sess = s->session;
726ebfedea0SLionel Sambuc
727ebfedea0SLionel Sambuc if ((sess == NULL) ||
728ebfedea0SLionel Sambuc (s->enc_write_ctx == NULL) ||
729*0a6a1f1dSLionel Sambuc (EVP_MD_CTX_md(s->write_hash) == NULL)) {
730ebfedea0SLionel Sambuc #if 1
731ebfedea0SLionel Sambuc clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */
732ebfedea0SLionel Sambuc #else
733ebfedea0SLionel Sambuc clear = 1;
734ebfedea0SLionel Sambuc #endif
735ebfedea0SLionel Sambuc mac_size = 0;
736*0a6a1f1dSLionel Sambuc } else {
737ebfedea0SLionel Sambuc mac_size = EVP_MD_CTX_size(s->write_hash);
738ebfedea0SLionel Sambuc if (mac_size < 0)
739ebfedea0SLionel Sambuc goto err;
740ebfedea0SLionel Sambuc }
741ebfedea0SLionel Sambuc
742*0a6a1f1dSLionel Sambuc /*
743*0a6a1f1dSLionel Sambuc * 'create_empty_fragment' is true only when this function calls itself
744*0a6a1f1dSLionel Sambuc */
745*0a6a1f1dSLionel Sambuc if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {
746*0a6a1f1dSLionel Sambuc /*
747*0a6a1f1dSLionel Sambuc * countermeasure against known-IV weakness in CBC ciphersuites (see
748*0a6a1f1dSLionel Sambuc * http://www.openssl.org/~bodo/tls-cbc.txt)
749*0a6a1f1dSLionel Sambuc */
750ebfedea0SLionel Sambuc
751*0a6a1f1dSLionel Sambuc if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
752*0a6a1f1dSLionel Sambuc /*
753*0a6a1f1dSLionel Sambuc * recursive function call with 'create_empty_fragment' set; this
754*0a6a1f1dSLionel Sambuc * prepares and buffers the data for an empty fragment (these
755*0a6a1f1dSLionel Sambuc * 'prefix_len' bytes are sent out later together with the actual
756*0a6a1f1dSLionel Sambuc * payload)
757*0a6a1f1dSLionel Sambuc */
758ebfedea0SLionel Sambuc prefix_len = do_ssl3_write(s, type, buf, 0, 1);
759ebfedea0SLionel Sambuc if (prefix_len <= 0)
760ebfedea0SLionel Sambuc goto err;
761ebfedea0SLionel Sambuc
762ebfedea0SLionel Sambuc if (prefix_len >
763ebfedea0SLionel Sambuc (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD))
764ebfedea0SLionel Sambuc {
765ebfedea0SLionel Sambuc /* insufficient space */
766ebfedea0SLionel Sambuc SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
767ebfedea0SLionel Sambuc goto err;
768ebfedea0SLionel Sambuc }
769ebfedea0SLionel Sambuc }
770ebfedea0SLionel Sambuc
771ebfedea0SLionel Sambuc s->s3->empty_fragment_done = 1;
772ebfedea0SLionel Sambuc }
773ebfedea0SLionel Sambuc
774*0a6a1f1dSLionel Sambuc if (create_empty_fragment) {
775ebfedea0SLionel Sambuc #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
776*0a6a1f1dSLionel Sambuc /*
777*0a6a1f1dSLionel Sambuc * extra fragment would be couple of cipher blocks, which would be
778*0a6a1f1dSLionel Sambuc * multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real
779*0a6a1f1dSLionel Sambuc * payload, then we can just pretent we simply have two headers.
780*0a6a1f1dSLionel Sambuc */
781ebfedea0SLionel Sambuc align = (long)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
782ebfedea0SLionel Sambuc align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
783ebfedea0SLionel Sambuc #endif
784ebfedea0SLionel Sambuc p = wb->buf + align;
785ebfedea0SLionel Sambuc wb->offset = align;
786*0a6a1f1dSLionel Sambuc } else if (prefix_len) {
787ebfedea0SLionel Sambuc p = wb->buf + wb->offset + prefix_len;
788*0a6a1f1dSLionel Sambuc } else {
789ebfedea0SLionel Sambuc #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
790ebfedea0SLionel Sambuc align = (long)wb->buf + SSL3_RT_HEADER_LENGTH;
791ebfedea0SLionel Sambuc align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
792ebfedea0SLionel Sambuc #endif
793ebfedea0SLionel Sambuc p = wb->buf + align;
794ebfedea0SLionel Sambuc wb->offset = align;
795ebfedea0SLionel Sambuc }
796ebfedea0SLionel Sambuc
797ebfedea0SLionel Sambuc /* write the header */
798ebfedea0SLionel Sambuc
799ebfedea0SLionel Sambuc *(p++) = type & 0xff;
800ebfedea0SLionel Sambuc wr->type = type;
801ebfedea0SLionel Sambuc
802ebfedea0SLionel Sambuc *(p++) = (s->version >> 8);
803*0a6a1f1dSLionel Sambuc /*
804*0a6a1f1dSLionel Sambuc * Some servers hang if iniatial client hello is larger than 256 bytes
805*0a6a1f1dSLionel Sambuc * and record version number > TLS 1.0
806ebfedea0SLionel Sambuc */
807ebfedea0SLionel Sambuc if (s->state == SSL3_ST_CW_CLNT_HELLO_B
808*0a6a1f1dSLionel Sambuc && !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION)
809ebfedea0SLionel Sambuc *(p++) = 0x1;
810ebfedea0SLionel Sambuc else
811ebfedea0SLionel Sambuc *(p++) = s->version & 0xff;
812ebfedea0SLionel Sambuc
813ebfedea0SLionel Sambuc /* field where we are to write out packet length */
814ebfedea0SLionel Sambuc plen = p;
815ebfedea0SLionel Sambuc p += 2;
816ebfedea0SLionel Sambuc /* Explicit IV length, block ciphers and TLS version 1.1 or later */
817*0a6a1f1dSLionel Sambuc if (s->enc_write_ctx && s->version >= TLS1_1_VERSION) {
818ebfedea0SLionel Sambuc int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
819*0a6a1f1dSLionel Sambuc if (mode == EVP_CIPH_CBC_MODE) {
820ebfedea0SLionel Sambuc eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
821ebfedea0SLionel Sambuc if (eivlen <= 1)
822ebfedea0SLionel Sambuc eivlen = 0;
823ebfedea0SLionel Sambuc }
824ebfedea0SLionel Sambuc /* Need explicit part of IV for GCM mode */
825ebfedea0SLionel Sambuc else if (mode == EVP_CIPH_GCM_MODE)
826ebfedea0SLionel Sambuc eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
827ebfedea0SLionel Sambuc else
828ebfedea0SLionel Sambuc eivlen = 0;
829*0a6a1f1dSLionel Sambuc } else
830ebfedea0SLionel Sambuc eivlen = 0;
831ebfedea0SLionel Sambuc
832ebfedea0SLionel Sambuc /* lets setup the record stuff. */
833ebfedea0SLionel Sambuc wr->data = p + eivlen;
834ebfedea0SLionel Sambuc wr->length = (int)len;
835ebfedea0SLionel Sambuc wr->input = (unsigned char *)buf;
836ebfedea0SLionel Sambuc
837*0a6a1f1dSLionel Sambuc /*
838*0a6a1f1dSLionel Sambuc * we now 'read' from wr->input, wr->length bytes into wr->data
839*0a6a1f1dSLionel Sambuc */
840ebfedea0SLionel Sambuc
841ebfedea0SLionel Sambuc /* first we compress */
842*0a6a1f1dSLionel Sambuc if (s->compress != NULL) {
843*0a6a1f1dSLionel Sambuc if (!ssl3_do_compress(s)) {
844ebfedea0SLionel Sambuc SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE);
845ebfedea0SLionel Sambuc goto err;
846ebfedea0SLionel Sambuc }
847*0a6a1f1dSLionel Sambuc } else {
848ebfedea0SLionel Sambuc memcpy(wr->data, wr->input, wr->length);
849ebfedea0SLionel Sambuc wr->input = wr->data;
850ebfedea0SLionel Sambuc }
851ebfedea0SLionel Sambuc
852*0a6a1f1dSLionel Sambuc /*
853*0a6a1f1dSLionel Sambuc * we should still have the output to wr->data and the input from
854*0a6a1f1dSLionel Sambuc * wr->input. Length should be wr->length. wr->data still points in the
855*0a6a1f1dSLionel Sambuc * wb->buf
856*0a6a1f1dSLionel Sambuc */
857ebfedea0SLionel Sambuc
858*0a6a1f1dSLionel Sambuc if (mac_size != 0) {
859ebfedea0SLionel Sambuc if (s->method->ssl3_enc->mac(s, &(p[wr->length + eivlen]), 1) < 0)
860ebfedea0SLionel Sambuc goto err;
861ebfedea0SLionel Sambuc wr->length += mac_size;
862ebfedea0SLionel Sambuc }
863ebfedea0SLionel Sambuc
864ebfedea0SLionel Sambuc wr->input = p;
865ebfedea0SLionel Sambuc wr->data = p;
866ebfedea0SLionel Sambuc
867*0a6a1f1dSLionel Sambuc if (eivlen) {
868*0a6a1f1dSLionel Sambuc /*
869*0a6a1f1dSLionel Sambuc * if (RAND_pseudo_bytes(p, eivlen) <= 0) goto err;
870*0a6a1f1dSLionel Sambuc */
871ebfedea0SLionel Sambuc wr->length += eivlen;
872ebfedea0SLionel Sambuc }
873ebfedea0SLionel Sambuc
874*0a6a1f1dSLionel Sambuc if (s->method->ssl3_enc->enc(s, 1) < 1)
875*0a6a1f1dSLionel Sambuc goto err;
876ebfedea0SLionel Sambuc
877ebfedea0SLionel Sambuc /* record length after mac and block padding */
878ebfedea0SLionel Sambuc s2n(wr->length, plen);
879ebfedea0SLionel Sambuc
880*0a6a1f1dSLionel Sambuc /*
881*0a6a1f1dSLionel Sambuc * we should now have wr->data pointing to the encrypted data, which is
882*0a6a1f1dSLionel Sambuc * wr->length long
883*0a6a1f1dSLionel Sambuc */
884ebfedea0SLionel Sambuc wr->type = type; /* not needed but helps for debugging */
885ebfedea0SLionel Sambuc wr->length += SSL3_RT_HEADER_LENGTH;
886ebfedea0SLionel Sambuc
887*0a6a1f1dSLionel Sambuc if (create_empty_fragment) {
888*0a6a1f1dSLionel Sambuc /*
889*0a6a1f1dSLionel Sambuc * we are in a recursive call; just return the length, don't write
890*0a6a1f1dSLionel Sambuc * out anything here
891ebfedea0SLionel Sambuc */
892ebfedea0SLionel Sambuc return wr->length;
893ebfedea0SLionel Sambuc }
894ebfedea0SLionel Sambuc
895ebfedea0SLionel Sambuc /* now let's set up wb */
896ebfedea0SLionel Sambuc wb->left = prefix_len + wr->length;
897ebfedea0SLionel Sambuc
898*0a6a1f1dSLionel Sambuc /*
899*0a6a1f1dSLionel Sambuc * memorize arguments so that ssl3_write_pending can detect bad write
900*0a6a1f1dSLionel Sambuc * retries later
901*0a6a1f1dSLionel Sambuc */
902ebfedea0SLionel Sambuc s->s3->wpend_tot = len;
903ebfedea0SLionel Sambuc s->s3->wpend_buf = buf;
904ebfedea0SLionel Sambuc s->s3->wpend_type = type;
905ebfedea0SLionel Sambuc s->s3->wpend_ret = len;
906ebfedea0SLionel Sambuc
907ebfedea0SLionel Sambuc /* we now just need to write the buffer */
908ebfedea0SLionel Sambuc return ssl3_write_pending(s, type, buf, len);
909ebfedea0SLionel Sambuc err:
910ebfedea0SLionel Sambuc return -1;
911ebfedea0SLionel Sambuc }
912ebfedea0SLionel Sambuc
913ebfedea0SLionel Sambuc /* if s->s3->wbuf.left != 0, we need to call this */
ssl3_write_pending(SSL * s,int type,const unsigned char * buf,unsigned int len)914ebfedea0SLionel Sambuc int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
915ebfedea0SLionel Sambuc unsigned int len)
916ebfedea0SLionel Sambuc {
917ebfedea0SLionel Sambuc int i;
918ebfedea0SLionel Sambuc SSL3_BUFFER *wb = &(s->s3->wbuf);
919ebfedea0SLionel Sambuc
920ebfedea0SLionel Sambuc /* XXXX */
921ebfedea0SLionel Sambuc if ((s->s3->wpend_tot > (int)len)
922ebfedea0SLionel Sambuc || ((s->s3->wpend_buf != buf) &&
923ebfedea0SLionel Sambuc !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
924*0a6a1f1dSLionel Sambuc || (s->s3->wpend_type != type)) {
925ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
926ebfedea0SLionel Sambuc return (-1);
927ebfedea0SLionel Sambuc }
928ebfedea0SLionel Sambuc
929*0a6a1f1dSLionel Sambuc for (;;) {
930ebfedea0SLionel Sambuc clear_sys_error();
931*0a6a1f1dSLionel Sambuc if (s->wbio != NULL) {
932ebfedea0SLionel Sambuc s->rwstate = SSL_WRITING;
933ebfedea0SLionel Sambuc i = BIO_write(s->wbio,
934ebfedea0SLionel Sambuc (char *)&(wb->buf[wb->offset]),
935ebfedea0SLionel Sambuc (unsigned int)wb->left);
936*0a6a1f1dSLionel Sambuc } else {
937ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
938ebfedea0SLionel Sambuc i = -1;
939ebfedea0SLionel Sambuc }
940*0a6a1f1dSLionel Sambuc if (i == wb->left) {
941ebfedea0SLionel Sambuc wb->left = 0;
942ebfedea0SLionel Sambuc wb->offset += i;
943ebfedea0SLionel Sambuc if (s->mode & SSL_MODE_RELEASE_BUFFERS &&
944*0a6a1f1dSLionel Sambuc SSL_version(s) != DTLS1_VERSION
945*0a6a1f1dSLionel Sambuc && SSL_version(s) != DTLS1_BAD_VER)
946ebfedea0SLionel Sambuc ssl3_release_write_buffer(s);
947ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
948ebfedea0SLionel Sambuc return (s->s3->wpend_ret);
949*0a6a1f1dSLionel Sambuc } else if (i <= 0) {
950*0a6a1f1dSLionel Sambuc if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
951*0a6a1f1dSLionel Sambuc /*
952*0a6a1f1dSLionel Sambuc * For DTLS, just drop it. That's kind of the whole point in
953*0a6a1f1dSLionel Sambuc * using a datagram service
954*0a6a1f1dSLionel Sambuc */
955ebfedea0SLionel Sambuc wb->left = 0;
956ebfedea0SLionel Sambuc }
957ebfedea0SLionel Sambuc return (i);
958ebfedea0SLionel Sambuc }
959ebfedea0SLionel Sambuc wb->offset += i;
960ebfedea0SLionel Sambuc wb->left -= i;
961ebfedea0SLionel Sambuc }
962ebfedea0SLionel Sambuc }
963ebfedea0SLionel Sambuc
964*0a6a1f1dSLionel Sambuc /*-
965*0a6a1f1dSLionel Sambuc * Return up to 'len' payload bytes received in 'type' records.
966ebfedea0SLionel Sambuc * 'type' is one of the following:
967ebfedea0SLionel Sambuc *
968ebfedea0SLionel Sambuc * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
969ebfedea0SLionel Sambuc * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
970ebfedea0SLionel Sambuc * - 0 (during a shutdown, no data has to be returned)
971ebfedea0SLionel Sambuc *
972ebfedea0SLionel Sambuc * If we don't have stored data to work from, read a SSL/TLS record first
973ebfedea0SLionel Sambuc * (possibly multiple records if we still don't have anything to return).
974ebfedea0SLionel Sambuc *
975ebfedea0SLionel Sambuc * This function must handle any surprises the peer may have for us, such as
976ebfedea0SLionel Sambuc * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
977ebfedea0SLionel Sambuc * a surprise, but handled as if it were), or renegotiation requests.
978ebfedea0SLionel Sambuc * Also if record payloads contain fragments too small to process, we store
979ebfedea0SLionel Sambuc * them until there is enough for the respective protocol (the record protocol
980ebfedea0SLionel Sambuc * may use arbitrary fragmentation and even interleaving):
981ebfedea0SLionel Sambuc * Change cipher spec protocol
982ebfedea0SLionel Sambuc * just 1 byte needed, no need for keeping anything stored
983ebfedea0SLionel Sambuc * Alert protocol
984ebfedea0SLionel Sambuc * 2 bytes needed (AlertLevel, AlertDescription)
985ebfedea0SLionel Sambuc * Handshake protocol
986ebfedea0SLionel Sambuc * 4 bytes needed (HandshakeType, uint24 length) -- we just have
987ebfedea0SLionel Sambuc * to detect unexpected Client Hello and Hello Request messages
988ebfedea0SLionel Sambuc * here, anything else is handled by higher layers
989ebfedea0SLionel Sambuc * Application data protocol
990ebfedea0SLionel Sambuc * none of our business
991ebfedea0SLionel Sambuc */
ssl3_read_bytes(SSL * s,int type,unsigned char * buf,int len,int peek)992ebfedea0SLionel Sambuc int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
993ebfedea0SLionel Sambuc {
994ebfedea0SLionel Sambuc int al, i, j, ret;
995ebfedea0SLionel Sambuc unsigned int n;
996ebfedea0SLionel Sambuc SSL3_RECORD *rr;
997ebfedea0SLionel Sambuc void (*cb) (const SSL *ssl, int type2, int val) = NULL;
998ebfedea0SLionel Sambuc
999ebfedea0SLionel Sambuc if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
1000ebfedea0SLionel Sambuc if (!ssl3_setup_read_buffer(s))
1001ebfedea0SLionel Sambuc return (-1);
1002ebfedea0SLionel Sambuc
1003*0a6a1f1dSLionel Sambuc if ((type && (type != SSL3_RT_APPLICATION_DATA)
1004*0a6a1f1dSLionel Sambuc && (type != SSL3_RT_HANDSHAKE)) || (peek
1005*0a6a1f1dSLionel Sambuc && (type !=
1006*0a6a1f1dSLionel Sambuc SSL3_RT_APPLICATION_DATA))) {
1007ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
1008ebfedea0SLionel Sambuc return -1;
1009ebfedea0SLionel Sambuc }
1010ebfedea0SLionel Sambuc
1011ebfedea0SLionel Sambuc if ((type == SSL3_RT_HANDSHAKE) && (s->s3->handshake_fragment_len > 0))
1012ebfedea0SLionel Sambuc /* (partially) satisfy request from storage */
1013ebfedea0SLionel Sambuc {
1014ebfedea0SLionel Sambuc unsigned char *src = s->s3->handshake_fragment;
1015ebfedea0SLionel Sambuc unsigned char *dst = buf;
1016ebfedea0SLionel Sambuc unsigned int k;
1017ebfedea0SLionel Sambuc
1018ebfedea0SLionel Sambuc /* peek == 0 */
1019ebfedea0SLionel Sambuc n = 0;
1020*0a6a1f1dSLionel Sambuc while ((len > 0) && (s->s3->handshake_fragment_len > 0)) {
1021ebfedea0SLionel Sambuc *dst++ = *src++;
1022*0a6a1f1dSLionel Sambuc len--;
1023*0a6a1f1dSLionel Sambuc s->s3->handshake_fragment_len--;
1024ebfedea0SLionel Sambuc n++;
1025ebfedea0SLionel Sambuc }
1026ebfedea0SLionel Sambuc /* move any remaining fragment bytes: */
1027ebfedea0SLionel Sambuc for (k = 0; k < s->s3->handshake_fragment_len; k++)
1028ebfedea0SLionel Sambuc s->s3->handshake_fragment[k] = *src++;
1029ebfedea0SLionel Sambuc return n;
1030ebfedea0SLionel Sambuc }
1031ebfedea0SLionel Sambuc
1032*0a6a1f1dSLionel Sambuc /*
1033*0a6a1f1dSLionel Sambuc * Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
1034*0a6a1f1dSLionel Sambuc */
1035ebfedea0SLionel Sambuc
1036*0a6a1f1dSLionel Sambuc if (!s->in_handshake && SSL_in_init(s)) {
1037ebfedea0SLionel Sambuc /* type == SSL3_RT_APPLICATION_DATA */
1038ebfedea0SLionel Sambuc i = s->handshake_func(s);
1039*0a6a1f1dSLionel Sambuc if (i < 0)
1040*0a6a1f1dSLionel Sambuc return (i);
1041*0a6a1f1dSLionel Sambuc if (i == 0) {
1042ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
1043ebfedea0SLionel Sambuc return (-1);
1044ebfedea0SLionel Sambuc }
1045ebfedea0SLionel Sambuc }
1046ebfedea0SLionel Sambuc start:
1047ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
1048ebfedea0SLionel Sambuc
1049*0a6a1f1dSLionel Sambuc /*-
1050*0a6a1f1dSLionel Sambuc * s->s3->rrec.type - is the type of record
1051ebfedea0SLionel Sambuc * s->s3->rrec.data, - data
1052ebfedea0SLionel Sambuc * s->s3->rrec.off, - offset into 'data' for next read
1053*0a6a1f1dSLionel Sambuc * s->s3->rrec.length, - number of bytes.
1054*0a6a1f1dSLionel Sambuc */
1055ebfedea0SLionel Sambuc rr = &(s->s3->rrec);
1056ebfedea0SLionel Sambuc
1057ebfedea0SLionel Sambuc /* get new packet if necessary */
1058*0a6a1f1dSLionel Sambuc if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) {
1059ebfedea0SLionel Sambuc ret = ssl3_get_record(s);
1060*0a6a1f1dSLionel Sambuc if (ret <= 0)
1061*0a6a1f1dSLionel Sambuc return (ret);
1062ebfedea0SLionel Sambuc }
1063ebfedea0SLionel Sambuc
1064ebfedea0SLionel Sambuc /* we now have a packet which can be read and processed */
1065ebfedea0SLionel Sambuc
1066ebfedea0SLionel Sambuc if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
1067ebfedea0SLionel Sambuc * reset by ssl3_get_finished */
1068*0a6a1f1dSLionel Sambuc && (rr->type != SSL3_RT_HANDSHAKE)) {
1069ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1070ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
1071ebfedea0SLionel Sambuc goto f_err;
1072ebfedea0SLionel Sambuc }
1073ebfedea0SLionel Sambuc
1074*0a6a1f1dSLionel Sambuc /*
1075*0a6a1f1dSLionel Sambuc * If the other end has shut down, throw anything we read away (even in
1076*0a6a1f1dSLionel Sambuc * 'peek' mode)
1077*0a6a1f1dSLionel Sambuc */
1078*0a6a1f1dSLionel Sambuc if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
1079ebfedea0SLionel Sambuc rr->length = 0;
1080ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
1081ebfedea0SLionel Sambuc return (0);
1082ebfedea0SLionel Sambuc }
1083ebfedea0SLionel Sambuc
1084*0a6a1f1dSLionel Sambuc if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or
1085*0a6a1f1dSLionel Sambuc * SSL3_RT_HANDSHAKE */
1086*0a6a1f1dSLionel Sambuc /*
1087*0a6a1f1dSLionel Sambuc * make sure that we are not getting application data when we are
1088*0a6a1f1dSLionel Sambuc * doing a handshake for the first time
1089*0a6a1f1dSLionel Sambuc */
1090ebfedea0SLionel Sambuc if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
1091*0a6a1f1dSLionel Sambuc (s->enc_read_ctx == NULL)) {
1092ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1093ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
1094ebfedea0SLionel Sambuc goto f_err;
1095ebfedea0SLionel Sambuc }
1096ebfedea0SLionel Sambuc
1097*0a6a1f1dSLionel Sambuc if (len <= 0)
1098*0a6a1f1dSLionel Sambuc return (len);
1099ebfedea0SLionel Sambuc
1100ebfedea0SLionel Sambuc if ((unsigned int)len > rr->length)
1101ebfedea0SLionel Sambuc n = rr->length;
1102ebfedea0SLionel Sambuc else
1103ebfedea0SLionel Sambuc n = (unsigned int)len;
1104ebfedea0SLionel Sambuc
1105ebfedea0SLionel Sambuc memcpy(buf, &(rr->data[rr->off]), n);
1106*0a6a1f1dSLionel Sambuc if (!peek) {
1107ebfedea0SLionel Sambuc rr->length -= n;
1108ebfedea0SLionel Sambuc rr->off += n;
1109*0a6a1f1dSLionel Sambuc if (rr->length == 0) {
1110ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_HEADER;
1111ebfedea0SLionel Sambuc rr->off = 0;
1112*0a6a1f1dSLionel Sambuc if (s->mode & SSL_MODE_RELEASE_BUFFERS
1113*0a6a1f1dSLionel Sambuc && s->s3->rbuf.left == 0)
1114ebfedea0SLionel Sambuc ssl3_release_read_buffer(s);
1115ebfedea0SLionel Sambuc }
1116ebfedea0SLionel Sambuc }
1117ebfedea0SLionel Sambuc return (n);
1118ebfedea0SLionel Sambuc }
1119ebfedea0SLionel Sambuc
1120*0a6a1f1dSLionel Sambuc /*
1121*0a6a1f1dSLionel Sambuc * If we get here, then type != rr->type; if we have a handshake message,
1122*0a6a1f1dSLionel Sambuc * then it was unexpected (Hello Request or Client Hello).
1123*0a6a1f1dSLionel Sambuc */
1124ebfedea0SLionel Sambuc
1125*0a6a1f1dSLionel Sambuc /*
1126*0a6a1f1dSLionel Sambuc * In case of record types for which we have 'fragment' storage, fill
1127*0a6a1f1dSLionel Sambuc * that so that we can process the data at a fixed place.
1128ebfedea0SLionel Sambuc */
1129ebfedea0SLionel Sambuc {
1130ebfedea0SLionel Sambuc unsigned int dest_maxlen = 0;
1131ebfedea0SLionel Sambuc unsigned char *dest = NULL;
1132ebfedea0SLionel Sambuc unsigned int *dest_len = NULL;
1133ebfedea0SLionel Sambuc
1134*0a6a1f1dSLionel Sambuc if (rr->type == SSL3_RT_HANDSHAKE) {
1135ebfedea0SLionel Sambuc dest_maxlen = sizeof s->s3->handshake_fragment;
1136ebfedea0SLionel Sambuc dest = s->s3->handshake_fragment;
1137ebfedea0SLionel Sambuc dest_len = &s->s3->handshake_fragment_len;
1138*0a6a1f1dSLionel Sambuc } else if (rr->type == SSL3_RT_ALERT) {
1139ebfedea0SLionel Sambuc dest_maxlen = sizeof s->s3->alert_fragment;
1140ebfedea0SLionel Sambuc dest = s->s3->alert_fragment;
1141ebfedea0SLionel Sambuc dest_len = &s->s3->alert_fragment_len;
1142ebfedea0SLionel Sambuc }
1143ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HEARTBEATS
1144*0a6a1f1dSLionel Sambuc else if (rr->type == TLS1_RT_HEARTBEAT) {
1145ebfedea0SLionel Sambuc tls1_process_heartbeat(s);
1146ebfedea0SLionel Sambuc
1147ebfedea0SLionel Sambuc /* Exit and notify application to read again */
1148ebfedea0SLionel Sambuc rr->length = 0;
1149ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
1150ebfedea0SLionel Sambuc BIO_clear_retry_flags(SSL_get_rbio(s));
1151ebfedea0SLionel Sambuc BIO_set_retry_read(SSL_get_rbio(s));
1152ebfedea0SLionel Sambuc return (-1);
1153ebfedea0SLionel Sambuc }
1154ebfedea0SLionel Sambuc #endif
1155ebfedea0SLionel Sambuc
1156*0a6a1f1dSLionel Sambuc if (dest_maxlen > 0) {
1157ebfedea0SLionel Sambuc n = dest_maxlen - *dest_len; /* available space in 'dest' */
1158ebfedea0SLionel Sambuc if (rr->length < n)
1159ebfedea0SLionel Sambuc n = rr->length; /* available bytes */
1160ebfedea0SLionel Sambuc
1161ebfedea0SLionel Sambuc /* now move 'n' bytes: */
1162*0a6a1f1dSLionel Sambuc while (n-- > 0) {
1163ebfedea0SLionel Sambuc dest[(*dest_len)++] = rr->data[rr->off++];
1164ebfedea0SLionel Sambuc rr->length--;
1165ebfedea0SLionel Sambuc }
1166ebfedea0SLionel Sambuc
1167ebfedea0SLionel Sambuc if (*dest_len < dest_maxlen)
1168ebfedea0SLionel Sambuc goto start; /* fragment was too small */
1169ebfedea0SLionel Sambuc }
1170ebfedea0SLionel Sambuc }
1171ebfedea0SLionel Sambuc
1172*0a6a1f1dSLionel Sambuc /*-
1173*0a6a1f1dSLionel Sambuc * s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
1174ebfedea0SLionel Sambuc * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
1175*0a6a1f1dSLionel Sambuc * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
1176*0a6a1f1dSLionel Sambuc */
1177ebfedea0SLionel Sambuc
1178ebfedea0SLionel Sambuc /* If we are a client, check for an incoming 'Hello Request': */
1179ebfedea0SLionel Sambuc if ((!s->server) &&
1180ebfedea0SLionel Sambuc (s->s3->handshake_fragment_len >= 4) &&
1181ebfedea0SLionel Sambuc (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
1182*0a6a1f1dSLionel Sambuc (s->session != NULL) && (s->session->cipher != NULL)) {
1183ebfedea0SLionel Sambuc s->s3->handshake_fragment_len = 0;
1184ebfedea0SLionel Sambuc
1185ebfedea0SLionel Sambuc if ((s->s3->handshake_fragment[1] != 0) ||
1186ebfedea0SLionel Sambuc (s->s3->handshake_fragment[2] != 0) ||
1187*0a6a1f1dSLionel Sambuc (s->s3->handshake_fragment[3] != 0)) {
1188ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1189ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
1190ebfedea0SLionel Sambuc goto f_err;
1191ebfedea0SLionel Sambuc }
1192ebfedea0SLionel Sambuc
1193ebfedea0SLionel Sambuc if (s->msg_callback)
1194*0a6a1f1dSLionel Sambuc s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1195*0a6a1f1dSLionel Sambuc s->s3->handshake_fragment, 4, s,
1196*0a6a1f1dSLionel Sambuc s->msg_callback_arg);
1197ebfedea0SLionel Sambuc
1198ebfedea0SLionel Sambuc if (SSL_is_init_finished(s) &&
1199ebfedea0SLionel Sambuc !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
1200*0a6a1f1dSLionel Sambuc !s->s3->renegotiate) {
1201ebfedea0SLionel Sambuc ssl3_renegotiate(s);
1202*0a6a1f1dSLionel Sambuc if (ssl3_renegotiate_check(s)) {
1203ebfedea0SLionel Sambuc i = s->handshake_func(s);
1204*0a6a1f1dSLionel Sambuc if (i < 0)
1205*0a6a1f1dSLionel Sambuc return (i);
1206*0a6a1f1dSLionel Sambuc if (i == 0) {
1207*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES,
1208*0a6a1f1dSLionel Sambuc SSL_R_SSL_HANDSHAKE_FAILURE);
1209ebfedea0SLionel Sambuc return (-1);
1210ebfedea0SLionel Sambuc }
1211ebfedea0SLionel Sambuc
1212*0a6a1f1dSLionel Sambuc if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
1213*0a6a1f1dSLionel Sambuc if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
1214ebfedea0SLionel Sambuc BIO *bio;
1215*0a6a1f1dSLionel Sambuc /*
1216*0a6a1f1dSLionel Sambuc * In the case where we try to read application data,
1217ebfedea0SLionel Sambuc * but we trigger an SSL handshake, we return -1 with
1218ebfedea0SLionel Sambuc * the retry option set. Otherwise renegotiation may
1219*0a6a1f1dSLionel Sambuc * cause nasty problems in the blocking world
1220*0a6a1f1dSLionel Sambuc */
1221ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
1222ebfedea0SLionel Sambuc bio = SSL_get_rbio(s);
1223ebfedea0SLionel Sambuc BIO_clear_retry_flags(bio);
1224ebfedea0SLionel Sambuc BIO_set_retry_read(bio);
1225ebfedea0SLionel Sambuc return (-1);
1226ebfedea0SLionel Sambuc }
1227ebfedea0SLionel Sambuc }
1228ebfedea0SLionel Sambuc }
1229ebfedea0SLionel Sambuc }
1230*0a6a1f1dSLionel Sambuc /*
1231*0a6a1f1dSLionel Sambuc * we either finished a handshake or ignored the request, now try
1232*0a6a1f1dSLionel Sambuc * again to obtain the (application) data we were asked for
1233*0a6a1f1dSLionel Sambuc */
1234ebfedea0SLionel Sambuc goto start;
1235ebfedea0SLionel Sambuc }
1236*0a6a1f1dSLionel Sambuc /*
1237*0a6a1f1dSLionel Sambuc * If we are a server and get a client hello when renegotiation isn't
1238*0a6a1f1dSLionel Sambuc * allowed send back a no renegotiation alert and carry on. WARNING:
1239*0a6a1f1dSLionel Sambuc * experimental code, needs reviewing (steve)
1240ebfedea0SLionel Sambuc */
1241ebfedea0SLionel Sambuc if (s->server &&
1242ebfedea0SLionel Sambuc SSL_is_init_finished(s) &&
1243ebfedea0SLionel Sambuc !s->s3->send_connection_binding &&
1244ebfedea0SLionel Sambuc (s->version > SSL3_VERSION) &&
1245ebfedea0SLionel Sambuc (s->s3->handshake_fragment_len >= 4) &&
1246ebfedea0SLionel Sambuc (s->s3->handshake_fragment[0] == SSL3_MT_CLIENT_HELLO) &&
1247ebfedea0SLionel Sambuc (s->session != NULL) && (s->session->cipher != NULL) &&
1248*0a6a1f1dSLionel Sambuc !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1249*0a6a1f1dSLionel Sambuc /*
1250*0a6a1f1dSLionel Sambuc * s->s3->handshake_fragment_len = 0;
1251*0a6a1f1dSLionel Sambuc */
1252ebfedea0SLionel Sambuc rr->length = 0;
1253ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1254ebfedea0SLionel Sambuc goto start;
1255ebfedea0SLionel Sambuc }
1256*0a6a1f1dSLionel Sambuc if (s->s3->alert_fragment_len >= 2) {
1257ebfedea0SLionel Sambuc int alert_level = s->s3->alert_fragment[0];
1258ebfedea0SLionel Sambuc int alert_descr = s->s3->alert_fragment[1];
1259ebfedea0SLionel Sambuc
1260ebfedea0SLionel Sambuc s->s3->alert_fragment_len = 0;
1261ebfedea0SLionel Sambuc
1262ebfedea0SLionel Sambuc if (s->msg_callback)
1263*0a6a1f1dSLionel Sambuc s->msg_callback(0, s->version, SSL3_RT_ALERT,
1264*0a6a1f1dSLionel Sambuc s->s3->alert_fragment, 2, s, s->msg_callback_arg);
1265ebfedea0SLionel Sambuc
1266ebfedea0SLionel Sambuc if (s->info_callback != NULL)
1267ebfedea0SLionel Sambuc cb = s->info_callback;
1268ebfedea0SLionel Sambuc else if (s->ctx->info_callback != NULL)
1269ebfedea0SLionel Sambuc cb = s->ctx->info_callback;
1270ebfedea0SLionel Sambuc
1271*0a6a1f1dSLionel Sambuc if (cb != NULL) {
1272ebfedea0SLionel Sambuc j = (alert_level << 8) | alert_descr;
1273ebfedea0SLionel Sambuc cb(s, SSL_CB_READ_ALERT, j);
1274ebfedea0SLionel Sambuc }
1275ebfedea0SLionel Sambuc
1276*0a6a1f1dSLionel Sambuc if (alert_level == SSL3_AL_WARNING) {
1277ebfedea0SLionel Sambuc s->s3->warn_alert = alert_descr;
1278*0a6a1f1dSLionel Sambuc if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
1279ebfedea0SLionel Sambuc s->shutdown |= SSL_RECEIVED_SHUTDOWN;
1280ebfedea0SLionel Sambuc return (0);
1281ebfedea0SLionel Sambuc }
1282*0a6a1f1dSLionel Sambuc /*
1283*0a6a1f1dSLionel Sambuc * This is a warning but we receive it if we requested
1284*0a6a1f1dSLionel Sambuc * renegotiation and the peer denied it. Terminate with a fatal
1285*0a6a1f1dSLionel Sambuc * alert because if application tried to renegotiatie it
1286*0a6a1f1dSLionel Sambuc * presumably had a good reason and expects it to succeed. In
1287*0a6a1f1dSLionel Sambuc * future we might have a renegotiation where we don't care if
1288*0a6a1f1dSLionel Sambuc * the peer refused it where we carry on.
1289ebfedea0SLionel Sambuc */
1290*0a6a1f1dSLionel Sambuc else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
1291ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1292ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_NO_RENEGOTIATION);
1293ebfedea0SLionel Sambuc goto f_err;
1294ebfedea0SLionel Sambuc }
1295ebfedea0SLionel Sambuc #ifdef SSL_AD_MISSING_SRP_USERNAME
1296ebfedea0SLionel Sambuc else if (alert_descr == SSL_AD_MISSING_SRP_USERNAME)
1297ebfedea0SLionel Sambuc return (0);
1298ebfedea0SLionel Sambuc #endif
1299*0a6a1f1dSLionel Sambuc } else if (alert_level == SSL3_AL_FATAL) {
1300ebfedea0SLionel Sambuc char tmp[16];
1301ebfedea0SLionel Sambuc
1302ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
1303ebfedea0SLionel Sambuc s->s3->fatal_alert = alert_descr;
1304ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
1305ebfedea0SLionel Sambuc BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
1306ebfedea0SLionel Sambuc ERR_add_error_data(2, "SSL alert number ", tmp);
1307ebfedea0SLionel Sambuc s->shutdown |= SSL_RECEIVED_SHUTDOWN;
1308ebfedea0SLionel Sambuc SSL_CTX_remove_session(s->ctx, s->session);
1309ebfedea0SLionel Sambuc return (0);
1310*0a6a1f1dSLionel Sambuc } else {
1311ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1312ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
1313ebfedea0SLionel Sambuc goto f_err;
1314ebfedea0SLionel Sambuc }
1315ebfedea0SLionel Sambuc
1316ebfedea0SLionel Sambuc goto start;
1317ebfedea0SLionel Sambuc }
1318ebfedea0SLionel Sambuc
1319*0a6a1f1dSLionel Sambuc if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
1320*0a6a1f1dSLionel Sambuc * shutdown */
1321ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
1322ebfedea0SLionel Sambuc rr->length = 0;
1323ebfedea0SLionel Sambuc return (0);
1324ebfedea0SLionel Sambuc }
1325ebfedea0SLionel Sambuc
1326*0a6a1f1dSLionel Sambuc if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1327*0a6a1f1dSLionel Sambuc /*
1328*0a6a1f1dSLionel Sambuc * 'Change Cipher Spec' is just a single byte, so we know exactly
1329*0a6a1f1dSLionel Sambuc * what the record payload has to look like
1330*0a6a1f1dSLionel Sambuc */
1331ebfedea0SLionel Sambuc if ((rr->length != 1) || (rr->off != 0) ||
1332*0a6a1f1dSLionel Sambuc (rr->data[0] != SSL3_MT_CCS)) {
1333ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1334ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_CHANGE_CIPHER_SPEC);
1335ebfedea0SLionel Sambuc goto f_err;
1336ebfedea0SLionel Sambuc }
1337ebfedea0SLionel Sambuc
1338ebfedea0SLionel Sambuc /* Check we have a cipher to change to */
1339*0a6a1f1dSLionel Sambuc if (s->s3->tmp.new_cipher == NULL) {
1340ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1341ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
1342ebfedea0SLionel Sambuc goto f_err;
1343ebfedea0SLionel Sambuc }
1344ebfedea0SLionel Sambuc
1345*0a6a1f1dSLionel Sambuc if (!(s->s3->flags & SSL3_FLAGS_CCS_OK)) {
1346*0a6a1f1dSLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1347*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
1348*0a6a1f1dSLionel Sambuc goto f_err;
1349*0a6a1f1dSLionel Sambuc }
1350*0a6a1f1dSLionel Sambuc
1351*0a6a1f1dSLionel Sambuc s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
1352*0a6a1f1dSLionel Sambuc
1353ebfedea0SLionel Sambuc rr->length = 0;
1354ebfedea0SLionel Sambuc
1355ebfedea0SLionel Sambuc if (s->msg_callback)
1356*0a6a1f1dSLionel Sambuc s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
1357*0a6a1f1dSLionel Sambuc rr->data, 1, s, s->msg_callback_arg);
1358ebfedea0SLionel Sambuc
1359ebfedea0SLionel Sambuc s->s3->change_cipher_spec = 1;
1360ebfedea0SLionel Sambuc if (!ssl3_do_change_cipher_spec(s))
1361ebfedea0SLionel Sambuc goto err;
1362ebfedea0SLionel Sambuc else
1363ebfedea0SLionel Sambuc goto start;
1364ebfedea0SLionel Sambuc }
1365ebfedea0SLionel Sambuc
1366*0a6a1f1dSLionel Sambuc /*
1367*0a6a1f1dSLionel Sambuc * Unexpected handshake message (Client Hello, or protocol violation)
1368*0a6a1f1dSLionel Sambuc */
1369*0a6a1f1dSLionel Sambuc if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake) {
1370ebfedea0SLionel Sambuc if (((s->state & SSL_ST_MASK) == SSL_ST_OK) &&
1371*0a6a1f1dSLionel Sambuc !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
1372*0a6a1f1dSLionel Sambuc #if 0 /* worked only because C operator preferences
1373*0a6a1f1dSLionel Sambuc * are not as expected (and because this is
1374*0a6a1f1dSLionel Sambuc * not really needed for clients except for
1375*0a6a1f1dSLionel Sambuc * detecting protocol violations): */
1376ebfedea0SLionel Sambuc s->state = SSL_ST_BEFORE | (s->server)
1377*0a6a1f1dSLionel Sambuc ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1378ebfedea0SLionel Sambuc #else
1379ebfedea0SLionel Sambuc s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1380ebfedea0SLionel Sambuc #endif
1381ebfedea0SLionel Sambuc s->renegotiate = 1;
1382ebfedea0SLionel Sambuc s->new_session = 1;
1383ebfedea0SLionel Sambuc }
1384ebfedea0SLionel Sambuc i = s->handshake_func(s);
1385*0a6a1f1dSLionel Sambuc if (i < 0)
1386*0a6a1f1dSLionel Sambuc return (i);
1387*0a6a1f1dSLionel Sambuc if (i == 0) {
1388ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
1389ebfedea0SLionel Sambuc return (-1);
1390ebfedea0SLionel Sambuc }
1391ebfedea0SLionel Sambuc
1392*0a6a1f1dSLionel Sambuc if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
1393*0a6a1f1dSLionel Sambuc if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
1394ebfedea0SLionel Sambuc BIO *bio;
1395*0a6a1f1dSLionel Sambuc /*
1396*0a6a1f1dSLionel Sambuc * In the case where we try to read application data, but we
1397*0a6a1f1dSLionel Sambuc * trigger an SSL handshake, we return -1 with the retry
1398*0a6a1f1dSLionel Sambuc * option set. Otherwise renegotiation may cause nasty
1399*0a6a1f1dSLionel Sambuc * problems in the blocking world
1400*0a6a1f1dSLionel Sambuc */
1401ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
1402ebfedea0SLionel Sambuc bio = SSL_get_rbio(s);
1403ebfedea0SLionel Sambuc BIO_clear_retry_flags(bio);
1404ebfedea0SLionel Sambuc BIO_set_retry_read(bio);
1405ebfedea0SLionel Sambuc return (-1);
1406ebfedea0SLionel Sambuc }
1407ebfedea0SLionel Sambuc }
1408ebfedea0SLionel Sambuc goto start;
1409ebfedea0SLionel Sambuc }
1410ebfedea0SLionel Sambuc
1411*0a6a1f1dSLionel Sambuc switch (rr->type) {
1412ebfedea0SLionel Sambuc default:
1413ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLS
1414*0a6a1f1dSLionel Sambuc /*
1415*0a6a1f1dSLionel Sambuc * TLS up to v1.1 just ignores unknown message types: TLS v1.2 give
1416*0a6a1f1dSLionel Sambuc * an unexpected message alert.
1417ebfedea0SLionel Sambuc */
1418*0a6a1f1dSLionel Sambuc if (s->version >= TLS1_VERSION && s->version <= TLS1_1_VERSION) {
1419ebfedea0SLionel Sambuc rr->length = 0;
1420ebfedea0SLionel Sambuc goto start;
1421ebfedea0SLionel Sambuc }
1422ebfedea0SLionel Sambuc #endif
1423ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1424ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
1425ebfedea0SLionel Sambuc goto f_err;
1426ebfedea0SLionel Sambuc case SSL3_RT_CHANGE_CIPHER_SPEC:
1427ebfedea0SLionel Sambuc case SSL3_RT_ALERT:
1428ebfedea0SLionel Sambuc case SSL3_RT_HANDSHAKE:
1429*0a6a1f1dSLionel Sambuc /*
1430*0a6a1f1dSLionel Sambuc * we already handled all of these, with the possible exception of
1431*0a6a1f1dSLionel Sambuc * SSL3_RT_HANDSHAKE when s->in_handshake is set, but that should not
1432*0a6a1f1dSLionel Sambuc * happen when type != rr->type
1433*0a6a1f1dSLionel Sambuc */
1434ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1435ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
1436ebfedea0SLionel Sambuc goto f_err;
1437ebfedea0SLionel Sambuc case SSL3_RT_APPLICATION_DATA:
1438*0a6a1f1dSLionel Sambuc /*
1439*0a6a1f1dSLionel Sambuc * At this point, we were expecting handshake data, but have
1440*0a6a1f1dSLionel Sambuc * application data. If the library was running inside ssl3_read()
1441*0a6a1f1dSLionel Sambuc * (i.e. in_read_app_data is set) and it makes sense to read
1442*0a6a1f1dSLionel Sambuc * application data at this point (session renegotiation not yet
1443*0a6a1f1dSLionel Sambuc * started), we will indulge it.
1444ebfedea0SLionel Sambuc */
1445ebfedea0SLionel Sambuc if (s->s3->in_read_app_data &&
1446ebfedea0SLionel Sambuc (s->s3->total_renegotiations != 0) &&
1447*0a6a1f1dSLionel Sambuc (((s->state & SSL_ST_CONNECT) &&
1448ebfedea0SLionel Sambuc (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
1449ebfedea0SLionel Sambuc (s->state <= SSL3_ST_CR_SRVR_HELLO_A)
1450*0a6a1f1dSLionel Sambuc ) || ((s->state & SSL_ST_ACCEPT) &&
1451ebfedea0SLionel Sambuc (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
1452ebfedea0SLionel Sambuc (s->state >= SSL3_ST_SR_CLNT_HELLO_A)
1453ebfedea0SLionel Sambuc )
1454*0a6a1f1dSLionel Sambuc )) {
1455ebfedea0SLionel Sambuc s->s3->in_read_app_data = 2;
1456ebfedea0SLionel Sambuc return (-1);
1457*0a6a1f1dSLionel Sambuc } else {
1458ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1459ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
1460ebfedea0SLionel Sambuc goto f_err;
1461ebfedea0SLionel Sambuc }
1462ebfedea0SLionel Sambuc }
1463ebfedea0SLionel Sambuc /* not reached */
1464ebfedea0SLionel Sambuc
1465ebfedea0SLionel Sambuc f_err:
1466ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
1467ebfedea0SLionel Sambuc err:
1468ebfedea0SLionel Sambuc return (-1);
1469ebfedea0SLionel Sambuc }
1470ebfedea0SLionel Sambuc
ssl3_do_change_cipher_spec(SSL * s)1471ebfedea0SLionel Sambuc int ssl3_do_change_cipher_spec(SSL *s)
1472ebfedea0SLionel Sambuc {
1473ebfedea0SLionel Sambuc int i;
1474ebfedea0SLionel Sambuc const char *sender;
1475ebfedea0SLionel Sambuc int slen;
1476ebfedea0SLionel Sambuc
1477ebfedea0SLionel Sambuc if (s->state & SSL_ST_ACCEPT)
1478ebfedea0SLionel Sambuc i = SSL3_CHANGE_CIPHER_SERVER_READ;
1479ebfedea0SLionel Sambuc else
1480ebfedea0SLionel Sambuc i = SSL3_CHANGE_CIPHER_CLIENT_READ;
1481ebfedea0SLionel Sambuc
1482*0a6a1f1dSLionel Sambuc if (s->s3->tmp.key_block == NULL) {
1483*0a6a1f1dSLionel Sambuc if (s->session == NULL || s->session->master_key_length == 0) {
1484ebfedea0SLionel Sambuc /* might happen if dtls1_read_bytes() calls this */
1485*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC,
1486*0a6a1f1dSLionel Sambuc SSL_R_CCS_RECEIVED_EARLY);
1487ebfedea0SLionel Sambuc return (0);
1488ebfedea0SLionel Sambuc }
1489ebfedea0SLionel Sambuc
1490ebfedea0SLionel Sambuc s->session->cipher = s->s3->tmp.new_cipher;
1491*0a6a1f1dSLionel Sambuc if (!s->method->ssl3_enc->setup_key_block(s))
1492*0a6a1f1dSLionel Sambuc return (0);
1493ebfedea0SLionel Sambuc }
1494ebfedea0SLionel Sambuc
1495ebfedea0SLionel Sambuc if (!s->method->ssl3_enc->change_cipher_state(s, i))
1496ebfedea0SLionel Sambuc return (0);
1497ebfedea0SLionel Sambuc
1498*0a6a1f1dSLionel Sambuc /*
1499*0a6a1f1dSLionel Sambuc * we have to record the message digest at this point so we can get it
1500*0a6a1f1dSLionel Sambuc * before we read the finished message
1501*0a6a1f1dSLionel Sambuc */
1502*0a6a1f1dSLionel Sambuc if (s->state & SSL_ST_CONNECT) {
1503ebfedea0SLionel Sambuc sender = s->method->ssl3_enc->server_finished_label;
1504ebfedea0SLionel Sambuc slen = s->method->ssl3_enc->server_finished_label_len;
1505*0a6a1f1dSLionel Sambuc } else {
1506ebfedea0SLionel Sambuc sender = s->method->ssl3_enc->client_finished_label;
1507ebfedea0SLionel Sambuc slen = s->method->ssl3_enc->client_finished_label_len;
1508ebfedea0SLionel Sambuc }
1509ebfedea0SLionel Sambuc
1510*0a6a1f1dSLionel Sambuc i = s->method->ssl3_enc->final_finish_mac(s,
1511*0a6a1f1dSLionel Sambuc sender, slen,
1512*0a6a1f1dSLionel Sambuc s->s3->tmp.peer_finish_md);
1513*0a6a1f1dSLionel Sambuc if (i == 0) {
1514*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
1515*0a6a1f1dSLionel Sambuc return 0;
1516*0a6a1f1dSLionel Sambuc }
1517*0a6a1f1dSLionel Sambuc s->s3->tmp.peer_finish_md_len = i;
1518ebfedea0SLionel Sambuc
1519ebfedea0SLionel Sambuc return (1);
1520ebfedea0SLionel Sambuc }
1521ebfedea0SLionel Sambuc
ssl3_send_alert(SSL * s,int level,int desc)1522ebfedea0SLionel Sambuc int ssl3_send_alert(SSL *s, int level, int desc)
1523ebfedea0SLionel Sambuc {
1524ebfedea0SLionel Sambuc /* Map tls/ssl alert value to correct one */
1525ebfedea0SLionel Sambuc desc = s->method->ssl3_enc->alert_value(desc);
1526ebfedea0SLionel Sambuc if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
1527*0a6a1f1dSLionel Sambuc desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
1528*0a6a1f1dSLionel Sambuc * protocol_version alerts */
1529*0a6a1f1dSLionel Sambuc if (desc < 0)
1530*0a6a1f1dSLionel Sambuc return -1;
1531ebfedea0SLionel Sambuc /* If a fatal one, remove from cache */
1532ebfedea0SLionel Sambuc if ((level == 2) && (s->session != NULL))
1533ebfedea0SLionel Sambuc SSL_CTX_remove_session(s->ctx, s->session);
1534ebfedea0SLionel Sambuc
1535ebfedea0SLionel Sambuc s->s3->alert_dispatch = 1;
1536ebfedea0SLionel Sambuc s->s3->send_alert[0] = level;
1537ebfedea0SLionel Sambuc s->s3->send_alert[1] = desc;
1538ebfedea0SLionel Sambuc if (s->s3->wbuf.left == 0) /* data still being written out? */
1539ebfedea0SLionel Sambuc return s->method->ssl_dispatch_alert(s);
1540*0a6a1f1dSLionel Sambuc /*
1541*0a6a1f1dSLionel Sambuc * else data is still being written out, we will get written some time in
1542*0a6a1f1dSLionel Sambuc * the future
1543*0a6a1f1dSLionel Sambuc */
1544ebfedea0SLionel Sambuc return -1;
1545ebfedea0SLionel Sambuc }
1546ebfedea0SLionel Sambuc
ssl3_dispatch_alert(SSL * s)1547ebfedea0SLionel Sambuc int ssl3_dispatch_alert(SSL *s)
1548ebfedea0SLionel Sambuc {
1549ebfedea0SLionel Sambuc int i, j;
1550ebfedea0SLionel Sambuc void (*cb) (const SSL *ssl, int type, int val) = NULL;
1551ebfedea0SLionel Sambuc
1552ebfedea0SLionel Sambuc s->s3->alert_dispatch = 0;
1553ebfedea0SLionel Sambuc i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
1554*0a6a1f1dSLionel Sambuc if (i <= 0) {
1555ebfedea0SLionel Sambuc s->s3->alert_dispatch = 1;
1556*0a6a1f1dSLionel Sambuc } else {
1557*0a6a1f1dSLionel Sambuc /*
1558*0a6a1f1dSLionel Sambuc * Alert sent to BIO. If it is important, flush it now. If the
1559*0a6a1f1dSLionel Sambuc * message does not get sent due to non-blocking IO, we will not
1560*0a6a1f1dSLionel Sambuc * worry too much.
1561*0a6a1f1dSLionel Sambuc */
1562ebfedea0SLionel Sambuc if (s->s3->send_alert[0] == SSL3_AL_FATAL)
1563ebfedea0SLionel Sambuc (void)BIO_flush(s->wbio);
1564ebfedea0SLionel Sambuc
1565ebfedea0SLionel Sambuc if (s->msg_callback)
1566*0a6a1f1dSLionel Sambuc s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
1567*0a6a1f1dSLionel Sambuc 2, s, s->msg_callback_arg);
1568ebfedea0SLionel Sambuc
1569ebfedea0SLionel Sambuc if (s->info_callback != NULL)
1570ebfedea0SLionel Sambuc cb = s->info_callback;
1571ebfedea0SLionel Sambuc else if (s->ctx->info_callback != NULL)
1572ebfedea0SLionel Sambuc cb = s->ctx->info_callback;
1573ebfedea0SLionel Sambuc
1574*0a6a1f1dSLionel Sambuc if (cb != NULL) {
1575ebfedea0SLionel Sambuc j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1576ebfedea0SLionel Sambuc cb(s, SSL_CB_WRITE_ALERT, j);
1577ebfedea0SLionel Sambuc }
1578ebfedea0SLionel Sambuc }
1579ebfedea0SLionel Sambuc return (i);
1580ebfedea0SLionel Sambuc }
1581