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