/src/openssl35/fuzz/quic-lcidm.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2024 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/quic_lcidm.h" |
16 | | #include "internal/packet.h" |
17 | | |
18 | | int FuzzerInitialize(int *argc, char ***argv) |
19 | 200 | { |
20 | 200 | FuzzerSetRand(); |
21 | 200 | OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL); |
22 | 200 | OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL); |
23 | 200 | ERR_clear_error(); |
24 | 200 | return 1; |
25 | 200 | } |
26 | | |
27 | | /* |
28 | | * Fuzzer input "protocol": |
29 | | * Big endian |
30 | | * u8(LCID length) |
31 | | * Zero or more of: |
32 | | * ENROL_ODCID u0(0x00) u64(opaque) u8(cidl):cid |
33 | | * RETIRE_ODCID u8(0x01) u64(opaque) |
34 | | * GENERATE_INITIAL u8(0x02) u64(opaque) |
35 | | * GENERATE u8(0x03) u64(opaque) |
36 | | * RETIRE u8(0x04) u64(opaque) u64(retire_prior_to) |
37 | | * CULL u8(0x05) u64(opaque) |
38 | | * LOOKUP u8(0x06) u8(cidl):cid |
39 | | */ |
40 | | |
41 | | enum { |
42 | | CMD_ENROL_ODCID, |
43 | | CMD_RETIRE_ODCID, |
44 | | CMD_GENERATE_INITIAL, |
45 | | CMD_GENERATE, |
46 | | CMD_RETIRE, |
47 | | CMD_CULL, |
48 | | CMD_LOOKUP |
49 | | }; |
50 | | |
51 | 1.53M | #define MAX_CMDS 5000 |
52 | | |
53 | | static int get_cid(PACKET *pkt, QUIC_CONN_ID *cid) |
54 | 822k | { |
55 | 822k | unsigned int cidl; |
56 | | |
57 | 822k | if (!PACKET_get_1(pkt, &cidl) |
58 | 822k | || cidl > QUIC_MAX_CONN_ID_LEN |
59 | 821k | || !PACKET_copy_bytes(pkt, cid->id, cidl)) |
60 | 243 | return 0; |
61 | | |
62 | 821k | cid->id_len = (unsigned char)cidl; |
63 | 821k | return 1; |
64 | 822k | } |
65 | | |
66 | | int FuzzerTestOneInput(const uint8_t *buf, size_t len) |
67 | 3.42k | { |
68 | 3.42k | int rc = 0; |
69 | 3.42k | QUIC_LCIDM *lcidm = NULL; |
70 | 3.42k | PACKET pkt; |
71 | 3.42k | uint64_t arg_opaque, arg_retire_prior_to, seq_num_out; |
72 | 3.42k | unsigned int cmd, lcidl; |
73 | 3.42k | QUIC_CONN_ID arg_cid, cid_out; |
74 | 3.42k | OSSL_QUIC_FRAME_NEW_CONN_ID ncid_frame; |
75 | 3.42k | int did_retire; |
76 | 3.42k | void *opaque_out; |
77 | 3.42k | size_t limit = 0; |
78 | | |
79 | 3.42k | if (!PACKET_buf_init(&pkt, buf, len)) |
80 | 0 | goto err; |
81 | | |
82 | 3.42k | if (!PACKET_get_1(&pkt, &lcidl) |
83 | 3.42k | || lcidl > QUIC_MAX_CONN_ID_LEN) { |
84 | 10 | rc = -1; |
85 | 10 | goto err; |
86 | 10 | } |
87 | | |
88 | 3.41k | if ((lcidm = ossl_quic_lcidm_new(NULL, lcidl)) == NULL) { |
89 | 0 | rc = -1; |
90 | 0 | goto err; |
91 | 0 | } |
92 | | |
93 | 1.53M | while (PACKET_remaining(&pkt) > 0) { |
94 | 1.53M | if (!PACKET_get_1(&pkt, &cmd)) |
95 | 0 | goto err; |
96 | | |
97 | 1.53M | if (++limit > MAX_CMDS) |
98 | 150 | goto err; |
99 | | |
100 | 1.53M | switch (cmd) { |
101 | 351k | case CMD_ENROL_ODCID: |
102 | 351k | if (!PACKET_get_net_8(&pkt, &arg_opaque) |
103 | 351k | || !get_cid(&pkt, &arg_cid)) { |
104 | 177 | rc = -1; |
105 | 177 | goto err; |
106 | 177 | } |
107 | | |
108 | 351k | ossl_quic_lcidm_enrol_odcid(lcidm, (void *)(uintptr_t)arg_opaque, |
109 | 351k | &arg_cid); |
110 | 351k | break; |
111 | | |
112 | 22.5k | case CMD_RETIRE_ODCID: |
113 | 22.5k | if (!PACKET_get_net_8(&pkt, &arg_opaque)) { |
114 | 15 | rc = -1; |
115 | 15 | goto err; |
116 | 15 | } |
117 | | |
118 | 22.5k | ossl_quic_lcidm_retire_odcid(lcidm, (void *)(uintptr_t)arg_opaque); |
119 | 22.5k | break; |
120 | | |
121 | 90.1k | case CMD_GENERATE_INITIAL: |
122 | 90.1k | if (!PACKET_get_net_8(&pkt, &arg_opaque)) { |
123 | 23 | rc = -1; |
124 | 23 | goto err; |
125 | 23 | } |
126 | | |
127 | 90.1k | ossl_quic_lcidm_generate_initial(lcidm, (void *)(uintptr_t)arg_opaque, |
128 | 90.1k | &cid_out); |
129 | 90.1k | break; |
130 | | |
131 | 703k | case CMD_GENERATE: |
132 | 703k | if (!PACKET_get_net_8(&pkt, &arg_opaque)) { |
133 | 78 | rc = -1; |
134 | 78 | goto err; |
135 | 78 | } |
136 | | |
137 | 703k | ossl_quic_lcidm_generate(lcidm, (void *)(uintptr_t)arg_opaque, |
138 | 703k | &ncid_frame); |
139 | 703k | break; |
140 | | |
141 | 57.6k | case CMD_RETIRE: |
142 | 57.6k | if (!PACKET_get_net_8(&pkt, &arg_opaque) |
143 | 57.5k | || !PACKET_get_net_8(&pkt, &arg_retire_prior_to)) { |
144 | 64 | rc = -1; |
145 | 64 | goto err; |
146 | 64 | } |
147 | | |
148 | 57.5k | ossl_quic_lcidm_retire(lcidm, (void *)(uintptr_t)arg_opaque, |
149 | 57.5k | arg_retire_prior_to, |
150 | 57.5k | NULL, &cid_out, |
151 | 57.5k | &seq_num_out, &did_retire); |
152 | 57.5k | break; |
153 | | |
154 | 13.8k | case CMD_CULL: |
155 | 13.8k | if (!PACKET_get_net_8(&pkt, &arg_opaque)) { |
156 | 18 | rc = -1; |
157 | 18 | goto err; |
158 | 18 | } |
159 | | |
160 | 13.8k | ossl_quic_lcidm_cull(lcidm, (void *)(uintptr_t)arg_opaque); |
161 | 13.8k | break; |
162 | | |
163 | 293k | case CMD_LOOKUP: |
164 | 293k | if (!get_cid(&pkt, &arg_cid)) { |
165 | 71 | rc = -1; |
166 | 71 | goto err; |
167 | 71 | } |
168 | | |
169 | 293k | ossl_quic_lcidm_lookup(lcidm, &arg_cid, &seq_num_out, &opaque_out); |
170 | 293k | break; |
171 | | |
172 | 166 | default: |
173 | 166 | rc = -1; |
174 | 166 | goto err; |
175 | 1.53M | } |
176 | 1.53M | } |
177 | | |
178 | 3.42k | err: |
179 | 3.42k | ossl_quic_lcidm_free(lcidm); |
180 | 3.42k | return rc; |
181 | 3.41k | } |
182 | | |
183 | | void FuzzerCleanup(void) |
184 | 0 | { |
185 | 0 | FuzzerClearRand(); |
186 | 0 | } |