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