/src/openssl34/fuzz/quic-client.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2022 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 | 275M | { |
26 | 275M | return fake_now; |
27 | 275M | } |
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 | 47.4M | #define HANDSHAKING 0 |
47 | 28.5M | #define READING 1 |
48 | 67.4k | #define WRITING 2 |
49 | 6.51M | #define ACCEPTING_STREAM 3 |
50 | 3.57M | #define CREATING_STREAM 4 |
51 | 5.78k | #define SWAPPING_STREAM 5 |
52 | | |
53 | | int FuzzerTestOneInput(const uint8_t *buf, size_t len) |
54 | 41.9k | { |
55 | 41.9k | SSL *client = NULL, *stream = NULL; |
56 | 41.9k | SSL *allstreams[] = { NULL, NULL, NULL, NULL }; |
57 | 41.9k | size_t i, thisstream = 0, numstreams = 1; |
58 | 41.9k | BIO *in; |
59 | 41.9k | BIO *out; |
60 | 41.9k | SSL_CTX *ctx; |
61 | 41.9k | BIO_ADDR *peer_addr = NULL; |
62 | 41.9k | struct in_addr ina = { 0 }; |
63 | 41.9k | struct timeval tv; |
64 | 41.9k | int state = HANDSHAKING; |
65 | 41.9k | uint8_t tmp[1024]; |
66 | 41.9k | int writelen = 0; |
67 | | |
68 | 41.9k | if (len == 0) |
69 | 0 | return 0; |
70 | | |
71 | | /* This only fuzzes the initial flow from the client so far. */ |
72 | 41.9k | ctx = SSL_CTX_new(OSSL_QUIC_client_method()); |
73 | 41.9k | if (ctx == NULL) |
74 | 0 | goto end; |
75 | | |
76 | 41.9k | client = SSL_new(ctx); |
77 | 41.9k | if (client == NULL) |
78 | 0 | goto end; |
79 | | |
80 | 41.9k | fake_now = ossl_ms2time(1); |
81 | 41.9k | if (!ossl_quic_conn_set_override_now_cb(client, fake_now_cb, NULL)) |
82 | 0 | goto end; |
83 | | |
84 | 41.9k | peer_addr = BIO_ADDR_new(); |
85 | 41.9k | if (peer_addr == NULL) |
86 | 0 | goto end; |
87 | | |
88 | 41.9k | ina.s_addr = htonl(0x7f000001UL); |
89 | | |
90 | 41.9k | if (!BIO_ADDR_rawmake(peer_addr, AF_INET, &ina, sizeof(ina), htons(4433))) |
91 | 0 | goto end; |
92 | | |
93 | 41.9k | SSL_set_tlsext_host_name(client, "localhost"); |
94 | 41.9k | in = BIO_new(BIO_s_dgram_mem()); |
95 | 41.9k | if (in == NULL) |
96 | 0 | goto end; |
97 | 41.9k | out = BIO_new(BIO_s_dgram_mem()); |
98 | 41.9k | if (out == NULL) { |
99 | 0 | BIO_free(in); |
100 | 0 | goto end; |
101 | 0 | } |
102 | 41.9k | 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 | 41.9k | SSL_set_bio(client, in, out); |
108 | 41.9k | if (SSL_set_alpn_protos(client, (const unsigned char *)"\x08ossltest", 9) != 0) |
109 | 0 | goto end; |
110 | 41.9k | if (SSL_set1_initial_peer_addr(client, peer_addr) != 1) |
111 | 0 | goto end; |
112 | 41.9k | SSL_set_connect_state(client); |
113 | | |
114 | 41.9k | if (!SSL_set_incoming_stream_policy(client, |
115 | 41.9k | SSL_INCOMING_STREAM_POLICY_ACCEPT, |
116 | 41.9k | 0)) |
117 | 0 | goto end; |
118 | | |
119 | 41.9k | allstreams[0] = stream = client; |
120 | 48.7M | for (;;) { |
121 | 48.7M | size_t size; |
122 | 48.7M | uint64_t nxtpktms = 0; |
123 | 48.7M | OSSL_TIME nxtpkt = ossl_time_zero(), nxttimeout; |
124 | 48.7M | int isinf, ret = 0; |
125 | | |
126 | 48.7M | if (len >= 2) { |
127 | 48.7M | if (len >= 5 && buf[0] == 0xff && buf[1] == 0xff) { |
128 | 5.09M | switch (buf[2]) { |
129 | 3.27M | case 0x00: |
130 | 3.27M | if (state == READING) |
131 | 3.25M | state = ACCEPTING_STREAM; |
132 | 3.27M | break; |
133 | 1.80M | case 0x01: |
134 | 1.80M | if (state == READING) |
135 | 1.78M | state = CREATING_STREAM; |
136 | 1.80M | break; |
137 | 3.56k | case 0x02: |
138 | 3.56k | if (state == READING) |
139 | 2.89k | state = SWAPPING_STREAM; |
140 | 3.56k | break; |
141 | 18.1k | default: |
142 | | /*ignore*/ |
143 | 18.1k | break; |
144 | 5.09M | } |
145 | 5.09M | len -= 3; |
146 | 5.09M | buf += 3; |
147 | 5.09M | } |
148 | 48.7M | nxtpktms = buf[0] + (buf[1] << 8); |
149 | 48.7M | nxtpkt = ossl_time_add(fake_now, ossl_ms2time(nxtpktms)); |
150 | 48.7M | len -= 2; |
151 | 48.7M | buf += 2; |
152 | 48.7M | } |
153 | | |
154 | 70.8M | for (;;) { |
155 | 70.8M | switch (state) { |
156 | 47.3M | case HANDSHAKING: |
157 | 47.3M | ret = SSL_do_handshake(stream); |
158 | 47.3M | if (ret == 1) |
159 | 10.5k | state = READING; |
160 | 47.3M | break; |
161 | | |
162 | 18.3M | case READING: |
163 | 18.3M | ret = SSL_read(stream, tmp, sizeof(tmp)); |
164 | 18.3M | if (ret > 0) { |
165 | 4.47k | state = WRITING; |
166 | 4.47k | writelen = ret; |
167 | 4.47k | assert(writelen <= (int)sizeof(tmp)); |
168 | 4.47k | } |
169 | 18.3M | break; |
170 | | |
171 | 18.3M | case WRITING: |
172 | 62.9k | ret = SSL_write(stream, tmp, writelen); |
173 | 62.9k | if (ret > 0) |
174 | 3.69k | state = READING; |
175 | 62.9k | break; |
176 | | |
177 | 3.25M | case ACCEPTING_STREAM: |
178 | 3.25M | state = READING; |
179 | 3.25M | ret = 1; |
180 | 3.25M | if (numstreams == OSSL_NELEM(allstreams) |
181 | 9.22k | || SSL_get_accept_stream_queue_len(client) == 0) |
182 | 3.25M | break; |
183 | 333 | thisstream = numstreams; |
184 | 333 | stream = allstreams[numstreams++] |
185 | 333 | = SSL_accept_stream(client, 0); |
186 | 333 | if (stream == NULL) |
187 | 0 | goto end; |
188 | 333 | break; |
189 | | |
190 | 1.78M | case CREATING_STREAM: |
191 | 1.78M | state = READING; |
192 | 1.78M | ret = 1; |
193 | 1.78M | if (numstreams == OSSL_NELEM(allstreams)) |
194 | 1.77M | break; |
195 | 9.56k | stream = SSL_new_stream(client, 0); |
196 | 9.56k | if (stream == NULL) { |
197 | | /* Ignore, and go back to the previous stream */ |
198 | 5.26k | stream = allstreams[thisstream]; |
199 | 5.26k | break; |
200 | 5.26k | } |
201 | 4.30k | thisstream = numstreams; |
202 | 4.30k | allstreams[numstreams++] = stream; |
203 | 4.30k | break; |
204 | | |
205 | 2.89k | case SWAPPING_STREAM: |
206 | 2.89k | state = READING; |
207 | 2.89k | ret = 1; |
208 | 2.89k | if (numstreams == 1) |
209 | 766 | break; |
210 | 2.12k | if (++thisstream == numstreams) |
211 | 1.05k | thisstream = 0; |
212 | 2.12k | stream = allstreams[thisstream]; |
213 | 2.12k | break; |
214 | 70.8M | } |
215 | 70.8M | assert(stream != NULL); |
216 | 70.8M | assert(thisstream < numstreams); |
217 | 70.8M | if (ret <= 0) { |
218 | 65.8M | switch (SSL_get_error(stream, ret)) { |
219 | 65.7M | case SSL_ERROR_WANT_READ: |
220 | 65.7M | case SSL_ERROR_WANT_WRITE: |
221 | 65.7M | break; |
222 | 33.5k | default: |
223 | 33.5k | goto end; |
224 | 65.8M | } |
225 | 65.8M | } |
226 | | |
227 | 70.8M | if (!SSL_get_event_timeout(client, &tv, &isinf)) |
228 | 0 | goto end; |
229 | | |
230 | 70.8M | if (isinf) { |
231 | 135k | fake_now = nxtpkt; |
232 | 135k | break; |
233 | 70.7M | } else { |
234 | 70.7M | nxttimeout = ossl_time_add(fake_now, |
235 | 70.7M | ossl_time_from_timeval(tv)); |
236 | 70.7M | if (len > 3 && ossl_time_compare(nxttimeout, nxtpkt) >= 0) { |
237 | 48.5M | fake_now = nxtpkt; |
238 | 48.5M | break; |
239 | 48.5M | } |
240 | 22.1M | fake_now = nxttimeout; |
241 | 22.1M | } |
242 | 70.8M | } |
243 | | |
244 | 48.7M | if (len <= 3) |
245 | 1.84k | break; |
246 | | |
247 | 48.7M | size = buf[0] + (buf[1] << 8); |
248 | 48.7M | if (size > len - 2) |
249 | 6.49k | break; |
250 | | |
251 | 48.6M | if (size > 0) |
252 | 10.6M | BIO_write(in, buf + 2, size); |
253 | 48.6M | len -= size + 2; |
254 | 48.6M | buf += size + 2; |
255 | 48.6M | } |
256 | 41.9k | end: |
257 | 88.4k | for (i = 0; i < numstreams; i++) |
258 | 46.5k | SSL_free(allstreams[i]); |
259 | 41.9k | ERR_clear_error(); |
260 | 41.9k | SSL_CTX_free(ctx); |
261 | 41.9k | BIO_ADDR_free(peer_addr); |
262 | | |
263 | 41.9k | return 0; |
264 | 41.9k | } |
265 | | |
266 | | void FuzzerCleanup(void) |
267 | 0 | { |
268 | 0 | FuzzerClearRand(); |
269 | 0 | } |