Coverage Report

Created: 2025-12-04 06:33

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