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>
68 # include "internal/numbers.h"
75 /* Pointer to where we are currently reading from */
77 /* Number of bytes remaining */
81 /* Internal unchecked shorthand; don't use outside this file. */
82 static ossl_inline void packet_forward(PACKET *pkt, size_t len)
85 pkt->remaining -= len;
89 * Returns the number of bytes remaining to be read in the PACKET
91 static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
93 return pkt->remaining;
97 * Returns a pointer to the PACKET's current position.
98 * For use in non-PACKETized APIs.
99 * TODO(openssl-team): this should return 'const unsigned char*' but can't
100 * currently because legacy code passes 'unsigned char*'s around.
102 static ossl_inline unsigned char *PACKET_data(const PACKET *pkt)
108 * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
109 * copy of the data so |buf| must be present for the whole time that the PACKET
112 __owur static ossl_inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf,
115 /* Sanity check for negative values. */
116 if (len > (size_t)(SIZE_MAX / 2))
120 pkt->remaining = len;
124 /* Initialize a PACKET to hold zero bytes. */
125 static ossl_inline void PACKET_null_init(PACKET *pkt)
132 * Returns 1 if the packet has length |num| and its contents equal the |num|
133 * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
134 * If lengths are equal, performs the comparison in constant time.
136 __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
139 if (PACKET_remaining(pkt) != num)
141 return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
145 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
146 * Data is not copied: the |subpkt| packet will share its underlying buffer with
147 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
149 __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
153 if (PACKET_remaining(pkt) < len)
156 return PACKET_buf_init(subpkt, pkt->curr, len);
160 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
161 * copied: the |subpkt| packet will share its underlying buffer with the
162 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
164 __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
168 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
171 packet_forward(pkt, len);
177 * Peek ahead at 2 bytes in network order from |pkt| and store the value in
180 __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
183 if (PACKET_remaining(pkt) < 2)
186 *data = ((unsigned int)(*pkt->curr)) << 8;
187 *data |= *(pkt->curr + 1);
192 /* Equivalent of n2s */
193 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
194 __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt,
197 if (!PACKET_peek_net_2(pkt, data))
200 packet_forward(pkt, 2);
206 * Peek ahead at 3 bytes in network order from |pkt| and store the value in
209 __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
212 if (PACKET_remaining(pkt) < 3)
215 *data = ((unsigned long)(*pkt->curr)) << 16;
216 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
217 *data |= *(pkt->curr + 2);
222 /* Equivalent of n2l3 */
223 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
224 __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt,
227 if (!PACKET_peek_net_3(pkt, data))
230 packet_forward(pkt, 3);
236 * Peek ahead at 4 bytes in network order from |pkt| and store the value in
239 __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
242 if (PACKET_remaining(pkt) < 4)
245 *data = ((unsigned long)(*pkt->curr)) << 24;
246 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
247 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
248 *data |= *(pkt->curr + 3);
253 /* Equivalent of n2l */
254 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
255 __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt,
258 if (!PACKET_peek_net_4(pkt, data))
261 packet_forward(pkt, 4);
266 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
267 __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
270 if (!PACKET_remaining(pkt))
278 /* Get 1 byte from |pkt| and store the value in |*data| */
279 __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
281 if (!PACKET_peek_1(pkt, data))
284 packet_forward(pkt, 1);
290 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
293 __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
296 if (PACKET_remaining(pkt) < 4)
300 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
301 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
302 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
307 /* Equivalent of c2l */
309 * Get 4 bytes in reverse network order from |pkt| and store the value in
312 __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
314 if (!PACKET_peek_4(pkt, data))
317 packet_forward(pkt, 4);
323 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
324 * |*data|. This just points at the underlying buffer that |pkt| is using. The
325 * caller should not free this data directly (it will be freed when the
326 * underlying buffer gets freed
328 __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
329 unsigned char **data,
332 if (PACKET_remaining(pkt) < len)
341 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
342 * just points at the underlying buffer that |pkt| is using. The caller should
343 * not free this data directly (it will be freed when the underlying buffer gets
346 __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
347 unsigned char **data,
350 if (!PACKET_peek_bytes(pkt, data, len))
353 packet_forward(pkt, len);
358 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
359 __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
363 if (PACKET_remaining(pkt) < len)
366 memcpy(data, pkt->curr, len);
372 * Read |len| bytes from |pkt| and copy them to |data|.
373 * The caller is responsible for ensuring that |data| can hold |len| bytes.
375 __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
379 if (!PACKET_peek_copy_bytes(pkt, data, len))
382 packet_forward(pkt, len);
388 * Copy packet data to |dest|, and set |len| to the number of copied bytes.
389 * If the packet has more than |dest_len| bytes, nothing is copied.
390 * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
391 * Does not forward PACKET position (because it is typically the last thing
392 * done with a given PACKET).
394 __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
396 size_t dest_len, size_t *len)
398 if (PACKET_remaining(pkt) > dest_len) {
402 *len = pkt->remaining;
403 memcpy(dest, pkt->curr, pkt->remaining);
408 * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
409 * result in |*data|, and the length in |len|.
410 * If |*data| is not NULL, the old data is OPENSSL_free'd.
411 * If the packet is empty, or malloc fails, |*data| will be set to NULL.
412 * Returns 1 if the malloc succeeds and 0 otherwise.
413 * Does not forward PACKET position (because it is typically the last thing
414 * done with a given PACKET).
416 __owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
417 unsigned char **data, size_t *len)
425 length = PACKET_remaining(pkt);
430 *data = OPENSSL_memdup(pkt->curr, length);
439 * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
440 * buffer. Store a pointer to the result in |*data|.
441 * If |*data| is not NULL, the old data is OPENSSL_free'd.
442 * If the data in |pkt| does not contain a NUL-byte, the entire data is
443 * copied and NUL-terminated.
444 * Returns 1 if the malloc succeeds and 0 otherwise.
445 * Does not forward PACKET position (because it is typically the last thing done
446 * with a given PACKET).
448 __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
452 /* This will succeed on an empty packet, unless pkt->curr == NULL. */
453 *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
454 return (*data != NULL);
457 /* Move the current reading position forward |len| bytes */
458 __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
460 if (PACKET_remaining(pkt) < len)
463 packet_forward(pkt, len);
469 * Reads a variable-length vector prefixed with a one-byte length, and stores
470 * the contents in |subpkt|. |pkt| can equal |subpkt|.
471 * Data is not copied: the |subpkt| packet will share its underlying buffer with
472 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
473 * Upon failure, the original |pkt| and |subpkt| are not modified.
475 __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
481 if (!PACKET_get_1(&tmp, &length) ||
482 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
488 subpkt->remaining = length;
494 * Reads a variable-length vector prefixed with a two-byte length, and stores
495 * the contents in |subpkt|. |pkt| can equal |subpkt|.
496 * Data is not copied: the |subpkt| packet will share its underlying buffer with
497 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
498 * Upon failure, the original |pkt| and |subpkt| are not modified.
500 __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
506 if (!PACKET_get_net_2(&tmp, &length) ||
507 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
513 subpkt->remaining = length;
519 * Reads a variable-length vector prefixed with a three-byte length, and stores
520 * the contents in |subpkt|. |pkt| can equal |subpkt|.
521 * Data is not copied: the |subpkt| packet will share its underlying buffer with
522 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
523 * Upon failure, the original |pkt| and |subpkt| are not modified.
525 __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
528 unsigned long length;
531 if (!PACKET_get_net_3(&tmp, &length) ||
532 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
538 subpkt->remaining = length;
546 #endif /* HEADER_PACKET_LOCL_H */