Make SSL_write_early_finish() an internal only function
[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     for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
236         if (!wpacket_intern_close(pkt, sub, 0))
237             return 0;
238     }
239
240     return 1;
241 }
242
243 int WPACKET_close(WPACKET *pkt)
244 {
245     /*
246      * Internal API, so should not fail - but we do negative testing of this
247      * so no assert (otherwise the tests fail)
248      */
249     if (pkt->subs == NULL || pkt->subs->parent == NULL)
250         return 0;
251
252     return wpacket_intern_close(pkt, pkt->subs, 1);
253 }
254
255 int WPACKET_finish(WPACKET *pkt)
256 {
257     int ret;
258
259     /*
260      * Internal API, so should not fail - but we do negative testing of this
261      * so no assert (otherwise the tests fail)
262      */
263     if (pkt->subs == NULL || pkt->subs->parent != NULL)
264         return 0;
265
266     ret = wpacket_intern_close(pkt, pkt->subs, 1);
267     if (ret) {
268         OPENSSL_free(pkt->subs);
269         pkt->subs = NULL;
270     }
271
272     return ret;
273 }
274
275 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
276 {
277     WPACKET_SUB *sub;
278     unsigned char *lenchars;
279
280     /* Internal API, so should not fail */
281     assert(pkt->subs != NULL);
282     if (pkt->subs == NULL)
283         return 0;
284
285     sub = OPENSSL_zalloc(sizeof(*sub));
286     if (sub == NULL)
287         return 0;
288
289     sub->parent = pkt->subs;
290     pkt->subs = sub;
291     sub->pwritten = pkt->written + lenbytes;
292     sub->lenbytes = lenbytes;
293
294     if (lenbytes == 0) {
295         sub->packet_len = 0;
296         return 1;
297     }
298
299     if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
300         return 0;
301     /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
302     sub->packet_len = lenchars - GETBUF(pkt);
303
304     return 1;
305 }
306
307 int WPACKET_start_sub_packet(WPACKET *pkt)
308 {
309     return WPACKET_start_sub_packet_len__(pkt, 0);
310 }
311
312 int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
313 {
314     unsigned char *data;
315
316     /* Internal API, so should not fail */
317     assert(size <= sizeof(unsigned int));
318
319     if (size > sizeof(unsigned int)
320             || !WPACKET_allocate_bytes(pkt, size, &data)
321             || !put_value(data, val, size))
322         return 0;
323
324     return 1;
325 }
326
327 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
328 {
329     WPACKET_SUB *sub;
330     size_t lenbytes;
331
332     /* Internal API, so should not fail */
333     assert(pkt->subs != NULL);
334     if (pkt->subs == NULL)
335         return 0;
336
337     /* Find the WPACKET_SUB for the top level */
338     for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
339         continue;
340
341     lenbytes = sub->lenbytes;
342     if (lenbytes == 0)
343         lenbytes = sizeof(pkt->maxsize);
344
345     if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
346         return 0;
347
348     pkt->maxsize = maxsize;
349
350     return 1;
351 }
352
353 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
354 {
355     unsigned char *dest;
356
357     if (len == 0)
358         return 1;
359
360     if (!WPACKET_allocate_bytes(pkt, len, &dest))
361         return 0;
362
363     memcpy(dest, src, len);
364
365     return 1;
366 }
367
368 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
369                          size_t lenbytes)
370 {
371     if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
372             || !WPACKET_memcpy(pkt, src, len)
373             || !WPACKET_close(pkt))
374         return 0;
375
376     return 1;
377 }
378
379 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
380 {
381     /* Internal API, so should not fail */
382     assert(written != NULL);
383     if (written == NULL)
384         return 0;
385
386     *written = pkt->written;
387
388     return 1;
389 }
390
391 int WPACKET_get_length(WPACKET *pkt, size_t *len)
392 {
393     /* Internal API, so should not fail */
394     assert(pkt->subs != NULL && len != NULL);
395     if (pkt->subs == NULL || len == NULL)
396         return 0;
397
398     *len = pkt->written - pkt->subs->pwritten;
399
400     return 1;
401 }
402
403 unsigned char *WPACKET_get_curr(WPACKET *pkt)
404 {
405     return GETBUF(pkt) + pkt->curr;
406 }
407
408 void WPACKET_cleanup(WPACKET *pkt)
409 {
410     WPACKET_SUB *sub, *parent;
411
412     for (sub = pkt->subs; sub != NULL; sub = parent) {
413         parent = sub->parent;
414         OPENSSL_free(sub);
415     }
416     pkt->subs = NULL;
417 }