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
10 #include "packet_locl.h"
12 #define DEFAULT_BUF_SIZE 256
15 * Allocate bytes in the WPACKET for the output. This reserves the bytes
16 * and count them as "written", but doesn't actually do the writing.
18 int WPACKET_allocate_bytes(WPACKET
*pkt
, size_t len
, unsigned char **allocbytes
)
20 if (pkt
->subs
== NULL
|| len
== 0)
23 if (SIZE_MAX
- pkt
->written
< len
)
26 if (pkt
->written
+ len
> pkt
->maxsize
)
29 if (pkt
->buf
->length
- pkt
->written
< len
) {
32 if (pkt
->buf
->length
> SIZE_MAX
/ 2) {
35 if (pkt
->buf
->length
== 0)
36 newlen
= DEFAULT_BUF_SIZE
;
38 newlen
= pkt
->buf
->length
* 2;
40 if (BUF_MEM_grow(pkt
->buf
, newlen
) == 0)
42 if (pkt
->curr
== NULL
) {
44 * Can happen if initialised with a BUF_MEM that hasn't been
47 pkt
->curr
= (unsigned char *)pkt
->buf
->data
;
51 *allocbytes
= pkt
->curr
;
57 static size_t maxmaxsize(size_t lenbytes
)
59 if (lenbytes
>= sizeof(size_t) || lenbytes
== 0)
62 return ((size_t)1 << (lenbytes
* 8)) - 1 + lenbytes
;
66 * Initialise a WPACKET with the buffer in |buf|. The buffer must exist
67 * for the whole time that the WPACKET is being used. Additionally |lenbytes| of
68 * data is preallocated at the start of the buffer to store the length of the
69 * WPACKET once we know it.
71 int WPACKET_init_len(WPACKET
*pkt
, BUF_MEM
*buf
, size_t lenbytes
)
78 pkt
->curr
= (unsigned char *)buf
->data
;
80 pkt
->maxsize
= maxmaxsize(lenbytes
);
82 pkt
->subs
= OPENSSL_zalloc(sizeof(*pkt
->subs
));
83 if (pkt
->subs
== NULL
)
89 pkt
->subs
->pwritten
= lenbytes
;
90 pkt
->subs
->lenbytes
= lenbytes
;
92 if (!WPACKET_allocate_bytes(pkt
, lenbytes
, &(pkt
->subs
->packet_len
))) {
93 OPENSSL_free(pkt
->subs
);
102 * Same as WPACKET_init_len except there is no preallocation of the WPACKET
105 int WPACKET_init(WPACKET
*pkt
, BUF_MEM
*buf
)
107 return WPACKET_init_len(pkt
, buf
, 0);
111 * Set the WPACKET length, and the location for where we should write that
112 * length. Normally this will be at the start of the WPACKET, and therefore
113 * the WPACKET would have been initialised via WPACKET_init_len(). However there
114 * is the possibility that the length needs to be written to some other location
115 * other than the start of the WPACKET. In that case init via WPACKET_init() and
116 * then set the location for the length using this function.
118 int WPACKET_set_packet_len(WPACKET
*pkt
, unsigned char *packet_len
,
123 /* We only allow this to be set once */
124 if (pkt
->subs
== NULL
|| pkt
->subs
->lenbytes
!= 0)
127 pkt
->subs
->lenbytes
= lenbytes
;
128 pkt
->subs
->packet_len
= packet_len
;
130 maxmax
= maxmaxsize(lenbytes
);
131 if (pkt
->maxsize
> maxmax
)
132 pkt
->maxsize
= maxmax
;
137 int WPACKET_set_flags(WPACKET
*pkt
, unsigned int flags
)
139 if (pkt
->subs
== NULL
)
142 pkt
->subs
->flags
= flags
;
149 * Internal helper function used by WPACKET_close() and WPACKET_finish() to
150 * close a sub-packet and write out its length if necessary.
152 static int wpacket_intern_close(WPACKET
*pkt
)
155 WPACKET_SUB
*sub
= pkt
->subs
;
157 packlen
= pkt
->written
- sub
->pwritten
;
159 && sub
->flags
& OPENSSL_WPACKET_FLAGS_NON_ZERO_LENGTH
)
163 && sub
->flags
& OPENSSL_WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
) {
164 /* Deallocate any bytes allocated for the length of the WPACKET */
165 if ((pkt
->curr
- sub
->lenbytes
) == sub
->packet_len
) {
166 pkt
->written
-= sub
->lenbytes
;
167 pkt
->curr
-= sub
->lenbytes
;
170 /* Don't write out the packet length */
171 sub
->packet_len
= NULL
;
174 /* Write out the WPACKET length if needed */
175 if (sub
->packet_len
!= NULL
) {
178 lenbytes
= sub
->lenbytes
;
180 for (; lenbytes
> 0; lenbytes
--) {
181 sub
->packet_len
[lenbytes
- 1]
182 = (unsigned char)(packlen
& 0xff);
187 * We've extended beyond the max allowed for the number of len bytes
193 pkt
->subs
= sub
->parent
;
200 * Closes the most recent sub-packet. It also writes out the length of the
201 * packet to the required location (normally the start of the WPACKET) if
202 * appropriate. The top level WPACKET should be closed using WPACKET_finish()
203 * instead of this function.
205 int WPACKET_close(WPACKET
*pkt
)
207 if (pkt
->subs
== NULL
|| pkt
->subs
->parent
== NULL
)
210 return wpacket_intern_close(pkt
);
214 * The same as WPACKET_close() but only for the top most WPACKET. Additionally
215 * frees memory resources for this WPACKET.
217 int WPACKET_finish(WPACKET
*pkt
)
221 if (pkt
->subs
== NULL
|| pkt
->subs
->parent
!= NULL
)
224 ret
= wpacket_intern_close(pkt
);
227 OPENSSL_free(pkt
->subs
);
234 * Initialise a new sub-packet. Additionally |lenbytes| of data is preallocated
235 * at the start of the sub-packet to store its length once we know it.
237 int WPACKET_start_sub_packet_len(WPACKET
*pkt
, size_t lenbytes
)
241 if (pkt
->subs
== NULL
)
244 sub
= OPENSSL_zalloc(sizeof(*sub
));
248 sub
->parent
= pkt
->subs
;
250 sub
->pwritten
= pkt
->written
+ lenbytes
;
251 sub
->lenbytes
= lenbytes
;
254 sub
->packet_len
= NULL
;
258 if (!WPACKET_allocate_bytes(pkt
, lenbytes
, &sub
->packet_len
)) {
266 * Same as WPACKET_get_sub_packet_len() except no bytes are pre-allocated for
267 * the sub-packet length.
269 int WPACKET_start_sub_packet(WPACKET
*pkt
)
271 return WPACKET_start_sub_packet_len(pkt
, 0);
275 * Write the value stored in |val| into the WPACKET. The value will consome
276 * |bytes| amount of storage. An error will occur if |val| cannot be accommdated
277 * in |bytes| storage, e.g. attempting to write the value 256 into 1 byte will
280 int WPACKET_put_bytes(WPACKET
*pkt
, unsigned int val
, size_t bytes
)
284 if (bytes
> sizeof(unsigned int)
285 || !WPACKET_allocate_bytes(pkt
, bytes
, &data
))
289 for (; bytes
> 0; bytes
--) {
290 *data
= (unsigned char)(val
& 0xff);
295 /* Check whether we could fit the value in the assigned number of bytes */
302 /* Set a maximum size that we will not allow the WPACKET to grow beyond */
303 int WPACKET_set_max_size(WPACKET
*pkt
, size_t maxsize
)
308 if (pkt
->subs
== NULL
)
311 /* Find the WPACKET_SUB for the top level */
312 for (sub
= pkt
->subs
; sub
->parent
!= NULL
; sub
= sub
->parent
);
314 lenbytes
= sub
->lenbytes
;
316 lenbytes
= sizeof(pkt
->maxsize
);
318 if (maxmaxsize(lenbytes
) < maxsize
|| maxsize
< pkt
->written
)
321 pkt
->maxsize
= maxsize
;
327 * Copy |len| bytes of data from |*src| into the WPACKET.
329 int WPACKET_memcpy(WPACKET
*pkt
, const void *src
, size_t len
)
336 if (!WPACKET_allocate_bytes(pkt
, len
, &dest
))
339 memcpy(dest
, src
, len
);
345 * Copy |len| bytes of data from |*src| into the WPACKET and prefix with its
346 * length (consuming |lenbytes| of data for the length)
348 int WPACKET_sub_memcpy(WPACKET
*pkt
, const void *src
, size_t len
, size_t lenbytes
)
350 if (!WPACKET_start_sub_packet_len(pkt
, lenbytes
)
351 || !WPACKET_memcpy(pkt
, src
, len
)
352 || !WPACKET_close(pkt
))
359 * Return the total number of bytes written so far to the underlying buffer
360 * including any storage allocated for length bytes
362 int WPACKET_get_total_written(WPACKET
*pkt
, size_t *written
)
367 *written
= pkt
->written
;
373 * Returns the length of the last sub-packet. This excludes any bytes allocated
374 * for the length itself.
376 int WPACKET_get_length(WPACKET
*pkt
, size_t *len
)
378 if (pkt
->subs
== NULL
|| len
== NULL
)
381 *len
= pkt
->written
- pkt
->subs
->pwritten
;
387 * Release resources in a WPACKET if a failure has occurred.
389 void WPACKET_cleanup(WPACKET
*pkt
)
391 WPACKET_SUB
*sub
, *parent
;
393 for (sub
= pkt
->subs
; sub
!= NULL
; sub
= parent
) {
394 parent
= sub
->parent
;