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 where we are currently reading from */
74 /* Number of bytes remaining */
78 /* Internal unchecked shorthand; don't use outside this file. */
79 static inline void packet_forward(PACKET *pkt, size_t len)
82 pkt->remaining -= len;
86 * Returns the number of bytes remaining to be read in the PACKET
88 __owur static inline size_t PACKET_remaining(const PACKET *pkt)
90 return pkt->remaining;
94 * Returns a pointer to the PACKET's current position.
95 * For use in non-PACKETized APIs.
96 * TODO(openssl-team): this should return 'const unsigned char*' but can't
97 * currently because legacy code passes 'unsigned char*'s around.
99 static inline unsigned char *PACKET_data(const PACKET *pkt)
105 * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
106 * copy of the data so |buf| must be present for the whole time that the PACKET
109 static inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf, size_t len)
111 /* Sanity check for negative values. */
116 pkt->remaining = len;
120 /* Initialize a PACKET to hold zero bytes. */
121 static inline void PACKET_null_init(PACKET *pkt)
128 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
129 * Data is not copied: the |subpkt| packet will share its underlying buffer with
130 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
132 __owur static inline int PACKET_peek_sub_packet(const PACKET *pkt,
133 PACKET *subpkt, size_t len)
135 if (PACKET_remaining(pkt) < len)
138 PACKET_buf_init(subpkt, pkt->curr, len);
144 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
145 * copied: the |subpkt| packet will share its underlying buffer with the
146 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
148 __owur static inline int PACKET_get_sub_packet(PACKET *pkt, PACKET *subpkt,
151 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
154 packet_forward(pkt, len);
159 /* Peek ahead at 2 bytes in network order from |pkt| and store the value in
162 __owur static inline int PACKET_peek_net_2(const PACKET *pkt,
165 if (PACKET_remaining(pkt) < 2)
168 *data = ((unsigned int)(*pkt->curr)) << 8;
169 *data |= *(pkt->curr + 1);
174 /* Equivalent of n2s */
175 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
176 __owur static inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
178 if (!PACKET_peek_net_2(pkt, data))
181 packet_forward(pkt, 2);
186 /* Peek ahead at 3 bytes in network order from |pkt| and store the value in
189 __owur static inline int PACKET_peek_net_3(const PACKET *pkt,
192 if (PACKET_remaining(pkt) < 3)
195 *data = ((unsigned long)(*pkt->curr)) << 16;
196 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
197 *data |= *(pkt->curr + 2);
202 /* Equivalent of n2l3 */
203 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
204 __owur static inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
206 if (!PACKET_peek_net_3(pkt, data))
209 packet_forward(pkt, 3);
214 /* Peek ahead at 4 bytes in network order from |pkt| and store the value in
217 __owur static inline int PACKET_peek_net_4(const PACKET *pkt,
220 if (PACKET_remaining(pkt) < 4)
223 *data = ((unsigned long)(*pkt->curr)) << 24;
224 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
225 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
226 *data |= *(pkt->curr+3);
231 /* Equivalent of n2l */
232 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
233 __owur static inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
235 if (!PACKET_peek_net_4(pkt, data))
238 packet_forward(pkt, 4);
243 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
244 __owur static inline int PACKET_peek_1(const PACKET *pkt, unsigned int *data)
246 if (!PACKET_remaining(pkt))
254 /* Get 1 byte from |pkt| and store the value in |*data| */
255 __owur static inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
257 if (!PACKET_peek_1(pkt, data))
260 packet_forward(pkt, 1);
266 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
269 __owur static inline int PACKET_peek_4(const PACKET *pkt, unsigned long *data)
271 if (PACKET_remaining(pkt) < 4)
275 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
276 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
277 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
282 /* Equivalent of c2l */
284 * Get 4 bytes in reverse network order from |pkt| and store the value in
287 __owur static inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
289 if (!PACKET_peek_4(pkt, data))
292 packet_forward(pkt, 4);
298 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
299 * |*data|. This just points at the underlying buffer that |pkt| is using. The
300 * caller should not free this data directly (it will be freed when the
301 * underlying buffer gets freed
303 __owur static inline int PACKET_peek_bytes(const PACKET *pkt, unsigned char **data,
306 if (PACKET_remaining(pkt) < len)
315 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
316 * just points at the underlying buffer that |pkt| is using. The caller should
317 * not free this data directly (it will be freed when the underlying buffer gets
320 __owur static inline int PACKET_get_bytes(PACKET *pkt, unsigned char **data,
323 if (!PACKET_peek_bytes(pkt, data, len))
326 packet_forward(pkt, len);
331 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
332 __owur static inline int PACKET_peek_copy_bytes(const PACKET *pkt,
333 unsigned char *data, size_t len)
335 if (PACKET_remaining(pkt) < len)
338 memcpy(data, pkt->curr, len);
344 * Read |len| bytes from |pkt| and copy them to |data|.
345 * The caller is responsible for ensuring that |data| can hold |len| bytes.
347 __owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data,
350 if (!PACKET_peek_copy_bytes(pkt, data, len))
353 packet_forward(pkt, len);
359 * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
360 * result in |*data|, and the length in |len|.
361 * If |*data| is not NULL, the old data is OPENSSL_free'd.
362 * If the packet is empty, or malloc fails, |*data| will be set to NULL.
363 * Returns 1 if the malloc succeeds and 0 otherwise.
364 * Does not forward PACKET position (because it is typically the last thing
365 * done with a given PACKET).
367 __owur static inline int PACKET_memdup(const PACKET *pkt, unsigned char **data,
376 length = PACKET_remaining(pkt);
381 *data = BUF_memdup(pkt->curr, length);
391 * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
392 * buffer. Store a pointer to the result in |*data|.
393 * If |*data| is not NULL, the old data is OPENSSL_free'd.
394 * If the data in |pkt| does not contain a NUL-byte, the entire data is
395 * copied and NUL-terminated.
396 * Returns 1 if the malloc succeeds and 0 otherwise.
397 * Does not forward PACKET position (because it is typically the last thing done
398 * with a given PACKET).
400 __owur static inline int PACKET_strndup(const PACKET *pkt, char **data)
403 *data = BUF_strndup((const char*)pkt->curr, PACKET_remaining(pkt));
404 return (*data != NULL);
407 /* Move the current reading position forward |len| bytes */
408 __owur static inline int PACKET_forward(PACKET *pkt, size_t len)
410 if (PACKET_remaining(pkt) < len)
413 packet_forward(pkt, len);
419 * Reads a variable-length vector prefixed with a one-byte length, and stores
420 * the contents in |subpkt|. |pkt| can equal |subpkt|.
421 * Data is not copied: the |subpkt| packet will share its underlying buffer with
422 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
423 * Upon failure, the original |pkt| and |subpkt| are not modified.
425 __owur static inline int PACKET_get_length_prefixed_1(PACKET *pkt, PACKET *subpkt)
430 if (!PACKET_get_1(&tmp, &length) ||
431 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
437 subpkt->remaining = length;
443 * Reads a variable-length vector prefixed with a two-byte length, and stores
444 * the contents in |subpkt|. |pkt| can equal |subpkt|.
445 * Data is not copied: the |subpkt| packet will share its underlying buffer with
446 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
447 * Upon failure, the original |pkt| and |subpkt| are not modified.
449 __owur static inline int PACKET_get_length_prefixed_2(PACKET *pkt, PACKET *subpkt)
454 if (!PACKET_get_net_2(&tmp, &length) ||
455 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
461 subpkt->remaining = length;
467 * Reads a variable-length vector prefixed with a three-byte length, and stores
468 * the contents in |subpkt|. |pkt| can equal |subpkt|.
469 * Data is not copied: the |subpkt| packet will share its underlying buffer with
470 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
471 * Upon failure, the original |pkt| and |subpkt| are not modified.
473 __owur static inline int PACKET_get_length_prefixed_3(PACKET *pkt, PACKET *subpkt)
475 unsigned long length;
478 if (!PACKET_get_net_3(&tmp, &length) ||
479 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
485 subpkt->remaining = length;
493 #endif /* HEADER_PACKET_LOCL_H */