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 if (!ossl_assert(pkt
->subs
!= NULL
&& len
!= 0))
45 if (pkt
->maxsize
- pkt
->written
< len
)
48 if (pkt
->staticbuf
== NULL
&& (pkt
->buf
->length
- pkt
->written
< len
)) {
52 reflen
= (len
> pkt
->buf
->length
) ? len
: pkt
->buf
->length
;
54 if (reflen
> SIZE_MAX
/ 2) {
58 if (newlen
< DEFAULT_BUF_SIZE
)
59 newlen
= DEFAULT_BUF_SIZE
;
61 if (BUF_MEM_grow(pkt
->buf
, newlen
) == 0)
64 if (allocbytes
!= NULL
)
65 *allocbytes
= WPACKET_get_curr(pkt
);
70 int WPACKET_sub_reserve_bytes__(WPACKET
*pkt
, size_t len
,
71 unsigned char **allocbytes
, size_t lenbytes
)
73 if (!WPACKET_reserve_bytes(pkt
, lenbytes
+ len
, allocbytes
))
76 *allocbytes
+= lenbytes
;
81 static size_t maxmaxsize(size_t lenbytes
)
83 if (lenbytes
>= sizeof(size_t) || lenbytes
== 0)
86 return ((size_t)1 << (lenbytes
* 8)) - 1 + lenbytes
;
89 static int wpacket_intern_init_len(WPACKET
*pkt
, size_t lenbytes
)
91 unsigned char *lenchars
;
96 pkt
->subs
= OPENSSL_zalloc(sizeof(*pkt
->subs
));
97 if (pkt
->subs
== NULL
)
103 pkt
->subs
->pwritten
= lenbytes
;
104 pkt
->subs
->lenbytes
= lenbytes
;
106 if (!WPACKET_allocate_bytes(pkt
, lenbytes
, &lenchars
)) {
107 OPENSSL_free(pkt
->subs
);
111 pkt
->subs
->packet_len
= lenchars
- GETBUF(pkt
);
116 int WPACKET_init_static_len(WPACKET
*pkt
, unsigned char *buf
, size_t len
,
119 size_t max
= maxmaxsize(lenbytes
);
121 /* Internal API, so should not fail */
122 if (!ossl_assert(buf
!= NULL
&& len
> 0))
125 pkt
->staticbuf
= buf
;
127 pkt
->maxsize
= (max
< len
) ? max
: len
;
129 return wpacket_intern_init_len(pkt
, lenbytes
);
132 int WPACKET_init_len(WPACKET
*pkt
, BUF_MEM
*buf
, size_t lenbytes
)
134 /* Internal API, so should not fail */
135 if (!ossl_assert(buf
!= NULL
))
138 pkt
->staticbuf
= NULL
;
140 pkt
->maxsize
= maxmaxsize(lenbytes
);
142 return wpacket_intern_init_len(pkt
, lenbytes
);
145 int WPACKET_init(WPACKET
*pkt
, BUF_MEM
*buf
)
147 return WPACKET_init_len(pkt
, buf
, 0);
150 int WPACKET_set_flags(WPACKET
*pkt
, unsigned int flags
)
152 /* Internal API, so should not fail */
153 if (!ossl_assert(pkt
->subs
!= NULL
))
156 pkt
->subs
->flags
= flags
;
161 /* Store the |value| of length |len| at location |data| */
162 static int put_value(unsigned char *data
, size_t value
, size_t len
)
164 for (data
+= len
- 1; len
> 0; len
--) {
165 *data
= (unsigned char)(value
& 0xff);
170 /* Check whether we could fit the value in the assigned number of bytes */
179 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
180 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
181 * necessary. If |doclose| is 0 then it goes through the motions of closing
182 * (i.e. it fills in all the lengths), but doesn't actually close anything.
184 static int wpacket_intern_close(WPACKET
*pkt
, WPACKET_SUB
*sub
, int doclose
)
186 size_t packlen
= pkt
->written
- sub
->pwritten
;
189 && (sub
->flags
& WPACKET_FLAGS_NON_ZERO_LENGTH
) != 0)
193 && sub
->flags
& WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
) {
194 /* We can't handle this case. Return an error */
198 /* Deallocate any bytes allocated for the length of the WPACKET */
199 if ((pkt
->curr
- sub
->lenbytes
) == sub
->packet_len
) {
200 pkt
->written
-= sub
->lenbytes
;
201 pkt
->curr
-= sub
->lenbytes
;
204 /* Don't write out the packet length */
209 /* Write out the WPACKET length if needed */
210 if (sub
->lenbytes
> 0
211 && !put_value(&GETBUF(pkt
)[sub
->packet_len
], packlen
,
216 pkt
->subs
= sub
->parent
;
223 int WPACKET_fill_lengths(WPACKET
*pkt
)
227 if (!ossl_assert(pkt
->subs
!= NULL
))
230 for (sub
= pkt
->subs
; sub
!= NULL
; sub
= sub
->parent
) {
231 if (!wpacket_intern_close(pkt
, sub
, 0))
238 int WPACKET_close(WPACKET
*pkt
)
241 * Internal API, so should not fail - but we do negative testing of this
242 * so no assert (otherwise the tests fail)
244 if (pkt
->subs
== NULL
|| pkt
->subs
->parent
== NULL
)
247 return wpacket_intern_close(pkt
, pkt
->subs
, 1);
250 int WPACKET_finish(WPACKET
*pkt
)
255 * Internal API, so should not fail - but we do negative testing of this
256 * so no assert (otherwise the tests fail)
258 if (pkt
->subs
== NULL
|| pkt
->subs
->parent
!= NULL
)
261 ret
= wpacket_intern_close(pkt
, pkt
->subs
, 1);
263 OPENSSL_free(pkt
->subs
);
270 int WPACKET_start_sub_packet_len__(WPACKET
*pkt
, size_t lenbytes
)
273 unsigned char *lenchars
;
275 /* Internal API, so should not fail */
276 if (!ossl_assert(pkt
->subs
!= NULL
))
279 sub
= OPENSSL_zalloc(sizeof(*sub
));
283 sub
->parent
= pkt
->subs
;
285 sub
->pwritten
= pkt
->written
+ lenbytes
;
286 sub
->lenbytes
= lenbytes
;
293 if (!WPACKET_allocate_bytes(pkt
, lenbytes
, &lenchars
))
295 /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
296 sub
->packet_len
= lenchars
- GETBUF(pkt
);
301 int WPACKET_start_sub_packet(WPACKET
*pkt
)
303 return WPACKET_start_sub_packet_len__(pkt
, 0);
306 int WPACKET_put_bytes__(WPACKET
*pkt
, unsigned int val
, size_t size
)
310 /* Internal API, so should not fail */
311 if (!ossl_assert(size
<= sizeof(unsigned int))
312 || !WPACKET_allocate_bytes(pkt
, size
, &data
)
313 || !put_value(data
, val
, size
))
319 int WPACKET_set_max_size(WPACKET
*pkt
, size_t maxsize
)
324 /* Internal API, so should not fail */
325 if (!ossl_assert(pkt
->subs
!= NULL
))
328 /* Find the WPACKET_SUB for the top level */
329 for (sub
= pkt
->subs
; sub
->parent
!= NULL
; sub
= sub
->parent
)
332 lenbytes
= sub
->lenbytes
;
334 lenbytes
= sizeof(pkt
->maxsize
);
336 if (maxmaxsize(lenbytes
) < maxsize
|| maxsize
< pkt
->written
)
339 pkt
->maxsize
= maxsize
;
344 int WPACKET_memset(WPACKET
*pkt
, int ch
, size_t len
)
351 if (!WPACKET_allocate_bytes(pkt
, len
, &dest
))
354 memset(dest
, ch
, len
);
359 int WPACKET_memcpy(WPACKET
*pkt
, const void *src
, size_t len
)
366 if (!WPACKET_allocate_bytes(pkt
, len
, &dest
))
369 memcpy(dest
, src
, len
);
374 int WPACKET_sub_memcpy__(WPACKET
*pkt
, const void *src
, size_t len
,
377 if (!WPACKET_start_sub_packet_len__(pkt
, lenbytes
)
378 || !WPACKET_memcpy(pkt
, src
, len
)
379 || !WPACKET_close(pkt
))
385 int WPACKET_get_total_written(WPACKET
*pkt
, size_t *written
)
387 /* Internal API, so should not fail */
388 if (!ossl_assert(written
!= NULL
))
391 *written
= pkt
->written
;
396 int WPACKET_get_length(WPACKET
*pkt
, size_t *len
)
398 /* Internal API, so should not fail */
399 if (!ossl_assert(pkt
->subs
!= NULL
&& len
!= NULL
))
402 *len
= pkt
->written
- pkt
->subs
->pwritten
;
407 unsigned char *WPACKET_get_curr(WPACKET
*pkt
)
409 return GETBUF(pkt
) + pkt
->curr
;
412 void WPACKET_cleanup(WPACKET
*pkt
)
414 WPACKET_SUB
*sub
, *parent
;
416 for (sub
= pkt
->subs
; sub
!= NULL
; sub
= parent
) {
417 parent
= sub
->parent
;