Coverage Report

Created: 2025-06-13 06:57

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