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