Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
175M
{
26
175M
    return fake_now;
27
175M
}
28
29
int FuzzerInitialize(int *argc, char ***argv)
30
64
{
31
64
    STACK_OF(SSL_COMP) *comp_methods;
32
33
64
    FuzzerSetRand();
34
64
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
35
64
    OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
36
64
    ERR_clear_error();
37
64
    CRYPTO_free_ex_index(0, -1);
38
64
    idx = SSL_get_ex_data_X509_STORE_CTX_idx();
39
64
    comp_methods = SSL_COMP_get_compression_methods();
40
64
    if (comp_methods != NULL)
41
64
        sk_SSL_COMP_sort(comp_methods);
42
43
64
    return 1;
44
64
}
45
46
29.8M
#define HANDSHAKING 0
47
17.2M
#define READING 1
48
20.8k
#define WRITING 2
49
2.89M
#define ACCEPTING_STREAM 3
50
1.66M
#define CREATING_STREAM 4
51
3.80k
#define SWAPPING_STREAM 5
52
53
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
54
33.2k
{
55
33.2k
    SSL *client = NULL, *stream = NULL;
56
33.2k
    SSL *allstreams[] = { NULL, NULL, NULL, NULL };
57
33.2k
    size_t i, thisstream = 0, numstreams = 1;
58
33.2k
    BIO *in;
59
33.2k
    BIO *out;
60
33.2k
    SSL_CTX *ctx;
61
33.2k
    BIO_ADDR *peer_addr = NULL;
62
33.2k
    struct in_addr ina = { 0 };
63
33.2k
    struct timeval tv;
64
33.2k
    int state = HANDSHAKING;
65
33.2k
    uint8_t tmp[1024];
66
33.2k
    int writelen = 0;
67
68
33.2k
    if (len == 0)
69
0
        return 0;
70
71
    /* This only fuzzes the initial flow from the client so far. */
72
33.2k
    ctx = SSL_CTX_new(OSSL_QUIC_client_method());
73
33.2k
    if (ctx == NULL)
74
0
        goto end;
75
76
33.2k
    client = SSL_new(ctx);
77
33.2k
    if (client == NULL)
78
0
        goto end;
79
80
33.2k
    fake_now = ossl_ms2time(1);
81
33.2k
    if (!ossl_quic_set_override_now_cb(client, fake_now_cb, NULL))
82
0
        goto end;
83
84
33.2k
    peer_addr = BIO_ADDR_new();
85
33.2k
    if (peer_addr == NULL)
86
0
        goto end;
87
88
33.2k
    ina.s_addr = htonl(0x7f000001UL);
89
90
33.2k
    if (!BIO_ADDR_rawmake(peer_addr, AF_INET, &ina, sizeof(ina), htons(4433)))
91
0
        goto end;
92
93
33.2k
    SSL_set_tlsext_host_name(client, "localhost");
94
33.2k
    in = BIO_new(BIO_s_dgram_mem());
95
33.2k
    if (in == NULL)
96
0
        goto end;
97
33.2k
    out = BIO_new(BIO_s_dgram_mem());
98
33.2k
    if (out == NULL) {
99
0
        BIO_free(in);
100
0
        goto end;
101
0
    }
102
33.2k
    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
33.2k
    SSL_set_bio(client, in, out);
108
33.2k
    if (SSL_set_alpn_protos(client, (const unsigned char *)"\x08ossltest", 9) != 0)
109
0
        goto end;
110
33.2k
    if (SSL_set1_initial_peer_addr(client, peer_addr) != 1)
111
0
        goto end;
112
33.2k
    SSL_set_connect_state(client);
113
114
33.2k
    if (!SSL_set_incoming_stream_policy(client,
115
33.2k
            SSL_INCOMING_STREAM_POLICY_ACCEPT,
116
33.2k
            0))
117
0
        goto end;
118
119
33.2k
    allstreams[0] = stream = client;
120
34.3M
    for (;;) {
121
34.3M
        size_t size;
122
34.3M
        uint64_t nxtpktms = 0;
123
34.3M
        OSSL_TIME nxtpkt = ossl_time_zero(), nxttimeout;
124
34.3M
        int isinf, ret = 0;
125
126
34.3M
        if (len >= 2) {
127
34.3M
            if (len >= 5 && buf[0] == 0xff && buf[1] == 0xff) {
128
2.30M
                switch (buf[2]) {
129
1.45M
                case 0x00:
130
1.45M
                    if (state == READING)
131
1.44M
                        state = ACCEPTING_STREAM;
132
1.45M
                    break;
133
836k
                case 0x01:
134
836k
                    if (state == READING)
135
830k
                        state = CREATING_STREAM;
136
836k
                    break;
137
2.46k
                case 0x02:
138
2.46k
                    if (state == READING)
139
1.90k
                        state = SWAPPING_STREAM;
140
2.46k
                    break;
141
6.15k
                default:
142
                    /*ignore*/
143
6.15k
                    break;
144
2.30M
                }
145
2.30M
                len -= 3;
146
2.30M
                buf += 3;
147
2.30M
            }
148
34.3M
            nxtpktms = buf[0] + (buf[1] << 8);
149
34.3M
            nxtpkt = ossl_time_add(fake_now, ossl_ms2time(nxtpktms));
150
34.3M
            len -= 2;
151
34.3M
            buf += 2;
152
34.3M
        }
153
154
44.8M
        for (;;) {
155
44.8M
            switch (state) {
156
29.8M
            case HANDSHAKING:
157
29.8M
                ret = SSL_do_handshake(stream);
158
29.8M
                if (ret == 1)
159
7.69k
                    state = READING;
160
29.8M
                break;
161
162
12.6M
            case READING:
163
12.6M
                ret = SSL_read(stream, tmp, sizeof(tmp));
164
12.6M
                if (ret > 0) {
165
2.76k
                    state = WRITING;
166
2.76k
                    writelen = ret;
167
2.76k
                    assert(writelen <= (int)sizeof(tmp));
168
2.76k
                }
169
12.6M
                break;
170
171
12.6M
            case WRITING:
172
18.1k
                ret = SSL_write(stream, tmp, writelen);
173
18.1k
                if (ret > 0)
174
2.24k
                    state = READING;
175
18.1k
                break;
176
177
1.44M
            case ACCEPTING_STREAM:
178
1.44M
                state = READING;
179
1.44M
                ret = 1;
180
1.44M
                if (numstreams == OSSL_NELEM(allstreams)
181
7.91k
                    || SSL_get_accept_stream_queue_len(client) == 0)
182
1.44M
                    break;
183
271
                thisstream = numstreams;
184
271
                stream = allstreams[numstreams++]
185
271
                    = SSL_accept_stream(client, 0);
186
271
                if (stream == NULL)
187
0
                    goto end;
188
271
                break;
189
190
830k
            case CREATING_STREAM:
191
830k
                state = READING;
192
830k
                ret = 1;
193
830k
                if (numstreams == OSSL_NELEM(allstreams))
194
823k
                    break;
195
7.49k
                stream = SSL_new_stream(client, 0);
196
7.49k
                if (stream == NULL) {
197
                    /* Ignore, and go back to the previous stream */
198
5.17k
                    stream = allstreams[thisstream];
199
5.17k
                    break;
200
5.17k
                }
201
2.31k
                thisstream = numstreams;
202
2.31k
                allstreams[numstreams++] = stream;
203
2.31k
                break;
204
205
1.90k
            case SWAPPING_STREAM:
206
1.90k
                state = READING;
207
1.90k
                ret = 1;
208
1.90k
                if (numstreams == 1)
209
641
                    break;
210
1.26k
                if (++thisstream == numstreams)
211
623
                    thisstream = 0;
212
1.26k
                stream = allstreams[thisstream];
213
1.26k
                break;
214
44.8M
            }
215
44.8M
            assert(stream != NULL);
216
44.8M
            assert(thisstream < numstreams);
217
44.8M
            if (ret <= 0) {
218
42.5M
                switch (SSL_get_error(stream, ret)) {
219
42.4M
                case SSL_ERROR_WANT_READ:
220
42.4M
                case SSL_ERROR_WANT_WRITE:
221
42.4M
                    break;
222
26.9k
                default:
223
26.9k
                    goto end;
224
42.5M
                }
225
42.5M
            }
226
227
44.7M
            if (!SSL_get_event_timeout(client, &tv, &isinf))
228
0
                goto end;
229
230
44.7M
            if (isinf) {
231
137k
                fake_now = nxtpkt;
232
137k
                break;
233
44.6M
            } else {
234
44.6M
                nxttimeout = ossl_time_add(fake_now,
235
44.6M
                    ossl_time_from_timeval(tv));
236
44.6M
                if (len > 3 && ossl_time_compare(nxttimeout, nxtpkt) >= 0) {
237
34.1M
                    fake_now = nxtpkt;
238
34.1M
                    break;
239
34.1M
                }
240
10.4M
                fake_now = nxttimeout;
241
10.4M
            }
242
44.7M
        }
243
244
34.3M
        if (len <= 3)
245
884
            break;
246
247
34.3M
        size = buf[0] + (buf[1] << 8);
248
34.3M
        if (size > len - 2)
249
5.37k
            break;
250
251
34.3M
        if (size > 0)
252
6.34M
            BIO_write(in, buf + 2, (int)size);
253
34.3M
        len -= size + 2;
254
34.3M
        buf += size + 2;
255
34.3M
    }
256
33.2k
end:
257
69.0k
    for (i = 0; i < numstreams; i++)
258
35.8k
        SSL_free(allstreams[i]);
259
33.2k
    ERR_clear_error();
260
33.2k
    SSL_CTX_free(ctx);
261
33.2k
    BIO_ADDR_free(peer_addr);
262
263
33.2k
    return 0;
264
33.2k
}
265
266
void FuzzerCleanup(void)
267
0
{
268
0
    FuzzerClearRand();
269
0
}