/src/openssl41/fuzz/quic-client.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2026 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 | | static OSSL_TIME fake_now; |
20 | | |
21 | | static OSSL_TIME fake_now_cb(void *arg) |
22 | 247M | { |
23 | 247M | return fake_now; |
24 | 247M | } |
25 | | |
26 | | int FuzzerInitialize(int *argc, char ***argv) |
27 | 16 | { |
28 | 16 | STACK_OF(SSL_COMP) *comp_methods; |
29 | | |
30 | 16 | FuzzerSetRand(); |
31 | 16 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL); |
32 | 16 | OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL); |
33 | 16 | ERR_clear_error(); |
34 | 16 | CRYPTO_free_ex_index(0, -1); |
35 | 16 | if (SSL_get_ex_data_X509_STORE_CTX_idx() == 0) { |
36 | | /* Just suppress warning */ |
37 | 0 | } |
38 | 16 | comp_methods = SSL_COMP_get_compression_methods(); |
39 | 16 | if (comp_methods != NULL) |
40 | 16 | sk_SSL_COMP_sort(comp_methods); |
41 | | |
42 | 16 | return 1; |
43 | 16 | } |
44 | | |
45 | 8.92M | #define HANDSHAKING 0 |
46 | 13.6M | #define READING 1 |
47 | 186k | #define WRITING 2 |
48 | 2.10M | #define ACCEPTING_STREAM 3 |
49 | 1.13M | #define CREATING_STREAM 4 |
50 | 5.89k | #define SWAPPING_STREAM 5 |
51 | | |
52 | | int FuzzerTestOneInput(const uint8_t *buf, size_t len) |
53 | 16.0k | { |
54 | 16.0k | SSL *client = NULL, *stream = NULL; |
55 | 16.0k | SSL *allstreams[] = { NULL, NULL, NULL, NULL }; |
56 | 16.0k | size_t i, thisstream = 0, numstreams = 1; |
57 | 16.0k | BIO *in; |
58 | 16.0k | BIO *out; |
59 | 16.0k | SSL_CTX *ctx; |
60 | 16.0k | BIO_ADDR *peer_addr = NULL; |
61 | 16.0k | struct in_addr ina = { 0 }; |
62 | 16.0k | struct timeval tv; |
63 | 16.0k | int state = HANDSHAKING; |
64 | 16.0k | uint8_t tmp[1024]; |
65 | 16.0k | int writelen = 0; |
66 | | |
67 | 16.0k | if (len == 0) |
68 | 0 | return 0; |
69 | | |
70 | | /* This only fuzzes the initial flow from the client so far. */ |
71 | 16.0k | ctx = SSL_CTX_new(OSSL_QUIC_client_method()); |
72 | 16.0k | if (ctx == NULL) |
73 | 0 | goto end; |
74 | | |
75 | 16.0k | client = SSL_new(ctx); |
76 | 16.0k | if (client == NULL) |
77 | 0 | goto end; |
78 | | |
79 | 16.0k | allstreams[0] = stream = client; |
80 | | |
81 | 16.0k | fake_now = ossl_ms2time(1); |
82 | 16.0k | if (!ossl_quic_set_override_now_cb(client, fake_now_cb, NULL)) |
83 | 0 | goto end; |
84 | | |
85 | 16.0k | peer_addr = BIO_ADDR_new(); |
86 | 16.0k | if (peer_addr == NULL) |
87 | 0 | goto end; |
88 | | |
89 | 16.0k | ina.s_addr = htonl(0x7f000001UL); |
90 | | |
91 | 16.0k | if (!BIO_ADDR_rawmake(peer_addr, AF_INET, &ina, sizeof(ina), htons(4433))) |
92 | 0 | goto end; |
93 | | |
94 | 16.0k | if (SSL_set_tlsext_host_name(client, "localhost") != 1) |
95 | 0 | goto end; |
96 | 16.0k | in = BIO_new(BIO_s_dgram_mem()); |
97 | 16.0k | if (in == NULL) |
98 | 0 | goto end; |
99 | 16.0k | out = BIO_new(BIO_s_dgram_mem()); |
100 | 16.0k | if (out == NULL) { |
101 | 0 | BIO_free(in); |
102 | 0 | goto end; |
103 | 0 | } |
104 | 16.0k | if (!BIO_dgram_set_caps(out, BIO_DGRAM_CAP_HANDLES_DST_ADDR)) { |
105 | 0 | BIO_free(in); |
106 | 0 | BIO_free(out); |
107 | 0 | goto end; |
108 | 0 | } |
109 | 16.0k | SSL_set_bio(client, in, out); |
110 | 16.0k | if (SSL_set_alpn_protos(client, (const unsigned char *)"\x08ossltest", 9) != 0) |
111 | 0 | goto end; |
112 | 16.0k | if (SSL_set1_initial_peer_addr(client, peer_addr) != 1) |
113 | 0 | goto end; |
114 | 16.0k | SSL_set_connect_state(client); |
115 | | |
116 | 16.0k | if (!SSL_set_incoming_stream_policy(client, |
117 | 16.0k | SSL_INCOMING_STREAM_POLICY_ACCEPT, |
118 | 16.0k | 0)) |
119 | 0 | goto end; |
120 | | |
121 | 16.3M | for (;;) { |
122 | 16.3M | size_t size; |
123 | 16.3M | uint64_t nxtpktms = 0; |
124 | 16.3M | OSSL_TIME nxtpkt = ossl_time_zero(), nxttimeout; |
125 | 16.3M | int isinf, ret = 0; |
126 | | |
127 | 16.3M | if (len >= 2) { |
128 | 16.3M | if (len >= 5 && buf[0] == 0xff && buf[1] == 0xff) { |
129 | 1.67M | switch (buf[2]) { |
130 | 1.06M | case 0x00: |
131 | 1.06M | if (state == READING) |
132 | 1.05M | state = ACCEPTING_STREAM; |
133 | 1.06M | break; |
134 | 576k | case 0x01: |
135 | 576k | if (state == READING) |
136 | 566k | state = CREATING_STREAM; |
137 | 576k | break; |
138 | 3.38k | case 0x02: |
139 | 3.38k | if (state == READING) |
140 | 2.94k | state = SWAPPING_STREAM; |
141 | 3.38k | break; |
142 | 21.4k | default: |
143 | | /*ignore*/ |
144 | 21.4k | break; |
145 | 1.67M | } |
146 | 1.67M | len -= 3; |
147 | 1.67M | buf += 3; |
148 | 1.67M | } |
149 | 16.3M | nxtpktms = buf[0] + (buf[1] << 8); |
150 | 16.3M | nxtpkt = ossl_time_add(fake_now, ossl_ms2time(nxtpktms)); |
151 | 16.3M | len -= 2; |
152 | 16.3M | buf += 2; |
153 | 16.3M | } |
154 | | |
155 | 21.0M | for (;;) { |
156 | 21.0M | switch (state) { |
157 | 8.90M | case HANDSHAKING: |
158 | 8.90M | ret = SSL_do_handshake(stream); |
159 | 8.90M | if (ret == 1) |
160 | 3.82k | state = READING; |
161 | 8.90M | break; |
162 | | |
163 | 10.3M | case READING: |
164 | 10.3M | ret = SSL_read(stream, tmp, sizeof(tmp)); |
165 | 10.3M | if (ret > 0) { |
166 | 1.60k | state = WRITING; |
167 | 1.60k | writelen = ret; |
168 | 1.60k | assert(writelen <= (int)sizeof(tmp)); |
169 | 1.60k | } |
170 | 10.3M | break; |
171 | | |
172 | 10.3M | case WRITING: |
173 | 184k | ret = SSL_write(stream, tmp, writelen); |
174 | 184k | if (ret > 0) |
175 | 1.37k | state = READING; |
176 | 184k | break; |
177 | | |
178 | 1.05M | case ACCEPTING_STREAM: |
179 | 1.05M | state = READING; |
180 | 1.05M | ret = 1; |
181 | 1.05M | if (numstreams == OSSL_NELEM(allstreams) |
182 | 7.69k | || SSL_get_accept_stream_queue_len(client) == 0) |
183 | 1.05M | break; |
184 | 386 | thisstream = numstreams; |
185 | 386 | stream = allstreams[numstreams++] |
186 | 386 | = SSL_accept_stream(client, 0); |
187 | 386 | if (stream == NULL) |
188 | 0 | goto end; |
189 | 386 | break; |
190 | | |
191 | 566k | case CREATING_STREAM: |
192 | 566k | state = READING; |
193 | 566k | ret = 1; |
194 | 566k | if (numstreams == OSSL_NELEM(allstreams)) |
195 | 560k | break; |
196 | 5.97k | stream = SSL_new_stream(client, 0); |
197 | 5.97k | if (stream == NULL) { |
198 | | /* Ignore, and go back to the previous stream */ |
199 | 4.54k | stream = allstreams[thisstream]; |
200 | 4.54k | break; |
201 | 4.54k | } |
202 | 1.43k | thisstream = numstreams; |
203 | 1.43k | allstreams[numstreams++] = stream; |
204 | 1.43k | break; |
205 | | |
206 | 2.94k | case SWAPPING_STREAM: |
207 | 2.94k | state = READING; |
208 | 2.94k | ret = 1; |
209 | 2.94k | if (numstreams == 1) |
210 | 400 | break; |
211 | 2.54k | if (++thisstream == numstreams) |
212 | 856 | thisstream = 0; |
213 | 2.54k | stream = allstreams[thisstream]; |
214 | 2.54k | break; |
215 | 21.0M | } |
216 | 21.0M | assert(stream != NULL); |
217 | 21.0M | assert(thisstream < numstreams); |
218 | 21.0M | if (ret <= 0) { |
219 | 19.4M | switch (SSL_get_error(stream, ret)) { |
220 | 19.2M | case SSL_ERROR_WANT_READ: |
221 | 19.4M | case SSL_ERROR_WANT_WRITE: |
222 | 19.4M | break; |
223 | 12.9k | default: |
224 | 12.9k | goto end; |
225 | 19.4M | } |
226 | 19.4M | } |
227 | | |
228 | 21.0M | if (!SSL_get_event_timeout(client, &tv, &isinf)) |
229 | 0 | goto end; |
230 | | |
231 | 21.0M | if (isinf) { |
232 | 52.3k | fake_now = nxtpkt; |
233 | 52.3k | break; |
234 | 20.9M | } else { |
235 | 20.9M | nxttimeout = ossl_time_add(fake_now, |
236 | 20.9M | ossl_time_from_timeval(tv)); |
237 | 20.9M | if (len > 3 && ossl_time_compare(nxttimeout, nxtpkt) >= 0) { |
238 | 16.2M | fake_now = nxtpkt; |
239 | 16.2M | break; |
240 | 16.2M | } |
241 | 4.73M | fake_now = nxttimeout; |
242 | 4.73M | } |
243 | 21.0M | } |
244 | | |
245 | 16.2M | if (len <= 3) |
246 | 463 | break; |
247 | | |
248 | 16.2M | size = buf[0] + (buf[1] << 8); |
249 | 16.2M | if (size > len - 2) |
250 | 2.70k | break; |
251 | | |
252 | 16.2M | if (size > 0) |
253 | 4.32M | BIO_write(in, buf + 2, (int)size); |
254 | 16.2M | len -= size + 2; |
255 | 16.2M | buf += size + 2; |
256 | 16.2M | } |
257 | 16.0k | end: |
258 | 33.9k | for (i = 0; i < numstreams; i++) |
259 | 17.9k | SSL_free(allstreams[i]); |
260 | 16.0k | ERR_clear_error(); |
261 | 16.0k | SSL_CTX_free(ctx); |
262 | 16.0k | BIO_ADDR_free(peer_addr); |
263 | | |
264 | 16.0k | return 0; |
265 | 16.0k | } |
266 | | |
267 | | void FuzzerCleanup(void) |
268 | 0 | { |
269 | 0 | FuzzerClearRand(); |
270 | 0 | } |