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