]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/packet.c
Make asn1 fuzzer more reproducible
[thirdparty/openssl.git] / ssl / packet.c
1 /*
2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <assert.h>
11 #include "packet_locl.h"
12
13 #define DEFAULT_BUF_SIZE 256
14
15 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
16 {
17 if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
18 return 0;
19
20 pkt->written += len;
21 pkt->curr += len;
22 return 1;
23 }
24
25 int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
26 unsigned char **allocbytes, size_t lenbytes)
27 {
28 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
29 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
30 || !WPACKET_close(pkt))
31 return 0;
32
33 return 1;
34 }
35
36 #define GETBUF(p) (((p)->staticbuf != NULL) \
37 ? (p)->staticbuf : (unsigned char *)(p)->buf->data)
38
39 int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
40 {
41 /* Internal API, so should not fail */
42 assert(pkt->subs != NULL && len != 0);
43 if (pkt->subs == NULL || len == 0)
44 return 0;
45
46 if (pkt->maxsize - pkt->written < len)
47 return 0;
48
49 if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
50 size_t newlen;
51 size_t reflen;
52
53 reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
54
55 if (reflen > SIZE_MAX / 2) {
56 newlen = SIZE_MAX;
57 } else {
58 newlen = reflen * 2;
59 if (newlen < DEFAULT_BUF_SIZE)
60 newlen = DEFAULT_BUF_SIZE;
61 }
62 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
63 return 0;
64 }
65 if (allocbytes != NULL)
66 *allocbytes = WPACKET_get_curr(pkt);
67
68 return 1;
69 }
70
71 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
72 unsigned char **allocbytes, size_t lenbytes)
73 {
74 if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
75 return 0;
76
77 *allocbytes += lenbytes;
78
79 return 1;
80 }
81
82 static size_t maxmaxsize(size_t lenbytes)
83 {
84 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
85 return SIZE_MAX;
86
87 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
88 }
89
90 static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
91 {
92 unsigned char *lenchars;
93
94 pkt->curr = 0;
95 pkt->written = 0;
96
97 pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
98 if (pkt->subs == NULL)
99 return 0;
100
101 if (lenbytes == 0)
102 return 1;
103
104 pkt->subs->pwritten = lenbytes;
105 pkt->subs->lenbytes = lenbytes;
106
107 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
108 OPENSSL_free(pkt->subs);
109 pkt->subs = NULL;
110 return 0;
111 }
112 pkt->subs->packet_len = lenchars - GETBUF(pkt);
113
114 return 1;
115 }
116
117 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
118 size_t lenbytes)
119 {
120 size_t max = maxmaxsize(lenbytes);
121
122 /* Internal API, so should not fail */
123 assert(buf != NULL && len > 0);
124 if (buf == NULL || len == 0)
125 return 0;
126
127 pkt->staticbuf = buf;
128 pkt->buf = NULL;
129 pkt->maxsize = (max < len) ? max : len;
130
131 return wpacket_intern_init_len(pkt, lenbytes);
132 }
133
134 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
135 {
136 /* Internal API, so should not fail */
137 assert(buf != NULL);
138 if (buf == NULL)
139 return 0;
140
141 pkt->staticbuf = NULL;
142 pkt->buf = buf;
143 pkt->maxsize = maxmaxsize(lenbytes);
144
145 return wpacket_intern_init_len(pkt, lenbytes);
146 }
147
148 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
149 {
150 return WPACKET_init_len(pkt, buf, 0);
151 }
152
153 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
154 {
155 /* Internal API, so should not fail */
156 assert(pkt->subs != NULL);
157 if (pkt->subs == NULL)
158 return 0;
159
160 pkt->subs->flags = flags;
161
162 return 1;
163 }
164
165 /* Store the |value| of length |len| at location |data| */
166 static int put_value(unsigned char *data, size_t value, size_t len)
167 {
168 for (data += len - 1; len > 0; len--) {
169 *data = (unsigned char)(value & 0xff);
170 data--;
171 value >>= 8;
172 }
173
174 /* Check whether we could fit the value in the assigned number of bytes */
175 if (value > 0)
176 return 0;
177
178 return 1;
179 }
180
181
182 /*
183 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
184 * close a sub-packet and write out its length if necessary.
185 */
186 static int wpacket_intern_close(WPACKET *pkt)
187 {
188 WPACKET_SUB *sub = pkt->subs;
189 size_t packlen = pkt->written - sub->pwritten;
190
191 if (packlen == 0
192 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
193 return 0;
194
195 if (packlen == 0
196 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
197 /* Deallocate any bytes allocated for the length of the WPACKET */
198 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
199 pkt->written -= sub->lenbytes;
200 pkt->curr -= sub->lenbytes;
201 }
202
203 /* Don't write out the packet length */
204 sub->packet_len = 0;
205 sub->lenbytes = 0;
206 }
207
208 /* Write out the WPACKET length if needed */
209 if (sub->lenbytes > 0
210 && !put_value(&GETBUF(pkt)[sub->packet_len], packlen,
211 sub->lenbytes))
212 return 0;
213
214 pkt->subs = sub->parent;
215 OPENSSL_free(sub);
216
217 return 1;
218 }
219
220 int WPACKET_close(WPACKET *pkt)
221 {
222 /*
223 * Internal API, so should not fail - but we do negative testing of this
224 * so no assert (otherwise the tests fail)
225 */
226 if (pkt->subs == NULL || pkt->subs->parent == NULL)
227 return 0;
228
229 return wpacket_intern_close(pkt);
230 }
231
232 int WPACKET_finish(WPACKET *pkt)
233 {
234 int ret;
235
236 /*
237 * Internal API, so should not fail - but we do negative testing of this
238 * so no assert (otherwise the tests fail)
239 */
240 if (pkt->subs == NULL || pkt->subs->parent != NULL)
241 return 0;
242
243 ret = wpacket_intern_close(pkt);
244 if (ret) {
245 OPENSSL_free(pkt->subs);
246 pkt->subs = NULL;
247 }
248
249 return ret;
250 }
251
252 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
253 {
254 WPACKET_SUB *sub;
255 unsigned char *lenchars;
256
257 /* Internal API, so should not fail */
258 assert(pkt->subs != NULL);
259 if (pkt->subs == NULL)
260 return 0;
261
262 sub = OPENSSL_zalloc(sizeof(*sub));
263 if (sub == NULL)
264 return 0;
265
266 sub->parent = pkt->subs;
267 pkt->subs = sub;
268 sub->pwritten = pkt->written + lenbytes;
269 sub->lenbytes = lenbytes;
270
271 if (lenbytes == 0) {
272 sub->packet_len = 0;
273 return 1;
274 }
275
276 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
277 return 0;
278 /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
279 sub->packet_len = lenchars - GETBUF(pkt);
280
281 return 1;
282 }
283
284 int WPACKET_start_sub_packet(WPACKET *pkt)
285 {
286 return WPACKET_start_sub_packet_len__(pkt, 0);
287 }
288
289 int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
290 {
291 unsigned char *data;
292
293 /* Internal API, so should not fail */
294 assert(size <= sizeof(unsigned int));
295
296 if (size > sizeof(unsigned int)
297 || !WPACKET_allocate_bytes(pkt, size, &data)
298 || !put_value(data, val, size))
299 return 0;
300
301 return 1;
302 }
303
304 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
305 {
306 WPACKET_SUB *sub;
307 size_t lenbytes;
308
309 /* Internal API, so should not fail */
310 assert(pkt->subs != NULL);
311 if (pkt->subs == NULL)
312 return 0;
313
314 /* Find the WPACKET_SUB for the top level */
315 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
316 continue;
317
318 lenbytes = sub->lenbytes;
319 if (lenbytes == 0)
320 lenbytes = sizeof(pkt->maxsize);
321
322 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
323 return 0;
324
325 pkt->maxsize = maxsize;
326
327 return 1;
328 }
329
330 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
331 {
332 unsigned char *dest;
333
334 if (len == 0)
335 return 1;
336
337 if (!WPACKET_allocate_bytes(pkt, len, &dest))
338 return 0;
339
340 memcpy(dest, src, len);
341
342 return 1;
343 }
344
345 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
346 size_t lenbytes)
347 {
348 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
349 || !WPACKET_memcpy(pkt, src, len)
350 || !WPACKET_close(pkt))
351 return 0;
352
353 return 1;
354 }
355
356 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
357 {
358 /* Internal API, so should not fail */
359 assert(written != NULL);
360 if (written == NULL)
361 return 0;
362
363 *written = pkt->written;
364
365 return 1;
366 }
367
368 int WPACKET_get_length(WPACKET *pkt, size_t *len)
369 {
370 /* Internal API, so should not fail */
371 assert(pkt->subs != NULL && len != NULL);
372 if (pkt->subs == NULL || len == NULL)
373 return 0;
374
375 *len = pkt->written - pkt->subs->pwritten;
376
377 return 1;
378 }
379
380 unsigned char *WPACKET_get_curr(WPACKET *pkt)
381 {
382 return GETBUF(pkt) + pkt->curr;
383 }
384
385 void WPACKET_cleanup(WPACKET *pkt)
386 {
387 WPACKET_SUB *sub, *parent;
388
389 for (sub = pkt->subs; sub != NULL; sub = parent) {
390 parent = sub->parent;
391 OPENSSL_free(sub);
392 }
393 pkt->subs = NULL;
394 }