Coverage Report

Created: 2025-06-13 06:58

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