Provide a new WPACKET function for filling in all the lengths
[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(), WPACKET_finish() and
184  * WPACKET_fill_lengths() to close a sub-packet and write out its length if
185  * necessary. If |doclose| is 0 then it goes through the motions of closing
186  * (i.e. it fills in all the lengths), but doesn't actually close anything.
187  */
188 static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
189 {
190     size_t packlen = pkt->written - sub->pwritten;
191
192     if (packlen == 0
193             && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
194         return 0;
195
196     if (packlen == 0
197             && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
198         /* We can't handle this case. Return an error */
199         if (!doclose)
200             return 0;
201
202         /* Deallocate any bytes allocated for the length of the WPACKET */
203         if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
204             pkt->written -= sub->lenbytes;
205             pkt->curr -= sub->lenbytes;
206         }
207
208         /* Don't write out the packet length */
209         sub->packet_len = 0;
210         sub->lenbytes = 0;
211     }
212
213     /* Write out the WPACKET length if needed */
214     if (sub->lenbytes > 0
215                 && !put_value(&GETBUF(pkt)[sub->packet_len], packlen,
216                               sub->lenbytes))
217             return 0;
218
219     if (doclose) {
220         pkt->subs = sub->parent;
221         OPENSSL_free(sub);
222     }
223
224     return 1;
225 }
226
227 int WPACKET_fill_lengths(WPACKET *pkt)
228 {
229     WPACKET_SUB *sub;
230
231     assert(pkt->subs != NULL);
232     if (pkt->subs == NULL)
233         return 0;
234
235     sub = pkt->subs;
236     do {
237         if (!wpacket_intern_close(pkt, sub, 0))
238             return 0;
239         sub = sub->parent;
240     } while (sub != NULL);
241
242     return 1;
243 }
244
245 int WPACKET_close(WPACKET *pkt)
246 {
247     /*
248      * Internal API, so should not fail - but we do negative testing of this
249      * so no assert (otherwise the tests fail)
250      */
251     if (pkt->subs == NULL || pkt->subs->parent == NULL)
252         return 0;
253
254     return wpacket_intern_close(pkt, pkt->subs, 1);
255 }
256
257 int WPACKET_finish(WPACKET *pkt)
258 {
259     int ret;
260
261     /*
262      * Internal API, so should not fail - but we do negative testing of this
263      * so no assert (otherwise the tests fail)
264      */
265     if (pkt->subs == NULL || pkt->subs->parent != NULL)
266         return 0;
267
268     ret = wpacket_intern_close(pkt, pkt->subs, 1);
269     if (ret) {
270         OPENSSL_free(pkt->subs);
271         pkt->subs = NULL;
272     }
273
274     return ret;
275 }
276
277 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
278 {
279     WPACKET_SUB *sub;
280     unsigned char *lenchars;
281
282     /* Internal API, so should not fail */
283     assert(pkt->subs != NULL);
284     if (pkt->subs == NULL)
285         return 0;
286
287     sub = OPENSSL_zalloc(sizeof(*sub));
288     if (sub == NULL)
289         return 0;
290
291     sub->parent = pkt->subs;
292     pkt->subs = sub;
293     sub->pwritten = pkt->written + lenbytes;
294     sub->lenbytes = lenbytes;
295
296     if (lenbytes == 0) {
297         sub->packet_len = 0;
298         return 1;
299     }
300
301     if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
302         return 0;
303     /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
304     sub->packet_len = lenchars - GETBUF(pkt);
305
306     return 1;
307 }
308
309 int WPACKET_start_sub_packet(WPACKET *pkt)
310 {
311     return WPACKET_start_sub_packet_len__(pkt, 0);
312 }
313
314 int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
315 {
316     unsigned char *data;
317
318     /* Internal API, so should not fail */
319     assert(size <= sizeof(unsigned int));
320
321     if (size > sizeof(unsigned int)
322             || !WPACKET_allocate_bytes(pkt, size, &data)
323             || !put_value(data, val, size))
324         return 0;
325
326     return 1;
327 }
328
329 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
330 {
331     WPACKET_SUB *sub;
332     size_t lenbytes;
333
334     /* Internal API, so should not fail */
335     assert(pkt->subs != NULL);
336     if (pkt->subs == NULL)
337         return 0;
338
339     /* Find the WPACKET_SUB for the top level */
340     for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
341         continue;
342
343     lenbytes = sub->lenbytes;
344     if (lenbytes == 0)
345         lenbytes = sizeof(pkt->maxsize);
346
347     if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
348         return 0;
349
350     pkt->maxsize = maxsize;
351
352     return 1;
353 }
354
355 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
356 {
357     unsigned char *dest;
358
359     if (len == 0)
360         return 1;
361
362     if (!WPACKET_allocate_bytes(pkt, len, &dest))
363         return 0;
364
365     memcpy(dest, src, len);
366
367     return 1;
368 }
369
370 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
371                          size_t lenbytes)
372 {
373     if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
374             || !WPACKET_memcpy(pkt, src, len)
375             || !WPACKET_close(pkt))
376         return 0;
377
378     return 1;
379 }
380
381 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
382 {
383     /* Internal API, so should not fail */
384     assert(written != NULL);
385     if (written == NULL)
386         return 0;
387
388     *written = pkt->written;
389
390     return 1;
391 }
392
393 int WPACKET_get_length(WPACKET *pkt, size_t *len)
394 {
395     /* Internal API, so should not fail */
396     assert(pkt->subs != NULL && len != NULL);
397     if (pkt->subs == NULL || len == NULL)
398         return 0;
399
400     *len = pkt->written - pkt->subs->pwritten;
401
402     return 1;
403 }
404
405 unsigned char *WPACKET_get_curr(WPACKET *pkt)
406 {
407     return GETBUF(pkt) + pkt->curr;
408 }
409
410 void WPACKET_cleanup(WPACKET *pkt)
411 {
412     WPACKET_SUB *sub, *parent;
413
414     for (sub = pkt->subs; sub != NULL; sub = parent) {
415         parent = sub->parent;
416         OPENSSL_free(sub);
417     }
418     pkt->subs = NULL;
419 }