2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "packet_locl.h"
13 #define DEFAULT_BUF_SIZE 256
15 int WPACKET_allocate_bytes(WPACKET
*pkt
, size_t len
, unsigned char **allocbytes
)
17 if (!WPACKET_reserve_bytes(pkt
, len
, allocbytes
))
25 int WPACKET_sub_allocate_bytes__(WPACKET
*pkt
, size_t len
,
26 unsigned char **allocbytes
, size_t lenbytes
)
28 if (!WPACKET_start_sub_packet_len__(pkt
, lenbytes
)
29 || !WPACKET_allocate_bytes(pkt
, len
, allocbytes
)
30 || !WPACKET_close(pkt
))
36 #define GETBUF(p) (((p)->staticbuf != NULL) \
37 ? (p)->staticbuf : (unsigned char *)(p)->buf->data)
39 int WPACKET_reserve_bytes(WPACKET
*pkt
, size_t len
, unsigned char **allocbytes
)
41 /* Internal API, so should not fail */
42 assert(pkt
->subs
!= NULL
&& len
!= 0);
43 if (pkt
->subs
== NULL
|| len
== 0)
46 if (pkt
->maxsize
- pkt
->written
< len
)
49 if (pkt
->staticbuf
== NULL
&& (pkt
->buf
->length
- pkt
->written
< len
)) {
53 reflen
= (len
> pkt
->buf
->length
) ? len
: pkt
->buf
->length
;
55 if (reflen
> SIZE_MAX
/ 2) {
59 if (newlen
< DEFAULT_BUF_SIZE
)
60 newlen
= DEFAULT_BUF_SIZE
;
62 if (BUF_MEM_grow(pkt
->buf
, newlen
) == 0)
65 if (allocbytes
!= NULL
)
66 *allocbytes
= WPACKET_get_curr(pkt
);
71 int WPACKET_sub_reserve_bytes__(WPACKET
*pkt
, size_t len
,
72 unsigned char **allocbytes
, size_t lenbytes
)
74 if (!WPACKET_reserve_bytes(pkt
, lenbytes
+ len
, allocbytes
))
77 *allocbytes
+= lenbytes
;
82 static size_t maxmaxsize(size_t lenbytes
)
84 if (lenbytes
>= sizeof(size_t) || lenbytes
== 0)
87 return ((size_t)1 << (lenbytes
* 8)) - 1 + lenbytes
;
90 static int wpacket_intern_init_len(WPACKET
*pkt
, size_t lenbytes
)
92 unsigned char *lenchars
;
97 pkt
->subs
= OPENSSL_zalloc(sizeof(*pkt
->subs
));
98 if (pkt
->subs
== NULL
)
104 pkt
->subs
->pwritten
= lenbytes
;
105 pkt
->subs
->lenbytes
= lenbytes
;
107 if (!WPACKET_allocate_bytes(pkt
, lenbytes
, &lenchars
)) {
108 OPENSSL_free(pkt
->subs
);
112 pkt
->subs
->packet_len
= lenchars
- GETBUF(pkt
);
117 int WPACKET_init_static_len(WPACKET
*pkt
, unsigned char *buf
, size_t len
,
120 size_t max
= maxmaxsize(lenbytes
);
122 /* Internal API, so should not fail */
123 assert(buf
!= NULL
&& len
> 0);
124 if (buf
== NULL
|| len
== 0)
127 pkt
->staticbuf
= buf
;
129 pkt
->maxsize
= (max
< len
) ? max
: len
;
131 return wpacket_intern_init_len(pkt
, lenbytes
);
134 int WPACKET_init_len(WPACKET
*pkt
, BUF_MEM
*buf
, size_t lenbytes
)
136 /* Internal API, so should not fail */
141 pkt
->staticbuf
= NULL
;
143 pkt
->maxsize
= maxmaxsize(lenbytes
);
145 return wpacket_intern_init_len(pkt
, lenbytes
);
148 int WPACKET_init(WPACKET
*pkt
, BUF_MEM
*buf
)
150 return WPACKET_init_len(pkt
, buf
, 0);
153 int WPACKET_set_flags(WPACKET
*pkt
, unsigned int flags
)
155 /* Internal API, so should not fail */
156 assert(pkt
->subs
!= NULL
);
157 if (pkt
->subs
== NULL
)
160 pkt
->subs
->flags
= flags
;
165 /* Store the |value| of length |len| at location |data| */
166 static int put_value(unsigned char *data
, size_t value
, size_t len
)
168 for (data
+= len
- 1; len
> 0; len
--) {
169 *data
= (unsigned char)(value
& 0xff);
174 /* Check whether we could fit the value in the assigned number of bytes */
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.
188 static int wpacket_intern_close(WPACKET
*pkt
, WPACKET_SUB
*sub
, int doclose
)
190 size_t packlen
= pkt
->written
- sub
->pwritten
;
193 && (sub
->flags
& WPACKET_FLAGS_NON_ZERO_LENGTH
) != 0)
197 && sub
->flags
& WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
) {
198 /* We can't handle this case. Return an error */
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
;
208 /* Don't write out the packet length */
213 /* Write out the WPACKET length if needed */
214 if (sub
->lenbytes
> 0
215 && !put_value(&GETBUF(pkt
)[sub
->packet_len
], packlen
,
220 pkt
->subs
= sub
->parent
;
227 int WPACKET_fill_lengths(WPACKET
*pkt
)
231 assert(pkt
->subs
!= NULL
);
232 if (pkt
->subs
== NULL
)
235 for (sub
= pkt
->subs
; sub
!= NULL
; sub
= sub
->parent
) {
236 if (!wpacket_intern_close(pkt
, sub
, 0))
243 int WPACKET_close(WPACKET
*pkt
)
246 * Internal API, so should not fail - but we do negative testing of this
247 * so no assert (otherwise the tests fail)
249 if (pkt
->subs
== NULL
|| pkt
->subs
->parent
== NULL
)
252 return wpacket_intern_close(pkt
, pkt
->subs
, 1);
255 int WPACKET_finish(WPACKET
*pkt
)
260 * Internal API, so should not fail - but we do negative testing of this
261 * so no assert (otherwise the tests fail)
263 if (pkt
->subs
== NULL
|| pkt
->subs
->parent
!= NULL
)
266 ret
= wpacket_intern_close(pkt
, pkt
->subs
, 1);
268 OPENSSL_free(pkt
->subs
);
275 int WPACKET_start_sub_packet_len__(WPACKET
*pkt
, size_t lenbytes
)
278 unsigned char *lenchars
;
280 /* Internal API, so should not fail */
281 assert(pkt
->subs
!= NULL
);
282 if (pkt
->subs
== NULL
)
285 sub
= OPENSSL_zalloc(sizeof(*sub
));
289 sub
->parent
= pkt
->subs
;
291 sub
->pwritten
= pkt
->written
+ lenbytes
;
292 sub
->lenbytes
= lenbytes
;
299 if (!WPACKET_allocate_bytes(pkt
, lenbytes
, &lenchars
))
301 /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
302 sub
->packet_len
= lenchars
- GETBUF(pkt
);
307 int WPACKET_start_sub_packet(WPACKET
*pkt
)
309 return WPACKET_start_sub_packet_len__(pkt
, 0);
312 int WPACKET_put_bytes__(WPACKET
*pkt
, unsigned int val
, size_t size
)
316 /* Internal API, so should not fail */
317 assert(size
<= sizeof(unsigned int));
319 if (size
> sizeof(unsigned int)
320 || !WPACKET_allocate_bytes(pkt
, size
, &data
)
321 || !put_value(data
, val
, size
))
327 int WPACKET_set_max_size(WPACKET
*pkt
, size_t maxsize
)
332 /* Internal API, so should not fail */
333 assert(pkt
->subs
!= NULL
);
334 if (pkt
->subs
== NULL
)
337 /* Find the WPACKET_SUB for the top level */
338 for (sub
= pkt
->subs
; sub
->parent
!= NULL
; sub
= sub
->parent
)
341 lenbytes
= sub
->lenbytes
;
343 lenbytes
= sizeof(pkt
->maxsize
);
345 if (maxmaxsize(lenbytes
) < maxsize
|| maxsize
< pkt
->written
)
348 pkt
->maxsize
= maxsize
;
353 int WPACKET_memset(WPACKET
*pkt
, int ch
, size_t len
)
360 if (!WPACKET_allocate_bytes(pkt
, len
, &dest
))
363 memset(dest
, ch
, len
);
368 int WPACKET_memcpy(WPACKET
*pkt
, const void *src
, size_t len
)
375 if (!WPACKET_allocate_bytes(pkt
, len
, &dest
))
378 memcpy(dest
, src
, len
);
383 int WPACKET_sub_memcpy__(WPACKET
*pkt
, const void *src
, size_t len
,
386 if (!WPACKET_start_sub_packet_len__(pkt
, lenbytes
)
387 || !WPACKET_memcpy(pkt
, src
, len
)
388 || !WPACKET_close(pkt
))
394 int WPACKET_get_total_written(WPACKET
*pkt
, size_t *written
)
396 /* Internal API, so should not fail */
397 assert(written
!= NULL
);
401 *written
= pkt
->written
;
406 int WPACKET_get_length(WPACKET
*pkt
, size_t *len
)
408 /* Internal API, so should not fail */
409 assert(pkt
->subs
!= NULL
&& len
!= NULL
);
410 if (pkt
->subs
== NULL
|| len
== NULL
)
413 *len
= pkt
->written
- pkt
->subs
->pwritten
;
418 unsigned char *WPACKET_get_curr(WPACKET
*pkt
)
420 return GETBUF(pkt
) + pkt
->curr
;
423 void WPACKET_cleanup(WPACKET
*pkt
)
425 WPACKET_SUB
*sub
, *parent
;
427 for (sub
= pkt
->subs
; sub
!= NULL
; sub
= parent
) {
428 parent
= sub
->parent
;