1ebfedea0SLionel Sambuc /* ssl/d1_pkt.c */
2ebfedea0SLionel Sambuc /*
3ebfedea0SLionel Sambuc * DTLS implementation written by Nagendra Modadugu
4ebfedea0SLionel Sambuc * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5ebfedea0SLionel Sambuc */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc * are met:
12ebfedea0SLionel Sambuc *
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc * distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc * software must display the following acknowledgment:
23ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25ebfedea0SLionel Sambuc *
26ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc * openssl-core@openssl.org.
30ebfedea0SLionel Sambuc *
31ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc *
35ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc * acknowledgment:
37ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39ebfedea0SLionel Sambuc *
40ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc * ====================================================================
53ebfedea0SLionel Sambuc *
54ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
56ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc *
58ebfedea0SLionel Sambuc */
59ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60ebfedea0SLionel Sambuc * All rights reserved.
61ebfedea0SLionel Sambuc *
62ebfedea0SLionel Sambuc * This package is an SSL implementation written
63ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
64ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
65ebfedea0SLionel Sambuc *
66ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
67ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
68ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
69ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
70ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
71ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
72ebfedea0SLionel Sambuc *
73ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
74ebfedea0SLionel Sambuc * the code are not to be removed.
75ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
76ebfedea0SLionel Sambuc * as the author of the parts of the library used.
77ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
78ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
79ebfedea0SLionel Sambuc *
80ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
81ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
82ebfedea0SLionel Sambuc * are met:
83ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
84ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
85ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
86ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
87ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
88ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
89ebfedea0SLionel Sambuc * must display the following acknowledgement:
90ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
91ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
92ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
93ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
94ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
95ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
96ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
97ebfedea0SLionel Sambuc *
98ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108ebfedea0SLionel Sambuc * SUCH DAMAGE.
109ebfedea0SLionel Sambuc *
110ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
111ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
112ebfedea0SLionel Sambuc * copied and put under another distribution licence
113ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
114ebfedea0SLionel Sambuc */
115ebfedea0SLionel Sambuc
116ebfedea0SLionel Sambuc #include <stdio.h>
117ebfedea0SLionel Sambuc #include <errno.h>
118ebfedea0SLionel Sambuc #define USE_SOCKETS
119ebfedea0SLionel Sambuc #include "ssl_locl.h"
120ebfedea0SLionel Sambuc #include <openssl/evp.h>
121ebfedea0SLionel Sambuc #include <openssl/buffer.h>
122ebfedea0SLionel Sambuc #include <openssl/pqueue.h>
123ebfedea0SLionel Sambuc #include <openssl/rand.h>
124ebfedea0SLionel Sambuc
125ebfedea0SLionel Sambuc /* mod 128 saturating subtract of two 64-bit values in big-endian order */
satsub64be(const unsigned char * v1,const unsigned char * v2)126ebfedea0SLionel Sambuc static int satsub64be(const unsigned char *v1, const unsigned char *v2)
127*0a6a1f1dSLionel Sambuc {
128*0a6a1f1dSLionel Sambuc int ret, sat, brw, i;
129ebfedea0SLionel Sambuc
130*0a6a1f1dSLionel Sambuc if (sizeof(long) == 8)
131*0a6a1f1dSLionel Sambuc do {
132*0a6a1f1dSLionel Sambuc const union {
133*0a6a1f1dSLionel Sambuc long one;
134*0a6a1f1dSLionel Sambuc char little;
135*0a6a1f1dSLionel Sambuc } is_endian = {
136*0a6a1f1dSLionel Sambuc 1
137*0a6a1f1dSLionel Sambuc };
138ebfedea0SLionel Sambuc long l;
139ebfedea0SLionel Sambuc
140*0a6a1f1dSLionel Sambuc if (is_endian.little)
141*0a6a1f1dSLionel Sambuc break;
142ebfedea0SLionel Sambuc /* not reached on little-endians */
143*0a6a1f1dSLionel Sambuc /*
144*0a6a1f1dSLionel Sambuc * following test is redundant, because input is always aligned,
145*0a6a1f1dSLionel Sambuc * but I take no chances...
146*0a6a1f1dSLionel Sambuc */
147*0a6a1f1dSLionel Sambuc if (((size_t)v1 | (size_t)v2) & 0x7)
148*0a6a1f1dSLionel Sambuc break;
149ebfedea0SLionel Sambuc
150ebfedea0SLionel Sambuc l = *((long *)v1);
151ebfedea0SLionel Sambuc l -= *((long *)v2);
152*0a6a1f1dSLionel Sambuc if (l > 128)
153*0a6a1f1dSLionel Sambuc return 128;
154*0a6a1f1dSLionel Sambuc else if (l < -128)
155*0a6a1f1dSLionel Sambuc return -128;
156*0a6a1f1dSLionel Sambuc else
157*0a6a1f1dSLionel Sambuc return (int)l;
158ebfedea0SLionel Sambuc } while (0);
159ebfedea0SLionel Sambuc
160ebfedea0SLionel Sambuc ret = (int)v1[7] - (int)v2[7];
161ebfedea0SLionel Sambuc sat = 0;
162ebfedea0SLionel Sambuc brw = ret >> 8; /* brw is either 0 or -1 */
163*0a6a1f1dSLionel Sambuc if (ret & 0x80) {
164*0a6a1f1dSLionel Sambuc for (i = 6; i >= 0; i--) {
165*0a6a1f1dSLionel Sambuc brw += (int)v1[i] - (int)v2[i];
166ebfedea0SLionel Sambuc sat |= ~brw;
167ebfedea0SLionel Sambuc brw >>= 8;
168ebfedea0SLionel Sambuc }
169*0a6a1f1dSLionel Sambuc } else {
170*0a6a1f1dSLionel Sambuc for (i = 6; i >= 0; i--) {
171*0a6a1f1dSLionel Sambuc brw += (int)v1[i] - (int)v2[i];
172ebfedea0SLionel Sambuc sat |= brw;
173ebfedea0SLionel Sambuc brw >>= 8;
174ebfedea0SLionel Sambuc }
175ebfedea0SLionel Sambuc }
176ebfedea0SLionel Sambuc brw <<= 8; /* brw is either 0 or -256 */
177ebfedea0SLionel Sambuc
178*0a6a1f1dSLionel Sambuc if (sat & 0xff)
179*0a6a1f1dSLionel Sambuc return brw | 0x80;
180*0a6a1f1dSLionel Sambuc else
181*0a6a1f1dSLionel Sambuc return brw + (ret & 0xFF);
182ebfedea0SLionel Sambuc }
183ebfedea0SLionel Sambuc
184ebfedea0SLionel Sambuc static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
185ebfedea0SLionel Sambuc int len, int peek);
186ebfedea0SLionel Sambuc static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap);
187ebfedea0SLionel Sambuc static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap);
188ebfedea0SLionel Sambuc static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
189ebfedea0SLionel Sambuc unsigned int *is_next_epoch);
190ebfedea0SLionel Sambuc #if 0
191ebfedea0SLionel Sambuc static int dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr,
192*0a6a1f1dSLionel Sambuc unsigned short *priority,
193*0a6a1f1dSLionel Sambuc unsigned long *offset);
194ebfedea0SLionel Sambuc #endif
195ebfedea0SLionel Sambuc static int dtls1_buffer_record(SSL *s, record_pqueue *q,
196ebfedea0SLionel Sambuc unsigned char *priority);
197ebfedea0SLionel Sambuc static int dtls1_process_record(SSL *s);
198ebfedea0SLionel Sambuc
199ebfedea0SLionel Sambuc /* copy buffered record into SSL structure */
dtls1_copy_record(SSL * s,pitem * item)200*0a6a1f1dSLionel Sambuc static int dtls1_copy_record(SSL *s, pitem *item)
201ebfedea0SLionel Sambuc {
202ebfedea0SLionel Sambuc DTLS1_RECORD_DATA *rdata;
203ebfedea0SLionel Sambuc
204ebfedea0SLionel Sambuc rdata = (DTLS1_RECORD_DATA *)item->data;
205ebfedea0SLionel Sambuc
206ebfedea0SLionel Sambuc if (s->s3->rbuf.buf != NULL)
207ebfedea0SLionel Sambuc OPENSSL_free(s->s3->rbuf.buf);
208ebfedea0SLionel Sambuc
209ebfedea0SLionel Sambuc s->packet = rdata->packet;
210ebfedea0SLionel Sambuc s->packet_length = rdata->packet_length;
211ebfedea0SLionel Sambuc memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER));
212ebfedea0SLionel Sambuc memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD));
213ebfedea0SLionel Sambuc
214ebfedea0SLionel Sambuc /* Set proper sequence number for mac calculation */
215ebfedea0SLionel Sambuc memcpy(&(s->s3->read_sequence[2]), &(rdata->packet[5]), 6);
216ebfedea0SLionel Sambuc
217ebfedea0SLionel Sambuc return (1);
218ebfedea0SLionel Sambuc }
219ebfedea0SLionel Sambuc
220ebfedea0SLionel Sambuc static int
dtls1_buffer_record(SSL * s,record_pqueue * queue,unsigned char * priority)221ebfedea0SLionel Sambuc dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
222ebfedea0SLionel Sambuc {
223ebfedea0SLionel Sambuc DTLS1_RECORD_DATA *rdata;
224ebfedea0SLionel Sambuc pitem *item;
225ebfedea0SLionel Sambuc
226ebfedea0SLionel Sambuc /* Limit the size of the queue to prevent DOS attacks */
227ebfedea0SLionel Sambuc if (pqueue_size(queue->q) >= 100)
228ebfedea0SLionel Sambuc return 0;
229ebfedea0SLionel Sambuc
230ebfedea0SLionel Sambuc rdata = OPENSSL_malloc(sizeof(DTLS1_RECORD_DATA));
231ebfedea0SLionel Sambuc item = pitem_new(priority, rdata);
232*0a6a1f1dSLionel Sambuc if (rdata == NULL || item == NULL) {
233*0a6a1f1dSLionel Sambuc if (rdata != NULL)
234*0a6a1f1dSLionel Sambuc OPENSSL_free(rdata);
235*0a6a1f1dSLionel Sambuc if (item != NULL)
236*0a6a1f1dSLionel Sambuc pitem_free(item);
237ebfedea0SLionel Sambuc
238ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
239*0a6a1f1dSLionel Sambuc return -1;
240ebfedea0SLionel Sambuc }
241ebfedea0SLionel Sambuc
242ebfedea0SLionel Sambuc rdata->packet = s->packet;
243ebfedea0SLionel Sambuc rdata->packet_length = s->packet_length;
244ebfedea0SLionel Sambuc memcpy(&(rdata->rbuf), &(s->s3->rbuf), sizeof(SSL3_BUFFER));
245ebfedea0SLionel Sambuc memcpy(&(rdata->rrec), &(s->s3->rrec), sizeof(SSL3_RECORD));
246ebfedea0SLionel Sambuc
247ebfedea0SLionel Sambuc item->data = rdata;
248ebfedea0SLionel Sambuc
249ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
250ebfedea0SLionel Sambuc /* Store bio_dgram_sctp_rcvinfo struct */
251ebfedea0SLionel Sambuc if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
252*0a6a1f1dSLionel Sambuc (s->state == SSL3_ST_SR_FINISHED_A
253*0a6a1f1dSLionel Sambuc || s->state == SSL3_ST_CR_FINISHED_A)) {
254*0a6a1f1dSLionel Sambuc BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
255*0a6a1f1dSLionel Sambuc sizeof(rdata->recordinfo), &rdata->recordinfo);
256ebfedea0SLionel Sambuc }
257ebfedea0SLionel Sambuc #endif
258ebfedea0SLionel Sambuc
259ebfedea0SLionel Sambuc s->packet = NULL;
260ebfedea0SLionel Sambuc s->packet_length = 0;
261ebfedea0SLionel Sambuc memset(&(s->s3->rbuf), 0, sizeof(SSL3_BUFFER));
262ebfedea0SLionel Sambuc memset(&(s->s3->rrec), 0, sizeof(SSL3_RECORD));
263ebfedea0SLionel Sambuc
264*0a6a1f1dSLionel Sambuc if (!ssl3_setup_buffers(s)) {
265ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
266*0a6a1f1dSLionel Sambuc if (rdata->rbuf.buf != NULL)
267*0a6a1f1dSLionel Sambuc OPENSSL_free(rdata->rbuf.buf);
268ebfedea0SLionel Sambuc OPENSSL_free(rdata);
269ebfedea0SLionel Sambuc pitem_free(item);
270*0a6a1f1dSLionel Sambuc return (-1);
271*0a6a1f1dSLionel Sambuc }
272*0a6a1f1dSLionel Sambuc
273*0a6a1f1dSLionel Sambuc /* insert should not fail, since duplicates are dropped */
274*0a6a1f1dSLionel Sambuc if (pqueue_insert(queue->q, item) == NULL) {
275*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
276*0a6a1f1dSLionel Sambuc if (rdata->rbuf.buf != NULL)
277*0a6a1f1dSLionel Sambuc OPENSSL_free(rdata->rbuf.buf);
278*0a6a1f1dSLionel Sambuc OPENSSL_free(rdata);
279*0a6a1f1dSLionel Sambuc pitem_free(item);
280*0a6a1f1dSLionel Sambuc return (-1);
281ebfedea0SLionel Sambuc }
282ebfedea0SLionel Sambuc
283ebfedea0SLionel Sambuc return (1);
284ebfedea0SLionel Sambuc }
285ebfedea0SLionel Sambuc
dtls1_retrieve_buffered_record(SSL * s,record_pqueue * queue)286*0a6a1f1dSLionel Sambuc static int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
287ebfedea0SLionel Sambuc {
288ebfedea0SLionel Sambuc pitem *item;
289ebfedea0SLionel Sambuc
290ebfedea0SLionel Sambuc item = pqueue_pop(queue->q);
291*0a6a1f1dSLionel Sambuc if (item) {
292ebfedea0SLionel Sambuc dtls1_copy_record(s, item);
293ebfedea0SLionel Sambuc
294ebfedea0SLionel Sambuc OPENSSL_free(item->data);
295ebfedea0SLionel Sambuc pitem_free(item);
296ebfedea0SLionel Sambuc
297ebfedea0SLionel Sambuc return (1);
298ebfedea0SLionel Sambuc }
299ebfedea0SLionel Sambuc
300ebfedea0SLionel Sambuc return (0);
301ebfedea0SLionel Sambuc }
302ebfedea0SLionel Sambuc
303*0a6a1f1dSLionel Sambuc /*
304*0a6a1f1dSLionel Sambuc * retrieve a buffered record that belongs to the new epoch, i.e., not
305*0a6a1f1dSLionel Sambuc * processed yet
306*0a6a1f1dSLionel Sambuc */
307ebfedea0SLionel Sambuc #define dtls1_get_unprocessed_record(s) \
308ebfedea0SLionel Sambuc dtls1_retrieve_buffered_record((s), \
309ebfedea0SLionel Sambuc &((s)->d1->unprocessed_rcds))
310ebfedea0SLionel Sambuc
311*0a6a1f1dSLionel Sambuc /*
312*0a6a1f1dSLionel Sambuc * retrieve a buffered record that belongs to the current epoch, ie,
313*0a6a1f1dSLionel Sambuc * processed
314*0a6a1f1dSLionel Sambuc */
315ebfedea0SLionel Sambuc #define dtls1_get_processed_record(s) \
316ebfedea0SLionel Sambuc dtls1_retrieve_buffered_record((s), \
317ebfedea0SLionel Sambuc &((s)->d1->processed_rcds))
318ebfedea0SLionel Sambuc
dtls1_process_buffered_records(SSL * s)319*0a6a1f1dSLionel Sambuc static int dtls1_process_buffered_records(SSL *s)
320ebfedea0SLionel Sambuc {
321ebfedea0SLionel Sambuc pitem *item;
322ebfedea0SLionel Sambuc
323ebfedea0SLionel Sambuc item = pqueue_peek(s->d1->unprocessed_rcds.q);
324*0a6a1f1dSLionel Sambuc if (item) {
325ebfedea0SLionel Sambuc /* Check if epoch is current. */
326ebfedea0SLionel Sambuc if (s->d1->unprocessed_rcds.epoch != s->d1->r_epoch)
327ebfedea0SLionel Sambuc return (1); /* Nothing to do. */
328ebfedea0SLionel Sambuc
329ebfedea0SLionel Sambuc /* Process all the records. */
330*0a6a1f1dSLionel Sambuc while (pqueue_peek(s->d1->unprocessed_rcds.q)) {
331ebfedea0SLionel Sambuc dtls1_get_unprocessed_record(s);
332ebfedea0SLionel Sambuc if (!dtls1_process_record(s))
333ebfedea0SLionel Sambuc return (0);
334*0a6a1f1dSLionel Sambuc if (dtls1_buffer_record(s, &(s->d1->processed_rcds),
335*0a6a1f1dSLionel Sambuc s->s3->rrec.seq_num) < 0)
336*0a6a1f1dSLionel Sambuc return -1;
337ebfedea0SLionel Sambuc }
338ebfedea0SLionel Sambuc }
339ebfedea0SLionel Sambuc
340*0a6a1f1dSLionel Sambuc /*
341*0a6a1f1dSLionel Sambuc * sync epoch numbers once all the unprocessed records have been
342*0a6a1f1dSLionel Sambuc * processed
343*0a6a1f1dSLionel Sambuc */
344ebfedea0SLionel Sambuc s->d1->processed_rcds.epoch = s->d1->r_epoch;
345ebfedea0SLionel Sambuc s->d1->unprocessed_rcds.epoch = s->d1->r_epoch + 1;
346ebfedea0SLionel Sambuc
347ebfedea0SLionel Sambuc return (1);
348ebfedea0SLionel Sambuc }
349ebfedea0SLionel Sambuc
350ebfedea0SLionel Sambuc #if 0
351ebfedea0SLionel Sambuc
352*0a6a1f1dSLionel Sambuc static int dtls1_get_buffered_record(SSL *s)
353ebfedea0SLionel Sambuc {
354ebfedea0SLionel Sambuc pitem *item;
355ebfedea0SLionel Sambuc PQ_64BIT priority =
356ebfedea0SLionel Sambuc (((PQ_64BIT) s->d1->handshake_read_seq) << 32) |
357ebfedea0SLionel Sambuc ((PQ_64BIT) s->d1->r_msg_hdr.frag_off);
358ebfedea0SLionel Sambuc
359*0a6a1f1dSLionel Sambuc /* if we're not (re)negotiating, nothing buffered */
360*0a6a1f1dSLionel Sambuc if (!SSL_in_init(s))
361ebfedea0SLionel Sambuc return 0;
362ebfedea0SLionel Sambuc
363ebfedea0SLionel Sambuc item = pqueue_peek(s->d1->rcvd_records);
364*0a6a1f1dSLionel Sambuc if (item && item->priority == priority) {
365*0a6a1f1dSLionel Sambuc /*
366*0a6a1f1dSLionel Sambuc * Check if we've received the record of interest. It must be a
367*0a6a1f1dSLionel Sambuc * handshake record, since data records as passed up without
368*0a6a1f1dSLionel Sambuc * buffering
369*0a6a1f1dSLionel Sambuc */
370ebfedea0SLionel Sambuc DTLS1_RECORD_DATA *rdata;
371ebfedea0SLionel Sambuc item = pqueue_pop(s->d1->rcvd_records);
372ebfedea0SLionel Sambuc rdata = (DTLS1_RECORD_DATA *)item->data;
373ebfedea0SLionel Sambuc
374ebfedea0SLionel Sambuc if (s->s3->rbuf.buf != NULL)
375ebfedea0SLionel Sambuc OPENSSL_free(s->s3->rbuf.buf);
376ebfedea0SLionel Sambuc
377ebfedea0SLionel Sambuc s->packet = rdata->packet;
378ebfedea0SLionel Sambuc s->packet_length = rdata->packet_length;
379ebfedea0SLionel Sambuc memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER));
380ebfedea0SLionel Sambuc memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD));
381ebfedea0SLionel Sambuc
382ebfedea0SLionel Sambuc OPENSSL_free(item->data);
383ebfedea0SLionel Sambuc pitem_free(item);
384ebfedea0SLionel Sambuc
385ebfedea0SLionel Sambuc /* s->d1->next_expected_seq_num++; */
386ebfedea0SLionel Sambuc return (1);
387ebfedea0SLionel Sambuc }
388ebfedea0SLionel Sambuc
389ebfedea0SLionel Sambuc return 0;
390ebfedea0SLionel Sambuc }
391ebfedea0SLionel Sambuc
392ebfedea0SLionel Sambuc #endif
393ebfedea0SLionel Sambuc
dtls1_process_record(SSL * s)394*0a6a1f1dSLionel Sambuc static int dtls1_process_record(SSL *s)
395ebfedea0SLionel Sambuc {
396ebfedea0SLionel Sambuc int i, al;
397ebfedea0SLionel Sambuc int enc_err;
398ebfedea0SLionel Sambuc SSL_SESSION *sess;
399ebfedea0SLionel Sambuc SSL3_RECORD *rr;
400ebfedea0SLionel Sambuc unsigned int mac_size, orig_len;
401ebfedea0SLionel Sambuc unsigned char md[EVP_MAX_MD_SIZE];
402ebfedea0SLionel Sambuc
403ebfedea0SLionel Sambuc rr = &(s->s3->rrec);
404ebfedea0SLionel Sambuc sess = s->session;
405ebfedea0SLionel Sambuc
406*0a6a1f1dSLionel Sambuc /*
407*0a6a1f1dSLionel Sambuc * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
408ebfedea0SLionel Sambuc * and we have that many bytes in s->packet
409ebfedea0SLionel Sambuc */
410ebfedea0SLionel Sambuc rr->input = &(s->packet[DTLS1_RT_HEADER_LENGTH]);
411ebfedea0SLionel Sambuc
412*0a6a1f1dSLionel Sambuc /*
413*0a6a1f1dSLionel Sambuc * ok, we can now read from 's->packet' data into 'rr' rr->input points
414*0a6a1f1dSLionel Sambuc * at rr->length bytes, which need to be copied into rr->data by either
415*0a6a1f1dSLionel Sambuc * the decryption or by the decompression When the data is 'copied' into
416*0a6a1f1dSLionel Sambuc * the rr->data buffer, rr->input will be pointed at the new buffer
417*0a6a1f1dSLionel Sambuc */
418ebfedea0SLionel Sambuc
419*0a6a1f1dSLionel Sambuc /*
420*0a6a1f1dSLionel Sambuc * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
421*0a6a1f1dSLionel Sambuc * bytes of encrypted compressed stuff.
422*0a6a1f1dSLionel Sambuc */
423ebfedea0SLionel Sambuc
424ebfedea0SLionel Sambuc /* check is not needed I believe */
425*0a6a1f1dSLionel Sambuc if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
426ebfedea0SLionel Sambuc al = SSL_AD_RECORD_OVERFLOW;
427ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
428ebfedea0SLionel Sambuc goto f_err;
429ebfedea0SLionel Sambuc }
430ebfedea0SLionel Sambuc
431ebfedea0SLionel Sambuc /* decrypt in place in 'rr->input' */
432ebfedea0SLionel Sambuc rr->data = rr->input;
433ebfedea0SLionel Sambuc
434ebfedea0SLionel Sambuc enc_err = s->method->ssl3_enc->enc(s, 0);
435*0a6a1f1dSLionel Sambuc /*-
436*0a6a1f1dSLionel Sambuc * enc_err is:
437ebfedea0SLionel Sambuc * 0: (in non-constant time) if the record is publically invalid.
438ebfedea0SLionel Sambuc * 1: if the padding is valid
439*0a6a1f1dSLionel Sambuc * -1: if the padding is invalid
440*0a6a1f1dSLionel Sambuc */
441*0a6a1f1dSLionel Sambuc if (enc_err == 0) {
442ebfedea0SLionel Sambuc /* For DTLS we simply ignore bad packets. */
443ebfedea0SLionel Sambuc rr->length = 0;
444ebfedea0SLionel Sambuc s->packet_length = 0;
445ebfedea0SLionel Sambuc goto err;
446ebfedea0SLionel Sambuc }
447ebfedea0SLionel Sambuc #ifdef TLS_DEBUG
448ebfedea0SLionel Sambuc printf("dec %d\n", rr->length);
449*0a6a1f1dSLionel Sambuc {
450*0a6a1f1dSLionel Sambuc unsigned int z;
451*0a6a1f1dSLionel Sambuc for (z = 0; z < rr->length; z++)
452*0a6a1f1dSLionel Sambuc printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
453*0a6a1f1dSLionel Sambuc }
454ebfedea0SLionel Sambuc printf("\n");
455ebfedea0SLionel Sambuc #endif
456ebfedea0SLionel Sambuc
457ebfedea0SLionel Sambuc /* r->length is now the compressed data plus mac */
458ebfedea0SLionel Sambuc if ((sess != NULL) &&
459*0a6a1f1dSLionel Sambuc (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
460ebfedea0SLionel Sambuc /* s->read_hash != NULL => mac_size != -1 */
461ebfedea0SLionel Sambuc unsigned char *mac = NULL;
462ebfedea0SLionel Sambuc unsigned char mac_tmp[EVP_MAX_MD_SIZE];
463ebfedea0SLionel Sambuc mac_size = EVP_MD_CTX_size(s->read_hash);
464ebfedea0SLionel Sambuc OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
465ebfedea0SLionel Sambuc
466*0a6a1f1dSLionel Sambuc /*
467*0a6a1f1dSLionel Sambuc * kludge: *_cbc_remove_padding passes padding length in rr->type
468*0a6a1f1dSLionel Sambuc */
469ebfedea0SLionel Sambuc orig_len = rr->length + ((unsigned int)rr->type >> 8);
470ebfedea0SLionel Sambuc
471*0a6a1f1dSLionel Sambuc /*
472*0a6a1f1dSLionel Sambuc * orig_len is the length of the record before any padding was
473ebfedea0SLionel Sambuc * removed. This is public information, as is the MAC in use,
474*0a6a1f1dSLionel Sambuc * therefore we can safely process the record in a different amount
475*0a6a1f1dSLionel Sambuc * of time if it's too short to possibly contain a MAC.
476ebfedea0SLionel Sambuc */
477ebfedea0SLionel Sambuc if (orig_len < mac_size ||
478ebfedea0SLionel Sambuc /* CBC records must have a padding length byte too. */
479ebfedea0SLionel Sambuc (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
480*0a6a1f1dSLionel Sambuc orig_len < mac_size + 1)) {
481ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
482ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
483ebfedea0SLionel Sambuc goto f_err;
484ebfedea0SLionel Sambuc }
485ebfedea0SLionel Sambuc
486*0a6a1f1dSLionel Sambuc if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
487*0a6a1f1dSLionel Sambuc /*
488*0a6a1f1dSLionel Sambuc * We update the length so that the TLS header bytes can be
489*0a6a1f1dSLionel Sambuc * constructed correctly but we need to extract the MAC in
490*0a6a1f1dSLionel Sambuc * constant time from within the record, without leaking the
491*0a6a1f1dSLionel Sambuc * contents of the padding bytes.
492*0a6a1f1dSLionel Sambuc */
493ebfedea0SLionel Sambuc mac = mac_tmp;
494ebfedea0SLionel Sambuc ssl3_cbc_copy_mac(mac_tmp, rr, mac_size, orig_len);
495ebfedea0SLionel Sambuc rr->length -= mac_size;
496*0a6a1f1dSLionel Sambuc } else {
497*0a6a1f1dSLionel Sambuc /*
498*0a6a1f1dSLionel Sambuc * In this case there's no padding, so |orig_len| equals
499*0a6a1f1dSLionel Sambuc * |rec->length| and we checked that there's enough bytes for
500*0a6a1f1dSLionel Sambuc * |mac_size| above.
501*0a6a1f1dSLionel Sambuc */
502ebfedea0SLionel Sambuc rr->length -= mac_size;
503ebfedea0SLionel Sambuc mac = &rr->data[rr->length];
504ebfedea0SLionel Sambuc }
505ebfedea0SLionel Sambuc
506ebfedea0SLionel Sambuc i = s->method->ssl3_enc->mac(s, md, 0 /* not send */ );
507*0a6a1f1dSLionel Sambuc if (i < 0 || mac == NULL
508*0a6a1f1dSLionel Sambuc || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
509ebfedea0SLionel Sambuc enc_err = -1;
510ebfedea0SLionel Sambuc if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
511ebfedea0SLionel Sambuc enc_err = -1;
512ebfedea0SLionel Sambuc }
513ebfedea0SLionel Sambuc
514*0a6a1f1dSLionel Sambuc if (enc_err < 0) {
515ebfedea0SLionel Sambuc /* decryption failed, silently discard message */
516ebfedea0SLionel Sambuc rr->length = 0;
517ebfedea0SLionel Sambuc s->packet_length = 0;
518ebfedea0SLionel Sambuc goto err;
519ebfedea0SLionel Sambuc }
520ebfedea0SLionel Sambuc
521ebfedea0SLionel Sambuc /* r->length is now just compressed */
522*0a6a1f1dSLionel Sambuc if (s->expand != NULL) {
523*0a6a1f1dSLionel Sambuc if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
524ebfedea0SLionel Sambuc al = SSL_AD_RECORD_OVERFLOW;
525*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
526*0a6a1f1dSLionel Sambuc 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_DTLS1_PROCESS_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) {
537ebfedea0SLionel Sambuc al = SSL_AD_RECORD_OVERFLOW;
538ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_PROCESS_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 return (1);
555ebfedea0SLionel Sambuc
556ebfedea0SLionel Sambuc f_err:
557ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
558ebfedea0SLionel Sambuc err:
559ebfedea0SLionel Sambuc return (0);
560ebfedea0SLionel Sambuc }
561ebfedea0SLionel Sambuc
562*0a6a1f1dSLionel Sambuc /*-
563*0a6a1f1dSLionel Sambuc * Call this to get a new input record.
564ebfedea0SLionel Sambuc * It will return <= 0 if more data is needed, normally due to an error
565ebfedea0SLionel Sambuc * or non-blocking IO.
566ebfedea0SLionel Sambuc * When it finishes, one packet has been decoded and can be found in
567ebfedea0SLionel Sambuc * ssl->s3->rrec.type - is the type of record
568ebfedea0SLionel Sambuc * ssl->s3->rrec.data, - data
569ebfedea0SLionel Sambuc * ssl->s3->rrec.length, - number of bytes
570ebfedea0SLionel Sambuc */
571ebfedea0SLionel Sambuc /* used only by dtls1_read_bytes */
dtls1_get_record(SSL * s)572ebfedea0SLionel Sambuc int dtls1_get_record(SSL *s)
573ebfedea0SLionel Sambuc {
574ebfedea0SLionel Sambuc int ssl_major, ssl_minor;
575ebfedea0SLionel Sambuc int i, n;
576ebfedea0SLionel Sambuc SSL3_RECORD *rr;
577ebfedea0SLionel Sambuc unsigned char *p = NULL;
578ebfedea0SLionel Sambuc unsigned short version;
579ebfedea0SLionel Sambuc DTLS1_BITMAP *bitmap;
580ebfedea0SLionel Sambuc unsigned int is_next_epoch;
581ebfedea0SLionel Sambuc
582ebfedea0SLionel Sambuc rr = &(s->s3->rrec);
583ebfedea0SLionel Sambuc
584*0a6a1f1dSLionel Sambuc /*
585*0a6a1f1dSLionel Sambuc * The epoch may have changed. If so, process all the pending records.
586*0a6a1f1dSLionel Sambuc * This is a non-blocking operation.
587*0a6a1f1dSLionel Sambuc */
588*0a6a1f1dSLionel Sambuc if (dtls1_process_buffered_records(s) < 0)
589*0a6a1f1dSLionel Sambuc return -1;
590ebfedea0SLionel Sambuc
591ebfedea0SLionel Sambuc /* if we're renegotiating, then there may be buffered records */
592ebfedea0SLionel Sambuc if (dtls1_get_processed_record(s))
593ebfedea0SLionel Sambuc return 1;
594ebfedea0SLionel Sambuc
595ebfedea0SLionel Sambuc /* get something from the wire */
596ebfedea0SLionel Sambuc again:
597ebfedea0SLionel Sambuc /* check if we have the header */
598ebfedea0SLionel Sambuc if ((s->rstate != SSL_ST_READ_BODY) ||
599*0a6a1f1dSLionel Sambuc (s->packet_length < DTLS1_RT_HEADER_LENGTH)) {
600ebfedea0SLionel Sambuc n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
601ebfedea0SLionel Sambuc /* read timeout is handled by dtls1_read_bytes */
602*0a6a1f1dSLionel Sambuc if (n <= 0)
603*0a6a1f1dSLionel Sambuc return (n); /* error or non-blocking */
604ebfedea0SLionel Sambuc
605ebfedea0SLionel Sambuc /* this packet contained a partial record, dump it */
606*0a6a1f1dSLionel Sambuc if (s->packet_length != DTLS1_RT_HEADER_LENGTH) {
607ebfedea0SLionel Sambuc s->packet_length = 0;
608ebfedea0SLionel Sambuc goto again;
609ebfedea0SLionel Sambuc }
610ebfedea0SLionel Sambuc
611ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_BODY;
612ebfedea0SLionel Sambuc
613ebfedea0SLionel Sambuc p = s->packet;
614ebfedea0SLionel Sambuc
615ebfedea0SLionel Sambuc /* Pull apart the header into the DTLS1_RECORD */
616ebfedea0SLionel Sambuc rr->type = *(p++);
617ebfedea0SLionel Sambuc ssl_major = *(p++);
618ebfedea0SLionel Sambuc ssl_minor = *(p++);
619ebfedea0SLionel Sambuc version = (ssl_major << 8) | ssl_minor;
620ebfedea0SLionel Sambuc
621ebfedea0SLionel Sambuc /* sequence number is 64 bits, with top 2 bytes = epoch */
622ebfedea0SLionel Sambuc n2s(p, rr->epoch);
623ebfedea0SLionel Sambuc
624ebfedea0SLionel Sambuc memcpy(&(s->s3->read_sequence[2]), p, 6);
625ebfedea0SLionel Sambuc p += 6;
626ebfedea0SLionel Sambuc
627ebfedea0SLionel Sambuc n2s(p, rr->length);
628ebfedea0SLionel Sambuc
629ebfedea0SLionel Sambuc /* Lets check version */
630*0a6a1f1dSLionel Sambuc if (!s->first_packet) {
631*0a6a1f1dSLionel Sambuc if (version != s->version) {
632ebfedea0SLionel Sambuc /* unexpected version, silently discard */
633ebfedea0SLionel Sambuc rr->length = 0;
634ebfedea0SLionel Sambuc s->packet_length = 0;
635ebfedea0SLionel Sambuc goto again;
636ebfedea0SLionel Sambuc }
637ebfedea0SLionel Sambuc }
638ebfedea0SLionel Sambuc
639*0a6a1f1dSLionel Sambuc if ((version & 0xff00) != (s->version & 0xff00)) {
640ebfedea0SLionel Sambuc /* wrong version, silently discard record */
641ebfedea0SLionel Sambuc rr->length = 0;
642ebfedea0SLionel Sambuc s->packet_length = 0;
643ebfedea0SLionel Sambuc goto again;
644ebfedea0SLionel Sambuc }
645ebfedea0SLionel Sambuc
646*0a6a1f1dSLionel Sambuc if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
647ebfedea0SLionel Sambuc /* record too long, silently discard it */
648ebfedea0SLionel Sambuc rr->length = 0;
649ebfedea0SLionel Sambuc s->packet_length = 0;
650ebfedea0SLionel Sambuc goto again;
651ebfedea0SLionel Sambuc }
652ebfedea0SLionel Sambuc
653ebfedea0SLionel Sambuc /* now s->rstate == SSL_ST_READ_BODY */
654ebfedea0SLionel Sambuc }
655ebfedea0SLionel Sambuc
656ebfedea0SLionel Sambuc /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
657ebfedea0SLionel Sambuc
658*0a6a1f1dSLionel Sambuc if (rr->length > s->packet_length - DTLS1_RT_HEADER_LENGTH) {
659ebfedea0SLionel Sambuc /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
660ebfedea0SLionel Sambuc i = rr->length;
661ebfedea0SLionel Sambuc n = ssl3_read_n(s, i, i, 1);
662ebfedea0SLionel Sambuc /* this packet contained a partial record, dump it */
663*0a6a1f1dSLionel Sambuc if (n != i) {
664ebfedea0SLionel Sambuc rr->length = 0;
665ebfedea0SLionel Sambuc s->packet_length = 0;
666ebfedea0SLionel Sambuc goto again;
667ebfedea0SLionel Sambuc }
668ebfedea0SLionel Sambuc
669*0a6a1f1dSLionel Sambuc /*
670*0a6a1f1dSLionel Sambuc * now n == rr->length, and s->packet_length ==
671*0a6a1f1dSLionel Sambuc * DTLS1_RT_HEADER_LENGTH + rr->length
672*0a6a1f1dSLionel Sambuc */
673ebfedea0SLionel Sambuc }
674ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
675ebfedea0SLionel Sambuc
676ebfedea0SLionel Sambuc /* match epochs. NULL means the packet is dropped on the floor */
677ebfedea0SLionel Sambuc bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
678*0a6a1f1dSLionel Sambuc if (bitmap == NULL) {
679ebfedea0SLionel Sambuc rr->length = 0;
680ebfedea0SLionel Sambuc s->packet_length = 0; /* dump this record */
681ebfedea0SLionel Sambuc goto again; /* get another record */
682ebfedea0SLionel Sambuc }
683ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
684ebfedea0SLionel Sambuc /* Only do replay check if no SCTP bio */
685*0a6a1f1dSLionel Sambuc if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
686ebfedea0SLionel Sambuc #endif
687*0a6a1f1dSLionel Sambuc /*
688*0a6a1f1dSLionel Sambuc * Check whether this is a repeat, or aged record. Don't check if
689*0a6a1f1dSLionel Sambuc * we're listening and this message is a ClientHello. They can look
690*0a6a1f1dSLionel Sambuc * as if they're replayed, since they arrive from different
691*0a6a1f1dSLionel Sambuc * connections and would be dropped unnecessarily.
692ebfedea0SLionel Sambuc */
693ebfedea0SLionel Sambuc if (!(s->d1->listen && rr->type == SSL3_RT_HANDSHAKE &&
694*0a6a1f1dSLionel Sambuc s->packet_length > DTLS1_RT_HEADER_LENGTH &&
695*0a6a1f1dSLionel Sambuc s->packet[DTLS1_RT_HEADER_LENGTH] == SSL3_MT_CLIENT_HELLO) &&
696*0a6a1f1dSLionel Sambuc !dtls1_record_replay_check(s, bitmap)) {
697ebfedea0SLionel Sambuc rr->length = 0;
698ebfedea0SLionel Sambuc s->packet_length = 0; /* dump this record */
699ebfedea0SLionel Sambuc goto again; /* get another record */
700ebfedea0SLionel Sambuc }
701ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
702ebfedea0SLionel Sambuc }
703ebfedea0SLionel Sambuc #endif
704ebfedea0SLionel Sambuc
705ebfedea0SLionel Sambuc /* just read a 0 length packet */
706*0a6a1f1dSLionel Sambuc if (rr->length == 0)
707*0a6a1f1dSLionel Sambuc goto again;
708ebfedea0SLionel Sambuc
709*0a6a1f1dSLionel Sambuc /*
710*0a6a1f1dSLionel Sambuc * If this record is from the next epoch (either HM or ALERT), and a
711*0a6a1f1dSLionel Sambuc * handshake is currently in progress, buffer it since it cannot be
712*0a6a1f1dSLionel Sambuc * processed at this time. However, do not buffer anything while
713*0a6a1f1dSLionel Sambuc * listening.
714ebfedea0SLionel Sambuc */
715*0a6a1f1dSLionel Sambuc if (is_next_epoch) {
716*0a6a1f1dSLionel Sambuc if ((SSL_in_init(s) || s->in_handshake) && !s->d1->listen) {
717*0a6a1f1dSLionel Sambuc if (dtls1_buffer_record
718*0a6a1f1dSLionel Sambuc (s, &(s->d1->unprocessed_rcds), rr->seq_num) < 0)
719*0a6a1f1dSLionel Sambuc return -1;
720*0a6a1f1dSLionel Sambuc /* Mark receipt of record. */
721*0a6a1f1dSLionel Sambuc dtls1_record_bitmap_update(s, bitmap);
722ebfedea0SLionel Sambuc }
723ebfedea0SLionel Sambuc rr->length = 0;
724ebfedea0SLionel Sambuc s->packet_length = 0;
725ebfedea0SLionel Sambuc goto again;
726ebfedea0SLionel Sambuc }
727ebfedea0SLionel Sambuc
728*0a6a1f1dSLionel Sambuc if (!dtls1_process_record(s)) {
729ebfedea0SLionel Sambuc rr->length = 0;
730ebfedea0SLionel Sambuc s->packet_length = 0; /* dump this record */
731ebfedea0SLionel Sambuc goto again; /* get another record */
732ebfedea0SLionel Sambuc }
733*0a6a1f1dSLionel Sambuc dtls1_record_bitmap_update(s, bitmap); /* Mark receipt of record. */
734ebfedea0SLionel Sambuc
735ebfedea0SLionel Sambuc return (1);
736ebfedea0SLionel Sambuc
737ebfedea0SLionel Sambuc }
738ebfedea0SLionel Sambuc
739*0a6a1f1dSLionel Sambuc /*-
740*0a6a1f1dSLionel Sambuc * Return up to 'len' payload bytes received in 'type' records.
741ebfedea0SLionel Sambuc * 'type' is one of the following:
742ebfedea0SLionel Sambuc *
743ebfedea0SLionel Sambuc * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
744ebfedea0SLionel Sambuc * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
745ebfedea0SLionel Sambuc * - 0 (during a shutdown, no data has to be returned)
746ebfedea0SLionel Sambuc *
747ebfedea0SLionel Sambuc * If we don't have stored data to work from, read a SSL/TLS record first
748ebfedea0SLionel Sambuc * (possibly multiple records if we still don't have anything to return).
749ebfedea0SLionel Sambuc *
750ebfedea0SLionel Sambuc * This function must handle any surprises the peer may have for us, such as
751ebfedea0SLionel Sambuc * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
752ebfedea0SLionel Sambuc * a surprise, but handled as if it were), or renegotiation requests.
753ebfedea0SLionel Sambuc * Also if record payloads contain fragments too small to process, we store
754ebfedea0SLionel Sambuc * them until there is enough for the respective protocol (the record protocol
755ebfedea0SLionel Sambuc * may use arbitrary fragmentation and even interleaving):
756ebfedea0SLionel Sambuc * Change cipher spec protocol
757ebfedea0SLionel Sambuc * just 1 byte needed, no need for keeping anything stored
758ebfedea0SLionel Sambuc * Alert protocol
759ebfedea0SLionel Sambuc * 2 bytes needed (AlertLevel, AlertDescription)
760ebfedea0SLionel Sambuc * Handshake protocol
761ebfedea0SLionel Sambuc * 4 bytes needed (HandshakeType, uint24 length) -- we just have
762ebfedea0SLionel Sambuc * to detect unexpected Client Hello and Hello Request messages
763ebfedea0SLionel Sambuc * here, anything else is handled by higher layers
764ebfedea0SLionel Sambuc * Application data protocol
765ebfedea0SLionel Sambuc * none of our business
766ebfedea0SLionel Sambuc */
dtls1_read_bytes(SSL * s,int type,unsigned char * buf,int len,int peek)767ebfedea0SLionel Sambuc int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
768ebfedea0SLionel Sambuc {
769ebfedea0SLionel Sambuc int al, i, j, ret;
770ebfedea0SLionel Sambuc unsigned int n;
771ebfedea0SLionel Sambuc SSL3_RECORD *rr;
772ebfedea0SLionel Sambuc void (*cb) (const SSL *ssl, int type2, int val) = NULL;
773ebfedea0SLionel Sambuc
774ebfedea0SLionel Sambuc if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
775ebfedea0SLionel Sambuc if (!ssl3_setup_buffers(s))
776ebfedea0SLionel Sambuc return (-1);
777ebfedea0SLionel Sambuc
778ebfedea0SLionel Sambuc /* XXX: check what the second '&& type' is about */
779ebfedea0SLionel Sambuc if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
780ebfedea0SLionel Sambuc (type != SSL3_RT_HANDSHAKE) && type) ||
781*0a6a1f1dSLionel Sambuc (peek && (type != SSL3_RT_APPLICATION_DATA))) {
782ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
783ebfedea0SLionel Sambuc return -1;
784ebfedea0SLionel Sambuc }
785ebfedea0SLionel Sambuc
786*0a6a1f1dSLionel Sambuc /*
787*0a6a1f1dSLionel Sambuc * check whether there's a handshake message (client hello?) waiting
788*0a6a1f1dSLionel Sambuc */
789ebfedea0SLionel Sambuc if ((ret = have_handshake_fragment(s, type, buf, len, peek)))
790ebfedea0SLionel Sambuc return ret;
791ebfedea0SLionel Sambuc
792*0a6a1f1dSLionel Sambuc /*
793*0a6a1f1dSLionel Sambuc * Now s->d1->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
794*0a6a1f1dSLionel Sambuc */
795ebfedea0SLionel Sambuc
796ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
797*0a6a1f1dSLionel Sambuc /*
798*0a6a1f1dSLionel Sambuc * Continue handshake if it had to be interrupted to read app data with
799*0a6a1f1dSLionel Sambuc * SCTP.
800ebfedea0SLionel Sambuc */
801ebfedea0SLionel Sambuc if ((!s->in_handshake && SSL_in_init(s)) ||
802ebfedea0SLionel Sambuc (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
803*0a6a1f1dSLionel Sambuc (s->state == DTLS1_SCTP_ST_SR_READ_SOCK
804*0a6a1f1dSLionel Sambuc || s->state == DTLS1_SCTP_ST_CR_READ_SOCK)
805*0a6a1f1dSLionel Sambuc && s->s3->in_read_app_data != 2))
806ebfedea0SLionel Sambuc #else
807ebfedea0SLionel Sambuc if (!s->in_handshake && SSL_in_init(s))
808ebfedea0SLionel Sambuc #endif
809ebfedea0SLionel Sambuc {
810ebfedea0SLionel Sambuc /* type == SSL3_RT_APPLICATION_DATA */
811ebfedea0SLionel Sambuc i = s->handshake_func(s);
812*0a6a1f1dSLionel Sambuc if (i < 0)
813*0a6a1f1dSLionel Sambuc return (i);
814*0a6a1f1dSLionel Sambuc if (i == 0) {
815ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
816ebfedea0SLionel Sambuc return (-1);
817ebfedea0SLionel Sambuc }
818ebfedea0SLionel Sambuc }
819ebfedea0SLionel Sambuc
820ebfedea0SLionel Sambuc start:
821ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
822ebfedea0SLionel Sambuc
823*0a6a1f1dSLionel Sambuc /*-
824*0a6a1f1dSLionel Sambuc * s->s3->rrec.type - is the type of record
825ebfedea0SLionel Sambuc * s->s3->rrec.data, - data
826ebfedea0SLionel Sambuc * s->s3->rrec.off, - offset into 'data' for next read
827*0a6a1f1dSLionel Sambuc * s->s3->rrec.length, - number of bytes.
828*0a6a1f1dSLionel Sambuc */
829ebfedea0SLionel Sambuc rr = &(s->s3->rrec);
830ebfedea0SLionel Sambuc
831*0a6a1f1dSLionel Sambuc /*
832*0a6a1f1dSLionel Sambuc * We are not handshaking and have no data yet, so process data buffered
833*0a6a1f1dSLionel Sambuc * during the last handshake in advance, if any.
834ebfedea0SLionel Sambuc */
835*0a6a1f1dSLionel Sambuc if (s->state == SSL_ST_OK && rr->length == 0) {
836ebfedea0SLionel Sambuc pitem *item;
837ebfedea0SLionel Sambuc item = pqueue_pop(s->d1->buffered_app_data.q);
838*0a6a1f1dSLionel Sambuc if (item) {
839ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
840ebfedea0SLionel Sambuc /* Restore bio_dgram_sctp_rcvinfo struct */
841*0a6a1f1dSLionel Sambuc if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {
842ebfedea0SLionel Sambuc DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;
843*0a6a1f1dSLionel Sambuc BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
844*0a6a1f1dSLionel Sambuc sizeof(rdata->recordinfo), &rdata->recordinfo);
845ebfedea0SLionel Sambuc }
846ebfedea0SLionel Sambuc #endif
847ebfedea0SLionel Sambuc
848ebfedea0SLionel Sambuc dtls1_copy_record(s, item);
849ebfedea0SLionel Sambuc
850ebfedea0SLionel Sambuc OPENSSL_free(item->data);
851ebfedea0SLionel Sambuc pitem_free(item);
852ebfedea0SLionel Sambuc }
853ebfedea0SLionel Sambuc }
854ebfedea0SLionel Sambuc
855ebfedea0SLionel Sambuc /* Check for timeout */
856ebfedea0SLionel Sambuc if (dtls1_handle_timeout(s) > 0)
857ebfedea0SLionel Sambuc goto start;
858ebfedea0SLionel Sambuc
859ebfedea0SLionel Sambuc /* get new packet if necessary */
860*0a6a1f1dSLionel Sambuc if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) {
861ebfedea0SLionel Sambuc ret = dtls1_get_record(s);
862*0a6a1f1dSLionel Sambuc if (ret <= 0) {
863ebfedea0SLionel Sambuc ret = dtls1_read_failed(s, ret);
864ebfedea0SLionel Sambuc /* anything other than a timeout is an error */
865ebfedea0SLionel Sambuc if (ret <= 0)
866ebfedea0SLionel Sambuc return (ret);
867ebfedea0SLionel Sambuc else
868ebfedea0SLionel Sambuc goto start;
869ebfedea0SLionel Sambuc }
870ebfedea0SLionel Sambuc }
871ebfedea0SLionel Sambuc
872*0a6a1f1dSLionel Sambuc if (s->d1->listen && rr->type != SSL3_RT_HANDSHAKE) {
873ebfedea0SLionel Sambuc rr->length = 0;
874ebfedea0SLionel Sambuc goto start;
875ebfedea0SLionel Sambuc }
876ebfedea0SLionel Sambuc
877*0a6a1f1dSLionel Sambuc /* we now have a packet which can be read and processed */
878*0a6a1f1dSLionel Sambuc
879*0a6a1f1dSLionel Sambuc if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
880*0a6a1f1dSLionel Sambuc * reset by ssl3_get_finished */
881*0a6a1f1dSLionel Sambuc && (rr->type != SSL3_RT_HANDSHAKE)) {
882*0a6a1f1dSLionel Sambuc /*
883*0a6a1f1dSLionel Sambuc * We now have application data between CCS and Finished. Most likely
884*0a6a1f1dSLionel Sambuc * the packets were reordered on their way, so buffer the application
885*0a6a1f1dSLionel Sambuc * data for later processing rather than dropping the connection.
886*0a6a1f1dSLionel Sambuc */
887*0a6a1f1dSLionel Sambuc if (dtls1_buffer_record(s, &(s->d1->buffered_app_data), rr->seq_num) <
888*0a6a1f1dSLionel Sambuc 0) {
889*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
890*0a6a1f1dSLionel Sambuc return -1;
891*0a6a1f1dSLionel Sambuc }
892*0a6a1f1dSLionel Sambuc rr->length = 0;
893*0a6a1f1dSLionel Sambuc goto start;
894*0a6a1f1dSLionel Sambuc }
895*0a6a1f1dSLionel Sambuc
896*0a6a1f1dSLionel Sambuc /*
897*0a6a1f1dSLionel Sambuc * If the other end has shut down, throw anything we read away (even in
898*0a6a1f1dSLionel Sambuc * 'peek' mode)
899*0a6a1f1dSLionel Sambuc */
900*0a6a1f1dSLionel Sambuc if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
901ebfedea0SLionel Sambuc rr->length = 0;
902ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
903ebfedea0SLionel Sambuc return (0);
904ebfedea0SLionel Sambuc }
905ebfedea0SLionel Sambuc
906*0a6a1f1dSLionel Sambuc if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or
907*0a6a1f1dSLionel Sambuc * SSL3_RT_HANDSHAKE */
908*0a6a1f1dSLionel Sambuc /*
909*0a6a1f1dSLionel Sambuc * make sure that we are not getting application data when we are
910*0a6a1f1dSLionel Sambuc * doing a handshake for the first time
911*0a6a1f1dSLionel Sambuc */
912ebfedea0SLionel Sambuc if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
913*0a6a1f1dSLionel Sambuc (s->enc_read_ctx == NULL)) {
914ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
915ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
916ebfedea0SLionel Sambuc goto f_err;
917ebfedea0SLionel Sambuc }
918ebfedea0SLionel Sambuc
919*0a6a1f1dSLionel Sambuc if (len <= 0)
920*0a6a1f1dSLionel Sambuc return (len);
921ebfedea0SLionel Sambuc
922ebfedea0SLionel Sambuc if ((unsigned int)len > rr->length)
923ebfedea0SLionel Sambuc n = rr->length;
924ebfedea0SLionel Sambuc else
925ebfedea0SLionel Sambuc n = (unsigned int)len;
926ebfedea0SLionel Sambuc
927ebfedea0SLionel Sambuc memcpy(buf, &(rr->data[rr->off]), n);
928*0a6a1f1dSLionel Sambuc if (!peek) {
929ebfedea0SLionel Sambuc rr->length -= n;
930ebfedea0SLionel Sambuc rr->off += n;
931*0a6a1f1dSLionel Sambuc if (rr->length == 0) {
932ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_HEADER;
933ebfedea0SLionel Sambuc rr->off = 0;
934ebfedea0SLionel Sambuc }
935ebfedea0SLionel Sambuc }
936ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
937*0a6a1f1dSLionel Sambuc /*
938*0a6a1f1dSLionel Sambuc * We were about to renegotiate but had to read belated application
939*0a6a1f1dSLionel Sambuc * data first, so retry.
940ebfedea0SLionel Sambuc */
941ebfedea0SLionel Sambuc if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
942ebfedea0SLionel Sambuc rr->type == SSL3_RT_APPLICATION_DATA &&
943*0a6a1f1dSLionel Sambuc (s->state == DTLS1_SCTP_ST_SR_READ_SOCK
944*0a6a1f1dSLionel Sambuc || s->state == DTLS1_SCTP_ST_CR_READ_SOCK)) {
945ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
946ebfedea0SLionel Sambuc BIO_clear_retry_flags(SSL_get_rbio(s));
947ebfedea0SLionel Sambuc BIO_set_retry_read(SSL_get_rbio(s));
948ebfedea0SLionel Sambuc }
949ebfedea0SLionel Sambuc
950*0a6a1f1dSLionel Sambuc /*
951*0a6a1f1dSLionel Sambuc * We might had to delay a close_notify alert because of reordered
952*0a6a1f1dSLionel Sambuc * app data. If there was an alert and there is no message to read
953*0a6a1f1dSLionel Sambuc * anymore, finally set shutdown.
954ebfedea0SLionel Sambuc */
955ebfedea0SLionel Sambuc if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
956*0a6a1f1dSLionel Sambuc s->d1->shutdown_received
957*0a6a1f1dSLionel Sambuc && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
958ebfedea0SLionel Sambuc s->shutdown |= SSL_RECEIVED_SHUTDOWN;
959ebfedea0SLionel Sambuc return (0);
960ebfedea0SLionel Sambuc }
961ebfedea0SLionel Sambuc #endif
962ebfedea0SLionel Sambuc return (n);
963ebfedea0SLionel Sambuc }
964ebfedea0SLionel Sambuc
965*0a6a1f1dSLionel Sambuc /*
966*0a6a1f1dSLionel Sambuc * If we get here, then type != rr->type; if we have a handshake message,
967*0a6a1f1dSLionel Sambuc * then it was unexpected (Hello Request or Client Hello).
968*0a6a1f1dSLionel Sambuc */
969ebfedea0SLionel Sambuc
970*0a6a1f1dSLionel Sambuc /*
971*0a6a1f1dSLionel Sambuc * In case of record types for which we have 'fragment' storage, fill
972*0a6a1f1dSLionel Sambuc * that so that we can process the data at a fixed place.
973ebfedea0SLionel Sambuc */
974ebfedea0SLionel Sambuc {
975ebfedea0SLionel Sambuc unsigned int k, dest_maxlen = 0;
976ebfedea0SLionel Sambuc unsigned char *dest = NULL;
977ebfedea0SLionel Sambuc unsigned int *dest_len = NULL;
978ebfedea0SLionel Sambuc
979*0a6a1f1dSLionel Sambuc if (rr->type == SSL3_RT_HANDSHAKE) {
980ebfedea0SLionel Sambuc dest_maxlen = sizeof s->d1->handshake_fragment;
981ebfedea0SLionel Sambuc dest = s->d1->handshake_fragment;
982ebfedea0SLionel Sambuc dest_len = &s->d1->handshake_fragment_len;
983*0a6a1f1dSLionel Sambuc } else if (rr->type == SSL3_RT_ALERT) {
984ebfedea0SLionel Sambuc dest_maxlen = sizeof(s->d1->alert_fragment);
985ebfedea0SLionel Sambuc dest = s->d1->alert_fragment;
986ebfedea0SLionel Sambuc dest_len = &s->d1->alert_fragment_len;
987ebfedea0SLionel Sambuc }
988ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HEARTBEATS
989*0a6a1f1dSLionel Sambuc else if (rr->type == TLS1_RT_HEARTBEAT) {
990ebfedea0SLionel Sambuc dtls1_process_heartbeat(s);
991ebfedea0SLionel Sambuc
992ebfedea0SLionel Sambuc /* Exit and notify application to read again */
993ebfedea0SLionel Sambuc rr->length = 0;
994ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
995ebfedea0SLionel Sambuc BIO_clear_retry_flags(SSL_get_rbio(s));
996ebfedea0SLionel Sambuc BIO_set_retry_read(SSL_get_rbio(s));
997ebfedea0SLionel Sambuc return (-1);
998ebfedea0SLionel Sambuc }
999ebfedea0SLionel Sambuc #endif
1000ebfedea0SLionel Sambuc /* else it's a CCS message, or application data or wrong */
1001*0a6a1f1dSLionel Sambuc else if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) {
1002*0a6a1f1dSLionel Sambuc /*
1003*0a6a1f1dSLionel Sambuc * Application data while renegotiating is allowed. Try again
1004*0a6a1f1dSLionel Sambuc * reading.
1005ebfedea0SLionel Sambuc */
1006*0a6a1f1dSLionel Sambuc if (rr->type == SSL3_RT_APPLICATION_DATA) {
1007ebfedea0SLionel Sambuc BIO *bio;
1008ebfedea0SLionel Sambuc s->s3->in_read_app_data = 2;
1009ebfedea0SLionel Sambuc bio = SSL_get_rbio(s);
1010ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
1011ebfedea0SLionel Sambuc BIO_clear_retry_flags(bio);
1012ebfedea0SLionel Sambuc BIO_set_retry_read(bio);
1013ebfedea0SLionel Sambuc return (-1);
1014ebfedea0SLionel Sambuc }
1015ebfedea0SLionel Sambuc
1016ebfedea0SLionel Sambuc /* Not certain if this is the right error handling */
1017ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1018ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
1019ebfedea0SLionel Sambuc goto f_err;
1020ebfedea0SLionel Sambuc }
1021ebfedea0SLionel Sambuc
1022*0a6a1f1dSLionel Sambuc if (dest_maxlen > 0) {
1023*0a6a1f1dSLionel Sambuc /*
1024*0a6a1f1dSLionel Sambuc * XDTLS: In a pathalogical case, the Client Hello may be
1025*0a6a1f1dSLionel Sambuc * fragmented--don't always expect dest_maxlen bytes
1026*0a6a1f1dSLionel Sambuc */
1027*0a6a1f1dSLionel Sambuc if (rr->length < dest_maxlen) {
1028ebfedea0SLionel Sambuc #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1029ebfedea0SLionel Sambuc /*
1030ebfedea0SLionel Sambuc * for normal alerts rr->length is 2, while
1031ebfedea0SLionel Sambuc * dest_maxlen is 7 if we were to handle this
1032ebfedea0SLionel Sambuc * non-existing alert...
1033ebfedea0SLionel Sambuc */
1034ebfedea0SLionel Sambuc FIX ME
1035ebfedea0SLionel Sambuc #endif
1036ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_HEADER;
1037ebfedea0SLionel Sambuc rr->length = 0;
1038ebfedea0SLionel Sambuc goto start;
1039ebfedea0SLionel Sambuc }
1040ebfedea0SLionel Sambuc
1041ebfedea0SLionel Sambuc /* now move 'n' bytes: */
1042*0a6a1f1dSLionel Sambuc for (k = 0; k < dest_maxlen; k++) {
1043ebfedea0SLionel Sambuc dest[k] = rr->data[rr->off++];
1044ebfedea0SLionel Sambuc rr->length--;
1045ebfedea0SLionel Sambuc }
1046ebfedea0SLionel Sambuc *dest_len = dest_maxlen;
1047ebfedea0SLionel Sambuc }
1048ebfedea0SLionel Sambuc }
1049ebfedea0SLionel Sambuc
1050*0a6a1f1dSLionel Sambuc /*-
1051*0a6a1f1dSLionel Sambuc * s->d1->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE;
1052ebfedea0SLionel Sambuc * s->d1->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT.
1053*0a6a1f1dSLionel Sambuc * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
1054*0a6a1f1dSLionel Sambuc */
1055ebfedea0SLionel Sambuc
1056ebfedea0SLionel Sambuc /* If we are a client, check for an incoming 'Hello Request': */
1057ebfedea0SLionel Sambuc if ((!s->server) &&
1058ebfedea0SLionel Sambuc (s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
1059ebfedea0SLionel Sambuc (s->d1->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
1060*0a6a1f1dSLionel Sambuc (s->session != NULL) && (s->session->cipher != NULL)) {
1061ebfedea0SLionel Sambuc s->d1->handshake_fragment_len = 0;
1062ebfedea0SLionel Sambuc
1063ebfedea0SLionel Sambuc if ((s->d1->handshake_fragment[1] != 0) ||
1064ebfedea0SLionel Sambuc (s->d1->handshake_fragment[2] != 0) ||
1065*0a6a1f1dSLionel Sambuc (s->d1->handshake_fragment[3] != 0)) {
1066ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1067ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
1068*0a6a1f1dSLionel Sambuc goto f_err;
1069ebfedea0SLionel Sambuc }
1070ebfedea0SLionel Sambuc
1071*0a6a1f1dSLionel Sambuc /*
1072*0a6a1f1dSLionel Sambuc * no need to check sequence number on HELLO REQUEST messages
1073*0a6a1f1dSLionel Sambuc */
1074ebfedea0SLionel Sambuc
1075ebfedea0SLionel Sambuc if (s->msg_callback)
1076ebfedea0SLionel Sambuc s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1077*0a6a1f1dSLionel Sambuc s->d1->handshake_fragment, 4, s,
1078*0a6a1f1dSLionel Sambuc s->msg_callback_arg);
1079ebfedea0SLionel Sambuc
1080ebfedea0SLionel Sambuc if (SSL_is_init_finished(s) &&
1081ebfedea0SLionel Sambuc !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
1082*0a6a1f1dSLionel Sambuc !s->s3->renegotiate) {
1083*0a6a1f1dSLionel Sambuc s->d1->handshake_read_seq++;
1084ebfedea0SLionel Sambuc s->new_session = 1;
1085ebfedea0SLionel Sambuc ssl3_renegotiate(s);
1086*0a6a1f1dSLionel Sambuc if (ssl3_renegotiate_check(s)) {
1087ebfedea0SLionel Sambuc i = s->handshake_func(s);
1088*0a6a1f1dSLionel Sambuc if (i < 0)
1089*0a6a1f1dSLionel Sambuc return (i);
1090*0a6a1f1dSLionel Sambuc if (i == 0) {
1091*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES,
1092*0a6a1f1dSLionel Sambuc SSL_R_SSL_HANDSHAKE_FAILURE);
1093ebfedea0SLionel Sambuc return (-1);
1094ebfedea0SLionel Sambuc }
1095ebfedea0SLionel Sambuc
1096*0a6a1f1dSLionel Sambuc if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
1097*0a6a1f1dSLionel Sambuc if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
1098ebfedea0SLionel Sambuc BIO *bio;
1099*0a6a1f1dSLionel Sambuc /*
1100*0a6a1f1dSLionel Sambuc * In the case where we try to read application data,
1101ebfedea0SLionel Sambuc * but we trigger an SSL handshake, we return -1 with
1102ebfedea0SLionel Sambuc * the retry option set. Otherwise renegotiation may
1103*0a6a1f1dSLionel Sambuc * cause nasty problems in the blocking world
1104*0a6a1f1dSLionel Sambuc */
1105ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
1106ebfedea0SLionel Sambuc bio = SSL_get_rbio(s);
1107ebfedea0SLionel Sambuc BIO_clear_retry_flags(bio);
1108ebfedea0SLionel Sambuc BIO_set_retry_read(bio);
1109ebfedea0SLionel Sambuc return (-1);
1110ebfedea0SLionel Sambuc }
1111ebfedea0SLionel Sambuc }
1112ebfedea0SLionel Sambuc }
1113ebfedea0SLionel Sambuc }
1114*0a6a1f1dSLionel Sambuc /*
1115*0a6a1f1dSLionel Sambuc * we either finished a handshake or ignored the request, now try
1116*0a6a1f1dSLionel Sambuc * again to obtain the (application) data we were asked for
1117*0a6a1f1dSLionel Sambuc */
1118ebfedea0SLionel Sambuc goto start;
1119ebfedea0SLionel Sambuc }
1120ebfedea0SLionel Sambuc
1121*0a6a1f1dSLionel Sambuc if (s->d1->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) {
1122ebfedea0SLionel Sambuc int alert_level = s->d1->alert_fragment[0];
1123ebfedea0SLionel Sambuc int alert_descr = s->d1->alert_fragment[1];
1124ebfedea0SLionel Sambuc
1125ebfedea0SLionel Sambuc s->d1->alert_fragment_len = 0;
1126ebfedea0SLionel Sambuc
1127ebfedea0SLionel Sambuc if (s->msg_callback)
1128ebfedea0SLionel Sambuc s->msg_callback(0, s->version, SSL3_RT_ALERT,
1129ebfedea0SLionel Sambuc s->d1->alert_fragment, 2, s, s->msg_callback_arg);
1130ebfedea0SLionel Sambuc
1131ebfedea0SLionel Sambuc if (s->info_callback != NULL)
1132ebfedea0SLionel Sambuc cb = s->info_callback;
1133ebfedea0SLionel Sambuc else if (s->ctx->info_callback != NULL)
1134ebfedea0SLionel Sambuc cb = s->ctx->info_callback;
1135ebfedea0SLionel Sambuc
1136*0a6a1f1dSLionel Sambuc if (cb != NULL) {
1137ebfedea0SLionel Sambuc j = (alert_level << 8) | alert_descr;
1138ebfedea0SLionel Sambuc cb(s, SSL_CB_READ_ALERT, j);
1139ebfedea0SLionel Sambuc }
1140ebfedea0SLionel Sambuc
1141*0a6a1f1dSLionel Sambuc if (alert_level == SSL3_AL_WARNING) {
1142ebfedea0SLionel Sambuc s->s3->warn_alert = alert_descr;
1143*0a6a1f1dSLionel Sambuc if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
1144ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
1145*0a6a1f1dSLionel Sambuc /*
1146*0a6a1f1dSLionel Sambuc * With SCTP and streams the socket may deliver app data
1147*0a6a1f1dSLionel Sambuc * after a close_notify alert. We have to check this first so
1148*0a6a1f1dSLionel Sambuc * that nothing gets discarded.
1149ebfedea0SLionel Sambuc */
1150ebfedea0SLionel Sambuc if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
1151*0a6a1f1dSLionel Sambuc BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
1152ebfedea0SLionel Sambuc s->d1->shutdown_received = 1;
1153ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
1154ebfedea0SLionel Sambuc BIO_clear_retry_flags(SSL_get_rbio(s));
1155ebfedea0SLionel Sambuc BIO_set_retry_read(SSL_get_rbio(s));
1156ebfedea0SLionel Sambuc return -1;
1157ebfedea0SLionel Sambuc }
1158ebfedea0SLionel Sambuc #endif
1159ebfedea0SLionel Sambuc s->shutdown |= SSL_RECEIVED_SHUTDOWN;
1160ebfedea0SLionel Sambuc return (0);
1161ebfedea0SLionel Sambuc }
1162ebfedea0SLionel Sambuc #if 0
1163ebfedea0SLionel Sambuc /* XXX: this is a possible improvement in the future */
1164ebfedea0SLionel Sambuc /* now check if it's a missing record */
1165*0a6a1f1dSLionel Sambuc if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
1166ebfedea0SLionel Sambuc unsigned short seq;
1167ebfedea0SLionel Sambuc unsigned int frag_off;
1168ebfedea0SLionel Sambuc unsigned char *p = &(s->d1->alert_fragment[2]);
1169ebfedea0SLionel Sambuc
1170ebfedea0SLionel Sambuc n2s(p, seq);
1171ebfedea0SLionel Sambuc n2l3(p, frag_off);
1172ebfedea0SLionel Sambuc
1173ebfedea0SLionel Sambuc dtls1_retransmit_message(s,
1174*0a6a1f1dSLionel Sambuc dtls1_get_queue_priority
1175*0a6a1f1dSLionel Sambuc (frag->msg_header.seq, 0), frag_off,
1176*0a6a1f1dSLionel Sambuc &found);
1177*0a6a1f1dSLionel Sambuc if (!found && SSL_in_init(s)) {
1178*0a6a1f1dSLionel Sambuc /*
1179*0a6a1f1dSLionel Sambuc * fprintf( stderr,"in init = %d\n", SSL_in_init(s));
1180*0a6a1f1dSLionel Sambuc */
1181*0a6a1f1dSLionel Sambuc /*
1182*0a6a1f1dSLionel Sambuc * requested a message not yet sent, send an alert
1183*0a6a1f1dSLionel Sambuc * ourselves
1184*0a6a1f1dSLionel Sambuc */
1185ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_WARNING,
1186ebfedea0SLionel Sambuc DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
1187ebfedea0SLionel Sambuc }
1188ebfedea0SLionel Sambuc }
1189ebfedea0SLionel Sambuc #endif
1190*0a6a1f1dSLionel Sambuc } else if (alert_level == SSL3_AL_FATAL) {
1191ebfedea0SLionel Sambuc char tmp[16];
1192ebfedea0SLionel Sambuc
1193ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
1194ebfedea0SLionel Sambuc s->s3->fatal_alert = alert_descr;
1195*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES,
1196*0a6a1f1dSLionel Sambuc SSL_AD_REASON_OFFSET + alert_descr);
1197ebfedea0SLionel Sambuc BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
1198ebfedea0SLionel Sambuc ERR_add_error_data(2, "SSL alert number ", tmp);
1199ebfedea0SLionel Sambuc s->shutdown |= SSL_RECEIVED_SHUTDOWN;
1200ebfedea0SLionel Sambuc SSL_CTX_remove_session(s->ctx, s->session);
1201ebfedea0SLionel Sambuc return (0);
1202*0a6a1f1dSLionel Sambuc } else {
1203ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1204ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
1205ebfedea0SLionel Sambuc goto f_err;
1206ebfedea0SLionel Sambuc }
1207ebfedea0SLionel Sambuc
1208ebfedea0SLionel Sambuc goto start;
1209ebfedea0SLionel Sambuc }
1210ebfedea0SLionel Sambuc
1211*0a6a1f1dSLionel Sambuc if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
1212*0a6a1f1dSLionel Sambuc * shutdown */
1213ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
1214ebfedea0SLionel Sambuc rr->length = 0;
1215ebfedea0SLionel Sambuc return (0);
1216ebfedea0SLionel Sambuc }
1217ebfedea0SLionel Sambuc
1218*0a6a1f1dSLionel Sambuc if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1219ebfedea0SLionel Sambuc struct ccs_header_st ccs_hdr;
1220ebfedea0SLionel Sambuc unsigned int ccs_hdr_len = DTLS1_CCS_HEADER_LENGTH;
1221ebfedea0SLionel Sambuc
1222ebfedea0SLionel Sambuc dtls1_get_ccs_header(rr->data, &ccs_hdr);
1223ebfedea0SLionel Sambuc
1224ebfedea0SLionel Sambuc if (s->version == DTLS1_BAD_VER)
1225ebfedea0SLionel Sambuc ccs_hdr_len = 3;
1226ebfedea0SLionel Sambuc
1227*0a6a1f1dSLionel Sambuc /*
1228*0a6a1f1dSLionel Sambuc * 'Change Cipher Spec' is just a single byte, so we know exactly
1229*0a6a1f1dSLionel Sambuc * what the record payload has to look like
1230*0a6a1f1dSLionel Sambuc */
1231ebfedea0SLionel Sambuc /* XDTLS: check that epoch is consistent */
1232ebfedea0SLionel Sambuc if ((rr->length != ccs_hdr_len) ||
1233*0a6a1f1dSLionel Sambuc (rr->off != 0) || (rr->data[0] != SSL3_MT_CCS)) {
1234ebfedea0SLionel Sambuc i = SSL_AD_ILLEGAL_PARAMETER;
1235ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_CHANGE_CIPHER_SPEC);
1236ebfedea0SLionel Sambuc goto err;
1237ebfedea0SLionel Sambuc }
1238ebfedea0SLionel Sambuc
1239ebfedea0SLionel Sambuc rr->length = 0;
1240ebfedea0SLionel Sambuc
1241ebfedea0SLionel Sambuc if (s->msg_callback)
1242ebfedea0SLionel Sambuc s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
1243ebfedea0SLionel Sambuc rr->data, 1, s, s->msg_callback_arg);
1244ebfedea0SLionel Sambuc
1245*0a6a1f1dSLionel Sambuc /*
1246*0a6a1f1dSLionel Sambuc * We can't process a CCS now, because previous handshake messages
1247*0a6a1f1dSLionel Sambuc * are still missing, so just drop it.
1248ebfedea0SLionel Sambuc */
1249*0a6a1f1dSLionel Sambuc if (!s->d1->change_cipher_spec_ok) {
1250ebfedea0SLionel Sambuc goto start;
1251ebfedea0SLionel Sambuc }
1252ebfedea0SLionel Sambuc
1253ebfedea0SLionel Sambuc s->d1->change_cipher_spec_ok = 0;
1254ebfedea0SLionel Sambuc
1255ebfedea0SLionel Sambuc s->s3->change_cipher_spec = 1;
1256ebfedea0SLionel Sambuc if (!ssl3_do_change_cipher_spec(s))
1257ebfedea0SLionel Sambuc goto err;
1258ebfedea0SLionel Sambuc
1259ebfedea0SLionel Sambuc /* do this whenever CCS is processed */
1260ebfedea0SLionel Sambuc dtls1_reset_seq_numbers(s, SSL3_CC_READ);
1261ebfedea0SLionel Sambuc
1262ebfedea0SLionel Sambuc if (s->version == DTLS1_BAD_VER)
1263ebfedea0SLionel Sambuc s->d1->handshake_read_seq++;
1264ebfedea0SLionel Sambuc
1265ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
1266*0a6a1f1dSLionel Sambuc /*
1267*0a6a1f1dSLionel Sambuc * Remember that a CCS has been received, so that an old key of
1268*0a6a1f1dSLionel Sambuc * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
1269*0a6a1f1dSLionel Sambuc * SCTP is used
1270ebfedea0SLionel Sambuc */
1271ebfedea0SLionel Sambuc BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
1272ebfedea0SLionel Sambuc #endif
1273ebfedea0SLionel Sambuc
1274ebfedea0SLionel Sambuc goto start;
1275ebfedea0SLionel Sambuc }
1276ebfedea0SLionel Sambuc
1277*0a6a1f1dSLionel Sambuc /*
1278*0a6a1f1dSLionel Sambuc * Unexpected handshake message (Client Hello, or protocol violation)
1279*0a6a1f1dSLionel Sambuc */
1280ebfedea0SLionel Sambuc if ((s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
1281*0a6a1f1dSLionel Sambuc !s->in_handshake) {
1282ebfedea0SLionel Sambuc struct hm_header_st msg_hdr;
1283ebfedea0SLionel Sambuc
1284ebfedea0SLionel Sambuc /* this may just be a stale retransmit */
1285ebfedea0SLionel Sambuc dtls1_get_message_header(rr->data, &msg_hdr);
1286*0a6a1f1dSLionel Sambuc if (rr->epoch != s->d1->r_epoch) {
1287ebfedea0SLionel Sambuc rr->length = 0;
1288ebfedea0SLionel Sambuc goto start;
1289ebfedea0SLionel Sambuc }
1290ebfedea0SLionel Sambuc
1291*0a6a1f1dSLionel Sambuc /*
1292*0a6a1f1dSLionel Sambuc * If we are server, we may have a repeated FINISHED of the client
1293*0a6a1f1dSLionel Sambuc * here, then retransmit our CCS and FINISHED.
1294ebfedea0SLionel Sambuc */
1295*0a6a1f1dSLionel Sambuc if (msg_hdr.type == SSL3_MT_FINISHED) {
1296ebfedea0SLionel Sambuc if (dtls1_check_timeout_num(s) < 0)
1297ebfedea0SLionel Sambuc return -1;
1298ebfedea0SLionel Sambuc
1299ebfedea0SLionel Sambuc dtls1_retransmit_buffered_messages(s);
1300ebfedea0SLionel Sambuc rr->length = 0;
1301ebfedea0SLionel Sambuc goto start;
1302ebfedea0SLionel Sambuc }
1303ebfedea0SLionel Sambuc
1304ebfedea0SLionel Sambuc if (((s->state & SSL_ST_MASK) == SSL_ST_OK) &&
1305*0a6a1f1dSLionel Sambuc !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
1306*0a6a1f1dSLionel Sambuc #if 0 /* worked only because C operator preferences
1307*0a6a1f1dSLionel Sambuc * are not as expected (and because this is
1308*0a6a1f1dSLionel Sambuc * not really needed for clients except for
1309*0a6a1f1dSLionel Sambuc * detecting protocol violations): */
1310ebfedea0SLionel Sambuc s->state = SSL_ST_BEFORE | (s->server)
1311*0a6a1f1dSLionel Sambuc ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1312ebfedea0SLionel Sambuc #else
1313ebfedea0SLionel Sambuc s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1314ebfedea0SLionel Sambuc #endif
1315ebfedea0SLionel Sambuc s->renegotiate = 1;
1316ebfedea0SLionel Sambuc s->new_session = 1;
1317ebfedea0SLionel Sambuc }
1318ebfedea0SLionel Sambuc i = s->handshake_func(s);
1319*0a6a1f1dSLionel Sambuc if (i < 0)
1320*0a6a1f1dSLionel Sambuc return (i);
1321*0a6a1f1dSLionel Sambuc if (i == 0) {
1322ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
1323ebfedea0SLionel Sambuc return (-1);
1324ebfedea0SLionel Sambuc }
1325ebfedea0SLionel Sambuc
1326*0a6a1f1dSLionel Sambuc if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
1327*0a6a1f1dSLionel Sambuc if (s->s3->rbuf.left == 0) { /* no read-ahead left? */
1328ebfedea0SLionel Sambuc BIO *bio;
1329*0a6a1f1dSLionel Sambuc /*
1330*0a6a1f1dSLionel Sambuc * In the case where we try to read application data, but we
1331*0a6a1f1dSLionel Sambuc * trigger an SSL handshake, we return -1 with the retry
1332*0a6a1f1dSLionel Sambuc * option set. Otherwise renegotiation may cause nasty
1333*0a6a1f1dSLionel Sambuc * problems in the blocking world
1334*0a6a1f1dSLionel Sambuc */
1335ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
1336ebfedea0SLionel Sambuc bio = SSL_get_rbio(s);
1337ebfedea0SLionel Sambuc BIO_clear_retry_flags(bio);
1338ebfedea0SLionel Sambuc BIO_set_retry_read(bio);
1339ebfedea0SLionel Sambuc return (-1);
1340ebfedea0SLionel Sambuc }
1341ebfedea0SLionel Sambuc }
1342ebfedea0SLionel Sambuc goto start;
1343ebfedea0SLionel Sambuc }
1344ebfedea0SLionel Sambuc
1345*0a6a1f1dSLionel Sambuc switch (rr->type) {
1346ebfedea0SLionel Sambuc default:
1347ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLS
1348ebfedea0SLionel Sambuc /* TLS just ignores unknown message types */
1349*0a6a1f1dSLionel Sambuc if (s->version == TLS1_VERSION) {
1350ebfedea0SLionel Sambuc rr->length = 0;
1351ebfedea0SLionel Sambuc goto start;
1352ebfedea0SLionel Sambuc }
1353ebfedea0SLionel Sambuc #endif
1354ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1355ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
1356ebfedea0SLionel Sambuc goto f_err;
1357ebfedea0SLionel Sambuc case SSL3_RT_CHANGE_CIPHER_SPEC:
1358ebfedea0SLionel Sambuc case SSL3_RT_ALERT:
1359ebfedea0SLionel Sambuc case SSL3_RT_HANDSHAKE:
1360*0a6a1f1dSLionel Sambuc /*
1361*0a6a1f1dSLionel Sambuc * we already handled all of these, with the possible exception of
1362*0a6a1f1dSLionel Sambuc * SSL3_RT_HANDSHAKE when s->in_handshake is set, but that should not
1363*0a6a1f1dSLionel Sambuc * happen when type != rr->type
1364*0a6a1f1dSLionel Sambuc */
1365ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1366ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
1367ebfedea0SLionel Sambuc goto f_err;
1368ebfedea0SLionel Sambuc case SSL3_RT_APPLICATION_DATA:
1369*0a6a1f1dSLionel Sambuc /*
1370*0a6a1f1dSLionel Sambuc * At this point, we were expecting handshake data, but have
1371*0a6a1f1dSLionel Sambuc * application data. If the library was running inside ssl3_read()
1372*0a6a1f1dSLionel Sambuc * (i.e. in_read_app_data is set) and it makes sense to read
1373*0a6a1f1dSLionel Sambuc * application data at this point (session renegotiation not yet
1374*0a6a1f1dSLionel Sambuc * started), we will indulge it.
1375ebfedea0SLionel Sambuc */
1376ebfedea0SLionel Sambuc if (s->s3->in_read_app_data &&
1377ebfedea0SLionel Sambuc (s->s3->total_renegotiations != 0) &&
1378*0a6a1f1dSLionel Sambuc (((s->state & SSL_ST_CONNECT) &&
1379ebfedea0SLionel Sambuc (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
1380ebfedea0SLionel Sambuc (s->state <= SSL3_ST_CR_SRVR_HELLO_A)
1381*0a6a1f1dSLionel Sambuc ) || ((s->state & SSL_ST_ACCEPT) &&
1382ebfedea0SLionel Sambuc (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
1383ebfedea0SLionel Sambuc (s->state >= SSL3_ST_SR_CLNT_HELLO_A)
1384ebfedea0SLionel Sambuc )
1385*0a6a1f1dSLionel Sambuc )) {
1386ebfedea0SLionel Sambuc s->s3->in_read_app_data = 2;
1387ebfedea0SLionel Sambuc return (-1);
1388*0a6a1f1dSLionel Sambuc } else {
1389ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1390ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
1391ebfedea0SLionel Sambuc goto f_err;
1392ebfedea0SLionel Sambuc }
1393ebfedea0SLionel Sambuc }
1394ebfedea0SLionel Sambuc /* not reached */
1395ebfedea0SLionel Sambuc
1396ebfedea0SLionel Sambuc f_err:
1397ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
1398ebfedea0SLionel Sambuc err:
1399ebfedea0SLionel Sambuc return (-1);
1400ebfedea0SLionel Sambuc }
1401ebfedea0SLionel Sambuc
dtls1_write_app_data_bytes(SSL * s,int type,const void * buf_,int len)1402*0a6a1f1dSLionel Sambuc int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len)
1403ebfedea0SLionel Sambuc {
1404ebfedea0SLionel Sambuc int i;
1405ebfedea0SLionel Sambuc
1406ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
1407*0a6a1f1dSLionel Sambuc /*
1408*0a6a1f1dSLionel Sambuc * Check if we have to continue an interrupted handshake for reading
1409*0a6a1f1dSLionel Sambuc * belated app data with SCTP.
1410ebfedea0SLionel Sambuc */
1411ebfedea0SLionel Sambuc if ((SSL_in_init(s) && !s->in_handshake) ||
1412ebfedea0SLionel Sambuc (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
1413*0a6a1f1dSLionel Sambuc (s->state == DTLS1_SCTP_ST_SR_READ_SOCK
1414*0a6a1f1dSLionel Sambuc || s->state == DTLS1_SCTP_ST_CR_READ_SOCK)))
1415ebfedea0SLionel Sambuc #else
1416ebfedea0SLionel Sambuc if (SSL_in_init(s) && !s->in_handshake)
1417ebfedea0SLionel Sambuc #endif
1418ebfedea0SLionel Sambuc {
1419ebfedea0SLionel Sambuc i = s->handshake_func(s);
1420*0a6a1f1dSLionel Sambuc if (i < 0)
1421*0a6a1f1dSLionel Sambuc return (i);
1422*0a6a1f1dSLionel Sambuc if (i == 0) {
1423*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES,
1424*0a6a1f1dSLionel Sambuc SSL_R_SSL_HANDSHAKE_FAILURE);
1425ebfedea0SLionel Sambuc return -1;
1426ebfedea0SLionel Sambuc }
1427ebfedea0SLionel Sambuc }
1428ebfedea0SLionel Sambuc
1429*0a6a1f1dSLionel Sambuc if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
1430ebfedea0SLionel Sambuc SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES, SSL_R_DTLS_MESSAGE_TOO_BIG);
1431ebfedea0SLionel Sambuc return -1;
1432ebfedea0SLionel Sambuc }
1433ebfedea0SLionel Sambuc
1434ebfedea0SLionel Sambuc i = dtls1_write_bytes(s, type, buf_, len);
1435ebfedea0SLionel Sambuc return i;
1436ebfedea0SLionel Sambuc }
1437ebfedea0SLionel Sambuc
1438*0a6a1f1dSLionel Sambuc /*
1439*0a6a1f1dSLionel Sambuc * this only happens when a client hello is received and a handshake
1440*0a6a1f1dSLionel Sambuc * is started.
1441*0a6a1f1dSLionel Sambuc */
1442ebfedea0SLionel Sambuc static int
have_handshake_fragment(SSL * s,int type,unsigned char * buf,int len,int peek)1443ebfedea0SLionel Sambuc have_handshake_fragment(SSL *s, int type, unsigned char *buf,
1444ebfedea0SLionel Sambuc int len, int peek)
1445ebfedea0SLionel Sambuc {
1446ebfedea0SLionel Sambuc
1447ebfedea0SLionel Sambuc if ((type == SSL3_RT_HANDSHAKE) && (s->d1->handshake_fragment_len > 0))
1448ebfedea0SLionel Sambuc /* (partially) satisfy request from storage */
1449ebfedea0SLionel Sambuc {
1450ebfedea0SLionel Sambuc unsigned char *src = s->d1->handshake_fragment;
1451ebfedea0SLionel Sambuc unsigned char *dst = buf;
1452ebfedea0SLionel Sambuc unsigned int k, n;
1453ebfedea0SLionel Sambuc
1454ebfedea0SLionel Sambuc /* peek == 0 */
1455ebfedea0SLionel Sambuc n = 0;
1456*0a6a1f1dSLionel Sambuc while ((len > 0) && (s->d1->handshake_fragment_len > 0)) {
1457ebfedea0SLionel Sambuc *dst++ = *src++;
1458*0a6a1f1dSLionel Sambuc len--;
1459*0a6a1f1dSLionel Sambuc s->d1->handshake_fragment_len--;
1460ebfedea0SLionel Sambuc n++;
1461ebfedea0SLionel Sambuc }
1462ebfedea0SLionel Sambuc /* move any remaining fragment bytes: */
1463ebfedea0SLionel Sambuc for (k = 0; k < s->d1->handshake_fragment_len; k++)
1464ebfedea0SLionel Sambuc s->d1->handshake_fragment[k] = *src++;
1465ebfedea0SLionel Sambuc return n;
1466ebfedea0SLionel Sambuc }
1467ebfedea0SLionel Sambuc
1468ebfedea0SLionel Sambuc return 0;
1469ebfedea0SLionel Sambuc }
1470ebfedea0SLionel Sambuc
1471*0a6a1f1dSLionel Sambuc /*
1472*0a6a1f1dSLionel Sambuc * Call this to write data in records of type 'type' It will return <= 0 if
1473*0a6a1f1dSLionel Sambuc * not all data has been sent or non-blocking IO.
1474ebfedea0SLionel Sambuc */
dtls1_write_bytes(SSL * s,int type,const void * buf,int len)1475ebfedea0SLionel Sambuc int dtls1_write_bytes(SSL *s, int type, const void *buf, int len)
1476ebfedea0SLionel Sambuc {
1477ebfedea0SLionel Sambuc int i;
1478ebfedea0SLionel Sambuc
1479ebfedea0SLionel Sambuc OPENSSL_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
1480ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
1481ebfedea0SLionel Sambuc i = do_dtls1_write(s, type, buf, len, 0);
1482ebfedea0SLionel Sambuc return i;
1483ebfedea0SLionel Sambuc }
1484ebfedea0SLionel Sambuc
do_dtls1_write(SSL * s,int type,const unsigned char * buf,unsigned int len,int create_empty_fragment)1485*0a6a1f1dSLionel Sambuc int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
1486*0a6a1f1dSLionel Sambuc unsigned int len, int create_empty_fragment)
1487ebfedea0SLionel Sambuc {
1488ebfedea0SLionel Sambuc unsigned char *p, *pseq;
1489ebfedea0SLionel Sambuc int i, mac_size, clear = 0;
1490ebfedea0SLionel Sambuc int prefix_len = 0;
1491ebfedea0SLionel Sambuc SSL3_RECORD *wr;
1492ebfedea0SLionel Sambuc SSL3_BUFFER *wb;
1493ebfedea0SLionel Sambuc SSL_SESSION *sess;
1494ebfedea0SLionel Sambuc int bs;
1495ebfedea0SLionel Sambuc
1496*0a6a1f1dSLionel Sambuc /*
1497*0a6a1f1dSLionel Sambuc * first check if there is a SSL3_BUFFER still being written out. This
1498*0a6a1f1dSLionel Sambuc * will happen with non blocking IO
1499*0a6a1f1dSLionel Sambuc */
1500*0a6a1f1dSLionel Sambuc if (s->s3->wbuf.left != 0) {
1501ebfedea0SLionel Sambuc OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */
1502ebfedea0SLionel Sambuc return (ssl3_write_pending(s, type, buf, len));
1503ebfedea0SLionel Sambuc }
1504ebfedea0SLionel Sambuc
1505ebfedea0SLionel Sambuc /* If we have an alert to send, lets send it */
1506*0a6a1f1dSLionel Sambuc if (s->s3->alert_dispatch) {
1507ebfedea0SLionel Sambuc i = s->method->ssl_dispatch_alert(s);
1508ebfedea0SLionel Sambuc if (i <= 0)
1509ebfedea0SLionel Sambuc return (i);
1510ebfedea0SLionel Sambuc /* if it went, fall through and send more stuff */
1511ebfedea0SLionel Sambuc }
1512ebfedea0SLionel Sambuc
1513ebfedea0SLionel Sambuc if (len == 0 && !create_empty_fragment)
1514ebfedea0SLionel Sambuc return 0;
1515ebfedea0SLionel Sambuc
1516ebfedea0SLionel Sambuc wr = &(s->s3->wrec);
1517ebfedea0SLionel Sambuc wb = &(s->s3->wbuf);
1518ebfedea0SLionel Sambuc sess = s->session;
1519ebfedea0SLionel Sambuc
1520ebfedea0SLionel Sambuc if ((sess == NULL) ||
1521*0a6a1f1dSLionel Sambuc (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))
1522ebfedea0SLionel Sambuc clear = 1;
1523ebfedea0SLionel Sambuc
1524ebfedea0SLionel Sambuc if (clear)
1525ebfedea0SLionel Sambuc mac_size = 0;
1526*0a6a1f1dSLionel Sambuc else {
1527ebfedea0SLionel Sambuc mac_size = EVP_MD_CTX_size(s->write_hash);
1528ebfedea0SLionel Sambuc if (mac_size < 0)
1529ebfedea0SLionel Sambuc goto err;
1530ebfedea0SLionel Sambuc }
1531ebfedea0SLionel Sambuc
1532ebfedea0SLionel Sambuc /* DTLS implements explicit IV, so no need for empty fragments */
1533ebfedea0SLionel Sambuc #if 0
1534*0a6a1f1dSLionel Sambuc /*
1535*0a6a1f1dSLionel Sambuc * 'create_empty_fragment' is true only when this function calls itself
1536*0a6a1f1dSLionel Sambuc */
1537ebfedea0SLionel Sambuc if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done
1538ebfedea0SLionel Sambuc && SSL_version(s) != DTLS1_VERSION && SSL_version(s) != DTLS1_BAD_VER)
1539ebfedea0SLionel Sambuc {
1540*0a6a1f1dSLionel Sambuc /*
1541*0a6a1f1dSLionel Sambuc * countermeasure against known-IV weakness in CBC ciphersuites (see
1542*0a6a1f1dSLionel Sambuc * http://www.openssl.org/~bodo/tls-cbc.txt)
1543ebfedea0SLionel Sambuc */
1544ebfedea0SLionel Sambuc
1545*0a6a1f1dSLionel Sambuc if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
1546*0a6a1f1dSLionel Sambuc /*
1547*0a6a1f1dSLionel Sambuc * recursive function call with 'create_empty_fragment' set; this
1548*0a6a1f1dSLionel Sambuc * prepares and buffers the data for an empty fragment (these
1549*0a6a1f1dSLionel Sambuc * 'prefix_len' bytes are sent out later together with the actual
1550*0a6a1f1dSLionel Sambuc * payload)
1551*0a6a1f1dSLionel Sambuc */
1552ebfedea0SLionel Sambuc prefix_len = s->method->do_ssl_write(s, type, buf, 0, 1);
1553ebfedea0SLionel Sambuc if (prefix_len <= 0)
1554ebfedea0SLionel Sambuc goto err;
1555ebfedea0SLionel Sambuc
1556*0a6a1f1dSLionel Sambuc if (s->s3->wbuf.len <
1557*0a6a1f1dSLionel Sambuc (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE) {
1558ebfedea0SLionel Sambuc /* insufficient space */
1559ebfedea0SLionel Sambuc SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR);
1560ebfedea0SLionel Sambuc goto err;
1561ebfedea0SLionel Sambuc }
1562ebfedea0SLionel Sambuc }
1563ebfedea0SLionel Sambuc
1564ebfedea0SLionel Sambuc s->s3->empty_fragment_done = 1;
1565ebfedea0SLionel Sambuc }
1566ebfedea0SLionel Sambuc #endif
1567ebfedea0SLionel Sambuc p = wb->buf + prefix_len;
1568ebfedea0SLionel Sambuc
1569ebfedea0SLionel Sambuc /* write the header */
1570ebfedea0SLionel Sambuc
1571ebfedea0SLionel Sambuc *(p++) = type & 0xff;
1572ebfedea0SLionel Sambuc wr->type = type;
1573ebfedea0SLionel Sambuc
1574ebfedea0SLionel Sambuc *(p++) = (s->version >> 8);
1575ebfedea0SLionel Sambuc *(p++) = s->version & 0xff;
1576ebfedea0SLionel Sambuc
1577ebfedea0SLionel Sambuc /* field where we are to write out packet epoch, seq num and len */
1578ebfedea0SLionel Sambuc pseq = p;
1579ebfedea0SLionel Sambuc p += 10;
1580ebfedea0SLionel Sambuc
1581ebfedea0SLionel Sambuc /* lets setup the record stuff. */
1582ebfedea0SLionel Sambuc
1583*0a6a1f1dSLionel Sambuc /*
1584*0a6a1f1dSLionel Sambuc * Make space for the explicit IV in case of CBC. (this is a bit of a
1585*0a6a1f1dSLionel Sambuc * boundary violation, but what the heck).
1586ebfedea0SLionel Sambuc */
1587ebfedea0SLionel Sambuc if (s->enc_write_ctx &&
1588ebfedea0SLionel Sambuc (EVP_CIPHER_mode(s->enc_write_ctx->cipher) & EVP_CIPH_CBC_MODE))
1589ebfedea0SLionel Sambuc bs = EVP_CIPHER_block_size(s->enc_write_ctx->cipher);
1590ebfedea0SLionel Sambuc else
1591ebfedea0SLionel Sambuc bs = 0;
1592ebfedea0SLionel Sambuc
1593ebfedea0SLionel Sambuc wr->data = p + bs; /* make room for IV in case of CBC */
1594ebfedea0SLionel Sambuc wr->length = (int)len;
1595ebfedea0SLionel Sambuc wr->input = (unsigned char *)buf;
1596ebfedea0SLionel Sambuc
1597*0a6a1f1dSLionel Sambuc /*
1598*0a6a1f1dSLionel Sambuc * we now 'read' from wr->input, wr->length bytes into wr->data
1599*0a6a1f1dSLionel Sambuc */
1600ebfedea0SLionel Sambuc
1601ebfedea0SLionel Sambuc /* first we compress */
1602*0a6a1f1dSLionel Sambuc if (s->compress != NULL) {
1603*0a6a1f1dSLionel Sambuc if (!ssl3_do_compress(s)) {
1604ebfedea0SLionel Sambuc SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);
1605ebfedea0SLionel Sambuc goto err;
1606ebfedea0SLionel Sambuc }
1607*0a6a1f1dSLionel Sambuc } else {
1608ebfedea0SLionel Sambuc memcpy(wr->data, wr->input, wr->length);
1609ebfedea0SLionel Sambuc wr->input = wr->data;
1610ebfedea0SLionel Sambuc }
1611ebfedea0SLionel Sambuc
1612*0a6a1f1dSLionel Sambuc /*
1613*0a6a1f1dSLionel Sambuc * we should still have the output to wr->data and the input from
1614*0a6a1f1dSLionel Sambuc * wr->input. Length should be wr->length. wr->data still points in the
1615*0a6a1f1dSLionel Sambuc * wb->buf
1616*0a6a1f1dSLionel Sambuc */
1617ebfedea0SLionel Sambuc
1618*0a6a1f1dSLionel Sambuc if (mac_size != 0) {
1619ebfedea0SLionel Sambuc if (s->method->ssl3_enc->mac(s, &(p[wr->length + bs]), 1) < 0)
1620ebfedea0SLionel Sambuc goto err;
1621ebfedea0SLionel Sambuc wr->length += mac_size;
1622ebfedea0SLionel Sambuc }
1623ebfedea0SLionel Sambuc
1624ebfedea0SLionel Sambuc /* this is true regardless of mac size */
1625ebfedea0SLionel Sambuc wr->input = p;
1626ebfedea0SLionel Sambuc wr->data = p;
1627ebfedea0SLionel Sambuc
1628ebfedea0SLionel Sambuc /* ssl3_enc can only have an error on read */
1629*0a6a1f1dSLionel Sambuc if (bs) { /* bs != 0 in case of CBC */
1630ebfedea0SLionel Sambuc RAND_pseudo_bytes(p, bs);
1631*0a6a1f1dSLionel Sambuc /*
1632*0a6a1f1dSLionel Sambuc * master IV and last CBC residue stand for the rest of randomness
1633*0a6a1f1dSLionel Sambuc */
1634ebfedea0SLionel Sambuc wr->length += bs;
1635ebfedea0SLionel Sambuc }
1636ebfedea0SLionel Sambuc
1637*0a6a1f1dSLionel Sambuc if (s->method->ssl3_enc->enc(s, 1) < 1)
1638*0a6a1f1dSLionel Sambuc goto err;
1639ebfedea0SLionel Sambuc
1640ebfedea0SLionel Sambuc /* record length after mac and block padding */
1641*0a6a1f1dSLionel Sambuc /*
1642*0a6a1f1dSLionel Sambuc * if (type == SSL3_RT_APPLICATION_DATA || (type == SSL3_RT_ALERT && !
1643*0a6a1f1dSLionel Sambuc * SSL_in_init(s)))
1644*0a6a1f1dSLionel Sambuc */
1645ebfedea0SLionel Sambuc
1646ebfedea0SLionel Sambuc /* there's only one epoch between handshake and app data */
1647ebfedea0SLionel Sambuc
1648ebfedea0SLionel Sambuc s2n(s->d1->w_epoch, pseq);
1649ebfedea0SLionel Sambuc
1650ebfedea0SLionel Sambuc /* XDTLS: ?? */
1651*0a6a1f1dSLionel Sambuc /*
1652*0a6a1f1dSLionel Sambuc * else s2n(s->d1->handshake_epoch, pseq);
1653*0a6a1f1dSLionel Sambuc */
1654ebfedea0SLionel Sambuc
1655ebfedea0SLionel Sambuc memcpy(pseq, &(s->s3->write_sequence[2]), 6);
1656ebfedea0SLionel Sambuc pseq += 6;
1657ebfedea0SLionel Sambuc s2n(wr->length, pseq);
1658ebfedea0SLionel Sambuc
1659*0a6a1f1dSLionel Sambuc /*
1660*0a6a1f1dSLionel Sambuc * we should now have wr->data pointing to the encrypted data, which is
1661*0a6a1f1dSLionel Sambuc * wr->length long
1662*0a6a1f1dSLionel Sambuc */
1663ebfedea0SLionel Sambuc wr->type = type; /* not needed but helps for debugging */
1664ebfedea0SLionel Sambuc wr->length += DTLS1_RT_HEADER_LENGTH;
1665ebfedea0SLionel Sambuc
1666ebfedea0SLionel Sambuc #if 0 /* this is now done at the message layer */
1667ebfedea0SLionel Sambuc /* buffer the record, making it easy to handle retransmits */
1668ebfedea0SLionel Sambuc if (type == SSL3_RT_HANDSHAKE || type == SSL3_RT_CHANGE_CIPHER_SPEC)
1669ebfedea0SLionel Sambuc dtls1_buffer_record(s, wr->data, wr->length,
1670ebfedea0SLionel Sambuc *((PQ_64BIT *) & (s->s3->write_sequence[0])));
1671ebfedea0SLionel Sambuc #endif
1672ebfedea0SLionel Sambuc
1673ebfedea0SLionel Sambuc ssl3_record_sequence_update(&(s->s3->write_sequence[0]));
1674ebfedea0SLionel Sambuc
1675*0a6a1f1dSLionel Sambuc if (create_empty_fragment) {
1676*0a6a1f1dSLionel Sambuc /*
1677*0a6a1f1dSLionel Sambuc * we are in a recursive call; just return the length, don't write
1678*0a6a1f1dSLionel Sambuc * out anything here
1679ebfedea0SLionel Sambuc */
1680ebfedea0SLionel Sambuc return wr->length;
1681ebfedea0SLionel Sambuc }
1682ebfedea0SLionel Sambuc
1683ebfedea0SLionel Sambuc /* now let's set up wb */
1684ebfedea0SLionel Sambuc wb->left = prefix_len + wr->length;
1685ebfedea0SLionel Sambuc wb->offset = 0;
1686ebfedea0SLionel Sambuc
1687*0a6a1f1dSLionel Sambuc /*
1688*0a6a1f1dSLionel Sambuc * memorize arguments so that ssl3_write_pending can detect bad write
1689*0a6a1f1dSLionel Sambuc * retries later
1690*0a6a1f1dSLionel Sambuc */
1691ebfedea0SLionel Sambuc s->s3->wpend_tot = len;
1692ebfedea0SLionel Sambuc s->s3->wpend_buf = buf;
1693ebfedea0SLionel Sambuc s->s3->wpend_type = type;
1694ebfedea0SLionel Sambuc s->s3->wpend_ret = len;
1695ebfedea0SLionel Sambuc
1696ebfedea0SLionel Sambuc /* we now just need to write the buffer */
1697ebfedea0SLionel Sambuc return ssl3_write_pending(s, type, buf, len);
1698ebfedea0SLionel Sambuc err:
1699ebfedea0SLionel Sambuc return -1;
1700ebfedea0SLionel Sambuc }
1701ebfedea0SLionel Sambuc
dtls1_record_replay_check(SSL * s,DTLS1_BITMAP * bitmap)1702ebfedea0SLionel Sambuc static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap)
1703ebfedea0SLionel Sambuc {
1704ebfedea0SLionel Sambuc int cmp;
1705ebfedea0SLionel Sambuc unsigned int shift;
1706ebfedea0SLionel Sambuc const unsigned char *seq = s->s3->read_sequence;
1707ebfedea0SLionel Sambuc
1708ebfedea0SLionel Sambuc cmp = satsub64be(seq, bitmap->max_seq_num);
1709*0a6a1f1dSLionel Sambuc if (cmp > 0) {
1710ebfedea0SLionel Sambuc memcpy(s->s3->rrec.seq_num, seq, 8);
1711ebfedea0SLionel Sambuc return 1; /* this record in new */
1712ebfedea0SLionel Sambuc }
1713ebfedea0SLionel Sambuc shift = -cmp;
1714ebfedea0SLionel Sambuc if (shift >= sizeof(bitmap->map) * 8)
1715ebfedea0SLionel Sambuc return 0; /* stale, outside the window */
1716ebfedea0SLionel Sambuc else if (bitmap->map & (1UL << shift))
1717ebfedea0SLionel Sambuc return 0; /* record previously received */
1718ebfedea0SLionel Sambuc
1719ebfedea0SLionel Sambuc memcpy(s->s3->rrec.seq_num, seq, 8);
1720ebfedea0SLionel Sambuc return 1;
1721ebfedea0SLionel Sambuc }
1722ebfedea0SLionel Sambuc
dtls1_record_bitmap_update(SSL * s,DTLS1_BITMAP * bitmap)1723ebfedea0SLionel Sambuc static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap)
1724ebfedea0SLionel Sambuc {
1725ebfedea0SLionel Sambuc int cmp;
1726ebfedea0SLionel Sambuc unsigned int shift;
1727ebfedea0SLionel Sambuc const unsigned char *seq = s->s3->read_sequence;
1728ebfedea0SLionel Sambuc
1729ebfedea0SLionel Sambuc cmp = satsub64be(seq, bitmap->max_seq_num);
1730*0a6a1f1dSLionel Sambuc if (cmp > 0) {
1731ebfedea0SLionel Sambuc shift = cmp;
1732ebfedea0SLionel Sambuc if (shift < sizeof(bitmap->map) * 8)
1733ebfedea0SLionel Sambuc bitmap->map <<= shift, bitmap->map |= 1UL;
1734ebfedea0SLionel Sambuc else
1735ebfedea0SLionel Sambuc bitmap->map = 1UL;
1736ebfedea0SLionel Sambuc memcpy(bitmap->max_seq_num, seq, 8);
1737*0a6a1f1dSLionel Sambuc } else {
1738ebfedea0SLionel Sambuc shift = -cmp;
1739ebfedea0SLionel Sambuc if (shift < sizeof(bitmap->map) * 8)
1740ebfedea0SLionel Sambuc bitmap->map |= 1UL << shift;
1741ebfedea0SLionel Sambuc }
1742ebfedea0SLionel Sambuc }
1743ebfedea0SLionel Sambuc
dtls1_dispatch_alert(SSL * s)1744ebfedea0SLionel Sambuc int dtls1_dispatch_alert(SSL *s)
1745ebfedea0SLionel Sambuc {
1746ebfedea0SLionel Sambuc int i, j;
1747ebfedea0SLionel Sambuc void (*cb) (const SSL *ssl, int type, int val) = NULL;
1748ebfedea0SLionel Sambuc unsigned char buf[DTLS1_AL_HEADER_LENGTH];
1749ebfedea0SLionel Sambuc unsigned char *ptr = &buf[0];
1750ebfedea0SLionel Sambuc
1751ebfedea0SLionel Sambuc s->s3->alert_dispatch = 0;
1752ebfedea0SLionel Sambuc
1753ebfedea0SLionel Sambuc memset(buf, 0x00, sizeof(buf));
1754ebfedea0SLionel Sambuc *ptr++ = s->s3->send_alert[0];
1755ebfedea0SLionel Sambuc *ptr++ = s->s3->send_alert[1];
1756ebfedea0SLionel Sambuc
1757ebfedea0SLionel Sambuc #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1758*0a6a1f1dSLionel Sambuc if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
1759ebfedea0SLionel Sambuc s2n(s->d1->handshake_read_seq, ptr);
1760ebfedea0SLionel Sambuc # if 0
1761*0a6a1f1dSLionel Sambuc if (s->d1->r_msg_hdr.frag_off == 0)
1762*0a6a1f1dSLionel Sambuc /*
1763*0a6a1f1dSLionel Sambuc * waiting for a new msg
1764*0a6a1f1dSLionel Sambuc */
1765ebfedea0SLionel Sambuc else
1766ebfedea0SLionel Sambuc s2n(s->d1->r_msg_hdr.seq, ptr); /* partial msg read */
1767ebfedea0SLionel Sambuc # endif
1768ebfedea0SLionel Sambuc
1769ebfedea0SLionel Sambuc # if 0
1770*0a6a1f1dSLionel Sambuc fprintf(stderr,
1771*0a6a1f1dSLionel Sambuc "s->d1->handshake_read_seq = %d, s->d1->r_msg_hdr.seq = %d\n",
1772*0a6a1f1dSLionel Sambuc s->d1->handshake_read_seq, s->d1->r_msg_hdr.seq);
1773ebfedea0SLionel Sambuc # endif
1774ebfedea0SLionel Sambuc l2n3(s->d1->r_msg_hdr.frag_off, ptr);
1775ebfedea0SLionel Sambuc }
1776ebfedea0SLionel Sambuc #endif
1777ebfedea0SLionel Sambuc
1778ebfedea0SLionel Sambuc i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0);
1779*0a6a1f1dSLionel Sambuc if (i <= 0) {
1780ebfedea0SLionel Sambuc s->s3->alert_dispatch = 1;
1781ebfedea0SLionel Sambuc /* fprintf( stderr, "not done with alert\n" ); */
1782*0a6a1f1dSLionel Sambuc } else {
1783ebfedea0SLionel Sambuc if (s->s3->send_alert[0] == SSL3_AL_FATAL
1784ebfedea0SLionel Sambuc #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1785ebfedea0SLionel Sambuc || s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
1786ebfedea0SLionel Sambuc #endif
1787ebfedea0SLionel Sambuc )
1788ebfedea0SLionel Sambuc (void)BIO_flush(s->wbio);
1789ebfedea0SLionel Sambuc
1790ebfedea0SLionel Sambuc if (s->msg_callback)
1791ebfedea0SLionel Sambuc s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
1792ebfedea0SLionel Sambuc 2, s, s->msg_callback_arg);
1793ebfedea0SLionel Sambuc
1794ebfedea0SLionel Sambuc if (s->info_callback != NULL)
1795ebfedea0SLionel Sambuc cb = s->info_callback;
1796ebfedea0SLionel Sambuc else if (s->ctx->info_callback != NULL)
1797ebfedea0SLionel Sambuc cb = s->ctx->info_callback;
1798ebfedea0SLionel Sambuc
1799*0a6a1f1dSLionel Sambuc if (cb != NULL) {
1800ebfedea0SLionel Sambuc j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1801ebfedea0SLionel Sambuc cb(s, SSL_CB_WRITE_ALERT, j);
1802ebfedea0SLionel Sambuc }
1803ebfedea0SLionel Sambuc }
1804ebfedea0SLionel Sambuc return (i);
1805ebfedea0SLionel Sambuc }
1806ebfedea0SLionel Sambuc
dtls1_get_bitmap(SSL * s,SSL3_RECORD * rr,unsigned int * is_next_epoch)1807*0a6a1f1dSLionel Sambuc static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
1808*0a6a1f1dSLionel Sambuc unsigned int *is_next_epoch)
1809ebfedea0SLionel Sambuc {
1810ebfedea0SLionel Sambuc
1811ebfedea0SLionel Sambuc *is_next_epoch = 0;
1812ebfedea0SLionel Sambuc
1813ebfedea0SLionel Sambuc /* In current epoch, accept HM, CCS, DATA, & ALERT */
1814ebfedea0SLionel Sambuc if (rr->epoch == s->d1->r_epoch)
1815ebfedea0SLionel Sambuc return &s->d1->bitmap;
1816ebfedea0SLionel Sambuc
1817ebfedea0SLionel Sambuc /* Only HM and ALERT messages can be from the next epoch */
1818ebfedea0SLionel Sambuc else if (rr->epoch == (unsigned long)(s->d1->r_epoch + 1) &&
1819*0a6a1f1dSLionel Sambuc (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
1820ebfedea0SLionel Sambuc *is_next_epoch = 1;
1821ebfedea0SLionel Sambuc return &s->d1->next_bitmap;
1822ebfedea0SLionel Sambuc }
1823ebfedea0SLionel Sambuc
1824ebfedea0SLionel Sambuc return NULL;
1825ebfedea0SLionel Sambuc }
1826ebfedea0SLionel Sambuc
1827ebfedea0SLionel Sambuc #if 0
1828ebfedea0SLionel Sambuc static int
1829*0a6a1f1dSLionel Sambuc dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr,
1830*0a6a1f1dSLionel Sambuc unsigned short *priority, unsigned long *offset)
1831ebfedea0SLionel Sambuc {
1832ebfedea0SLionel Sambuc
1833ebfedea0SLionel Sambuc /* alerts are passed up immediately */
1834*0a6a1f1dSLionel Sambuc if (rr->type == SSL3_RT_APPLICATION_DATA || rr->type == SSL3_RT_ALERT)
1835ebfedea0SLionel Sambuc return 0;
1836ebfedea0SLionel Sambuc
1837*0a6a1f1dSLionel Sambuc /*
1838*0a6a1f1dSLionel Sambuc * Only need to buffer if a handshake is underway. (this implies that
1839*0a6a1f1dSLionel Sambuc * Hello Request and Client Hello are passed up immediately)
1840*0a6a1f1dSLionel Sambuc */
1841*0a6a1f1dSLionel Sambuc if (SSL_in_init(s)) {
1842ebfedea0SLionel Sambuc unsigned char *data = rr->data;
1843ebfedea0SLionel Sambuc /* need to extract the HM/CCS sequence number here */
1844ebfedea0SLionel Sambuc if (rr->type == SSL3_RT_HANDSHAKE ||
1845*0a6a1f1dSLionel Sambuc rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1846ebfedea0SLionel Sambuc unsigned short seq_num;
1847ebfedea0SLionel Sambuc struct hm_header_st msg_hdr;
1848ebfedea0SLionel Sambuc struct ccs_header_st ccs_hdr;
1849ebfedea0SLionel Sambuc
1850*0a6a1f1dSLionel Sambuc if (rr->type == SSL3_RT_HANDSHAKE) {
1851ebfedea0SLionel Sambuc dtls1_get_message_header(data, &msg_hdr);
1852ebfedea0SLionel Sambuc seq_num = msg_hdr.seq;
1853ebfedea0SLionel Sambuc *offset = msg_hdr.frag_off;
1854*0a6a1f1dSLionel Sambuc } else {
1855ebfedea0SLionel Sambuc dtls1_get_ccs_header(data, &ccs_hdr);
1856ebfedea0SLionel Sambuc seq_num = ccs_hdr.seq;
1857ebfedea0SLionel Sambuc *offset = 0;
1858ebfedea0SLionel Sambuc }
1859ebfedea0SLionel Sambuc
1860*0a6a1f1dSLionel Sambuc /*
1861*0a6a1f1dSLionel Sambuc * this is either a record we're waiting for, or a retransmit of
1862*0a6a1f1dSLionel Sambuc * something we happened to previously receive (higher layers
1863*0a6a1f1dSLionel Sambuc * will drop the repeat silently
1864*0a6a1f1dSLionel Sambuc */
1865ebfedea0SLionel Sambuc if (seq_num < s->d1->handshake_read_seq)
1866ebfedea0SLionel Sambuc return 0;
1867ebfedea0SLionel Sambuc if (rr->type == SSL3_RT_HANDSHAKE &&
1868ebfedea0SLionel Sambuc seq_num == s->d1->handshake_read_seq &&
1869ebfedea0SLionel Sambuc msg_hdr.frag_off < s->d1->r_msg_hdr.frag_off)
1870ebfedea0SLionel Sambuc return 0;
1871ebfedea0SLionel Sambuc else if (seq_num == s->d1->handshake_read_seq &&
1872ebfedea0SLionel Sambuc (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC ||
1873ebfedea0SLionel Sambuc msg_hdr.frag_off == s->d1->r_msg_hdr.frag_off))
1874ebfedea0SLionel Sambuc return 0;
1875*0a6a1f1dSLionel Sambuc else {
1876ebfedea0SLionel Sambuc *priority = seq_num;
1877ebfedea0SLionel Sambuc return 1;
1878ebfedea0SLionel Sambuc }
1879*0a6a1f1dSLionel Sambuc } else /* unknown record type */
1880ebfedea0SLionel Sambuc return 0;
1881ebfedea0SLionel Sambuc }
1882ebfedea0SLionel Sambuc
1883ebfedea0SLionel Sambuc return 0;
1884ebfedea0SLionel Sambuc }
1885ebfedea0SLionel Sambuc #endif
1886ebfedea0SLionel Sambuc
dtls1_reset_seq_numbers(SSL * s,int rw)1887*0a6a1f1dSLionel Sambuc void dtls1_reset_seq_numbers(SSL *s, int rw)
1888ebfedea0SLionel Sambuc {
1889ebfedea0SLionel Sambuc unsigned char *seq;
1890ebfedea0SLionel Sambuc unsigned int seq_bytes = sizeof(s->s3->read_sequence);
1891ebfedea0SLionel Sambuc
1892*0a6a1f1dSLionel Sambuc if (rw & SSL3_CC_READ) {
1893ebfedea0SLionel Sambuc seq = s->s3->read_sequence;
1894ebfedea0SLionel Sambuc s->d1->r_epoch++;
1895ebfedea0SLionel Sambuc memcpy(&(s->d1->bitmap), &(s->d1->next_bitmap), sizeof(DTLS1_BITMAP));
1896ebfedea0SLionel Sambuc memset(&(s->d1->next_bitmap), 0x00, sizeof(DTLS1_BITMAP));
1897*0a6a1f1dSLionel Sambuc } else {
1898ebfedea0SLionel Sambuc seq = s->s3->write_sequence;
1899*0a6a1f1dSLionel Sambuc memcpy(s->d1->last_write_sequence, seq,
1900*0a6a1f1dSLionel Sambuc sizeof(s->s3->write_sequence));
1901ebfedea0SLionel Sambuc s->d1->w_epoch++;
1902ebfedea0SLionel Sambuc }
1903ebfedea0SLionel Sambuc
1904ebfedea0SLionel Sambuc memset(seq, 0x00, seq_bytes);
1905ebfedea0SLionel Sambuc }
1906