summaryrefslogtreecommitdiff
path: root/src/lib/libssl/d1_both.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/d1_both.c')
-rw-r--r--src/lib/libssl/d1_both.c1424
1 files changed, 0 insertions, 1424 deletions
diff --git a/src/lib/libssl/d1_both.c b/src/lib/libssl/d1_both.c
deleted file mode 100644
index b479c61322..0000000000
--- a/src/lib/libssl/d1_both.c
+++ /dev/null
@@ -1,1424 +0,0 @@
1/* $OpenBSD: d1_both.c,v 1.34 2015/07/19 20:32:18 doug Exp $ */
2/*
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
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
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
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/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@openssl.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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.
65 *
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).
72 *
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.
79 *
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
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
88 * 3. All advertising materials mentioning features or use of this software
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 :-).
94 * 4. If you include any Windows specific code (or a derivative thereof) from
95 * the apps directory (application code) you must include an acknowledgement:
96 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
97 *
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.
109 *
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 <stdio.h>
118#include <string.h>
119
120#include "ssl_locl.h"
121
122#include <openssl/buffer.h>
123#include <openssl/evp.h>
124#include <openssl/objects.h>
125#include <openssl/x509.h>
126
127#include "pqueue.h"
128#include "bytestring.h"
129
130#define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
131
132#define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
133 if ((end) - (start) <= 8) { \
134 long ii; \
135 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
136 } else { \
137 long ii; \
138 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
139 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
140 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
141 } }
142
143#define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
144 long ii; \
145 OPENSSL_assert((msg_len) > 0); \
146 is_complete = 1; \
147 if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
148 if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
149 if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
150
151static unsigned char bitmask_start_values[] = {
152 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80
153};
154static unsigned char bitmask_end_values[] = {
155 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
156};
157
158/* XDTLS: figure out the right values */
159static unsigned int g_probable_mtu[] = {1500 - 28, 512 - 28, 256 - 28};
160
161static unsigned int dtls1_guess_mtu(unsigned int curr_mtu);
162static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
163 unsigned long frag_len);
164static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p);
165static void dtls1_set_message_header_int(SSL *s, unsigned char mt,
166 unsigned long len, unsigned short seq_num, unsigned long frag_off,
167 unsigned long frag_len);
168static long dtls1_get_message_fragment(SSL *s, int st1, int stn, long max,
169 int *ok);
170
171static hm_fragment *
172dtls1_hm_fragment_new(unsigned long frag_len, int reassembly)
173{
174 hm_fragment *frag = NULL;
175 unsigned char *buf = NULL;
176 unsigned char *bitmask = NULL;
177
178 frag = malloc(sizeof(hm_fragment));
179 if (frag == NULL)
180 return NULL;
181
182 if (frag_len) {
183 buf = malloc(frag_len);
184 if (buf == NULL) {
185 free(frag);
186 return NULL;
187 }
188 }
189
190 /* zero length fragment gets zero frag->fragment */
191 frag->fragment = buf;
192
193 /* Initialize reassembly bitmask if necessary */
194 if (reassembly) {
195 bitmask = malloc(RSMBLY_BITMASK_SIZE(frag_len));
196 if (bitmask == NULL) {
197 free(buf);
198 free(frag);
199 return NULL;
200 }
201 memset(bitmask, 0, RSMBLY_BITMASK_SIZE(frag_len));
202 }
203
204 frag->reassembly = bitmask;
205
206 return frag;
207}
208
209static void
210dtls1_hm_fragment_free(hm_fragment *frag)
211{
212 if (frag == NULL)
213 return;
214
215 if (frag->msg_header.is_ccs) {
216 EVP_CIPHER_CTX_free(
217 frag->msg_header.saved_retransmit_state.enc_write_ctx);
218 EVP_MD_CTX_destroy(
219 frag->msg_header.saved_retransmit_state.write_hash);
220 }
221 free(frag->fragment);
222 free(frag->reassembly);
223 free(frag);
224}
225
226/* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */
227int
228dtls1_do_write(SSL *s, int type)
229{
230 int ret;
231 int curr_mtu;
232 unsigned int len, frag_off, mac_size, blocksize;
233
234 /* AHA! Figure out the MTU, and stick to the right size */
235 if (s->d1->mtu < dtls1_min_mtu() &&
236 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
237 s->d1->mtu = BIO_ctrl(SSL_get_wbio(s),
238 BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
239
240 /*
241 * I've seen the kernel return bogus numbers when it
242 * doesn't know the MTU (ie., the initial write), so just
243 * make sure we have a reasonable number
244 */
245 if (s->d1->mtu < dtls1_min_mtu()) {
246 s->d1->mtu = 0;
247 s->d1->mtu = dtls1_guess_mtu(s->d1->mtu);
248 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU,
249 s->d1->mtu, NULL);
250 }
251 }
252
253 OPENSSL_assert(s->d1->mtu >= dtls1_min_mtu());
254 /* should have something reasonable now */
255
256 if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE)
257 OPENSSL_assert(s->init_num ==
258 (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
259
260 if (s->write_hash)
261 mac_size = EVP_MD_CTX_size(s->write_hash);
262 else
263 mac_size = 0;
264
265 if (s->enc_write_ctx &&
266 (EVP_CIPHER_mode( s->enc_write_ctx->cipher) & EVP_CIPH_CBC_MODE))
267 blocksize = 2 * EVP_CIPHER_block_size(s->enc_write_ctx->cipher);
268 else
269 blocksize = 0;
270
271 frag_off = 0;
272 while (s->init_num) {
273 curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
274 DTLS1_RT_HEADER_LENGTH - mac_size - blocksize;
275
276 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
277 /* grr.. we could get an error if MTU picked was wrong */
278 ret = BIO_flush(SSL_get_wbio(s));
279 if (ret <= 0)
280 return ret;
281 curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH -
282 mac_size - blocksize;
283 }
284
285 if (s->init_num > curr_mtu)
286 len = curr_mtu;
287 else
288 len = s->init_num;
289
290
291 /* XDTLS: this function is too long. split out the CCS part */
292 if (type == SSL3_RT_HANDSHAKE) {
293 if (s->init_off != 0) {
294 OPENSSL_assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
295 s->init_off -= DTLS1_HM_HEADER_LENGTH;
296 s->init_num += DTLS1_HM_HEADER_LENGTH;
297
298 if (s->init_num > curr_mtu)
299 len = curr_mtu;
300 else
301 len = s->init_num;
302 }
303
304 dtls1_fix_message_header(s, frag_off,
305 len - DTLS1_HM_HEADER_LENGTH);
306
307 dtls1_write_message_header(s,
308 (unsigned char *)&s->init_buf->data[s->init_off]);
309
310 OPENSSL_assert(len >= DTLS1_HM_HEADER_LENGTH);
311 }
312
313 ret = dtls1_write_bytes(s, type,
314 &s->init_buf->data[s->init_off], len);
315 if (ret < 0) {
316 /*
317 * Might need to update MTU here, but we don't know
318 * which previous packet caused the failure -- so
319 * can't really retransmit anything. continue as
320 * if everything is fine and wait for an alert to
321 * handle the retransmit
322 */
323 if (BIO_ctrl(SSL_get_wbio(s),
324 BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0)
325 s->d1->mtu = BIO_ctrl(SSL_get_wbio(s),
326 BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
327 else
328 return (-1);
329 } else {
330
331 /*
332 * Bad if this assert fails, only part of the
333 * handshake message got sent. but why would
334 * this happen?
335 */
336 OPENSSL_assert(len == (unsigned int)ret);
337
338 if (type == SSL3_RT_HANDSHAKE &&
339 !s->d1->retransmitting) {
340 /*
341 * Should not be done for 'Hello Request's,
342 * but in that case we'll ignore the result
343 * anyway
344 */
345 unsigned char *p = (unsigned char *)&s->init_buf->data[s->init_off];
346 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
347 int xlen;
348
349 if (frag_off == 0 &&
350 s->version != DTLS1_BAD_VER) {
351 /*
352 * Reconstruct message header is if it
353 * is being sent in single fragment
354 */
355 *p++ = msg_hdr->type;
356 l2n3(msg_hdr->msg_len, p);
357 s2n (msg_hdr->seq, p);
358 l2n3(0, p);
359 l2n3(msg_hdr->msg_len, p);
360 p -= DTLS1_HM_HEADER_LENGTH;
361 xlen = ret;
362 } else {
363 p += DTLS1_HM_HEADER_LENGTH;
364 xlen = ret - DTLS1_HM_HEADER_LENGTH;
365 }
366
367 ssl3_finish_mac(s, p, xlen);
368 }
369
370 if (ret == s->init_num) {
371 if (s->msg_callback)
372 s->msg_callback(1, s->version, type,
373 s->init_buf->data,
374 (size_t)(s->init_off + s->init_num),
375 s, s->msg_callback_arg);
376
377 s->init_off = 0;
378 /* done writing this message */
379 s->init_num = 0;
380
381 return (1);
382 }
383 s->init_off += ret;
384 s->init_num -= ret;
385 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
386 }
387 }
388 return (0);
389}
390
391
392/*
393 * Obtain handshake message of message type 'mt' (any if mt == -1),
394 * maximum acceptable body length 'max'.
395 * Read an entire handshake message. Handshake messages arrive in
396 * fragments.
397 */
398long
399dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
400{
401 int i, al;
402 struct hm_header_st *msg_hdr;
403 unsigned char *p;
404 unsigned long msg_len;
405
406 /*
407 * s3->tmp is used to store messages that are unexpected, caused
408 * by the absence of an optional handshake message
409 */
410 if (s->s3->tmp.reuse_message) {
411 s->s3->tmp.reuse_message = 0;
412 if ((mt >= 0) && (s->s3->tmp.message_type != mt)) {
413 al = SSL_AD_UNEXPECTED_MESSAGE;
414 SSLerr(SSL_F_DTLS1_GET_MESSAGE,
415 SSL_R_UNEXPECTED_MESSAGE);
416 goto f_err;
417 }
418 *ok = 1;
419 s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
420 s->init_num = (int)s->s3->tmp.message_size;
421 return s->init_num;
422 }
423
424 msg_hdr = &s->d1->r_msg_hdr;
425 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
426
427again:
428 i = dtls1_get_message_fragment(s, st1, stn, max, ok);
429 if (i == DTLS1_HM_BAD_FRAGMENT ||
430 i == DTLS1_HM_FRAGMENT_RETRY) /* bad fragment received */
431 goto again;
432 else if (i <= 0 && !*ok)
433 return i;
434
435 p = (unsigned char *)s->init_buf->data;
436 msg_len = msg_hdr->msg_len;
437
438 /* reconstruct message header */
439 *(p++) = msg_hdr->type;
440 l2n3(msg_len, p);
441 s2n (msg_hdr->seq, p);
442 l2n3(0, p);
443 l2n3(msg_len, p);
444 if (s->version != DTLS1_BAD_VER) {
445 p -= DTLS1_HM_HEADER_LENGTH;
446 msg_len += DTLS1_HM_HEADER_LENGTH;
447 }
448
449 ssl3_finish_mac(s, p, msg_len);
450 if (s->msg_callback)
451 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, msg_len,
452 s, s->msg_callback_arg);
453
454 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
455
456 /* Don't change sequence numbers while listening */
457 if (!s->d1->listen)
458 s->d1->handshake_read_seq++;
459
460 s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
461 return s->init_num;
462
463f_err:
464 ssl3_send_alert(s, SSL3_AL_FATAL, al);
465 *ok = 0;
466 return -1;
467}
468
469
470static int
471dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr, int max)
472{
473 size_t frag_off, frag_len, msg_len;
474
475 msg_len = msg_hdr->msg_len;
476 frag_off = msg_hdr->frag_off;
477 frag_len = msg_hdr->frag_len;
478
479 /* sanity checking */
480 if ((frag_off + frag_len) > msg_len) {
481 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,
482 SSL_R_EXCESSIVE_MESSAGE_SIZE);
483 return SSL_AD_ILLEGAL_PARAMETER;
484 }
485
486 if ((frag_off + frag_len) > (unsigned long)max) {
487 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,
488 SSL_R_EXCESSIVE_MESSAGE_SIZE);
489 return SSL_AD_ILLEGAL_PARAMETER;
490 }
491
492 if ( s->d1->r_msg_hdr.frag_off == 0) /* first fragment */
493 {
494 /*
495 * msg_len is limited to 2^24, but is effectively checked
496 * against max above
497 */
498 if (!BUF_MEM_grow_clean(s->init_buf,
499 msg_len + DTLS1_HM_HEADER_LENGTH)) {
500 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, ERR_R_BUF_LIB);
501 return SSL_AD_INTERNAL_ERROR;
502 }
503
504 s->s3->tmp.message_size = msg_len;
505 s->d1->r_msg_hdr.msg_len = msg_len;
506 s->s3->tmp.message_type = msg_hdr->type;
507 s->d1->r_msg_hdr.type = msg_hdr->type;
508 s->d1->r_msg_hdr.seq = msg_hdr->seq;
509 } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
510 /*
511 * They must be playing with us! BTW, failure to enforce
512 * upper limit would open possibility for buffer overrun.
513 */
514 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,
515 SSL_R_EXCESSIVE_MESSAGE_SIZE);
516 return SSL_AD_ILLEGAL_PARAMETER;
517 }
518
519 return 0; /* no error */
520}
521
522static int
523dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok)
524{
525 /*
526 * (0) check whether the desired fragment is available
527 * if so:
528 * (1) copy over the fragment to s->init_buf->data[]
529 * (2) update s->init_num
530 */
531 pitem *item;
532 hm_fragment *frag;
533 int al;
534
535 *ok = 0;
536 item = pqueue_peek(s->d1->buffered_messages);
537 if (item == NULL)
538 return 0;
539
540 frag = (hm_fragment *)item->data;
541
542 /* Don't return if reassembly still in progress */
543 if (frag->reassembly != NULL)
544 return 0;
545
546 if (s->d1->handshake_read_seq == frag->msg_header.seq) {
547 unsigned long frag_len = frag->msg_header.frag_len;
548 pqueue_pop(s->d1->buffered_messages);
549
550 al = dtls1_preprocess_fragment(s, &frag->msg_header, max);
551
552 if (al == 0) /* no alert */
553 {
554 unsigned char *p = (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
555 memcpy(&p[frag->msg_header.frag_off],
556 frag->fragment, frag->msg_header.frag_len);
557 }
558
559 dtls1_hm_fragment_free(frag);
560 pitem_free(item);
561
562 if (al == 0) {
563 *ok = 1;
564 return frag_len;
565 }
566
567 ssl3_send_alert(s, SSL3_AL_FATAL, al);
568 s->init_num = 0;
569 *ok = 0;
570 return -1;
571 } else
572 return 0;
573}
574
575/*
576 * dtls1_max_handshake_message_len returns the maximum number of bytes
577 * permitted in a DTLS handshake message for |s|. The minimum is 16KB,
578 * but may be greater if the maximum certificate list size requires it.
579 */
580static unsigned long
581dtls1_max_handshake_message_len(const SSL *s)
582{
583 unsigned long max_len;
584
585 max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
586 if (max_len < (unsigned long)s->max_cert_list)
587 return s->max_cert_list;
588 return max_len;
589}
590
591static int
592dtls1_reassemble_fragment(SSL *s, struct hm_header_st* msg_hdr, int *ok)
593{
594 hm_fragment *frag = NULL;
595 pitem *item = NULL;
596 int i = -1, is_complete;
597 unsigned char seq64be[8];
598 unsigned long frag_len = msg_hdr->frag_len;
599
600 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
601 msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
602 goto err;
603
604 if (frag_len == 0) {
605 i = DTLS1_HM_FRAGMENT_RETRY;
606 goto err;
607 }
608
609 /* Try to find item in queue */
610 memset(seq64be, 0, sizeof(seq64be));
611 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
612 seq64be[7] = (unsigned char)msg_hdr->seq;
613 item = pqueue_find(s->d1->buffered_messages, seq64be);
614
615 if (item == NULL) {
616 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
617 if (frag == NULL)
618 goto err;
619 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
620 frag->msg_header.frag_len = frag->msg_header.msg_len;
621 frag->msg_header.frag_off = 0;
622 } else {
623 frag = (hm_fragment*)item->data;
624 if (frag->msg_header.msg_len != msg_hdr->msg_len) {
625 item = NULL;
626 frag = NULL;
627 goto err;
628 }
629 }
630
631 /*
632 * If message is already reassembled, this must be a
633 * retransmit and can be dropped.
634 */
635 if (frag->reassembly == NULL) {
636 unsigned char devnull [256];
637
638 while (frag_len) {
639 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
640 devnull, frag_len > sizeof(devnull) ?
641 sizeof(devnull) : frag_len, 0);
642 if (i <= 0)
643 goto err;
644 frag_len -= i;
645 }
646 i = DTLS1_HM_FRAGMENT_RETRY;
647 goto err;
648 }
649
650 /* read the body of the fragment (header has already been read */
651 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
652 frag->fragment + msg_hdr->frag_off, frag_len, 0);
653 if (i <= 0 || (unsigned long)i != frag_len)
654 goto err;
655
656 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
657 (long)(msg_hdr->frag_off + frag_len));
658
659 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
660 is_complete);
661
662 if (is_complete) {
663 free(frag->reassembly);
664 frag->reassembly = NULL;
665 }
666
667 if (item == NULL) {
668 memset(seq64be, 0, sizeof(seq64be));
669 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
670 seq64be[7] = (unsigned char)(msg_hdr->seq);
671
672 item = pitem_new(seq64be, frag);
673 if (item == NULL) {
674 i = -1;
675 goto err;
676 }
677
678 pqueue_insert(s->d1->buffered_messages, item);
679 }
680
681 return DTLS1_HM_FRAGMENT_RETRY;
682
683err:
684 if (item == NULL && frag != NULL)
685 dtls1_hm_fragment_free(frag);
686 *ok = 0;
687 return i;
688}
689
690
691static int
692dtls1_process_out_of_seq_message(SSL *s, struct hm_header_st* msg_hdr, int *ok)
693{
694 int i = -1;
695 hm_fragment *frag = NULL;
696 pitem *item = NULL;
697 unsigned char seq64be[8];
698 unsigned long frag_len = msg_hdr->frag_len;
699
700 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
701 goto err;
702
703 /* Try to find item in queue, to prevent duplicate entries */
704 memset(seq64be, 0, sizeof(seq64be));
705 seq64be[6] = (unsigned char) (msg_hdr->seq >> 8);
706 seq64be[7] = (unsigned char) msg_hdr->seq;
707 item = pqueue_find(s->d1->buffered_messages, seq64be);
708
709 /*
710 * If we already have an entry and this one is a fragment,
711 * don't discard it and rather try to reassemble it.
712 */
713 if (item != NULL && frag_len < msg_hdr->msg_len)
714 item = NULL;
715
716 /*
717 * Discard the message if sequence number was already there, is
718 * too far in the future, already in the queue or if we received
719 * a FINISHED before the SERVER_HELLO, which then must be a stale
720 * retransmit.
721 */
722 if (msg_hdr->seq <= s->d1->handshake_read_seq ||
723 msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
724 (s->d1->handshake_read_seq == 0 &&
725 msg_hdr->type == SSL3_MT_FINISHED)) {
726 unsigned char devnull [256];
727
728 while (frag_len) {
729 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
730 devnull, frag_len > sizeof(devnull) ?
731 sizeof(devnull) : frag_len, 0);
732 if (i <= 0)
733 goto err;
734 frag_len -= i;
735 }
736 } else {
737 if (frag_len < msg_hdr->msg_len)
738 return dtls1_reassemble_fragment(s, msg_hdr, ok);
739
740 if (frag_len > dtls1_max_handshake_message_len(s))
741 goto err;
742
743 frag = dtls1_hm_fragment_new(frag_len, 0);
744 if (frag == NULL)
745 goto err;
746
747 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
748
749 if (frag_len) {
750 /* read the body of the fragment (header has already been read */
751 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
752 frag->fragment, frag_len, 0);
753 if (i <= 0 || (unsigned long)i != frag_len)
754 goto err;
755 }
756
757 memset(seq64be, 0, sizeof(seq64be));
758 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
759 seq64be[7] = (unsigned char)(msg_hdr->seq);
760
761 item = pitem_new(seq64be, frag);
762 if (item == NULL)
763 goto err;
764
765 pqueue_insert(s->d1->buffered_messages, item);
766 }
767
768 return DTLS1_HM_FRAGMENT_RETRY;
769
770err:
771 if (item == NULL && frag != NULL)
772 dtls1_hm_fragment_free(frag);
773 *ok = 0;
774 return i;
775}
776
777
778static long
779dtls1_get_message_fragment(SSL *s, int st1, int stn, long max, int *ok)
780{
781 unsigned char wire[DTLS1_HM_HEADER_LENGTH];
782 unsigned long len, frag_off, frag_len;
783 int i, al;
784 struct hm_header_st msg_hdr;
785
786again:
787 /* see if we have the required fragment already */
788 if ((frag_len = dtls1_retrieve_buffered_fragment(s, max, ok)) || *ok) {
789 if (*ok)
790 s->init_num = frag_len;
791 return frag_len;
792 }
793
794 /* read handshake message header */
795 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire,
796 DTLS1_HM_HEADER_LENGTH, 0);
797 if (i <= 0) /* nbio, or an error */
798 {
799 s->rwstate = SSL_READING;
800 *ok = 0;
801 return i;
802 }
803 /* Handshake fails if message header is incomplete */
804 if (i != DTLS1_HM_HEADER_LENGTH ||
805 /* parse the message fragment header */
806 dtls1_get_message_header(wire, &msg_hdr) == 0) {
807 al = SSL_AD_UNEXPECTED_MESSAGE;
808 SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT,
809 SSL_R_UNEXPECTED_MESSAGE);
810 goto f_err;
811 }
812
813 /*
814 * if this is a future (or stale) message it gets buffered
815 * (or dropped)--no further processing at this time
816 * While listening, we accept seq 1 (ClientHello with cookie)
817 * although we're still expecting seq 0 (ClientHello)
818 */
819 if (msg_hdr.seq != s->d1->handshake_read_seq &&
820 !(s->d1->listen && msg_hdr.seq == 1))
821 return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
822
823 len = msg_hdr.msg_len;
824 frag_off = msg_hdr.frag_off;
825 frag_len = msg_hdr.frag_len;
826
827 if (frag_len && frag_len < len)
828 return dtls1_reassemble_fragment(s, &msg_hdr, ok);
829
830 if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
831 wire[0] == SSL3_MT_HELLO_REQUEST) {
832 /*
833 * The server may always send 'Hello Request' messages --
834 * we are doing a handshake anyway now, so ignore them
835 * if their format is correct. Does not count for
836 * 'Finished' MAC.
837 */
838 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
839 if (s->msg_callback)
840 s->msg_callback(0, s->version,
841 SSL3_RT_HANDSHAKE, wire,
842 DTLS1_HM_HEADER_LENGTH, s,
843 s->msg_callback_arg);
844
845 s->init_num = 0;
846 goto again;
847 }
848 else /* Incorrectly formated Hello request */
849 {
850 al = SSL_AD_UNEXPECTED_MESSAGE;
851 SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT,
852 SSL_R_UNEXPECTED_MESSAGE);
853 goto f_err;
854 }
855 }
856
857 if ((al = dtls1_preprocess_fragment(s, &msg_hdr, max)))
858 goto f_err;
859
860 /* XDTLS: ressurect this when restart is in place */
861 s->state = stn;
862
863 if (frag_len > 0) {
864 unsigned char *p = (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
865
866 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
867 &p[frag_off], frag_len, 0);
868 /* XDTLS: fix this--message fragments cannot span multiple packets */
869 if (i <= 0) {
870 s->rwstate = SSL_READING;
871 *ok = 0;
872 return i;
873 }
874 } else
875 i = 0;
876
877 /*
878 * XDTLS: an incorrectly formatted fragment should cause the
879 * handshake to fail
880 */
881 if (i != (int)frag_len) {
882 al = SSL3_AD_ILLEGAL_PARAMETER;
883 SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT,
884 SSL3_AD_ILLEGAL_PARAMETER);
885 goto f_err;
886 }
887
888 *ok = 1;
889
890 /*
891 * Note that s->init_num is *not* used as current offset in
892 * s->init_buf->data, but as a counter summing up fragments'
893 * lengths: as soon as they sum up to handshake packet
894 * length, we assume we have got all the fragments.
895 */
896 s->init_num = frag_len;
897 return frag_len;
898
899f_err:
900 ssl3_send_alert(s, SSL3_AL_FATAL, al);
901 s->init_num = 0;
902
903 *ok = 0;
904 return (-1);
905}
906
907int
908dtls1_send_finished(SSL *s, int a, int b, const char *sender, int slen)
909{
910 unsigned char *p, *d;
911 int i;
912 unsigned long l;
913
914 if (s->state == a) {
915 d = (unsigned char *)s->init_buf->data;
916 p = &(d[DTLS1_HM_HEADER_LENGTH]);
917
918 i = s->method->ssl3_enc->final_finish_mac(s, sender, slen,
919 s->s3->tmp.finish_md);
920 s->s3->tmp.finish_md_len = i;
921 memcpy(p, s->s3->tmp.finish_md, i);
922 p += i;
923 l = i;
924
925 /*
926 * Copy the finished so we can use it for
927 * renegotiation checks
928 */
929 if (s->type == SSL_ST_CONNECT) {
930 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
931 memcpy(s->s3->previous_client_finished,
932 s->s3->tmp.finish_md, i);
933 s->s3->previous_client_finished_len = i;
934 } else {
935 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
936 memcpy(s->s3->previous_server_finished,
937 s->s3->tmp.finish_md, i);
938 s->s3->previous_server_finished_len = i;
939 }
940
941 d = dtls1_set_message_header(s, d, SSL3_MT_FINISHED, l, 0, l);
942 s->init_num = (int)l + DTLS1_HM_HEADER_LENGTH;
943 s->init_off = 0;
944
945 /* buffer the message to handle re-xmits */
946 dtls1_buffer_message(s, 0);
947
948 s->state = b;
949 }
950
951 /* SSL3_ST_SEND_xxxxxx_HELLO_B */
952 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
953}
954
955/*
956 * for these 2 messages, we need to
957 * ssl->enc_read_ctx re-init
958 * ssl->s3->read_sequence zero
959 * ssl->s3->read_mac_secret re-init
960 * ssl->session->read_sym_enc assign
961 * ssl->session->read_hash assign
962 */
963int
964dtls1_send_change_cipher_spec(SSL *s, int a, int b)
965{
966 unsigned char *p;
967
968 if (s->state == a) {
969 p = (unsigned char *)s->init_buf->data;
970 *p++=SSL3_MT_CCS;
971 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
972 s->init_num = DTLS1_CCS_HEADER_LENGTH;
973
974 if (s->version == DTLS1_BAD_VER) {
975 s->d1->next_handshake_write_seq++;
976 s2n(s->d1->handshake_write_seq, p);
977 s->init_num += 2;
978 }
979
980 s->init_off = 0;
981
982 dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
983 s->d1->handshake_write_seq, 0, 0);
984
985 /* buffer the message to handle re-xmits */
986 dtls1_buffer_message(s, 1);
987
988 s->state = b;
989 }
990
991 /* SSL3_ST_CW_CHANGE_B */
992 return (dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC));
993}
994
995static int
996dtls1_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
997{
998 int n;
999 unsigned char *p;
1000
1001 n = i2d_X509(x, NULL);
1002 if (!BUF_MEM_grow_clean(buf, n + (*l) + 3)) {
1003 SSLerr(SSL_F_DTLS1_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
1004 return 0;
1005 }
1006 p = (unsigned char *)&(buf->data[*l]);
1007 l2n3(n, p);
1008 i2d_X509(x, &p);
1009 *l += n + 3;
1010
1011 return 1;
1012}
1013
1014unsigned long
1015dtls1_output_cert_chain(SSL *s, X509 *x)
1016{
1017 unsigned char *p;
1018 int i;
1019 unsigned long l = 3 + DTLS1_HM_HEADER_LENGTH;
1020 BUF_MEM *buf;
1021
1022 /* TLSv1 sends a chain with nothing in it, instead of an alert */
1023 buf = s->init_buf;
1024 if (!BUF_MEM_grow_clean(buf, 10)) {
1025 SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN, ERR_R_BUF_LIB);
1026 return (0);
1027 }
1028 if (x != NULL) {
1029 X509_STORE_CTX xs_ctx;
1030
1031 if (!X509_STORE_CTX_init(&xs_ctx, s->ctx->cert_store,
1032 x, NULL)) {
1033 SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN, ERR_R_X509_LIB);
1034 return (0);
1035 }
1036
1037 X509_verify_cert(&xs_ctx);
1038 /* Don't leave errors in the queue */
1039 ERR_clear_error();
1040 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
1041 x = sk_X509_value(xs_ctx.chain, i);
1042
1043 if (!dtls1_add_cert_to_buf(buf, &l, x)) {
1044 X509_STORE_CTX_cleanup(&xs_ctx);
1045 return 0;
1046 }
1047 }
1048 X509_STORE_CTX_cleanup(&xs_ctx);
1049 }
1050 /* Thawte special :-) */
1051 for (i = 0; i < sk_X509_num(s->ctx->extra_certs); i++) {
1052 x = sk_X509_value(s->ctx->extra_certs, i);
1053 if (!dtls1_add_cert_to_buf(buf, &l, x))
1054 return 0;
1055 }
1056
1057 l -= (3 + DTLS1_HM_HEADER_LENGTH);
1058
1059 p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
1060 l2n3(l, p);
1061 l += 3;
1062 p = (unsigned char *)&(buf->data[0]);
1063 p = dtls1_set_message_header(s, p, SSL3_MT_CERTIFICATE, l, 0, l);
1064
1065 l += DTLS1_HM_HEADER_LENGTH;
1066 return (l);
1067}
1068
1069int
1070dtls1_read_failed(SSL *s, int code)
1071{
1072 if (code > 0) {
1073 fprintf(stderr, "invalid state reached %s:%d",
1074 __FILE__, __LINE__);
1075 return 1;
1076 }
1077
1078 if (!dtls1_is_timer_expired(s)) {
1079 /*
1080 * not a timeout, none of our business, let higher layers
1081 * handle this. in fact it's probably an error
1082 */
1083 return code;
1084 }
1085
1086 if (!SSL_in_init(s)) /* done, no need to send a retransmit */
1087 {
1088 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
1089 return code;
1090 }
1091
1092 return dtls1_handle_timeout(s);
1093}
1094
1095int
1096dtls1_get_queue_priority(unsigned short seq, int is_ccs)
1097{
1098 /*
1099 * The index of the retransmission queue actually is the message
1100 * sequence number, since the queue only contains messages of a
1101 * single handshake. However, the ChangeCipherSpec has no message
1102 * sequence number and so using only the sequence will result in
1103 * the CCS and Finished having the same index. To prevent this, the
1104 * sequence number is multiplied by 2. In case of a CCS 1 is
1105 * subtracted. This does not only differ CSS and Finished, it also
1106 * maintains the order of the index (important for priority queues)
1107 * and fits in the unsigned short variable.
1108 */
1109 return seq * 2 - is_ccs;
1110}
1111
1112int
1113dtls1_retransmit_buffered_messages(SSL *s)
1114{
1115 pqueue sent = s->d1->sent_messages;
1116 piterator iter;
1117 pitem *item;
1118 hm_fragment *frag;
1119 int found = 0;
1120
1121 iter = pqueue_iterator(sent);
1122
1123 for (item = pqueue_next(&iter); item != NULL;
1124 item = pqueue_next(&iter)) {
1125 frag = (hm_fragment *)item->data;
1126 if (dtls1_retransmit_message(s,
1127 (unsigned short)dtls1_get_queue_priority(
1128 frag->msg_header.seq, frag->msg_header.is_ccs), 0,
1129 &found) <= 0 && found) {
1130 fprintf(stderr, "dtls1_retransmit_message() failed\n");
1131 return -1;
1132 }
1133 }
1134
1135 return 1;
1136}
1137
1138int
1139dtls1_buffer_message(SSL *s, int is_ccs)
1140{
1141 pitem *item;
1142 hm_fragment *frag;
1143 unsigned char seq64be[8];
1144
1145 /* Buffer the messsage in order to handle DTLS retransmissions. */
1146
1147 /*
1148 * This function is called immediately after a message has
1149 * been serialized
1150 */
1151 OPENSSL_assert(s->init_off == 0);
1152
1153 frag = dtls1_hm_fragment_new(s->init_num, 0);
1154 if (frag == NULL)
1155 return 0;
1156
1157 memcpy(frag->fragment, s->init_buf->data, s->init_num);
1158
1159 if (is_ccs) {
1160 OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
1161 ((s->version == DTLS1_VERSION) ?
1162 DTLS1_CCS_HEADER_LENGTH : 3) == (unsigned int)s->init_num);
1163 } else {
1164 OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
1165 DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);
1166 }
1167
1168 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1169 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1170 frag->msg_header.type = s->d1->w_msg_hdr.type;
1171 frag->msg_header.frag_off = 0;
1172 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1173 frag->msg_header.is_ccs = is_ccs;
1174
1175 /* save current state*/
1176 frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
1177 frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
1178 frag->msg_header.saved_retransmit_state.session = s->session;
1179 frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;
1180
1181 memset(seq64be, 0, sizeof(seq64be));
1182 seq64be[6] = (unsigned char)(dtls1_get_queue_priority(
1183 frag->msg_header.seq, frag->msg_header.is_ccs) >> 8);
1184 seq64be[7] = (unsigned char)(dtls1_get_queue_priority(
1185 frag->msg_header.seq, frag->msg_header.is_ccs));
1186
1187 item = pitem_new(seq64be, frag);
1188 if (item == NULL) {
1189 dtls1_hm_fragment_free(frag);
1190 return 0;
1191 }
1192
1193 pqueue_insert(s->d1->sent_messages, item);
1194 return 1;
1195}
1196
1197int
1198dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
1199 int *found)
1200{
1201 int ret;
1202 /* XDTLS: for now assuming that read/writes are blocking */
1203 pitem *item;
1204 hm_fragment *frag;
1205 unsigned long header_length;
1206 unsigned char seq64be[8];
1207 struct dtls1_retransmit_state saved_state;
1208 unsigned char save_write_sequence[8];
1209
1210 /*
1211 OPENSSL_assert(s->init_num == 0);
1212 OPENSSL_assert(s->init_off == 0);
1213 */
1214
1215 /* XDTLS: the requested message ought to be found, otherwise error */
1216 memset(seq64be, 0, sizeof(seq64be));
1217 seq64be[6] = (unsigned char)(seq >> 8);
1218 seq64be[7] = (unsigned char)seq;
1219
1220 item = pqueue_find(s->d1->sent_messages, seq64be);
1221 if (item == NULL) {
1222 fprintf(stderr, "retransmit: message %d non-existant\n", seq);
1223 *found = 0;
1224 return 0;
1225 }
1226
1227 *found = 1;
1228 frag = (hm_fragment *)item->data;
1229
1230 if (frag->msg_header.is_ccs)
1231 header_length = DTLS1_CCS_HEADER_LENGTH;
1232 else
1233 header_length = DTLS1_HM_HEADER_LENGTH;
1234
1235 memcpy(s->init_buf->data, frag->fragment,
1236 frag->msg_header.msg_len + header_length);
1237 s->init_num = frag->msg_header.msg_len + header_length;
1238
1239 dtls1_set_message_header_int(s, frag->msg_header.type,
1240 frag->msg_header.msg_len, frag->msg_header.seq, 0,
1241 frag->msg_header.frag_len);
1242
1243 /* save current state */
1244 saved_state.enc_write_ctx = s->enc_write_ctx;
1245 saved_state.write_hash = s->write_hash;
1246 saved_state.session = s->session;
1247 saved_state.epoch = s->d1->w_epoch;
1248
1249 s->d1->retransmitting = 1;
1250
1251 /* restore state in which the message was originally sent */
1252 s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
1253 s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
1254 s->session = frag->msg_header.saved_retransmit_state.session;
1255 s->d1->w_epoch = frag->msg_header.saved_retransmit_state.epoch;
1256
1257 if (frag->msg_header.saved_retransmit_state.epoch ==
1258 saved_state.epoch - 1) {
1259 memcpy(save_write_sequence, s->s3->write_sequence,
1260 sizeof(s->s3->write_sequence));
1261 memcpy(s->s3->write_sequence, s->d1->last_write_sequence,
1262 sizeof(s->s3->write_sequence));
1263 }
1264
1265 ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
1266 SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
1267
1268 /* restore current state */
1269 s->enc_write_ctx = saved_state.enc_write_ctx;
1270 s->write_hash = saved_state.write_hash;
1271 s->session = saved_state.session;
1272 s->d1->w_epoch = saved_state.epoch;
1273
1274 if (frag->msg_header.saved_retransmit_state.epoch ==
1275 saved_state.epoch - 1) {
1276 memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
1277 sizeof(s->s3->write_sequence));
1278 memcpy(s->s3->write_sequence, save_write_sequence,
1279 sizeof(s->s3->write_sequence));
1280 }
1281
1282 s->d1->retransmitting = 0;
1283
1284 (void)BIO_flush(SSL_get_wbio(s));
1285 return ret;
1286}
1287
1288/* call this function when the buffered messages are no longer needed */
1289void
1290dtls1_clear_record_buffer(SSL *s)
1291{
1292 pitem *item;
1293
1294 for(item = pqueue_pop(s->d1->sent_messages); item != NULL;
1295 item = pqueue_pop(s->d1->sent_messages)) {
1296 dtls1_hm_fragment_free((hm_fragment *)item->data);
1297 pitem_free(item);
1298 }
1299}
1300
1301unsigned char *
1302dtls1_set_message_header(SSL *s, unsigned char *p, unsigned char mt,
1303 unsigned long len, unsigned long frag_off, unsigned long frag_len)
1304{
1305 /* Don't change sequence numbers while listening */
1306 if (frag_off == 0 && !s->d1->listen) {
1307 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1308 s->d1->next_handshake_write_seq++;
1309 }
1310
1311 dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
1312 frag_off, frag_len);
1313
1314 return p += DTLS1_HM_HEADER_LENGTH;
1315}
1316
1317/* don't actually do the writing, wait till the MTU has been retrieved */
1318static void
1319dtls1_set_message_header_int(SSL *s, unsigned char mt, unsigned long len,
1320 unsigned short seq_num, unsigned long frag_off, unsigned long frag_len)
1321{
1322 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1323
1324 msg_hdr->type = mt;
1325 msg_hdr->msg_len = len;
1326 msg_hdr->seq = seq_num;
1327 msg_hdr->frag_off = frag_off;
1328 msg_hdr->frag_len = frag_len;
1329}
1330
1331static void
1332dtls1_fix_message_header(SSL *s, unsigned long frag_off, unsigned long frag_len)
1333{
1334 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1335
1336 msg_hdr->frag_off = frag_off;
1337 msg_hdr->frag_len = frag_len;
1338}
1339
1340static unsigned char *
1341dtls1_write_message_header(SSL *s, unsigned char *p)
1342{
1343 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1344
1345 *p++ = msg_hdr->type;
1346 l2n3(msg_hdr->msg_len, p);
1347
1348 s2n(msg_hdr->seq, p);
1349 l2n3(msg_hdr->frag_off, p);
1350 l2n3(msg_hdr->frag_len, p);
1351
1352 return p;
1353}
1354
1355unsigned int
1356dtls1_min_mtu(void)
1357{
1358 return (g_probable_mtu[(sizeof(g_probable_mtu) /
1359 sizeof(g_probable_mtu[0])) - 1]);
1360}
1361
1362static unsigned int
1363dtls1_guess_mtu(unsigned int curr_mtu)
1364{
1365 unsigned int i;
1366
1367 if (curr_mtu == 0)
1368 return g_probable_mtu[0];
1369
1370 for (i = 0; i < sizeof(g_probable_mtu) / sizeof(g_probable_mtu[0]); i++)
1371 if (curr_mtu > g_probable_mtu[i])
1372 return g_probable_mtu[i];
1373
1374 return curr_mtu;
1375}
1376
1377int
1378dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
1379{
1380 CBS header;
1381 uint32_t msg_len, frag_off, frag_len;
1382 uint16_t seq;
1383 uint8_t type;
1384
1385 CBS_init(&header, data, sizeof(*msg_hdr));
1386
1387 memset(msg_hdr, 0, sizeof(*msg_hdr));
1388
1389 if (!CBS_get_u8(&header, &type))
1390 return 0;
1391 if (!CBS_get_u24(&header, &msg_len))
1392 return 0;
1393 if (!CBS_get_u16(&header, &seq))
1394 return 0;
1395 if (!CBS_get_u24(&header, &frag_off))
1396 return 0;
1397 if (!CBS_get_u24(&header, &frag_len))
1398 return 0;
1399
1400 msg_hdr->type = type;
1401 msg_hdr->msg_len = msg_len;
1402 msg_hdr->seq = seq;
1403 msg_hdr->frag_off = frag_off;
1404 msg_hdr->frag_len = frag_len;
1405
1406 return 1;
1407}
1408
1409void
1410dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr)
1411{
1412 memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
1413
1414 ccs_hdr->type = *(data++);
1415}
1416
1417int
1418dtls1_shutdown(SSL *s)
1419{
1420 int ret;
1421
1422 ret = ssl3_shutdown(s);
1423 return ret;
1424}