Convert dtls_write_records to use standard record layer functions
[openssl.git] / ssl / statem / statem_dtls.c
1 /*
2  * Copyright 2005-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <limits.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include "../ssl_local.h"
14 #include "statem_local.h"
15 #include "internal/cryptlib.h"
16 #include <openssl/buffer.h>
17 #include <openssl/objects.h>
18 #include <openssl/evp.h>
19 #include <openssl/x509.h>
20
21 #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
22
23 #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
24                         if ((end) - (start) <= 8) { \
25                                 long ii; \
26                                 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
27                         } else { \
28                                 long ii; \
29                                 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
30                                 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
31                                 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
32                         } }
33
34 #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
35                         long ii; \
36                         is_complete = 1; \
37                         if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
38                         if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
39                                 if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
40
41 static unsigned char bitmask_start_values[] =
42     { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 };
43 static unsigned char bitmask_end_values[] =
44     { 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f };
45
46 static void dtls1_fix_message_header(SSL_CONNECTION *s, size_t frag_off,
47                                      size_t frag_len);
48 static unsigned char *dtls1_write_message_header(SSL_CONNECTION *s,
49                                                  unsigned char *p);
50 static void dtls1_set_message_header_int(SSL_CONNECTION *s, unsigned char mt,
51                                          size_t len,
52                                          unsigned short seq_num,
53                                          size_t frag_off,
54                                          size_t frag_len);
55 static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype,
56                                         size_t *len);
57
58 static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly)
59 {
60     hm_fragment *frag = NULL;
61     unsigned char *buf = NULL;
62     unsigned char *bitmask = NULL;
63
64     if ((frag = OPENSSL_malloc(sizeof(*frag))) == NULL)
65         return NULL;
66
67     if (frag_len) {
68         if ((buf = OPENSSL_malloc(frag_len)) == NULL) {
69             OPENSSL_free(frag);
70             return NULL;
71         }
72     }
73
74     /* zero length fragment gets zero frag->fragment */
75     frag->fragment = buf;
76
77     /* Initialize reassembly bitmask if necessary */
78     if (reassembly) {
79         bitmask = OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len));
80         if (bitmask == NULL) {
81             OPENSSL_free(buf);
82             OPENSSL_free(frag);
83             return NULL;
84         }
85     }
86
87     frag->reassembly = bitmask;
88
89     return frag;
90 }
91
92 void dtls1_hm_fragment_free(hm_fragment *frag)
93 {
94     if (!frag)
95         return;
96     if (frag->msg_header.is_ccs) {
97         /*
98          * If we're freeing the CCS then we're done with the old wrl and it
99          * can bee freed
100          */
101         if (frag->msg_header.saved_retransmit_state.wrlmethod != NULL)
102             frag->msg_header.saved_retransmit_state.wrlmethod->free(frag->msg_header.saved_retransmit_state.wrl);
103     }
104     OPENSSL_free(frag->fragment);
105     OPENSSL_free(frag->reassembly);
106     OPENSSL_free(frag);
107 }
108
109 /*
110  * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
111  * SSL3_RT_CHANGE_CIPHER_SPEC)
112  */
113 int dtls1_do_write(SSL_CONNECTION *s, int type)
114 {
115     int ret;
116     size_t written;
117     size_t curr_mtu;
118     int retry = 1;
119     size_t len, frag_off, mac_size, blocksize, used_len;
120     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
121
122     if (!dtls1_query_mtu(s))
123         return -1;
124
125     if (s->d1->mtu < dtls1_min_mtu(s))
126         /* should have something reasonable now */
127         return -1;
128
129     if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
130         if (!ossl_assert(s->init_num ==
131                          s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH))
132             return -1;
133     }
134
135     if (s->write_hash) {
136         if (s->enc_write_ctx
137             && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx)) &
138                 EVP_CIPH_FLAG_AEAD_CIPHER) != 0)
139             mac_size = 0;
140         else
141             mac_size = EVP_MD_CTX_get_size(s->write_hash);
142     } else
143         mac_size = 0;
144
145     if (s->enc_write_ctx &&
146         (EVP_CIPHER_CTX_get_mode(s->enc_write_ctx) == EVP_CIPH_CBC_MODE))
147         blocksize = 2 * EVP_CIPHER_CTX_get_block_size(s->enc_write_ctx);
148     else
149         blocksize = 0;
150
151     frag_off = 0;
152     s->rwstate = SSL_NOTHING;
153
154     /* s->init_num shouldn't ever be < 0...but just in case */
155     while (s->init_num > 0) {
156         if (type == SSL3_RT_HANDSHAKE && s->init_off != 0) {
157             /* We must be writing a fragment other than the first one */
158
159             if (frag_off > 0) {
160                 /* This is the first attempt at writing out this fragment */
161
162                 if (s->init_off <= DTLS1_HM_HEADER_LENGTH) {
163                     /*
164                      * Each fragment that was already sent must at least have
165                      * contained the message header plus one other byte.
166                      * Therefore |init_off| must have progressed by at least
167                      * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went
168                      * wrong.
169                      */
170                     return -1;
171                 }
172
173                 /*
174                  * Adjust |init_off| and |init_num| to allow room for a new
175                  * message header for this fragment.
176                  */
177                 s->init_off -= DTLS1_HM_HEADER_LENGTH;
178                 s->init_num += DTLS1_HM_HEADER_LENGTH;
179             } else {
180                 /*
181                  * We must have been called again after a retry so use the
182                  * fragment offset from our last attempt. We do not need
183                  * to adjust |init_off| and |init_num| as above, because
184                  * that should already have been done before the retry.
185                  */
186                 frag_off = s->d1->w_msg_hdr.frag_off;
187             }
188         }
189
190         used_len = BIO_wpending(s->wbio) + DTLS1_RT_HEADER_LENGTH
191             + mac_size + blocksize;
192         if (s->d1->mtu > used_len)
193             curr_mtu = s->d1->mtu - used_len;
194         else
195             curr_mtu = 0;
196
197         if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
198             /*
199              * grr.. we could get an error if MTU picked was wrong
200              */
201             ret = BIO_flush(s->wbio);
202             if (ret <= 0) {
203                 s->rwstate = SSL_WRITING;
204                 return ret;
205             }
206             used_len = DTLS1_RT_HEADER_LENGTH + mac_size + blocksize;
207             if (s->d1->mtu > used_len + DTLS1_HM_HEADER_LENGTH) {
208                 curr_mtu = s->d1->mtu - used_len;
209             } else {
210                 /* Shouldn't happen */
211                 return -1;
212             }
213         }
214
215         /*
216          * We just checked that s->init_num > 0 so this cast should be safe
217          */
218         if (((unsigned int)s->init_num) > curr_mtu)
219             len = curr_mtu;
220         else
221             len = s->init_num;
222
223         if (len > ssl_get_max_send_fragment(s))
224             len = ssl_get_max_send_fragment(s);
225
226         /*
227          * XDTLS: this function is too long.  split out the CCS part
228          */
229         if (type == SSL3_RT_HANDSHAKE) {
230             if (len < DTLS1_HM_HEADER_LENGTH) {
231                 /*
232                  * len is so small that we really can't do anything sensible
233                  * so fail
234                  */
235                 return -1;
236             }
237             dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
238
239             dtls1_write_message_header(s,
240                                        (unsigned char *)&s->init_buf->
241                                        data[s->init_off]);
242         }
243
244         ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len,
245                                 &written);
246         if (ret <= 0) {
247             /*
248              * might need to update MTU here, but we don't know which
249              * previous packet caused the failure -- so can't really
250              * retransmit anything.  continue as if everything is fine and
251              * wait for an alert to handle the retransmit
252              */
253             if (retry && BIO_ctrl(SSL_get_wbio(ssl),
254                                   BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {
255                 if (!(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
256                     if (!dtls1_query_mtu(s))
257                         return -1;
258                     /* Have one more go */
259                     retry = 0;
260                 } else
261                     return -1;
262             } else {
263                 return -1;
264             }
265         } else {
266
267             /*
268              * bad if this assert fails, only part of the handshake message
269              * got sent.  but why would this happen?
270              */
271             if (!ossl_assert(len == written))
272                 return -1;
273
274             if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) {
275                 /*
276                  * should not be done for 'Hello Request's, but in that case
277                  * we'll ignore the result anyway
278                  */
279                 unsigned char *p =
280                     (unsigned char *)&s->init_buf->data[s->init_off];
281                 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
282                 size_t xlen;
283
284                 if (frag_off == 0 && s->version != DTLS1_BAD_VER) {
285                     /*
286                      * reconstruct message header is if it is being sent in
287                      * single fragment
288                      */
289                     *p++ = msg_hdr->type;
290                     l2n3(msg_hdr->msg_len, p);
291                     s2n(msg_hdr->seq, p);
292                     l2n3(0, p);
293                     l2n3(msg_hdr->msg_len, p);
294                     p -= DTLS1_HM_HEADER_LENGTH;
295                     xlen = written;
296                 } else {
297                     p += DTLS1_HM_HEADER_LENGTH;
298                     xlen = written - DTLS1_HM_HEADER_LENGTH;
299                 }
300
301                 if (!ssl3_finish_mac(s, p, xlen))
302                     return -1;
303             }
304
305             if (written == s->init_num) {
306                 if (s->msg_callback)
307                     s->msg_callback(1, s->version, type, s->init_buf->data,
308                                     (size_t)(s->init_off + s->init_num), ssl,
309                                     s->msg_callback_arg);
310
311                 s->init_off = 0; /* done writing this message */
312                 s->init_num = 0;
313
314                 return 1;
315             }
316             s->init_off += written;
317             s->init_num -= written;
318             written -= DTLS1_HM_HEADER_LENGTH;
319             frag_off += written;
320
321             /*
322              * We save the fragment offset for the next fragment so we have it
323              * available in case of an IO retry. We don't know the length of the
324              * next fragment yet so just set that to 0 for now. It will be
325              * updated again later.
326              */
327             dtls1_fix_message_header(s, frag_off, 0);
328         }
329     }
330     return 0;
331 }
332
333 int dtls_get_message(SSL_CONNECTION *s, int *mt)
334 {
335     struct hm_header_st *msg_hdr;
336     unsigned char *p;
337     size_t msg_len;
338     size_t tmplen;
339     int errtype;
340
341     msg_hdr = &s->d1->r_msg_hdr;
342     memset(msg_hdr, 0, sizeof(*msg_hdr));
343
344  again:
345     if (!dtls_get_reassembled_message(s, &errtype, &tmplen)) {
346         if (errtype == DTLS1_HM_BAD_FRAGMENT
347                 || errtype == DTLS1_HM_FRAGMENT_RETRY) {
348             /* bad fragment received */
349             goto again;
350         }
351         return 0;
352     }
353
354     *mt = s->s3.tmp.message_type;
355
356     p = (unsigned char *)s->init_buf->data;
357
358     if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
359         if (s->msg_callback) {
360             s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
361                             p, 1, SSL_CONNECTION_GET_SSL(s),
362                             s->msg_callback_arg);
363         }
364         /*
365          * This isn't a real handshake message so skip the processing below.
366          */
367         return 1;
368     }
369
370     msg_len = msg_hdr->msg_len;
371
372     /* reconstruct message header */
373     *(p++) = msg_hdr->type;
374     l2n3(msg_len, p);
375     s2n(msg_hdr->seq, p);
376     l2n3(0, p);
377     l2n3(msg_len, p);
378
379     memset(msg_hdr, 0, sizeof(*msg_hdr));
380
381     s->d1->handshake_read_seq++;
382
383     s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
384
385     return 1;
386 }
387
388 /*
389  * Actually we already have the message body - but this is an opportunity for
390  * DTLS to do any further processing it wants at the same point that TLS would
391  * be asked for the message body.
392  */
393 int dtls_get_message_body(SSL_CONNECTION *s, size_t *len)
394 {
395     unsigned char *msg = (unsigned char *)s->init_buf->data;
396     size_t msg_len = s->init_num + DTLS1_HM_HEADER_LENGTH;
397
398     if (s->s3.tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
399         /* Nothing to be done */
400         goto end;
401     }
402     /*
403      * If receiving Finished, record MAC of prior handshake messages for
404      * Finished verification.
405      */
406     if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
407         /* SSLfatal() already called */
408         return 0;
409     }
410
411     if (s->version == DTLS1_BAD_VER) {
412         msg += DTLS1_HM_HEADER_LENGTH;
413         msg_len -= DTLS1_HM_HEADER_LENGTH;
414     }
415
416     if (!ssl3_finish_mac(s, msg, msg_len))
417         return 0;
418
419     if (s->msg_callback)
420         s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
421                         s->init_buf->data, s->init_num + DTLS1_HM_HEADER_LENGTH,
422                         SSL_CONNECTION_GET_SSL(s), s->msg_callback_arg);
423
424  end:
425     *len = s->init_num;
426     return 1;
427 }
428
429 /*
430  * dtls1_max_handshake_message_len returns the maximum number of bytes
431  * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
432  * may be greater if the maximum certificate list size requires it.
433  */
434 static size_t dtls1_max_handshake_message_len(const SSL_CONNECTION *s)
435 {
436     size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
437     if (max_len < s->max_cert_list)
438         return s->max_cert_list;
439     return max_len;
440 }
441
442 static int dtls1_preprocess_fragment(SSL_CONNECTION *s,
443                                      struct hm_header_st *msg_hdr)
444 {
445     size_t frag_off, frag_len, msg_len;
446
447     msg_len = msg_hdr->msg_len;
448     frag_off = msg_hdr->frag_off;
449     frag_len = msg_hdr->frag_len;
450
451     /* sanity checking */
452     if ((frag_off + frag_len) > msg_len
453             || msg_len > dtls1_max_handshake_message_len(s)) {
454         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
455         return 0;
456     }
457
458     if (s->d1->r_msg_hdr.frag_off == 0) { /* first fragment */
459         /*
460          * msg_len is limited to 2^24, but is effectively checked against
461          * dtls_max_handshake_message_len(s) above
462          */
463         if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
464             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB);
465             return 0;
466         }
467
468         s->s3.tmp.message_size = msg_len;
469         s->d1->r_msg_hdr.msg_len = msg_len;
470         s->s3.tmp.message_type = msg_hdr->type;
471         s->d1->r_msg_hdr.type = msg_hdr->type;
472         s->d1->r_msg_hdr.seq = msg_hdr->seq;
473     } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
474         /*
475          * They must be playing with us! BTW, failure to enforce upper limit
476          * would open possibility for buffer overrun.
477          */
478         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
479         return 0;
480     }
481
482     return 1;
483 }
484
485 /*
486  * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a
487  * fatal error.
488  */
489 static int dtls1_retrieve_buffered_fragment(SSL_CONNECTION *s, size_t *len)
490 {
491     /*-
492      * (0) check whether the desired fragment is available
493      * if so:
494      * (1) copy over the fragment to s->init_buf->data[]
495      * (2) update s->init_num
496      */
497     pitem *item;
498     piterator iter;
499     hm_fragment *frag;
500     int ret;
501     int chretran = 0;
502
503     iter = pqueue_iterator(s->d1->buffered_messages);
504     do {
505         item = pqueue_next(&iter);
506         if (item == NULL)
507             return 0;
508
509         frag = (hm_fragment *)item->data;
510
511         if (frag->msg_header.seq < s->d1->handshake_read_seq) {
512             pitem *next;
513             hm_fragment *nextfrag;
514
515             if (!s->server
516                     || frag->msg_header.seq != 0
517                     || s->d1->handshake_read_seq != 1
518                     || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) {
519                 /*
520                  * This is a stale message that has been buffered so clear it.
521                  * It is safe to pop this message from the queue even though
522                  * we have an active iterator
523                  */
524                 pqueue_pop(s->d1->buffered_messages);
525                 dtls1_hm_fragment_free(frag);
526                 pitem_free(item);
527                 item = NULL;
528                 frag = NULL;
529             } else {
530                 /*
531                  * We have fragments for a ClientHello without a cookie,
532                  * even though we have sent a HelloVerifyRequest. It is possible
533                  * that the HelloVerifyRequest got lost and this is a
534                  * retransmission of the original ClientHello
535                  */
536                 next = pqueue_next(&iter);
537                 if (next != NULL) {
538                     nextfrag = (hm_fragment *)next->data;
539                     if (nextfrag->msg_header.seq == s->d1->handshake_read_seq) {
540                         /*
541                         * We have fragments for both a ClientHello without
542                         * cookie and one with. Ditch the one without.
543                         */
544                         pqueue_pop(s->d1->buffered_messages);
545                         dtls1_hm_fragment_free(frag);
546                         pitem_free(item);
547                         item = next;
548                         frag = nextfrag;
549                     } else {
550                         chretran = 1;
551                     }
552                 } else {
553                     chretran = 1;
554                 }
555             }
556         }
557     } while (item == NULL);
558
559     /* Don't return if reassembly still in progress */
560     if (frag->reassembly != NULL)
561         return 0;
562
563     if (s->d1->handshake_read_seq == frag->msg_header.seq || chretran) {
564         size_t frag_len = frag->msg_header.frag_len;
565         pqueue_pop(s->d1->buffered_messages);
566
567         /* Calls SSLfatal() as required */
568         ret = dtls1_preprocess_fragment(s, &frag->msg_header);
569
570         if (ret && frag->msg_header.frag_len > 0) {
571             unsigned char *p =
572                 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
573             memcpy(&p[frag->msg_header.frag_off], frag->fragment,
574                    frag->msg_header.frag_len);
575         }
576
577         dtls1_hm_fragment_free(frag);
578         pitem_free(item);
579
580         if (ret) {
581             if (chretran) {
582                 /*
583                  * We got a new ClientHello with a message sequence of 0.
584                  * Reset the read/write sequences back to the beginning.
585                  * We process it like this is the first time we've seen a
586                  * ClientHello from the client.
587                  */
588                 s->d1->handshake_read_seq = 0;
589                 s->d1->next_handshake_write_seq = 0;
590             }
591             *len = frag_len;
592             return 1;
593         }
594
595         /* Fatal error */
596         s->init_num = 0;
597         return -1;
598     } else {
599         return 0;
600     }
601 }
602
603 static int dtls1_reassemble_fragment(SSL_CONNECTION *s,
604                                      const struct hm_header_st *msg_hdr)
605 {
606     hm_fragment *frag = NULL;
607     pitem *item = NULL;
608     int i = -1, is_complete;
609     unsigned char seq64be[8];
610     size_t frag_len = msg_hdr->frag_len;
611     size_t readbytes;
612     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
613
614     if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
615         msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
616         goto err;
617
618     if (frag_len == 0) {
619         return DTLS1_HM_FRAGMENT_RETRY;
620     }
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         frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
630         if (frag == NULL)
631             goto err;
632         memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
633         frag->msg_header.frag_len = frag->msg_header.msg_len;
634         frag->msg_header.frag_off = 0;
635     } else {
636         frag = (hm_fragment *)item->data;
637         if (frag->msg_header.msg_len != msg_hdr->msg_len) {
638             item = NULL;
639             frag = NULL;
640             goto err;
641         }
642     }
643
644     /*
645      * If message is already reassembled, this must be a retransmit and can
646      * be dropped. In this case item != NULL and so frag does not need to be
647      * freed.
648      */
649     if (frag->reassembly == NULL) {
650         unsigned char devnull[256];
651
652         while (frag_len) {
653             i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
654                                             devnull,
655                                             frag_len >
656                                             sizeof(devnull) ? sizeof(devnull) :
657                                             frag_len, 0, &readbytes);
658             if (i <= 0)
659                 goto err;
660             frag_len -= readbytes;
661         }
662         return DTLS1_HM_FRAGMENT_RETRY;
663     }
664
665     /* read the body of the fragment (header has already been read */
666     i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
667                                     frag->fragment + msg_hdr->frag_off,
668                                     frag_len, 0, &readbytes);
669     if (i <= 0 || readbytes != frag_len)
670         i = -1;
671     if (i <= 0)
672         goto err;
673
674     RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
675                         (long)(msg_hdr->frag_off + frag_len));
676
677     if (!ossl_assert(msg_hdr->msg_len > 0))
678         goto err;
679     RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
680                                is_complete);
681
682     if (is_complete) {
683         OPENSSL_free(frag->reassembly);
684         frag->reassembly = NULL;
685     }
686
687     if (item == NULL) {
688         item = pitem_new(seq64be, frag);
689         if (item == NULL) {
690             i = -1;
691             goto err;
692         }
693
694         item = pqueue_insert(s->d1->buffered_messages, item);
695         /*
696          * pqueue_insert fails iff a duplicate item is inserted. However,
697          * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
698          * would have returned it and control would never have reached this
699          * branch.
700          */
701         if (!ossl_assert(item != NULL))
702             goto err;
703     }
704
705     return DTLS1_HM_FRAGMENT_RETRY;
706
707  err:
708     if (item == NULL)
709         dtls1_hm_fragment_free(frag);
710     return -1;
711 }
712
713 static int dtls1_process_out_of_seq_message(SSL_CONNECTION *s,
714                                             const struct hm_header_st *msg_hdr)
715 {
716     int i = -1;
717     hm_fragment *frag = NULL;
718     pitem *item = NULL;
719     unsigned char seq64be[8];
720     size_t frag_len = msg_hdr->frag_len;
721     size_t readbytes;
722     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
723
724     if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
725         goto err;
726
727     /* Try to find item in queue, to prevent duplicate entries */
728     memset(seq64be, 0, sizeof(seq64be));
729     seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
730     seq64be[7] = (unsigned char)msg_hdr->seq;
731     item = pqueue_find(s->d1->buffered_messages, seq64be);
732
733     /*
734      * If we already have an entry and this one is a fragment, don't discard
735      * it and rather try to reassemble it.
736      */
737     if (item != NULL && frag_len != msg_hdr->msg_len)
738         item = NULL;
739
740     /*
741      * Discard the message if sequence number was already there, is too far
742      * in the future, already in the queue or if we received a FINISHED
743      * before the SERVER_HELLO, which then must be a stale retransmit.
744      */
745     if (msg_hdr->seq <= s->d1->handshake_read_seq ||
746         msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
747         (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
748         unsigned char devnull[256];
749
750         while (frag_len) {
751             i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
752                                             devnull,
753                                             frag_len >
754                                             sizeof(devnull) ? sizeof(devnull) :
755                                             frag_len, 0, &readbytes);
756             if (i <= 0)
757                 goto err;
758             frag_len -= readbytes;
759         }
760     } else {
761         if (frag_len != msg_hdr->msg_len) {
762             return dtls1_reassemble_fragment(s, msg_hdr);
763         }
764
765         if (frag_len > dtls1_max_handshake_message_len(s))
766             goto err;
767
768         frag = dtls1_hm_fragment_new(frag_len, 0);
769         if (frag == NULL)
770             goto err;
771
772         memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
773
774         if (frag_len) {
775             /*
776              * read the body of the fragment (header has already been read
777              */
778             i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
779                                             frag->fragment, frag_len, 0,
780                                             &readbytes);
781             if (i<=0 || readbytes != frag_len)
782                 i = -1;
783             if (i <= 0)
784                 goto err;
785         }
786
787         item = pitem_new(seq64be, frag);
788         if (item == NULL)
789             goto err;
790
791         item = pqueue_insert(s->d1->buffered_messages, item);
792         /*
793          * pqueue_insert fails iff a duplicate item is inserted. However,
794          * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
795          * would have returned it. Then, either |frag_len| !=
796          * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
797          * have been processed with |dtls1_reassemble_fragment|, above, or
798          * the record will have been discarded.
799          */
800         if (!ossl_assert(item != NULL))
801             goto err;
802     }
803
804     return DTLS1_HM_FRAGMENT_RETRY;
805
806  err:
807     if (item == NULL)
808         dtls1_hm_fragment_free(frag);
809     return 0;
810 }
811
812 static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype,
813                                         size_t *len)
814 {
815     unsigned char wire[DTLS1_HM_HEADER_LENGTH];
816     size_t mlen, frag_off, frag_len;
817     int i, ret, recvd_type;
818     struct hm_header_st msg_hdr;
819     size_t readbytes;
820     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
821     int chretran = 0;
822
823     *errtype = 0;
824
825  redo:
826     /* see if we have the required fragment already */
827     ret = dtls1_retrieve_buffered_fragment(s, &frag_len);
828     if (ret < 0) {
829         /* SSLfatal() already called */
830         return 0;
831     }
832     if (ret > 0) {
833         s->init_num = frag_len;
834         *len = frag_len;
835         return 1;
836     }
837
838     /* read handshake message header */
839     i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, &recvd_type, wire,
840                                     DTLS1_HM_HEADER_LENGTH, 0, &readbytes);
841     if (i <= 0) {               /* nbio, or an error */
842         s->rwstate = SSL_READING;
843         *len = 0;
844         return 0;
845     }
846     if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
847         if (wire[0] != SSL3_MT_CCS) {
848             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
849                      SSL_R_BAD_CHANGE_CIPHER_SPEC);
850             goto f_err;
851         }
852
853         memcpy(s->init_buf->data, wire, readbytes);
854         s->init_num = readbytes - 1;
855         s->init_msg = s->init_buf->data + 1;
856         s->s3.tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
857         s->s3.tmp.message_size = readbytes - 1;
858         *len = readbytes - 1;
859         return 1;
860     }
861
862     /* Handshake fails if message header is incomplete */
863     if (readbytes != DTLS1_HM_HEADER_LENGTH) {
864         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
865         goto f_err;
866     }
867
868     /* parse the message fragment header */
869     dtls1_get_message_header(wire, &msg_hdr);
870
871     mlen = msg_hdr.msg_len;
872     frag_off = msg_hdr.frag_off;
873     frag_len = msg_hdr.frag_len;
874
875     /*
876      * We must have at least frag_len bytes left in the record to be read.
877      * Fragments must not span records.
878      */
879     if (frag_len > s->rlayer.tlsrecs[s->rlayer.curr_rec].length) {
880         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);
881         goto f_err;
882     }
883
884     /*
885      * if this is a future (or stale) message it gets buffered
886      * (or dropped)--no further processing at this time
887      * While listening, we accept seq 1 (ClientHello with cookie)
888      * although we're still expecting seq 0 (ClientHello)
889      */
890     if (msg_hdr.seq != s->d1->handshake_read_seq) {
891         if (!s->server
892                 || msg_hdr.seq != 0
893                 || s->d1->handshake_read_seq != 1
894                 || wire[0] != SSL3_MT_CLIENT_HELLO
895                 || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) {
896             *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr);
897             return 0;
898         }
899         /*
900          * We received a ClientHello and sent back a HelloVerifyRequest. We
901          * now seem to have received a retransmitted initial ClientHello. That
902          * is allowed (possibly our HelloVerifyRequest got lost).
903          */
904         chretran = 1;
905     }
906
907     if (frag_len && frag_len < mlen) {
908         *errtype = dtls1_reassemble_fragment(s, &msg_hdr);
909         return 0;
910     }
911
912     if (!s->server
913             && s->d1->r_msg_hdr.frag_off == 0
914             && s->statem.hand_state != TLS_ST_OK
915             && wire[0] == SSL3_MT_HELLO_REQUEST) {
916         /*
917          * The server may always send 'Hello Request' messages -- we are
918          * doing a handshake anyway now, so ignore them if their format is
919          * correct. Does not count for 'Finished' MAC.
920          */
921         if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
922             if (s->msg_callback)
923                 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
924                                 wire, DTLS1_HM_HEADER_LENGTH, ssl,
925                                 s->msg_callback_arg);
926
927             s->init_num = 0;
928             goto redo;
929         } else {                /* Incorrectly formatted Hello request */
930
931             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
932             goto f_err;
933         }
934     }
935
936     if (!dtls1_preprocess_fragment(s, &msg_hdr)) {
937         /* SSLfatal() already called */
938         goto f_err;
939     }
940
941     if (frag_len > 0) {
942         unsigned char *p =
943             (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
944
945         i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
946                                         &p[frag_off], frag_len, 0, &readbytes);
947
948         /*
949          * This shouldn't ever fail due to NBIO because we already checked
950          * that we have enough data in the record
951          */
952         if (i <= 0) {
953             s->rwstate = SSL_READING;
954             *len = 0;
955             return 0;
956         }
957     } else {
958         readbytes = 0;
959     }
960
961     /*
962      * XDTLS: an incorrectly formatted fragment should cause the handshake
963      * to fail
964      */
965     if (readbytes != frag_len) {
966         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);
967         goto f_err;
968     }
969
970     if (chretran) {
971         /*
972          * We got a new ClientHello with a message sequence of 0.
973          * Reset the read/write sequences back to the beginning.
974          * We process it like this is the first time we've seen a ClientHello
975          * from the client.
976          */
977         s->d1->handshake_read_seq = 0;
978         s->d1->next_handshake_write_seq = 0;
979     }
980
981     /*
982      * Note that s->init_num is *not* used as current offset in
983      * s->init_buf->data, but as a counter summing up fragments' lengths: as
984      * soon as they sum up to handshake packet length, we assume we have got
985      * all the fragments.
986      */
987     *len = s->init_num = frag_len;
988     return 1;
989
990  f_err:
991     s->init_num = 0;
992     *len = 0;
993     return 0;
994 }
995
996 /*-
997  * for these 2 messages, we need to
998  * ssl->enc_read_ctx                    re-init
999  * ssl->s3.read_mac_secret             re-init
1000  * ssl->session->read_sym_enc           assign
1001  * ssl->session->read_compression       assign
1002  * ssl->session->read_hash              assign
1003  */
1004 CON_FUNC_RETURN dtls_construct_change_cipher_spec(SSL_CONNECTION *s,
1005                                                   WPACKET *pkt)
1006 {
1007     if (s->version == DTLS1_BAD_VER) {
1008         s->d1->next_handshake_write_seq++;
1009
1010         if (!WPACKET_put_bytes_u16(pkt, s->d1->handshake_write_seq)) {
1011             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1012             return CON_FUNC_ERROR;
1013         }
1014     }
1015
1016     return CON_FUNC_SUCCESS;
1017 }
1018
1019 #ifndef OPENSSL_NO_SCTP
1020 /*
1021  * Wait for a dry event. Should only be called at a point in the handshake
1022  * where we are not expecting any data from the peer except an alert.
1023  */
1024 WORK_STATE dtls_wait_for_dry(SSL_CONNECTION *s)
1025 {
1026     int ret, errtype;
1027     size_t len;
1028     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1029
1030     /* read app data until dry event */
1031     ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(ssl));
1032     if (ret < 0) {
1033         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1034         return WORK_ERROR;
1035     }
1036
1037     if (ret == 0) {
1038         /*
1039          * We're not expecting any more messages from the peer at this point -
1040          * but we could get an alert. If an alert is waiting then we will never
1041          * return successfully. Therefore we attempt to read a message. This
1042          * should never succeed but will process any waiting alerts.
1043          */
1044         if (dtls_get_reassembled_message(s, &errtype, &len)) {
1045             /* The call succeeded! This should never happen */
1046             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
1047             return WORK_ERROR;
1048         }
1049
1050         s->s3.in_read_app_data = 2;
1051         s->rwstate = SSL_READING;
1052         BIO_clear_retry_flags(SSL_get_rbio(ssl));
1053         BIO_set_retry_read(SSL_get_rbio(ssl));
1054         return WORK_MORE_A;
1055     }
1056     return WORK_FINISHED_CONTINUE;
1057 }
1058 #endif
1059
1060 int dtls1_read_failed(SSL_CONNECTION *s, int code)
1061 {
1062     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1063
1064     if (code > 0) {
1065         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1066         return 0;
1067     }
1068
1069     if (!dtls1_is_timer_expired(s) || ossl_statem_in_error(s)) {
1070         /*
1071          * not a timeout, none of our business, let higher layers handle
1072          * this.  in fact it's probably an error
1073          */
1074         return code;
1075     }
1076     /* done, no need to send a retransmit */
1077     if (!SSL_in_init(ssl))
1078     {
1079         BIO_set_flags(SSL_get_rbio(ssl), BIO_FLAGS_READ);
1080         return code;
1081     }
1082
1083     return dtls1_handle_timeout(s);
1084 }
1085
1086 int dtls1_get_queue_priority(unsigned short seq, int is_ccs)
1087 {
1088     /*
1089      * The index of the retransmission queue actually is the message sequence
1090      * number, since the queue only contains messages of a single handshake.
1091      * However, the ChangeCipherSpec has no message sequence number and so
1092      * using only the sequence will result in the CCS and Finished having the
1093      * same index. To prevent this, the sequence number is multiplied by 2.
1094      * In case of a CCS 1 is subtracted. This does not only differ CSS and
1095      * Finished, it also maintains the order of the index (important for
1096      * priority queues) and fits in the unsigned short variable.
1097      */
1098     return seq * 2 - is_ccs;
1099 }
1100
1101 int dtls1_retransmit_buffered_messages(SSL_CONNECTION *s)
1102 {
1103     pqueue *sent = s->d1->sent_messages;
1104     piterator iter;
1105     pitem *item;
1106     hm_fragment *frag;
1107     int found = 0;
1108
1109     iter = pqueue_iterator(sent);
1110
1111     for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
1112         frag = (hm_fragment *)item->data;
1113         if (dtls1_retransmit_message(s, (unsigned short)
1114                                      dtls1_get_queue_priority
1115                                      (frag->msg_header.seq,
1116                                       frag->msg_header.is_ccs), &found) <= 0)
1117             return -1;
1118     }
1119
1120     return 1;
1121 }
1122
1123 int dtls1_buffer_message(SSL_CONNECTION *s, int is_ccs)
1124 {
1125     pitem *item;
1126     hm_fragment *frag;
1127     unsigned char seq64be[8];
1128
1129     /*
1130      * this function is called immediately after a message has been
1131      * serialized
1132      */
1133     if (!ossl_assert(s->init_off == 0))
1134         return 0;
1135
1136     frag = dtls1_hm_fragment_new(s->init_num, 0);
1137     if (frag == NULL)
1138         return 0;
1139
1140     memcpy(frag->fragment, s->init_buf->data, s->init_num);
1141
1142     if (is_ccs) {
1143         /* For DTLS1_BAD_VER the header length is non-standard */
1144         if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
1145                          ((s->version ==
1146                            DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH)
1147                          == (unsigned int)s->init_num)) {
1148             dtls1_hm_fragment_free(frag);
1149             return 0;
1150         }
1151     } else {
1152         if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
1153                          DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num)) {
1154             dtls1_hm_fragment_free(frag);
1155             return 0;
1156         }
1157     }
1158
1159     frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1160     frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1161     frag->msg_header.type = s->d1->w_msg_hdr.type;
1162     frag->msg_header.frag_off = 0;
1163     frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1164     frag->msg_header.is_ccs = is_ccs;
1165
1166     /* save current state */
1167     frag->msg_header.saved_retransmit_state.wrlmethod = s->rlayer.wrlmethod;
1168     frag->msg_header.saved_retransmit_state.wrl = s->rlayer.wrl;
1169
1170
1171     memset(seq64be, 0, sizeof(seq64be));
1172     seq64be[6] =
1173         (unsigned
1174          char)(dtls1_get_queue_priority(frag->msg_header.seq,
1175                                         frag->msg_header.is_ccs) >> 8);
1176     seq64be[7] =
1177         (unsigned
1178          char)(dtls1_get_queue_priority(frag->msg_header.seq,
1179                                         frag->msg_header.is_ccs));
1180
1181     item = pitem_new(seq64be, frag);
1182     if (item == NULL) {
1183         dtls1_hm_fragment_free(frag);
1184         return 0;
1185     }
1186
1187     pqueue_insert(s->d1->sent_messages, item);
1188     return 1;
1189 }
1190
1191 int dtls1_retransmit_message(SSL_CONNECTION *s, unsigned short seq, int *found)
1192 {
1193     int ret;
1194     /* XDTLS: for now assuming that read/writes are blocking */
1195     pitem *item;
1196     hm_fragment *frag;
1197     unsigned long header_length;
1198     unsigned char seq64be[8];
1199     struct dtls1_retransmit_state saved_state;
1200
1201     /* XDTLS:  the requested message ought to be found, otherwise error */
1202     memset(seq64be, 0, sizeof(seq64be));
1203     seq64be[6] = (unsigned char)(seq >> 8);
1204     seq64be[7] = (unsigned char)seq;
1205
1206     item = pqueue_find(s->d1->sent_messages, seq64be);
1207     if (item == NULL) {
1208         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1209         *found = 0;
1210         return 0;
1211     }
1212
1213     *found = 1;
1214     frag = (hm_fragment *)item->data;
1215
1216     if (frag->msg_header.is_ccs)
1217         header_length = DTLS1_CCS_HEADER_LENGTH;
1218     else
1219         header_length = DTLS1_HM_HEADER_LENGTH;
1220
1221     memcpy(s->init_buf->data, frag->fragment,
1222            frag->msg_header.msg_len + header_length);
1223     s->init_num = frag->msg_header.msg_len + header_length;
1224
1225     dtls1_set_message_header_int(s, frag->msg_header.type,
1226                                  frag->msg_header.msg_len,
1227                                  frag->msg_header.seq, 0,
1228                                  frag->msg_header.frag_len);
1229
1230     /* save current state */
1231     saved_state.wrlmethod = s->rlayer.wrlmethod;
1232     saved_state.wrl = s->rlayer.wrl;
1233
1234     s->d1->retransmitting = 1;
1235
1236     /* restore state in which the message was originally sent */
1237     s->rlayer.wrlmethod = frag->msg_header.saved_retransmit_state.wrlmethod;
1238     s->rlayer.wrl = frag->msg_header.saved_retransmit_state.wrl;
1239
1240     /*
1241      * The old wrl may be still pointing at an old BIO. Update it to what we're
1242      * using now.
1243      */
1244     s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
1245
1246     ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
1247                          SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
1248
1249     /* restore current state */
1250     s->rlayer.wrlmethod = saved_state.wrlmethod;
1251     s->rlayer.wrl = saved_state.wrl;
1252
1253     s->d1->retransmitting = 0;
1254
1255     (void)BIO_flush(s->wbio);
1256     return ret;
1257 }
1258
1259 void dtls1_set_message_header(SSL_CONNECTION *s,
1260                               unsigned char mt, size_t len,
1261                               size_t frag_off, size_t frag_len)
1262 {
1263     if (frag_off == 0) {
1264         s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1265         s->d1->next_handshake_write_seq++;
1266     }
1267
1268     dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
1269                                  frag_off, frag_len);
1270 }
1271
1272 /* don't actually do the writing, wait till the MTU has been retrieved */
1273 static void
1274 dtls1_set_message_header_int(SSL_CONNECTION *s, unsigned char mt,
1275                              size_t len, unsigned short seq_num,
1276                              size_t frag_off, size_t frag_len)
1277 {
1278     struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1279
1280     msg_hdr->type = mt;
1281     msg_hdr->msg_len = len;
1282     msg_hdr->seq = seq_num;
1283     msg_hdr->frag_off = frag_off;
1284     msg_hdr->frag_len = frag_len;
1285 }
1286
1287 static void
1288 dtls1_fix_message_header(SSL_CONNECTION *s, size_t frag_off, size_t frag_len)
1289 {
1290     struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1291
1292     msg_hdr->frag_off = frag_off;
1293     msg_hdr->frag_len = frag_len;
1294 }
1295
1296 static unsigned char *dtls1_write_message_header(SSL_CONNECTION *s,
1297                                                  unsigned char *p)
1298 {
1299     struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1300
1301     *p++ = msg_hdr->type;
1302     l2n3(msg_hdr->msg_len, p);
1303
1304     s2n(msg_hdr->seq, p);
1305     l2n3(msg_hdr->frag_off, p);
1306     l2n3(msg_hdr->frag_len, p);
1307
1308     return p;
1309 }
1310
1311 void dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
1312 {
1313     memset(msg_hdr, 0, sizeof(*msg_hdr));
1314     msg_hdr->type = *(data++);
1315     n2l3(data, msg_hdr->msg_len);
1316
1317     n2s(data, msg_hdr->seq);
1318     n2l3(data, msg_hdr->frag_off);
1319     n2l3(data, msg_hdr->frag_len);
1320 }
1321
1322 int dtls1_set_handshake_header(SSL_CONNECTION *s, WPACKET *pkt, int htype)
1323 {
1324     unsigned char *header;
1325
1326     if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) {
1327         s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1328         dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
1329                                      s->d1->handshake_write_seq, 0, 0);
1330         if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS))
1331             return 0;
1332     } else {
1333         dtls1_set_message_header(s, htype, 0, 0, 0);
1334         /*
1335          * We allocate space at the start for the message header. This gets
1336          * filled in later
1337          */
1338         if (!WPACKET_allocate_bytes(pkt, DTLS1_HM_HEADER_LENGTH, &header)
1339                 || !WPACKET_start_sub_packet(pkt))
1340             return 0;
1341     }
1342
1343     return 1;
1344 }
1345
1346 int dtls1_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype)
1347 {
1348     size_t msglen;
1349
1350     if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
1351             || !WPACKET_get_length(pkt, &msglen)
1352             || msglen > INT_MAX)
1353         return 0;
1354
1355     if (htype != SSL3_MT_CHANGE_CIPHER_SPEC) {
1356         s->d1->w_msg_hdr.msg_len = msglen - DTLS1_HM_HEADER_LENGTH;
1357         s->d1->w_msg_hdr.frag_len = msglen - DTLS1_HM_HEADER_LENGTH;
1358     }
1359     s->init_num = (int)msglen;
1360     s->init_off = 0;
1361
1362     if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST) {
1363         /* Buffer the message to handle re-xmits */
1364         if (!dtls1_buffer_message(s, htype == SSL3_MT_CHANGE_CIPHER_SPEC
1365                                      ? 1 : 0))
1366             return 0;
1367     }
1368
1369     return 1;
1370 }