Coverage Report

Created: 2026-04-01 06:39

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
22.4k
    {                                                                        \
25
22.4k
        if ((end) - (start) <= 8) {                                          \
26
17.5k
            long ii;                                                         \
27
63.7k
            for (ii = (start); ii < (end); ii++)                             \
28
46.1k
                bitmask[((ii) >> 3)] |= (1 << ((ii) & 7));                   \
29
17.5k
        } else {                                                             \
30
4.90k
            long ii;                                                         \
31
4.90k
            bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)];  \
32
257k
            for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) \
33
252k
                bitmask[ii] = 0xff;                                          \
34
4.90k
            bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)];  \
35
4.90k
        }                                                                    \
36
22.4k
    }
37
38
#define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete)                   \
39
22.4k
    {                                                                               \
40
22.4k
        long ii;                                                                    \
41
22.4k
        is_complete = 1;                                                            \
42
22.4k
        if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) \
43
22.4k
            is_complete = 0;                                                        \
44
22.4k
        if (is_complete)                                                            \
45
231k
            for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0; ii--)                    \
46
229k
                if (bitmask[ii] != 0xff) {                                          \
47
2.10k
                    is_complete = 0;                                                \
48
2.10k
                    break;                                                          \
49
2.10k
                }                                                                   \
50
22.4k
    }
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
100k
{
67
100k
    hm_fragment *frag = NULL;
68
100k
    unsigned char *buf = NULL;
69
100k
    unsigned char *bitmask = NULL;
70
71
100k
    if ((frag = OPENSSL_zalloc(sizeof(*frag))) == NULL) {
72
0
        ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
73
0
        return NULL;
74
0
    }
75
76
100k
    if (frag_len) {
77
98.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
98.8k
    }
83
84
    /* zero length fragment gets zero frag->fragment */
85
100k
    frag->fragment = buf;
86
87
    /* Initialize reassembly bitmask if necessary */
88
100k
    if (reassembly) {
89
6.84k
        bitmask = OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len));
90
6.84k
        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
6.84k
    }
97
98
100k
    frag->reassembly = bitmask;
99
100
100k
    return frag;
101
100k
}
102
103
void dtls1_hm_fragment_free(hm_fragment *frag)
104
102k
{
105
102k
    if (!frag)
106
1.59k
        return;
107
108
100k
    OPENSSL_free(frag->fragment);
109
100k
    OPENSSL_free(frag->reassembly);
110
100k
    OPENSSL_free(frag);
111
100k
}
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
106k
{
332
106k
    struct hm_header_st *msg_hdr;
333
106k
    unsigned char *p;
334
106k
    size_t msg_len;
335
106k
    size_t tmplen;
336
106k
    int errtype;
337
338
106k
    msg_hdr = &s->d1->r_msg_hdr;
339
106k
    memset(msg_hdr, 0, sizeof(*msg_hdr));
340
341
172k
again:
342
172k
    if (!dtls_get_reassembled_message(s, &errtype, &tmplen)) {
343
94.4k
        if (errtype == DTLS1_HM_BAD_FRAGMENT
344
94.4k
            || errtype == DTLS1_HM_FRAGMENT_RETRY) {
345
            /* bad fragment received */
346
66.5k
            goto again;
347
66.5k
        }
348
27.9k
        return 0;
349
94.4k
    }
350
351
78.2k
    *mt = s->s3.tmp.message_type;
352
353
78.2k
    p = (unsigned char *)s->init_buf->data;
354
355
78.2k
    if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
356
9.37k
        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
9.37k
        return 1;
364
9.37k
    }
365
366
68.9k
    msg_len = msg_hdr->msg_len;
367
368
    /* reconstruct message header */
369
68.9k
    *(p++) = msg_hdr->type;
370
68.9k
    l2n3(msg_len, p);
371
68.9k
    s2n(msg_hdr->seq, p);
372
68.9k
    l2n3(0, p);
373
68.9k
    l2n3(msg_len, p);
374
375
68.9k
    memset(msg_hdr, 0, sizeof(*msg_hdr));
376
377
68.9k
    s->d1->handshake_read_seq++;
378
379
68.9k
    s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
380
381
68.9k
    return 1;
382
78.2k
}
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
77.5k
{
391
77.5k
    unsigned char *msg = (unsigned char *)s->init_buf->data;
392
77.5k
    size_t msg_len = s->init_num + DTLS1_HM_HEADER_LENGTH;
393
394
77.5k
    if (s->s3.tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
395
        /* Nothing to be done */
396
9.32k
        goto end;
397
9.32k
    }
398
    /*
399
     * If receiving Finished, record MAC of prior handshake messages for
400
     * Finished verification.
401
     */
402
68.2k
    if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
403
        /* SSLfatal() already called */
404
0
        return 0;
405
0
    }
406
407
68.2k
    if (s->version == DTLS1_BAD_VER) {
408
1.68k
        msg += DTLS1_HM_HEADER_LENGTH;
409
1.68k
        msg_len -= DTLS1_HM_HEADER_LENGTH;
410
1.68k
    }
411
412
68.2k
    if (!ssl3_finish_mac(s, msg, msg_len))
413
7
        return 0;
414
415
68.2k
    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
77.5k
end:
421
77.5k
    *len = s->init_num;
422
77.5k
    return 1;
423
68.2k
}
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
107k
{
432
107k
    size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
433
107k
    if (max_len < s->max_cert_list)
434
107k
        return s->max_cert_list;
435
0
    return max_len;
436
107k
}
437
438
static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr)
439
69.5k
{
440
69.5k
    size_t frag_off, frag_len, msg_len;
441
442
69.5k
    msg_len = msg_hdr->msg_len;
443
69.5k
    frag_off = msg_hdr->frag_off;
444
69.5k
    frag_len = msg_hdr->frag_len;
445
446
    /* sanity checking */
447
69.5k
    if ((frag_off + frag_len) > msg_len
448
69.2k
        || msg_len > dtls1_max_handshake_message_len(s)) {
449
667
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
450
667
        return 0;
451
667
    }
452
453
68.9k
    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
68.9k
        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
68.9k
        s->s3.tmp.message_size = msg_len;
464
68.9k
        s->d1->r_msg_hdr.msg_len = msg_len;
465
68.9k
        s->s3.tmp.message_type = msg_hdr->type;
466
68.9k
        s->d1->r_msg_hdr.type = msg_hdr->type;
467
68.9k
        s->d1->r_msg_hdr.seq = msg_hdr->seq;
468
68.9k
    } 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
68.9k
    return 1;
478
68.9k
}
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
184k
{
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
184k
    pitem *item;
493
184k
    piterator iter;
494
184k
    hm_fragment *frag;
495
184k
    int ret;
496
184k
    int chretran = 0;
497
498
184k
    iter = pqueue_iterator(s->d1->buffered_messages);
499
186k
    do {
500
186k
        item = pqueue_next(&iter);
501
186k
        if (item == NULL)
502
117k
            return 0;
503
504
69.1k
        frag = (hm_fragment *)item->data;
505
506
69.1k
        if (frag->msg_header.seq < s->d1->handshake_read_seq) {
507
1.80k
            pitem *next;
508
1.80k
            hm_fragment *nextfrag;
509
510
1.80k
            if (!s->server
511
1.70k
                || frag->msg_header.seq != 0
512
805
                || s->d1->handshake_read_seq != 1
513
1.80k
                || 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
1.80k
                pqueue_pop(s->d1->buffered_messages);
520
1.80k
                dtls1_hm_fragment_free(frag);
521
1.80k
                pitem_free(item);
522
1.80k
                item = NULL;
523
1.80k
                frag = NULL;
524
1.80k
            } 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
1.80k
        }
552
69.1k
    } while (item == NULL);
553
554
    /* Don't return if reassembly still in progress */
555
67.3k
    if (frag->reassembly != NULL)
556
32.9k
        return 0;
557
558
34.4k
    if (s->d1->handshake_read_seq == frag->msg_header.seq || chretran) {
559
11.7k
        size_t frag_len = frag->msg_header.frag_len;
560
11.7k
        pqueue_pop(s->d1->buffered_messages);
561
562
        /* Calls SSLfatal() as required */
563
11.7k
        ret = dtls1_preprocess_fragment(s, &frag->msg_header);
564
565
11.7k
        if (ret && frag->msg_header.frag_len > 0) {
566
11.1k
            unsigned char *p = (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
567
11.1k
            memcpy(&p[frag->msg_header.frag_off], frag->fragment,
568
11.1k
                frag->msg_header.frag_len);
569
11.1k
        }
570
571
11.7k
        dtls1_hm_fragment_free(frag);
572
11.7k
        pitem_free(item);
573
574
11.7k
        if (ret) {
575
11.7k
            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
11.7k
            *len = frag_len;
586
11.7k
            return 1;
587
11.7k
        }
588
589
        /* Fatal error */
590
0
        s->init_num = 0;
591
0
        return -1;
592
22.7k
    } else {
593
22.7k
        return 0;
594
22.7k
    }
595
34.4k
}
596
597
static int
598
dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr)
599
25.9k
{
600
25.9k
    hm_fragment *frag = NULL;
601
25.9k
    pitem *item = NULL;
602
25.9k
    int i = -1, is_complete;
603
25.9k
    unsigned char seq64be[8];
604
25.9k
    size_t frag_len = msg_hdr->frag_len;
605
25.9k
    size_t readbytes;
606
607
25.9k
    if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len || msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
608
857
        goto err;
609
610
25.1k
    if (frag_len == 0) {
611
1.31k
        return DTLS1_HM_FRAGMENT_RETRY;
612
1.31k
    }
613
614
    /* Try to find item in queue */
615
23.8k
    memset(seq64be, 0, sizeof(seq64be));
616
23.8k
    seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
617
23.8k
    seq64be[7] = (unsigned char)msg_hdr->seq;
618
23.8k
    item = pqueue_find(s->d1->buffered_messages, seq64be);
619
620
23.8k
    if (item == NULL) {
621
6.84k
        frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
622
6.84k
        if (frag == NULL)
623
0
            goto err;
624
6.84k
        memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
625
6.84k
        frag->msg_header.frag_len = frag->msg_header.msg_len;
626
6.84k
        frag->msg_header.frag_off = 0;
627
16.9k
    } else {
628
16.9k
        frag = (hm_fragment *)item->data;
629
16.9k
        if (frag->msg_header.msg_len != msg_hdr->msg_len) {
630
266
            item = NULL;
631
266
            frag = NULL;
632
266
            goto err;
633
266
        }
634
16.9k
    }
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
23.5k
    if (frag->reassembly == NULL) {
642
1.06k
        unsigned char devnull[256];
643
644
2.20k
        while (frag_len) {
645
1.14k
            i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
646
1.14k
                devnull,
647
1.14k
                frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0, &readbytes);
648
1.14k
            if (i <= 0)
649
0
                goto err;
650
1.14k
            frag_len -= readbytes;
651
1.14k
        }
652
1.06k
        return DTLS1_HM_FRAGMENT_RETRY;
653
1.06k
    }
654
655
    /* read the body of the fragment (header has already been read */
656
22.4k
    i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
657
22.4k
        frag->fragment + msg_hdr->frag_off,
658
22.4k
        frag_len, 0, &readbytes);
659
22.4k
    if (i <= 0 || readbytes != frag_len)
660
0
        i = -1;
661
22.4k
    if (i <= 0)
662
0
        goto err;
663
664
22.4k
    RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
665
22.4k
        (long)(msg_hdr->frag_off + frag_len));
666
667
22.4k
    if (!ossl_assert(msg_hdr->msg_len > 0))
668
0
        goto err;
669
22.4k
    RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
670
22.4k
        is_complete);
671
672
22.4k
    if (is_complete) {
673
1.31k
        OPENSSL_free(frag->reassembly);
674
1.31k
        frag->reassembly = NULL;
675
1.31k
    }
676
677
22.4k
    if (item == NULL) {
678
6.84k
        item = pitem_new(seq64be, frag);
679
6.84k
        if (item == NULL) {
680
0
            i = -1;
681
0
            goto err;
682
0
        }
683
684
6.84k
        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
6.84k
        if (!ossl_assert(item != NULL))
692
0
            goto err;
693
6.84k
    }
694
695
22.4k
    return DTLS1_HM_FRAGMENT_RETRY;
696
697
1.12k
err:
698
1.12k
    if (item == NULL)
699
1.12k
        dtls1_hm_fragment_free(frag);
700
1.12k
    return -1;
701
22.4k
}
702
703
static int
704
dtls1_process_out_of_seq_message(SSL *s, const struct hm_header_st *msg_hdr)
705
60.5k
{
706
60.5k
    int i = -1;
707
60.5k
    hm_fragment *frag = NULL;
708
60.5k
    pitem *item = NULL;
709
60.5k
    unsigned char seq64be[8];
710
60.5k
    size_t frag_len = msg_hdr->frag_len;
711
60.5k
    size_t readbytes;
712
713
60.5k
    if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
714
468
        goto err;
715
716
    /* Try to find item in queue, to prevent duplicate entries */
717
60.0k
    memset(seq64be, 0, sizeof(seq64be));
718
60.0k
    seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
719
60.0k
    seq64be[7] = (unsigned char)msg_hdr->seq;
720
60.0k
    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
60.0k
    if (item != NULL && frag_len != msg_hdr->msg_len)
727
13.3k
        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
60.0k
    if (msg_hdr->seq <= s->d1->handshake_read_seq || msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL || (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
735
29.0k
        unsigned char devnull[256];
736
737
40.7k
        while (frag_len) {
738
11.6k
            i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
739
11.6k
                devnull,
740
11.6k
                frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0, &readbytes);
741
11.6k
            if (i <= 0)
742
0
                goto err;
743
11.6k
            frag_len -= readbytes;
744
11.6k
        }
745
30.9k
    } else {
746
30.9k
        if (frag_len != msg_hdr->msg_len) {
747
18.4k
            return dtls1_reassemble_fragment(s, msg_hdr);
748
18.4k
        }
749
750
12.5k
        if (frag_len > dtls1_max_handshake_message_len(s))
751
0
            goto err;
752
753
12.5k
        frag = dtls1_hm_fragment_new(frag_len, 0);
754
12.5k
        if (frag == NULL)
755
0
            goto err;
756
757
12.5k
        memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
758
759
12.5k
        if (frag_len) {
760
            /*
761
             * read the body of the fragment (header has already been read
762
             */
763
10.5k
            i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
764
10.5k
                frag->fragment, frag_len, 0,
765
10.5k
                &readbytes);
766
10.5k
            if (i <= 0 || readbytes != frag_len)
767
0
                i = -1;
768
10.5k
            if (i <= 0)
769
0
                goto err;
770
10.5k
        }
771
772
12.5k
        item = pitem_new(seq64be, frag);
773
12.5k
        if (item == NULL)
774
0
            goto err;
775
776
12.5k
        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
12.5k
        if (!ossl_assert(item != NULL))
786
0
            goto err;
787
12.5k
    }
788
789
41.6k
    return DTLS1_HM_FRAGMENT_RETRY;
790
791
468
err:
792
468
    if (item == NULL)
793
468
        dtls1_hm_fragment_free(frag);
794
468
    return 0;
795
60.0k
}
796
797
static int dtls_get_reassembled_message(SSL *s, int *errtype, size_t *len)
798
141k
{
799
141k
    unsigned char wire[DTLS1_HM_HEADER_LENGTH];
800
141k
    size_t mlen, frag_off, frag_len;
801
141k
    int i, ret, recvd_type;
802
141k
    struct hm_header_st msg_hdr;
803
141k
    size_t readbytes;
804
141k
    int chretran = 0;
805
806
141k
    *errtype = 0;
807
808
151k
redo:
809
    /* see if we have the required fragment already */
810
151k
    ret = dtls1_retrieve_buffered_fragment(s, &frag_len);
811
151k
    if (ret < 0) {
812
        /* SSLfatal() already called */
813
0
        return 0;
814
0
    }
815
151k
    if (ret > 0) {
816
9.25k
        s->init_num = frag_len;
817
9.25k
        *len = frag_len;
818
9.25k
        return 1;
819
9.25k
    }
820
821
    /* read handshake message header */
822
141k
    i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, wire,
823
141k
        DTLS1_HM_HEADER_LENGTH, 0, &readbytes);
824
141k
    if (i <= 0) { /* nbio, or an error */
825
17.4k
        s->rwstate = SSL_READING;
826
17.4k
        *len = 0;
827
17.4k
        return 0;
828
17.4k
    }
829
124k
    if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
830
7.92k
        if (wire[0] != SSL3_MT_CCS) {
831
45
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
832
45
                SSL_R_BAD_CHANGE_CIPHER_SPEC);
833
45
            goto f_err;
834
45
        }
835
836
7.87k
        memcpy(s->init_buf->data, wire, readbytes);
837
7.87k
        s->init_num = readbytes - 1;
838
7.87k
        s->init_msg = s->init_buf->data + 1;
839
7.87k
        s->s3.tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
840
7.87k
        s->s3.tmp.message_size = readbytes - 1;
841
7.87k
        *len = readbytes - 1;
842
7.87k
        return 1;
843
7.92k
    }
844
845
    /* Handshake fails if message header is incomplete */
846
116k
    if (readbytes != DTLS1_HM_HEADER_LENGTH) {
847
1.97k
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
848
1.97k
        goto f_err;
849
1.97k
    }
850
851
    /* parse the message fragment header */
852
114k
    dtls1_get_message_header(wire, &msg_hdr);
853
854
114k
    mlen = msg_hdr.msg_len;
855
114k
    frag_off = msg_hdr.frag_off;
856
114k
    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
114k
    if (frag_len > RECORD_LAYER_get_rrec_length(&s->rlayer)) {
863
1.61k
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);
864
1.61k
        goto f_err;
865
1.61k
    }
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
112k
    if (msg_hdr.seq != s->d1->handshake_read_seq) {
874
47.6k
        if (!s->server
875
24.4k
            || msg_hdr.seq != 0
876
10.5k
            || s->d1->handshake_read_seq != 1
877
6.61k
            || wire[0] != SSL3_MT_CLIENT_HELLO
878
47.6k
            || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) {
879
47.6k
            *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr);
880
47.6k
            return 0;
881
47.6k
        }
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
65.1k
    if (frag_len && frag_len < mlen) {
891
5.97k
        *errtype = dtls1_reassemble_fragment(s, &msg_hdr);
892
5.97k
        return 0;
893
5.97k
    }
894
895
59.1k
    if (!s->server
896
40.3k
        && s->d1->r_msg_hdr.frag_off == 0
897
40.3k
        && s->statem.hand_state != TLS_ST_OK
898
40.3k
        && 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
9.95k
        if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
905
9.87k
            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
9.87k
            s->init_num = 0;
911
9.87k
            goto redo;
912
9.87k
        } else { /* Incorrectly formatted Hello request */
913
914
78
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
915
78
            goto f_err;
916
78
        }
917
9.95k
    }
918
919
49.2k
    if (!dtls1_preprocess_fragment(s, &msg_hdr)) {
920
        /* SSLfatal() already called */
921
539
        goto f_err;
922
539
    }
923
924
48.6k
    if (frag_len > 0) {
925
44.3k
        unsigned char *p = (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
926
927
44.3k
        i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
928
44.3k
            &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
44.3k
        if (i <= 0) {
935
0
            s->rwstate = SSL_READING;
936
0
            *len = 0;
937
0
            return 0;
938
0
        }
939
44.3k
    } else {
940
4.34k
        readbytes = 0;
941
4.34k
    }
942
943
    /*
944
     * XDTLS: an incorrectly formatted fragment should cause the handshake
945
     * to fail
946
     */
947
48.6k
    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
48.6k
    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
48.6k
    *len = s->init_num = frag_len;
970
48.6k
    return 1;
971
972
4.25k
f_err:
973
4.25k
    s->init_num = 0;
974
4.25k
    *len = 0;
975
4.25k
    return 0;
976
48.6k
}
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
3.76k
{
989
3.76k
    if (s->version == DTLS1_BAD_VER) {
990
4
        s->d1->next_handshake_write_seq++;
991
992
4
        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
4
    }
997
998
3.76k
    return 1;
999
3.76k
}
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
20.3k
{
1043
20.3k
    if (code > 0) {
1044
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1045
0
        return 0;
1046
0
    }
1047
1048
20.3k
    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
20.3k
        return code;
1054
20.3k
    }
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
162k
{
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
162k
    return seq * 2 - is_ccs;
1077
162k
}
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
81.4k
{
1100
81.4k
    pitem *item;
1101
81.4k
    hm_fragment *frag;
1102
81.4k
    unsigned char seq64be[8];
1103
1104
    /*
1105
     * this function is called immediately after a message has been
1106
     * serialized
1107
     */
1108
81.4k
    if (!ossl_assert(s->init_off == 0))
1109
0
        return 0;
1110
1111
81.4k
    frag = dtls1_hm_fragment_new(s->init_num, 0);
1112
81.4k
    if (frag == NULL)
1113
0
        return 0;
1114
1115
81.4k
    memcpy(frag->fragment, s->init_buf->data, s->init_num);
1116
1117
81.4k
    if (is_ccs) {
1118
        /* For DTLS1_BAD_VER the header length is non-standard */
1119
3.76k
        if (!ossl_assert(s->d1->w_msg_hdr.msg_len + ((s->version == DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH)
1120
3.76k
                == (unsigned int)s->init_num)) {
1121
0
            dtls1_hm_fragment_free(frag);
1122
0
            return 0;
1123
0
        }
1124
77.6k
    } else {
1125
77.6k
        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
77.6k
    }
1130
1131
81.4k
    frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1132
81.4k
    frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1133
81.4k
    frag->msg_header.type = s->d1->w_msg_hdr.type;
1134
81.4k
    frag->msg_header.frag_off = 0;
1135
81.4k
    frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1136
81.4k
    frag->msg_header.is_ccs = is_ccs;
1137
1138
    /* save current state */
1139
81.4k
    frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
1140
81.4k
    frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
1141
81.4k
    frag->msg_header.saved_retransmit_state.compress = s->compress;
1142
81.4k
    frag->msg_header.saved_retransmit_state.session = s->session;
1143
81.4k
    frag->msg_header.saved_retransmit_state.epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
1144
1145
81.4k
    memset(seq64be, 0, sizeof(seq64be));
1146
81.4k
    seq64be[6] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
1147
81.4k
                                     frag->msg_header.is_ccs)
1148
81.4k
        >> 8);
1149
81.4k
    seq64be[7] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq,
1150
81.4k
        frag->msg_header.is_ccs));
1151
1152
81.4k
    item = pitem_new(seq64be, frag);
1153
81.4k
    if (item == NULL) {
1154
0
        dtls1_hm_fragment_free(frag);
1155
0
        return 0;
1156
0
    }
1157
1158
81.4k
    if (pqueue_insert(s->d1->sent_messages, item) == NULL) {
1159
0
        dtls1_hm_fragment_free(frag);
1160
0
        pitem_free(item);
1161
0
        return 0;
1162
0
    }
1163
81.4k
    return 1;
1164
81.4k
}
1165
1166
int dtls1_retransmit_message(SSL *s, unsigned short seq, int *found)
1167
0
{
1168
0
    int ret;
1169
    /* XDTLS: for now assuming that read/writes are blocking */
1170
0
    pitem *item;
1171
0
    hm_fragment *frag;
1172
0
    unsigned long header_length;
1173
0
    unsigned char seq64be[8];
1174
0
    struct dtls1_retransmit_state saved_state;
1175
1176
    /* XDTLS:  the requested message ought to be found, otherwise error */
1177
0
    memset(seq64be, 0, sizeof(seq64be));
1178
0
    seq64be[6] = (unsigned char)(seq >> 8);
1179
0
    seq64be[7] = (unsigned char)seq;
1180
1181
0
    item = pqueue_find(s->d1->sent_messages, seq64be);
1182
0
    if (item == NULL) {
1183
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1184
0
        *found = 0;
1185
0
        return 0;
1186
0
    }
1187
1188
0
    *found = 1;
1189
0
    frag = (hm_fragment *)item->data;
1190
1191
0
    if (frag->msg_header.is_ccs)
1192
0
        header_length = DTLS1_CCS_HEADER_LENGTH;
1193
0
    else
1194
0
        header_length = DTLS1_HM_HEADER_LENGTH;
1195
1196
0
    memcpy(s->init_buf->data, frag->fragment,
1197
0
        frag->msg_header.msg_len + header_length);
1198
0
    s->init_num = frag->msg_header.msg_len + header_length;
1199
1200
0
    dtls1_set_message_header_int(s, frag->msg_header.type,
1201
0
        frag->msg_header.msg_len,
1202
0
        frag->msg_header.seq, 0,
1203
0
        frag->msg_header.frag_len);
1204
1205
    /* save current state */
1206
0
    saved_state.enc_write_ctx = s->enc_write_ctx;
1207
0
    saved_state.write_hash = s->write_hash;
1208
0
    saved_state.compress = s->compress;
1209
0
    saved_state.session = s->session;
1210
0
    saved_state.epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
1211
1212
0
    s->d1->retransmitting = 1;
1213
1214
    /* restore state in which the message was originally sent */
1215
0
    s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
1216
0
    s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
1217
0
    s->compress = frag->msg_header.saved_retransmit_state.compress;
1218
0
    s->session = frag->msg_header.saved_retransmit_state.session;
1219
0
    DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer,
1220
0
        frag->msg_header.saved_retransmit_state.epoch);
1221
1222
0
    ret = dtls1_do_write(s, frag->msg_header.is_ccs ? SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
1223
1224
    /* restore current state */
1225
0
    s->enc_write_ctx = saved_state.enc_write_ctx;
1226
0
    s->write_hash = saved_state.write_hash;
1227
0
    s->compress = saved_state.compress;
1228
0
    s->session = saved_state.session;
1229
0
    DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer, saved_state.epoch);
1230
1231
0
    s->d1->retransmitting = 0;
1232
1233
0
    (void)BIO_flush(s->wbio);
1234
0
    return ret;
1235
0
}
1236
1237
void dtls1_set_message_header(SSL *s,
1238
    unsigned char mt, size_t len,
1239
    size_t frag_off, size_t frag_len)
1240
78.2k
{
1241
78.2k
    if (frag_off == 0) {
1242
78.2k
        s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1243
78.2k
        s->d1->next_handshake_write_seq++;
1244
78.2k
    }
1245
1246
78.2k
    dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
1247
78.2k
        frag_off, frag_len);
1248
78.2k
}
1249
1250
/* don't actually do the writing, wait till the MTU has been retrieved */
1251
static void
1252
dtls1_set_message_header_int(SSL *s, unsigned char mt,
1253
    size_t len, unsigned short seq_num,
1254
    size_t frag_off, size_t frag_len)
1255
82.0k
{
1256
82.0k
    struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1257
1258
82.0k
    msg_hdr->type = mt;
1259
82.0k
    msg_hdr->msg_len = len;
1260
82.0k
    msg_hdr->seq = seq_num;
1261
82.0k
    msg_hdr->frag_off = frag_off;
1262
82.0k
    msg_hdr->frag_len = frag_len;
1263
82.0k
}
1264
1265
static void
1266
dtls1_fix_message_header(SSL *s, size_t frag_off, size_t frag_len)
1267
228k
{
1268
228k
    struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1269
1270
228k
    msg_hdr->frag_off = frag_off;
1271
228k
    msg_hdr->frag_len = frag_len;
1272
228k
}
1273
1274
static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p)
1275
153k
{
1276
153k
    struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1277
1278
153k
    *p++ = msg_hdr->type;
1279
153k
    l2n3(msg_hdr->msg_len, p);
1280
1281
153k
    s2n(msg_hdr->seq, p);
1282
153k
    l2n3(msg_hdr->frag_off, p);
1283
153k
    l2n3(msg_hdr->frag_len, p);
1284
1285
153k
    return p;
1286
153k
}
1287
1288
void dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
1289
140k
{
1290
140k
    memset(msg_hdr, 0, sizeof(*msg_hdr));
1291
140k
    msg_hdr->type = *(data++);
1292
140k
    n2l3(data, msg_hdr->msg_len);
1293
1294
140k
    n2s(data, msg_hdr->seq);
1295
140k
    n2l3(data, msg_hdr->frag_off);
1296
140k
    n2l3(data, msg_hdr->frag_len);
1297
140k
}
1298
1299
int dtls1_set_handshake_header(SSL *s, WPACKET *pkt, int htype)
1300
82.0k
{
1301
82.0k
    unsigned char *header;
1302
1303
82.0k
    if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) {
1304
3.76k
        s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1305
3.76k
        dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
1306
3.76k
            s->d1->handshake_write_seq, 0, 0);
1307
3.76k
        if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS))
1308
0
            return 0;
1309
78.2k
    } else {
1310
78.2k
        dtls1_set_message_header(s, htype, 0, 0, 0);
1311
        /*
1312
         * We allocate space at the start for the message header. This gets
1313
         * filled in later
1314
         */
1315
78.2k
        if (!WPACKET_allocate_bytes(pkt, DTLS1_HM_HEADER_LENGTH, &header)
1316
78.2k
            || !WPACKET_start_sub_packet(pkt))
1317
0
            return 0;
1318
78.2k
    }
1319
1320
82.0k
    return 1;
1321
82.0k
}
1322
1323
int dtls1_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
1324
81.4k
{
1325
81.4k
    size_t msglen;
1326
1327
81.4k
    if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
1328
81.4k
        || !WPACKET_get_length(pkt, &msglen)
1329
81.4k
        || msglen > INT_MAX)
1330
0
        return 0;
1331
1332
81.4k
    if (htype != SSL3_MT_CHANGE_CIPHER_SPEC) {
1333
77.6k
        s->d1->w_msg_hdr.msg_len = msglen - DTLS1_HM_HEADER_LENGTH;
1334
77.6k
        s->d1->w_msg_hdr.frag_len = msglen - DTLS1_HM_HEADER_LENGTH;
1335
77.6k
    }
1336
81.4k
    s->init_num = (int)msglen;
1337
81.4k
    s->init_off = 0;
1338
1339
81.4k
    if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST) {
1340
        /* Buffer the message to handle re-xmits */
1341
81.4k
        if (!dtls1_buffer_message(s, htype == SSL3_MT_CHANGE_CIPHER_SPEC ? 1 : 0))
1342
0
            return 0;
1343
81.4k
    }
1344
1345
81.4k
    return 1;
1346
81.4k
}