/src/openssl33/fuzz/quic-lcidm.c
Line | Count | Source (jump to first uncovered line) |
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/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 | | static int get_cid(PACKET *pkt, QUIC_CONN_ID *cid) |
52 | 1.23M | { |
53 | 1.23M | unsigned int cidl; |
54 | | |
55 | 1.23M | if (!PACKET_get_1(pkt, &cidl) |
56 | 1.23M | || cidl > QUIC_MAX_CONN_ID_LEN |
57 | 1.23M | || !PACKET_copy_bytes(pkt, cid->id, cidl)) |
58 | 276 | return 0; |
59 | | |
60 | 1.23M | cid->id_len = (unsigned char)cidl; |
61 | 1.23M | return 1; |
62 | 1.23M | } |
63 | | |
64 | | int FuzzerTestOneInput(const uint8_t *buf, size_t len) |
65 | 1.23k | { |
66 | 1.23k | int rc = 0; |
67 | 1.23k | QUIC_LCIDM *lcidm = NULL; |
68 | 1.23k | PACKET pkt; |
69 | 1.23k | uint64_t arg_opaque, arg_retire_prior_to, seq_num_out; |
70 | 1.23k | unsigned int cmd, lcidl; |
71 | 1.23k | QUIC_CONN_ID arg_cid, cid_out; |
72 | 1.23k | OSSL_QUIC_FRAME_NEW_CONN_ID ncid_frame; |
73 | 1.23k | int did_retire; |
74 | 1.23k | void *opaque_out; |
75 | | |
76 | 1.23k | if (!PACKET_buf_init(&pkt, buf, len)) |
77 | 0 | goto err; |
78 | | |
79 | 1.23k | if (!PACKET_get_1(&pkt, &lcidl) |
80 | 1.23k | || lcidl > QUIC_MAX_CONN_ID_LEN) { |
81 | 4 | rc = -1; |
82 | 4 | goto err; |
83 | 4 | } |
84 | | |
85 | 1.23k | if ((lcidm = ossl_quic_lcidm_new(NULL, lcidl)) == NULL) { |
86 | 0 | rc = -1; |
87 | 0 | goto err; |
88 | 0 | } |
89 | | |
90 | 1.77M | while (PACKET_remaining(&pkt) > 0) { |
91 | 1.77M | if (!PACKET_get_1(&pkt, &cmd)) |
92 | 0 | goto err; |
93 | | |
94 | 1.77M | switch (cmd) { |
95 | 173k | case CMD_ENROL_ODCID: |
96 | 173k | if (!PACKET_get_net_8(&pkt, &arg_opaque) |
97 | 173k | || !get_cid(&pkt, &arg_cid)) { |
98 | 79 | rc = -1; |
99 | 79 | goto err; |
100 | 79 | } |
101 | | |
102 | 173k | ossl_quic_lcidm_enrol_odcid(lcidm, (void *)(uintptr_t)arg_opaque, |
103 | 173k | &arg_cid); |
104 | 173k | break; |
105 | | |
106 | 15.3k | case CMD_RETIRE_ODCID: |
107 | 15.3k | if (!PACKET_get_net_8(&pkt, &arg_opaque)) { |
108 | 3 | rc = -1; |
109 | 3 | goto err; |
110 | 3 | } |
111 | | |
112 | 15.3k | ossl_quic_lcidm_retire_odcid(lcidm, (void *)(uintptr_t)arg_opaque); |
113 | 15.3k | break; |
114 | | |
115 | 70.1k | case CMD_GENERATE_INITIAL: |
116 | 70.1k | if (!PACKET_get_net_8(&pkt, &arg_opaque)) { |
117 | 15 | rc = -1; |
118 | 15 | goto err; |
119 | 15 | } |
120 | | |
121 | 70.1k | ossl_quic_lcidm_generate_initial(lcidm, (void *)(uintptr_t)arg_opaque, |
122 | 70.1k | &cid_out); |
123 | 70.1k | break; |
124 | | |
125 | 1.42M | case CMD_GENERATE: |
126 | 1.42M | if (!PACKET_get_net_8(&pkt, &arg_opaque)) { |
127 | 48 | rc = -1; |
128 | 48 | goto err; |
129 | 48 | } |
130 | | |
131 | 1.42M | ossl_quic_lcidm_generate(lcidm, (void *)(uintptr_t)arg_opaque, |
132 | 1.42M | &ncid_frame); |
133 | 1.42M | break; |
134 | | |
135 | 43.0k | case CMD_RETIRE: |
136 | 43.0k | if (!PACKET_get_net_8(&pkt, &arg_opaque) |
137 | 43.0k | || !PACKET_get_net_8(&pkt, &arg_retire_prior_to)) { |
138 | 19 | rc = -1; |
139 | 19 | goto err; |
140 | 19 | } |
141 | | |
142 | 43.0k | ossl_quic_lcidm_retire(lcidm, (void *)(uintptr_t)arg_opaque, |
143 | 43.0k | arg_retire_prior_to, |
144 | 43.0k | NULL, &cid_out, |
145 | 43.0k | &seq_num_out, &did_retire); |
146 | 43.0k | break; |
147 | | |
148 | 34.7k | case CMD_CULL: |
149 | 34.7k | if (!PACKET_get_net_8(&pkt, &arg_opaque)) { |
150 | 6 | rc = -1; |
151 | 6 | goto err; |
152 | 6 | } |
153 | | |
154 | 34.7k | ossl_quic_lcidm_cull(lcidm, (void *)(uintptr_t)arg_opaque); |
155 | 34.7k | break; |
156 | | |
157 | 12.8k | case CMD_LOOKUP: |
158 | 12.8k | if (!get_cid(&pkt, &arg_cid)) { |
159 | 17 | rc = -1; |
160 | 17 | goto err; |
161 | 17 | } |
162 | | |
163 | 12.8k | ossl_quic_lcidm_lookup(lcidm, &arg_cid, &seq_num_out, &opaque_out); |
164 | 12.8k | break; |
165 | | |
166 | 85 | default: |
167 | 85 | rc = -1; |
168 | 85 | goto err; |
169 | 1.77M | } |
170 | 1.77M | } |
171 | | |
172 | 1.23k | err: |
173 | 1.23k | ossl_quic_lcidm_free(lcidm); |
174 | 1.23k | return rc; |
175 | 1.23k | } |
176 | | |
177 | | void FuzzerCleanup(void) |
178 | 0 | { |
179 | 0 | FuzzerClearRand(); |
180 | 0 | } |