1/*
2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 "internal/cryptlib.h"
11#include "internal/packet.h"
12#include <openssl/sslerr.h>
13
14#define DEFAULT_BUF_SIZE 256
15
16int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
17{
18 if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
19 return 0;
20
21 pkt->written += len;
22 pkt->curr += len;
23 return 1;
24}
25
26int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
27 unsigned char **allocbytes, size_t lenbytes)
28{
29 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
30 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
31 || !WPACKET_close(pkt))
32 return 0;
33
34 return 1;
35}
36
37#define GETBUF(p) (((p)->staticbuf != NULL) \
38 ? (p)->staticbuf \
39 : ((p)->buf != NULL \
40 ? (unsigned char *)(p)->buf->data \
41 : NULL))
42
43int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
44{
45 /* Internal API, so should not fail */
46 if (!ossl_assert(pkt->subs != NULL && len != 0))
47 return 0;
48
49 if (pkt->maxsize - pkt->written < len)
50 return 0;
51
52 if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
53 size_t newlen;
54 size_t reflen;
55
56 reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
57
58 if (reflen > SIZE_MAX / 2) {
59 newlen = SIZE_MAX;
60 } else {
61 newlen = reflen * 2;
62 if (newlen < DEFAULT_BUF_SIZE)
63 newlen = DEFAULT_BUF_SIZE;
64 }
65 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
66 return 0;
67 }
68 if (allocbytes != NULL)
69 *allocbytes = WPACKET_get_curr(pkt);
70
71 return 1;
72}
73
74int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
75 unsigned char **allocbytes, size_t lenbytes)
76{
77 if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
78 return 0;
79
80 if (*allocbytes != NULL)
81 *allocbytes += lenbytes;
82
83 return 1;
84}
85
86static size_t maxmaxsize(size_t lenbytes)
87{
88 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
89 return SIZE_MAX;
90
91 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
92}
93
94static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
95{
96 unsigned char *lenchars;
97
98 pkt->curr = 0;
99 pkt->written = 0;
100
101 if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL) {
102 SSLerr(SSL_F_WPACKET_INTERN_INIT_LEN, ERR_R_MALLOC_FAILURE);
103 return 0;
104 }
105
106 if (lenbytes == 0)
107 return 1;
108
109 pkt->subs->pwritten = lenbytes;
110 pkt->subs->lenbytes = lenbytes;
111
112 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
113 OPENSSL_free(pkt->subs);
114 pkt->subs = NULL;
115 return 0;
116 }
117 pkt->subs->packet_len = 0;
118
119 return 1;
120}
121
122int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
123 size_t lenbytes)
124{
125 size_t max = maxmaxsize(lenbytes);
126
127 /* Internal API, so should not fail */
128 if (!ossl_assert(buf != NULL && len > 0))
129 return 0;
130
131 pkt->staticbuf = buf;
132 pkt->buf = NULL;
133 pkt->maxsize = (max < len) ? max : len;
134
135 return wpacket_intern_init_len(pkt, lenbytes);
136}
137
138int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
139{
140 /* Internal API, so should not fail */
141 if (!ossl_assert(buf != NULL))
142 return 0;
143
144 pkt->staticbuf = NULL;
145 pkt->buf = buf;
146 pkt->maxsize = maxmaxsize(lenbytes);
147
148 return wpacket_intern_init_len(pkt, lenbytes);
149}
150
151int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
152{
153 return WPACKET_init_len(pkt, buf, 0);
154}
155
156int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
157{
158 pkt->staticbuf = NULL;
159 pkt->buf = NULL;
160 pkt->maxsize = maxmaxsize(lenbytes);
161
162 return wpacket_intern_init_len(pkt, 0);
163}
164
165int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
166{
167 /* Internal API, so should not fail */
168 if (!ossl_assert(pkt->subs != NULL))
169 return 0;
170
171 pkt->subs->flags = flags;
172
173 return 1;
174}
175
176/* Store the |value| of length |len| at location |data| */
177static int put_value(unsigned char *data, size_t value, size_t len)
178{
179 if (data == NULL)
180 return 1;
181
182 for (data += len - 1; len > 0; len--) {
183 *data = (unsigned char)(value & 0xff);
184 data--;
185 value >>= 8;
186 }
187
188 /* Check whether we could fit the value in the assigned number of bytes */
189 if (value > 0)
190 return 0;
191
192 return 1;
193}
194
195
196/*
197 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
198 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
199 * necessary. If |doclose| is 0 then it goes through the motions of closing
200 * (i.e. it fills in all the lengths), but doesn't actually close anything.
201 */
202static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
203{
204 size_t packlen = pkt->written - sub->pwritten;
205
206 if (packlen == 0
207 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
208 return 0;
209
210 if (packlen == 0
211 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
212 /* We can't handle this case. Return an error */
213 if (!doclose)
214 return 0;
215
216 /* Deallocate any bytes allocated for the length of the WPACKET */
217 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
218 pkt->written -= sub->lenbytes;
219 pkt->curr -= sub->lenbytes;
220 }
221
222 /* Don't write out the packet length */
223 sub->packet_len = 0;
224 sub->lenbytes = 0;
225 }
226
227 /* Write out the WPACKET length if needed */
228 if (sub->lenbytes > 0) {
229 unsigned char *buf = GETBUF(pkt);
230
231 if (buf != NULL
232 && !put_value(&buf[sub->packet_len], packlen,
233 sub->lenbytes))
234 return 0;
235 }
236
237 if (doclose) {
238 pkt->subs = sub->parent;
239 OPENSSL_free(sub);
240 }
241
242 return 1;
243}
244
245int WPACKET_fill_lengths(WPACKET *pkt)
246{
247 WPACKET_SUB *sub;
248
249 if (!ossl_assert(pkt->subs != NULL))
250 return 0;
251
252 for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
253 if (!wpacket_intern_close(pkt, sub, 0))
254 return 0;
255 }
256
257 return 1;
258}
259
260int WPACKET_close(WPACKET *pkt)
261{
262 /*
263 * Internal API, so should not fail - but we do negative testing of this
264 * so no assert (otherwise the tests fail)
265 */
266 if (pkt->subs == NULL || pkt->subs->parent == NULL)
267 return 0;
268
269 return wpacket_intern_close(pkt, pkt->subs, 1);
270}
271
272int WPACKET_finish(WPACKET *pkt)
273{
274 int ret;
275
276 /*
277 * Internal API, so should not fail - but we do negative testing of this
278 * so no assert (otherwise the tests fail)
279 */
280 if (pkt->subs == NULL || pkt->subs->parent != NULL)
281 return 0;
282
283 ret = wpacket_intern_close(pkt, pkt->subs, 1);
284 if (ret) {
285 OPENSSL_free(pkt->subs);
286 pkt->subs = NULL;
287 }
288
289 return ret;
290}
291
292int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
293{
294 WPACKET_SUB *sub;
295 unsigned char *lenchars;
296
297 /* Internal API, so should not fail */
298 if (!ossl_assert(pkt->subs != NULL))
299 return 0;
300
301 if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
302 SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);
303 return 0;
304 }
305
306 sub->parent = pkt->subs;
307 pkt->subs = sub;
308 sub->pwritten = pkt->written + lenbytes;
309 sub->lenbytes = lenbytes;
310
311 if (lenbytes == 0) {
312 sub->packet_len = 0;
313 return 1;
314 }
315
316 sub->packet_len = pkt->written;
317
318 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
319 return 0;
320
321 return 1;
322}
323
324int WPACKET_start_sub_packet(WPACKET *pkt)
325{
326 return WPACKET_start_sub_packet_len__(pkt, 0);
327}
328
329int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
330{
331 unsigned char *data;
332
333 /* Internal API, so should not fail */
334 if (!ossl_assert(size <= sizeof(unsigned int))
335 || !WPACKET_allocate_bytes(pkt, size, &data)
336 || !put_value(data, val, size))
337 return 0;
338
339 return 1;
340}
341
342int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
343{
344 WPACKET_SUB *sub;
345 size_t lenbytes;
346
347 /* Internal API, so should not fail */
348 if (!ossl_assert(pkt->subs != NULL))
349 return 0;
350
351 /* Find the WPACKET_SUB for the top level */
352 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
353 continue;
354
355 lenbytes = sub->lenbytes;
356 if (lenbytes == 0)
357 lenbytes = sizeof(pkt->maxsize);
358
359 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
360 return 0;
361
362 pkt->maxsize = maxsize;
363
364 return 1;
365}
366
367int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
368{
369 unsigned char *dest;
370
371 if (len == 0)
372 return 1;
373
374 if (!WPACKET_allocate_bytes(pkt, len, &dest))
375 return 0;
376
377 if (dest != NULL)
378 memset(dest, ch, len);
379
380 return 1;
381}
382
383int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
384{
385 unsigned char *dest;
386
387 if (len == 0)
388 return 1;
389
390 if (!WPACKET_allocate_bytes(pkt, len, &dest))
391 return 0;
392
393 if (dest != NULL)
394 memcpy(dest, src, len);
395
396 return 1;
397}
398
399int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
400 size_t lenbytes)
401{
402 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
403 || !WPACKET_memcpy(pkt, src, len)
404 || !WPACKET_close(pkt))
405 return 0;
406
407 return 1;
408}
409
410int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
411{
412 /* Internal API, so should not fail */
413 if (!ossl_assert(written != NULL))
414 return 0;
415
416 *written = pkt->written;
417
418 return 1;
419}
420
421int WPACKET_get_length(WPACKET *pkt, size_t *len)
422{
423 /* Internal API, so should not fail */
424 if (!ossl_assert(pkt->subs != NULL && len != NULL))
425 return 0;
426
427 *len = pkt->written - pkt->subs->pwritten;
428
429 return 1;
430}
431
432unsigned char *WPACKET_get_curr(WPACKET *pkt)
433{
434 unsigned char *buf = GETBUF(pkt);
435
436 if (buf == NULL)
437 return NULL;
438
439 return buf + pkt->curr;
440}
441
442int WPACKET_is_null_buf(WPACKET *pkt)
443{
444 return pkt->buf == NULL && pkt->staticbuf == NULL;
445}
446
447void WPACKET_cleanup(WPACKET *pkt)
448{
449 WPACKET_SUB *sub, *parent;
450
451 for (sub = pkt->subs; sub != NULL; sub = parent) {
452 parent = sub->parent;
453 OPENSSL_free(sub);
454 }
455 pkt->subs = NULL;
456}
457