Coverage Report

Created: 2026-04-01 06:39

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
276M
{
26
276M
    return fake_now;
27
276M
}
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
50.3M
#define HANDSHAKING 0
47
35.8M
#define READING 1
48
125k
#define WRITING 2
49
8.56M
#define ACCEPTING_STREAM 3
50
4.72M
#define CREATING_STREAM 4
51
7.48k
#define SWAPPING_STREAM 5
52
53
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
54
51.2k
{
55
51.2k
    SSL *client = NULL, *stream = NULL;
56
51.2k
    SSL *allstreams[] = { NULL, NULL, NULL, NULL };
57
51.2k
    size_t i, thisstream = 0, numstreams = 1;
58
51.2k
    BIO *in;
59
51.2k
    BIO *out;
60
51.2k
    SSL_CTX *ctx;
61
51.2k
    BIO_ADDR *peer_addr = NULL;
62
51.2k
    struct in_addr ina = { 0 };
63
51.2k
    struct timeval tv;
64
51.2k
    int state = HANDSHAKING;
65
51.2k
    uint8_t tmp[1024];
66
51.2k
    int writelen = 0;
67
68
51.2k
    if (len == 0)
69
0
        return 0;
70
71
    /* This only fuzzes the initial flow from the client so far. */
72
51.2k
    ctx = SSL_CTX_new(OSSL_QUIC_client_method());
73
51.2k
    if (ctx == NULL)
74
0
        goto end;
75
76
51.2k
    client = SSL_new(ctx);
77
51.2k
    if (client == NULL)
78
0
        goto end;
79
80
51.2k
    fake_now = ossl_ms2time(1);
81
51.2k
    if (!ossl_quic_set_override_now_cb(client, fake_now_cb, NULL))
82
0
        goto end;
83
84
51.2k
    peer_addr = BIO_ADDR_new();
85
51.2k
    if (peer_addr == NULL)
86
0
        goto end;
87
88
51.2k
    ina.s_addr = htonl(0x7f000001UL);
89
90
51.2k
    if (!BIO_ADDR_rawmake(peer_addr, AF_INET, &ina, sizeof(ina), htons(4433)))
91
0
        goto end;
92
93
51.2k
    SSL_set_tlsext_host_name(client, "localhost");
94
51.2k
    in = BIO_new(BIO_s_dgram_mem());
95
51.2k
    if (in == NULL)
96
0
        goto end;
97
51.2k
    out = BIO_new(BIO_s_dgram_mem());
98
51.2k
    if (out == NULL) {
99
0
        BIO_free(in);
100
0
        goto end;
101
0
    }
102
51.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
51.2k
    SSL_set_bio(client, in, out);
108
51.2k
    if (SSL_set_alpn_protos(client, (const unsigned char *)"\x08ossltest", 9) != 0)
109
0
        goto end;
110
51.2k
    if (SSL_set1_initial_peer_addr(client, peer_addr) != 1)
111
0
        goto end;
112
51.2k
    SSL_set_connect_state(client);
113
114
51.2k
    if (!SSL_set_incoming_stream_policy(client,
115
51.2k
            SSL_INCOMING_STREAM_POLICY_ACCEPT,
116
51.2k
            0))
117
0
        goto end;
118
119
51.2k
    allstreams[0] = stream = client;
120
54.9M
    for (;;) {
121
54.9M
        size_t size;
122
54.9M
        uint64_t nxtpktms = 0;
123
54.9M
        OSSL_TIME nxtpkt = ossl_time_zero(), nxttimeout;
124
54.9M
        int isinf, ret = 0;
125
126
54.9M
        if (len >= 2) {
127
54.9M
            if (len >= 5 && buf[0] == 0xff && buf[1] == 0xff) {
128
6.73M
                switch (buf[2]) {
129
4.31M
                case 0x00:
130
4.31M
                    if (state == READING)
131
4.28M
                        state = ACCEPTING_STREAM;
132
4.31M
                    break;
133
2.38M
                case 0x01:
134
2.38M
                    if (state == READING)
135
2.36M
                        state = CREATING_STREAM;
136
2.38M
                    break;
137
4.61k
                case 0x02:
138
4.61k
                    if (state == READING)
139
3.74k
                        state = SWAPPING_STREAM;
140
4.61k
                    break;
141
19.3k
                default:
142
                    /*ignore*/
143
19.3k
                    break;
144
6.73M
                }
145
6.73M
                len -= 3;
146
6.73M
                buf += 3;
147
6.73M
            }
148
54.9M
            nxtpktms = buf[0] + (buf[1] << 8);
149
54.9M
            nxtpkt = ossl_time_add(fake_now, ossl_ms2time(nxtpktms));
150
54.9M
            len -= 2;
151
54.9M
            buf += 2;
152
54.9M
        }
153
154
79.5M
        for (;;) {
155
79.5M
            switch (state) {
156
50.2M
            case HANDSHAKING:
157
50.2M
                ret = SSL_do_handshake(stream);
158
50.2M
                if (ret == 1)
159
12.7k
                    state = READING;
160
50.2M
                break;
161
162
22.4M
            case READING:
163
22.4M
                ret = SSL_read(stream, tmp, sizeof(tmp));
164
22.4M
                if (ret > 0) {
165
5.56k
                    state = WRITING;
166
5.56k
                    writelen = ret;
167
5.56k
                    assert(writelen <= (int)sizeof(tmp));
168
5.56k
                }
169
22.4M
                break;
170
171
22.4M
            case WRITING:
172
120k
                ret = SSL_write(stream, tmp, writelen);
173
120k
                if (ret > 0)
174
4.53k
                    state = READING;
175
120k
                break;
176
177
4.28M
            case ACCEPTING_STREAM:
178
4.28M
                state = READING;
179
4.28M
                ret = 1;
180
4.28M
                if (numstreams == OSSL_NELEM(allstreams)
181
11.6k
                    || SSL_get_accept_stream_queue_len(client) == 0)
182
4.28M
                    break;
183
510
                thisstream = numstreams;
184
510
                stream = allstreams[numstreams++]
185
510
                    = SSL_accept_stream(client, 0);
186
510
                if (stream == NULL)
187
0
                    goto end;
188
510
                break;
189
190
2.36M
            case CREATING_STREAM:
191
2.36M
                state = READING;
192
2.36M
                ret = 1;
193
2.36M
                if (numstreams == OSSL_NELEM(allstreams))
194
2.34M
                    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
7.03k
                    stream = allstreams[thisstream];
199
7.03k
                    break;
200
7.03k
                }
201
5.03k
                thisstream = numstreams;
202
5.03k
                allstreams[numstreams++] = stream;
203
5.03k
                break;
204
205
3.74k
            case SWAPPING_STREAM:
206
3.74k
                state = READING;
207
3.74k
                ret = 1;
208
3.74k
                if (numstreams == 1)
209
976
                    break;
210
2.76k
                if (++thisstream == numstreams)
211
1.18k
                    thisstream = 0;
212
2.76k
                stream = allstreams[thisstream];
213
2.76k
                break;
214
79.5M
            }
215
79.5M
            assert(stream != NULL);
216
79.5M
            assert(thisstream < numstreams);
217
79.5M
            if (ret <= 0) {
218
72.8M
                switch (SSL_get_error(stream, ret)) {
219
72.7M
                case SSL_ERROR_WANT_READ:
220
72.8M
                case SSL_ERROR_WANT_WRITE:
221
72.8M
                    break;
222
40.7k
                default:
223
40.7k
                    goto end;
224
72.8M
                }
225
72.8M
            }
226
227
79.4M
            if (!SSL_get_event_timeout(client, &tv, &isinf))
228
0
                goto end;
229
230
79.4M
            if (isinf) {
231
277k
                fake_now = nxtpkt;
232
277k
                break;
233
79.2M
            } else {
234
79.2M
                nxttimeout = ossl_time_add(fake_now,
235
79.2M
                    ossl_time_from_timeval(tv));
236
79.2M
                if (len > 3 && ossl_time_compare(nxttimeout, nxtpkt) >= 0) {
237
54.6M
                    fake_now = nxtpkt;
238
54.6M
                    break;
239
54.6M
                }
240
24.5M
                fake_now = nxttimeout;
241
24.5M
            }
242
79.4M
        }
243
244
54.9M
        if (len <= 3)
245
2.16k
            break;
246
247
54.9M
        size = buf[0] + (buf[1] << 8);
248
54.9M
        if (size > len - 2)
249
8.27k
            break;
250
251
54.9M
        if (size > 0)
252
13.4M
            BIO_write(in, buf + 2, size);
253
54.9M
        len -= size + 2;
254
54.9M
        buf += size + 2;
255
54.9M
    }
256
51.2k
end:
257
107k
    for (i = 0; i < numstreams; i++)
258
56.7k
        SSL_free(allstreams[i]);
259
51.2k
    ERR_clear_error();
260
51.2k
    SSL_CTX_free(ctx);
261
51.2k
    BIO_ADDR_free(peer_addr);
262
263
51.2k
    return 0;
264
51.2k
}
265
266
void FuzzerCleanup(void)
267
0
{
268
0
    FuzzerClearRand();
269
0
}