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