xref: /minix3/crypto/external/bsd/openssl/dist/ssl/d1_both.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/d1_both.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 <limits.h>
117ebfedea0SLionel Sambuc #include <string.h>
118ebfedea0SLionel Sambuc #include <stdio.h>
119ebfedea0SLionel Sambuc #include "ssl_locl.h"
120ebfedea0SLionel Sambuc #include <openssl/buffer.h>
121ebfedea0SLionel Sambuc #include <openssl/rand.h>
122ebfedea0SLionel Sambuc #include <openssl/objects.h>
123ebfedea0SLionel Sambuc #include <openssl/evp.h>
124ebfedea0SLionel Sambuc #include <openssl/x509.h>
125ebfedea0SLionel Sambuc 
126ebfedea0SLionel Sambuc #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
129ebfedea0SLionel Sambuc                         if ((end) - (start) <= 8) { \
130ebfedea0SLionel Sambuc                                 long ii; \
131ebfedea0SLionel Sambuc                                 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
132ebfedea0SLionel Sambuc                         } else { \
133ebfedea0SLionel Sambuc                                 long ii; \
134ebfedea0SLionel Sambuc                                 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
135ebfedea0SLionel Sambuc                                 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
136ebfedea0SLionel Sambuc                                 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
137ebfedea0SLionel Sambuc                         } }
138ebfedea0SLionel Sambuc 
139ebfedea0SLionel Sambuc #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
140ebfedea0SLionel Sambuc                         long ii; \
141ebfedea0SLionel Sambuc                         OPENSSL_assert((msg_len) > 0); \
142ebfedea0SLionel Sambuc                         is_complete = 1; \
143ebfedea0SLionel Sambuc                         if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
144ebfedea0SLionel Sambuc                         if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
145ebfedea0SLionel Sambuc                                 if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
146ebfedea0SLionel Sambuc 
147ebfedea0SLionel Sambuc #if 0
148ebfedea0SLionel Sambuc # define RSMBLY_BITMASK_PRINT(bitmask, msg_len) { \
149ebfedea0SLionel Sambuc                         long ii; \
150ebfedea0SLionel Sambuc                         printf("bitmask: "); for (ii = 0; ii < (msg_len); ii++) \
151ebfedea0SLionel Sambuc                         printf("%d ", (bitmask[ii >> 3] & (1 << (ii & 7))) >> (ii & 7)); \
152ebfedea0SLionel Sambuc                         printf("\n"); }
153ebfedea0SLionel Sambuc #endif
154ebfedea0SLionel Sambuc 
155*0a6a1f1dSLionel Sambuc static unsigned char bitmask_start_values[] =
156*0a6a1f1dSLionel Sambuc     { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 };
157*0a6a1f1dSLionel Sambuc static unsigned char bitmask_end_values[] =
158*0a6a1f1dSLionel Sambuc     { 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f };
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc /* XDTLS:  figure out the right values */
161*0a6a1f1dSLionel Sambuc static const unsigned int g_probable_mtu[] = { 1500, 512, 256 };
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
164ebfedea0SLionel Sambuc                                      unsigned long frag_len);
165*0a6a1f1dSLionel Sambuc static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p);
166ebfedea0SLionel Sambuc static void dtls1_set_message_header_int(SSL *s, unsigned char mt,
167*0a6a1f1dSLionel Sambuc                                          unsigned long len,
168*0a6a1f1dSLionel Sambuc                                          unsigned short seq_num,
169*0a6a1f1dSLionel Sambuc                                          unsigned long frag_off,
170ebfedea0SLionel Sambuc                                          unsigned long frag_len);
171*0a6a1f1dSLionel Sambuc static long dtls1_get_message_fragment(SSL *s, int st1, int stn, long max,
172*0a6a1f1dSLionel Sambuc                                        int *ok);
173ebfedea0SLionel Sambuc 
dtls1_hm_fragment_new(unsigned long frag_len,int reassembly)174*0a6a1f1dSLionel Sambuc static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len,
175*0a6a1f1dSLionel Sambuc                                           int reassembly)
176ebfedea0SLionel Sambuc {
177ebfedea0SLionel Sambuc     hm_fragment *frag = NULL;
178ebfedea0SLionel Sambuc     unsigned char *buf = NULL;
179ebfedea0SLionel Sambuc     unsigned char *bitmask = NULL;
180ebfedea0SLionel Sambuc 
181ebfedea0SLionel Sambuc     frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
182ebfedea0SLionel Sambuc     if (frag == NULL)
183ebfedea0SLionel Sambuc         return NULL;
184ebfedea0SLionel Sambuc 
185*0a6a1f1dSLionel Sambuc     if (frag_len) {
186ebfedea0SLionel Sambuc         buf = (unsigned char *)OPENSSL_malloc(frag_len);
187*0a6a1f1dSLionel Sambuc         if (buf == NULL) {
188ebfedea0SLionel Sambuc             OPENSSL_free(frag);
189ebfedea0SLionel Sambuc             return NULL;
190ebfedea0SLionel Sambuc         }
191ebfedea0SLionel Sambuc     }
192ebfedea0SLionel Sambuc 
193ebfedea0SLionel Sambuc     /* zero length fragment gets zero frag->fragment */
194ebfedea0SLionel Sambuc     frag->fragment = buf;
195ebfedea0SLionel Sambuc 
196ebfedea0SLionel Sambuc     /* Initialize reassembly bitmask if necessary */
197*0a6a1f1dSLionel Sambuc     if (reassembly) {
198*0a6a1f1dSLionel Sambuc         bitmask =
199*0a6a1f1dSLionel Sambuc             (unsigned char *)OPENSSL_malloc(RSMBLY_BITMASK_SIZE(frag_len));
200*0a6a1f1dSLionel Sambuc         if (bitmask == NULL) {
201*0a6a1f1dSLionel Sambuc             if (buf != NULL)
202*0a6a1f1dSLionel Sambuc                 OPENSSL_free(buf);
203ebfedea0SLionel Sambuc             OPENSSL_free(frag);
204ebfedea0SLionel Sambuc             return NULL;
205ebfedea0SLionel Sambuc         }
206ebfedea0SLionel Sambuc         memset(bitmask, 0, RSMBLY_BITMASK_SIZE(frag_len));
207ebfedea0SLionel Sambuc     }
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc     frag->reassembly = bitmask;
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc     return frag;
212ebfedea0SLionel Sambuc }
213ebfedea0SLionel Sambuc 
dtls1_hm_fragment_free(hm_fragment * frag)214*0a6a1f1dSLionel Sambuc void dtls1_hm_fragment_free(hm_fragment *frag)
215ebfedea0SLionel Sambuc {
216*0a6a1f1dSLionel Sambuc 
217*0a6a1f1dSLionel Sambuc     if (frag->msg_header.is_ccs) {
218*0a6a1f1dSLionel Sambuc         EVP_CIPHER_CTX_free(frag->msg_header.
219*0a6a1f1dSLionel Sambuc                             saved_retransmit_state.enc_write_ctx);
220*0a6a1f1dSLionel Sambuc         EVP_MD_CTX_destroy(frag->msg_header.
221*0a6a1f1dSLionel Sambuc                            saved_retransmit_state.write_hash);
222*0a6a1f1dSLionel Sambuc     }
223*0a6a1f1dSLionel Sambuc     if (frag->fragment)
224*0a6a1f1dSLionel Sambuc         OPENSSL_free(frag->fragment);
225*0a6a1f1dSLionel Sambuc     if (frag->reassembly)
226*0a6a1f1dSLionel Sambuc         OPENSSL_free(frag->reassembly);
227ebfedea0SLionel Sambuc     OPENSSL_free(frag);
228ebfedea0SLionel Sambuc }
229ebfedea0SLionel Sambuc 
dtls1_query_mtu(SSL * s)230*0a6a1f1dSLionel Sambuc static int dtls1_query_mtu(SSL *s)
231ebfedea0SLionel Sambuc {
232*0a6a1f1dSLionel Sambuc     if (s->d1->link_mtu) {
233*0a6a1f1dSLionel Sambuc         s->d1->mtu =
234*0a6a1f1dSLionel Sambuc             s->d1->link_mtu - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s));
235*0a6a1f1dSLionel Sambuc         s->d1->link_mtu = 0;
236*0a6a1f1dSLionel Sambuc     }
237ebfedea0SLionel Sambuc 
238ebfedea0SLionel Sambuc     /* AHA!  Figure out the MTU, and stick to the right size */
239*0a6a1f1dSLionel Sambuc     if (s->d1->mtu < dtls1_min_mtu(s)) {
240*0a6a1f1dSLionel Sambuc         if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
241ebfedea0SLionel Sambuc             s->d1->mtu =
242ebfedea0SLionel Sambuc                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
243ebfedea0SLionel Sambuc 
244*0a6a1f1dSLionel Sambuc             /*
245*0a6a1f1dSLionel Sambuc              * I've seen the kernel return bogus numbers when it doesn't know
246*0a6a1f1dSLionel Sambuc              * (initial write), so just make sure we have a reasonable number
247*0a6a1f1dSLionel Sambuc              */
248*0a6a1f1dSLionel Sambuc             if (s->d1->mtu < dtls1_min_mtu(s)) {
249*0a6a1f1dSLionel Sambuc                 /* Set to min mtu */
250*0a6a1f1dSLionel Sambuc                 s->d1->mtu = dtls1_min_mtu(s);
251ebfedea0SLionel Sambuc                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU,
252ebfedea0SLionel Sambuc                          s->d1->mtu, NULL);
253ebfedea0SLionel Sambuc             }
254*0a6a1f1dSLionel Sambuc         } else
255*0a6a1f1dSLionel Sambuc             return 0;
256ebfedea0SLionel Sambuc     }
257*0a6a1f1dSLionel Sambuc     return 1;
258*0a6a1f1dSLionel Sambuc }
259ebfedea0SLionel Sambuc 
260*0a6a1f1dSLionel Sambuc /*
261*0a6a1f1dSLionel Sambuc  * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
262*0a6a1f1dSLionel Sambuc  * SSL3_RT_CHANGE_CIPHER_SPEC)
263*0a6a1f1dSLionel Sambuc  */
dtls1_do_write(SSL * s,int type)264*0a6a1f1dSLionel Sambuc int dtls1_do_write(SSL *s, int type)
265ebfedea0SLionel Sambuc {
266*0a6a1f1dSLionel Sambuc     int ret;
267*0a6a1f1dSLionel Sambuc     unsigned int curr_mtu;
268*0a6a1f1dSLionel Sambuc     int retry = 1;
269*0a6a1f1dSLionel Sambuc     unsigned int len, frag_off, mac_size, blocksize, used_len;
270ebfedea0SLionel Sambuc 
271*0a6a1f1dSLionel Sambuc     if (!dtls1_query_mtu(s))
272*0a6a1f1dSLionel Sambuc         return -1;
273*0a6a1f1dSLionel Sambuc 
274*0a6a1f1dSLionel Sambuc     OPENSSL_assert(s->d1->mtu >= dtls1_min_mtu(s)); /* should have something
275*0a6a1f1dSLionel Sambuc                                                      * reasonable now */
276ebfedea0SLionel Sambuc 
277ebfedea0SLionel Sambuc     if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE)
278ebfedea0SLionel Sambuc         OPENSSL_assert(s->init_num ==
279*0a6a1f1dSLionel Sambuc                        (int)s->d1->w_msg_hdr.msg_len +
280*0a6a1f1dSLionel Sambuc                        DTLS1_HM_HEADER_LENGTH);
281ebfedea0SLionel Sambuc 
282ebfedea0SLionel Sambuc     if (s->write_hash)
283ebfedea0SLionel Sambuc         mac_size = EVP_MD_CTX_size(s->write_hash);
284ebfedea0SLionel Sambuc     else
285ebfedea0SLionel Sambuc         mac_size = 0;
286ebfedea0SLionel Sambuc 
287ebfedea0SLionel Sambuc     if (s->enc_write_ctx &&
288ebfedea0SLionel Sambuc         (EVP_CIPHER_mode(s->enc_write_ctx->cipher) & EVP_CIPH_CBC_MODE))
289ebfedea0SLionel Sambuc         blocksize = 2 * EVP_CIPHER_block_size(s->enc_write_ctx->cipher);
290ebfedea0SLionel Sambuc     else
291ebfedea0SLionel Sambuc         blocksize = 0;
292ebfedea0SLionel Sambuc 
293ebfedea0SLionel Sambuc     frag_off = 0;
294*0a6a1f1dSLionel Sambuc     /* s->init_num shouldn't ever be < 0...but just in case */
295*0a6a1f1dSLionel Sambuc     while (s->init_num > 0) {
296*0a6a1f1dSLionel Sambuc         used_len = BIO_wpending(SSL_get_wbio(s)) + DTLS1_RT_HEADER_LENGTH
297*0a6a1f1dSLionel Sambuc             + mac_size + blocksize;
298*0a6a1f1dSLionel Sambuc         if (s->d1->mtu > used_len)
299*0a6a1f1dSLionel Sambuc             curr_mtu = s->d1->mtu - used_len;
300*0a6a1f1dSLionel Sambuc         else
301*0a6a1f1dSLionel Sambuc             curr_mtu = 0;
302ebfedea0SLionel Sambuc 
303*0a6a1f1dSLionel Sambuc         if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
304*0a6a1f1dSLionel Sambuc             /*
305*0a6a1f1dSLionel Sambuc              * grr.. we could get an error if MTU picked was wrong
306*0a6a1f1dSLionel Sambuc              */
307ebfedea0SLionel Sambuc             ret = BIO_flush(SSL_get_wbio(s));
308ebfedea0SLionel Sambuc             if (ret <= 0)
309ebfedea0SLionel Sambuc                 return ret;
310*0a6a1f1dSLionel Sambuc             used_len = DTLS1_RT_HEADER_LENGTH + mac_size + blocksize;
311*0a6a1f1dSLionel Sambuc             if (s->d1->mtu > used_len + DTLS1_HM_HEADER_LENGTH) {
312*0a6a1f1dSLionel Sambuc                 curr_mtu = s->d1->mtu - used_len;
313*0a6a1f1dSLionel Sambuc             } else {
314*0a6a1f1dSLionel Sambuc                 /* Shouldn't happen */
315*0a6a1f1dSLionel Sambuc                 return -1;
316*0a6a1f1dSLionel Sambuc             }
317ebfedea0SLionel Sambuc         }
318ebfedea0SLionel Sambuc 
319*0a6a1f1dSLionel Sambuc         /*
320*0a6a1f1dSLionel Sambuc          * We just checked that s->init_num > 0 so this cast should be safe
321*0a6a1f1dSLionel Sambuc          */
322*0a6a1f1dSLionel Sambuc         if (((unsigned int)s->init_num) > curr_mtu)
323ebfedea0SLionel Sambuc             len = curr_mtu;
324ebfedea0SLionel Sambuc         else
325ebfedea0SLionel Sambuc             len = s->init_num;
326ebfedea0SLionel Sambuc 
327*0a6a1f1dSLionel Sambuc         /* Shouldn't ever happen */
328*0a6a1f1dSLionel Sambuc         if (len > INT_MAX)
329*0a6a1f1dSLionel Sambuc             len = INT_MAX;
330ebfedea0SLionel Sambuc 
331*0a6a1f1dSLionel Sambuc         /*
332*0a6a1f1dSLionel Sambuc          * XDTLS: this function is too long.  split out the CCS part
333*0a6a1f1dSLionel Sambuc          */
334*0a6a1f1dSLionel Sambuc         if (type == SSL3_RT_HANDSHAKE) {
335*0a6a1f1dSLionel Sambuc             if (s->init_off != 0) {
336ebfedea0SLionel Sambuc                 OPENSSL_assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
337ebfedea0SLionel Sambuc                 s->init_off -= DTLS1_HM_HEADER_LENGTH;
338ebfedea0SLionel Sambuc                 s->init_num += DTLS1_HM_HEADER_LENGTH;
339ebfedea0SLionel Sambuc 
340*0a6a1f1dSLionel Sambuc                 /*
341*0a6a1f1dSLionel Sambuc                  * We just checked that s->init_num > 0 so this cast should
342*0a6a1f1dSLionel Sambuc                  * be safe
343*0a6a1f1dSLionel Sambuc                  */
344*0a6a1f1dSLionel Sambuc                 if (((unsigned int)s->init_num) > curr_mtu)
345*0a6a1f1dSLionel Sambuc                     len = curr_mtu;
346*0a6a1f1dSLionel Sambuc                 else
347*0a6a1f1dSLionel Sambuc                     len = s->init_num;
348ebfedea0SLionel Sambuc             }
349ebfedea0SLionel Sambuc 
350*0a6a1f1dSLionel Sambuc             /* Shouldn't ever happen */
351*0a6a1f1dSLionel Sambuc             if (len > INT_MAX)
352*0a6a1f1dSLionel Sambuc                 len = INT_MAX;
353*0a6a1f1dSLionel Sambuc 
354*0a6a1f1dSLionel Sambuc             if (len < DTLS1_HM_HEADER_LENGTH) {
355*0a6a1f1dSLionel Sambuc                 /*
356*0a6a1f1dSLionel Sambuc                  * len is so small that we really can't do anything sensible
357*0a6a1f1dSLionel Sambuc                  * so fail
358*0a6a1f1dSLionel Sambuc                  */
359*0a6a1f1dSLionel Sambuc                 return -1;
360*0a6a1f1dSLionel Sambuc             }
361ebfedea0SLionel Sambuc             dtls1_fix_message_header(s, frag_off,
362ebfedea0SLionel Sambuc                                      len - DTLS1_HM_HEADER_LENGTH);
363ebfedea0SLionel Sambuc 
364*0a6a1f1dSLionel Sambuc             dtls1_write_message_header(s,
365*0a6a1f1dSLionel Sambuc                                        (unsigned char *)&s->init_buf->
366*0a6a1f1dSLionel Sambuc                                        data[s->init_off]);
367ebfedea0SLionel Sambuc         }
368ebfedea0SLionel Sambuc 
369ebfedea0SLionel Sambuc         ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off],
370ebfedea0SLionel Sambuc                                 len);
371*0a6a1f1dSLionel Sambuc         if (ret < 0) {
372*0a6a1f1dSLionel Sambuc             /*
373*0a6a1f1dSLionel Sambuc              * might need to update MTU here, but we don't know which
374*0a6a1f1dSLionel Sambuc              * previous packet caused the failure -- so can't really
375*0a6a1f1dSLionel Sambuc              * retransmit anything.  continue as if everything is fine and
376*0a6a1f1dSLionel Sambuc              * wait for an alert to handle the retransmit
377ebfedea0SLionel Sambuc              */
378*0a6a1f1dSLionel Sambuc             if (retry && BIO_ctrl(SSL_get_wbio(s),
379*0a6a1f1dSLionel Sambuc                                   BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {
380*0a6a1f1dSLionel Sambuc                 if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
381*0a6a1f1dSLionel Sambuc                     if (!dtls1_query_mtu(s))
382*0a6a1f1dSLionel Sambuc                         return -1;
383*0a6a1f1dSLionel Sambuc                     /* Have one more go */
384*0a6a1f1dSLionel Sambuc                     retry = 0;
385*0a6a1f1dSLionel Sambuc                 } else
386*0a6a1f1dSLionel Sambuc                     return -1;
387*0a6a1f1dSLionel Sambuc             } else {
388ebfedea0SLionel Sambuc                 return (-1);
389ebfedea0SLionel Sambuc             }
390*0a6a1f1dSLionel Sambuc         } else {
391ebfedea0SLionel Sambuc 
392*0a6a1f1dSLionel Sambuc             /*
393*0a6a1f1dSLionel Sambuc              * bad if this assert fails, only part of the handshake message
394*0a6a1f1dSLionel Sambuc              * got sent.  but why would this happen?
395*0a6a1f1dSLionel Sambuc              */
396ebfedea0SLionel Sambuc             OPENSSL_assert(len == (unsigned int)ret);
397ebfedea0SLionel Sambuc 
398*0a6a1f1dSLionel Sambuc             if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) {
399*0a6a1f1dSLionel Sambuc                 /*
400*0a6a1f1dSLionel Sambuc                  * should not be done for 'Hello Request's, but in that case
401*0a6a1f1dSLionel Sambuc                  * we'll ignore the result anyway
402*0a6a1f1dSLionel Sambuc                  */
403*0a6a1f1dSLionel Sambuc                 unsigned char *p =
404*0a6a1f1dSLionel Sambuc                     (unsigned char *)&s->init_buf->data[s->init_off];
405ebfedea0SLionel Sambuc                 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
406ebfedea0SLionel Sambuc                 int xlen;
407ebfedea0SLionel Sambuc 
408*0a6a1f1dSLionel Sambuc                 if (frag_off == 0 && s->version != DTLS1_BAD_VER) {
409*0a6a1f1dSLionel Sambuc                     /*
410*0a6a1f1dSLionel Sambuc                      * reconstruct message header is if it is being sent in
411*0a6a1f1dSLionel Sambuc                      * single fragment
412*0a6a1f1dSLionel Sambuc                      */
413ebfedea0SLionel Sambuc                     *p++ = msg_hdr->type;
414ebfedea0SLionel Sambuc                     l2n3(msg_hdr->msg_len, p);
415ebfedea0SLionel Sambuc                     s2n(msg_hdr->seq, p);
416ebfedea0SLionel Sambuc                     l2n3(0, p);
417ebfedea0SLionel Sambuc                     l2n3(msg_hdr->msg_len, p);
418ebfedea0SLionel Sambuc                     p -= DTLS1_HM_HEADER_LENGTH;
419ebfedea0SLionel Sambuc                     xlen = ret;
420*0a6a1f1dSLionel Sambuc                 } else {
421ebfedea0SLionel Sambuc                     p += DTLS1_HM_HEADER_LENGTH;
422ebfedea0SLionel Sambuc                     xlen = ret - DTLS1_HM_HEADER_LENGTH;
423ebfedea0SLionel Sambuc                 }
424ebfedea0SLionel Sambuc 
425ebfedea0SLionel Sambuc                 ssl3_finish_mac(s, p, xlen);
426ebfedea0SLionel Sambuc             }
427ebfedea0SLionel Sambuc 
428*0a6a1f1dSLionel Sambuc             if (ret == s->init_num) {
429ebfedea0SLionel Sambuc                 if (s->msg_callback)
430ebfedea0SLionel Sambuc                     s->msg_callback(1, s->version, type, s->init_buf->data,
431ebfedea0SLionel Sambuc                                     (size_t)(s->init_off + s->init_num), s,
432ebfedea0SLionel Sambuc                                     s->msg_callback_arg);
433ebfedea0SLionel Sambuc 
434ebfedea0SLionel Sambuc                 s->init_off = 0; /* done writing this message */
435ebfedea0SLionel Sambuc                 s->init_num = 0;
436ebfedea0SLionel Sambuc 
437ebfedea0SLionel Sambuc                 return (1);
438ebfedea0SLionel Sambuc             }
439ebfedea0SLionel Sambuc             s->init_off += ret;
440ebfedea0SLionel Sambuc             s->init_num -= ret;
441ebfedea0SLionel Sambuc             frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
442ebfedea0SLionel Sambuc         }
443ebfedea0SLionel Sambuc     }
444ebfedea0SLionel Sambuc     return (0);
445ebfedea0SLionel Sambuc }
446ebfedea0SLionel Sambuc 
447*0a6a1f1dSLionel Sambuc /*
448*0a6a1f1dSLionel Sambuc  * Obtain handshake message of message type 'mt' (any if mt == -1), maximum
449*0a6a1f1dSLionel Sambuc  * acceptable body length 'max'. Read an entire handshake message.  Handshake
450*0a6a1f1dSLionel Sambuc  * messages arrive in fragments.
451ebfedea0SLionel Sambuc  */
dtls1_get_message(SSL * s,int st1,int stn,int mt,long max,int * ok)452ebfedea0SLionel Sambuc long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
453ebfedea0SLionel Sambuc {
454ebfedea0SLionel Sambuc     int i, al;
455ebfedea0SLionel Sambuc     struct hm_header_st *msg_hdr;
456ebfedea0SLionel Sambuc     unsigned char *p;
457ebfedea0SLionel Sambuc     unsigned long msg_len;
458ebfedea0SLionel Sambuc 
459*0a6a1f1dSLionel Sambuc     /*
460*0a6a1f1dSLionel Sambuc      * s3->tmp is used to store messages that are unexpected, caused by the
461*0a6a1f1dSLionel Sambuc      * absence of an optional handshake message
462*0a6a1f1dSLionel Sambuc      */
463*0a6a1f1dSLionel Sambuc     if (s->s3->tmp.reuse_message) {
464ebfedea0SLionel Sambuc         s->s3->tmp.reuse_message = 0;
465*0a6a1f1dSLionel Sambuc         if ((mt >= 0) && (s->s3->tmp.message_type != mt)) {
466ebfedea0SLionel Sambuc             al = SSL_AD_UNEXPECTED_MESSAGE;
467ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_GET_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
468ebfedea0SLionel Sambuc             goto f_err;
469ebfedea0SLionel Sambuc         }
470ebfedea0SLionel Sambuc         *ok = 1;
471ebfedea0SLionel Sambuc         s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
472ebfedea0SLionel Sambuc         s->init_num = (int)s->s3->tmp.message_size;
473ebfedea0SLionel Sambuc         return s->init_num;
474ebfedea0SLionel Sambuc     }
475ebfedea0SLionel Sambuc 
476ebfedea0SLionel Sambuc     msg_hdr = &s->d1->r_msg_hdr;
477ebfedea0SLionel Sambuc     memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
478ebfedea0SLionel Sambuc 
479ebfedea0SLionel Sambuc  again:
480ebfedea0SLionel Sambuc     i = dtls1_get_message_fragment(s, st1, stn, max, ok);
481*0a6a1f1dSLionel Sambuc     if (i == DTLS1_HM_BAD_FRAGMENT || i == DTLS1_HM_FRAGMENT_RETRY) {
482*0a6a1f1dSLionel Sambuc         /* bad fragment received */
483ebfedea0SLionel Sambuc         goto again;
484*0a6a1f1dSLionel Sambuc     } else if (i <= 0 && !*ok) {
485ebfedea0SLionel Sambuc         return i;
486*0a6a1f1dSLionel Sambuc     }
487*0a6a1f1dSLionel Sambuc 
488*0a6a1f1dSLionel Sambuc     if (mt >= 0 && s->s3->tmp.message_type != mt) {
489*0a6a1f1dSLionel Sambuc         al = SSL_AD_UNEXPECTED_MESSAGE;
490*0a6a1f1dSLionel Sambuc         SSLerr(SSL_F_DTLS1_GET_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
491*0a6a1f1dSLionel Sambuc         goto f_err;
492*0a6a1f1dSLionel Sambuc     }
493ebfedea0SLionel Sambuc 
494ebfedea0SLionel Sambuc     p = (unsigned char *)s->init_buf->data;
495ebfedea0SLionel Sambuc     msg_len = msg_hdr->msg_len;
496ebfedea0SLionel Sambuc 
497ebfedea0SLionel Sambuc     /* reconstruct message header */
498ebfedea0SLionel Sambuc     *(p++) = msg_hdr->type;
499ebfedea0SLionel Sambuc     l2n3(msg_len, p);
500ebfedea0SLionel Sambuc     s2n(msg_hdr->seq, p);
501ebfedea0SLionel Sambuc     l2n3(0, p);
502ebfedea0SLionel Sambuc     l2n3(msg_len, p);
503ebfedea0SLionel Sambuc     if (s->version != DTLS1_BAD_VER) {
504ebfedea0SLionel Sambuc         p -= DTLS1_HM_HEADER_LENGTH;
505ebfedea0SLionel Sambuc         msg_len += DTLS1_HM_HEADER_LENGTH;
506ebfedea0SLionel Sambuc     }
507ebfedea0SLionel Sambuc 
508ebfedea0SLionel Sambuc     ssl3_finish_mac(s, p, msg_len);
509ebfedea0SLionel Sambuc     if (s->msg_callback)
510ebfedea0SLionel Sambuc         s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
511*0a6a1f1dSLionel Sambuc                         p, msg_len, s, s->msg_callback_arg);
512ebfedea0SLionel Sambuc 
513ebfedea0SLionel Sambuc     memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
514ebfedea0SLionel Sambuc 
515ebfedea0SLionel Sambuc     /* Don't change sequence numbers while listening */
516ebfedea0SLionel Sambuc     if (!s->d1->listen)
517ebfedea0SLionel Sambuc         s->d1->handshake_read_seq++;
518ebfedea0SLionel Sambuc 
519ebfedea0SLionel Sambuc     s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
520ebfedea0SLionel Sambuc     return s->init_num;
521ebfedea0SLionel Sambuc 
522ebfedea0SLionel Sambuc  f_err:
523ebfedea0SLionel Sambuc     ssl3_send_alert(s, SSL3_AL_FATAL, al);
524ebfedea0SLionel Sambuc     *ok = 0;
525ebfedea0SLionel Sambuc     return -1;
526ebfedea0SLionel Sambuc }
527ebfedea0SLionel Sambuc 
dtls1_preprocess_fragment(SSL * s,struct hm_header_st * msg_hdr,int max)528*0a6a1f1dSLionel Sambuc static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr,
529*0a6a1f1dSLionel Sambuc                                      int max)
530ebfedea0SLionel Sambuc {
531ebfedea0SLionel Sambuc     size_t frag_off, frag_len, msg_len;
532ebfedea0SLionel Sambuc 
533ebfedea0SLionel Sambuc     msg_len = msg_hdr->msg_len;
534ebfedea0SLionel Sambuc     frag_off = msg_hdr->frag_off;
535ebfedea0SLionel Sambuc     frag_len = msg_hdr->frag_len;
536ebfedea0SLionel Sambuc 
537ebfedea0SLionel Sambuc     /* sanity checking */
538*0a6a1f1dSLionel Sambuc     if ((frag_off + frag_len) > msg_len) {
539ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, SSL_R_EXCESSIVE_MESSAGE_SIZE);
540ebfedea0SLionel Sambuc         return SSL_AD_ILLEGAL_PARAMETER;
541ebfedea0SLionel Sambuc     }
542ebfedea0SLionel Sambuc 
543*0a6a1f1dSLionel Sambuc     if ((frag_off + frag_len) > (unsigned long)max) {
544ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, SSL_R_EXCESSIVE_MESSAGE_SIZE);
545ebfedea0SLionel Sambuc         return SSL_AD_ILLEGAL_PARAMETER;
546ebfedea0SLionel Sambuc     }
547ebfedea0SLionel Sambuc 
548*0a6a1f1dSLionel Sambuc     if (s->d1->r_msg_hdr.frag_off == 0) { /* first fragment */
549*0a6a1f1dSLionel Sambuc         /*
550*0a6a1f1dSLionel Sambuc          * msg_len is limited to 2^24, but is effectively checked against max
551*0a6a1f1dSLionel Sambuc          * above
552*0a6a1f1dSLionel Sambuc          */
553*0a6a1f1dSLionel Sambuc         if (!BUF_MEM_grow_clean
554*0a6a1f1dSLionel Sambuc             (s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
555ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, ERR_R_BUF_LIB);
556ebfedea0SLionel Sambuc             return SSL_AD_INTERNAL_ERROR;
557ebfedea0SLionel Sambuc         }
558ebfedea0SLionel Sambuc 
559ebfedea0SLionel Sambuc         s->s3->tmp.message_size = msg_len;
560ebfedea0SLionel Sambuc         s->d1->r_msg_hdr.msg_len = msg_len;
561ebfedea0SLionel Sambuc         s->s3->tmp.message_type = msg_hdr->type;
562ebfedea0SLionel Sambuc         s->d1->r_msg_hdr.type = msg_hdr->type;
563ebfedea0SLionel Sambuc         s->d1->r_msg_hdr.seq = msg_hdr->seq;
564*0a6a1f1dSLionel Sambuc     } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
565*0a6a1f1dSLionel Sambuc         /*
566*0a6a1f1dSLionel Sambuc          * They must be playing with us! BTW, failure to enforce upper limit
567*0a6a1f1dSLionel Sambuc          * would open possibility for buffer overrun.
568*0a6a1f1dSLionel Sambuc          */
569ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, SSL_R_EXCESSIVE_MESSAGE_SIZE);
570ebfedea0SLionel Sambuc         return SSL_AD_ILLEGAL_PARAMETER;
571ebfedea0SLionel Sambuc     }
572ebfedea0SLionel Sambuc 
573ebfedea0SLionel Sambuc     return 0;                   /* no error */
574ebfedea0SLionel Sambuc }
575ebfedea0SLionel Sambuc 
dtls1_retrieve_buffered_fragment(SSL * s,long max,int * ok)576*0a6a1f1dSLionel Sambuc static int dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok)
577ebfedea0SLionel Sambuc {
578*0a6a1f1dSLionel Sambuc     /*-
579*0a6a1f1dSLionel Sambuc      * (0) check whether the desired fragment is available
580ebfedea0SLionel Sambuc      * if so:
581ebfedea0SLionel Sambuc      * (1) copy over the fragment to s->init_buf->data[]
582ebfedea0SLionel Sambuc      * (2) update s->init_num
583ebfedea0SLionel Sambuc      */
584ebfedea0SLionel Sambuc     pitem *item;
585ebfedea0SLionel Sambuc     hm_fragment *frag;
586ebfedea0SLionel Sambuc     int al;
587ebfedea0SLionel Sambuc 
588ebfedea0SLionel Sambuc     *ok = 0;
589ebfedea0SLionel Sambuc     item = pqueue_peek(s->d1->buffered_messages);
590ebfedea0SLionel Sambuc     if (item == NULL)
591ebfedea0SLionel Sambuc         return 0;
592ebfedea0SLionel Sambuc 
593ebfedea0SLionel Sambuc     frag = (hm_fragment *)item->data;
594ebfedea0SLionel Sambuc 
595ebfedea0SLionel Sambuc     /* Don't return if reassembly still in progress */
596ebfedea0SLionel Sambuc     if (frag->reassembly != NULL)
597ebfedea0SLionel Sambuc         return 0;
598ebfedea0SLionel Sambuc 
599*0a6a1f1dSLionel Sambuc     if (s->d1->handshake_read_seq == frag->msg_header.seq) {
600ebfedea0SLionel Sambuc         unsigned long frag_len = frag->msg_header.frag_len;
601ebfedea0SLionel Sambuc         pqueue_pop(s->d1->buffered_messages);
602ebfedea0SLionel Sambuc 
603ebfedea0SLionel Sambuc         al = dtls1_preprocess_fragment(s, &frag->msg_header, max);
604ebfedea0SLionel Sambuc 
605*0a6a1f1dSLionel Sambuc         if (al == 0) {          /* no alert */
606*0a6a1f1dSLionel Sambuc             unsigned char *p =
607*0a6a1f1dSLionel Sambuc                 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
608*0a6a1f1dSLionel Sambuc             memcpy(&p[frag->msg_header.frag_off], frag->fragment,
609*0a6a1f1dSLionel Sambuc                    frag->msg_header.frag_len);
610ebfedea0SLionel Sambuc         }
611ebfedea0SLionel Sambuc 
612ebfedea0SLionel Sambuc         dtls1_hm_fragment_free(frag);
613ebfedea0SLionel Sambuc         pitem_free(item);
614ebfedea0SLionel Sambuc 
615*0a6a1f1dSLionel Sambuc         if (al == 0) {
616ebfedea0SLionel Sambuc             *ok = 1;
617ebfedea0SLionel Sambuc             return frag_len;
618ebfedea0SLionel Sambuc         }
619ebfedea0SLionel Sambuc 
620ebfedea0SLionel Sambuc         ssl3_send_alert(s, SSL3_AL_FATAL, al);
621ebfedea0SLionel Sambuc         s->init_num = 0;
622ebfedea0SLionel Sambuc         *ok = 0;
623ebfedea0SLionel Sambuc         return -1;
624*0a6a1f1dSLionel Sambuc     } else
625ebfedea0SLionel Sambuc         return 0;
626ebfedea0SLionel Sambuc }
627ebfedea0SLionel Sambuc 
628*0a6a1f1dSLionel Sambuc /*
629*0a6a1f1dSLionel Sambuc  * dtls1_max_handshake_message_len returns the maximum number of bytes
630*0a6a1f1dSLionel Sambuc  * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
631*0a6a1f1dSLionel Sambuc  * may be greater if the maximum certificate list size requires it.
632*0a6a1f1dSLionel Sambuc  */
dtls1_max_handshake_message_len(const SSL * s)633*0a6a1f1dSLionel Sambuc static unsigned long dtls1_max_handshake_message_len(const SSL *s)
634*0a6a1f1dSLionel Sambuc {
635*0a6a1f1dSLionel Sambuc     unsigned long max_len =
636*0a6a1f1dSLionel Sambuc         DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
637*0a6a1f1dSLionel Sambuc     if (max_len < (unsigned long)s->max_cert_list)
638*0a6a1f1dSLionel Sambuc         return s->max_cert_list;
639*0a6a1f1dSLionel Sambuc     return max_len;
640*0a6a1f1dSLionel Sambuc }
641ebfedea0SLionel Sambuc 
642ebfedea0SLionel Sambuc static int
dtls1_reassemble_fragment(SSL * s,const struct hm_header_st * msg_hdr,int * ok)643*0a6a1f1dSLionel Sambuc dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr, int *ok)
644ebfedea0SLionel Sambuc {
645ebfedea0SLionel Sambuc     hm_fragment *frag = NULL;
646ebfedea0SLionel Sambuc     pitem *item = NULL;
647ebfedea0SLionel Sambuc     int i = -1, is_complete;
648ebfedea0SLionel Sambuc     unsigned char seq64be[8];
649*0a6a1f1dSLionel Sambuc     unsigned long frag_len = msg_hdr->frag_len;
650ebfedea0SLionel Sambuc 
651*0a6a1f1dSLionel Sambuc     if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
652*0a6a1f1dSLionel Sambuc         msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
653ebfedea0SLionel Sambuc         goto err;
654ebfedea0SLionel Sambuc 
655*0a6a1f1dSLionel Sambuc     if (frag_len == 0)
656*0a6a1f1dSLionel Sambuc         return DTLS1_HM_FRAGMENT_RETRY;
657ebfedea0SLionel Sambuc 
658ebfedea0SLionel Sambuc     /* Try to find item in queue */
659ebfedea0SLionel Sambuc     memset(seq64be, 0, sizeof(seq64be));
660ebfedea0SLionel Sambuc     seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
661ebfedea0SLionel Sambuc     seq64be[7] = (unsigned char)msg_hdr->seq;
662ebfedea0SLionel Sambuc     item = pqueue_find(s->d1->buffered_messages, seq64be);
663ebfedea0SLionel Sambuc 
664*0a6a1f1dSLionel Sambuc     if (item == NULL) {
665ebfedea0SLionel Sambuc         frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
666ebfedea0SLionel Sambuc         if (frag == NULL)
667ebfedea0SLionel Sambuc             goto err;
668ebfedea0SLionel Sambuc         memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
669ebfedea0SLionel Sambuc         frag->msg_header.frag_len = frag->msg_header.msg_len;
670ebfedea0SLionel Sambuc         frag->msg_header.frag_off = 0;
671*0a6a1f1dSLionel Sambuc     } else {
672ebfedea0SLionel Sambuc         frag = (hm_fragment *)item->data;
673*0a6a1f1dSLionel Sambuc         if (frag->msg_header.msg_len != msg_hdr->msg_len) {
674*0a6a1f1dSLionel Sambuc             item = NULL;
675*0a6a1f1dSLionel Sambuc             frag = NULL;
676*0a6a1f1dSLionel Sambuc             goto err;
677*0a6a1f1dSLionel Sambuc         }
678*0a6a1f1dSLionel Sambuc     }
679ebfedea0SLionel Sambuc 
680*0a6a1f1dSLionel Sambuc     /*
681*0a6a1f1dSLionel Sambuc      * If message is already reassembled, this must be a retransmit and can
682*0a6a1f1dSLionel Sambuc      * be dropped. In this case item != NULL and so frag does not need to be
683*0a6a1f1dSLionel Sambuc      * freed.
684ebfedea0SLionel Sambuc      */
685*0a6a1f1dSLionel Sambuc     if (frag->reassembly == NULL) {
686ebfedea0SLionel Sambuc         unsigned char devnull[256];
687ebfedea0SLionel Sambuc 
688*0a6a1f1dSLionel Sambuc         while (frag_len) {
689ebfedea0SLionel Sambuc             i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
690ebfedea0SLionel Sambuc                                           devnull,
691*0a6a1f1dSLionel Sambuc                                           frag_len >
692*0a6a1f1dSLionel Sambuc                                           sizeof(devnull) ? sizeof(devnull) :
693*0a6a1f1dSLionel Sambuc                                           frag_len, 0);
694*0a6a1f1dSLionel Sambuc             if (i <= 0)
695*0a6a1f1dSLionel Sambuc                 goto err;
696ebfedea0SLionel Sambuc             frag_len -= i;
697ebfedea0SLionel Sambuc         }
698ebfedea0SLionel Sambuc         return DTLS1_HM_FRAGMENT_RETRY;
699ebfedea0SLionel Sambuc     }
700ebfedea0SLionel Sambuc 
701ebfedea0SLionel Sambuc     /* read the body of the fragment (header has already been read */
702ebfedea0SLionel Sambuc     i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
703*0a6a1f1dSLionel Sambuc                                   frag->fragment + msg_hdr->frag_off,
704*0a6a1f1dSLionel Sambuc                                   frag_len, 0);
705*0a6a1f1dSLionel Sambuc     if ((unsigned long)i != frag_len)
706*0a6a1f1dSLionel Sambuc         i = -1;
707*0a6a1f1dSLionel Sambuc     if (i <= 0)
708ebfedea0SLionel Sambuc         goto err;
709ebfedea0SLionel Sambuc 
710ebfedea0SLionel Sambuc     RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
711ebfedea0SLionel Sambuc                         (long)(msg_hdr->frag_off + frag_len));
712ebfedea0SLionel Sambuc 
713ebfedea0SLionel Sambuc     RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
714ebfedea0SLionel Sambuc                                is_complete);
715ebfedea0SLionel Sambuc 
716*0a6a1f1dSLionel Sambuc     if (is_complete) {
717ebfedea0SLionel Sambuc         OPENSSL_free(frag->reassembly);
718ebfedea0SLionel Sambuc         frag->reassembly = NULL;
719ebfedea0SLionel Sambuc     }
720ebfedea0SLionel Sambuc 
721*0a6a1f1dSLionel Sambuc     if (item == NULL) {
722ebfedea0SLionel Sambuc         item = pitem_new(seq64be, frag);
723*0a6a1f1dSLionel Sambuc         if (item == NULL) {
724ebfedea0SLionel Sambuc             i = -1;
725*0a6a1f1dSLionel Sambuc             goto err;
726ebfedea0SLionel Sambuc         }
727ebfedea0SLionel Sambuc 
728*0a6a1f1dSLionel Sambuc         item = pqueue_insert(s->d1->buffered_messages, item);
729*0a6a1f1dSLionel Sambuc         /*
730*0a6a1f1dSLionel Sambuc          * pqueue_insert fails iff a duplicate item is inserted. However,
731*0a6a1f1dSLionel Sambuc          * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
732*0a6a1f1dSLionel Sambuc          * would have returned it and control would never have reached this
733*0a6a1f1dSLionel Sambuc          * branch.
734*0a6a1f1dSLionel Sambuc          */
735*0a6a1f1dSLionel Sambuc         OPENSSL_assert(item != NULL);
736ebfedea0SLionel Sambuc     }
737ebfedea0SLionel Sambuc 
738ebfedea0SLionel Sambuc     return DTLS1_HM_FRAGMENT_RETRY;
739ebfedea0SLionel Sambuc 
740ebfedea0SLionel Sambuc  err:
741*0a6a1f1dSLionel Sambuc     if (frag != NULL && item == NULL)
742*0a6a1f1dSLionel Sambuc         dtls1_hm_fragment_free(frag);
743ebfedea0SLionel Sambuc     *ok = 0;
744ebfedea0SLionel Sambuc     return i;
745ebfedea0SLionel Sambuc }
746ebfedea0SLionel Sambuc 
747ebfedea0SLionel Sambuc static int
dtls1_process_out_of_seq_message(SSL * s,const struct hm_header_st * msg_hdr,int * ok)748*0a6a1f1dSLionel Sambuc dtls1_process_out_of_seq_message(SSL *s, const struct hm_header_st *msg_hdr,
749*0a6a1f1dSLionel Sambuc                                  int *ok)
750ebfedea0SLionel Sambuc {
751ebfedea0SLionel Sambuc     int i = -1;
752ebfedea0SLionel Sambuc     hm_fragment *frag = NULL;
753ebfedea0SLionel Sambuc     pitem *item = NULL;
754ebfedea0SLionel Sambuc     unsigned char seq64be[8];
755ebfedea0SLionel Sambuc     unsigned long frag_len = msg_hdr->frag_len;
756ebfedea0SLionel Sambuc 
757ebfedea0SLionel Sambuc     if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
758ebfedea0SLionel Sambuc         goto err;
759ebfedea0SLionel Sambuc 
760ebfedea0SLionel Sambuc     /* Try to find item in queue, to prevent duplicate entries */
761ebfedea0SLionel Sambuc     memset(seq64be, 0, sizeof(seq64be));
762ebfedea0SLionel Sambuc     seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
763ebfedea0SLionel Sambuc     seq64be[7] = (unsigned char)msg_hdr->seq;
764ebfedea0SLionel Sambuc     item = pqueue_find(s->d1->buffered_messages, seq64be);
765ebfedea0SLionel Sambuc 
766*0a6a1f1dSLionel Sambuc     /*
767*0a6a1f1dSLionel Sambuc      * If we already have an entry and this one is a fragment, don't discard
768*0a6a1f1dSLionel Sambuc      * it and rather try to reassemble it.
769ebfedea0SLionel Sambuc      */
770*0a6a1f1dSLionel Sambuc     if (item != NULL && frag_len != msg_hdr->msg_len)
771ebfedea0SLionel Sambuc         item = NULL;
772ebfedea0SLionel Sambuc 
773*0a6a1f1dSLionel Sambuc     /*
774*0a6a1f1dSLionel Sambuc      * Discard the message if sequence number was already there, is too far
775*0a6a1f1dSLionel Sambuc      * in the future, already in the queue or if we received a FINISHED
776*0a6a1f1dSLionel Sambuc      * before the SERVER_HELLO, which then must be a stale retransmit.
777ebfedea0SLionel Sambuc      */
778ebfedea0SLionel Sambuc     if (msg_hdr->seq <= s->d1->handshake_read_seq ||
779ebfedea0SLionel Sambuc         msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
780ebfedea0SLionel Sambuc         (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED))
781ebfedea0SLionel Sambuc     {
782ebfedea0SLionel Sambuc         unsigned char devnull[256];
783ebfedea0SLionel Sambuc 
784*0a6a1f1dSLionel Sambuc         while (frag_len) {
785ebfedea0SLionel Sambuc             i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
786ebfedea0SLionel Sambuc                                           devnull,
787*0a6a1f1dSLionel Sambuc                                           frag_len >
788*0a6a1f1dSLionel Sambuc                                           sizeof(devnull) ? sizeof(devnull) :
789*0a6a1f1dSLionel Sambuc                                           frag_len, 0);
790*0a6a1f1dSLionel Sambuc             if (i <= 0)
791*0a6a1f1dSLionel Sambuc                 goto err;
792ebfedea0SLionel Sambuc             frag_len -= i;
793ebfedea0SLionel Sambuc         }
794*0a6a1f1dSLionel Sambuc     } else {
795*0a6a1f1dSLionel Sambuc         if (frag_len != msg_hdr->msg_len)
796ebfedea0SLionel Sambuc             return dtls1_reassemble_fragment(s, msg_hdr, ok);
797ebfedea0SLionel Sambuc 
798*0a6a1f1dSLionel Sambuc         if (frag_len > dtls1_max_handshake_message_len(s))
799*0a6a1f1dSLionel Sambuc             goto err;
800*0a6a1f1dSLionel Sambuc 
801ebfedea0SLionel Sambuc         frag = dtls1_hm_fragment_new(frag_len, 0);
802ebfedea0SLionel Sambuc         if (frag == NULL)
803ebfedea0SLionel Sambuc             goto err;
804ebfedea0SLionel Sambuc 
805ebfedea0SLionel Sambuc         memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
806ebfedea0SLionel Sambuc 
807*0a6a1f1dSLionel Sambuc         if (frag_len) {
808*0a6a1f1dSLionel Sambuc             /*
809*0a6a1f1dSLionel Sambuc              * read the body of the fragment (header has already been read
810*0a6a1f1dSLionel Sambuc              */
811ebfedea0SLionel Sambuc             i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
812ebfedea0SLionel Sambuc                                           frag->fragment, frag_len, 0);
813*0a6a1f1dSLionel Sambuc             if ((unsigned long)i != frag_len)
814*0a6a1f1dSLionel Sambuc                 i = -1;
815*0a6a1f1dSLionel Sambuc             if (i <= 0)
816ebfedea0SLionel Sambuc                 goto err;
817ebfedea0SLionel Sambuc         }
818ebfedea0SLionel Sambuc 
819ebfedea0SLionel Sambuc         item = pitem_new(seq64be, frag);
820ebfedea0SLionel Sambuc         if (item == NULL)
821ebfedea0SLionel Sambuc             goto err;
822ebfedea0SLionel Sambuc 
823*0a6a1f1dSLionel Sambuc         item = pqueue_insert(s->d1->buffered_messages, item);
824*0a6a1f1dSLionel Sambuc         /*
825*0a6a1f1dSLionel Sambuc          * pqueue_insert fails iff a duplicate item is inserted. However,
826*0a6a1f1dSLionel Sambuc          * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
827*0a6a1f1dSLionel Sambuc          * would have returned it. Then, either |frag_len| !=
828*0a6a1f1dSLionel Sambuc          * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
829*0a6a1f1dSLionel Sambuc          * have been processed with |dtls1_reassemble_fragment|, above, or
830*0a6a1f1dSLionel Sambuc          * the record will have been discarded.
831*0a6a1f1dSLionel Sambuc          */
832*0a6a1f1dSLionel Sambuc         OPENSSL_assert(item != NULL);
833ebfedea0SLionel Sambuc     }
834ebfedea0SLionel Sambuc 
835ebfedea0SLionel Sambuc     return DTLS1_HM_FRAGMENT_RETRY;
836ebfedea0SLionel Sambuc 
837ebfedea0SLionel Sambuc  err:
838*0a6a1f1dSLionel Sambuc     if (frag != NULL && item == NULL)
839*0a6a1f1dSLionel Sambuc         dtls1_hm_fragment_free(frag);
840ebfedea0SLionel Sambuc     *ok = 0;
841ebfedea0SLionel Sambuc     return i;
842ebfedea0SLionel Sambuc }
843ebfedea0SLionel Sambuc 
844ebfedea0SLionel Sambuc static long
dtls1_get_message_fragment(SSL * s,int st1,int stn,long max,int * ok)845ebfedea0SLionel Sambuc dtls1_get_message_fragment(SSL *s, int st1, int stn, long max, int *ok)
846ebfedea0SLionel Sambuc {
847ebfedea0SLionel Sambuc     unsigned char wire[DTLS1_HM_HEADER_LENGTH];
848ebfedea0SLionel Sambuc     unsigned long len, frag_off, frag_len;
849ebfedea0SLionel Sambuc     int i, al;
850ebfedea0SLionel Sambuc     struct hm_header_st msg_hdr;
851ebfedea0SLionel Sambuc 
852*0a6a1f1dSLionel Sambuc  redo:
853ebfedea0SLionel Sambuc     /* see if we have the required fragment already */
854*0a6a1f1dSLionel Sambuc     if ((frag_len = dtls1_retrieve_buffered_fragment(s, max, ok)) || *ok) {
855*0a6a1f1dSLionel Sambuc         if (*ok)
856*0a6a1f1dSLionel Sambuc             s->init_num = frag_len;
857ebfedea0SLionel Sambuc         return frag_len;
858ebfedea0SLionel Sambuc     }
859ebfedea0SLionel Sambuc 
860ebfedea0SLionel Sambuc     /* read handshake message header */
861ebfedea0SLionel Sambuc     i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire,
862ebfedea0SLionel Sambuc                                   DTLS1_HM_HEADER_LENGTH, 0);
863*0a6a1f1dSLionel Sambuc     if (i <= 0) {               /* nbio, or an error */
864ebfedea0SLionel Sambuc         s->rwstate = SSL_READING;
865ebfedea0SLionel Sambuc         *ok = 0;
866ebfedea0SLionel Sambuc         return i;
867ebfedea0SLionel Sambuc     }
868ebfedea0SLionel Sambuc     /* Handshake fails if message header is incomplete */
869*0a6a1f1dSLionel Sambuc     if (i != DTLS1_HM_HEADER_LENGTH) {
870ebfedea0SLionel Sambuc         al = SSL_AD_UNEXPECTED_MESSAGE;
871ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT, SSL_R_UNEXPECTED_MESSAGE);
872ebfedea0SLionel Sambuc         goto f_err;
873ebfedea0SLionel Sambuc     }
874ebfedea0SLionel Sambuc 
875ebfedea0SLionel Sambuc     /* parse the message fragment header */
876ebfedea0SLionel Sambuc     dtls1_get_message_header(wire, &msg_hdr);
877ebfedea0SLionel Sambuc 
878*0a6a1f1dSLionel Sambuc     len = msg_hdr.msg_len;
879*0a6a1f1dSLionel Sambuc     frag_off = msg_hdr.frag_off;
880*0a6a1f1dSLionel Sambuc     frag_len = msg_hdr.frag_len;
881*0a6a1f1dSLionel Sambuc 
882*0a6a1f1dSLionel Sambuc     /*
883*0a6a1f1dSLionel Sambuc      * We must have at least frag_len bytes left in the record to be read.
884*0a6a1f1dSLionel Sambuc      * Fragments must not span records.
885*0a6a1f1dSLionel Sambuc      */
886*0a6a1f1dSLionel Sambuc     if (frag_len > s->s3->rrec.length) {
887*0a6a1f1dSLionel Sambuc         al = SSL3_AD_ILLEGAL_PARAMETER;
888*0a6a1f1dSLionel Sambuc         SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT, SSL_R_BAD_LENGTH);
889*0a6a1f1dSLionel Sambuc         goto f_err;
890*0a6a1f1dSLionel Sambuc     }
891*0a6a1f1dSLionel Sambuc 
892ebfedea0SLionel Sambuc     /*
893ebfedea0SLionel Sambuc      * if this is a future (or stale) message it gets buffered
894ebfedea0SLionel Sambuc      * (or dropped)--no further processing at this time
895ebfedea0SLionel Sambuc      * While listening, we accept seq 1 (ClientHello with cookie)
896ebfedea0SLionel Sambuc      * although we're still expecting seq 0 (ClientHello)
897ebfedea0SLionel Sambuc      */
898*0a6a1f1dSLionel Sambuc     if (msg_hdr.seq != s->d1->handshake_read_seq
899*0a6a1f1dSLionel Sambuc         && !(s->d1->listen && msg_hdr.seq == 1))
900ebfedea0SLionel Sambuc         return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
901ebfedea0SLionel Sambuc 
902ebfedea0SLionel Sambuc     if (frag_len && frag_len < len)
903ebfedea0SLionel Sambuc         return dtls1_reassemble_fragment(s, &msg_hdr, ok);
904ebfedea0SLionel Sambuc 
905ebfedea0SLionel Sambuc     if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
906*0a6a1f1dSLionel Sambuc         wire[0] == SSL3_MT_HELLO_REQUEST) {
907*0a6a1f1dSLionel Sambuc         /*
908*0a6a1f1dSLionel Sambuc          * The server may always send 'Hello Request' messages -- we are
909*0a6a1f1dSLionel Sambuc          * doing a handshake anyway now, so ignore them if their format is
910*0a6a1f1dSLionel Sambuc          * correct. Does not count for 'Finished' MAC.
911*0a6a1f1dSLionel Sambuc          */
912*0a6a1f1dSLionel Sambuc         if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
913ebfedea0SLionel Sambuc             if (s->msg_callback)
914ebfedea0SLionel Sambuc                 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
915ebfedea0SLionel Sambuc                                 wire, DTLS1_HM_HEADER_LENGTH, s,
916ebfedea0SLionel Sambuc                                 s->msg_callback_arg);
917ebfedea0SLionel Sambuc 
918ebfedea0SLionel Sambuc             s->init_num = 0;
919*0a6a1f1dSLionel Sambuc             goto redo;
920*0a6a1f1dSLionel Sambuc         } else {                /* Incorrectly formated Hello request */
921*0a6a1f1dSLionel Sambuc 
922ebfedea0SLionel Sambuc             al = SSL_AD_UNEXPECTED_MESSAGE;
923*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT,
924*0a6a1f1dSLionel Sambuc                    SSL_R_UNEXPECTED_MESSAGE);
925ebfedea0SLionel Sambuc             goto f_err;
926ebfedea0SLionel Sambuc         }
927ebfedea0SLionel Sambuc     }
928ebfedea0SLionel Sambuc 
929ebfedea0SLionel Sambuc     if ((al = dtls1_preprocess_fragment(s, &msg_hdr, max)))
930ebfedea0SLionel Sambuc         goto f_err;
931ebfedea0SLionel Sambuc 
932*0a6a1f1dSLionel Sambuc     if (frag_len > 0) {
933*0a6a1f1dSLionel Sambuc         unsigned char *p =
934*0a6a1f1dSLionel Sambuc             (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
935ebfedea0SLionel Sambuc 
936ebfedea0SLionel Sambuc         i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
937ebfedea0SLionel Sambuc                                       &p[frag_off], frag_len, 0);
938*0a6a1f1dSLionel Sambuc 
939*0a6a1f1dSLionel Sambuc         /*
940*0a6a1f1dSLionel Sambuc          * This shouldn't ever fail due to NBIO because we already checked
941*0a6a1f1dSLionel Sambuc          * that we have enough data in the record
942*0a6a1f1dSLionel Sambuc          */
943*0a6a1f1dSLionel Sambuc         if (i <= 0) {
944ebfedea0SLionel Sambuc             s->rwstate = SSL_READING;
945ebfedea0SLionel Sambuc             *ok = 0;
946ebfedea0SLionel Sambuc             return i;
947ebfedea0SLionel Sambuc         }
948*0a6a1f1dSLionel Sambuc     } else
949ebfedea0SLionel Sambuc         i = 0;
950ebfedea0SLionel Sambuc 
951*0a6a1f1dSLionel Sambuc     /*
952*0a6a1f1dSLionel Sambuc      * XDTLS: an incorrectly formatted fragment should cause the handshake
953*0a6a1f1dSLionel Sambuc      * to fail
954*0a6a1f1dSLionel Sambuc      */
955*0a6a1f1dSLionel Sambuc     if (i != (int)frag_len) {
956ebfedea0SLionel Sambuc         al = SSL3_AD_ILLEGAL_PARAMETER;
957ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT, SSL3_AD_ILLEGAL_PARAMETER);
958ebfedea0SLionel Sambuc         goto f_err;
959ebfedea0SLionel Sambuc     }
960ebfedea0SLionel Sambuc 
961ebfedea0SLionel Sambuc     *ok = 1;
962*0a6a1f1dSLionel Sambuc     s->state = stn;
963ebfedea0SLionel Sambuc 
964*0a6a1f1dSLionel Sambuc     /*
965*0a6a1f1dSLionel Sambuc      * Note that s->init_num is *not* used as current offset in
966*0a6a1f1dSLionel Sambuc      * s->init_buf->data, but as a counter summing up fragments' lengths: as
967*0a6a1f1dSLionel Sambuc      * soon as they sum up to handshake packet length, we assume we have got
968*0a6a1f1dSLionel Sambuc      * all the fragments.
969*0a6a1f1dSLionel Sambuc      */
970ebfedea0SLionel Sambuc     s->init_num = frag_len;
971ebfedea0SLionel Sambuc     return frag_len;
972ebfedea0SLionel Sambuc 
973ebfedea0SLionel Sambuc  f_err:
974ebfedea0SLionel Sambuc     ssl3_send_alert(s, SSL3_AL_FATAL, al);
975ebfedea0SLionel Sambuc     s->init_num = 0;
976ebfedea0SLionel Sambuc 
977ebfedea0SLionel Sambuc     *ok = 0;
978ebfedea0SLionel Sambuc     return (-1);
979ebfedea0SLionel Sambuc }
980ebfedea0SLionel Sambuc 
dtls1_send_finished(SSL * s,int a,int b,const char * sender,int slen)981ebfedea0SLionel Sambuc int dtls1_send_finished(SSL *s, int a, int b, const char *sender, int slen)
982ebfedea0SLionel Sambuc {
983ebfedea0SLionel Sambuc     unsigned char *p, *d;
984ebfedea0SLionel Sambuc     int i;
985ebfedea0SLionel Sambuc     unsigned long l;
986ebfedea0SLionel Sambuc 
987*0a6a1f1dSLionel Sambuc     if (s->state == a) {
988ebfedea0SLionel Sambuc         d = (unsigned char *)s->init_buf->data;
989ebfedea0SLionel Sambuc         p = &(d[DTLS1_HM_HEADER_LENGTH]);
990ebfedea0SLionel Sambuc 
991ebfedea0SLionel Sambuc         i = s->method->ssl3_enc->final_finish_mac(s,
992*0a6a1f1dSLionel Sambuc                                                   sender, slen,
993*0a6a1f1dSLionel Sambuc                                                   s->s3->tmp.finish_md);
994ebfedea0SLionel Sambuc         s->s3->tmp.finish_md_len = i;
995ebfedea0SLionel Sambuc         memcpy(p, s->s3->tmp.finish_md, i);
996ebfedea0SLionel Sambuc         p += i;
997ebfedea0SLionel Sambuc         l = i;
998ebfedea0SLionel Sambuc 
999*0a6a1f1dSLionel Sambuc         /*
1000*0a6a1f1dSLionel Sambuc          * Copy the finished so we can use it for renegotiation checks
1001ebfedea0SLionel Sambuc          */
1002*0a6a1f1dSLionel Sambuc         if (s->type == SSL_ST_CONNECT) {
1003ebfedea0SLionel Sambuc             OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
1004*0a6a1f1dSLionel Sambuc             memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, i);
1005ebfedea0SLionel Sambuc             s->s3->previous_client_finished_len = i;
1006*0a6a1f1dSLionel Sambuc         } else {
1007ebfedea0SLionel Sambuc             OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
1008*0a6a1f1dSLionel Sambuc             memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, i);
1009ebfedea0SLionel Sambuc             s->s3->previous_server_finished_len = i;
1010ebfedea0SLionel Sambuc         }
1011ebfedea0SLionel Sambuc 
1012ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_WIN16
1013*0a6a1f1dSLionel Sambuc         /*
1014*0a6a1f1dSLionel Sambuc          * MSVC 1.5 does not clear the top bytes of the word unless I do
1015*0a6a1f1dSLionel Sambuc          * this.
1016ebfedea0SLionel Sambuc          */
1017ebfedea0SLionel Sambuc         l &= 0xffff;
1018ebfedea0SLionel Sambuc #endif
1019ebfedea0SLionel Sambuc 
1020ebfedea0SLionel Sambuc         d = dtls1_set_message_header(s, d, SSL3_MT_FINISHED, l, 0, l);
1021ebfedea0SLionel Sambuc         s->init_num = (int)l + DTLS1_HM_HEADER_LENGTH;
1022ebfedea0SLionel Sambuc         s->init_off = 0;
1023ebfedea0SLionel Sambuc 
1024ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1025ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 0);
1026ebfedea0SLionel Sambuc 
1027ebfedea0SLionel Sambuc         s->state = b;
1028ebfedea0SLionel Sambuc     }
1029ebfedea0SLionel Sambuc 
1030ebfedea0SLionel Sambuc     /* SSL3_ST_SEND_xxxxxx_HELLO_B */
1031ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1032ebfedea0SLionel Sambuc }
1033ebfedea0SLionel Sambuc 
1034*0a6a1f1dSLionel Sambuc /*-
1035*0a6a1f1dSLionel Sambuc  * for these 2 messages, we need to
1036ebfedea0SLionel Sambuc  * ssl->enc_read_ctx                    re-init
1037ebfedea0SLionel Sambuc  * ssl->s3->read_sequence               zero
1038ebfedea0SLionel Sambuc  * ssl->s3->read_mac_secret             re-init
1039ebfedea0SLionel Sambuc  * ssl->session->read_sym_enc           assign
1040ebfedea0SLionel Sambuc  * ssl->session->read_compression       assign
1041ebfedea0SLionel Sambuc  * ssl->session->read_hash              assign
1042ebfedea0SLionel Sambuc  */
dtls1_send_change_cipher_spec(SSL * s,int a,int b)1043ebfedea0SLionel Sambuc int dtls1_send_change_cipher_spec(SSL *s, int a, int b)
1044ebfedea0SLionel Sambuc {
1045ebfedea0SLionel Sambuc     unsigned char *p;
1046ebfedea0SLionel Sambuc 
1047*0a6a1f1dSLionel Sambuc     if (s->state == a) {
1048ebfedea0SLionel Sambuc         p = (unsigned char *)s->init_buf->data;
1049ebfedea0SLionel Sambuc         *p++ = SSL3_MT_CCS;
1050ebfedea0SLionel Sambuc         s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1051ebfedea0SLionel Sambuc         s->init_num = DTLS1_CCS_HEADER_LENGTH;
1052ebfedea0SLionel Sambuc 
1053ebfedea0SLionel Sambuc         if (s->version == DTLS1_BAD_VER) {
1054ebfedea0SLionel Sambuc             s->d1->next_handshake_write_seq++;
1055ebfedea0SLionel Sambuc             s2n(s->d1->handshake_write_seq, p);
1056ebfedea0SLionel Sambuc             s->init_num += 2;
1057ebfedea0SLionel Sambuc         }
1058ebfedea0SLionel Sambuc 
1059ebfedea0SLionel Sambuc         s->init_off = 0;
1060ebfedea0SLionel Sambuc 
1061ebfedea0SLionel Sambuc         dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
1062ebfedea0SLionel Sambuc                                      s->d1->handshake_write_seq, 0, 0);
1063ebfedea0SLionel Sambuc 
1064ebfedea0SLionel Sambuc         /* buffer the message to handle re-xmits */
1065ebfedea0SLionel Sambuc         dtls1_buffer_message(s, 1);
1066ebfedea0SLionel Sambuc 
1067ebfedea0SLionel Sambuc         s->state = b;
1068ebfedea0SLionel Sambuc     }
1069ebfedea0SLionel Sambuc 
1070ebfedea0SLionel Sambuc     /* SSL3_ST_CW_CHANGE_B */
1071ebfedea0SLionel Sambuc     return (dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC));
1072ebfedea0SLionel Sambuc }
1073ebfedea0SLionel Sambuc 
dtls1_add_cert_to_buf(BUF_MEM * buf,unsigned long * l,X509 * x)1074ebfedea0SLionel Sambuc static int dtls1_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
1075ebfedea0SLionel Sambuc {
1076ebfedea0SLionel Sambuc     int n;
1077ebfedea0SLionel Sambuc     unsigned char *p;
1078ebfedea0SLionel Sambuc 
1079ebfedea0SLionel Sambuc     n = i2d_X509(x, NULL);
1080*0a6a1f1dSLionel Sambuc     if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
1081ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
1082ebfedea0SLionel Sambuc         return 0;
1083ebfedea0SLionel Sambuc     }
1084ebfedea0SLionel Sambuc     p = (unsigned char *)&(buf->data[*l]);
1085ebfedea0SLionel Sambuc     l2n3(n, p);
1086ebfedea0SLionel Sambuc     i2d_X509(x, &p);
1087ebfedea0SLionel Sambuc     *l += n + 3;
1088ebfedea0SLionel Sambuc 
1089ebfedea0SLionel Sambuc     return 1;
1090ebfedea0SLionel Sambuc }
1091*0a6a1f1dSLionel Sambuc 
dtls1_output_cert_chain(SSL * s,X509 * x)1092ebfedea0SLionel Sambuc unsigned long dtls1_output_cert_chain(SSL *s, X509 *x)
1093ebfedea0SLionel Sambuc {
1094ebfedea0SLionel Sambuc     unsigned char *p;
1095ebfedea0SLionel Sambuc     int i;
1096ebfedea0SLionel Sambuc     unsigned long l = 3 + DTLS1_HM_HEADER_LENGTH;
1097ebfedea0SLionel Sambuc     BUF_MEM *buf;
1098ebfedea0SLionel Sambuc 
1099ebfedea0SLionel Sambuc     /* TLSv1 sends a chain with nothing in it, instead of an alert */
1100ebfedea0SLionel Sambuc     buf = s->init_buf;
1101*0a6a1f1dSLionel Sambuc     if (!BUF_MEM_grow_clean(buf, 10)) {
1102ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN, ERR_R_BUF_LIB);
1103ebfedea0SLionel Sambuc         return (0);
1104ebfedea0SLionel Sambuc     }
1105*0a6a1f1dSLionel Sambuc     if (x != NULL) {
1106ebfedea0SLionel Sambuc         X509_STORE_CTX xs_ctx;
1107ebfedea0SLionel Sambuc 
1108*0a6a1f1dSLionel Sambuc         if (!X509_STORE_CTX_init(&xs_ctx, s->ctx->cert_store, x, NULL)) {
1109ebfedea0SLionel Sambuc             SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN, ERR_R_X509_LIB);
1110ebfedea0SLionel Sambuc             return (0);
1111ebfedea0SLionel Sambuc         }
1112ebfedea0SLionel Sambuc 
1113ebfedea0SLionel Sambuc         X509_verify_cert(&xs_ctx);
1114ebfedea0SLionel Sambuc         /* Don't leave errors in the queue */
1115ebfedea0SLionel Sambuc         ERR_clear_error();
1116*0a6a1f1dSLionel Sambuc         for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
1117ebfedea0SLionel Sambuc             x = sk_X509_value(xs_ctx.chain, i);
1118ebfedea0SLionel Sambuc 
1119*0a6a1f1dSLionel Sambuc             if (!dtls1_add_cert_to_buf(buf, &l, x)) {
1120ebfedea0SLionel Sambuc                 X509_STORE_CTX_cleanup(&xs_ctx);
1121ebfedea0SLionel Sambuc                 return 0;
1122ebfedea0SLionel Sambuc             }
1123ebfedea0SLionel Sambuc         }
1124ebfedea0SLionel Sambuc         X509_STORE_CTX_cleanup(&xs_ctx);
1125ebfedea0SLionel Sambuc     }
1126ebfedea0SLionel Sambuc     /* Thawte special :-) */
1127*0a6a1f1dSLionel Sambuc     for (i = 0; i < sk_X509_num(s->ctx->extra_certs); i++) {
1128ebfedea0SLionel Sambuc         x = sk_X509_value(s->ctx->extra_certs, i);
1129ebfedea0SLionel Sambuc         if (!dtls1_add_cert_to_buf(buf, &l, x))
1130ebfedea0SLionel Sambuc             return 0;
1131ebfedea0SLionel Sambuc     }
1132ebfedea0SLionel Sambuc 
1133ebfedea0SLionel Sambuc     l -= (3 + DTLS1_HM_HEADER_LENGTH);
1134ebfedea0SLionel Sambuc 
1135ebfedea0SLionel Sambuc     p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
1136ebfedea0SLionel Sambuc     l2n3(l, p);
1137ebfedea0SLionel Sambuc     l += 3;
1138ebfedea0SLionel Sambuc     p = (unsigned char *)&(buf->data[0]);
1139ebfedea0SLionel Sambuc     p = dtls1_set_message_header(s, p, SSL3_MT_CERTIFICATE, l, 0, l);
1140ebfedea0SLionel Sambuc 
1141ebfedea0SLionel Sambuc     l += DTLS1_HM_HEADER_LENGTH;
1142ebfedea0SLionel Sambuc     return (l);
1143ebfedea0SLionel Sambuc }
1144ebfedea0SLionel Sambuc 
dtls1_read_failed(SSL * s,int code)1145ebfedea0SLionel Sambuc int dtls1_read_failed(SSL *s, int code)
1146ebfedea0SLionel Sambuc {
1147*0a6a1f1dSLionel Sambuc     if (code > 0) {
1148ebfedea0SLionel Sambuc         fprintf(stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
1149ebfedea0SLionel Sambuc         return 1;
1150ebfedea0SLionel Sambuc     }
1151ebfedea0SLionel Sambuc 
1152*0a6a1f1dSLionel Sambuc     if (!dtls1_is_timer_expired(s)) {
1153*0a6a1f1dSLionel Sambuc         /*
1154*0a6a1f1dSLionel Sambuc          * not a timeout, none of our business, let higher layers handle
1155*0a6a1f1dSLionel Sambuc          * this.  in fact it's probably an error
1156*0a6a1f1dSLionel Sambuc          */
1157ebfedea0SLionel Sambuc         return code;
1158ebfedea0SLionel Sambuc     }
1159ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HEARTBEATS
1160*0a6a1f1dSLionel Sambuc     /* done, no need to send a retransmit */
1161*0a6a1f1dSLionel Sambuc     if (!SSL_in_init(s) && !s->tlsext_hb_pending)
1162ebfedea0SLionel Sambuc #else
1163*0a6a1f1dSLionel Sambuc     /* done, no need to send a retransmit */
1164*0a6a1f1dSLionel Sambuc     if (!SSL_in_init(s))
1165ebfedea0SLionel Sambuc #endif
1166ebfedea0SLionel Sambuc     {
1167ebfedea0SLionel Sambuc         BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
1168ebfedea0SLionel Sambuc         return code;
1169ebfedea0SLionel Sambuc     }
1170*0a6a1f1dSLionel Sambuc #if 0                           /* for now, each alert contains only one
1171*0a6a1f1dSLionel Sambuc                                  * record number */
1172ebfedea0SLionel Sambuc     item = pqueue_peek(state->rcvd_records);
1173*0a6a1f1dSLionel Sambuc     if (item) {
1174ebfedea0SLionel Sambuc         /* send an alert immediately for all the missing records */
1175*0a6a1f1dSLionel Sambuc     } else
1176ebfedea0SLionel Sambuc #endif
1177ebfedea0SLionel Sambuc 
1178*0a6a1f1dSLionel Sambuc #if 0                           /* no more alert sending, just retransmit the
1179*0a6a1f1dSLionel Sambuc                                  * last set of messages */
1180ebfedea0SLionel Sambuc     if (state->timeout.read_timeouts >= DTLS1_TMO_READ_COUNT)
1181ebfedea0SLionel Sambuc         ssl3_send_alert(s, SSL3_AL_WARNING,
1182ebfedea0SLionel Sambuc                         DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
1183ebfedea0SLionel Sambuc #endif
1184ebfedea0SLionel Sambuc 
1185ebfedea0SLionel Sambuc     return dtls1_handle_timeout(s);
1186ebfedea0SLionel Sambuc }
1187ebfedea0SLionel Sambuc 
dtls1_get_queue_priority(unsigned short seq,int is_ccs)1188*0a6a1f1dSLionel Sambuc int dtls1_get_queue_priority(unsigned short seq, int is_ccs)
1189ebfedea0SLionel Sambuc {
1190*0a6a1f1dSLionel Sambuc     /*
1191*0a6a1f1dSLionel Sambuc      * The index of the retransmission queue actually is the message sequence
1192*0a6a1f1dSLionel Sambuc      * number, since the queue only contains messages of a single handshake.
1193*0a6a1f1dSLionel Sambuc      * However, the ChangeCipherSpec has no message sequence number and so
1194*0a6a1f1dSLionel Sambuc      * using only the sequence will result in the CCS and Finished having the
1195*0a6a1f1dSLionel Sambuc      * same index. To prevent this, the sequence number is multiplied by 2.
1196*0a6a1f1dSLionel Sambuc      * In case of a CCS 1 is subtracted. This does not only differ CSS and
1197*0a6a1f1dSLionel Sambuc      * Finished, it also maintains the order of the index (important for
1198*0a6a1f1dSLionel Sambuc      * priority queues) and fits in the unsigned short variable.
1199ebfedea0SLionel Sambuc      */
1200ebfedea0SLionel Sambuc     return seq * 2 - is_ccs;
1201ebfedea0SLionel Sambuc }
1202ebfedea0SLionel Sambuc 
dtls1_retransmit_buffered_messages(SSL * s)1203*0a6a1f1dSLionel Sambuc int dtls1_retransmit_buffered_messages(SSL *s)
1204ebfedea0SLionel Sambuc {
1205ebfedea0SLionel Sambuc     pqueue sent = s->d1->sent_messages;
1206ebfedea0SLionel Sambuc     piterator iter;
1207ebfedea0SLionel Sambuc     pitem *item;
1208ebfedea0SLionel Sambuc     hm_fragment *frag;
1209ebfedea0SLionel Sambuc     int found = 0;
1210ebfedea0SLionel Sambuc 
1211ebfedea0SLionel Sambuc     iter = pqueue_iterator(sent);
1212ebfedea0SLionel Sambuc 
1213*0a6a1f1dSLionel Sambuc     for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
1214ebfedea0SLionel Sambuc         frag = (hm_fragment *)item->data;
1215*0a6a1f1dSLionel Sambuc         if (dtls1_retransmit_message(s, (unsigned short)
1216*0a6a1f1dSLionel Sambuc                                      dtls1_get_queue_priority
1217*0a6a1f1dSLionel Sambuc                                      (frag->msg_header.seq,
1218*0a6a1f1dSLionel Sambuc                                       frag->msg_header.is_ccs), 0,
1219*0a6a1f1dSLionel Sambuc                                      &found) <= 0 && found) {
1220ebfedea0SLionel Sambuc             fprintf(stderr, "dtls1_retransmit_message() failed\n");
1221ebfedea0SLionel Sambuc             return -1;
1222ebfedea0SLionel Sambuc         }
1223ebfedea0SLionel Sambuc     }
1224ebfedea0SLionel Sambuc 
1225ebfedea0SLionel Sambuc     return 1;
1226ebfedea0SLionel Sambuc }
1227ebfedea0SLionel Sambuc 
dtls1_buffer_message(SSL * s,int is_ccs)1228*0a6a1f1dSLionel Sambuc int dtls1_buffer_message(SSL *s, int is_ccs)
1229ebfedea0SLionel Sambuc {
1230ebfedea0SLionel Sambuc     pitem *item;
1231ebfedea0SLionel Sambuc     hm_fragment *frag;
1232ebfedea0SLionel Sambuc     unsigned char seq64be[8];
1233ebfedea0SLionel Sambuc 
1234*0a6a1f1dSLionel Sambuc     /*
1235*0a6a1f1dSLionel Sambuc      * this function is called immediately after a message has been
1236*0a6a1f1dSLionel Sambuc      * serialized
1237*0a6a1f1dSLionel Sambuc      */
1238ebfedea0SLionel Sambuc     OPENSSL_assert(s->init_off == 0);
1239ebfedea0SLionel Sambuc 
1240ebfedea0SLionel Sambuc     frag = dtls1_hm_fragment_new(s->init_num, 0);
1241*0a6a1f1dSLionel Sambuc     if (!frag)
1242*0a6a1f1dSLionel Sambuc         return 0;
1243ebfedea0SLionel Sambuc 
1244ebfedea0SLionel Sambuc     memcpy(frag->fragment, s->init_buf->data, s->init_num);
1245ebfedea0SLionel Sambuc 
1246*0a6a1f1dSLionel Sambuc     if (is_ccs) {
1247ebfedea0SLionel Sambuc         OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
1248*0a6a1f1dSLionel Sambuc                        ((s->version ==
1249*0a6a1f1dSLionel Sambuc                          DTLS1_VERSION) ? DTLS1_CCS_HEADER_LENGTH : 3) ==
1250*0a6a1f1dSLionel Sambuc                        (unsigned int)s->init_num);
1251*0a6a1f1dSLionel Sambuc     } else {
1252ebfedea0SLionel Sambuc         OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
1253ebfedea0SLionel Sambuc                        DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);
1254ebfedea0SLionel Sambuc     }
1255ebfedea0SLionel Sambuc 
1256ebfedea0SLionel Sambuc     frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1257ebfedea0SLionel Sambuc     frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1258ebfedea0SLionel Sambuc     frag->msg_header.type = s->d1->w_msg_hdr.type;
1259ebfedea0SLionel Sambuc     frag->msg_header.frag_off = 0;
1260ebfedea0SLionel Sambuc     frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1261ebfedea0SLionel Sambuc     frag->msg_header.is_ccs = is_ccs;
1262ebfedea0SLionel Sambuc 
1263ebfedea0SLionel Sambuc     /* save current state */
1264ebfedea0SLionel Sambuc     frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
1265ebfedea0SLionel Sambuc     frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
1266ebfedea0SLionel Sambuc     frag->msg_header.saved_retransmit_state.compress = s->compress;
1267ebfedea0SLionel Sambuc     frag->msg_header.saved_retransmit_state.session = s->session;
1268ebfedea0SLionel Sambuc     frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;
1269ebfedea0SLionel Sambuc 
1270ebfedea0SLionel Sambuc     memset(seq64be, 0, sizeof(seq64be));
1271*0a6a1f1dSLionel Sambuc     seq64be[6] =
1272*0a6a1f1dSLionel Sambuc         (unsigned
1273*0a6a1f1dSLionel Sambuc          char)(dtls1_get_queue_priority(frag->msg_header.seq,
1274ebfedea0SLionel Sambuc                                         frag->msg_header.is_ccs) >> 8);
1275*0a6a1f1dSLionel Sambuc     seq64be[7] =
1276*0a6a1f1dSLionel Sambuc         (unsigned
1277*0a6a1f1dSLionel Sambuc          char)(dtls1_get_queue_priority(frag->msg_header.seq,
1278ebfedea0SLionel Sambuc                                         frag->msg_header.is_ccs));
1279ebfedea0SLionel Sambuc 
1280ebfedea0SLionel Sambuc     item = pitem_new(seq64be, frag);
1281*0a6a1f1dSLionel Sambuc     if (item == NULL) {
1282ebfedea0SLionel Sambuc         dtls1_hm_fragment_free(frag);
1283ebfedea0SLionel Sambuc         return 0;
1284ebfedea0SLionel Sambuc     }
1285ebfedea0SLionel Sambuc #if 0
1286ebfedea0SLionel Sambuc     fprintf(stderr, "buffered messge: \ttype = %xx\n", msg_buf->type);
1287ebfedea0SLionel Sambuc     fprintf(stderr, "\t\t\t\t\tlen = %d\n", msg_buf->len);
1288ebfedea0SLionel Sambuc     fprintf(stderr, "\t\t\t\t\tseq_num = %d\n", msg_buf->seq_num);
1289ebfedea0SLionel Sambuc #endif
1290ebfedea0SLionel Sambuc 
1291ebfedea0SLionel Sambuc     pqueue_insert(s->d1->sent_messages, item);
1292ebfedea0SLionel Sambuc     return 1;
1293ebfedea0SLionel Sambuc }
1294ebfedea0SLionel Sambuc 
1295ebfedea0SLionel Sambuc int
dtls1_retransmit_message(SSL * s,unsigned short seq,unsigned long frag_off,int * found)1296ebfedea0SLionel Sambuc dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
1297ebfedea0SLionel Sambuc                          int *found)
1298ebfedea0SLionel Sambuc {
1299ebfedea0SLionel Sambuc     int ret;
1300ebfedea0SLionel Sambuc     /* XDTLS: for now assuming that read/writes are blocking */
1301ebfedea0SLionel Sambuc     pitem *item;
1302ebfedea0SLionel Sambuc     hm_fragment *frag;
1303ebfedea0SLionel Sambuc     unsigned long header_length;
1304ebfedea0SLionel Sambuc     unsigned char seq64be[8];
1305ebfedea0SLionel Sambuc     struct dtls1_retransmit_state saved_state;
1306ebfedea0SLionel Sambuc     unsigned char save_write_sequence[8];
1307ebfedea0SLionel Sambuc 
1308*0a6a1f1dSLionel Sambuc     /*-
1309ebfedea0SLionel Sambuc       OPENSSL_assert(s->init_num == 0);
1310ebfedea0SLionel Sambuc       OPENSSL_assert(s->init_off == 0);
1311ebfedea0SLionel Sambuc      */
1312ebfedea0SLionel Sambuc 
1313ebfedea0SLionel Sambuc     /* XDTLS:  the requested message ought to be found, otherwise error */
1314ebfedea0SLionel Sambuc     memset(seq64be, 0, sizeof(seq64be));
1315ebfedea0SLionel Sambuc     seq64be[6] = (unsigned char)(seq >> 8);
1316ebfedea0SLionel Sambuc     seq64be[7] = (unsigned char)seq;
1317ebfedea0SLionel Sambuc 
1318ebfedea0SLionel Sambuc     item = pqueue_find(s->d1->sent_messages, seq64be);
1319*0a6a1f1dSLionel Sambuc     if (item == NULL) {
1320ebfedea0SLionel Sambuc         fprintf(stderr, "retransmit:  message %d non-existant\n", seq);
1321ebfedea0SLionel Sambuc         *found = 0;
1322ebfedea0SLionel Sambuc         return 0;
1323ebfedea0SLionel Sambuc     }
1324ebfedea0SLionel Sambuc 
1325ebfedea0SLionel Sambuc     *found = 1;
1326ebfedea0SLionel Sambuc     frag = (hm_fragment *)item->data;
1327ebfedea0SLionel Sambuc 
1328ebfedea0SLionel Sambuc     if (frag->msg_header.is_ccs)
1329ebfedea0SLionel Sambuc         header_length = DTLS1_CCS_HEADER_LENGTH;
1330ebfedea0SLionel Sambuc     else
1331ebfedea0SLionel Sambuc         header_length = DTLS1_HM_HEADER_LENGTH;
1332ebfedea0SLionel Sambuc 
1333ebfedea0SLionel Sambuc     memcpy(s->init_buf->data, frag->fragment,
1334ebfedea0SLionel Sambuc            frag->msg_header.msg_len + header_length);
1335ebfedea0SLionel Sambuc     s->init_num = frag->msg_header.msg_len + header_length;
1336ebfedea0SLionel Sambuc 
1337ebfedea0SLionel Sambuc     dtls1_set_message_header_int(s, frag->msg_header.type,
1338*0a6a1f1dSLionel Sambuc                                  frag->msg_header.msg_len,
1339*0a6a1f1dSLionel Sambuc                                  frag->msg_header.seq, 0,
1340ebfedea0SLionel Sambuc                                  frag->msg_header.frag_len);
1341ebfedea0SLionel Sambuc 
1342ebfedea0SLionel Sambuc     /* save current state */
1343ebfedea0SLionel Sambuc     saved_state.enc_write_ctx = s->enc_write_ctx;
1344ebfedea0SLionel Sambuc     saved_state.write_hash = s->write_hash;
1345ebfedea0SLionel Sambuc     saved_state.compress = s->compress;
1346ebfedea0SLionel Sambuc     saved_state.session = s->session;
1347ebfedea0SLionel Sambuc     saved_state.epoch = s->d1->w_epoch;
1348ebfedea0SLionel Sambuc     saved_state.epoch = s->d1->w_epoch;
1349ebfedea0SLionel Sambuc 
1350ebfedea0SLionel Sambuc     s->d1->retransmitting = 1;
1351ebfedea0SLionel Sambuc 
1352ebfedea0SLionel Sambuc     /* restore state in which the message was originally sent */
1353ebfedea0SLionel Sambuc     s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
1354ebfedea0SLionel Sambuc     s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
1355ebfedea0SLionel Sambuc     s->compress = frag->msg_header.saved_retransmit_state.compress;
1356ebfedea0SLionel Sambuc     s->session = frag->msg_header.saved_retransmit_state.session;
1357ebfedea0SLionel Sambuc     s->d1->w_epoch = frag->msg_header.saved_retransmit_state.epoch;
1358ebfedea0SLionel Sambuc 
1359*0a6a1f1dSLionel Sambuc     if (frag->msg_header.saved_retransmit_state.epoch ==
1360*0a6a1f1dSLionel Sambuc         saved_state.epoch - 1) {
1361*0a6a1f1dSLionel Sambuc         memcpy(save_write_sequence, s->s3->write_sequence,
1362*0a6a1f1dSLionel Sambuc                sizeof(s->s3->write_sequence));
1363*0a6a1f1dSLionel Sambuc         memcpy(s->s3->write_sequence, s->d1->last_write_sequence,
1364*0a6a1f1dSLionel Sambuc                sizeof(s->s3->write_sequence));
1365ebfedea0SLionel Sambuc     }
1366ebfedea0SLionel Sambuc 
1367ebfedea0SLionel Sambuc     ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
1368ebfedea0SLionel Sambuc                          SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
1369ebfedea0SLionel Sambuc 
1370ebfedea0SLionel Sambuc     /* restore current state */
1371ebfedea0SLionel Sambuc     s->enc_write_ctx = saved_state.enc_write_ctx;
1372ebfedea0SLionel Sambuc     s->write_hash = saved_state.write_hash;
1373ebfedea0SLionel Sambuc     s->compress = saved_state.compress;
1374ebfedea0SLionel Sambuc     s->session = saved_state.session;
1375ebfedea0SLionel Sambuc     s->d1->w_epoch = saved_state.epoch;
1376ebfedea0SLionel Sambuc 
1377*0a6a1f1dSLionel Sambuc     if (frag->msg_header.saved_retransmit_state.epoch ==
1378*0a6a1f1dSLionel Sambuc         saved_state.epoch - 1) {
1379*0a6a1f1dSLionel Sambuc         memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
1380*0a6a1f1dSLionel Sambuc                sizeof(s->s3->write_sequence));
1381*0a6a1f1dSLionel Sambuc         memcpy(s->s3->write_sequence, save_write_sequence,
1382*0a6a1f1dSLionel Sambuc                sizeof(s->s3->write_sequence));
1383ebfedea0SLionel Sambuc     }
1384ebfedea0SLionel Sambuc 
1385ebfedea0SLionel Sambuc     s->d1->retransmitting = 0;
1386ebfedea0SLionel Sambuc 
1387ebfedea0SLionel Sambuc     (void)BIO_flush(SSL_get_wbio(s));
1388ebfedea0SLionel Sambuc     return ret;
1389ebfedea0SLionel Sambuc }
1390ebfedea0SLionel Sambuc 
1391ebfedea0SLionel Sambuc /* call this function when the buffered messages are no longer needed */
dtls1_clear_record_buffer(SSL * s)1392*0a6a1f1dSLionel Sambuc void dtls1_clear_record_buffer(SSL *s)
1393ebfedea0SLionel Sambuc {
1394ebfedea0SLionel Sambuc     pitem *item;
1395ebfedea0SLionel Sambuc 
1396ebfedea0SLionel Sambuc     for (item = pqueue_pop(s->d1->sent_messages);
1397*0a6a1f1dSLionel Sambuc          item != NULL; item = pqueue_pop(s->d1->sent_messages)) {
1398ebfedea0SLionel Sambuc         dtls1_hm_fragment_free((hm_fragment *)item->data);
1399ebfedea0SLionel Sambuc         pitem_free(item);
1400ebfedea0SLionel Sambuc     }
1401ebfedea0SLionel Sambuc }
1402ebfedea0SLionel Sambuc 
dtls1_set_message_header(SSL * s,unsigned char * p,unsigned char mt,unsigned long len,unsigned long frag_off,unsigned long frag_len)1403*0a6a1f1dSLionel Sambuc unsigned char *dtls1_set_message_header(SSL *s, unsigned char *p,
1404*0a6a1f1dSLionel Sambuc                                         unsigned char mt, unsigned long len,
1405*0a6a1f1dSLionel Sambuc                                         unsigned long frag_off,
1406*0a6a1f1dSLionel Sambuc                                         unsigned long frag_len)
1407ebfedea0SLionel Sambuc {
1408ebfedea0SLionel Sambuc     /* Don't change sequence numbers while listening */
1409*0a6a1f1dSLionel Sambuc     if (frag_off == 0 && !s->d1->listen) {
1410ebfedea0SLionel Sambuc         s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1411ebfedea0SLionel Sambuc         s->d1->next_handshake_write_seq++;
1412ebfedea0SLionel Sambuc     }
1413ebfedea0SLionel Sambuc 
1414ebfedea0SLionel Sambuc     dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
1415ebfedea0SLionel Sambuc                                  frag_off, frag_len);
1416ebfedea0SLionel Sambuc 
1417ebfedea0SLionel Sambuc     return p += DTLS1_HM_HEADER_LENGTH;
1418ebfedea0SLionel Sambuc }
1419ebfedea0SLionel Sambuc 
1420ebfedea0SLionel Sambuc /* don't actually do the writing, wait till the MTU has been retrieved */
1421ebfedea0SLionel Sambuc 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)1422ebfedea0SLionel Sambuc dtls1_set_message_header_int(SSL *s, unsigned char mt,
1423*0a6a1f1dSLionel Sambuc                              unsigned long len, unsigned short seq_num,
1424*0a6a1f1dSLionel Sambuc                              unsigned long frag_off, unsigned long frag_len)
1425ebfedea0SLionel Sambuc {
1426ebfedea0SLionel Sambuc     struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1427ebfedea0SLionel Sambuc 
1428ebfedea0SLionel Sambuc     msg_hdr->type = mt;
1429ebfedea0SLionel Sambuc     msg_hdr->msg_len = len;
1430ebfedea0SLionel Sambuc     msg_hdr->seq = seq_num;
1431ebfedea0SLionel Sambuc     msg_hdr->frag_off = frag_off;
1432ebfedea0SLionel Sambuc     msg_hdr->frag_len = frag_len;
1433ebfedea0SLionel Sambuc }
1434ebfedea0SLionel Sambuc 
1435ebfedea0SLionel Sambuc static void
dtls1_fix_message_header(SSL * s,unsigned long frag_off,unsigned long frag_len)1436ebfedea0SLionel Sambuc dtls1_fix_message_header(SSL *s, unsigned long frag_off,
1437ebfedea0SLionel Sambuc                          unsigned long frag_len)
1438ebfedea0SLionel Sambuc {
1439ebfedea0SLionel Sambuc     struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1440ebfedea0SLionel Sambuc 
1441ebfedea0SLionel Sambuc     msg_hdr->frag_off = frag_off;
1442ebfedea0SLionel Sambuc     msg_hdr->frag_len = frag_len;
1443ebfedea0SLionel Sambuc }
1444ebfedea0SLionel Sambuc 
dtls1_write_message_header(SSL * s,unsigned char * p)1445*0a6a1f1dSLionel Sambuc static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p)
1446ebfedea0SLionel Sambuc {
1447ebfedea0SLionel Sambuc     struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1448ebfedea0SLionel Sambuc 
1449ebfedea0SLionel Sambuc     *p++ = msg_hdr->type;
1450ebfedea0SLionel Sambuc     l2n3(msg_hdr->msg_len, p);
1451ebfedea0SLionel Sambuc 
1452ebfedea0SLionel Sambuc     s2n(msg_hdr->seq, p);
1453ebfedea0SLionel Sambuc     l2n3(msg_hdr->frag_off, p);
1454ebfedea0SLionel Sambuc     l2n3(msg_hdr->frag_len, p);
1455ebfedea0SLionel Sambuc 
1456ebfedea0SLionel Sambuc     return p;
1457ebfedea0SLionel Sambuc }
1458ebfedea0SLionel Sambuc 
dtls1_link_min_mtu(void)1459*0a6a1f1dSLionel Sambuc unsigned int dtls1_link_min_mtu(void)
1460ebfedea0SLionel Sambuc {
1461ebfedea0SLionel Sambuc     return (g_probable_mtu[(sizeof(g_probable_mtu) /
1462ebfedea0SLionel Sambuc                             sizeof(g_probable_mtu[0])) - 1]);
1463ebfedea0SLionel Sambuc }
1464ebfedea0SLionel Sambuc 
dtls1_min_mtu(SSL * s)1465*0a6a1f1dSLionel Sambuc unsigned int dtls1_min_mtu(SSL *s)
1466ebfedea0SLionel Sambuc {
1467*0a6a1f1dSLionel Sambuc     return dtls1_link_min_mtu() - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s));
1468ebfedea0SLionel Sambuc }
1469ebfedea0SLionel Sambuc 
1470ebfedea0SLionel Sambuc void
dtls1_get_message_header(unsigned char * data,struct hm_header_st * msg_hdr)1471ebfedea0SLionel Sambuc dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
1472ebfedea0SLionel Sambuc {
1473ebfedea0SLionel Sambuc     memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
1474ebfedea0SLionel Sambuc     msg_hdr->type = *(data++);
1475ebfedea0SLionel Sambuc     n2l3(data, msg_hdr->msg_len);
1476ebfedea0SLionel Sambuc 
1477ebfedea0SLionel Sambuc     n2s(data, msg_hdr->seq);
1478ebfedea0SLionel Sambuc     n2l3(data, msg_hdr->frag_off);
1479ebfedea0SLionel Sambuc     n2l3(data, msg_hdr->frag_len);
1480ebfedea0SLionel Sambuc }
1481ebfedea0SLionel Sambuc 
dtls1_get_ccs_header(unsigned char * data,struct ccs_header_st * ccs_hdr)1482*0a6a1f1dSLionel Sambuc void dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr)
1483ebfedea0SLionel Sambuc {
1484ebfedea0SLionel Sambuc     memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
1485ebfedea0SLionel Sambuc 
1486ebfedea0SLionel Sambuc     ccs_hdr->type = *(data++);
1487ebfedea0SLionel Sambuc }
1488ebfedea0SLionel Sambuc 
dtls1_shutdown(SSL * s)1489ebfedea0SLionel Sambuc int dtls1_shutdown(SSL *s)
1490ebfedea0SLionel Sambuc {
1491ebfedea0SLionel Sambuc     int ret;
1492ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
1493ebfedea0SLionel Sambuc     if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
1494*0a6a1f1dSLionel Sambuc         !(s->shutdown & SSL_SENT_SHUTDOWN)) {
1495ebfedea0SLionel Sambuc         ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
1496*0a6a1f1dSLionel Sambuc         if (ret < 0)
1497*0a6a1f1dSLionel Sambuc             return -1;
1498ebfedea0SLionel Sambuc 
1499ebfedea0SLionel Sambuc         if (ret == 0)
1500*0a6a1f1dSLionel Sambuc             BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 1,
1501*0a6a1f1dSLionel Sambuc                      NULL);
1502ebfedea0SLionel Sambuc     }
1503ebfedea0SLionel Sambuc #endif
1504ebfedea0SLionel Sambuc     ret = ssl3_shutdown(s);
1505ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SCTP
1506ebfedea0SLionel Sambuc     BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 0, NULL);
1507ebfedea0SLionel Sambuc #endif
1508ebfedea0SLionel Sambuc     return ret;
1509ebfedea0SLionel Sambuc }
1510ebfedea0SLionel Sambuc 
1511ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HEARTBEATS
dtls1_process_heartbeat(SSL * s)1512*0a6a1f1dSLionel Sambuc int dtls1_process_heartbeat(SSL *s)
1513ebfedea0SLionel Sambuc {
1514ebfedea0SLionel Sambuc     unsigned char *p = &s->s3->rrec.data[0], *pl;
1515ebfedea0SLionel Sambuc     unsigned short hbtype;
1516ebfedea0SLionel Sambuc     unsigned int payload;
1517ebfedea0SLionel Sambuc     unsigned int padding = 16;  /* Use minimum padding */
1518ebfedea0SLionel Sambuc 
1519ebfedea0SLionel Sambuc     if (s->msg_callback)
1520ebfedea0SLionel Sambuc         s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
1521ebfedea0SLionel Sambuc                         &s->s3->rrec.data[0], s->s3->rrec.length,
1522ebfedea0SLionel Sambuc                         s, s->msg_callback_arg);
1523ebfedea0SLionel Sambuc 
1524*0a6a1f1dSLionel Sambuc     /* Read type and payload length first */
1525*0a6a1f1dSLionel Sambuc     if (1 + 2 + 16 > s->s3->rrec.length)
1526*0a6a1f1dSLionel Sambuc         return 0;               /* silently discard */
1527*0a6a1f1dSLionel Sambuc     if (s->s3->rrec.length > SSL3_RT_MAX_PLAIN_LENGTH)
1528*0a6a1f1dSLionel Sambuc         return 0;               /* silently discard per RFC 6520 sec. 4 */
1529*0a6a1f1dSLionel Sambuc 
1530*0a6a1f1dSLionel Sambuc     hbtype = *p++;
1531*0a6a1f1dSLionel Sambuc     n2s(p, payload);
1532*0a6a1f1dSLionel Sambuc     if (1 + 2 + payload + 16 > s->s3->rrec.length)
1533*0a6a1f1dSLionel Sambuc         return 0;               /* silently discard per RFC 6520 sec. 4 */
1534*0a6a1f1dSLionel Sambuc     pl = p;
1535*0a6a1f1dSLionel Sambuc 
1536*0a6a1f1dSLionel Sambuc     if (hbtype == TLS1_HB_REQUEST) {
1537ebfedea0SLionel Sambuc         unsigned char *buffer, *bp;
1538*0a6a1f1dSLionel Sambuc         unsigned int write_length = 1 /* heartbeat type */  +
1539*0a6a1f1dSLionel Sambuc             2 /* heartbeat length */  +
1540*0a6a1f1dSLionel Sambuc             payload + padding;
1541ebfedea0SLionel Sambuc         int r;
1542ebfedea0SLionel Sambuc 
1543*0a6a1f1dSLionel Sambuc         if (write_length > SSL3_RT_MAX_PLAIN_LENGTH)
1544*0a6a1f1dSLionel Sambuc             return 0;
1545*0a6a1f1dSLionel Sambuc 
1546*0a6a1f1dSLionel Sambuc         /*
1547*0a6a1f1dSLionel Sambuc          * Allocate memory for the response, size is 1 byte message type,
1548*0a6a1f1dSLionel Sambuc          * plus 2 bytes payload length, plus payload, plus padding
1549ebfedea0SLionel Sambuc          */
1550*0a6a1f1dSLionel Sambuc         buffer = OPENSSL_malloc(write_length);
1551ebfedea0SLionel Sambuc         bp = buffer;
1552ebfedea0SLionel Sambuc 
1553ebfedea0SLionel Sambuc         /* Enter response type, length and copy payload */
1554ebfedea0SLionel Sambuc         *bp++ = TLS1_HB_RESPONSE;
1555ebfedea0SLionel Sambuc         s2n(payload, bp);
1556ebfedea0SLionel Sambuc         memcpy(bp, pl, payload);
1557ebfedea0SLionel Sambuc         bp += payload;
1558ebfedea0SLionel Sambuc         /* Random padding */
1559*0a6a1f1dSLionel Sambuc         if (RAND_pseudo_bytes(bp, padding) < 0) {
1560*0a6a1f1dSLionel Sambuc             OPENSSL_free(buffer);
1561*0a6a1f1dSLionel Sambuc             return -1;
1562*0a6a1f1dSLionel Sambuc         }
1563ebfedea0SLionel Sambuc 
1564*0a6a1f1dSLionel Sambuc         r = dtls1_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, write_length);
1565ebfedea0SLionel Sambuc 
1566ebfedea0SLionel Sambuc         if (r >= 0 && s->msg_callback)
1567ebfedea0SLionel Sambuc             s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
1568*0a6a1f1dSLionel Sambuc                             buffer, write_length, s, s->msg_callback_arg);
1569ebfedea0SLionel Sambuc 
1570ebfedea0SLionel Sambuc         OPENSSL_free(buffer);
1571ebfedea0SLionel Sambuc 
1572ebfedea0SLionel Sambuc         if (r < 0)
1573ebfedea0SLionel Sambuc             return r;
1574*0a6a1f1dSLionel Sambuc     } else if (hbtype == TLS1_HB_RESPONSE) {
1575ebfedea0SLionel Sambuc         unsigned int seq;
1576ebfedea0SLionel Sambuc 
1577*0a6a1f1dSLionel Sambuc         /*
1578*0a6a1f1dSLionel Sambuc          * We only send sequence numbers (2 bytes unsigned int), and 16
1579*0a6a1f1dSLionel Sambuc          * random bytes, so we just try to read the sequence number
1580*0a6a1f1dSLionel Sambuc          */
1581ebfedea0SLionel Sambuc         n2s(pl, seq);
1582ebfedea0SLionel Sambuc 
1583*0a6a1f1dSLionel Sambuc         if (payload == 18 && seq == s->tlsext_hb_seq) {
1584ebfedea0SLionel Sambuc             dtls1_stop_timer(s);
1585ebfedea0SLionel Sambuc             s->tlsext_hb_seq++;
1586ebfedea0SLionel Sambuc             s->tlsext_hb_pending = 0;
1587ebfedea0SLionel Sambuc         }
1588ebfedea0SLionel Sambuc     }
1589ebfedea0SLionel Sambuc 
1590ebfedea0SLionel Sambuc     return 0;
1591ebfedea0SLionel Sambuc }
1592ebfedea0SLionel Sambuc 
dtls1_heartbeat(SSL * s)1593*0a6a1f1dSLionel Sambuc int dtls1_heartbeat(SSL *s)
1594ebfedea0SLionel Sambuc {
1595ebfedea0SLionel Sambuc     unsigned char *buf, *p;
1596*0a6a1f1dSLionel Sambuc     int ret = -1;
1597ebfedea0SLionel Sambuc     unsigned int payload = 18;  /* Sequence number + random bytes */
1598ebfedea0SLionel Sambuc     unsigned int padding = 16;  /* Use minimum padding */
1599ebfedea0SLionel Sambuc 
1600ebfedea0SLionel Sambuc     /* Only send if peer supports and accepts HB requests... */
1601ebfedea0SLionel Sambuc     if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
1602*0a6a1f1dSLionel Sambuc         s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS) {
1603ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
1604ebfedea0SLionel Sambuc         return -1;
1605ebfedea0SLionel Sambuc     }
1606ebfedea0SLionel Sambuc 
1607ebfedea0SLionel Sambuc     /* ...and there is none in flight yet... */
1608*0a6a1f1dSLionel Sambuc     if (s->tlsext_hb_pending) {
1609ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PENDING);
1610ebfedea0SLionel Sambuc         return -1;
1611ebfedea0SLionel Sambuc     }
1612ebfedea0SLionel Sambuc 
1613ebfedea0SLionel Sambuc     /* ...and no handshake in progress. */
1614*0a6a1f1dSLionel Sambuc     if (SSL_in_init(s) || s->in_handshake) {
1615ebfedea0SLionel Sambuc         SSLerr(SSL_F_DTLS1_HEARTBEAT, SSL_R_UNEXPECTED_MESSAGE);
1616ebfedea0SLionel Sambuc         return -1;
1617ebfedea0SLionel Sambuc     }
1618ebfedea0SLionel Sambuc 
1619*0a6a1f1dSLionel Sambuc     /*
1620*0a6a1f1dSLionel Sambuc      * Check if padding is too long, payload and padding must not exceed 2^14
1621*0a6a1f1dSLionel Sambuc      * - 3 = 16381 bytes in total.
1622ebfedea0SLionel Sambuc      */
1623ebfedea0SLionel Sambuc     OPENSSL_assert(payload + padding <= 16381);
1624ebfedea0SLionel Sambuc 
1625*0a6a1f1dSLionel Sambuc     /*-
1626*0a6a1f1dSLionel Sambuc      * Create HeartBeat message, we just use a sequence number
1627ebfedea0SLionel Sambuc      * as payload to distuingish different messages and add
1628ebfedea0SLionel Sambuc      * some random stuff.
1629ebfedea0SLionel Sambuc      *  - Message Type, 1 byte
1630ebfedea0SLionel Sambuc      *  - Payload Length, 2 bytes (unsigned int)
1631ebfedea0SLionel Sambuc      *  - Payload, the sequence number (2 bytes uint)
1632ebfedea0SLionel Sambuc      *  - Payload, random bytes (16 bytes uint)
1633ebfedea0SLionel Sambuc      *  - Padding
1634ebfedea0SLionel Sambuc      */
1635ebfedea0SLionel Sambuc     buf = OPENSSL_malloc(1 + 2 + payload + padding);
1636ebfedea0SLionel Sambuc     p = buf;
1637ebfedea0SLionel Sambuc     /* Message Type */
1638ebfedea0SLionel Sambuc     *p++ = TLS1_HB_REQUEST;
1639ebfedea0SLionel Sambuc     /* Payload length (18 bytes here) */
1640ebfedea0SLionel Sambuc     s2n(payload, p);
1641ebfedea0SLionel Sambuc     /* Sequence number */
1642ebfedea0SLionel Sambuc     s2n(s->tlsext_hb_seq, p);
1643ebfedea0SLionel Sambuc     /* 16 random bytes */
1644*0a6a1f1dSLionel Sambuc     if (RAND_pseudo_bytes(p, 16) < 0)
1645*0a6a1f1dSLionel Sambuc         goto err;
1646ebfedea0SLionel Sambuc     p += 16;
1647ebfedea0SLionel Sambuc     /* Random padding */
1648*0a6a1f1dSLionel Sambuc     if (RAND_pseudo_bytes(p, padding) < 0)
1649*0a6a1f1dSLionel Sambuc         goto err;
1650ebfedea0SLionel Sambuc 
1651ebfedea0SLionel Sambuc     ret = dtls1_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
1652*0a6a1f1dSLionel Sambuc     if (ret >= 0) {
1653ebfedea0SLionel Sambuc         if (s->msg_callback)
1654ebfedea0SLionel Sambuc             s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
1655ebfedea0SLionel Sambuc                             buf, 3 + payload + padding,
1656ebfedea0SLionel Sambuc                             s, s->msg_callback_arg);
1657ebfedea0SLionel Sambuc 
1658ebfedea0SLionel Sambuc         dtls1_start_timer(s);
1659ebfedea0SLionel Sambuc         s->tlsext_hb_pending = 1;
1660ebfedea0SLionel Sambuc     }
1661ebfedea0SLionel Sambuc 
1662*0a6a1f1dSLionel Sambuc err:
1663ebfedea0SLionel Sambuc     OPENSSL_free(buf);
1664ebfedea0SLionel Sambuc 
1665ebfedea0SLionel Sambuc     return ret;
1666ebfedea0SLionel Sambuc }
1667ebfedea0SLionel Sambuc #endif
1668