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