2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef HEADER_PACKET_LOCL_H
11 # define HEADER_PACKET_LOCL_H
14 # include <openssl/bn.h>
15 # include <openssl/buffer.h>
16 # include <openssl/crypto.h>
17 # include <openssl/e_os2.h>
19 # include "internal/numbers.h"
26 /* Pointer to where we are currently reading from */
27 const unsigned char *curr;
28 /* Number of bytes remaining */
32 /* Internal unchecked shorthand; don't use outside this file. */
33 static ossl_inline void packet_forward(PACKET *pkt, size_t len)
36 pkt->remaining -= len;
40 * Returns the number of bytes remaining to be read in the PACKET
42 static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
44 return pkt->remaining;
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.
53 static ossl_inline const unsigned char *PACKET_end(const PACKET *pkt)
55 return pkt->curr + pkt->remaining;
58 * Returns a pointer to the PACKET's current position.
59 * For use in non-PACKETized APIs.
61 static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt)
67 * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
68 * copy of the data so |buf| must be present for the whole time that the PACKET
71 __owur static ossl_inline int PACKET_buf_init(PACKET *pkt,
72 const unsigned char *buf,
75 /* Sanity check for negative values. */
76 if (len > (size_t)(SIZE_MAX / 2))
84 /* Initialize a PACKET to hold zero bytes. */
85 static ossl_inline void PACKET_null_init(PACKET *pkt)
92 * Returns 1 if the packet has length |num| and its contents equal the |num|
93 * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
94 * If lengths are equal, performs the comparison in constant time.
96 __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
99 if (PACKET_remaining(pkt) != num)
101 return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
105 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
106 * Data is not copied: the |subpkt| packet will share its underlying buffer with
107 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
109 __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
113 if (PACKET_remaining(pkt) < len)
116 return PACKET_buf_init(subpkt, pkt->curr, len);
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|.
124 __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
128 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
131 packet_forward(pkt, len);
137 * Peek ahead at 2 bytes in network order from |pkt| and store the value in
140 __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
143 if (PACKET_remaining(pkt) < 2)
146 *data = ((unsigned int)(*pkt->curr)) << 8;
147 *data |= *(pkt->curr + 1);
152 /* Equivalent of n2s */
153 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
154 __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt,
157 if (!PACKET_peek_net_2(pkt, data))
160 packet_forward(pkt, 2);
166 * Peek ahead at 3 bytes in network order from |pkt| and store the value in
169 __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
172 if (PACKET_remaining(pkt) < 3)
175 *data = ((unsigned long)(*pkt->curr)) << 16;
176 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
177 *data |= *(pkt->curr + 2);
182 /* Equivalent of n2l3 */
183 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
184 __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt,
187 if (!PACKET_peek_net_3(pkt, data))
190 packet_forward(pkt, 3);
196 * Peek ahead at 4 bytes in network order from |pkt| and store the value in
199 __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
202 if (PACKET_remaining(pkt) < 4)
205 *data = ((unsigned long)(*pkt->curr)) << 24;
206 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
207 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
208 *data |= *(pkt->curr + 3);
213 /* Equivalent of n2l */
214 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
215 __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt,
218 if (!PACKET_peek_net_4(pkt, data))
221 packet_forward(pkt, 4);
226 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
227 __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
230 if (!PACKET_remaining(pkt))
238 /* Get 1 byte from |pkt| and store the value in |*data| */
239 __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
241 if (!PACKET_peek_1(pkt, data))
244 packet_forward(pkt, 1);
250 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
253 __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
256 if (PACKET_remaining(pkt) < 4)
260 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
261 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
262 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
267 /* Equivalent of c2l */
269 * Get 4 bytes in reverse network order from |pkt| and store the value in
272 __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
274 if (!PACKET_peek_4(pkt, data))
277 packet_forward(pkt, 4);
283 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
284 * |*data|. This just points at the underlying buffer that |pkt| is using. The
285 * caller should not free this data directly (it will be freed when the
286 * underlying buffer gets freed
288 __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
289 const unsigned char **data,
292 if (PACKET_remaining(pkt) < len)
301 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
302 * just points at the underlying buffer that |pkt| is using. The caller should
303 * not free this data directly (it will be freed when the underlying buffer gets
306 __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
307 const unsigned char **data,
310 if (!PACKET_peek_bytes(pkt, data, len))
313 packet_forward(pkt, len);
318 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
319 __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
323 if (PACKET_remaining(pkt) < len)
326 memcpy(data, pkt->curr, len);
332 * Read |len| bytes from |pkt| and copy them to |data|.
333 * The caller is responsible for ensuring that |data| can hold |len| bytes.
335 __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
339 if (!PACKET_peek_copy_bytes(pkt, data, len))
342 packet_forward(pkt, len);
348 * Copy packet data to |dest|, and set |len| to the number of copied bytes.
349 * If the packet has more than |dest_len| bytes, nothing is copied.
350 * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
351 * Does not forward PACKET position (because it is typically the last thing
352 * done with a given PACKET).
354 __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
356 size_t dest_len, size_t *len)
358 if (PACKET_remaining(pkt) > dest_len) {
362 *len = pkt->remaining;
363 memcpy(dest, pkt->curr, pkt->remaining);
368 * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
369 * result in |*data|, and the length in |len|.
370 * If |*data| is not NULL, the old data is OPENSSL_free'd.
371 * If the packet is empty, or malloc fails, |*data| will be set to NULL.
372 * Returns 1 if the malloc succeeds and 0 otherwise.
373 * Does not forward PACKET position (because it is typically the last thing
374 * done with a given PACKET).
376 __owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
377 unsigned char **data, size_t *len)
385 length = PACKET_remaining(pkt);
390 *data = OPENSSL_memdup(pkt->curr, length);
399 * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
400 * buffer. Store a pointer to the result in |*data|.
401 * If |*data| is not NULL, the old data is OPENSSL_free'd.
402 * If the data in |pkt| does not contain a NUL-byte, the entire data is
403 * copied and NUL-terminated.
404 * Returns 1 if the malloc succeeds and 0 otherwise.
405 * Does not forward PACKET position (because it is typically the last thing done
406 * with a given PACKET).
408 __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
412 /* This will succeed on an empty packet, unless pkt->curr == NULL. */
413 *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
414 return (*data != NULL);
417 /* Returns 1 if |pkt| contains at least one 0-byte, 0 otherwise. */
418 static ossl_inline int PACKET_contains_zero_byte(const PACKET *pkt)
420 return memchr(pkt->curr, 0, pkt->remaining) != NULL;
423 /* Move the current reading position forward |len| bytes */
424 __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
426 if (PACKET_remaining(pkt) < len)
429 packet_forward(pkt, len);
435 * Reads a variable-length vector prefixed with a one-byte length, and stores
436 * the contents in |subpkt|. |pkt| can equal |subpkt|.
437 * Data is not copied: the |subpkt| packet will share its underlying buffer with
438 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
439 * Upon failure, the original |pkt| and |subpkt| are not modified.
441 __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
445 const unsigned char *data;
447 if (!PACKET_get_1(&tmp, &length) ||
448 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
454 subpkt->remaining = length;
460 * Like PACKET_get_length_prefixed_1, but additionally, fails when there are
461 * leftover bytes in |pkt|.
463 __owur static ossl_inline int PACKET_as_length_prefixed_1(PACKET *pkt, PACKET *subpkt)
466 const unsigned char *data;
468 if (!PACKET_get_1(&tmp, &length) ||
469 !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
470 PACKET_remaining(&tmp) != 0) {
476 subpkt->remaining = length;
482 * Reads a variable-length vector prefixed with a two-byte length, and stores
483 * the contents in |subpkt|. |pkt| can equal |subpkt|.
484 * Data is not copied: the |subpkt| packet will share its underlying buffer with
485 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
486 * Upon failure, the original |pkt| and |subpkt| are not modified.
488 __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
492 const unsigned char *data;
495 if (!PACKET_get_net_2(&tmp, &length) ||
496 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
502 subpkt->remaining = length;
508 * Like PACKET_get_length_prefixed_2, but additionally, fails when there are
509 * leftover bytes in |pkt|.
511 __owur static ossl_inline int PACKET_as_length_prefixed_2(PACKET *pkt,
515 const unsigned char *data;
518 if (!PACKET_get_net_2(&tmp, &length) ||
519 !PACKET_get_bytes(&tmp, &data, (size_t)length) ||
520 PACKET_remaining(&tmp) != 0) {
526 subpkt->remaining = length;
532 * Reads a variable-length vector prefixed with a three-byte length, and stores
533 * the contents in |subpkt|. |pkt| can equal |subpkt|.
534 * Data is not copied: the |subpkt| packet will share its underlying buffer with
535 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
536 * Upon failure, the original |pkt| and |subpkt| are not modified.
538 __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
541 unsigned long length;
542 const unsigned char *data;
544 if (!PACKET_get_net_3(&tmp, &length) ||
545 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
551 subpkt->remaining = length;
559 #endif /* HEADER_PACKET_LOCL_H */