]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/packet_locl.h
Restore last-resort expired untrusted intermediate issuers
[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
MC
701
702/*
703 * Initialise a new sub-packet. Additionally |lenbytes| of data is preallocated
869d0a37
MC
704 * at the start of the sub-packet to store its length once we know it. Don't
705 * call this directly. Use the convenience macros below instead.
9bf85bf9 706 */
869d0a37 707int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes);
9bf85bf9 708
de451856
MC
709/*
710 * Convenience macros for calling WPACKET_start_sub_packet_len with different
711 * lengths
712 */
713#define WPACKET_start_sub_packet_u8(pkt) \
869d0a37 714 WPACKET_start_sub_packet_len__((pkt), 1)
de451856 715#define WPACKET_start_sub_packet_u16(pkt) \
869d0a37 716 WPACKET_start_sub_packet_len__((pkt), 2)
de451856 717#define WPACKET_start_sub_packet_u24(pkt) \
869d0a37 718 WPACKET_start_sub_packet_len__((pkt), 3)
de451856 719#define WPACKET_start_sub_packet_u32(pkt) \
869d0a37 720 WPACKET_start_sub_packet_len__((pkt), 4)
de451856 721
9bf85bf9 722/*
869d0a37
MC
723 * Same as WPACKET_start_sub_packet_len__() except no bytes are pre-allocated
724 * for the sub-packet length.
9bf85bf9 725 */
0217dd19 726int WPACKET_start_sub_packet(WPACKET *pkt);
9bf85bf9
MC
727
728/*
729 * Allocate bytes in the WPACKET for the output. This reserves the bytes
730 * and counts them as "written", but doesn't actually do the writing. A pointer
731 * to the allocated bytes is stored in |*allocbytes|.
4b0fc9fc
MC
732 * WARNING: the allocated bytes must be filled in immediately, without further
733 * WPACKET_* calls. If not then the underlying buffer may be realloc'd and
734 * change its location.
9bf85bf9 735 */
1ff84340 736int WPACKET_allocate_bytes(WPACKET *pkt, size_t len,
b7273855 737 unsigned char **allocbytes);
9bf85bf9 738
b2b3024e
MC
739/*
740 * The same as WPACKET_allocate_bytes() except additionally a new sub-packet is
741 * started for the allocated bytes, and then closed immediately afterwards. The
869d0a37
MC
742 * number of length bytes for the sub-packet is in |lenbytes|. Don't call this
743 * directly. Use the convenience macros below instead.
b2b3024e 744 */
869d0a37
MC
745int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
746 unsigned char **allocbytes, size_t lenbytes);
b2b3024e
MC
747
748/*
749 * Convenience macros for calling WPACKET_sub_allocate_bytes with different
750 * lengths
751 */
752#define WPACKET_sub_allocate_bytes_u8(pkt, len, bytes) \
869d0a37 753 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 1)
b2b3024e 754#define WPACKET_sub_allocate_bytes_u16(pkt, len, bytes) \
869d0a37 755 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 2)
b2b3024e 756#define WPACKET_sub_allocate_bytes_u24(pkt, len, bytes) \
869d0a37 757 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 3)
b2b3024e 758#define WPACKET_sub_allocate_bytes_u32(pkt, len, bytes) \
869d0a37 759 WPACKET_sub_allocate_bytes__((pkt), (len), (bytes), 4)
b2b3024e 760
1ff84340
MC
761/*
762 * The same as WPACKET_allocate_bytes() except the reserved bytes are not
763 * actually counted as written. Typically this will be for when we don't know
764 * how big arbitrary data is going to be up front, but we do know what the
765 * maximum size will be. If this function is used, then it should be immediately
766 * followed by a WPACKET_allocate_bytes() call before any other WPACKET
767 * functions are called (unless the write to the allocated bytes is abandoned).
609b0852 768 *
0023baff
MC
769 * For example: If we are generating a signature, then the size of that
770 * signature may not be known in advance. We can use WPACKET_reserve_bytes() to
771 * handle this:
772 *
773 * if (!WPACKET_sub_reserve_bytes_u16(&pkt, EVP_PKEY_size(pkey), &sigbytes1)
774 * || EVP_SignFinal(md_ctx, sigbytes1, &siglen, pkey) <= 0
775 * || !WPACKET_sub_allocate_bytes_u16(&pkt, siglen, &sigbytes2)
776 * || sigbytes1 != sigbytes2)
777 * goto err;
1ff84340
MC
778 */
779int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes);
780
781/*
782 * The "reserve_bytes" equivalent of WPACKET_sub_allocate_bytes__()
783 */
784int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
785 unsigned char **allocbytes, size_t lenbytes);
786
787/*
788 * Convenience macros for WPACKET_sub_reserve_bytes with different lengths
789 */
790#define WPACKET_sub_reserve_bytes_u8(pkt, len, bytes) \
791 WPACKET_reserve_bytes__((pkt), (len), (bytes), 1)
792#define WPACKET_sub_reserve_bytes_u16(pkt, len, bytes) \
793 WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 2)
794#define WPACKET_sub_reserve_bytes_u24(pkt, len, bytes) \
795 WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 3)
796#define WPACKET_sub_reserve_bytes_u32(pkt, len, bytes) \
797 WPACKET_sub_reserve_bytes__((pkt), (len), (bytes), 4)
798
9bf85bf9
MC
799/*
800 * Write the value stored in |val| into the WPACKET. The value will consume
801 * |bytes| amount of storage. An error will occur if |val| cannot be
802 * accommodated in |bytes| storage, e.g. attempting to write the value 256 into
08029dfa
MC
803 * 1 byte will fail. Don't call this directly. Use the convenience macros below
804 * instead.
9bf85bf9 805 */
08029dfa
MC
806int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t bytes);
807
808/*
809 * Convenience macros for calling WPACKET_put_bytes with different
810 * lengths
811 */
812#define WPACKET_put_bytes_u8(pkt, val) \
813 WPACKET_put_bytes__((pkt), (val), 1)
814#define WPACKET_put_bytes_u16(pkt, val) \
815 WPACKET_put_bytes__((pkt), (val), 2)
816#define WPACKET_put_bytes_u24(pkt, val) \
4b0fc9fc 817 WPACKET_put_bytes__((pkt), (val), 3)
08029dfa 818#define WPACKET_put_bytes_u32(pkt, val) \
b36017fe 819 WPACKET_put_bytes__((pkt), (val), 4)
9bf85bf9
MC
820
821/* Set a maximum size that we will not allow the WPACKET to grow beyond */
ae2f7b37 822int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize);
9bf85bf9
MC
823
824/* Copy |len| bytes of data from |*src| into the WPACKET. */
ae2f7b37 825int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len);
9bf85bf9
MC
826
827/*
828 * Copy |len| bytes of data from |*src| into the WPACKET and prefix with its
869d0a37
MC
829 * length (consuming |lenbytes| of data for the length). Don't call this
830 * directly. Use the convenience macros below instead.
9bf85bf9 831 */
869d0a37 832int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
fb790f16 833 size_t lenbytes);
9bf85bf9 834
b2b3024e
MC
835/* Convenience macros for calling WPACKET_sub_memcpy with different lengths */
836#define WPACKET_sub_memcpy_u8(pkt, src, len) \
869d0a37 837 WPACKET_sub_memcpy__((pkt), (src), (len), 1)
b2b3024e 838#define WPACKET_sub_memcpy_u16(pkt, src, len) \
869d0a37 839 WPACKET_sub_memcpy__((pkt), (src), (len), 2)
f308416e 840#define WPACKET_sub_memcpy_u24(pkt, src, len) \
869d0a37 841 WPACKET_sub_memcpy__((pkt), (src), (len), 3)
f308416e 842#define WPACKET_sub_memcpy_u32(pkt, src, len) \
869d0a37 843 WPACKET_sub_memcpy__((pkt), (src), (len), 4)
b2b3024e 844
9bf85bf9
MC
845/*
846 * Return the total number of bytes written so far to the underlying buffer
847 * including any storage allocated for length bytes
848 */
ae2f7b37 849int WPACKET_get_total_written(WPACKET *pkt, size_t *written);
9bf85bf9
MC
850
851/*
852 * Returns the length of the current sub-packet. This excludes any bytes
853 * allocated for the length itself.
854 */
ae2f7b37 855int WPACKET_get_length(WPACKET *pkt, size_t *len);
9bf85bf9
MC
856
857/* Release resources in a WPACKET if a failure has occurred. */
871bc59b 858void WPACKET_cleanup(WPACKET *pkt);
b7273855 859
7e729bb5
MC
860# ifdef __cplusplus
861}
862# endif
863
80e0ecbf 864#endif /* HEADER_PACKET_LOCL_H */