12139Sjp161948 /* ssl/d1_both.c */
22139Sjp161948 /*
32139Sjp161948 * DTLS implementation written by Nagendra Modadugu
42139Sjp161948 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
52139Sjp161948 */
62139Sjp161948 /* ====================================================================
72139Sjp161948 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
82139Sjp161948 *
92139Sjp161948 * Redistribution and use in source and binary forms, with or without
102139Sjp161948 * modification, are permitted provided that the following conditions
112139Sjp161948 * are met:
122139Sjp161948 *
132139Sjp161948 * 1. Redistributions of source code must retain the above copyright
142139Sjp161948 * notice, this list of conditions and the following disclaimer.
152139Sjp161948 *
162139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
172139Sjp161948 * notice, this list of conditions and the following disclaimer in
182139Sjp161948 * the documentation and/or other materials provided with the
192139Sjp161948 * distribution.
202139Sjp161948 *
212139Sjp161948 * 3. All advertising materials mentioning features or use of this
222139Sjp161948 * software must display the following acknowledgment:
232139Sjp161948 * "This product includes software developed by the OpenSSL Project
242139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
252139Sjp161948 *
262139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
272139Sjp161948 * endorse or promote products derived from this software without
282139Sjp161948 * prior written permission. For written permission, please contact
292139Sjp161948 * openssl-core@openssl.org.
302139Sjp161948 *
312139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL"
322139Sjp161948 * nor may "OpenSSL" appear in their names without prior written
332139Sjp161948 * permission of the OpenSSL Project.
342139Sjp161948 *
352139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following
362139Sjp161948 * acknowledgment:
372139Sjp161948 * "This product includes software developed by the OpenSSL Project
382139Sjp161948 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
392139Sjp161948 *
402139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
412139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
422139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
432139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
442139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
452139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
462139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
472139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
482139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
492139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
502139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
512139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE.
522139Sjp161948 * ====================================================================
532139Sjp161948 *
542139Sjp161948 * This product includes cryptographic software written by Eric Young
552139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim
562139Sjp161948 * Hudson (tjh@cryptsoft.com).
572139Sjp161948 *
582139Sjp161948 */
592139Sjp161948 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
602139Sjp161948 * All rights reserved.
612139Sjp161948 *
622139Sjp161948 * This package is an SSL implementation written
632139Sjp161948 * by Eric Young (eay@cryptsoft.com).
642139Sjp161948 * The implementation was written so as to conform with Netscapes SSL.
652139Sjp161948 *
662139Sjp161948 * This library is free for commercial and non-commercial use as long as
672139Sjp161948 * the following conditions are aheared to. The following conditions
682139Sjp161948 * apply to all code found in this distribution, be it the RC4, RSA,
692139Sjp161948 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
702139Sjp161948 * included with this distribution is covered by the same copyright terms
712139Sjp161948 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
722139Sjp161948 *
732139Sjp161948 * Copyright remains Eric Young's, and as such any Copyright notices in
742139Sjp161948 * the code are not to be removed.
752139Sjp161948 * If this package is used in a product, Eric Young should be given attribution
762139Sjp161948 * as the author of the parts of the library used.
772139Sjp161948 * This can be in the form of a textual message at program startup or
782139Sjp161948 * in documentation (online or textual) provided with the package.
792139Sjp161948 *
802139Sjp161948 * Redistribution and use in source and binary forms, with or without
812139Sjp161948 * modification, are permitted provided that the following conditions
822139Sjp161948 * are met:
832139Sjp161948 * 1. Redistributions of source code must retain the copyright
842139Sjp161948 * notice, this list of conditions and the following disclaimer.
852139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
862139Sjp161948 * notice, this list of conditions and the following disclaimer in the
872139Sjp161948 * documentation and/or other materials provided with the distribution.
882139Sjp161948 * 3. All advertising materials mentioning features or use of this software
892139Sjp161948 * must display the following acknowledgement:
902139Sjp161948 * "This product includes cryptographic software written by
912139Sjp161948 * Eric Young (eay@cryptsoft.com)"
922139Sjp161948 * The word 'cryptographic' can be left out if the rouines from the library
932139Sjp161948 * being used are not cryptographic related :-).
942139Sjp161948 * 4. If you include any Windows specific code (or a derivative thereof) from
952139Sjp161948 * the apps directory (application code) you must include an acknowledgement:
962139Sjp161948 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
972139Sjp161948 *
982139Sjp161948 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
992139Sjp161948 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1002139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1012139Sjp161948 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1022139Sjp161948 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1032139Sjp161948 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1042139Sjp161948 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1052139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1062139Sjp161948 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1072139Sjp161948 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1082139Sjp161948 * SUCH DAMAGE.
1092139Sjp161948 *
1102139Sjp161948 * The licence and distribution terms for any publically available version or
1112139Sjp161948 * derivative of this code cannot be changed. i.e. this code cannot simply be
1122139Sjp161948 * copied and put under another distribution licence
1132139Sjp161948 * [including the GNU Public Licence.]
1142139Sjp161948 */
1152139Sjp161948
1162139Sjp161948 #include <limits.h>
1172139Sjp161948 #include <string.h>
1182139Sjp161948 #include <stdio.h>
1192139Sjp161948 #include "ssl_locl.h"
1202139Sjp161948 #include <openssl/buffer.h>
1212139Sjp161948 #include <openssl/rand.h>
1222139Sjp161948 #include <openssl/objects.h>
1232139Sjp161948 #include <openssl/evp.h>
1242139Sjp161948 #include <openssl/x509.h>
1252139Sjp161948
1262139Sjp161948
1272139Sjp161948 /* XDTLS: figure out the right values */
1282139Sjp161948 static unsigned int g_probable_mtu[] = {1500 - 28, 512 - 28, 256 - 28};
1292139Sjp161948
1302139Sjp161948 static unsigned int dtls1_min_mtu(void);
1312139Sjp161948 static unsigned int dtls1_guess_mtu(unsigned int curr_mtu);
1322139Sjp161948 static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
1332139Sjp161948 unsigned long frag_len);
1342139Sjp161948 static unsigned char *dtls1_write_message_header(SSL *s,
1352139Sjp161948 unsigned char *p);
1362139Sjp161948 static void dtls1_set_message_header_int(SSL *s, unsigned char mt,
1372139Sjp161948 unsigned long len, unsigned short seq_num, unsigned long frag_off,
1382139Sjp161948 unsigned long frag_len);
1392139Sjp161948 static int dtls1_retransmit_buffered_messages(SSL *s);
1402139Sjp161948 static long dtls1_get_message_fragment(SSL *s, int st1, int stn,
141*5434Sjp161948 long max, int *ok);
1422139Sjp161948
1432139Sjp161948 static hm_fragment *
dtls1_hm_fragment_new(unsigned long frag_len)1442139Sjp161948 dtls1_hm_fragment_new(unsigned long frag_len)
145*5434Sjp161948 {
146*5434Sjp161948 hm_fragment *frag = NULL;
147*5434Sjp161948 unsigned char *buf = NULL;
1482139Sjp161948
149*5434Sjp161948 frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
150*5434Sjp161948 if ( frag == NULL)
151*5434Sjp161948 return NULL;
1522139Sjp161948
153*5434Sjp161948 if (frag_len)
154*5434Sjp161948 {
155*5434Sjp161948 buf = (unsigned char *)OPENSSL_malloc(frag_len);
156*5434Sjp161948 if ( buf == NULL)
157*5434Sjp161948 {
158*5434Sjp161948 OPENSSL_free(frag);
159*5434Sjp161948 return NULL;
160*5434Sjp161948 }
161*5434Sjp161948 }
1622139Sjp161948
163*5434Sjp161948 /* zero length fragment gets zero frag->fragment */
164*5434Sjp161948 frag->fragment = buf;
165*5434Sjp161948
166*5434Sjp161948 return frag;
167*5434Sjp161948 }
1682139Sjp161948
1692139Sjp161948 static void
dtls1_hm_fragment_free(hm_fragment * frag)1702139Sjp161948 dtls1_hm_fragment_free(hm_fragment *frag)
171*5434Sjp161948 {
172*5434Sjp161948 if (frag->fragment) OPENSSL_free(frag->fragment);
173*5434Sjp161948 OPENSSL_free(frag);
174*5434Sjp161948 }
1752139Sjp161948
1762139Sjp161948 /* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */
dtls1_do_write(SSL * s,int type)1772139Sjp161948 int dtls1_do_write(SSL *s, int type)
1782139Sjp161948 {
1792139Sjp161948 int ret;
1802139Sjp161948 int curr_mtu;
1812139Sjp161948 unsigned int len, frag_off;
1822139Sjp161948
1832139Sjp161948 /* AHA! Figure out the MTU, and stick to the right size */
1842139Sjp161948 if ( ! (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU))
185*5434Sjp161948 {
1862139Sjp161948 s->d1->mtu =
1872139Sjp161948 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
1882139Sjp161948
1892139Sjp161948 /* I've seen the kernel return bogus numbers when it doesn't know
1902139Sjp161948 * (initial write), so just make sure we have a reasonable number */
1912139Sjp161948 if ( s->d1->mtu < dtls1_min_mtu())
1922139Sjp161948 {
1932139Sjp161948 s->d1->mtu = 0;
1942139Sjp161948 s->d1->mtu = dtls1_guess_mtu(s->d1->mtu);
1952139Sjp161948 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU,
1962139Sjp161948 s->d1->mtu, NULL);
1972139Sjp161948 }
1982139Sjp161948 }
1992139Sjp161948 #if 0
2002139Sjp161948 mtu = s->d1->mtu;
2012139Sjp161948
2022139Sjp161948 fprintf(stderr, "using MTU = %d\n", mtu);
2032139Sjp161948
2042139Sjp161948 mtu -= (DTLS1_HM_HEADER_LENGTH + DTLS1_RT_HEADER_LENGTH);
2052139Sjp161948
2062139Sjp161948 curr_mtu = mtu - BIO_wpending(SSL_get_wbio(s));
2072139Sjp161948
2082139Sjp161948 if ( curr_mtu > 0)
2092139Sjp161948 mtu = curr_mtu;
2102139Sjp161948 else if ( ( ret = BIO_flush(SSL_get_wbio(s))) <= 0)
2112139Sjp161948 return ret;
212*5434Sjp161948
2132139Sjp161948 if ( BIO_wpending(SSL_get_wbio(s)) + s->init_num >= mtu)
2142139Sjp161948 {
2152139Sjp161948 ret = BIO_flush(SSL_get_wbio(s));
2162139Sjp161948 if ( ret <= 0)
2172139Sjp161948 return ret;
2182139Sjp161948 mtu = s->d1->mtu - (DTLS1_HM_HEADER_LENGTH + DTLS1_RT_HEADER_LENGTH);
2192139Sjp161948 }
2202139Sjp161948
2212139Sjp161948 OPENSSL_assert(mtu > 0); /* should have something reasonable now */
2222139Sjp161948
2232139Sjp161948 #endif
2242139Sjp161948
2252139Sjp161948 if ( s->init_off == 0 && type == SSL3_RT_HANDSHAKE)
2262139Sjp161948 OPENSSL_assert(s->init_num ==
2272139Sjp161948 (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
2282139Sjp161948
2292139Sjp161948 frag_off = 0;
2302139Sjp161948 while( s->init_num)
2312139Sjp161948 {
2322139Sjp161948 curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
2332139Sjp161948 DTLS1_RT_HEADER_LENGTH;
2342139Sjp161948
2352139Sjp161948 if ( curr_mtu <= DTLS1_HM_HEADER_LENGTH)
2362139Sjp161948 {
2372139Sjp161948 /* grr.. we could get an error if MTU picked was wrong */
2382139Sjp161948 ret = BIO_flush(SSL_get_wbio(s));
2392139Sjp161948 if ( ret <= 0)
2402139Sjp161948 return ret;
2412139Sjp161948 curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH;
2422139Sjp161948 }
2432139Sjp161948
2442139Sjp161948 if ( s->init_num > curr_mtu)
2452139Sjp161948 len = curr_mtu;
2462139Sjp161948 else
2472139Sjp161948 len = s->init_num;
2482139Sjp161948
2492139Sjp161948
2502139Sjp161948 /* XDTLS: this function is too long. split out the CCS part */
2512139Sjp161948 if ( type == SSL3_RT_HANDSHAKE)
2522139Sjp161948 {
2532139Sjp161948 if ( s->init_off != 0)
2542139Sjp161948 {
2552139Sjp161948 OPENSSL_assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
2562139Sjp161948 s->init_off -= DTLS1_HM_HEADER_LENGTH;
2572139Sjp161948 s->init_num += DTLS1_HM_HEADER_LENGTH;
2582139Sjp161948
259*5434Sjp161948 /* write atleast DTLS1_HM_HEADER_LENGTH bytes */
2602139Sjp161948 if ( len <= DTLS1_HM_HEADER_LENGTH)
2612139Sjp161948 len += DTLS1_HM_HEADER_LENGTH;
2622139Sjp161948 }
263*5434Sjp161948
2642139Sjp161948 dtls1_fix_message_header(s, frag_off,
2652139Sjp161948 len - DTLS1_HM_HEADER_LENGTH);
2662139Sjp161948
2672139Sjp161948 dtls1_write_message_header(s, (unsigned char *)&s->init_buf->data[s->init_off]);
2682139Sjp161948
2692139Sjp161948 OPENSSL_assert(len >= DTLS1_HM_HEADER_LENGTH);
2702139Sjp161948 }
2712139Sjp161948
2722139Sjp161948 ret=dtls1_write_bytes(s,type,&s->init_buf->data[s->init_off],
2732139Sjp161948 len);
2742139Sjp161948 if (ret < 0)
2752139Sjp161948 {
2762139Sjp161948 /* might need to update MTU here, but we don't know
2772139Sjp161948 * which previous packet caused the failure -- so can't
2782139Sjp161948 * really retransmit anything. continue as if everything
2792139Sjp161948 * is fine and wait for an alert to handle the
2802139Sjp161948 * retransmit
2812139Sjp161948 */
2822139Sjp161948 if ( BIO_ctrl(SSL_get_wbio(s),
2832139Sjp161948 BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL))
2842139Sjp161948 s->d1->mtu = BIO_ctrl(SSL_get_wbio(s),
2852139Sjp161948 BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
2862139Sjp161948 else
2872139Sjp161948 return(-1);
2882139Sjp161948 }
2892139Sjp161948 else
2902139Sjp161948 {
291*5434Sjp161948
2922139Sjp161948 /* bad if this assert fails, only part of the handshake
2932139Sjp161948 * message got sent. but why would this happen? */
294*5434Sjp161948 OPENSSL_assert(len == (unsigned int)ret);
295*5434Sjp161948
2962139Sjp161948 if (type == SSL3_RT_HANDSHAKE && ! s->d1->retransmitting)
297*5434Sjp161948 {
2982139Sjp161948 /* should not be done for 'Hello Request's, but in that case
2992139Sjp161948 * we'll ignore the result anyway */
300*5434Sjp161948 unsigned char *p = (unsigned char *)&s->init_buf->data[s->init_off];
301*5434Sjp161948 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
302*5434Sjp161948 int xlen;
303*5434Sjp161948
304*5434Sjp161948 if (frag_off == 0 && s->client_version != DTLS1_BAD_VER)
305*5434Sjp161948 {
306*5434Sjp161948 /* reconstruct message header is if it
307*5434Sjp161948 * is being sent in single fragment */
308*5434Sjp161948 *p++ = msg_hdr->type;
309*5434Sjp161948 l2n3(msg_hdr->msg_len,p);
310*5434Sjp161948 s2n (msg_hdr->seq,p);
311*5434Sjp161948 l2n3(0,p);
312*5434Sjp161948 l2n3(msg_hdr->msg_len,p);
313*5434Sjp161948 p -= DTLS1_HM_HEADER_LENGTH;
314*5434Sjp161948 xlen = ret;
315*5434Sjp161948 }
316*5434Sjp161948 else
317*5434Sjp161948 {
318*5434Sjp161948 p += DTLS1_HM_HEADER_LENGTH;
319*5434Sjp161948 xlen = ret - DTLS1_HM_HEADER_LENGTH;
320*5434Sjp161948 }
321*5434Sjp161948
322*5434Sjp161948 ssl3_finish_mac(s, p, xlen);
323*5434Sjp161948 }
324*5434Sjp161948
3252139Sjp161948 if (ret == s->init_num)
3262139Sjp161948 {
3272139Sjp161948 if (s->msg_callback)
3282139Sjp161948 s->msg_callback(1, s->version, type, s->init_buf->data,
3292139Sjp161948 (size_t)(s->init_off + s->init_num), s,
3302139Sjp161948 s->msg_callback_arg);
3312139Sjp161948
3322139Sjp161948 s->init_off = 0; /* done writing this message */
3332139Sjp161948 s->init_num = 0;
334*5434Sjp161948
3352139Sjp161948 return(1);
3362139Sjp161948 }
3372139Sjp161948 s->init_off+=ret;
3382139Sjp161948 s->init_num-=ret;
3392139Sjp161948 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
3402139Sjp161948 }
3412139Sjp161948 }
3422139Sjp161948 return(0);
3432139Sjp161948 }
3442139Sjp161948
3452139Sjp161948
3462139Sjp161948 /* Obtain handshake message of message type 'mt' (any if mt == -1),
3472139Sjp161948 * maximum acceptable body length 'max'.
3482139Sjp161948 * Read an entire handshake message. Handshake messages arrive in
3492139Sjp161948 * fragments.
3502139Sjp161948 */
dtls1_get_message(SSL * s,int st1,int stn,int mt,long max,int * ok)3512139Sjp161948 long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
3522139Sjp161948 {
3532139Sjp161948 int i, al;
354*5434Sjp161948 struct hm_header_st *msg_hdr;
3552139Sjp161948
3562139Sjp161948 /* s3->tmp is used to store messages that are unexpected, caused
3572139Sjp161948 * by the absence of an optional handshake message */
3582139Sjp161948 if (s->s3->tmp.reuse_message)
3592139Sjp161948 {
3602139Sjp161948 s->s3->tmp.reuse_message=0;
3612139Sjp161948 if ((mt >= 0) && (s->s3->tmp.message_type != mt))
3622139Sjp161948 {
3632139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE;
3642139Sjp161948 SSLerr(SSL_F_DTLS1_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);
3652139Sjp161948 goto f_err;
3662139Sjp161948 }
3672139Sjp161948 *ok=1;
3682139Sjp161948 s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
3692139Sjp161948 s->init_num = (int)s->s3->tmp.message_size;
3702139Sjp161948 return s->init_num;
3712139Sjp161948 }
372*5434Sjp161948
373*5434Sjp161948 msg_hdr = &s->d1->r_msg_hdr;
3742139Sjp161948 do
3752139Sjp161948 {
376*5434Sjp161948 if ( msg_hdr->frag_off == 0)
3772139Sjp161948 {
3782139Sjp161948 /* s->d1->r_message_header.msg_len = 0; */
379*5434Sjp161948 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
3802139Sjp161948 }
3812139Sjp161948
3822139Sjp161948 i = dtls1_get_message_fragment(s, st1, stn, max, ok);
3832139Sjp161948 if ( i == DTLS1_HM_BAD_FRAGMENT ||
384*5434Sjp161948 i == DTLS1_HM_FRAGMENT_RETRY) /* bad fragment received */
3852139Sjp161948 continue;
3862139Sjp161948 else if ( i <= 0 && !*ok)
3872139Sjp161948 return i;
3882139Sjp161948
389*5434Sjp161948 /* Note that s->init_sum is used as a counter summing
390*5434Sjp161948 * up fragments' lengths: as soon as they sum up to
391*5434Sjp161948 * handshake packet length, we assume we have got all
392*5434Sjp161948 * the fragments. Overlapping fragments would cause
393*5434Sjp161948 * premature termination, so we don't expect overlaps.
394*5434Sjp161948 * Well, handling overlaps would require something more
395*5434Sjp161948 * drastic. Indeed, as it is now there is no way to
396*5434Sjp161948 * tell if out-of-order fragment from the middle was
397*5434Sjp161948 * the last. '>=' is the best/least we can do to control
398*5434Sjp161948 * the potential damage caused by malformed overlaps. */
399*5434Sjp161948 if ((unsigned int)s->init_num >= msg_hdr->msg_len)
4002139Sjp161948 {
401*5434Sjp161948 unsigned char *p = (unsigned char *)s->init_buf->data;
402*5434Sjp161948 unsigned long msg_len = msg_hdr->msg_len;
403*5434Sjp161948
404*5434Sjp161948 /* reconstruct message header as if it was
405*5434Sjp161948 * sent in single fragment */
406*5434Sjp161948 *(p++) = msg_hdr->type;
407*5434Sjp161948 l2n3(msg_len,p);
408*5434Sjp161948 s2n (msg_hdr->seq,p);
409*5434Sjp161948 l2n3(0,p);
410*5434Sjp161948 l2n3(msg_len,p);
411*5434Sjp161948 if (s->client_version != DTLS1_BAD_VER)
412*5434Sjp161948 p -= DTLS1_HM_HEADER_LENGTH,
413*5434Sjp161948 msg_len += DTLS1_HM_HEADER_LENGTH;
414*5434Sjp161948
415*5434Sjp161948 ssl3_finish_mac(s, p, msg_len);
416*5434Sjp161948 if (s->msg_callback)
417*5434Sjp161948 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
418*5434Sjp161948 p, msg_len,
419*5434Sjp161948 s, s->msg_callback_arg);
420*5434Sjp161948
421*5434Sjp161948 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
4222139Sjp161948
4232139Sjp161948 s->d1->handshake_read_seq++;
4242139Sjp161948 /* we just read a handshake message from the other side:
4252139Sjp161948 * this means that we don't need to retransmit of the
4262139Sjp161948 * buffered messages.
4272139Sjp161948 * XDTLS: may be able clear out this
4282139Sjp161948 * buffer a little sooner (i.e if an out-of-order
4292139Sjp161948 * handshake message/record is received at the record
4302139Sjp161948 * layer.
4312139Sjp161948 * XDTLS: exception is that the server needs to
4322139Sjp161948 * know that change cipher spec and finished messages
4332139Sjp161948 * have been received by the client before clearing this
4342139Sjp161948 * buffer. this can simply be done by waiting for the
4352139Sjp161948 * first data segment, but is there a better way? */
4362139Sjp161948 dtls1_clear_record_buffer(s);
4372139Sjp161948
438*5434Sjp161948 s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
439*5434Sjp161948 return s->init_num;
4402139Sjp161948 }
4412139Sjp161948 else
442*5434Sjp161948 msg_hdr->frag_off = i;
4432139Sjp161948 } while(1) ;
4442139Sjp161948
4452139Sjp161948 f_err:
4462139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al);
4472139Sjp161948 *ok = 0;
4482139Sjp161948 return -1;
4492139Sjp161948 }
4502139Sjp161948
4512139Sjp161948
dtls1_preprocess_fragment(SSL * s,struct hm_header_st * msg_hdr,int max)452*5434Sjp161948 static int dtls1_preprocess_fragment(SSL *s,struct hm_header_st *msg_hdr,int max)
453*5434Sjp161948 {
454*5434Sjp161948 size_t frag_off,frag_len,msg_len;
455*5434Sjp161948
456*5434Sjp161948 msg_len = msg_hdr->msg_len;
457*5434Sjp161948 frag_off = msg_hdr->frag_off;
458*5434Sjp161948 frag_len = msg_hdr->frag_len;
4592139Sjp161948
460*5434Sjp161948 /* sanity checking */
461*5434Sjp161948 if ( (frag_off+frag_len) > msg_len)
462*5434Sjp161948 {
463*5434Sjp161948 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,SSL_R_EXCESSIVE_MESSAGE_SIZE);
464*5434Sjp161948 return SSL_AD_ILLEGAL_PARAMETER;
465*5434Sjp161948 }
466*5434Sjp161948
467*5434Sjp161948 if ( (frag_off+frag_len) > (unsigned long)max)
468*5434Sjp161948 {
469*5434Sjp161948 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,SSL_R_EXCESSIVE_MESSAGE_SIZE);
470*5434Sjp161948 return SSL_AD_ILLEGAL_PARAMETER;
471*5434Sjp161948 }
4722139Sjp161948
473*5434Sjp161948 if ( s->d1->r_msg_hdr.frag_off == 0) /* first fragment */
474*5434Sjp161948 {
475*5434Sjp161948 /* msg_len is limited to 2^24, but is effectively checked
476*5434Sjp161948 * against max above */
477*5434Sjp161948 if (!BUF_MEM_grow_clean(s->init_buf,(int)msg_len+DTLS1_HM_HEADER_LENGTH))
478*5434Sjp161948 {
479*5434Sjp161948 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,ERR_R_BUF_LIB);
480*5434Sjp161948 return SSL_AD_INTERNAL_ERROR;
481*5434Sjp161948 }
4822139Sjp161948
483*5434Sjp161948 s->s3->tmp.message_size = msg_len;
484*5434Sjp161948 s->d1->r_msg_hdr.msg_len = msg_len;
485*5434Sjp161948 s->s3->tmp.message_type = msg_hdr->type;
486*5434Sjp161948 s->d1->r_msg_hdr.type = msg_hdr->type;
487*5434Sjp161948 s->d1->r_msg_hdr.seq = msg_hdr->seq;
488*5434Sjp161948 }
489*5434Sjp161948 else if (msg_len != s->d1->r_msg_hdr.msg_len)
490*5434Sjp161948 {
491*5434Sjp161948 /* They must be playing with us! BTW, failure to enforce
492*5434Sjp161948 * upper limit would open possibility for buffer overrun. */
493*5434Sjp161948 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,SSL_R_EXCESSIVE_MESSAGE_SIZE);
494*5434Sjp161948 return SSL_AD_ILLEGAL_PARAMETER;
495*5434Sjp161948 }
4962139Sjp161948
497*5434Sjp161948 return 0; /* no error */
498*5434Sjp161948 }
4992139Sjp161948
5002139Sjp161948
5012139Sjp161948 static int
dtls1_retrieve_buffered_fragment(SSL * s,long max,int * ok)502*5434Sjp161948 dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok)
503*5434Sjp161948 {
504*5434Sjp161948 /* (0) check whether the desired fragment is available
505*5434Sjp161948 * if so:
506*5434Sjp161948 * (1) copy over the fragment to s->init_buf->data[]
507*5434Sjp161948 * (2) update s->init_num
508*5434Sjp161948 */
509*5434Sjp161948 pitem *item;
510*5434Sjp161948 hm_fragment *frag;
511*5434Sjp161948 int al;
5122139Sjp161948
513*5434Sjp161948 *ok = 0;
514*5434Sjp161948 item = pqueue_peek(s->d1->buffered_messages);
515*5434Sjp161948 if ( item == NULL)
516*5434Sjp161948 return 0;
5172139Sjp161948
518*5434Sjp161948 frag = (hm_fragment *)item->data;
5192139Sjp161948
520*5434Sjp161948 if ( s->d1->handshake_read_seq == frag->msg_header.seq)
521*5434Sjp161948 {
522*5434Sjp161948 pqueue_pop(s->d1->buffered_messages);
523*5434Sjp161948
524*5434Sjp161948 al=dtls1_preprocess_fragment(s,&frag->msg_header,max);
5252139Sjp161948
526*5434Sjp161948 if (al==0) /* no alert */
527*5434Sjp161948 {
528*5434Sjp161948 unsigned char *p = (unsigned char *)s->init_buf->data+DTLS1_HM_HEADER_LENGTH;
529*5434Sjp161948 memcpy(&p[frag->msg_header.frag_off],
530*5434Sjp161948 frag->fragment,frag->msg_header.frag_len);
531*5434Sjp161948 }
5322139Sjp161948
533*5434Sjp161948 dtls1_hm_fragment_free(frag);
534*5434Sjp161948 pitem_free(item);
5352139Sjp161948
536*5434Sjp161948 if (al==0)
537*5434Sjp161948 {
538*5434Sjp161948 *ok = 1;
539*5434Sjp161948 return frag->msg_header.frag_len;
540*5434Sjp161948 }
5412139Sjp161948
542*5434Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al);
543*5434Sjp161948 s->init_num = 0;
544*5434Sjp161948 *ok = 0;
545*5434Sjp161948 return -1;
546*5434Sjp161948 }
547*5434Sjp161948 else
548*5434Sjp161948 return 0;
549*5434Sjp161948 }
5502139Sjp161948
5512139Sjp161948
5522139Sjp161948 static int
dtls1_process_out_of_seq_message(SSL * s,struct hm_header_st * msg_hdr,int * ok)553*5434Sjp161948 dtls1_process_out_of_seq_message(SSL *s, struct hm_header_st* msg_hdr, int *ok)
554*5434Sjp161948 {
555*5434Sjp161948 int i=-1;
556*5434Sjp161948 hm_fragment *frag = NULL;
557*5434Sjp161948 pitem *item = NULL;
558*5434Sjp161948 PQ_64BIT seq64;
559*5434Sjp161948 unsigned long frag_len = msg_hdr->frag_len;
5602139Sjp161948
561*5434Sjp161948 if ((msg_hdr->frag_off+frag_len) > msg_hdr->msg_len)
562*5434Sjp161948 goto err;
5632139Sjp161948
564*5434Sjp161948 if (msg_hdr->seq <= s->d1->handshake_read_seq)
5652139Sjp161948 {
566*5434Sjp161948 unsigned char devnull [256];
567*5434Sjp161948
568*5434Sjp161948 while (frag_len)
5692139Sjp161948 {
570*5434Sjp161948 i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
571*5434Sjp161948 devnull,
572*5434Sjp161948 frag_len>sizeof(devnull)?sizeof(devnull):frag_len,0);
573*5434Sjp161948 if (i<=0) goto err;
574*5434Sjp161948 frag_len -= i;
5752139Sjp161948 }
5762139Sjp161948 }
5772139Sjp161948
578*5434Sjp161948 frag = dtls1_hm_fragment_new(frag_len);
579*5434Sjp161948 if ( frag == NULL)
580*5434Sjp161948 goto err;
581*5434Sjp161948
582*5434Sjp161948 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
583*5434Sjp161948
584*5434Sjp161948 if (frag_len)
585*5434Sjp161948 {
586*5434Sjp161948 /* read the body of the fragment (header has already been read */
587*5434Sjp161948 i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
588*5434Sjp161948 frag->fragment,frag_len,0);
589*5434Sjp161948 if (i<=0 || (unsigned long)i!=frag_len)
590*5434Sjp161948 goto err;
591*5434Sjp161948 }
5922139Sjp161948
593*5434Sjp161948 pq_64bit_init(&seq64);
594*5434Sjp161948 pq_64bit_assign_word(&seq64, msg_hdr->seq);
595*5434Sjp161948
596*5434Sjp161948 item = pitem_new(seq64, frag);
597*5434Sjp161948 pq_64bit_free(&seq64);
598*5434Sjp161948 if ( item == NULL)
599*5434Sjp161948 goto err;
600*5434Sjp161948
601*5434Sjp161948 pqueue_insert(s->d1->buffered_messages, item);
602*5434Sjp161948 return DTLS1_HM_FRAGMENT_RETRY;
603*5434Sjp161948
6042139Sjp161948 err:
605*5434Sjp161948 if ( frag != NULL) dtls1_hm_fragment_free(frag);
606*5434Sjp161948 if ( item != NULL) OPENSSL_free(item);
607*5434Sjp161948 *ok = 0;
608*5434Sjp161948 return i;
609*5434Sjp161948 }
6102139Sjp161948
6112139Sjp161948
6122139Sjp161948 static long
dtls1_get_message_fragment(SSL * s,int st1,int stn,long max,int * ok)6132139Sjp161948 dtls1_get_message_fragment(SSL *s, int st1, int stn, long max, int *ok)
6142139Sjp161948 {
615*5434Sjp161948 unsigned char wire[DTLS1_HM_HEADER_LENGTH];
6162139Sjp161948 unsigned long l, frag_off, frag_len;
6172139Sjp161948 int i,al;
6182139Sjp161948 struct hm_header_st msg_hdr;
6192139Sjp161948
620*5434Sjp161948 /* see if we have the required fragment already */
621*5434Sjp161948 if ((frag_len = dtls1_retrieve_buffered_fragment(s,max,ok)) || *ok)
622*5434Sjp161948 {
623*5434Sjp161948 if (*ok) s->init_num += frag_len;
624*5434Sjp161948 return frag_len;
625*5434Sjp161948 }
6262139Sjp161948
627*5434Sjp161948 /* read handshake message header */
628*5434Sjp161948 i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,wire,
6292139Sjp161948 DTLS1_HM_HEADER_LENGTH, 0);
6302139Sjp161948 if (i <= 0) /* nbio, or an error */
6312139Sjp161948 {
6322139Sjp161948 s->rwstate=SSL_READING;
6332139Sjp161948 *ok = 0;
6342139Sjp161948 return i;
6352139Sjp161948 }
6362139Sjp161948 OPENSSL_assert(i == DTLS1_HM_HEADER_LENGTH);
6372139Sjp161948
638*5434Sjp161948 /* parse the message fragment header */
639*5434Sjp161948 dtls1_get_message_header(wire, &msg_hdr);
6402139Sjp161948
641*5434Sjp161948 /*
642*5434Sjp161948 * if this is a future (or stale) message it gets buffered
643*5434Sjp161948 * (or dropped)--no further processing at this time
644*5434Sjp161948 */
645*5434Sjp161948 if ( msg_hdr.seq != s->d1->handshake_read_seq)
646*5434Sjp161948 return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
6472139Sjp161948
648*5434Sjp161948 l = msg_hdr.msg_len;
649*5434Sjp161948 frag_off = msg_hdr.frag_off;
650*5434Sjp161948 frag_len = msg_hdr.frag_len;
6512139Sjp161948
6522139Sjp161948 if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
653*5434Sjp161948 wire[0] == SSL3_MT_HELLO_REQUEST)
6542139Sjp161948 {
655*5434Sjp161948 /* The server may always send 'Hello Request' messages --
656*5434Sjp161948 * we are doing a handshake anyway now, so ignore them
657*5434Sjp161948 * if their format is correct. Does not count for
658*5434Sjp161948 * 'Finished' MAC. */
659*5434Sjp161948 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0)
6602139Sjp161948 {
661*5434Sjp161948 if (s->msg_callback)
662*5434Sjp161948 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
663*5434Sjp161948 wire, DTLS1_HM_HEADER_LENGTH, s,
664*5434Sjp161948 s->msg_callback_arg);
665*5434Sjp161948
666*5434Sjp161948 s->init_num = 0;
667*5434Sjp161948 return dtls1_get_message_fragment(s, st1, stn,
668*5434Sjp161948 max, ok);
669*5434Sjp161948 }
670*5434Sjp161948 else /* Incorrectly formated Hello request */
671*5434Sjp161948 {
672*5434Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE;
673*5434Sjp161948 SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT,SSL_R_UNEXPECTED_MESSAGE);
6742139Sjp161948 goto f_err;
6752139Sjp161948 }
6762139Sjp161948 }
6772139Sjp161948
678*5434Sjp161948 if ((al=dtls1_preprocess_fragment(s,&msg_hdr,max)))
679*5434Sjp161948 goto f_err;
6802139Sjp161948
6812139Sjp161948 /* XDTLS: ressurect this when restart is in place */
6822139Sjp161948 s->state=stn;
6832139Sjp161948
6842139Sjp161948 if ( frag_len > 0)
6852139Sjp161948 {
686*5434Sjp161948 unsigned char *p=(unsigned char *)s->init_buf->data+DTLS1_HM_HEADER_LENGTH;
687*5434Sjp161948
6882139Sjp161948 i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
689*5434Sjp161948 &p[frag_off],frag_len,0);
690*5434Sjp161948 /* XDTLS: fix this--message fragments cannot span multiple packets */
6912139Sjp161948 if (i <= 0)
6922139Sjp161948 {
6932139Sjp161948 s->rwstate=SSL_READING;
6942139Sjp161948 *ok = 0;
6952139Sjp161948 return i;
6962139Sjp161948 }
6972139Sjp161948 }
6982139Sjp161948 else
6992139Sjp161948 i = 0;
7002139Sjp161948
701*5434Sjp161948 /* XDTLS: an incorrectly formatted fragment should cause the
702*5434Sjp161948 * handshake to fail */
7032139Sjp161948 OPENSSL_assert(i == (int)frag_len);
7042139Sjp161948
705*5434Sjp161948 *ok = 1;
7062139Sjp161948
707*5434Sjp161948 /* Note that s->init_num is *not* used as current offset in
708*5434Sjp161948 * s->init_buf->data, but as a counter summing up fragments'
709*5434Sjp161948 * lengths: as soon as they sum up to handshake packet
710*5434Sjp161948 * length, we assume we have got all the fragments. */
711*5434Sjp161948 s->init_num += frag_len;
712*5434Sjp161948 return frag_len;
7132139Sjp161948
7142139Sjp161948 f_err:
7152139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al);
716*5434Sjp161948 s->init_num = 0;
717*5434Sjp161948
7182139Sjp161948 *ok=0;
7192139Sjp161948 return(-1);
7202139Sjp161948 }
7212139Sjp161948
dtls1_send_finished(SSL * s,int a,int b,const char * sender,int slen)7222139Sjp161948 int dtls1_send_finished(SSL *s, int a, int b, const char *sender, int slen)
7232139Sjp161948 {
7242139Sjp161948 unsigned char *p,*d;
7252139Sjp161948 int i;
7262139Sjp161948 unsigned long l;
7272139Sjp161948
7282139Sjp161948 if (s->state == a)
7292139Sjp161948 {
7302139Sjp161948 d=(unsigned char *)s->init_buf->data;
7312139Sjp161948 p= &(d[DTLS1_HM_HEADER_LENGTH]);
7322139Sjp161948
7332139Sjp161948 i=s->method->ssl3_enc->final_finish_mac(s,
7342139Sjp161948 &(s->s3->finish_dgst1),
7352139Sjp161948 &(s->s3->finish_dgst2),
7362139Sjp161948 sender,slen,s->s3->tmp.finish_md);
7372139Sjp161948 s->s3->tmp.finish_md_len = i;
7382139Sjp161948 memcpy(p, s->s3->tmp.finish_md, i);
7392139Sjp161948 p+=i;
7402139Sjp161948 l=i;
7412139Sjp161948
7422139Sjp161948 #ifdef OPENSSL_SYS_WIN16
7432139Sjp161948 /* MSVC 1.5 does not clear the top bytes of the word unless
7442139Sjp161948 * I do this.
7452139Sjp161948 */
7462139Sjp161948 l&=0xffff;
7472139Sjp161948 #endif
7482139Sjp161948
7492139Sjp161948 d = dtls1_set_message_header(s, d, SSL3_MT_FINISHED, l, 0, l);
7502139Sjp161948 s->init_num=(int)l+DTLS1_HM_HEADER_LENGTH;
7512139Sjp161948 s->init_off=0;
7522139Sjp161948
7532139Sjp161948 /* buffer the message to handle re-xmits */
7542139Sjp161948 dtls1_buffer_message(s, 0);
755*5434Sjp161948
7562139Sjp161948 s->state=b;
7572139Sjp161948 }
7582139Sjp161948
7592139Sjp161948 /* SSL3_ST_SEND_xxxxxx_HELLO_B */
7602139Sjp161948 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
7612139Sjp161948 }
7622139Sjp161948
7632139Sjp161948 /* for these 2 messages, we need to
7642139Sjp161948 * ssl->enc_read_ctx re-init
7652139Sjp161948 * ssl->s3->read_sequence zero
7662139Sjp161948 * ssl->s3->read_mac_secret re-init
7672139Sjp161948 * ssl->session->read_sym_enc assign
7682139Sjp161948 * ssl->session->read_compression assign
7692139Sjp161948 * ssl->session->read_hash assign
7702139Sjp161948 */
dtls1_send_change_cipher_spec(SSL * s,int a,int b)7712139Sjp161948 int dtls1_send_change_cipher_spec(SSL *s, int a, int b)
7722139Sjp161948 {
7732139Sjp161948 unsigned char *p;
7742139Sjp161948
7752139Sjp161948 if (s->state == a)
7762139Sjp161948 {
7772139Sjp161948 p=(unsigned char *)s->init_buf->data;
7782139Sjp161948 *p++=SSL3_MT_CCS;
7792139Sjp161948 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
780*5434Sjp161948 s->init_num=DTLS1_CCS_HEADER_LENGTH;
7812139Sjp161948
782*5434Sjp161948 if (s->client_version == DTLS1_BAD_VER)
783*5434Sjp161948 {
784*5434Sjp161948 s->d1->next_handshake_write_seq++;
785*5434Sjp161948 s2n(s->d1->handshake_write_seq,p);
786*5434Sjp161948 s->init_num+=2;
787*5434Sjp161948 }
788*5434Sjp161948
7892139Sjp161948 s->init_off=0;
7902139Sjp161948
7912139Sjp161948 dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
7922139Sjp161948 s->d1->handshake_write_seq, 0, 0);
7932139Sjp161948
7942139Sjp161948 /* buffer the message to handle re-xmits */
7952139Sjp161948 dtls1_buffer_message(s, 1);
7962139Sjp161948
7972139Sjp161948 s->state=b;
7982139Sjp161948 }
7992139Sjp161948
8002139Sjp161948 /* SSL3_ST_CW_CHANGE_B */
8012139Sjp161948 return(dtls1_do_write(s,SSL3_RT_CHANGE_CIPHER_SPEC));
8022139Sjp161948 }
8032139Sjp161948
dtls1_output_cert_chain(SSL * s,X509 * x)8042139Sjp161948 unsigned long dtls1_output_cert_chain(SSL *s, X509 *x)
8052139Sjp161948 {
8062139Sjp161948 unsigned char *p;
8072139Sjp161948 int n,i;
8082139Sjp161948 unsigned long l= 3 + DTLS1_HM_HEADER_LENGTH;
8092139Sjp161948 BUF_MEM *buf;
8102139Sjp161948 X509_STORE_CTX xs_ctx;
8112139Sjp161948 X509_OBJECT obj;
8122139Sjp161948
8132139Sjp161948 /* TLSv1 sends a chain with nothing in it, instead of an alert */
8142139Sjp161948 buf=s->init_buf;
8152139Sjp161948 if (!BUF_MEM_grow_clean(buf,10))
8162139Sjp161948 {
8172139Sjp161948 SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
8182139Sjp161948 return(0);
8192139Sjp161948 }
8202139Sjp161948 if (x != NULL)
8212139Sjp161948 {
8222139Sjp161948 if(!X509_STORE_CTX_init(&xs_ctx,s->ctx->cert_store,NULL,NULL))
8232139Sjp161948 {
8242139Sjp161948 SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN,ERR_R_X509_LIB);
8252139Sjp161948 return(0);
8262139Sjp161948 }
8272139Sjp161948
8282139Sjp161948 for (;;)
8292139Sjp161948 {
8302139Sjp161948 n=i2d_X509(x,NULL);
8312139Sjp161948 if (!BUF_MEM_grow_clean(buf,(int)(n+l+3)))
8322139Sjp161948 {
8332139Sjp161948 SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
8342139Sjp161948 return(0);
8352139Sjp161948 }
8362139Sjp161948 p=(unsigned char *)&(buf->data[l]);
8372139Sjp161948 l2n3(n,p);
8382139Sjp161948 i2d_X509(x,&p);
8392139Sjp161948 l+=n+3;
8402139Sjp161948 if (X509_NAME_cmp(X509_get_subject_name(x),
8412139Sjp161948 X509_get_issuer_name(x)) == 0) break;
8422139Sjp161948
8432139Sjp161948 i=X509_STORE_get_by_subject(&xs_ctx,X509_LU_X509,
8442139Sjp161948 X509_get_issuer_name(x),&obj);
8452139Sjp161948 if (i <= 0) break;
8462139Sjp161948 x=obj.data.x509;
8472139Sjp161948 /* Count is one too high since the X509_STORE_get uped the
8482139Sjp161948 * ref count */
8492139Sjp161948 X509_free(x);
8502139Sjp161948 }
8512139Sjp161948
8522139Sjp161948 X509_STORE_CTX_cleanup(&xs_ctx);
8532139Sjp161948 }
8542139Sjp161948
8552139Sjp161948 /* Thawte special :-) */
8562139Sjp161948 if (s->ctx->extra_certs != NULL)
8572139Sjp161948 for (i=0; i<sk_X509_num(s->ctx->extra_certs); i++)
8582139Sjp161948 {
8592139Sjp161948 x=sk_X509_value(s->ctx->extra_certs,i);
8602139Sjp161948 n=i2d_X509(x,NULL);
8612139Sjp161948 if (!BUF_MEM_grow_clean(buf,(int)(n+l+3)))
8622139Sjp161948 {
8632139Sjp161948 SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
8642139Sjp161948 return(0);
8652139Sjp161948 }
8662139Sjp161948 p=(unsigned char *)&(buf->data[l]);
8672139Sjp161948 l2n3(n,p);
8682139Sjp161948 i2d_X509(x,&p);
8692139Sjp161948 l+=n+3;
8702139Sjp161948 }
8712139Sjp161948
8722139Sjp161948 l-= (3 + DTLS1_HM_HEADER_LENGTH);
8732139Sjp161948
8742139Sjp161948 p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
8752139Sjp161948 l2n3(l,p);
8762139Sjp161948 l+=3;
8772139Sjp161948 p=(unsigned char *)&(buf->data[0]);
8782139Sjp161948 p = dtls1_set_message_header(s, p, SSL3_MT_CERTIFICATE, l, 0, l);
8792139Sjp161948
8802139Sjp161948 l+=DTLS1_HM_HEADER_LENGTH;
8812139Sjp161948 return(l);
8822139Sjp161948 }
8832139Sjp161948
dtls1_read_failed(SSL * s,int code)8842139Sjp161948 int dtls1_read_failed(SSL *s, int code)
885*5434Sjp161948 {
886*5434Sjp161948 DTLS1_STATE *state;
887*5434Sjp161948 BIO *bio;
888*5434Sjp161948 int send_alert = 0;
8892139Sjp161948
890*5434Sjp161948 if ( code > 0)
891*5434Sjp161948 {
892*5434Sjp161948 fprintf( stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
893*5434Sjp161948 return 1;
894*5434Sjp161948 }
8952139Sjp161948
896*5434Sjp161948 bio = SSL_get_rbio(s);
897*5434Sjp161948 if ( ! BIO_dgram_recv_timedout(bio))
898*5434Sjp161948 {
899*5434Sjp161948 /* not a timeout, none of our business,
900*5434Sjp161948 let higher layers handle this. in fact it's probably an error */
901*5434Sjp161948 return code;
902*5434Sjp161948 }
9032139Sjp161948
904*5434Sjp161948 if ( ! SSL_in_init(s)) /* done, no need to send a retransmit */
905*5434Sjp161948 {
906*5434Sjp161948 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
907*5434Sjp161948 return code;
908*5434Sjp161948 }
9092139Sjp161948
910*5434Sjp161948 state = s->d1;
911*5434Sjp161948 state->timeout.num_alerts++;
912*5434Sjp161948 if ( state->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT)
913*5434Sjp161948 {
914*5434Sjp161948 /* fail the connection, enough alerts have been sent */
915*5434Sjp161948 SSLerr(SSL_F_DTLS1_READ_FAILED,SSL_R_READ_TIMEOUT_EXPIRED);
916*5434Sjp161948 return 0;
917*5434Sjp161948 }
9182139Sjp161948
919*5434Sjp161948 state->timeout.read_timeouts++;
920*5434Sjp161948 if ( state->timeout.read_timeouts > DTLS1_TMO_READ_COUNT)
921*5434Sjp161948 {
922*5434Sjp161948 send_alert = 1;
923*5434Sjp161948 state->timeout.read_timeouts = 1;
924*5434Sjp161948 }
925*5434Sjp161948
926*5434Sjp161948
9272139Sjp161948 #if 0 /* for now, each alert contains only one record number */
928*5434Sjp161948 item = pqueue_peek(state->rcvd_records);
929*5434Sjp161948 if ( item )
930*5434Sjp161948 {
931*5434Sjp161948 /* send an alert immediately for all the missing records */
932*5434Sjp161948 }
933*5434Sjp161948 else
9342139Sjp161948 #endif
9352139Sjp161948
9362139Sjp161948 #if 0 /* no more alert sending, just retransmit the last set of messages */
937*5434Sjp161948 if ( send_alert)
938*5434Sjp161948 ssl3_send_alert(s,SSL3_AL_WARNING,
939*5434Sjp161948 DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
9402139Sjp161948 #endif
9412139Sjp161948
942*5434Sjp161948 return dtls1_retransmit_buffered_messages(s) ;
943*5434Sjp161948 }
9442139Sjp161948
9452139Sjp161948
9462139Sjp161948 static int
dtls1_retransmit_buffered_messages(SSL * s)9472139Sjp161948 dtls1_retransmit_buffered_messages(SSL *s)
948*5434Sjp161948 {
949*5434Sjp161948 pqueue sent = s->d1->sent_messages;
950*5434Sjp161948 piterator iter;
951*5434Sjp161948 pitem *item;
952*5434Sjp161948 hm_fragment *frag;
953*5434Sjp161948 int found = 0;
9542139Sjp161948
955*5434Sjp161948 iter = pqueue_iterator(sent);
9562139Sjp161948
957*5434Sjp161948 for ( item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter))
958*5434Sjp161948 {
959*5434Sjp161948 frag = (hm_fragment *)item->data;
960*5434Sjp161948 if ( dtls1_retransmit_message(s, frag->msg_header.seq, 0, &found) <= 0 &&
961*5434Sjp161948 found)
962*5434Sjp161948 {
963*5434Sjp161948 fprintf(stderr, "dtls1_retransmit_message() failed\n");
964*5434Sjp161948 return -1;
965*5434Sjp161948 }
966*5434Sjp161948 }
9672139Sjp161948
968*5434Sjp161948 return 1;
969*5434Sjp161948 }
9702139Sjp161948
9712139Sjp161948 int
dtls1_buffer_message(SSL * s,int is_ccs)9722139Sjp161948 dtls1_buffer_message(SSL *s, int is_ccs)
973*5434Sjp161948 {
974*5434Sjp161948 pitem *item;
975*5434Sjp161948 hm_fragment *frag;
9762139Sjp161948 PQ_64BIT seq64;
977*5434Sjp161948 unsigned int epoch = s->d1->w_epoch;
9782139Sjp161948
979*5434Sjp161948 /* this function is called immediately after a message has
980*5434Sjp161948 * been serialized */
981*5434Sjp161948 OPENSSL_assert(s->init_off == 0);
9822139Sjp161948
983*5434Sjp161948 frag = dtls1_hm_fragment_new(s->init_num);
9842139Sjp161948
985*5434Sjp161948 memcpy(frag->fragment, s->init_buf->data, s->init_num);
9862139Sjp161948
987*5434Sjp161948 if ( is_ccs)
988*5434Sjp161948 {
989*5434Sjp161948 OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
990*5434Sjp161948 DTLS1_CCS_HEADER_LENGTH <= (unsigned int)s->init_num);
991*5434Sjp161948 epoch++;
992*5434Sjp161948 }
993*5434Sjp161948 else
994*5434Sjp161948 {
995*5434Sjp161948 OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
996*5434Sjp161948 DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);
997*5434Sjp161948 }
9982139Sjp161948
999*5434Sjp161948 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1000*5434Sjp161948 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1001*5434Sjp161948 frag->msg_header.type = s->d1->w_msg_hdr.type;
1002*5434Sjp161948 frag->msg_header.frag_off = 0;
1003*5434Sjp161948 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1004*5434Sjp161948 frag->msg_header.is_ccs = is_ccs;
10052139Sjp161948
1006*5434Sjp161948 pq_64bit_init(&seq64);
1007*5434Sjp161948 pq_64bit_assign_word(&seq64, epoch<<16 | frag->msg_header.seq);
10082139Sjp161948
1009*5434Sjp161948 item = pitem_new(seq64, frag);
1010*5434Sjp161948 pq_64bit_free(&seq64);
1011*5434Sjp161948 if ( item == NULL)
1012*5434Sjp161948 {
1013*5434Sjp161948 dtls1_hm_fragment_free(frag);
1014*5434Sjp161948 return 0;
1015*5434Sjp161948 }
10162139Sjp161948
10172139Sjp161948 #if 0
1018*5434Sjp161948 fprintf( stderr, "buffered messge: \ttype = %xx\n", msg_buf->type);
1019*5434Sjp161948 fprintf( stderr, "\t\t\t\t\tlen = %d\n", msg_buf->len);
1020*5434Sjp161948 fprintf( stderr, "\t\t\t\t\tseq_num = %d\n", msg_buf->seq_num);
10212139Sjp161948 #endif
10222139Sjp161948
1023*5434Sjp161948 pqueue_insert(s->d1->sent_messages, item);
1024*5434Sjp161948 return 1;
1025*5434Sjp161948 }
10262139Sjp161948
10272139Sjp161948 int
dtls1_retransmit_message(SSL * s,unsigned short seq,unsigned long frag_off,int * found)10282139Sjp161948 dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
1029*5434Sjp161948 int *found)
1030*5434Sjp161948 {
1031*5434Sjp161948 int ret;
1032*5434Sjp161948 /* XDTLS: for now assuming that read/writes are blocking */
1033*5434Sjp161948 pitem *item;
1034*5434Sjp161948 hm_fragment *frag ;
1035*5434Sjp161948 unsigned long header_length;
10362139Sjp161948 PQ_64BIT seq64;
10372139Sjp161948
1038*5434Sjp161948 /*
1039*5434Sjp161948 OPENSSL_assert(s->init_num == 0);
1040*5434Sjp161948 OPENSSL_assert(s->init_off == 0);
1041*5434Sjp161948 */
10422139Sjp161948
1043*5434Sjp161948 /* XDTLS: the requested message ought to be found, otherwise error */
1044*5434Sjp161948 pq_64bit_init(&seq64);
1045*5434Sjp161948 pq_64bit_assign_word(&seq64, seq);
10462139Sjp161948
1047*5434Sjp161948 item = pqueue_find(s->d1->sent_messages, seq64);
1048*5434Sjp161948 pq_64bit_free(&seq64);
1049*5434Sjp161948 if ( item == NULL)
1050*5434Sjp161948 {
1051*5434Sjp161948 fprintf(stderr, "retransmit: message %d non-existant\n", seq);
1052*5434Sjp161948 *found = 0;
1053*5434Sjp161948 return 0;
1054*5434Sjp161948 }
10552139Sjp161948
1056*5434Sjp161948 *found = 1;
1057*5434Sjp161948 frag = (hm_fragment *)item->data;
10582139Sjp161948
1059*5434Sjp161948 if ( frag->msg_header.is_ccs)
1060*5434Sjp161948 header_length = DTLS1_CCS_HEADER_LENGTH;
1061*5434Sjp161948 else
1062*5434Sjp161948 header_length = DTLS1_HM_HEADER_LENGTH;
1063*5434Sjp161948
1064*5434Sjp161948 memcpy(s->init_buf->data, frag->fragment,
1065*5434Sjp161948 frag->msg_header.msg_len + header_length);
1066*5434Sjp161948 s->init_num = frag->msg_header.msg_len + header_length;
10672139Sjp161948
1068*5434Sjp161948 dtls1_set_message_header_int(s, frag->msg_header.type,
1069*5434Sjp161948 frag->msg_header.msg_len, frag->msg_header.seq, 0,
1070*5434Sjp161948 frag->msg_header.frag_len);
10712139Sjp161948
1072*5434Sjp161948 s->d1->retransmitting = 1;
1073*5434Sjp161948 ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
1074*5434Sjp161948 SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
1075*5434Sjp161948 s->d1->retransmitting = 0;
10762139Sjp161948
1077*5434Sjp161948 (void)BIO_flush(SSL_get_wbio(s));
1078*5434Sjp161948 return ret;
1079*5434Sjp161948 }
10802139Sjp161948
10812139Sjp161948 /* call this function when the buffered messages are no longer needed */
10822139Sjp161948 void
dtls1_clear_record_buffer(SSL * s)10832139Sjp161948 dtls1_clear_record_buffer(SSL *s)
1084*5434Sjp161948 {
1085*5434Sjp161948 pitem *item;
1086*5434Sjp161948
1087*5434Sjp161948 for(item = pqueue_pop(s->d1->sent_messages);
1088*5434Sjp161948 item != NULL; item = pqueue_pop(s->d1->sent_messages))
1089*5434Sjp161948 {
1090*5434Sjp161948 dtls1_hm_fragment_free((hm_fragment *)item->data);
1091*5434Sjp161948 pitem_free(item);
1092*5434Sjp161948 }
1093*5434Sjp161948 }
10942139Sjp161948
10952139Sjp161948
10962139Sjp161948 unsigned char *
dtls1_set_message_header(SSL * s,unsigned char * p,unsigned char mt,unsigned long len,unsigned long frag_off,unsigned long frag_len)10972139Sjp161948 dtls1_set_message_header(SSL *s, unsigned char *p, unsigned char mt,
1098*5434Sjp161948 unsigned long len, unsigned long frag_off, unsigned long frag_len)
1099*5434Sjp161948 {
1100*5434Sjp161948 if ( frag_off == 0)
1101*5434Sjp161948 {
1102*5434Sjp161948 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1103*5434Sjp161948 s->d1->next_handshake_write_seq++;
1104*5434Sjp161948 }
1105*5434Sjp161948
1106*5434Sjp161948 dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
1107*5434Sjp161948 frag_off, frag_len);
1108*5434Sjp161948
1109*5434Sjp161948 return p += DTLS1_HM_HEADER_LENGTH;
1110*5434Sjp161948 }
11112139Sjp161948
11122139Sjp161948
11132139Sjp161948 /* don't actually do the writing, wait till the MTU has been retrieved */
11142139Sjp161948 static void
dtls1_set_message_header_int(SSL * s,unsigned char mt,unsigned long len,unsigned short seq_num,unsigned long frag_off,unsigned long frag_len)11152139Sjp161948 dtls1_set_message_header_int(SSL *s, unsigned char mt,
1116*5434Sjp161948 unsigned long len, unsigned short seq_num, unsigned long frag_off,
1117*5434Sjp161948 unsigned long frag_len)
1118*5434Sjp161948 {
1119*5434Sjp161948 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1120*5434Sjp161948
1121*5434Sjp161948 msg_hdr->type = mt;
1122*5434Sjp161948 msg_hdr->msg_len = len;
1123*5434Sjp161948 msg_hdr->seq = seq_num;
1124*5434Sjp161948 msg_hdr->frag_off = frag_off;
1125*5434Sjp161948 msg_hdr->frag_len = frag_len;
1126*5434Sjp161948 }
11272139Sjp161948
11282139Sjp161948 static void
dtls1_fix_message_header(SSL * s,unsigned long frag_off,unsigned long frag_len)11292139Sjp161948 dtls1_fix_message_header(SSL *s, unsigned long frag_off,
1130*5434Sjp161948 unsigned long frag_len)
1131*5434Sjp161948 {
1132*5434Sjp161948 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1133*5434Sjp161948
1134*5434Sjp161948 msg_hdr->frag_off = frag_off;
1135*5434Sjp161948 msg_hdr->frag_len = frag_len;
1136*5434Sjp161948 }
11372139Sjp161948
11382139Sjp161948 static unsigned char *
dtls1_write_message_header(SSL * s,unsigned char * p)11392139Sjp161948 dtls1_write_message_header(SSL *s, unsigned char *p)
1140*5434Sjp161948 {
1141*5434Sjp161948 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1142*5434Sjp161948
1143*5434Sjp161948 *p++ = msg_hdr->type;
1144*5434Sjp161948 l2n3(msg_hdr->msg_len, p);
1145*5434Sjp161948
1146*5434Sjp161948 s2n(msg_hdr->seq, p);
1147*5434Sjp161948 l2n3(msg_hdr->frag_off, p);
1148*5434Sjp161948 l2n3(msg_hdr->frag_len, p);
1149*5434Sjp161948
1150*5434Sjp161948 return p;
1151*5434Sjp161948 }
11522139Sjp161948
11532139Sjp161948 static unsigned int
dtls1_min_mtu(void)11542139Sjp161948 dtls1_min_mtu(void)
1155*5434Sjp161948 {
1156*5434Sjp161948 return (g_probable_mtu[(sizeof(g_probable_mtu) /
1157*5434Sjp161948 sizeof(g_probable_mtu[0])) - 1]);
1158*5434Sjp161948 }
11592139Sjp161948
11602139Sjp161948 static unsigned int
dtls1_guess_mtu(unsigned int curr_mtu)11612139Sjp161948 dtls1_guess_mtu(unsigned int curr_mtu)
11622139Sjp161948 {
11632139Sjp161948 size_t i;
11642139Sjp161948
11652139Sjp161948 if ( curr_mtu == 0 )
11662139Sjp161948 return g_probable_mtu[0] ;
11672139Sjp161948
11682139Sjp161948 for ( i = 0; i < sizeof(g_probable_mtu)/sizeof(g_probable_mtu[0]); i++)
11692139Sjp161948 if ( curr_mtu > g_probable_mtu[i])
11702139Sjp161948 return g_probable_mtu[i];
1171*5434Sjp161948
11722139Sjp161948 return curr_mtu;
11732139Sjp161948 }
11742139Sjp161948
11752139Sjp161948 void
dtls1_get_message_header(unsigned char * data,struct hm_header_st * msg_hdr)11762139Sjp161948 dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
1177*5434Sjp161948 {
1178*5434Sjp161948 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
1179*5434Sjp161948 msg_hdr->type = *(data++);
1180*5434Sjp161948 n2l3(data, msg_hdr->msg_len);
1181*5434Sjp161948
1182*5434Sjp161948 n2s(data, msg_hdr->seq);
1183*5434Sjp161948 n2l3(data, msg_hdr->frag_off);
1184*5434Sjp161948 n2l3(data, msg_hdr->frag_len);
1185*5434Sjp161948 }
11862139Sjp161948
11872139Sjp161948 void
dtls1_get_ccs_header(unsigned char * data,struct ccs_header_st * ccs_hdr)11882139Sjp161948 dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr)
1189*5434Sjp161948 {
1190*5434Sjp161948 memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
1191*5434Sjp161948
1192*5434Sjp161948 ccs_hdr->type = *(data++);
1193*5434Sjp161948 }
1194