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