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