Coverage Report

Created: 2026-07-12 07:21

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
271M
{
26
271M
    return fake_now;
27
271M
}
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
48.5M
#define HANDSHAKING 0
47
35.3M
#define READING 1
48
68.6k
#define WRITING 2
49
7.90M
#define ACCEPTING_STREAM 3
50
4.39M
#define CREATING_STREAM 4
51
8.26k
#define SWAPPING_STREAM 5
52
53
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
54
49.5k
{
55
49.5k
    SSL *client = NULL, *stream = NULL;
56
49.5k
    SSL *allstreams[] = { NULL, NULL, NULL, NULL };
57
49.5k
    size_t i, thisstream = 0, numstreams = 1;
58
49.5k
    BIO *in;
59
49.5k
    BIO *out;
60
49.5k
    SSL_CTX *ctx;
61
49.5k
    BIO_ADDR *peer_addr = NULL;
62
49.5k
    struct in_addr ina = { 0 };
63
49.5k
    struct timeval tv;
64
49.5k
    int state = HANDSHAKING;
65
49.5k
    uint8_t tmp[1024];
66
49.5k
    int writelen = 0;
67
68
49.5k
    if (len == 0)
69
0
        return 0;
70
71
    /* This only fuzzes the initial flow from the client so far. */
72
49.5k
    ctx = SSL_CTX_new(OSSL_QUIC_client_method());
73
49.5k
    if (ctx == NULL)
74
0
        goto end;
75
76
49.5k
    client = SSL_new(ctx);
77
49.5k
    if (client == NULL)
78
0
        goto end;
79
80
49.5k
    fake_now = ossl_ms2time(1);
81
49.5k
    if (!ossl_quic_set_override_now_cb(client, fake_now_cb, NULL))
82
0
        goto end;
83
84
49.5k
    peer_addr = BIO_ADDR_new();
85
49.5k
    if (peer_addr == NULL)
86
0
        goto end;
87
88
49.5k
    ina.s_addr = htonl(0x7f000001UL);
89
90
49.5k
    if (!BIO_ADDR_rawmake(peer_addr, AF_INET, &ina, sizeof(ina), htons(4433)))
91
0
        goto end;
92
93
49.5k
    SSL_set_tlsext_host_name(client, "localhost");
94
49.5k
    in = BIO_new(BIO_s_dgram_mem());
95
49.5k
    if (in == NULL)
96
0
        goto end;
97
49.5k
    out = BIO_new(BIO_s_dgram_mem());
98
49.5k
    if (out == NULL) {
99
0
        BIO_free(in);
100
0
        goto end;
101
0
    }
102
49.5k
    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
49.5k
    SSL_set_bio(client, in, out);
108
49.5k
    if (SSL_set_alpn_protos(client, (const unsigned char *)"\x08ossltest", 9) != 0)
109
0
        goto end;
110
49.5k
    if (SSL_set1_initial_peer_addr(client, peer_addr) != 1)
111
0
        goto end;
112
49.5k
    SSL_set_connect_state(client);
113
114
49.5k
    if (!SSL_set_incoming_stream_policy(client,
115
49.5k
            SSL_INCOMING_STREAM_POLICY_ACCEPT,
116
49.5k
            0))
117
0
        goto end;
118
119
49.5k
    allstreams[0] = stream = client;
120
53.4M
    for (;;) {
121
53.4M
        size_t size;
122
53.4M
        uint64_t nxtpktms = 0;
123
53.4M
        OSSL_TIME nxtpkt = ossl_time_zero(), nxttimeout;
124
53.4M
        int isinf, ret = 0;
125
126
53.4M
        if (len >= 2) {
127
53.4M
            if (len >= 5 && buf[0] == 0xff && buf[1] == 0xff) {
128
6.22M
                switch (buf[2]) {
129
3.98M
                case 0x00:
130
3.98M
                    if (state == READING)
131
3.95M
                        state = ACCEPTING_STREAM;
132
3.98M
                    break;
133
2.21M
                case 0x01:
134
2.21M
                    if (state == READING)
135
2.19M
                        state = CREATING_STREAM;
136
2.21M
                    break;
137
5.01k
                case 0x02:
138
5.01k
                    if (state == READING)
139
4.13k
                        state = SWAPPING_STREAM;
140
5.01k
                    break;
141
18.7k
                default:
142
                    /*ignore*/
143
18.7k
                    break;
144
6.22M
                }
145
6.22M
                len -= 3;
146
6.22M
                buf += 3;
147
6.22M
            }
148
53.4M
            nxtpktms = buf[0] + (buf[1] << 8);
149
53.4M
            nxtpkt = ossl_time_add(fake_now, ossl_ms2time(nxtpktms));
150
53.4M
            len -= 2;
151
53.4M
            buf += 2;
152
53.4M
        }
153
154
77.6M
        for (;;) {
155
77.6M
            switch (state) {
156
48.4M
            case HANDSHAKING:
157
48.4M
                ret = SSL_do_handshake(stream);
158
48.4M
                if (ret == 1)
159
12.4k
                    state = READING;
160
48.4M
                break;
161
162
22.9M
            case READING:
163
22.9M
                ret = SSL_read(stream, tmp, sizeof(tmp));
164
22.9M
                if (ret > 0) {
165
5.05k
                    state = WRITING;
166
5.05k
                    writelen = ret;
167
5.05k
                    assert(writelen <= (int)sizeof(tmp));
168
5.05k
                }
169
22.9M
                break;
170
171
22.9M
            case WRITING:
172
63.6k
                ret = SSL_write(stream, tmp, writelen);
173
63.6k
                if (ret > 0)
174
4.16k
                    state = READING;
175
63.6k
                break;
176
177
3.95M
            case ACCEPTING_STREAM:
178
3.95M
                state = READING;
179
3.95M
                ret = 1;
180
3.95M
                if (numstreams == OSSL_NELEM(allstreams)
181
11.3k
                    || SSL_get_accept_stream_queue_len(client) == 0)
182
3.95M
                    break;
183
461
                thisstream = numstreams;
184
461
                stream = allstreams[numstreams++]
185
461
                    = SSL_accept_stream(client, 0);
186
461
                if (stream == NULL)
187
0
                    goto end;
188
461
                break;
189
190
2.19M
            case CREATING_STREAM:
191
2.19M
                state = READING;
192
2.19M
                ret = 1;
193
2.19M
                if (numstreams == OSSL_NELEM(allstreams))
194
2.18M
                    break;
195
12.0k
                stream = SSL_new_stream(client, 0);
196
12.0k
                if (stream == NULL) {
197
                    /* Ignore, and go back to the previous stream */
198
6.97k
                    stream = allstreams[thisstream];
199
6.97k
                    break;
200
6.97k
                }
201
5.03k
                thisstream = numstreams;
202
5.03k
                allstreams[numstreams++] = stream;
203
5.03k
                break;
204
205
4.13k
            case SWAPPING_STREAM:
206
4.13k
                state = READING;
207
4.13k
                ret = 1;
208
4.13k
                if (numstreams == 1)
209
986
                    break;
210
3.14k
                if (++thisstream == numstreams)
211
1.39k
                    thisstream = 0;
212
3.14k
                stream = allstreams[thisstream];
213
3.14k
                break;
214
77.6M
            }
215
77.6M
            assert(stream != NULL);
216
77.6M
            assert(thisstream < numstreams);
217
77.6M
            if (ret <= 0) {
218
71.4M
                switch (SSL_get_error(stream, ret)) {
219
71.3M
                case SSL_ERROR_WANT_READ:
220
71.4M
                case SSL_ERROR_WANT_WRITE:
221
71.4M
                    break;
222
39.8k
                default:
223
39.8k
                    goto end;
224
71.4M
                }
225
71.4M
            }
226
227
77.6M
            if (!SSL_get_event_timeout(client, &tv, &isinf))
228
0
                goto end;
229
230
77.6M
            if (isinf) {
231
302k
                fake_now = nxtpkt;
232
302k
                break;
233
77.3M
            } else {
234
77.3M
                nxttimeout = ossl_time_add(fake_now,
235
77.3M
                    ossl_time_from_timeval(tv));
236
77.3M
                if (len > 3 && ossl_time_compare(nxttimeout, nxtpkt) >= 0) {
237
53.0M
                    fake_now = nxtpkt;
238
53.0M
                    break;
239
53.0M
                }
240
24.2M
                fake_now = nxttimeout;
241
24.2M
            }
242
77.6M
        }
243
244
53.3M
        if (len <= 3)
245
2.17k
            break;
246
247
53.3M
        size = buf[0] + (buf[1] << 8);
248
53.3M
        if (size > len - 2)
249
7.48k
            break;
250
251
53.3M
        if (size > 0)
252
12.4M
            BIO_write(in, buf + 2, size);
253
53.3M
        len -= size + 2;
254
53.3M
        buf += size + 2;
255
53.3M
    }
256
49.5k
end:
257
104k
    for (i = 0; i < numstreams; i++)
258
55.0k
        SSL_free(allstreams[i]);
259
49.5k
    ERR_clear_error();
260
49.5k
    SSL_CTX_free(ctx);
261
49.5k
    BIO_ADDR_free(peer_addr);
262
263
49.5k
    return 0;
264
49.5k
}
265
266
void FuzzerCleanup(void)
267
0
{
268
0
    FuzzerClearRand();
269
0
}