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