]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/packet.c
Make wpackettest conform to style rules
[thirdparty/openssl.git] / ssl / packet.c
CommitLineData
b7273855
MC
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
c39609aa 10#include <assert.h>
b7273855
MC
11#include "packet_locl.h"
12
871bc59b
MC
13#define DEFAULT_BUF_SIZE 256
14
0217dd19 15int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
b7273855 16{
c39609aa
MC
17 /* Internal API, so should not fail */
18 assert(pkt->subs != NULL && len != 0);
0217dd19
MC
19 if (pkt->subs == NULL || len == 0)
20 return 0;
b7273855 21
6ae4f5e0 22 if (pkt->maxsize - pkt->written < len)
b7273855
MC
23 return 0;
24
0217dd19 25 if (pkt->buf->length - pkt->written < len) {
b7273855
MC
26 size_t newlen;
27
871bc59b 28 if (pkt->buf->length > SIZE_MAX / 2) {
b7273855 29 newlen = SIZE_MAX;
871bc59b 30 } else {
de451856
MC
31 newlen = (pkt->buf->length == 0) ? DEFAULT_BUF_SIZE
32 : pkt->buf->length * 2;
871bc59b 33 }
0217dd19
MC
34 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
35 return 0;
b7273855 36 }
de451856 37 *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;
c0f9e23c 38 pkt->written += len;
0217dd19 39 pkt->curr += len;
b7273855 40
0217dd19 41 return 1;
b7273855
MC
42}
43
b2b3024e
MC
44int WPACKET_sub_allocate_bytes(WPACKET *pkt, size_t len,
45 unsigned char **allocbytes, size_t lenbytes)
46{
47 if (!WPACKET_start_sub_packet_len(pkt, lenbytes)
48 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
49 || !WPACKET_close(pkt))
50 return 0;
51
52 return 1;
53}
54
871bc59b
MC
55static size_t maxmaxsize(size_t lenbytes)
56{
57 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
58 return SIZE_MAX;
df065a2b
MC
59
60 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
871bc59b
MC
61}
62
ae2f7b37 63int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
b7273855 64{
de451856
MC
65 unsigned char *lenchars;
66
c39609aa
MC
67 /* Internal API, so should not fail */
68 assert(buf != NULL);
b7273855
MC
69 if (buf == NULL)
70 return 0;
71
0217dd19 72 pkt->buf = buf;
de451856 73 pkt->curr = 0;
0217dd19 74 pkt->written = 0;
871bc59b 75 pkt->maxsize = maxmaxsize(lenbytes);
b7273855 76
0217dd19
MC
77 pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));
78 if (pkt->subs == NULL)
79 return 0;
b7273855 80
0217dd19 81 if (lenbytes == 0)
b7273855 82 return 1;
b7273855 83
0217dd19
MC
84 pkt->subs->pwritten = lenbytes;
85 pkt->subs->lenbytes = lenbytes;
86
de451856 87 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
0217dd19
MC
88 OPENSSL_free(pkt->subs);
89 pkt->subs = NULL;
b7273855
MC
90 return 0;
91 }
de451856 92 pkt->subs->packet_len = lenchars - (unsigned char *)pkt->buf->data;
b7273855
MC
93
94 return 1;
95}
96
ae2f7b37 97int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
b7273855 98{
ae2f7b37 99 return WPACKET_init_len(pkt, buf, 0);
b7273855
MC
100}
101
ae2f7b37 102int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
b7273855 103{
c39609aa
MC
104 /* Internal API, so should not fail */
105 assert(pkt->subs != NULL);
0217dd19
MC
106 if (pkt->subs == NULL)
107 return 0;
108
109 pkt->subs->flags = flags;
b7273855
MC
110
111 return 1;
112}
113
c0f9e23c
MC
114/* Store the |value| of length |len| at location |data| */
115static int put_value(unsigned char *data, size_t value, size_t len)
82657355 116{
c0f9e23c 117 for (data += len - 1; len > 0; len--) {
82657355
MC
118 *data = (unsigned char)(value & 0xff);
119 data--;
120 value >>= 8;
121 }
122
123 /* Check whether we could fit the value in the assigned number of bytes */
124 if (value > 0)
125 return 0;
126
127 return 1;
128}
129
0217dd19 130
b7273855 131/*
0217dd19
MC
132 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
133 * close a sub-packet and write out its length if necessary.
b7273855 134 */
0217dd19 135static int wpacket_intern_close(WPACKET *pkt)
b7273855 136{
0217dd19 137 WPACKET_SUB *sub = pkt->subs;
de451856 138 size_t packlen = pkt->written - sub->pwritten;
b7273855 139
0217dd19 140 if (packlen == 0
c0f9e23c 141 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
b7273855
MC
142 return 0;
143
144 if (packlen == 0
de451856 145 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
ae2f7b37 146 /* Deallocate any bytes allocated for the length of the WPACKET */
0217dd19
MC
147 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
148 pkt->written -= sub->lenbytes;
149 pkt->curr -= sub->lenbytes;
b7273855
MC
150 }
151
152 /* Don't write out the packet length */
de451856
MC
153 sub->packet_len = 0;
154 sub->lenbytes = 0;
b7273855
MC
155 }
156
ae2f7b37 157 /* Write out the WPACKET length if needed */
82657355
MC
158 if (sub->lenbytes > 0
159 && !put_value((unsigned char *)&pkt->buf->data[sub->packet_len],
160 packlen, sub->lenbytes))
b7273855 161 return 0;
b7273855 162
0217dd19
MC
163 pkt->subs = sub->parent;
164 OPENSSL_free(sub);
b7273855
MC
165
166 return 1;
167}
168
0217dd19 169int WPACKET_close(WPACKET *pkt)
b7273855 170{
c39609aa
MC
171 /*
172 * Internal API, so should not fail - but we do negative testing of this
173 * so no assert (otherwise the tests fail)
174 */
0217dd19 175 if (pkt->subs == NULL || pkt->subs->parent == NULL)
b7273855
MC
176 return 0;
177
0217dd19 178 return wpacket_intern_close(pkt);
b7273855
MC
179}
180
0217dd19 181int WPACKET_finish(WPACKET *pkt)
b7273855 182{
0217dd19
MC
183 int ret;
184
c39609aa
MC
185 /*
186 * Internal API, so should not fail - but we do negative testing of this
187 * so no assert (otherwise the tests fail)
188 */
0217dd19
MC
189 if (pkt->subs == NULL || pkt->subs->parent != NULL)
190 return 0;
191
192 ret = wpacket_intern_close(pkt);
871bc59b
MC
193 if (ret) {
194 OPENSSL_free(pkt->subs);
195 pkt->subs = NULL;
196 }
de451856 197
0217dd19 198 return ret;
b7273855
MC
199}
200
0217dd19 201int WPACKET_start_sub_packet_len(WPACKET *pkt, size_t lenbytes)
b7273855 202{
0217dd19 203 WPACKET_SUB *sub;
de451856 204 unsigned char *lenchars;
b7273855 205
c39609aa
MC
206 /* Internal API, so should not fail */
207 assert(pkt->subs != NULL);
0217dd19 208 if (pkt->subs == NULL)
b7273855
MC
209 return 0;
210
0217dd19
MC
211 sub = OPENSSL_zalloc(sizeof(*sub));
212 if (sub == NULL)
b7273855
MC
213 return 0;
214
0217dd19
MC
215 sub->parent = pkt->subs;
216 pkt->subs = sub;
217 sub->pwritten = pkt->written + lenbytes;
218 sub->lenbytes = lenbytes;
219
220 if (lenbytes == 0) {
de451856 221 sub->packet_len = 0;
0217dd19
MC
222 return 1;
223 }
224
de451856 225 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
0217dd19 226 return 0;
de451856 227 sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;
b7273855
MC
228
229 return 1;
230}
231
0217dd19
MC
232int WPACKET_start_sub_packet(WPACKET *pkt)
233{
234 return WPACKET_start_sub_packet_len(pkt, 0);
235}
236
de451856 237int WPACKET_put_bytes(WPACKET *pkt, unsigned int val, size_t size)
b7273855
MC
238{
239 unsigned char *data;
240
c39609aa
MC
241 /* Internal API, so should not fail */
242 assert(size <= sizeof(unsigned int));
c0f9e23c 243
de451856 244 if (size > sizeof(unsigned int)
82657355
MC
245 || !WPACKET_allocate_bytes(pkt, size, &data)
246 || !put_value(data, val, size))
b7273855
MC
247 return 0;
248
249 return 1;
250}
251
ae2f7b37 252int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
b7273855 253{
871bc59b
MC
254 WPACKET_SUB *sub;
255 size_t lenbytes;
256
c39609aa
MC
257 /* Internal API, so should not fail */
258 assert(pkt->subs != NULL);
871bc59b
MC
259 if (pkt->subs == NULL)
260 return 0;
261
262 /* Find the WPACKET_SUB for the top level */
de451856
MC
263 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
264 continue;
871bc59b
MC
265
266 lenbytes = sub->lenbytes;
267 if (lenbytes == 0)
268 lenbytes = sizeof(pkt->maxsize);
269
270 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
271 return 0;
272
0217dd19 273 pkt->maxsize = maxsize;
b7273855
MC
274
275 return 1;
276}
277
ae2f7b37 278int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
b7273855
MC
279{
280 unsigned char *dest;
281
282 if (len == 0)
283 return 1;
284
ae2f7b37 285 if (!WPACKET_allocate_bytes(pkt, len, &dest))
b7273855
MC
286 return 0;
287
288 memcpy(dest, src, len);
289
290 return 1;
291}
292
fb790f16
MC
293int WPACKET_sub_memcpy(WPACKET *pkt, const void *src, size_t len, size_t lenbytes)
294{
295 if (!WPACKET_start_sub_packet_len(pkt, lenbytes)
296 || !WPACKET_memcpy(pkt, src, len)
297 || !WPACKET_close(pkt))
298 return 0;
299
300 return 1;
301}
302
ae2f7b37 303int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
b7273855 304{
c39609aa
MC
305 /* Internal API, so should not fail */
306 assert(written != NULL);
871bc59b 307 if (written == NULL)
b7273855
MC
308 return 0;
309
0217dd19 310 *written = pkt->written;
b7273855
MC
311
312 return 1;
313}
314
ae2f7b37 315int WPACKET_get_length(WPACKET *pkt, size_t *len)
b7273855 316{
c39609aa
MC
317 /* Internal API, so should not fail */
318 assert(pkt->subs != NULL && len != NULL);
0217dd19 319 if (pkt->subs == NULL || len == NULL)
b7273855
MC
320 return 0;
321
0217dd19 322 *len = pkt->written - pkt->subs->pwritten;
b7273855
MC
323
324 return 1;
325}
871bc59b 326
871bc59b
MC
327void WPACKET_cleanup(WPACKET *pkt)
328{
329 WPACKET_SUB *sub, *parent;
330
331 for (sub = pkt->subs; sub != NULL; sub = parent) {
332 parent = sub->parent;
333 OPENSSL_free(sub);
334 }
335 pkt->subs = NULL;
336}