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() and WPACKET_finish() to
184 * close a sub-packet and write out its length if necessary.
186 static int wpacket_intern_close(WPACKET
*pkt
)
188 WPACKET_SUB
*sub
= pkt
->subs
;
189 size_t packlen
= pkt
->written
- sub
->pwritten
;
192 && (sub
->flags
& WPACKET_FLAGS_NON_ZERO_LENGTH
) != 0)
196 && sub
->flags
& WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
) {
197 /* Deallocate any bytes allocated for the length of the WPACKET */
198 if ((pkt
->curr
- sub
->lenbytes
) == sub
->packet_len
) {
199 pkt
->written
-= sub
->lenbytes
;
200 pkt
->curr
-= sub
->lenbytes
;
203 /* Don't write out the packet length */
208 /* Write out the WPACKET length if needed */
209 if (sub
->lenbytes
> 0
210 && !put_value(&GETBUF(pkt
)[sub
->packet_len
], packlen
,
214 pkt
->subs
= sub
->parent
;
220 int WPACKET_close(WPACKET
*pkt
)
223 * Internal API, so should not fail - but we do negative testing of this
224 * so no assert (otherwise the tests fail)
226 if (pkt
->subs
== NULL
|| pkt
->subs
->parent
== NULL
)
229 return wpacket_intern_close(pkt
);
232 int WPACKET_finish(WPACKET
*pkt
)
237 * Internal API, so should not fail - but we do negative testing of this
238 * so no assert (otherwise the tests fail)
240 if (pkt
->subs
== NULL
|| pkt
->subs
->parent
!= NULL
)
243 ret
= wpacket_intern_close(pkt
);
245 OPENSSL_free(pkt
->subs
);
252 int WPACKET_start_sub_packet_len__(WPACKET
*pkt
, size_t lenbytes
)
255 unsigned char *lenchars
;
257 /* Internal API, so should not fail */
258 assert(pkt
->subs
!= NULL
);
259 if (pkt
->subs
== NULL
)
262 sub
= OPENSSL_zalloc(sizeof(*sub
));
266 sub
->parent
= pkt
->subs
;
268 sub
->pwritten
= pkt
->written
+ lenbytes
;
269 sub
->lenbytes
= lenbytes
;
276 if (!WPACKET_allocate_bytes(pkt
, lenbytes
, &lenchars
))
278 /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
279 sub
->packet_len
= lenchars
- GETBUF(pkt
);
284 int WPACKET_start_sub_packet(WPACKET
*pkt
)
286 return WPACKET_start_sub_packet_len__(pkt
, 0);
289 int WPACKET_put_bytes__(WPACKET
*pkt
, unsigned int val
, size_t size
)
293 /* Internal API, so should not fail */
294 assert(size
<= sizeof(unsigned int));
296 if (size
> sizeof(unsigned int)
297 || !WPACKET_allocate_bytes(pkt
, size
, &data
)
298 || !put_value(data
, val
, size
))
304 int WPACKET_set_max_size(WPACKET
*pkt
, size_t maxsize
)
309 /* Internal API, so should not fail */
310 assert(pkt
->subs
!= NULL
);
311 if (pkt
->subs
== NULL
)
314 /* Find the WPACKET_SUB for the top level */
315 for (sub
= pkt
->subs
; sub
->parent
!= NULL
; sub
= sub
->parent
)
318 lenbytes
= sub
->lenbytes
;
320 lenbytes
= sizeof(pkt
->maxsize
);
322 if (maxmaxsize(lenbytes
) < maxsize
|| maxsize
< pkt
->written
)
325 pkt
->maxsize
= maxsize
;
330 int WPACKET_memcpy(WPACKET
*pkt
, const void *src
, size_t len
)
337 if (!WPACKET_allocate_bytes(pkt
, len
, &dest
))
340 memcpy(dest
, src
, len
);
345 int WPACKET_sub_memcpy__(WPACKET
*pkt
, const void *src
, size_t len
,
348 if (!WPACKET_start_sub_packet_len__(pkt
, lenbytes
)
349 || !WPACKET_memcpy(pkt
, src
, len
)
350 || !WPACKET_close(pkt
))
356 int WPACKET_get_total_written(WPACKET
*pkt
, size_t *written
)
358 /* Internal API, so should not fail */
359 assert(written
!= NULL
);
363 *written
= pkt
->written
;
368 int WPACKET_get_length(WPACKET
*pkt
, size_t *len
)
370 /* Internal API, so should not fail */
371 assert(pkt
->subs
!= NULL
&& len
!= NULL
);
372 if (pkt
->subs
== NULL
|| len
== NULL
)
375 *len
= pkt
->written
- pkt
->subs
->pwritten
;
380 unsigned char *WPACKET_get_curr(WPACKET
*pkt
)
382 return GETBUF(pkt
) + pkt
->curr
;
385 void WPACKET_cleanup(WPACKET
*pkt
)
387 WPACKET_SUB
*sub
, *parent
;
389 for (sub
= pkt
->subs
; sub
!= NULL
; sub
= parent
) {
390 parent
= sub
->parent
;