QUIC: Enable building with QUIC support disabled
[openssl.git] / include / internal / packet.h
1 /*
2  * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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
10 #ifndef OSSL_INTERNAL_PACKET_H
11 # define OSSL_INTERNAL_PACKET_H
12 # pragma once
13
14 # include <string.h>
15 # include <openssl/bn.h>
16 # include <openssl/buffer.h>
17 # include <openssl/crypto.h>
18 # include <openssl/e_os2.h>
19
20 # include "internal/numbers.h"
21 # include "internal/quic_vlint.h"
22
23 typedef struct {
24     /* Pointer to where we are currently reading from */
25     const unsigned char *curr;
26     /* Number of bytes remaining */
27     size_t remaining;
28 } PACKET;
29
30 /* Internal unchecked shorthand; don't use outside this file. */
31 static ossl_inline void packet_forward(PACKET *pkt, size_t len)
32 {
33     pkt->curr += len;
34     pkt->remaining -= len;
35 }
36
37 /*
38  * Returns the number of bytes remaining to be read in the PACKET
39  */
40 static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
41 {
42     return pkt->remaining;
43 }
44
45 /*
46  * Returns a pointer to the first byte after the packet data.
47  * Useful for integrating with non-PACKET parsing code.
48  * Specifically, we use PACKET_end() to verify that a d2i_... call
49  * has consumed the entire packet contents.
50  */
51 static ossl_inline const unsigned char *PACKET_end(const PACKET *pkt)
52 {
53     return pkt->curr + pkt->remaining;
54 }
55
56 /*
57  * Returns a pointer to the PACKET's current position.
58  * For use in non-PACKETized APIs.
59  */
60 static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt)
61 {
62     return pkt->curr;
63 }
64
65 /*
66  * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
67  * copy of the data so |buf| must be present for the whole time that the PACKET
68  * is being used.
69  */
70 __owur static ossl_inline int PACKET_buf_init(PACKET *pkt,
71                                               const unsigned char *buf,
72                                               size_t len)
73 {
74     /* Sanity check for negative values. */
75     if (len > (size_t)(SIZE_MAX / 2))
76         return 0;
77
78     pkt->curr = buf;
79     pkt->remaining = len;
80     return 1;
81 }
82
83 /* Initialize a PACKET to hold zero bytes. */
84 static ossl_inline void PACKET_null_init(PACKET *pkt)
85 {
86     pkt->curr = NULL;
87     pkt->remaining = 0;
88 }
89
90 /*
91  * Returns 1 if the packet has length |num| and its contents equal the |num|
92  * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
93  * If lengths are equal, performs the comparison in constant time.
94  */
95 __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
96                                            size_t num)
97 {
98     if (PACKET_remaining(pkt) != num)
99         return 0;
100     return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
101 }
102
103 /*
104  * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
105  * Data is not copied: the |subpkt| packet will share its underlying buffer with
106  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
107  */
108 __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
109                                                      PACKET *subpkt, size_t len)
110 {
111     if (PACKET_remaining(pkt) < len)
112         return 0;
113
114     return PACKET_buf_init(subpkt, pkt->curr, len);
115 }
116
117 /*
118  * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
119  * copied: the |subpkt| packet will share its underlying buffer with the
120  * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
121  */
122 __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
123                                                     PACKET *subpkt, size_t len)
124 {
125     if (!PACKET_peek_sub_packet(pkt, subpkt, len))
126         return 0;
127
128     packet_forward(pkt, len);
129
130     return 1;
131 }
132
133 /*
134  * Peek ahead at 2 bytes in network order from |pkt| and store the value in
135  * |*data|
136  */
137 __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
138                                                 unsigned int *data)
139 {
140     if (PACKET_remaining(pkt) < 2)
141         return 0;
142
143     *data = ((unsigned int)(*pkt->curr)) << 8;
144     *data |= *(pkt->curr + 1);
145
146     return 1;
147 }
148
149 /* Equivalent of n2s */
150 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
151 __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
152 {
153     if (!PACKET_peek_net_2(pkt, data))
154         return 0;
155
156     packet_forward(pkt, 2);
157
158     return 1;
159 }
160
161 /* Same as PACKET_get_net_2() but for a size_t */
162 __owur static ossl_inline int PACKET_get_net_2_len(PACKET *pkt, size_t *data)
163 {
164     unsigned int i;
165     int ret = PACKET_get_net_2(pkt, &i);
166
167     if (ret)
168         *data = (size_t)i;
169
170     return ret;
171 }
172
173 /*
174  * Peek ahead at 3 bytes in network order from |pkt| and store the value in
175  * |*data|
176  */
177 __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
178                                                 unsigned long *data)
179 {
180     if (PACKET_remaining(pkt) < 3)
181         return 0;
182
183     *data = ((unsigned long)(*pkt->curr)) << 16;
184     *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
185     *data |= *(pkt->curr + 2);
186
187     return 1;
188 }
189
190 /* Equivalent of n2l3 */
191 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
192 __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
193 {
194     if (!PACKET_peek_net_3(pkt, data))
195         return 0;
196
197     packet_forward(pkt, 3);
198
199     return 1;
200 }
201
202 /* Same as PACKET_get_net_3() but for a size_t */
203 __owur static ossl_inline int PACKET_get_net_3_len(PACKET *pkt, size_t *data)
204 {
205     unsigned long i;
206     int ret = PACKET_get_net_3(pkt, &i);
207
208     if (ret)
209         *data = (size_t)i;
210
211     return ret;
212 }
213
214 /*
215  * Peek ahead at 4 bytes in network order from |pkt| and store the value in
216  * |*data|
217  */
218 __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
219                                                 unsigned long *data)
220 {
221     if (PACKET_remaining(pkt) < 4)
222         return 0;
223
224     *data = ((unsigned long)(*pkt->curr)) << 24;
225     *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
226     *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
227     *data |= *(pkt->curr + 3);
228
229     return 1;
230 }
231
232 /*
233  * Peek ahead at 8 bytes in network order from |pkt| and store the value in
234  * |*data|
235  */
236 __owur static ossl_inline int PACKET_peek_net_8(const PACKET *pkt,
237                                                 uint64_t *data)
238 {
239     if (PACKET_remaining(pkt) < 8)
240         return 0;
241
242     *data = ((uint64_t)(*pkt->curr)) << 56;
243     *data |= ((uint64_t)(*(pkt->curr + 1))) << 48;
244     *data |= ((uint64_t)(*(pkt->curr + 2))) << 40;
245     *data |= ((uint64_t)(*(pkt->curr + 3))) << 32;
246     *data |= ((uint64_t)(*(pkt->curr + 4))) << 24;
247     *data |= ((uint64_t)(*(pkt->curr + 5))) << 16;
248     *data |= ((uint64_t)(*(pkt->curr + 6))) << 8;
249     *data |= *(pkt->curr + 7);
250
251     return 1;
252 }
253
254 # ifndef OPENSSL_NO_QUIC
255
256 /*
257  * Decodes a QUIC variable-length integer in |pkt| and stores the result in
258  * |data|.
259  */
260 __owur static ossl_inline int PACKET_get_quic_vlint(PACKET *pkt,
261                                                     uint64_t *data)
262 {
263     size_t enclen;
264
265     if (PACKET_remaining(pkt) < 1)
266         return 0;
267
268     enclen = ossl_quic_vlint_decode_len(*pkt->curr);
269
270     if (PACKET_remaining(pkt) < enclen)
271         return 0;
272
273     *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
274     packet_forward(pkt, enclen);
275     return 1;
276 }
277
278 /*
279  * Decodes a QUIC variable-length integer in |pkt| and stores the result in
280  * |data|. Unlike PACKET_get_quic_vlint, this does not advance the current
281  * position.
282  */
283 __owur static ossl_inline int PACKET_peek_quic_vlint(PACKET *pkt,
284                                                      uint64_t *data)
285 {
286     size_t enclen;
287
288     if (PACKET_remaining(pkt) < 1)
289         return 0;
290
291     enclen = ossl_quic_vlint_decode_len(*pkt->curr);
292
293     if (PACKET_remaining(pkt) < enclen)
294         return 0;
295
296     *data = ossl_quic_vlint_decode_unchecked(pkt->curr);
297     return 1;
298 }
299
300 /*
301  * Skips over a QUIC variable-length integer in |pkt| without decoding it.
302  */
303 __owur static ossl_inline int PACKET_skip_quic_vlint(PACKET *pkt)
304 {
305     size_t enclen;
306
307     if (PACKET_remaining(pkt) < 1)
308         return 0;
309
310     enclen = ossl_quic_vlint_decode_len(*pkt->curr);
311
312     if (PACKET_remaining(pkt) < enclen)
313         return 0;
314
315     packet_forward(pkt, enclen);
316     return 1;
317 }
318
319 # endif
320
321 /* Equivalent of n2l */
322 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
323 __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
324 {
325     if (!PACKET_peek_net_4(pkt, data))
326         return 0;
327
328     packet_forward(pkt, 4);
329
330     return 1;
331 }
332
333 /* Same as PACKET_get_net_4() but for a size_t */
334 __owur static ossl_inline int PACKET_get_net_4_len(PACKET *pkt, size_t *data)
335 {
336     unsigned long i;
337     int ret = PACKET_get_net_4(pkt, &i);
338
339     if (ret)
340         *data = (size_t)i;
341
342     return ret;
343 }
344
345 /* Get 8 bytes in network order from |pkt| and store the value in |*data| */
346 __owur static ossl_inline int PACKET_get_net_8(PACKET *pkt, uint64_t *data)
347 {
348     if (!PACKET_peek_net_8(pkt, data))
349         return 0;
350
351     packet_forward(pkt, 8);
352
353     return 1;
354 }
355
356 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
357 __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
358                                             unsigned int *data)
359 {
360     if (!PACKET_remaining(pkt))
361         return 0;
362
363     *data = *pkt->curr;
364
365     return 1;
366 }
367
368 /* Get 1 byte from |pkt| and store the value in |*data| */
369 __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
370 {
371     if (!PACKET_peek_1(pkt, data))
372         return 0;
373
374     packet_forward(pkt, 1);
375
376     return 1;
377 }
378
379 /* Same as PACKET_get_1() but for a size_t */
380 __owur static ossl_inline int PACKET_get_1_len(PACKET *pkt, size_t *data)
381 {
382     unsigned int i;
383     int ret = PACKET_get_1(pkt, &i);
384
385     if (ret)
386         *data = (size_t)i;
387
388     return ret;
389 }
390
391 /*
392  * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
393  * in |*data|
394  */
395 __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
396                                             unsigned long *data)
397 {
398     if (PACKET_remaining(pkt) < 4)
399         return 0;
400
401     *data = *pkt->curr;
402     *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
403     *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
404     *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
405
406     return 1;
407 }
408
409 /* Equivalent of c2l */
410 /*
411  * Get 4 bytes in reverse network order from |pkt| and store the value in
412  * |*data|
413  */
414 __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
415 {
416     if (!PACKET_peek_4(pkt, data))
417         return 0;
418
419     packet_forward(pkt, 4);
420
421     return 1;
422 }
423
424 /*
425  * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
426  * |*data|. This just points at the underlying buffer that |pkt| is using. The
427  * caller should not free this data directly (it will be freed when the
428  * underlying buffer gets freed
429  */
430 __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
431                                                 const unsigned char **data,
432                                                 size_t len)
433 {
434     if (PACKET_remaining(pkt) < len)
435         return 0;
436
437     *data = pkt->curr;
438
439     return 1;
440 }
441
442 /*
443  * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
444  * just points at the underlying buffer that |pkt| is using. The caller should
445  * not free this data directly (it will be freed when the underlying buffer gets
446  * freed
447  */
448 __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
449                                                const unsigned char **data,
450                                                size_t len)
451 {
452     if (!PACKET_peek_bytes(pkt, data, len))
453         return 0;
454
455     packet_forward(pkt, len);
456
457     return 1;
458 }
459
460 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
461 __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
462                                                      unsigned char *data,
463                                                      size_t len)
464 {
465     if (PACKET_remaining(pkt) < len)
466         return 0;
467
468     memcpy(data, pkt->curr, len);
469
470     return 1;
471 }
472
473 /*
474  * Read |len| bytes from |pkt| and copy them to |data|.
475  * The caller is responsible for ensuring that |data| can hold |len| bytes.
476  */
477 __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
478                                                 unsigned char *data, size_t len)
479 {
480     if (!PACKET_peek_copy_bytes(pkt, data, len))
481         return 0;
482
483     packet_forward(pkt, len);
484
485     return 1;
486 }
487
488 /*
489  * Copy packet data to |dest|, and set |len| to the number of copied bytes.
490  * If the packet has more than |dest_len| bytes, nothing is copied.
491  * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
492  * Does not forward PACKET position (because it is typically the last thing
493  * done with a given PACKET).
494  */
495 __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
496                                               unsigned char *dest,
497                                               size_t dest_len, size_t *len)
498 {
499     if (PACKET_remaining(pkt) > dest_len) {
500         *len = 0;
501         return 0;
502     }
503     *len = pkt->remaining;
504     memcpy(dest, pkt->curr, pkt->remaining);
505     return 1;
506 }
507
508 /*
509  * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
510  * result in |*data|, and the length in |len|.
511  * If |*data| is not NULL, the old data is OPENSSL_free'd.
512  * If the packet is empty, or malloc fails, |*data| will be set to NULL.
513  * Returns 1 if the malloc succeeds and 0 otherwise.
514  * Does not forward PACKET position (because it is typically the last thing
515  * done with a given PACKET).
516  */
517 __owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
518                                             unsigned char **data, size_t *len)
519 {
520     size_t length;
521
522     OPENSSL_free(*data);
523     *data = NULL;
524     *len = 0;
525
526     length = PACKET_remaining(pkt);
527
528     if (length == 0)
529         return 1;
530
531     *data = OPENSSL_memdup(pkt->curr, length);
532     if (*data == NULL)
533         return 0;
534
535     *len = length;
536     return 1;
537 }
538
539 /*
540  * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
541  * buffer. Store a pointer to the result in |*data|.
542  * If |*data| is not NULL, the old data is OPENSSL_free'd.
543  * If the data in |pkt| does not contain a NUL-byte, the entire data is
544  * copied and NUL-terminated.
545  * Returns 1 if the malloc succeeds and 0 otherwise.
546  * Does not forward PACKET position (because it is typically the last thing done
547  * with a given PACKET).
548  */
549 __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
550 {
551     OPENSSL_free(*data);
552
553     /* This will succeed on an empty packet, unless pkt->curr == NULL. */
554     *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
555     return (*data != NULL);
556 }
557
558 /* Returns 1 if |pkt| contains at least one 0-byte, 0 otherwise. */
559 static ossl_inline int PACKET_contains_zero_byte(const PACKET *pkt)
560 {
561     return memchr(pkt->curr, 0, pkt->remaining) != NULL;
562 }
563
564 /* Move the current reading position forward |len| bytes */
565 __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
566 {
567     if (PACKET_remaining(pkt) < len)
568         return 0;
569
570     packet_forward(pkt, len);
571
572     return 1;
573 }
574
575 /*
576  * Reads a variable-length vector prefixed with a one-byte length, and stores
577  * the contents in |subpkt|. |pkt| can equal |subpkt|.
578  * Data is not copied: the |subpkt| packet will share its underlying buffer with
579  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
580  * Upon failure, the original |pkt| and |subpkt| are not modified.
581  */
582 __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
583                                                            PACKET *subpkt)
584 {
585     unsigned int length;
586     const unsigned char *data;
587     PACKET tmp = *pkt;
588     if (!PACKET_get_1(&tmp, &length) ||
589         !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
590         return 0;
591     }
592
593     *pkt = tmp;
594     subpkt->curr = data;
595     subpkt->remaining = length;
596
597     return 1;
598 }
599
600 /*
601  * Like PACKET_get_length_prefixed_1, but additionally, fails when there are
602  * leftover bytes in |pkt|.
603  */
604 __owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt,
605                                                           PACKET *subpkt)
606 {
607     unsigned int length;
608     const unsigned char *data;
609     PACKET tmp = *pkt;
610     if (!PACKET_get_1(&tmp, &length) ||
611         !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
612         PACKET_remaining(&tmp) != 0) {
613         return 0;
614     }
615
616     *pkt = tmp;
617     subpkt->curr = data;
618     subpkt->remaining = length;
619
620     return 1;
621 }
622
623 /*
624  * Reads a variable-length vector prefixed with a two-byte length, and stores
625  * the contents in |subpkt|. |pkt| can equal |subpkt|.
626  * Data is not copied: the |subpkt| packet will share its underlying buffer with
627  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
628  * Upon failure, the original |pkt| and |subpkt| are not modified.
629  */
630 __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
631                                                            PACKET *subpkt)
632 {
633     unsigned int length;
634     const unsigned char *data;
635     PACKET tmp = *pkt;
636
637     if (!PACKET_get_net_2(&tmp, &length) ||
638         !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
639         return 0;
640     }
641
642     *pkt = tmp;
643     subpkt->curr = data;
644     subpkt->remaining = length;
645
646     return 1;
647 }
648
649 /*
650  * Like PACKET_get_length_prefixed_2, but additionally, fails when there are
651  * leftover bytes in |pkt|.
652  */
653 __owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt,
654                                                           PACKET *subpkt)
655 {
656     unsigned int length;
657     const unsigned char *data;
658     PACKET tmp = *pkt;
659
660     if (!PACKET_get_net_2(&tmp, &length) ||
661         !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
662         PACKET_remaining(&tmp) != 0) {
663         return 0;
664     }
665
666     *pkt = tmp;
667     subpkt->curr = data;
668     subpkt->remaining = length;
669
670     return 1;
671 }
672
673 /*
674  * Reads a variable-length vector prefixed with a three-byte length, and stores
675  * the contents in |subpkt|. |pkt| can equal |subpkt|.
676  * Data is not copied: the |subpkt| packet will share its underlying buffer with
677  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
678  * Upon failure, the original |pkt| and |subpkt| are not modified.
679  */
680 __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
681                                                            PACKET *subpkt)
682 {
683     unsigned long length;
684     const unsigned char *data;
685     PACKET tmp = *pkt;
686     if (!PACKET_get_net_3(&tmp, &length) ||
687         !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
688         return 0;
689     }
690
691     *pkt = tmp;
692     subpkt->curr = data;
693     subpkt->remaining = length;
694
695     return 1;
696 }
697
698 # ifndef OPENSSL_NO_QUIC
699
700 /*
701  * Reads a variable-length vector prefixed with a QUIC variable-length integer
702  * denoting the length, and stores the contents in |subpkt|. |pkt| can equal
703  * |subpkt|. Data is not copied: the |subpkt| packet will share its underlying
704  * buffer with the original |pkt|, so data wrapped by |pkt| must outlive the
705  * |subpkt|. Upon failure, the original |pkt| and |subpkt| are not modified.
706  */
707 __owur static ossl_inline int PACKET_get_quic_length_prefixed(PACKET *pkt,
708                                                               PACKET *subpkt)
709 {
710     uint64_t length;
711     const unsigned char *data;
712     PACKET tmp = *pkt;
713
714     if (!PACKET_get_quic_vlint(&tmp, &length) ||
715         length > SIZE_MAX ||
716         !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
717         return 0;
718     }
719
720     *pkt = tmp;
721     subpkt->curr = data;
722     subpkt->remaining = (size_t)length;
723
724     return 1;
725 }
726
727 # endif
728
729 /* Writeable packets */
730
731 typedef struct wpacket_sub WPACKET_SUB;
732 struct wpacket_sub {
733     /* The parent WPACKET_SUB if we have one or NULL otherwise */
734     WPACKET_SUB *parent;
735
736     /*
737      * Offset into the buffer where the length of this WPACKET goes. We use an
738      * offset in case the buffer grows and gets reallocated.
739      */
740     size_t packet_len;
741
742     /* Number of bytes in the packet_len or 0 if we don't write the length */
743     size_t lenbytes;
744
745     /* Number of bytes written to the buf prior to this packet starting */
746     size_t pwritten;
747
748     /* Flags for this sub-packet */
749     unsigned int flags;
750 };
751
752 typedef struct wpacket_st WPACKET;
753 struct wpacket_st {
754     /* The buffer where we store the output data */
755     BUF_MEM *buf;
756
757     /* Fixed sized buffer which can be used as an alternative to buf */
758     unsigned char *staticbuf;
759
760     /*
761      * Offset into the buffer where we are currently writing. We use an offset
762      * in case the buffer grows and gets reallocated.
763      */
764     size_t curr;
765
766     /* Number of bytes written so far */
767     size_t written;
768
769     /* Maximum number of bytes we will allow to be written to this WPACKET */
770     size_t maxsize;
771
772     /* Our sub-packets (always at least one if not finished) */
773     WPACKET_SUB *subs;
774
775     /* Writing from the end first? */
776     unsigned int endfirst : 1;
777 };
778
779 /* Flags */
780
781 /* Default */
782 #define WPACKET_FLAGS_NONE                      0
783
784 /* Error on WPACKET_close() if no data written to the WPACKET */
785 #define WPACKET_FLAGS_NON_ZERO_LENGTH           1
786
787 /*
788  * Abandon all changes on WPACKET_close() if no data written to the WPACKET,
789  * i.e. this does not write out a zero packet length
790  */
791 #define WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH    2
792
793 /* QUIC variable-length integer length prefix */
794 #define WPACKET_FLAGS_QUIC_VLINT                4
795
796 /*
797  * Initialise a WPACKET with the buffer in |buf|. The buffer must exist
798  * for the whole time that the WPACKET is being used. Additionally |lenbytes| of
799  * data is preallocated at the start of the buffer to store the length of the
800  * WPACKET once we know it.
801  */
802 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes);
803
804 /*
805  * Same as WPACKET_init_len except there is no preallocation of the WPACKET
806  * length.
807  */
808 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf);
809
810 /*
811  * Same as WPACKET_init_len except there is no underlying buffer. No data is
812  * ever actually written. We just keep track of how much data would have been
813  * written if a buffer was there.
814  */
815 int WPACKET_init_null(WPACKET *pkt, size_t lenbytes);
816
817 /*
818  * Same as WPACKET_init_null except we set the WPACKET to assume DER length
819  * encoding for sub-packets.
820  */
821 int WPACKET_init_null_der(WPACKET *pkt);
822
823 /*
824  * Same as WPACKET_init_len except we do not use a growable BUF_MEM structure.
825  * A fixed buffer of memory |buf| of size |len| is used instead. A failure will
826  * occur if you attempt to write beyond the end of the buffer
827  */
828 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
829                             size_t lenbytes);
830
831 /*
832  * Same as WPACKET_init_static_len except lenbytes is always 0, and we set the
833  * WPACKET to write to the end of the buffer moving towards the start and use
834  * DER length encoding for sub-packets.
835  */
836 int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len);
837
838 /*
839  * Set the flags to be applied to the current sub-packet
840  */
841 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags);
842
843 /*
844  * Closes the most recent sub-packet. It also writes out the length of the
845  * packet to the required location (normally the start of the WPACKET) if
846  * appropriate. The top level WPACKET should be closed using WPACKET_finish()
847  * instead of this function.
848  */
849 int WPACKET_close(WPACKET *pkt);
850
851 /*
852  * The same as WPACKET_close() but only for the top most WPACKET. Additionally
853  * frees memory resources for this WPACKET.
854  */
855 int WPACKET_finish(WPACKET *pkt);
856
857 /*
858  * Iterate through all the sub-packets and write out their lengths as if they
859  * were being closed. The lengths will be overwritten with the final lengths
860  * when the sub-packets are eventually closed (which may be different if more
861  * data is added to the WPACKET). This function fails if a sub-packet is of 0
862  * length and WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH is set.
863  */
864 int WPACKET_fill_lengths(WPACKET *pkt);
865
866 /*
867  * Initialise a new sub-packet. Additionally |lenbytes| of data is preallocated
868  * at the start of the sub-packet to store its length once we know it. Don't
869  * call this directly. Use the convenience macros below instead.
870  */
871 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes);
872
873 /*
874  * Convenience macros for calling WPACKET_start_sub_packet_len with different
875  * lengths
876  */
877 #define WPACKET_start_sub_packet_u8(pkt) \
878     WPACKET_start_sub_packet_len__((pkt), 1)
879 #define WPACKET_start_sub_packet_u16(pkt) \
880     WPACKET_start_sub_packet_len__((pkt), 2)
881 #define WPACKET_start_sub_packet_u24(pkt) \
882     WPACKET_start_sub_packet_len__((pkt), 3)
883 #define WPACKET_start_sub_packet_u32(pkt) \
884     WPACKET_start_sub_packet_len__((pkt), 4)
885
886 /*
887  * Same as WPACKET_start_sub_packet_len__() except no bytes are pre-allocated
888  * for the sub-packet length.
889  */
890 int WPACKET_start_sub_packet(WPACKET *pkt);
891
892 /*
893  * Allocate bytes in the WPACKET for the output. This reserves the bytes
894  * and counts them as "written", but doesn't actually do the writing. A pointer
895  * to the allocated bytes is stored in |*allocbytes|. |allocbytes| may be NULL.
896  * WARNING: the allocated bytes must be filled in immediately, without further
897  * WPACKET_* calls. If not then the underlying buffer may be realloc'd and
898  * change its location.
899  */
900 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len,
901                            unsigned char **allocbytes);
902
903 /*
904  * The same as WPACKET_allocate_bytes() except additionally a new sub-packet is
905  * started for the allocated bytes, and then closed immediately afterwards. The
906  * number of length bytes for the sub-packet is in |lenbytes|. Don't call this
907  * directly. Use the convenience macros below instead.
908  */
909 int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
910                                  unsigned char **allocbytes, size_t lenbytes);
911
912 /*
913  * Convenience macros for calling WPACKET_sub_allocate_bytes with different
914  * lengths
915  */
916 #define WPACKET_sub_allocate_bytes_u8(pkt, len, bytes) \
917     WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 1)
918 #define WPACKET_sub_allocate_bytes_u16(pkt, len, bytes) \
919     WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 2)
920 #define WPACKET_sub_allocate_bytes_u24(pkt, len, bytes) \
921     WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 3)
922 #define WPACKET_sub_allocate_bytes_u32(pkt, len, bytes) \
923     WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 4)
924
925 /*
926  * The same as WPACKET_allocate_bytes() except the reserved bytes are not
927  * actually counted as written. Typically this will be for when we don't know
928  * how big arbitrary data is going to be up front, but we do know what the
929  * maximum size will be. If this function is used, then it should be immediately
930  * followed by a WPACKET_allocate_bytes() call before any other WPACKET
931  * functions are called (unless the write to the allocated bytes is abandoned).
932  *
933  * For example: If we are generating a signature, then the size of that
934  * signature may not be known in advance. We can use WPACKET_reserve_bytes() to
935  * handle this:
936  *
937  *  if (!WPACKET_sub_reserve_bytes_u16(&pkt, EVP_PKEY_get_size(pkey), &sigbytes1)
938  *          || EVP_SignFinal(md_ctx, sigbytes1, &siglen, pkey) <= 0
939  *          || !WPACKET_sub_allocate_bytes_u16(&pkt, siglen, &sigbytes2)
940  *          || sigbytes1 != sigbytes2)
941  *      goto err;
942  */
943 int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes);
944
945 /*
946  * The "reserve_bytes" equivalent of WPACKET_sub_allocate_bytes__()
947  */
948 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
949                                  unsigned char **allocbytes, size_t lenbytes);
950
951 /*
952  * Convenience macros for  WPACKET_sub_reserve_bytes with different lengths
953  */
954 #define WPACKET_sub_reserve_bytes_u8(pkt, len, bytes) \
955     WPACKET_reserve_bytes__((pkt), (len), (bytes), 1)
956 #define WPACKET_sub_reserve_bytes_u16(pkt, len, bytes) \
957     WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 2)
958 #define WPACKET_sub_reserve_bytes_u24(pkt, len, bytes) \
959     WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 3)
960 #define WPACKET_sub_reserve_bytes_u32(pkt, len, bytes) \
961     WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 4)
962
963 /*
964  * Write the value stored in |val| into the WPACKET. The value will consume
965  * |bytes| amount of storage. An error will occur if |val| cannot be
966  * accommodated in |bytes| storage, e.g. attempting to write the value 256 into
967  * 1 byte will fail. Don't call this directly. Use the convenience macros below
968  * instead.
969  */
970 int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t bytes);
971
972 /*
973  * Convenience macros for calling WPACKET_put_bytes with different
974  * lengths
975  */
976 #define WPACKET_put_bytes_u8(pkt, val) \
977     WPACKET_put_bytes__((pkt), (val), 1)
978 #define WPACKET_put_bytes_u16(pkt, val) \
979     WPACKET_put_bytes__((pkt), (val), 2)
980 #define WPACKET_put_bytes_u24(pkt, val) \
981     WPACKET_put_bytes__((pkt), (val), 3)
982 #define WPACKET_put_bytes_u32(pkt, val) \
983     WPACKET_put_bytes__((pkt), (val), 4)
984 #define WPACKET_put_bytes_u64(pkt, val) \
985     WPACKET_put_bytes__((pkt), (val), 8)
986
987 /* Set a maximum size that we will not allow the WPACKET to grow beyond */
988 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize);
989
990 /* Copy |len| bytes of data from |*src| into the WPACKET. */
991 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len);
992
993 /* Set |len| bytes of data to |ch| into the WPACKET. */
994 int WPACKET_memset(WPACKET *pkt, int ch, size_t len);
995
996 /*
997  * Copy |len| bytes of data from |*src| into the WPACKET and prefix with its
998  * length (consuming |lenbytes| of data for the length). Don't call this
999  * directly. Use the convenience macros below instead.
1000  */
1001 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
1002                        size_t lenbytes);
1003
1004 /* Convenience macros for calling WPACKET_sub_memcpy with different lengths */
1005 #define WPACKET_sub_memcpy_u8(pkt, src, len) \
1006     WPACKET_sub_memcpy__((pkt), (src), (len), 1)
1007 #define WPACKET_sub_memcpy_u16(pkt, src, len) \
1008     WPACKET_sub_memcpy__((pkt), (src), (len), 2)
1009 #define WPACKET_sub_memcpy_u24(pkt, src, len) \
1010     WPACKET_sub_memcpy__((pkt), (src), (len), 3)
1011 #define WPACKET_sub_memcpy_u32(pkt, src, len) \
1012     WPACKET_sub_memcpy__((pkt), (src), (len), 4)
1013
1014 /*
1015  * Return the total number of bytes written so far to the underlying buffer
1016  * including any storage allocated for length bytes
1017  */
1018 int WPACKET_get_total_written(WPACKET *pkt, size_t *written);
1019
1020 /*
1021  * Returns the length of the current sub-packet. This excludes any bytes
1022  * allocated for the length itself.
1023  */
1024 int WPACKET_get_length(WPACKET *pkt, size_t *len);
1025
1026 /*
1027  * Returns a pointer to the current write location, but does not allocate any
1028  * bytes.
1029  */
1030 unsigned char *WPACKET_get_curr(WPACKET *pkt);
1031
1032 /* Returns true if the underlying buffer is actually NULL */
1033 int WPACKET_is_null_buf(WPACKET *pkt);
1034
1035 /* Release resources in a WPACKET if a failure has occurred. */
1036 void WPACKET_cleanup(WPACKET *pkt);
1037
1038 # ifndef OPENSSL_NO_QUIC
1039
1040 /*
1041  * Starts a QUIC sub-packet headed by a QUIC variable-length integer. A 4-byte
1042  * representation is used.
1043  */
1044 __owur int WPACKET_start_quic_sub_packet(WPACKET *pkt);
1045
1046 /*
1047  * Starts a QUIC sub-packet headed by a QUIC variable-length integer. max_len
1048  * specifies the upper bound for the sub-packet size at the time the sub-packet
1049  * is closed, which determines the encoding size for tthe variable-length
1050  * integer header. max_len can be a precise figure or a worst-case bound
1051  * if a precise figure is not available.
1052  */
1053 __owur int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len);
1054
1055 /*
1056  * Allocates a QUIC sub-packet with exactly len bytes of payload, headed by a
1057  * QUIC variable-length integer. The pointer to the payload buffer is output and
1058  * must be filled by the caller. This function assures optimal selection of
1059  * variable-length integer encoding length.
1060  */
1061 __owur int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len,
1062                                            unsigned char **bytes);
1063
1064 /*
1065  * Write a QUIC variable-length integer to the packet.
1066  */
1067 __owur int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v);
1068
1069 # endif
1070
1071 #endif                          /* OSSL_INTERNAL_PACKET_H */