Coverage Report

Created: 2026-07-23 06:28

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