Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/fuzz/quic-client.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 * https://www.openssl.org/source/license.html
8
 * or in the file LICENSE in the source distribution.
9
 */
10
11
#include <openssl/ssl.h>
12
#include <openssl/err.h>
13
#include <openssl/bio.h>
14
#include "fuzzer.h"
15
#include "internal/sockets.h"
16
#include "internal/time.h"
17
#include "internal/quic_ssl.h"
18
19
/* unused, to avoid warning. */
20
static int idx;
21
22
static OSSL_TIME fake_now;
23
24
static OSSL_TIME fake_now_cb(void *arg)
25
234M
{
26
234M
    return fake_now;
27
234M
}
28
29
int FuzzerInitialize(int *argc, char ***argv)
30
60
{
31
60
    STACK_OF(SSL_COMP) *comp_methods;
32
33
60
    FuzzerSetRand();
34
60
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
35
60
    OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
36
60
    ERR_clear_error();
37
60
    CRYPTO_free_ex_index(0, -1);
38
60
    idx = SSL_get_ex_data_X509_STORE_CTX_idx();
39
60
    comp_methods = SSL_COMP_get_compression_methods();
40
60
    if (comp_methods != NULL)
41
60
        sk_SSL_COMP_sort(comp_methods);
42
43
60
    return 1;
44
60
}
45
46
40.0M
#define HANDSHAKING 0
47
35.9M
#define READING 1
48
134k
#define WRITING 2
49
9.35M
#define ACCEPTING_STREAM 3
50
5.20M
#define CREATING_STREAM 4
51
7.26k
#define SWAPPING_STREAM 5
52
53
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
54
51.3k
{
55
51.3k
    SSL *client = NULL, *stream = NULL;
56
51.3k
    SSL *allstreams[] = { NULL, NULL, NULL, NULL };
57
51.3k
    size_t i, thisstream = 0, numstreams = 1;
58
51.3k
    BIO *in;
59
51.3k
    BIO *out;
60
51.3k
    SSL_CTX *ctx;
61
51.3k
    BIO_ADDR *peer_addr = NULL;
62
51.3k
    struct in_addr ina = { 0 };
63
51.3k
    struct timeval tv;
64
51.3k
    int state = HANDSHAKING;
65
51.3k
    uint8_t tmp[1024];
66
51.3k
    int writelen = 0;
67
68
51.3k
    if (len == 0)
69
0
        return 0;
70
71
    /* This only fuzzes the initial flow from the client so far. */
72
51.3k
    ctx = SSL_CTX_new(OSSL_QUIC_client_method());
73
51.3k
    if (ctx == NULL)
74
0
        goto end;
75
76
51.3k
    client = SSL_new(ctx);
77
51.3k
    if (client == NULL)
78
0
        goto end;
79
80
51.3k
    fake_now = ossl_ms2time(1);
81
51.3k
    if (!ossl_quic_set_override_now_cb(client, fake_now_cb, NULL))
82
0
        goto end;
83
84
51.3k
    peer_addr = BIO_ADDR_new();
85
51.3k
    if (peer_addr == NULL)
86
0
        goto end;
87
88
51.3k
    ina.s_addr = htonl(0x7f000001UL);
89
90
51.3k
    if (!BIO_ADDR_rawmake(peer_addr, AF_INET, &ina, sizeof(ina), htons(4433)))
91
0
        goto end;
92
93
51.3k
    SSL_set_tlsext_host_name(client, "localhost");
94
51.3k
    in = BIO_new(BIO_s_dgram_mem());
95
51.3k
    if (in == NULL)
96
0
        goto end;
97
51.3k
    out = BIO_new(BIO_s_dgram_mem());
98
51.3k
    if (out == NULL) {
99
0
        BIO_free(in);
100
0
        goto end;
101
0
    }
102
51.3k
    if (!BIO_dgram_set_caps(out, BIO_DGRAM_CAP_HANDLES_DST_ADDR)) {
103
0
        BIO_free(in);
104
0
        BIO_free(out);
105
0
        goto end;
106
0
    }
107
51.3k
    SSL_set_bio(client, in, out);
108
51.3k
    if (SSL_set_alpn_protos(client, (const unsigned char *)"\x08ossltest", 9) != 0)
109
0
        goto end;
110
51.3k
    if (SSL_set1_initial_peer_addr(client, peer_addr) != 1)
111
0
        goto end;
112
51.3k
    SSL_set_connect_state(client);
113
114
51.3k
    if (!SSL_set_incoming_stream_policy(client,
115
51.3k
            SSL_INCOMING_STREAM_POLICY_ACCEPT,
116
51.3k
            0))
117
0
        goto end;
118
119
51.3k
    allstreams[0] = stream = client;
120
49.6M
    for (;;) {
121
49.6M
        size_t size;
122
49.6M
        uint64_t nxtpktms = 0;
123
49.6M
        OSSL_TIME nxtpkt = ossl_time_zero(), nxttimeout;
124
49.6M
        int isinf, ret = 0;
125
126
49.6M
        if (len >= 2) {
127
49.6M
            if (len >= 5 && buf[0] == 0xff && buf[1] == 0xff) {
128
7.35M
                switch (buf[2]) {
129
4.70M
                case 0x00:
130
4.70M
                    if (state == READING)
131
4.67M
                        state = ACCEPTING_STREAM;
132
4.70M
                    break;
133
2.62M
                case 0x01:
134
2.62M
                    if (state == READING)
135
2.60M
                        state = CREATING_STREAM;
136
2.62M
                    break;
137
4.51k
                case 0x02:
138
4.51k
                    if (state == READING)
139
3.63k
                        state = SWAPPING_STREAM;
140
4.51k
                    break;
141
16.0k
                default:
142
                    /*ignore*/
143
16.0k
                    break;
144
7.35M
                }
145
7.35M
                len -= 3;
146
7.35M
                buf += 3;
147
7.35M
            }
148
49.6M
            nxtpktms = buf[0] + (buf[1] << 8);
149
49.6M
            nxtpkt = ossl_time_add(fake_now, ossl_ms2time(nxtpktms));
150
49.6M
            len -= 2;
151
49.6M
            buf += 2;
152
49.6M
        }
153
154
68.6M
        for (;;) {
155
68.6M
            switch (state) {
156
39.9M
            case HANDSHAKING:
157
39.9M
                ret = SSL_do_handshake(stream);
158
39.9M
                if (ret == 1)
159
12.6k
                    state = READING;
160
39.9M
                break;
161
162
21.2M
            case READING:
163
21.2M
                ret = SSL_read(stream, tmp, sizeof(tmp));
164
21.2M
                if (ret > 0) {
165
5.69k
                    state = WRITING;
166
5.69k
                    writelen = ret;
167
5.69k
                    assert(writelen <= (int)sizeof(tmp));
168
5.69k
                }
169
21.2M
                break;
170
171
21.2M
            case WRITING:
172
128k
                ret = SSL_write(stream, tmp, writelen);
173
128k
                if (ret > 0)
174
4.65k
                    state = READING;
175
128k
                break;
176
177
4.67M
            case ACCEPTING_STREAM:
178
4.67M
                state = READING;
179
4.67M
                ret = 1;
180
4.67M
                if (numstreams == OSSL_NELEM(allstreams)
181
11.5k
                    || SSL_get_accept_stream_queue_len(client) == 0)
182
4.67M
                    break;
183
522
                thisstream = numstreams;
184
522
                stream = allstreams[numstreams++]
185
522
                    = SSL_accept_stream(client, 0);
186
522
                if (stream == NULL)
187
0
                    goto end;
188
522
                break;
189
190
2.60M
            case CREATING_STREAM:
191
2.60M
                state = READING;
192
2.60M
                ret = 1;
193
2.60M
                if (numstreams == OSSL_NELEM(allstreams))
194
2.58M
                    break;
195
11.9k
                stream = SSL_new_stream(client, 0);
196
11.9k
                if (stream == NULL) {
197
                    /* Ignore, and go back to the previous stream */
198
6.69k
                    stream = allstreams[thisstream];
199
6.69k
                    break;
200
6.69k
                }
201
5.21k
                thisstream = numstreams;
202
5.21k
                allstreams[numstreams++] = stream;
203
5.21k
                break;
204
205
3.63k
            case SWAPPING_STREAM:
206
3.63k
                state = READING;
207
3.63k
                ret = 1;
208
3.63k
                if (numstreams == 1)
209
1.00k
                    break;
210
2.63k
                if (++thisstream == numstreams)
211
1.15k
                    thisstream = 0;
212
2.63k
                stream = allstreams[thisstream];
213
2.63k
                break;
214
68.6M
            }
215
68.6M
            assert(stream != NULL);
216
68.6M
            assert(thisstream < numstreams);
217
68.6M
            if (ret <= 0) {
218
61.3M
                switch (SSL_get_error(stream, ret)) {
219
61.1M
                case SSL_ERROR_WANT_READ:
220
61.3M
                case SSL_ERROR_WANT_WRITE:
221
61.3M
                    break;
222
40.6k
                default:
223
40.6k
                    goto end;
224
61.3M
                }
225
61.3M
            }
226
227
68.6M
            if (!SSL_get_event_timeout(client, &tv, &isinf))
228
0
                goto end;
229
230
68.6M
            if (isinf) {
231
395k
                fake_now = nxtpkt;
232
395k
                break;
233
68.2M
            } else {
234
68.2M
                nxttimeout = ossl_time_add(fake_now,
235
68.2M
                    ossl_time_from_timeval(tv));
236
68.2M
                if (len > 3 && ossl_time_compare(nxttimeout, nxtpkt) >= 0) {
237
49.2M
                    fake_now = nxtpkt;
238
49.2M
                    break;
239
49.2M
                }
240
18.9M
                fake_now = nxttimeout;
241
18.9M
            }
242
68.6M
        }
243
244
49.6M
        if (len <= 3)
245
2.11k
            break;
246
247
49.6M
        size = buf[0] + (buf[1] << 8);
248
49.6M
        if (size > len - 2)
249
8.61k
            break;
250
251
49.6M
        if (size > 0)
252
13.7M
            BIO_write(in, buf + 2, size);
253
49.6M
        len -= size + 2;
254
49.6M
        buf += size + 2;
255
49.6M
    }
256
51.3k
end:
257
108k
    for (i = 0; i < numstreams; i++)
258
57.0k
        SSL_free(allstreams[i]);
259
51.3k
    ERR_clear_error();
260
51.3k
    SSL_CTX_free(ctx);
261
51.3k
    BIO_ADDR_free(peer_addr);
262
263
51.3k
    return 0;
264
51.3k
}
265
266
void FuzzerCleanup(void)
267
0
{
268
0
    FuzzerClearRand();
269
0
}