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