1 /* ssl/packet_locl.h */
3 * Written by Matt Caswell for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 #ifndef HEADER_PACKET_LOCL_H
60 # define HEADER_PACKET_LOCL_H
63 # include <openssl/bn.h>
64 # include <openssl/buffer.h>
65 # include <openssl/crypto.h>
66 # include <openssl/e_os2.h>
73 /* Pointer to where we are currently reading from */
75 /* Number of bytes remaining */
79 /* Internal unchecked shorthand; don't use outside this file. */
80 static ossl_inline void packet_forward(PACKET *pkt, size_t len)
83 pkt->remaining -= len;
87 * Returns the number of bytes remaining to be read in the PACKET
89 static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
91 return pkt->remaining;
95 * Returns a pointer to the PACKET's current position.
96 * For use in non-PACKETized APIs.
97 * TODO(openssl-team): this should return 'const unsigned char*' but can't
98 * currently because legacy code passes 'unsigned char*'s around.
100 static ossl_inline unsigned char *PACKET_data(const PACKET *pkt)
106 * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
107 * copy of the data so |buf| must be present for the whole time that the PACKET
110 __owur static ossl_inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf,
113 /* Sanity check for negative values. */
115 if (len > (size_t)(SIZE_MAX / 2))
118 if (len > ((size_t)2 << (sizeof(size_t) * 8 - 1)))
123 pkt->remaining = len;
127 /* Initialize a PACKET to hold zero bytes. */
128 static ossl_inline void PACKET_null_init(PACKET *pkt)
135 * Returns 1 if the packet has length |num| and its contents equal the |num|
136 * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
137 * If lengths are equal, performs the comparison in constant time.
139 __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
142 if (PACKET_remaining(pkt) != num)
144 return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
148 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
149 * Data is not copied: the |subpkt| packet will share its underlying buffer with
150 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
152 __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
156 if (PACKET_remaining(pkt) < len)
159 return PACKET_buf_init(subpkt, pkt->curr, len);
163 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
164 * copied: the |subpkt| packet will share its underlying buffer with the
165 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
167 __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
171 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
174 packet_forward(pkt, len);
180 * Peek ahead at 2 bytes in network order from |pkt| and store the value in
183 __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
186 if (PACKET_remaining(pkt) < 2)
189 *data = ((unsigned int)(*pkt->curr)) << 8;
190 *data |= *(pkt->curr + 1);
195 /* Equivalent of n2s */
196 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
197 __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt,
200 if (!PACKET_peek_net_2(pkt, data))
203 packet_forward(pkt, 2);
209 * Peek ahead at 3 bytes in network order from |pkt| and store the value in
212 __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
215 if (PACKET_remaining(pkt) < 3)
218 *data = ((unsigned long)(*pkt->curr)) << 16;
219 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
220 *data |= *(pkt->curr + 2);
225 /* Equivalent of n2l3 */
226 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
227 __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt,
230 if (!PACKET_peek_net_3(pkt, data))
233 packet_forward(pkt, 3);
239 * Peek ahead at 4 bytes in network order from |pkt| and store the value in
242 __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
245 if (PACKET_remaining(pkt) < 4)
248 *data = ((unsigned long)(*pkt->curr)) << 24;
249 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
250 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
251 *data |= *(pkt->curr + 3);
256 /* Equivalent of n2l */
257 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
258 __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt,
261 if (!PACKET_peek_net_4(pkt, data))
264 packet_forward(pkt, 4);
269 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
270 __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
273 if (!PACKET_remaining(pkt))
281 /* Get 1 byte from |pkt| and store the value in |*data| */
282 __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
284 if (!PACKET_peek_1(pkt, data))
287 packet_forward(pkt, 1);
293 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
296 __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
299 if (PACKET_remaining(pkt) < 4)
303 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
304 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
305 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
310 /* Equivalent of c2l */
312 * Get 4 bytes in reverse network order from |pkt| and store the value in
315 __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
317 if (!PACKET_peek_4(pkt, data))
320 packet_forward(pkt, 4);
326 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
327 * |*data|. This just points at the underlying buffer that |pkt| is using. The
328 * caller should not free this data directly (it will be freed when the
329 * underlying buffer gets freed
331 __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
332 unsigned char **data,
335 if (PACKET_remaining(pkt) < len)
344 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
345 * just points at the underlying buffer that |pkt| is using. The caller should
346 * not free this data directly (it will be freed when the underlying buffer gets
349 __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
350 unsigned char **data,
353 if (!PACKET_peek_bytes(pkt, data, len))
356 packet_forward(pkt, len);
361 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
362 __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
366 if (PACKET_remaining(pkt) < len)
369 memcpy(data, pkt->curr, len);
375 * Read |len| bytes from |pkt| and copy them to |data|.
376 * The caller is responsible for ensuring that |data| can hold |len| bytes.
378 __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
382 if (!PACKET_peek_copy_bytes(pkt, data, len))
385 packet_forward(pkt, len);
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).
397 __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
399 size_t dest_len, size_t *len)
401 if (PACKET_remaining(pkt) > dest_len) {
405 *len = pkt->remaining;
406 memcpy(dest, pkt->curr, pkt->remaining);
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).
419 __owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
420 unsigned char **data, size_t *len)
428 length = PACKET_remaining(pkt);
433 *data = OPENSSL_memdup(pkt->curr, length);
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).
451 __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
455 /* This will succeed on an empty packet, unless pkt->curr == NULL. */
456 *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
457 return (*data != NULL);
460 /* Move the current reading position forward |len| bytes */
461 __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
463 if (PACKET_remaining(pkt) < len)
466 packet_forward(pkt, len);
472 * Reads a variable-length vector prefixed with a one-byte length, and stores
473 * the contents in |subpkt|. |pkt| can equal |subpkt|.
474 * Data is not copied: the |subpkt| packet will share its underlying buffer with
475 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
476 * Upon failure, the original |pkt| and |subpkt| are not modified.
478 __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
484 if (!PACKET_get_1(&tmp, &length) ||
485 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
491 subpkt->remaining = length;
497 * Reads a variable-length vector prefixed with a two-byte length, and stores
498 * the contents in |subpkt|. |pkt| can equal |subpkt|.
499 * Data is not copied: the |subpkt| packet will share its underlying buffer with
500 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
501 * Upon failure, the original |pkt| and |subpkt| are not modified.
503 __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
509 if (!PACKET_get_net_2(&tmp, &length) ||
510 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
516 subpkt->remaining = length;
522 * Reads a variable-length vector prefixed with a three-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.
528 __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
531 unsigned long length;
534 if (!PACKET_get_net_3(&tmp, &length) ||
535 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
541 subpkt->remaining = length;
549 #endif /* HEADER_PACKET_LOCL_H */