]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/packet.c
Transfer the functionality from ssl3_read_n to the new record layer
[thirdparty/openssl.git] / crypto / packet.c
CommitLineData
b7273855 1/*
38fc02a7 2 * Copyright 2015-2021 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"
085e3cec 12#include <openssl/err.h>
b7273855 13
871bc59b
MC
14#define DEFAULT_BUF_SIZE 256
15
0217dd19 16int 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
26int 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 43int 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
77int 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
92static 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 100static 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) {
085e3cec 108 ERR_raise(ERR_LIB_CRYPTO, 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
128int 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
145int 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
159int 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 173int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
b7273855 174{
ae2f7b37 175 return WPACKET_init_len(pkt, buf, 0);
b7273855
MC
176}
177
15cb0f09
MC
178int 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
188int 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 198int 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 209/* Store the |value| of length |len| at location |data| */
dffafaf4 210static int put_value(unsigned char *data, uint64_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
416d0a63
HL
228static int put_quic_value(unsigned char *data, size_t value, size_t len)
229{
230 if (data == NULL)
231 return 1;
232
233 /* Value too large for field. */
234 if (ossl_quic_vlint_encode_len(value) > len)
235 return 0;
236
237 ossl_quic_vlint_encode_n(data, value, len);
238 return 1;
239}
0217dd19 240
b7273855 241/*
a2b7e655
MC
242 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
243 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
244 * necessary. If |doclose| is 0 then it goes through the motions of closing
245 * (i.e. it fills in all the lengths), but doesn't actually close anything.
b7273855 246 */
a2b7e655 247static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
b7273855 248{
de451856 249 size_t packlen = pkt->written - sub->pwritten;
b7273855 250
0217dd19 251 if (packlen == 0
c0f9e23c 252 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
b7273855
MC
253 return 0;
254
255 if (packlen == 0
de451856 256 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
a2b7e655
MC
257 /* We can't handle this case. Return an error */
258 if (!doclose)
259 return 0;
260
ae2f7b37 261 /* Deallocate any bytes allocated for the length of the WPACKET */
0217dd19
MC
262 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
263 pkt->written -= sub->lenbytes;
264 pkt->curr -= sub->lenbytes;
b7273855
MC
265 }
266
267 /* Don't write out the packet length */
de451856
MC
268 sub->packet_len = 0;
269 sub->lenbytes = 0;
b7273855
MC
270 }
271
ae2f7b37 272 /* Write out the WPACKET length if needed */
15cb0f09
MC
273 if (sub->lenbytes > 0) {
274 unsigned char *buf = GETBUF(pkt);
275
416d0a63
HL
276 if (buf != NULL) {
277 if ((sub->flags & WPACKET_FLAGS_QUIC_VLINT) == 0) {
278 if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
279 return 0;
280 } else {
281 if (!put_quic_value(&buf[sub->packet_len], packlen, sub->lenbytes))
282 return 0;
283 }
284 }
12cbb8e0
RL
285 } else if (pkt->endfirst && sub->parent != NULL
286 && (packlen != 0
287 || (sub->flags
288 & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) == 0)) {
d3ba3916
MC
289 size_t tmplen = packlen;
290 size_t numlenbytes = 1;
291
292 while ((tmplen = tmplen >> 8) > 0)
293 numlenbytes++;
294 if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
295 return 0;
296 if (packlen > 0x7f) {
297 numlenbytes |= 0x80;
298 if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
299 return 0;
300 }
15cb0f09 301 }
b7273855 302
a2b7e655
MC
303 if (doclose) {
304 pkt->subs = sub->parent;
305 OPENSSL_free(sub);
306 }
307
308 return 1;
309}
310
311int WPACKET_fill_lengths(WPACKET *pkt)
312{
313 WPACKET_SUB *sub;
314
b77f3ed1 315 if (!ossl_assert(pkt->subs != NULL))
a2b7e655
MC
316 return 0;
317
fed60a78 318 for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
a2b7e655
MC
319 if (!wpacket_intern_close(pkt, sub, 0))
320 return 0;
fed60a78 321 }
b7273855
MC
322
323 return 1;
324}
325
0217dd19 326int WPACKET_close(WPACKET *pkt)
b7273855 327{
c39609aa
MC
328 /*
329 * Internal API, so should not fail - but we do negative testing of this
330 * so no assert (otherwise the tests fail)
331 */
0217dd19 332 if (pkt->subs == NULL || pkt->subs->parent == NULL)
b7273855
MC
333 return 0;
334
a2b7e655 335 return wpacket_intern_close(pkt, pkt->subs, 1);
b7273855
MC
336}
337
0217dd19 338int WPACKET_finish(WPACKET *pkt)
b7273855 339{
0217dd19
MC
340 int ret;
341
c39609aa
MC
342 /*
343 * Internal API, so should not fail - but we do negative testing of this
344 * so no assert (otherwise the tests fail)
345 */
0217dd19
MC
346 if (pkt->subs == NULL || pkt->subs->parent != NULL)
347 return 0;
348
a2b7e655 349 ret = wpacket_intern_close(pkt, pkt->subs, 1);
871bc59b
MC
350 if (ret) {
351 OPENSSL_free(pkt->subs);
352 pkt->subs = NULL;
353 }
de451856 354
0217dd19 355 return ret;
b7273855
MC
356}
357
869d0a37 358int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
b7273855 359{
0217dd19 360 WPACKET_SUB *sub;
de451856 361 unsigned char *lenchars;
b7273855 362
c39609aa 363 /* Internal API, so should not fail */
b77f3ed1 364 if (!ossl_assert(pkt->subs != NULL))
b7273855
MC
365 return 0;
366
d3ba3916
MC
367 /* We don't support lenbytes greater than 0 when doing endfirst writing */
368 if (lenbytes > 0 && pkt->endfirst)
369 return 0;
370
cdb10bae 371 if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
085e3cec 372 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
b7273855 373 return 0;
cdb10bae 374 }
b7273855 375
0217dd19
MC
376 sub->parent = pkt->subs;
377 pkt->subs = sub;
378 sub->pwritten = pkt->written + lenbytes;
379 sub->lenbytes = lenbytes;
380
381 if (lenbytes == 0) {
de451856 382 sub->packet_len = 0;
0217dd19
MC
383 return 1;
384 }
385
15cb0f09
MC
386 sub->packet_len = pkt->written;
387
de451856 388 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
0217dd19 389 return 0;
b7273855
MC
390
391 return 1;
392}
393
0217dd19
MC
394int WPACKET_start_sub_packet(WPACKET *pkt)
395{
869d0a37 396 return WPACKET_start_sub_packet_len__(pkt, 0);
0217dd19
MC
397}
398
dffafaf4 399int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t size)
b7273855
MC
400{
401 unsigned char *data;
402
c39609aa 403 /* Internal API, so should not fail */
dffafaf4 404 if (!ossl_assert(size <= sizeof(uint64_t))
82657355
MC
405 || !WPACKET_allocate_bytes(pkt, size, &data)
406 || !put_value(data, val, size))
b7273855
MC
407 return 0;
408
409 return 1;
410}
411
ae2f7b37 412int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
b7273855 413{
871bc59b
MC
414 WPACKET_SUB *sub;
415 size_t lenbytes;
416
c39609aa 417 /* Internal API, so should not fail */
b77f3ed1 418 if (!ossl_assert(pkt->subs != NULL))
871bc59b
MC
419 return 0;
420
421 /* Find the WPACKET_SUB for the top level */
de451856
MC
422 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
423 continue;
871bc59b
MC
424
425 lenbytes = sub->lenbytes;
426 if (lenbytes == 0)
427 lenbytes = sizeof(pkt->maxsize);
428
429 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
430 return 0;
431
0217dd19 432 pkt->maxsize = maxsize;
b7273855
MC
433
434 return 1;
435}
436
c649d10d
TS
437int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
438{
439 unsigned char *dest;
440
441 if (len == 0)
442 return 1;
443
444 if (!WPACKET_allocate_bytes(pkt, len, &dest))
445 return 0;
446
15cb0f09
MC
447 if (dest != NULL)
448 memset(dest, ch, len);
c649d10d
TS
449
450 return 1;
451}
452
ae2f7b37 453int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
b7273855
MC
454{
455 unsigned char *dest;
456
457 if (len == 0)
458 return 1;
459
ae2f7b37 460 if (!WPACKET_allocate_bytes(pkt, len, &dest))
b7273855
MC
461 return 0;
462
15cb0f09
MC
463 if (dest != NULL)
464 memcpy(dest, src, len);
b7273855
MC
465
466 return 1;
467}
468
869d0a37
MC
469int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
470 size_t lenbytes)
fb790f16 471{
869d0a37 472 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
fb790f16
MC
473 || !WPACKET_memcpy(pkt, src, len)
474 || !WPACKET_close(pkt))
475 return 0;
476
477 return 1;
478}
479
ae2f7b37 480int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
b7273855 481{
c39609aa 482 /* Internal API, so should not fail */
b77f3ed1 483 if (!ossl_assert(written != NULL))
b7273855
MC
484 return 0;
485
0217dd19 486 *written = pkt->written;
b7273855
MC
487
488 return 1;
489}
490
ae2f7b37 491int WPACKET_get_length(WPACKET *pkt, size_t *len)
b7273855 492{
c39609aa 493 /* Internal API, so should not fail */
b77f3ed1 494 if (!ossl_assert(pkt->subs != NULL && len != NULL))
b7273855
MC
495 return 0;
496
0217dd19 497 *len = pkt->written - pkt->subs->pwritten;
b7273855
MC
498
499 return 1;
500}
871bc59b 501
3171bad6
MC
502unsigned char *WPACKET_get_curr(WPACKET *pkt)
503{
15cb0f09
MC
504 unsigned char *buf = GETBUF(pkt);
505
506 if (buf == NULL)
507 return NULL;
508
d3ba3916
MC
509 if (pkt->endfirst)
510 return buf + pkt->maxsize - pkt->curr;
511
15cb0f09
MC
512 return buf + pkt->curr;
513}
514
515int WPACKET_is_null_buf(WPACKET *pkt)
516{
517 return pkt->buf == NULL && pkt->staticbuf == NULL;
3171bad6
MC
518}
519
871bc59b
MC
520void WPACKET_cleanup(WPACKET *pkt)
521{
522 WPACKET_SUB *sub, *parent;
523
524 for (sub = pkt->subs; sub != NULL; sub = parent) {
525 parent = sub->parent;
526 OPENSSL_free(sub);
527 }
528 pkt->subs = NULL;
529}
416d0a63
HL
530
531int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len)
532{
533 size_t enclen = ossl_quic_vlint_encode_len(max_len);
534
535 if (enclen == 0)
536 return 0;
537
538 if (WPACKET_start_sub_packet_len__(pkt, enclen) == 0)
539 return 0;
540
541 pkt->subs->flags |= WPACKET_FLAGS_QUIC_VLINT;
542 return 1;
543}
544
545int WPACKET_start_quic_sub_packet(WPACKET *pkt)
546{
547 /*
548 * Assume no (sub)packet will exceed 4GiB, thus the 8-byte encoding need not
549 * be used.
550 */
551 return WPACKET_start_quic_sub_packet_bound(pkt, OSSL_QUIC_VLINT_4B_MIN);
552}
553
554int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
555{
556 if (!WPACKET_start_quic_sub_packet_bound(pkt, len)
557 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
558 || !WPACKET_close(pkt))
559 return 0;
560
561 return 1;
562}
563
564/*
565 * Write a QUIC variable-length integer to the packet.
566 */
567int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v)
568{
569 unsigned char *b = NULL;
570 size_t enclen = ossl_quic_vlint_encode_len(v);
571
572 if (enclen == 0)
573 return 0;
574
575 if (WPACKET_allocate_bytes(pkt, enclen, &b) == 0)
576 return 0;
577
578 ossl_quic_vlint_encode(b, v);
579 return 1;
580}