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