]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/statem_dtls.c
Cleanup: fix all sources that used EVP_MD_CTX_(create|init|destroy)
[thirdparty/openssl.git] / ssl / statem / statem_dtls.c
CommitLineData
8ba708e5 1/* ssl/statem/statem_dtls.c */
0f113f3e 2/*
36d16f8e 3 * DTLS implementation written by Nagendra Modadugu
0f113f3e 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
36d16f8e
BL
5 */
6/* ====================================================================
7 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
0f113f3e 14 * notice, this list of conditions and the following disclaimer.
36d16f8e
BL
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
90acf770
AP
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
36d16f8e
BL
20 *
21 * 3. All advertising materials mentioning features or use of this
90acf770
AP
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
36d16f8e
BL
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
90acf770
AP
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@openssl.org.
36d16f8e
BL
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
90acf770
AP
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
36d16f8e
BL
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
90acf770
AP
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36d16f8e
BL
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60 * All rights reserved.
61 *
62 * This package is an SSL implementation written
63 * by Eric Young (eay@cryptsoft.com).
64 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 65 *
36d16f8e
BL
66 * This library is free for commercial and non-commercial use as long as
67 * the following conditions are aheared to. The following conditions
68 * apply to all code found in this distribution, be it the RC4, RSA,
69 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
70 * included with this distribution is covered by the same copyright terms
71 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 72 *
36d16f8e
BL
73 * Copyright remains Eric Young's, and as such any Copyright notices in
74 * the code are not to be removed.
75 * If this package is used in a product, Eric Young should be given attribution
76 * as the author of the parts of the library used.
77 * This can be in the form of a textual message at program startup or
78 * in documentation (online or textual) provided with the package.
0f113f3e 79 *
36d16f8e
BL
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
82 * are met:
83 * 1. Redistributions of source code must retain the copyright
90acf770 84 * notice, this list of conditions and the following disclaimer.
36d16f8e 85 * 2. Redistributions in binary form must reproduce the above copyright
90acf770
AP
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
36d16f8e 88 * 3. All advertising materials mentioning features or use of this software
90acf770
AP
89 * must display the following acknowledgement:
90 * "This product includes cryptographic software written by
91 * Eric Young (eay@cryptsoft.com)"
92 * The word 'cryptographic' can be left out if the rouines from the library
93 * being used are not cryptographic related :-).
0f113f3e 94 * 4. If you include any Windows specific code (or a derivative thereof) from
90acf770
AP
95 * the apps directory (application code) you must include an acknowledgement:
96 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 97 *
36d16f8e
BL
98 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 * SUCH DAMAGE.
0f113f3e 109 *
36d16f8e
BL
110 * The licence and distribution terms for any publically available version or
111 * derivative of this code cannot be changed. i.e. this code cannot simply be
112 * copied and put under another distribution licence
113 * [including the GNU Public Licence.]
114 */
115
116#include <limits.h>
117#include <string.h>
118#include <stdio.h>
8ba708e5 119#include "../ssl_locl.h"
61ae935a 120#include "statem_locl.h"
36d16f8e
BL
121#include <openssl/buffer.h>
122#include <openssl/rand.h>
123#include <openssl/objects.h>
124#include <openssl/evp.h>
125#include <openssl/x509.h>
126
934e22e8
DSH
127#define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
128
129#define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
0f113f3e
MC
130 if ((end) - (start) <= 8) { \
131 long ii; \
132 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
133 } else { \
134 long ii; \
135 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
136 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
137 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
138 } }
934e22e8
DSH
139
140#define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
0f113f3e
MC
141 long ii; \
142 OPENSSL_assert((msg_len) > 0); \
143 is_complete = 1; \
144 if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
145 if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
146 if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
934e22e8 147
0f113f3e
MC
148static unsigned char bitmask_start_values[] =
149 { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 };
150static unsigned char bitmask_end_values[] =
151 { 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f };
36d16f8e 152
0f113f3e
MC
153static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
154 unsigned long frag_len);
155static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p);
36d16f8e 156static void dtls1_set_message_header_int(SSL *s, unsigned char mt,
0f113f3e
MC
157 unsigned long len,
158 unsigned short seq_num,
159 unsigned long frag_off,
160 unsigned long frag_len);
76af3037 161static int dtls_get_reassembled_message(SSL *s, long *len);
0f113f3e
MC
162
163static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len,
164 int reassembly)
165{
166 hm_fragment *frag = NULL;
167 unsigned char *buf = NULL;
168 unsigned char *bitmask = NULL;
169
b4faea50 170 frag = OPENSSL_malloc(sizeof(*frag));
0f113f3e
MC
171 if (frag == NULL)
172 return NULL;
173
174 if (frag_len) {
b196e7d9 175 buf = OPENSSL_malloc(frag_len);
0f113f3e
MC
176 if (buf == NULL) {
177 OPENSSL_free(frag);
178 return NULL;
179 }
180 }
181
182 /* zero length fragment gets zero frag->fragment */
183 frag->fragment = buf;
184
185 /* Initialize reassembly bitmask if necessary */
186 if (reassembly) {
b51bce94 187 bitmask = OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len));
0f113f3e 188 if (bitmask == NULL) {
b548a1f1 189 OPENSSL_free(buf);
0f113f3e
MC
190 OPENSSL_free(frag);
191 return NULL;
192 }
0f113f3e
MC
193 }
194
195 frag->reassembly = bitmask;
196
197 return frag;
198}
36d16f8e 199
8a35dbb6 200void dtls1_hm_fragment_free(hm_fragment *frag)
0f113f3e 201{
25aaa98a
RS
202 if (!frag)
203 return;
0f113f3e
MC
204 if (frag->msg_header.is_ccs) {
205 EVP_CIPHER_CTX_free(frag->msg_header.
206 saved_retransmit_state.enc_write_ctx);
bfb0641f 207 EVP_MD_CTX_free(frag->msg_header.saved_retransmit_state.write_hash);
0f113f3e 208 }
b548a1f1
RS
209 OPENSSL_free(frag->fragment);
210 OPENSSL_free(frag->reassembly);
0f113f3e
MC
211 OPENSSL_free(frag);
212}
36d16f8e 213
0f113f3e
MC
214/*
215 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
216 * SSL3_RT_CHANGE_CIPHER_SPEC)
217 */
6abb0d1f 218int dtls1_do_write(SSL *s, int type)
0f113f3e
MC
219{
220 int ret;
221 unsigned int curr_mtu;
222 int retry = 1;
223 unsigned int len, frag_off, mac_size, blocksize, used_len;
224
225 if (!dtls1_query_mtu(s))
226 return -1;
227
228 OPENSSL_assert(s->d1->mtu >= dtls1_min_mtu(s)); /* should have something
229 * reasonable now */
230
231 if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE)
232 OPENSSL_assert(s->init_num ==
233 (int)s->d1->w_msg_hdr.msg_len +
234 DTLS1_HM_HEADER_LENGTH);
235
236 if (s->write_hash) {
237 if (s->enc_write_ctx
04dc8b36
DSH
238 && ((EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_GCM_MODE) ||
239 (EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_CCM_MODE)))
0f113f3e
MC
240 mac_size = 0;
241 else
242 mac_size = EVP_MD_CTX_size(s->write_hash);
243 } else
244 mac_size = 0;
245
246 if (s->enc_write_ctx &&
247 (EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_CBC_MODE))
248 blocksize = 2 * EVP_CIPHER_block_size(s->enc_write_ctx->cipher);
249 else
250 blocksize = 0;
251
252 frag_off = 0;
253 /* s->init_num shouldn't ever be < 0...but just in case */
254 while (s->init_num > 0) {
255 used_len = BIO_wpending(SSL_get_wbio(s)) + DTLS1_RT_HEADER_LENGTH
256 + mac_size + blocksize;
257 if (s->d1->mtu > used_len)
258 curr_mtu = s->d1->mtu - used_len;
259 else
260 curr_mtu = 0;
261
262 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
263 /*
264 * grr.. we could get an error if MTU picked was wrong
265 */
266 ret = BIO_flush(SSL_get_wbio(s));
267 if (ret <= 0)
268 return ret;
269 used_len = DTLS1_RT_HEADER_LENGTH + mac_size + blocksize;
270 if (s->d1->mtu > used_len + DTLS1_HM_HEADER_LENGTH) {
271 curr_mtu = s->d1->mtu - used_len;
272 } else {
273 /* Shouldn't happen */
274 return -1;
275 }
276 }
277
278 /*
279 * We just checked that s->init_num > 0 so this cast should be safe
280 */
281 if (((unsigned int)s->init_num) > curr_mtu)
282 len = curr_mtu;
283 else
284 len = s->init_num;
285
286 /* Shouldn't ever happen */
287 if (len > INT_MAX)
288 len = INT_MAX;
289
290 /*
291 * XDTLS: this function is too long. split out the CCS part
292 */
293 if (type == SSL3_RT_HANDSHAKE) {
294 if (s->init_off != 0) {
295 OPENSSL_assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
296 s->init_off -= DTLS1_HM_HEADER_LENGTH;
297 s->init_num += DTLS1_HM_HEADER_LENGTH;
298
299 /*
300 * We just checked that s->init_num > 0 so this cast should
301 * be safe
302 */
303 if (((unsigned int)s->init_num) > curr_mtu)
304 len = curr_mtu;
305 else
306 len = s->init_num;
307 }
308
309 /* Shouldn't ever happen */
310 if (len > INT_MAX)
311 len = INT_MAX;
312
313 if (len < DTLS1_HM_HEADER_LENGTH) {
314 /*
315 * len is so small that we really can't do anything sensible
316 * so fail
317 */
318 return -1;
319 }
320 dtls1_fix_message_header(s, frag_off,
321 len - DTLS1_HM_HEADER_LENGTH);
322
323 dtls1_write_message_header(s,
324 (unsigned char *)&s->init_buf->
325 data[s->init_off]);
326 }
327
328 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off],
329 len);
330 if (ret < 0) {
331 /*
332 * might need to update MTU here, but we don't know which
333 * previous packet caused the failure -- so can't really
334 * retransmit anything. continue as if everything is fine and
335 * wait for an alert to handle the retransmit
336 */
337 if (retry && BIO_ctrl(SSL_get_wbio(s),
338 BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {
339 if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
340 if (!dtls1_query_mtu(s))
341 return -1;
342 /* Have one more go */
343 retry = 0;
344 } else
345 return -1;
346 } else {
347 return (-1);
348 }
349 } else {
350
351 /*
352 * bad if this assert fails, only part of the handshake message
353 * got sent. but why would this happen?
354 */
355 OPENSSL_assert(len == (unsigned int)ret);
356
357 if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) {
358 /*
359 * should not be done for 'Hello Request's, but in that case
360 * we'll ignore the result anyway
361 */
362 unsigned char *p =
363 (unsigned char *)&s->init_buf->data[s->init_off];
364 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
365 int xlen;
366
367 if (frag_off == 0 && s->version != DTLS1_BAD_VER) {
368 /*
369 * reconstruct message header is if it is being sent in
370 * single fragment
371 */
372 *p++ = msg_hdr->type;
373 l2n3(msg_hdr->msg_len, p);
374 s2n(msg_hdr->seq, p);
375 l2n3(0, p);
376 l2n3(msg_hdr->msg_len, p);
377 p -= DTLS1_HM_HEADER_LENGTH;
378 xlen = ret;
379 } else {
380 p += DTLS1_HM_HEADER_LENGTH;
381 xlen = ret - DTLS1_HM_HEADER_LENGTH;
382 }
383
384 ssl3_finish_mac(s, p, xlen);
385 }
386
387 if (ret == s->init_num) {
388 if (s->msg_callback)
389 s->msg_callback(1, s->version, type, s->init_buf->data,
390 (size_t)(s->init_off + s->init_num), s,
391 s->msg_callback_arg);
392
393 s->init_off = 0; /* done writing this message */
394 s->init_num = 0;
395
396 return (1);
397 }
398 s->init_off += ret;
399 s->init_num -= ret;
400 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
401 }
402 }
403 return (0);
404}
405
76af3037
MC
406int dtls_get_message(SSL *s, int *mt, unsigned long *len)
407{
408 struct hm_header_st *msg_hdr;
409 unsigned char *p;
410 unsigned long msg_len;
411 int ok;
412 long tmplen;
413
414 msg_hdr = &s->d1->r_msg_hdr;
415 memset(msg_hdr, 0, sizeof(*msg_hdr));
416
417 again:
418 ok = dtls_get_reassembled_message(s, &tmplen);
419 if (tmplen == DTLS1_HM_BAD_FRAGMENT
420 || tmplen == DTLS1_HM_FRAGMENT_RETRY) {
421 /* bad fragment received */
422 goto again;
423 } else if (tmplen <= 0 && !ok) {
424 return 0;
425 }
426
427 *mt = s->s3->tmp.message_type;
428
429 p = (unsigned char *)s->init_buf->data;
430
431 if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
432 if (s->msg_callback) {
433 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
434 p, 1, s, s->msg_callback_arg);
435 }
436 /*
437 * This isn't a real handshake message so skip the processing below.
438 */
473483d4 439 *len = (unsigned long)tmplen;
76af3037
MC
440 return 1;
441 }
442
443 msg_len = msg_hdr->msg_len;
444
445 /* reconstruct message header */
446 *(p++) = msg_hdr->type;
447 l2n3(msg_len, p);
448 s2n(msg_hdr->seq, p);
449 l2n3(0, p);
450 l2n3(msg_len, p);
451 if (s->version != DTLS1_BAD_VER) {
452 p -= DTLS1_HM_HEADER_LENGTH;
453 msg_len += DTLS1_HM_HEADER_LENGTH;
454 }
455
456 ssl3_finish_mac(s, p, msg_len);
457 if (s->msg_callback)
458 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
459 p, msg_len, s, s->msg_callback_arg);
460
461 memset(msg_hdr, 0, sizeof(*msg_hdr));
462
463 s->d1->handshake_read_seq++;
464
465
466 s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
467 *len = s->init_num;
468
469 return 1;
470}
471
472static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr)
0f113f3e
MC
473{
474 size_t frag_off, frag_len, msg_len;
475
476 msg_len = msg_hdr->msg_len;
477 frag_off = msg_hdr->frag_off;
478 frag_len = msg_hdr->frag_len;
479
480 /* sanity checking */
481 if ((frag_off + frag_len) > msg_len) {
482 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, SSL_R_EXCESSIVE_MESSAGE_SIZE);
483 return SSL_AD_ILLEGAL_PARAMETER;
484 }
485
0f113f3e
MC
486 if (s->d1->r_msg_hdr.frag_off == 0) { /* first fragment */
487 /*
488 * msg_len is limited to 2^24, but is effectively checked against max
489 * above
490 */
491 if (!BUF_MEM_grow_clean
492 (s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
493 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, ERR_R_BUF_LIB);
494 return SSL_AD_INTERNAL_ERROR;
495 }
496
497 s->s3->tmp.message_size = msg_len;
498 s->d1->r_msg_hdr.msg_len = msg_len;
499 s->s3->tmp.message_type = msg_hdr->type;
500 s->d1->r_msg_hdr.type = msg_hdr->type;
501 s->d1->r_msg_hdr.seq = msg_hdr->seq;
502 } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
503 /*
504 * They must be playing with us! BTW, failure to enforce upper limit
505 * would open possibility for buffer overrun.
506 */
507 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, SSL_R_EXCESSIVE_MESSAGE_SIZE);
508 return SSL_AD_ILLEGAL_PARAMETER;
509 }
510
511 return 0; /* no error */
512}
90acf770 513
76af3037 514static int dtls1_retrieve_buffered_fragment(SSL *s, int *ok)
0f113f3e 515{
50e735f9
MC
516 /*-
517 * (0) check whether the desired fragment is available
518 * if so:
519 * (1) copy over the fragment to s->init_buf->data[]
520 * (2) update s->init_num
521 */
0f113f3e
MC
522 pitem *item;
523 hm_fragment *frag;
524 int al;
525
526 *ok = 0;
527 item = pqueue_peek(s->d1->buffered_messages);
528 if (item == NULL)
529 return 0;
530
531 frag = (hm_fragment *)item->data;
532
533 /* Don't return if reassembly still in progress */
534 if (frag->reassembly != NULL)
535 return 0;
536
537 if (s->d1->handshake_read_seq == frag->msg_header.seq) {
538 unsigned long frag_len = frag->msg_header.frag_len;
539 pqueue_pop(s->d1->buffered_messages);
540
76af3037 541 al = dtls1_preprocess_fragment(s, &frag->msg_header);
0f113f3e
MC
542
543 if (al == 0) { /* no alert */
544 unsigned char *p =
545 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
546 memcpy(&p[frag->msg_header.frag_off], frag->fragment,
547 frag->msg_header.frag_len);
548 }
549
550 dtls1_hm_fragment_free(frag);
551 pitem_free(item);
552
553 if (al == 0) {
554 *ok = 1;
555 return frag_len;
556 }
557
558 ssl3_send_alert(s, SSL3_AL_FATAL, al);
559 s->init_num = 0;
560 *ok = 0;
561 return -1;
562 } else
563 return 0;
564}
565
566/*
567 * dtls1_max_handshake_message_len returns the maximum number of bytes
568 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
569 * may be greater if the maximum certificate list size requires it.
570 */
1250f126 571static unsigned long dtls1_max_handshake_message_len(const SSL *s)
0f113f3e
MC
572{
573 unsigned long max_len =
574 DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
575 if (max_len < (unsigned long)s->max_cert_list)
576 return s->max_cert_list;
577 return max_len;
578}
36d16f8e 579
934e22e8 580static int
0f113f3e
MC
581dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr, int *ok)
582{
583 hm_fragment *frag = NULL;
584 pitem *item = NULL;
585 int i = -1, is_complete;
586 unsigned char seq64be[8];
587 unsigned long frag_len = msg_hdr->frag_len;
588
589 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
590 msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
591 goto err;
592
593 if (frag_len == 0)
594 return DTLS1_HM_FRAGMENT_RETRY;
595
596 /* Try to find item in queue */
597 memset(seq64be, 0, sizeof(seq64be));
598 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
599 seq64be[7] = (unsigned char)msg_hdr->seq;
600 item = pqueue_find(s->d1->buffered_messages, seq64be);
601
602 if (item == NULL) {
603 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
604 if (frag == NULL)
605 goto err;
606 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
607 frag->msg_header.frag_len = frag->msg_header.msg_len;
608 frag->msg_header.frag_off = 0;
609 } else {
610 frag = (hm_fragment *)item->data;
611 if (frag->msg_header.msg_len != msg_hdr->msg_len) {
612 item = NULL;
613 frag = NULL;
614 goto err;
615 }
616 }
617
618 /*
619 * If message is already reassembled, this must be a retransmit and can
620 * be dropped. In this case item != NULL and so frag does not need to be
621 * freed.
622 */
623 if (frag->reassembly == NULL) {
624 unsigned char devnull[256];
625
626 while (frag_len) {
657da85e 627 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
0f113f3e
MC
628 devnull,
629 frag_len >
630 sizeof(devnull) ? sizeof(devnull) :
631 frag_len, 0);
632 if (i <= 0)
633 goto err;
634 frag_len -= i;
635 }
636 return DTLS1_HM_FRAGMENT_RETRY;
637 }
638
639 /* read the body of the fragment (header has already been read */
657da85e 640 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
0f113f3e
MC
641 frag->fragment + msg_hdr->frag_off,
642 frag_len, 0);
643 if ((unsigned long)i != frag_len)
644 i = -1;
645 if (i <= 0)
646 goto err;
647
648 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
649 (long)(msg_hdr->frag_off + frag_len));
650
651 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
652 is_complete);
653
654 if (is_complete) {
655 OPENSSL_free(frag->reassembly);
656 frag->reassembly = NULL;
657 }
658
659 if (item == NULL) {
660 item = pitem_new(seq64be, frag);
661 if (item == NULL) {
662 i = -1;
663 goto err;
664 }
665
666 item = pqueue_insert(s->d1->buffered_messages, item);
667 /*
668 * pqueue_insert fails iff a duplicate item is inserted. However,
669 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
670 * would have returned it and control would never have reached this
671 * branch.
672 */
673 OPENSSL_assert(item != NULL);
674 }
675
676 return DTLS1_HM_FRAGMENT_RETRY;
677
678 err:
25aaa98a 679 if (item == NULL)
0f113f3e
MC
680 dtls1_hm_fragment_free(frag);
681 *ok = 0;
682 return i;
683}
934e22e8 684
36d16f8e 685static int
0f113f3e
MC
686dtls1_process_out_of_seq_message(SSL *s, const struct hm_header_st *msg_hdr,
687 int *ok)
36d16f8e 688{
0f113f3e
MC
689 int i = -1;
690 hm_fragment *frag = NULL;
691 pitem *item = NULL;
692 unsigned char seq64be[8];
693 unsigned long frag_len = msg_hdr->frag_len;
694
695 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
696 goto err;
697
698 /* Try to find item in queue, to prevent duplicate entries */
699 memset(seq64be, 0, sizeof(seq64be));
700 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
701 seq64be[7] = (unsigned char)msg_hdr->seq;
702 item = pqueue_find(s->d1->buffered_messages, seq64be);
703
704 /*
705 * If we already have an entry and this one is a fragment, don't discard
706 * it and rather try to reassemble it.
707 */
708 if (item != NULL && frag_len != msg_hdr->msg_len)
709 item = NULL;
710
711 /*
712 * Discard the message if sequence number was already there, is too far
713 * in the future, already in the queue or if we received a FINISHED
714 * before the SERVER_HELLO, which then must be a stale retransmit.
715 */
716 if (msg_hdr->seq <= s->d1->handshake_read_seq ||
717 msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
718 (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED))
719 {
720 unsigned char devnull[256];
721
722 while (frag_len) {
657da85e 723 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
0f113f3e
MC
724 devnull,
725 frag_len >
726 sizeof(devnull) ? sizeof(devnull) :
727 frag_len, 0);
728 if (i <= 0)
729 goto err;
730 frag_len -= i;
731 }
732 } else {
733 if (frag_len != msg_hdr->msg_len)
734 return dtls1_reassemble_fragment(s, msg_hdr, ok);
735
736 if (frag_len > dtls1_max_handshake_message_len(s))
737 goto err;
738
739 frag = dtls1_hm_fragment_new(frag_len, 0);
740 if (frag == NULL)
741 goto err;
742
743 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
744
745 if (frag_len) {
746 /*
747 * read the body of the fragment (header has already been read
748 */
657da85e 749 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
0f113f3e
MC
750 frag->fragment, frag_len, 0);
751 if ((unsigned long)i != frag_len)
752 i = -1;
753 if (i <= 0)
754 goto err;
755 }
756
757 item = pitem_new(seq64be, frag);
758 if (item == NULL)
759 goto err;
760
761 item = pqueue_insert(s->d1->buffered_messages, item);
762 /*
763 * pqueue_insert fails iff a duplicate item is inserted. However,
764 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
765 * would have returned it. Then, either |frag_len| !=
766 * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
767 * have been processed with |dtls1_reassemble_fragment|, above, or
768 * the record will have been discarded.
769 */
770 OPENSSL_assert(item != NULL);
771 }
772
773 return DTLS1_HM_FRAGMENT_RETRY;
774
775 err:
25aaa98a 776 if (item == NULL)
0f113f3e
MC
777 dtls1_hm_fragment_free(frag);
778 *ok = 0;
779 return i;
780}
36d16f8e 781
76af3037 782static int dtls_get_reassembled_message(SSL *s, long *len)
0f113f3e
MC
783{
784 unsigned char wire[DTLS1_HM_HEADER_LENGTH];
76af3037 785 unsigned long mlen, frag_off, frag_len;
c69f2adf 786 int i, al, recvd_type;
0f113f3e 787 struct hm_header_st msg_hdr;
76af3037 788 int ok;
0f113f3e
MC
789
790 redo:
791 /* see if we have the required fragment already */
76af3037
MC
792 if ((frag_len = dtls1_retrieve_buffered_fragment(s, &ok)) || ok) {
793 if (ok)
0f113f3e 794 s->init_num = frag_len;
76af3037
MC
795 *len = frag_len;
796 return ok;
0f113f3e
MC
797 }
798
799 /* read handshake message header */
c69f2adf 800 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, wire,
0f113f3e
MC
801 DTLS1_HM_HEADER_LENGTH, 0);
802 if (i <= 0) { /* nbio, or an error */
803 s->rwstate = SSL_READING;
76af3037
MC
804 *len = i;
805 return 0;
0f113f3e 806 }
c69f2adf 807 if(recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
76af3037
MC
808 if (wire[0] != SSL3_MT_CCS) {
809 al = SSL_AD_UNEXPECTED_MESSAGE;
810 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,
811 SSL_R_BAD_CHANGE_CIPHER_SPEC);
812 goto f_err;
c69f2adf 813 }
76af3037
MC
814
815 memcpy(s->init_buf->data, wire, i);
816 s->init_num = i - 1;
817 s->init_msg = s->init_buf->data + 1;
818 s->s3->tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
819 s->s3->tmp.message_size = i - 1;
820 *len = i - 1;
821 return 1;
c69f2adf
MC
822 }
823
0f113f3e
MC
824 /* Handshake fails if message header is incomplete */
825 if (i != DTLS1_HM_HEADER_LENGTH) {
826 al = SSL_AD_UNEXPECTED_MESSAGE;
76af3037 827 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
0f113f3e
MC
828 goto f_err;
829 }
830
831 /* parse the message fragment header */
832 dtls1_get_message_header(wire, &msg_hdr);
833
76af3037 834 mlen = msg_hdr.msg_len;
91d13f1a
MC
835 frag_off = msg_hdr.frag_off;
836 frag_len = msg_hdr.frag_len;
837
838 /*
839 * We must have at least frag_len bytes left in the record to be read.
840 * Fragments must not span records.
841 */
842 if (frag_len > RECORD_LAYER_get_rrec_length(&s->rlayer)) {
843 al = SSL3_AD_ILLEGAL_PARAMETER;
76af3037 844 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_BAD_LENGTH);
91d13f1a
MC
845 goto f_err;
846 }
847
0f113f3e
MC
848 /*
849 * if this is a future (or stale) message it gets buffered
850 * (or dropped)--no further processing at this time
851 * While listening, we accept seq 1 (ClientHello with cookie)
852 * although we're still expecting seq 0 (ClientHello)
853 */
76af3037
MC
854 if (msg_hdr.seq != s->d1->handshake_read_seq) {
855 *len = dtls1_process_out_of_seq_message(s, &msg_hdr, &ok);
856 return ok;
857 }
0f113f3e 858
76af3037
MC
859 if (frag_len && frag_len < mlen) {
860 *len = dtls1_reassemble_fragment(s, &msg_hdr, &ok);
861 return ok;
862 }
0f113f3e
MC
863
864 if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
865 wire[0] == SSL3_MT_HELLO_REQUEST) {
866 /*
867 * The server may always send 'Hello Request' messages -- we are
868 * doing a handshake anyway now, so ignore them if their format is
869 * correct. Does not count for 'Finished' MAC.
870 */
871 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
872 if (s->msg_callback)
873 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
874 wire, DTLS1_HM_HEADER_LENGTH, s,
875 s->msg_callback_arg);
876
877 s->init_num = 0;
878 goto redo;
879 } else { /* Incorrectly formated Hello request */
880
881 al = SSL_AD_UNEXPECTED_MESSAGE;
76af3037 882 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,
0f113f3e
MC
883 SSL_R_UNEXPECTED_MESSAGE);
884 goto f_err;
885 }
886 }
887
76af3037 888 if ((al = dtls1_preprocess_fragment(s, &msg_hdr)))
0f113f3e
MC
889 goto f_err;
890
0f113f3e
MC
891 if (frag_len > 0) {
892 unsigned char *p =
893 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
894
657da85e 895 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
0f113f3e 896 &p[frag_off], frag_len, 0);
91d13f1a 897
0f113f3e 898 /*
91d13f1a
MC
899 * This shouldn't ever fail due to NBIO because we already checked
900 * that we have enough data in the record
0f113f3e
MC
901 */
902 if (i <= 0) {
903 s->rwstate = SSL_READING;
76af3037
MC
904 *len = i;
905 return 0;
0f113f3e
MC
906 }
907 } else
908 i = 0;
909
910 /*
911 * XDTLS: an incorrectly formatted fragment should cause the handshake
912 * to fail
913 */
914 if (i != (int)frag_len) {
915 al = SSL3_AD_ILLEGAL_PARAMETER;
76af3037 916 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL3_AD_ILLEGAL_PARAMETER);
0f113f3e
MC
917 goto f_err;
918 }
919
0f113f3e
MC
920 /*
921 * Note that s->init_num is *not* used as current offset in
922 * s->init_buf->data, but as a counter summing up fragments' lengths: as
923 * soon as they sum up to handshake packet length, we assume we have got
924 * all the fragments.
925 */
76af3037
MC
926 *len = s->init_num = frag_len;
927 return 1;
0f113f3e
MC
928
929 f_err:
930 ssl3_send_alert(s, SSL3_AL_FATAL, al);
931 s->init_num = 0;
76af3037
MC
932 *len = -1;
933 return 0;
0f113f3e 934}
36d16f8e 935
1d97c843
TH
936/*-
937 * for these 2 messages, we need to
0f113f3e 938 * ssl->enc_read_ctx re-init
de07f311 939 * ssl->rlayer.read_sequence zero
0f113f3e
MC
940 * ssl->s3->read_mac_secret re-init
941 * ssl->session->read_sym_enc assign
942 * ssl->session->read_compression assign
943 * ssl->session->read_hash assign
36d16f8e 944 */
473483d4 945int dtls_construct_change_cipher_spec(SSL *s)
0f113f3e
MC
946{
947 unsigned char *p;
36d16f8e 948
473483d4
MC
949 p = (unsigned char *)s->init_buf->data;
950 *p++ = SSL3_MT_CCS;
951 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
952 s->init_num = DTLS1_CCS_HEADER_LENGTH;
953
954 if (s->version == DTLS1_BAD_VER) {
955 s->d1->next_handshake_write_seq++;
956 s2n(s->d1->handshake_write_seq, p);
957 s->init_num += 2;
958 }
36d16f8e 959
473483d4 960 s->init_off = 0;
36d16f8e 961
473483d4
MC
962 dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
963 s->d1->handshake_write_seq, 0, 0);
36d16f8e 964
473483d4
MC
965 /* buffer the message to handle re-xmits */
966 if (!dtls1_buffer_message(s, 1)) {
967 SSLerr(SSL_F_DTLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
968 return 0;
0f113f3e 969 }
36d16f8e 970
473483d4
MC
971 return 1;
972}
973
974#ifndef OPENSSL_NO_SCTP
be3583fa 975WORK_STATE dtls_wait_for_dry(SSL *s)
473483d4
MC
976{
977 int ret;
978
979 /* read app data until dry event */
980 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
981 if (ret < 0)
982 return WORK_ERROR;
983
984 if (ret == 0) {
985 s->s3->in_read_app_data = 2;
986 s->rwstate = SSL_READING;
987 BIO_clear_retry_flags(SSL_get_rbio(s));
988 BIO_set_retry_read(SSL_get_rbio(s));
989 return WORK_MORE_A;
990 }
991 return WORK_FINISHED_CONTINUE;
0f113f3e 992}
473483d4 993#endif
36d16f8e 994
36d16f8e 995int dtls1_read_failed(SSL *s, int code)
0f113f3e
MC
996{
997 if (code > 0) {
998 fprintf(stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
999 return 1;
1000 }
1001
1002 if (!dtls1_is_timer_expired(s)) {
1003 /*
1004 * not a timeout, none of our business, let higher layers handle
1005 * this. in fact it's probably an error
1006 */
1007 return code;
1008 }
4817504d 1009#ifndef OPENSSL_NO_HEARTBEATS
0f113f3e
MC
1010 /* done, no need to send a retransmit */
1011 if (!SSL_in_init(s) && !s->tlsext_hb_pending)
4817504d 1012#else
0f113f3e
MC
1013 /* done, no need to send a retransmit */
1014 if (!SSL_in_init(s))
4817504d 1015#endif
0f113f3e
MC
1016 {
1017 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
1018 return code;
1019 }
36d16f8e 1020
0f113f3e
MC
1021 return dtls1_handle_timeout(s);
1022}
36d16f8e 1023
0f113f3e
MC
1024int dtls1_get_queue_priority(unsigned short seq, int is_ccs)
1025{
1026 /*
1027 * The index of the retransmission queue actually is the message sequence
1028 * number, since the queue only contains messages of a single handshake.
1029 * However, the ChangeCipherSpec has no message sequence number and so
1030 * using only the sequence will result in the CCS and Finished having the
1031 * same index. To prevent this, the sequence number is multiplied by 2.
1032 * In case of a CCS 1 is subtracted. This does not only differ CSS and
1033 * Finished, it also maintains the order of the index (important for
1034 * priority queues) and fits in the unsigned short variable.
1035 */
1036 return seq * 2 - is_ccs;
1037}
36d16f8e 1038
0f113f3e
MC
1039int dtls1_retransmit_buffered_messages(SSL *s)
1040{
1041 pqueue sent = s->d1->sent_messages;
1042 piterator iter;
1043 pitem *item;
1044 hm_fragment *frag;
1045 int found = 0;
1046
1047 iter = pqueue_iterator(sent);
1048
1049 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
1050 frag = (hm_fragment *)item->data;
1051 if (dtls1_retransmit_message(s, (unsigned short)
1052 dtls1_get_queue_priority
1053 (frag->msg_header.seq,
1054 frag->msg_header.is_ccs), 0,
1055 &found) <= 0 && found) {
1056 fprintf(stderr, "dtls1_retransmit_message() failed\n");
1057 return -1;
1058 }
1059 }
1060
1061 return 1;
1062}
36d16f8e 1063
0f113f3e
MC
1064int dtls1_buffer_message(SSL *s, int is_ccs)
1065{
1066 pitem *item;
1067 hm_fragment *frag;
1068 unsigned char seq64be[8];
1069
1070 /*
1071 * this function is called immediately after a message has been
1072 * serialized
1073 */
1074 OPENSSL_assert(s->init_off == 0);
1075
1076 frag = dtls1_hm_fragment_new(s->init_num, 0);
a71edf3b 1077 if (frag == NULL)
0f113f3e
MC
1078 return 0;
1079
1080 memcpy(frag->fragment, s->init_buf->data, s->init_num);
1081
1082 if (is_ccs) {
5178a16c 1083 /* For DTLS1_BAD_VER the header length is non-standard */
0f113f3e 1084 OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
5178a16c
MC
1085 ((s->version==DTLS1_BAD_VER)?3:DTLS1_CCS_HEADER_LENGTH)
1086 == (unsigned int)s->init_num);
0f113f3e
MC
1087 } else {
1088 OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
1089 DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);
1090 }
1091
1092 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1093 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1094 frag->msg_header.type = s->d1->w_msg_hdr.type;
1095 frag->msg_header.frag_off = 0;
1096 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1097 frag->msg_header.is_ccs = is_ccs;
1098
1099 /* save current state */
1100 frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
1101 frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
1102 frag->msg_header.saved_retransmit_state.compress = s->compress;
1103 frag->msg_header.saved_retransmit_state.session = s->session;
78a39fe7
MC
1104 frag->msg_header.saved_retransmit_state.epoch =
1105 DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
0f113f3e
MC
1106
1107 memset(seq64be, 0, sizeof(seq64be));
1108 seq64be[6] =
1109 (unsigned
1110 char)(dtls1_get_queue_priority(frag->msg_header.seq,
1111 frag->msg_header.is_ccs) >> 8);
1112 seq64be[7] =
1113 (unsigned
1114 char)(dtls1_get_queue_priority(frag->msg_header.seq,
1115 frag->msg_header.is_ccs));
1116
1117 item = pitem_new(seq64be, frag);
1118 if (item == NULL) {
1119 dtls1_hm_fragment_free(frag);
1120 return 0;
1121 }
36d16f8e 1122
0f113f3e
MC
1123 pqueue_insert(s->d1->sent_messages, item);
1124 return 1;
1125}
36d16f8e
BL
1126
1127int
1128dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
0f113f3e
MC
1129 int *found)
1130{
1131 int ret;
1132 /* XDTLS: for now assuming that read/writes are blocking */
1133 pitem *item;
1134 hm_fragment *frag;
1135 unsigned long header_length;
1136 unsigned char seq64be[8];
1137 struct dtls1_retransmit_state saved_state;
0f113f3e 1138
50e735f9
MC
1139 /*-
1140 OPENSSL_assert(s->init_num == 0);
1141 OPENSSL_assert(s->init_off == 0);
1142 */
0f113f3e
MC
1143
1144 /* XDTLS: the requested message ought to be found, otherwise error */
1145 memset(seq64be, 0, sizeof(seq64be));
1146 seq64be[6] = (unsigned char)(seq >> 8);
1147 seq64be[7] = (unsigned char)seq;
1148
1149 item = pqueue_find(s->d1->sent_messages, seq64be);
1150 if (item == NULL) {
1151 fprintf(stderr, "retransmit: message %d non-existant\n", seq);
1152 *found = 0;
1153 return 0;
1154 }
1155
1156 *found = 1;
1157 frag = (hm_fragment *)item->data;
1158
1159 if (frag->msg_header.is_ccs)
1160 header_length = DTLS1_CCS_HEADER_LENGTH;
1161 else
1162 header_length = DTLS1_HM_HEADER_LENGTH;
1163
1164 memcpy(s->init_buf->data, frag->fragment,
1165 frag->msg_header.msg_len + header_length);
1166 s->init_num = frag->msg_header.msg_len + header_length;
1167
1168 dtls1_set_message_header_int(s, frag->msg_header.type,
1169 frag->msg_header.msg_len,
1170 frag->msg_header.seq, 0,
1171 frag->msg_header.frag_len);
1172
1173 /* save current state */
1174 saved_state.enc_write_ctx = s->enc_write_ctx;
1175 saved_state.write_hash = s->write_hash;
1176 saved_state.compress = s->compress;
1177 saved_state.session = s->session;
78a39fe7 1178 saved_state.epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
0f113f3e
MC
1179
1180 s->d1->retransmitting = 1;
1181
1182 /* restore state in which the message was originally sent */
1183 s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
1184 s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
1185 s->compress = frag->msg_header.saved_retransmit_state.compress;
1186 s->session = frag->msg_header.saved_retransmit_state.session;
3bb8f87d 1187 DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer,
78a39fe7 1188 frag->msg_header.saved_retransmit_state.epoch);
0f113f3e 1189
0f113f3e
MC
1190 ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
1191 SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
1192
1193 /* restore current state */
1194 s->enc_write_ctx = saved_state.enc_write_ctx;
1195 s->write_hash = saved_state.write_hash;
1196 s->compress = saved_state.compress;
1197 s->session = saved_state.session;
3bb8f87d 1198 DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer, saved_state.epoch);
0f113f3e
MC
1199
1200 s->d1->retransmitting = 0;
1201
1202 (void)BIO_flush(SSL_get_wbio(s));
1203 return ret;
1204}
36d16f8e
BL
1205
1206/* call this function when the buffered messages are no longer needed */
0f113f3e
MC
1207void dtls1_clear_record_buffer(SSL *s)
1208{
1209 pitem *item;
36d16f8e 1210
0f113f3e
MC
1211 for (item = pqueue_pop(s->d1->sent_messages);
1212 item != NULL; item = pqueue_pop(s->d1->sent_messages)) {
1213 dtls1_hm_fragment_free((hm_fragment *)item->data);
1214 pitem_free(item);
1215 }
1216}
54ef01b5 1217
69f68237 1218void dtls1_set_message_header(SSL *s, unsigned char *p,
0f113f3e
MC
1219 unsigned char mt, unsigned long len,
1220 unsigned long frag_off,
1221 unsigned long frag_len)
1222{
912c89c5 1223 if (frag_off == 0) {
0f113f3e
MC
1224 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1225 s->d1->next_handshake_write_seq++;
1226 }
54ef01b5 1227
0f113f3e
MC
1228 dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
1229 frag_off, frag_len);
0f113f3e 1230}
36d16f8e
BL
1231
1232/* don't actually do the writing, wait till the MTU has been retrieved */
1233static void
1234dtls1_set_message_header_int(SSL *s, unsigned char mt,
0f113f3e
MC
1235 unsigned long len, unsigned short seq_num,
1236 unsigned long frag_off, unsigned long frag_len)
1237{
1238 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1239
1240 msg_hdr->type = mt;
1241 msg_hdr->msg_len = len;
1242 msg_hdr->seq = seq_num;
1243 msg_hdr->frag_off = frag_off;
1244 msg_hdr->frag_len = frag_len;
1245}
36d16f8e
BL
1246
1247static void
1248dtls1_fix_message_header(SSL *s, unsigned long frag_off,
0f113f3e
MC
1249 unsigned long frag_len)
1250{
1251 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
36d16f8e 1252
0f113f3e
MC
1253 msg_hdr->frag_off = frag_off;
1254 msg_hdr->frag_len = frag_len;
1255}
54ef01b5 1256
0f113f3e
MC
1257static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p)
1258{
1259 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
54ef01b5 1260
0f113f3e
MC
1261 *p++ = msg_hdr->type;
1262 l2n3(msg_hdr->msg_len, p);
54ef01b5 1263
0f113f3e
MC
1264 s2n(msg_hdr->seq, p);
1265 l2n3(msg_hdr->frag_off, p);
1266 l2n3(msg_hdr->frag_len, p);
36d16f8e 1267
0f113f3e
MC
1268 return p;
1269}
36d16f8e 1270
36d16f8e
BL
1271void
1272dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
0f113f3e 1273{
16f8d4eb 1274 memset(msg_hdr, 0, sizeof(*msg_hdr));
0f113f3e
MC
1275 msg_hdr->type = *(data++);
1276 n2l3(data, msg_hdr->msg_len);
54ef01b5 1277
0f113f3e
MC
1278 n2s(data, msg_hdr->seq);
1279 n2l3(data, msg_hdr->frag_off);
1280 n2l3(data, msg_hdr->frag_len);
1281}
36d16f8e 1282
0f113f3e 1283