Add support for initialising WPACKETs from a static buffer
[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     *allocbytes = GETBUF(pkt) + pkt->curr;
66
67     return 1;
68 }
69
70 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
71                                 unsigned char **allocbytes, size_t lenbytes)
72 {
73     if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
74         return 0;
75
76     *allocbytes += lenbytes;
77
78     return 1;
79 }
80
81 static size_t maxmaxsize(size_t lenbytes)
82 {
83     if (lenbytes >= sizeof(size_t) || lenbytes == 0)
84         return SIZE_MAX;
85
86     return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
87 }
88
89 static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
90 {
91     unsigned char *lenchars;
92
93     pkt->curr = 0;
94     pkt->written = 0;
95
96     pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
97     if (pkt->subs == NULL)
98         return 0;
99
100     if (lenbytes == 0)
101         return 1;
102
103     pkt->subs->pwritten = lenbytes;
104     pkt->subs->lenbytes = lenbytes;
105
106     if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
107         OPENSSL_free(pkt->subs);
108         pkt->subs = NULL;
109         return 0;
110     }
111     pkt->subs->packet_len = lenchars - GETBUF(pkt);
112
113     return 1;
114 }
115
116 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
117                             size_t lenbytes)
118 {
119     size_t max = maxmaxsize(lenbytes);
120
121     /* Internal API, so should not fail */
122     assert(buf != NULL && len > 0);
123     if (buf == NULL || len == 0)
124         return 0;
125
126     pkt->staticbuf = buf;
127     pkt->buf = NULL;
128     pkt->maxsize = (max < len) ? max : len;
129
130     return wpacket_intern_init_len(pkt, lenbytes);
131 }
132
133 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
134 {
135     /* Internal API, so should not fail */
136     assert(buf != NULL);
137     if (buf == NULL)
138         return 0;
139
140     pkt->staticbuf = NULL;
141     pkt->buf = buf;
142     pkt->maxsize = maxmaxsize(lenbytes);
143
144     return wpacket_intern_init_len(pkt, lenbytes);
145 }
146
147 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
148 {
149     return WPACKET_init_len(pkt, buf, 0);
150 }
151
152 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
153 {
154     /* Internal API, so should not fail */
155     assert(pkt->subs != NULL);
156     if (pkt->subs == NULL)
157         return 0;
158
159     pkt->subs->flags = flags;
160
161     return 1;
162 }
163
164 /* Store the |value| of length |len| at location |data| */
165 static int put_value(unsigned char *data, size_t value, size_t len)
166 {
167     for (data += len - 1; len > 0; len--) {
168         *data = (unsigned char)(value & 0xff);
169         data--;
170         value >>= 8;
171     }
172
173     /* Check whether we could fit the value in the assigned number of bytes */
174     if (value > 0)
175         return 0;
176
177     return 1;
178 }
179
180
181 /*
182  * Internal helper function used by WPACKET_close() and WPACKET_finish() to
183  * close a sub-packet and write out its length if necessary.
184  */
185 static int wpacket_intern_close(WPACKET *pkt)
186 {
187     WPACKET_SUB *sub = pkt->subs;
188     size_t packlen = pkt->written - sub->pwritten;
189
190     if (packlen == 0
191             && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
192         return 0;
193
194     if (packlen == 0
195             && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
196         /* Deallocate any bytes allocated for the length of the WPACKET */
197         if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
198             pkt->written -= sub->lenbytes;
199             pkt->curr -= sub->lenbytes;
200         }
201
202         /* Don't write out the packet length */
203         sub->packet_len = 0;
204         sub->lenbytes = 0;
205     }
206
207     /* Write out the WPACKET length if needed */
208     if (sub->lenbytes > 0
209                 && !put_value(&GETBUF(pkt)[sub->packet_len], packlen,
210                               sub->lenbytes))
211             return 0;
212
213     pkt->subs = sub->parent;
214     OPENSSL_free(sub);
215
216     return 1;
217 }
218
219 int WPACKET_close(WPACKET *pkt)
220 {
221     /*
222      * Internal API, so should not fail - but we do negative testing of this
223      * so no assert (otherwise the tests fail)
224      */
225     if (pkt->subs == NULL || pkt->subs->parent == NULL)
226         return 0;
227
228     return wpacket_intern_close(pkt);
229 }
230
231 int WPACKET_finish(WPACKET *pkt)
232 {
233     int ret;
234
235     /*
236      * Internal API, so should not fail - but we do negative testing of this
237      * so no assert (otherwise the tests fail)
238      */
239     if (pkt->subs == NULL || pkt->subs->parent != NULL)
240         return 0;
241
242     ret = wpacket_intern_close(pkt);
243     if (ret) {
244         OPENSSL_free(pkt->subs);
245         pkt->subs = NULL;
246     }
247
248     return ret;
249 }
250
251 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
252 {
253     WPACKET_SUB *sub;
254     unsigned char *lenchars;
255
256     /* Internal API, so should not fail */
257     assert(pkt->subs != NULL);
258     if (pkt->subs == NULL)
259         return 0;
260
261     sub = OPENSSL_zalloc(sizeof(*sub));
262     if (sub == NULL)
263         return 0;
264
265     sub->parent = pkt->subs;
266     pkt->subs = sub;
267     sub->pwritten = pkt->written + lenbytes;
268     sub->lenbytes = lenbytes;
269
270     if (lenbytes == 0) {
271         sub->packet_len = 0;
272         return 1;
273     }
274
275     if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
276         return 0;
277     /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
278     sub->packet_len = lenchars - GETBUF(pkt);
279
280     return 1;
281 }
282
283 int WPACKET_start_sub_packet(WPACKET *pkt)
284 {
285     return WPACKET_start_sub_packet_len__(pkt, 0);
286 }
287
288 int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
289 {
290     unsigned char *data;
291
292     /* Internal API, so should not fail */
293     assert(size <= sizeof(unsigned int));
294
295     if (size > sizeof(unsigned int)
296             || !WPACKET_allocate_bytes(pkt, size, &data)
297             || !put_value(data, val, size))
298         return 0;
299
300     return 1;
301 }
302
303 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
304 {
305     WPACKET_SUB *sub;
306     size_t lenbytes;
307
308     /* Internal API, so should not fail */
309     assert(pkt->subs != NULL);
310     if (pkt->subs == NULL)
311         return 0;
312
313     /* Find the WPACKET_SUB for the top level */
314     for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
315         continue;
316
317     lenbytes = sub->lenbytes;
318     if (lenbytes == 0)
319         lenbytes = sizeof(pkt->maxsize);
320
321     if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
322         return 0;
323
324     pkt->maxsize = maxsize;
325
326     return 1;
327 }
328
329 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
330 {
331     unsigned char *dest;
332
333     if (len == 0)
334         return 1;
335
336     if (!WPACKET_allocate_bytes(pkt, len, &dest))
337         return 0;
338
339     memcpy(dest, src, len);
340
341     return 1;
342 }
343
344 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
345                          size_t lenbytes)
346 {
347     if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
348             || !WPACKET_memcpy(pkt, src, len)
349             || !WPACKET_close(pkt))
350         return 0;
351
352     return 1;
353 }
354
355 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
356 {
357     /* Internal API, so should not fail */
358     assert(written != NULL);
359     if (written == NULL)
360         return 0;
361
362     *written = pkt->written;
363
364     return 1;
365 }
366
367 int WPACKET_get_length(WPACKET *pkt, size_t *len)
368 {
369     /* Internal API, so should not fail */
370     assert(pkt->subs != NULL && len != NULL);
371     if (pkt->subs == NULL || len == NULL)
372         return 0;
373
374     *len = pkt->written - pkt->subs->pwritten;
375
376     return 1;
377 }
378
379 void WPACKET_cleanup(WPACKET *pkt)
380 {
381     WPACKET_SUB *sub, *parent;
382
383     for (sub = pkt->subs; sub != NULL; sub = parent) {
384         parent = sub->parent;
385         OPENSSL_free(sub);
386     }
387     pkt->subs = NULL;
388 }