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>
72 /* Pointer to the start of the buffer data */
75 /* Pointer to the first byte after the end of the buffer data */
78 /* Pointer to where we are currently reading from */
83 * Returns the number of bytes remaining to be read in the PACKET
85 __owur static inline size_t PACKET_remaining(const PACKET *pkt)
87 return (size_t)(pkt->end - pkt->curr);
91 * Returns a pointer to the PACKET's current position.
92 * For use in non-PACKETized APIs.
93 * TODO(openssl-team): this should return 'const unsigned char*' but can't
94 * currently because legacy code passes 'unsigned char*'s around.
96 static inline unsigned char *PACKET_data(const PACKET *pkt)
102 * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
103 * copy of the data so |buf| must be present for the whole time that the PACKET
106 static inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf, size_t len)
108 pkt->start = pkt->curr = buf;
109 pkt->end = pkt->start + len;
112 if (pkt->start > pkt->end
113 || pkt->curr < pkt->start
114 || pkt->curr > pkt->end
115 || len != (size_t)(pkt->end - pkt->start)) {
123 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
124 * Data is not copied: the |subpkt| packet will share its underlying buffer with
125 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
127 __owur static inline int PACKET_peek_sub_packet(const PACKET *pkt,
128 PACKET *subpkt, size_t len)
130 if (PACKET_remaining(pkt) < len)
133 PACKET_buf_init(subpkt, pkt->curr, len);
139 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
140 * copied: the |subpkt| packet will share its underlying buffer with the
141 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
143 __owur static inline int PACKET_get_sub_packet(PACKET *pkt, PACKET *subpkt,
146 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
154 /* Peek ahead at 2 bytes in network order from |pkt| and store the value in
157 __owur static inline int PACKET_peek_net_2(const PACKET *pkt,
160 if (PACKET_remaining(pkt) < 2)
163 *data = ((unsigned int)(*pkt->curr)) << 8;
164 *data |= *(pkt->curr + 1);
169 /* Equivalent of n2s */
170 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
171 __owur static inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
173 if (!PACKET_peek_net_2(pkt, data))
181 /* Peek ahead at 3 bytes in network order from |pkt| and store the value in
184 __owur static inline int PACKET_peek_net_3(const PACKET *pkt,
187 if (PACKET_remaining(pkt) < 3)
190 *data = ((unsigned long)(*pkt->curr)) << 16;
191 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
192 *data |= *(pkt->curr + 2);
197 /* Equivalent of n2l3 */
198 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
199 __owur static inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
201 if (!PACKET_peek_net_3(pkt, data))
209 /* Peek ahead at 4 bytes in network order from |pkt| and store the value in
212 __owur static inline int PACKET_peek_net_4(const PACKET *pkt,
215 if (PACKET_remaining(pkt) < 4)
218 *data = ((unsigned long)(*pkt->curr)) << 24;
219 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
220 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
221 *data |= *(pkt->curr+3);
226 /* Equivalent of n2l */
227 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
228 __owur static inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
230 if (!PACKET_peek_net_4(pkt, data))
238 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
239 __owur static inline int PACKET_peek_1(const PACKET *pkt, unsigned int *data)
241 if (!PACKET_remaining(pkt))
249 /* Get 1 byte from |pkt| and store the value in |*data| */
250 __owur static inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
252 if (!PACKET_peek_1(pkt, data))
261 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
264 __owur static inline int PACKET_peek_4(const PACKET *pkt, unsigned long *data)
266 if (PACKET_remaining(pkt) < 4)
270 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
271 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
272 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
277 /* Equivalent of c2l */
279 * Get 4 bytes in reverse network order from |pkt| and store the value in
282 __owur static inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
284 if (!PACKET_peek_4(pkt, data))
293 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
294 * |*data|. This just points at the underlying buffer that |pkt| is using. The
295 * caller should not free this data directly (it will be freed when the
296 * underlying buffer gets freed
298 __owur static inline int PACKET_peek_bytes(const PACKET *pkt, unsigned char **data,
301 if (PACKET_remaining(pkt) < len)
310 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
311 * just points at the underlying buffer that |pkt| is using. The caller should
312 * not free this data directly (it will be freed when the underlying buffer gets
315 __owur static inline int PACKET_get_bytes(PACKET *pkt, unsigned char **data,
318 if (!PACKET_peek_bytes(pkt, data, len))
326 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
327 __owur static inline int PACKET_peek_copy_bytes(const PACKET *pkt,
328 unsigned char *data, size_t len)
330 if (PACKET_remaining(pkt) < len)
333 memcpy(data, pkt->curr, len);
339 * Read |len| bytes from |pkt| and copy them to |data|.
340 * The caller is responsible for ensuring that |data| can hold |len| bytes.
342 __owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data,
345 if (!PACKET_peek_copy_bytes(pkt, data, len))
354 * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
355 * result in |*data|, and the length in |len|.
356 * If |*data| is not NULL, the old data is OPENSSL_free'd.
357 * If the packet is empty, or malloc fails, |*data| will be set to NULL.
358 * Returns 1 if the malloc succeeds and 0 otherwise.
359 * Does not forward PACKET position (because it is typically the last thing
360 * done with a given PACKET).
362 __owur static inline int PACKET_memdup(const PACKET *pkt, unsigned char **data,
371 length = PACKET_remaining(pkt);
376 *data = BUF_memdup(pkt->curr, length);
386 * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
387 * buffer. Store a pointer to the result in |*data|.
388 * If |*data| is not NULL, the old data is OPENSSL_free'd.
389 * If the data in |pkt| does not contain a NUL-byte, the entire data is
390 * copied and NUL-terminated.
391 * Returns 1 if the malloc succeeds and 0 otherwise.
392 * Does not forward PACKET position (because it is typically the last thing done
393 * with a given PACKET).
395 __owur static inline int PACKET_strndup(const PACKET *pkt, char **data)
398 *data = BUF_strndup((const char*)pkt->curr, PACKET_remaining(pkt));
399 return (*data != NULL);
402 /* Move the current reading position forward |len| bytes */
403 __owur static inline int PACKET_forward(PACKET *pkt, size_t len)
405 if (PACKET_remaining(pkt) < len)
414 * Stores the total length of the packet we have in the underlying buffer in
417 __owur static inline int PACKET_length(const PACKET *pkt, size_t *len)
419 *len = pkt->end - pkt->start;
425 * Reads a variable-length vector prefixed with a one-byte length, and stores
426 * the contents in |subpkt|. |pkt| can equal |subpkt|.
427 * Data is not copied: the |subpkt| packet will share its underlying buffer with
428 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
429 * Upon failure, the original |pkt| and |subpkt| are not modified.
431 __owur static inline int PACKET_get_length_prefixed_1(PACKET *pkt, PACKET *subpkt)
436 if (!PACKET_get_1(&tmp, &length) ||
437 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
442 subpkt->start = subpkt->curr = data;
443 subpkt->end = subpkt->start + length;
449 * Reads a variable-length vector prefixed with a two-byte length, and stores
450 * the contents in |subpkt|. |pkt| can equal |subpkt|.
451 * Data is not copied: the |subpkt| packet will share its underlying buffer with
452 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
453 * Upon failure, the original |pkt| and |subpkt| are not modified.
455 __owur static inline int PACKET_get_length_prefixed_2(PACKET *pkt, PACKET *subpkt)
460 if (!PACKET_get_net_2(&tmp, &length) ||
461 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
466 subpkt->start = subpkt->curr = data;
467 subpkt->end = subpkt->start + length;
473 * Reads a variable-length vector prefixed with a three-byte length, and stores
474 * the contents in |subpkt|. |pkt| can equal |subpkt|.
475 * Data is not copied: the |subpkt| packet will share its underlying buffer with
476 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
477 * Upon failure, the original |pkt| and |subpkt| are not modified.
479 __owur static inline int PACKET_get_length_prefixed_3(PACKET *pkt, PACKET *subpkt)
481 unsigned long length;
484 if (!PACKET_get_net_3(&tmp, &length) ||
485 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
490 subpkt->start = subpkt->curr = data;
491 subpkt->end = subpkt->start + length;
499 #endif /* HEADER_PACKET_LOCL_H */