Coverage Report

Created: 2026-04-01 06:39

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