Coverage Report

Created: 2025-08-28 07:07

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