Coverage Report

Created: 2025-12-31 06:58

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