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