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