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