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