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>
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 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 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 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 inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf,
113 /* Sanity check for negative values. */
114 if (len > (size_t)(SIZE_MAX / 2))
118 pkt->remaining = len;
122 /* Initialize a PACKET to hold zero bytes. */
123 static inline void PACKET_null_init(PACKET *pkt)
130 * Returns 1 if the packet has length |num| and its contents equal the |num|
131 * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
132 * If lengths are equal, performs the comparison in constant time.
134 __owur static inline int PACKET_equal(const PACKET *pkt, const void *ptr,
136 if (PACKET_remaining(pkt) != num)
138 return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
142 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
143 * Data is not copied: the |subpkt| packet will share its underlying buffer with
144 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
146 __owur static inline int PACKET_peek_sub_packet(const PACKET *pkt,
147 PACKET *subpkt, size_t len)
149 if (PACKET_remaining(pkt) < len)
152 return PACKET_buf_init(subpkt, pkt->curr, len);
156 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
157 * copied: the |subpkt| packet will share its underlying buffer with the
158 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
160 __owur static inline int PACKET_get_sub_packet(PACKET *pkt, PACKET *subpkt,
163 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
166 packet_forward(pkt, len);
171 /* Peek ahead at 2 bytes in network order from |pkt| and store the value in
174 __owur static inline int PACKET_peek_net_2(const PACKET *pkt,
177 if (PACKET_remaining(pkt) < 2)
180 *data = ((unsigned int)(*pkt->curr)) << 8;
181 *data |= *(pkt->curr + 1);
186 /* Equivalent of n2s */
187 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
188 __owur static inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
190 if (!PACKET_peek_net_2(pkt, data))
193 packet_forward(pkt, 2);
198 /* Peek ahead at 3 bytes in network order from |pkt| and store the value in
201 __owur static inline int PACKET_peek_net_3(const PACKET *pkt,
204 if (PACKET_remaining(pkt) < 3)
207 *data = ((unsigned long)(*pkt->curr)) << 16;
208 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
209 *data |= *(pkt->curr + 2);
214 /* Equivalent of n2l3 */
215 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
216 __owur static inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
218 if (!PACKET_peek_net_3(pkt, data))
221 packet_forward(pkt, 3);
226 /* Peek ahead at 4 bytes in network order from |pkt| and store the value in
229 __owur static inline int PACKET_peek_net_4(const PACKET *pkt,
232 if (PACKET_remaining(pkt) < 4)
235 *data = ((unsigned long)(*pkt->curr)) << 24;
236 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
237 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
238 *data |= *(pkt->curr+3);
243 /* Equivalent of n2l */
244 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
245 __owur static inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
247 if (!PACKET_peek_net_4(pkt, data))
250 packet_forward(pkt, 4);
255 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
256 __owur static inline int PACKET_peek_1(const PACKET *pkt, unsigned int *data)
258 if (!PACKET_remaining(pkt))
266 /* Get 1 byte from |pkt| and store the value in |*data| */
267 __owur static inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
269 if (!PACKET_peek_1(pkt, data))
272 packet_forward(pkt, 1);
278 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
281 __owur static inline int PACKET_peek_4(const PACKET *pkt, unsigned long *data)
283 if (PACKET_remaining(pkt) < 4)
287 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
288 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
289 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
294 /* Equivalent of c2l */
296 * Get 4 bytes in reverse network order from |pkt| and store the value in
299 __owur static inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
301 if (!PACKET_peek_4(pkt, data))
304 packet_forward(pkt, 4);
310 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
311 * |*data|. This just points at the underlying buffer that |pkt| is using. The
312 * caller should not free this data directly (it will be freed when the
313 * underlying buffer gets freed
315 __owur static inline int PACKET_peek_bytes(const PACKET *pkt, unsigned char **data,
318 if (PACKET_remaining(pkt) < len)
327 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
328 * just points at the underlying buffer that |pkt| is using. The caller should
329 * not free this data directly (it will be freed when the underlying buffer gets
332 __owur static inline int PACKET_get_bytes(PACKET *pkt, unsigned char **data,
335 if (!PACKET_peek_bytes(pkt, data, len))
338 packet_forward(pkt, len);
343 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
344 __owur static inline int PACKET_peek_copy_bytes(const PACKET *pkt,
345 unsigned char *data, size_t len)
347 if (PACKET_remaining(pkt) < len)
350 memcpy(data, pkt->curr, len);
356 * Read |len| bytes from |pkt| and copy them to |data|.
357 * The caller is responsible for ensuring that |data| can hold |len| bytes.
359 __owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data,
362 if (!PACKET_peek_copy_bytes(pkt, data, len))
365 packet_forward(pkt, len);
371 * Copy packet data to |dest|, and set |len| to the number of copied bytes.
372 * If the packet has more than |dest_len| bytes, nothing is copied.
373 * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
374 * Does not forward PACKET position (because it is typically the last thing
375 * done with a given PACKET).
377 __owur static inline int PACKET_copy_all(const PACKET *pkt, unsigned char *dest,
378 size_t dest_len, size_t *len) {
379 if (PACKET_remaining(pkt) > dest_len) {
383 *len = pkt->remaining;
384 memcpy(dest, pkt->curr, pkt->remaining);
389 * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
390 * result in |*data|, and the length in |len|.
391 * If |*data| is not NULL, the old data is OPENSSL_free'd.
392 * If the packet is empty, or malloc fails, |*data| will be set to NULL.
393 * Returns 1 if the malloc succeeds and 0 otherwise.
394 * Does not forward PACKET position (because it is typically the last thing
395 * done with a given PACKET).
397 __owur static inline int PACKET_memdup(const PACKET *pkt, unsigned char **data,
406 length = PACKET_remaining(pkt);
411 *data = OPENSSL_memdup(pkt->curr, length);
420 * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
421 * buffer. Store a pointer to the result in |*data|.
422 * If |*data| is not NULL, the old data is OPENSSL_free'd.
423 * If the data in |pkt| does not contain a NUL-byte, the entire data is
424 * copied and NUL-terminated.
425 * Returns 1 if the malloc succeeds and 0 otherwise.
426 * Does not forward PACKET position (because it is typically the last thing done
427 * with a given PACKET).
429 __owur static inline int PACKET_strndup(const PACKET *pkt, char **data)
433 /* This will succeed on an empty packet, unless pkt->curr == NULL. */
434 *data = OPENSSL_strndup((const char*)pkt->curr, PACKET_remaining(pkt));
435 return (*data != NULL);
438 /* Move the current reading position forward |len| bytes */
439 __owur static inline int PACKET_forward(PACKET *pkt, size_t len)
441 if (PACKET_remaining(pkt) < len)
444 packet_forward(pkt, len);
450 * Reads a variable-length vector prefixed with a one-byte length, and stores
451 * the contents in |subpkt|. |pkt| can equal |subpkt|.
452 * Data is not copied: the |subpkt| packet will share its underlying buffer with
453 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
454 * Upon failure, the original |pkt| and |subpkt| are not modified.
456 __owur static inline int PACKET_get_length_prefixed_1(PACKET *pkt, PACKET *subpkt)
461 if (!PACKET_get_1(&tmp, &length) ||
462 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
468 subpkt->remaining = length;
474 * Reads a variable-length vector prefixed with a two-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.
480 __owur static inline int PACKET_get_length_prefixed_2(PACKET *pkt, PACKET *subpkt)
485 if (!PACKET_get_net_2(&tmp, &length) ||
486 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
492 subpkt->remaining = length;
498 * Reads a variable-length vector prefixed with a three-byte length, and stores
499 * the contents in |subpkt|. |pkt| can equal |subpkt|.
500 * Data is not copied: the |subpkt| packet will share its underlying buffer with
501 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
502 * Upon failure, the original |pkt| and |subpkt| are not modified.
504 __owur static inline int PACKET_get_length_prefixed_3(PACKET *pkt, PACKET *subpkt)
506 unsigned long length;
509 if (!PACKET_get_net_3(&tmp, &length) ||
510 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
516 subpkt->remaining = length;
524 #endif /* HEADER_PACKET_LOCL_H */