]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/packet_locl.h
make update
[thirdparty/openssl.git] / ssl / packet_locl.h
CommitLineData
7e729bb5
MC
1/*
2 * Written by Matt Caswell for the OpenSSL project.
3 */
4/* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
55 *
56 */
57
58#ifndef HEADER_PACKET_LOCL_H
59# define HEADER_PACKET_LOCL_H
60
61# include <string.h>
62# include <openssl/bn.h>
63# include <openssl/buffer.h>
31011544 64# include <openssl/crypto.h>
80e0ecbf 65# include <openssl/e_os2.h>
7e729bb5 66
1de1d768
RL
67# include "internal/numbers.h"
68
7e729bb5
MC
69# ifdef __cplusplus
70extern "C" {
71# endif
72
73typedef struct {
7e729bb5 74 /* Pointer to where we are currently reading from */
b6981744 75 const unsigned char *curr;
6a12a574
EK
76 /* Number of bytes remaining */
77 size_t remaining;
7e729bb5
MC
78} PACKET;
79
6a12a574 80/* Internal unchecked shorthand; don't use outside this file. */
80e0ecbf 81static ossl_inline void packet_forward(PACKET *pkt, size_t len)
6a12a574
EK
82{
83 pkt->curr += len;
84 pkt->remaining -= len;
85}
86
7e729bb5 87/*
bc6616a4 88 * Returns the number of bytes remaining to be read in the PACKET
7e729bb5 89 */
80e0ecbf 90static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
7e729bb5 91{
6a12a574 92 return pkt->remaining;
7e729bb5
MC
93}
94
ec30e856
EK
95/*
96 * Returns a pointer to the PACKET's current position.
97 * For use in non-PACKETized APIs.
ec30e856 98 */
b6981744 99static ossl_inline const unsigned char *PACKET_data(const PACKET *pkt)
ec30e856
EK
100{
101 return pkt->curr;
102}
103
7e729bb5
MC
104/*
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
107 * is being used.
108 */
b6981744
EK
109__owur static ossl_inline int PACKET_buf_init(PACKET *pkt,
110 const unsigned char *buf,
80e0ecbf 111 size_t len)
7e729bb5 112{
6a12a574 113 /* Sanity check for negative values. */
3fde6c92 114 if (len > (size_t)(SIZE_MAX / 2))
7e729bb5 115 return 0;
7e729bb5 116
6a12a574
EK
117 pkt->curr = buf;
118 pkt->remaining = len;
7e729bb5
MC
119 return 1;
120}
121
b3e2272c 122/* Initialize a PACKET to hold zero bytes. */
80e0ecbf 123static ossl_inline void PACKET_null_init(PACKET *pkt)
b3e2272c
EK
124{
125 pkt->curr = NULL;
126 pkt->remaining = 0;
127}
128
31011544
EK
129/*
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.
133 */
80e0ecbf
DSH
134__owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
135 size_t num)
136{
31011544
EK
137 if (PACKET_remaining(pkt) != num)
138 return 0;
139 return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
140}
141
7e729bb5
MC
142/*
143 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
144 * Data is not copied: the |subpkt| packet will share its underlying buffer with
145 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
146 */
80e0ecbf
DSH
147__owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
148 PACKET *subpkt,
149 size_t len)
7e729bb5
MC
150{
151 if (PACKET_remaining(pkt) < len)
152 return 0;
153
f4f78ff7 154 return PACKET_buf_init(subpkt, pkt->curr, len);
7e729bb5
MC
155}
156
157/*
158 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
159 * copied: the |subpkt| packet will share its underlying buffer with the
160 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
161 */
80e0ecbf
DSH
162__owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
163 PACKET *subpkt,
164 size_t len)
7e729bb5
MC
165{
166 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
167 return 0;
168
6a12a574 169 packet_forward(pkt, len);
7e729bb5
MC
170
171 return 1;
172}
173
80e0ecbf
DSH
174/*
175 * Peek ahead at 2 bytes in network order from |pkt| and store the value in
7e729bb5
MC
176 * |*data|
177 */
80e0ecbf
DSH
178__owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
179 unsigned int *data)
7e729bb5
MC
180{
181 if (PACKET_remaining(pkt) < 2)
182 return 0;
183
80e0ecbf 184 *data = ((unsigned int)(*pkt->curr)) << 8;
7e729bb5
MC
185 *data |= *(pkt->curr + 1);
186
187 return 1;
188}
189
190/* Equivalent of n2s */
191/* Get 2 bytes in network order from |pkt| and store the value in |*data| */
80e0ecbf
DSH
192__owur static ossl_inline int PACKET_get_net_2(PACKET *pkt,
193 unsigned int *data)
7e729bb5
MC
194{
195 if (!PACKET_peek_net_2(pkt, data))
196 return 0;
197
6a12a574 198 packet_forward(pkt, 2);
7e729bb5
MC
199
200 return 1;
201}
202
80e0ecbf
DSH
203/*
204 * Peek ahead at 3 bytes in network order from |pkt| and store the value in
7e729bb5
MC
205 * |*data|
206 */
80e0ecbf
DSH
207__owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
208 unsigned long *data)
7e729bb5
MC
209{
210 if (PACKET_remaining(pkt) < 3)
211 return 0;
212
80e0ecbf
DSH
213 *data = ((unsigned long)(*pkt->curr)) << 16;
214 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
44128847 215 *data |= *(pkt->curr + 2);
7e729bb5
MC
216
217 return 1;
218}
219
220/* Equivalent of n2l3 */
221/* Get 3 bytes in network order from |pkt| and store the value in |*data| */
80e0ecbf
DSH
222__owur static ossl_inline int PACKET_get_net_3(PACKET *pkt,
223 unsigned long *data)
7e729bb5
MC
224{
225 if (!PACKET_peek_net_3(pkt, data))
226 return 0;
227
6a12a574 228 packet_forward(pkt, 3);
7e729bb5
MC
229
230 return 1;
231}
232
80e0ecbf
DSH
233/*
234 * Peek ahead at 4 bytes in network order from |pkt| and store the value in
7e729bb5
MC
235 * |*data|
236 */
80e0ecbf
DSH
237__owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
238 unsigned long *data)
7e729bb5
MC
239{
240 if (PACKET_remaining(pkt) < 4)
241 return 0;
242
80e0ecbf 243 *data = ((unsigned long)(*pkt->curr)) << 24;
44128847 244 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
80e0ecbf
DSH
245 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
246 *data |= *(pkt->curr + 3);
7e729bb5
MC
247
248 return 1;
249}
250
251/* Equivalent of n2l */
252/* Get 4 bytes in network order from |pkt| and store the value in |*data| */
80e0ecbf
DSH
253__owur static ossl_inline int PACKET_get_net_4(PACKET *pkt,
254 unsigned long *data)
7e729bb5
MC
255{
256 if (!PACKET_peek_net_4(pkt, data))
257 return 0;
258
6a12a574 259 packet_forward(pkt, 4);
7e729bb5
MC
260
261 return 1;
262}
263
264/* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
80e0ecbf
DSH
265__owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
266 unsigned int *data)
7e729bb5
MC
267{
268 if (!PACKET_remaining(pkt))
269 return 0;
270
271 *data = *pkt->curr;
272
273 return 1;
274}
275
276/* Get 1 byte from |pkt| and store the value in |*data| */
80e0ecbf 277__owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
7e729bb5
MC
278{
279 if (!PACKET_peek_1(pkt, data))
280 return 0;
281
6a12a574 282 packet_forward(pkt, 1);
7e729bb5
MC
283
284 return 1;
285}
286
287/*
288 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
289 * in |*data|
290 */
80e0ecbf
DSH
291__owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
292 unsigned long *data)
7e729bb5
MC
293{
294 if (PACKET_remaining(pkt) < 4)
295 return 0;
296
80e0ecbf
DSH
297 *data = *pkt->curr;
298 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
44128847
MC
299 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
300 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
7e729bb5
MC
301
302 return 1;
303}
304
305/* Equivalent of c2l */
306/*
307 * Get 4 bytes in reverse network order from |pkt| and store the value in
308 * |*data|
309 */
80e0ecbf 310__owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
7e729bb5
MC
311{
312 if (!PACKET_peek_4(pkt, data))
313 return 0;
314
6a12a574 315 packet_forward(pkt, 4);
7e729bb5
MC
316
317 return 1;
318}
319
320/*
321 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
322 * |*data|. This just points at the underlying buffer that |pkt| is using. The
323 * caller should not free this data directly (it will be freed when the
324 * underlying buffer gets freed
325 */
80e0ecbf 326__owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
b6981744 327 const unsigned char **data,
80e0ecbf 328 size_t len)
7e729bb5
MC
329{
330 if (PACKET_remaining(pkt) < len)
331 return 0;
332
333 *data = pkt->curr;
334
335 return 1;
336}
337
338/*
339 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
340 * just points at the underlying buffer that |pkt| is using. The caller should
341 * not free this data directly (it will be freed when the underlying buffer gets
342 * freed
343 */
80e0ecbf 344__owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
b6981744 345 const unsigned char **data,
80e0ecbf 346 size_t len)
7e729bb5
MC
347{
348 if (!PACKET_peek_bytes(pkt, data, len))
349 return 0;
350
6a12a574 351 packet_forward(pkt, len);
7e729bb5
MC
352
353 return 1;
354}
355
356/* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
80e0ecbf
DSH
357__owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
358 unsigned char *data,
359 size_t len)
7e729bb5
MC
360{
361 if (PACKET_remaining(pkt) < len)
362 return 0;
363
364 memcpy(data, pkt->curr, len);
365
366 return 1;
367}
368
6d41fc80
EK
369/*
370 * Read |len| bytes from |pkt| and copy them to |data|.
371 * The caller is responsible for ensuring that |data| can hold |len| bytes.
372 */
80e0ecbf
DSH
373__owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
374 unsigned char *data,
375 size_t len)
7e729bb5
MC
376{
377 if (!PACKET_peek_copy_bytes(pkt, data, len))
378 return 0;
379
6a12a574 380 packet_forward(pkt, len);
7e729bb5
MC
381
382 return 1;
383}
384
67202973
EK
385/*
386 * Copy packet data to |dest|, and set |len| to the number of copied bytes.
387 * If the packet has more than |dest_len| bytes, nothing is copied.
388 * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
389 * Does not forward PACKET position (because it is typically the last thing
390 * done with a given PACKET).
391 */
80e0ecbf
DSH
392__owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
393 unsigned char *dest,
394 size_t dest_len, size_t *len)
395{
67202973
EK
396 if (PACKET_remaining(pkt) > dest_len) {
397 *len = 0;
398 return 0;
399 }
400 *len = pkt->remaining;
401 memcpy(dest, pkt->curr, pkt->remaining);
402 return 1;
403}
404
6d41fc80
EK
405/*
406 * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
407 * result in |*data|, and the length in |len|.
408 * If |*data| is not NULL, the old data is OPENSSL_free'd.
409 * If the packet is empty, or malloc fails, |*data| will be set to NULL.
410 * Returns 1 if the malloc succeeds and 0 otherwise.
411 * Does not forward PACKET position (because it is typically the last thing
412 * done with a given PACKET).
413 */
80e0ecbf
DSH
414__owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
415 unsigned char **data, size_t *len)
6d41fc80
EK
416{
417 size_t length;
418
419 OPENSSL_free(*data);
420 *data = NULL;
421 *len = 0;
422
423 length = PACKET_remaining(pkt);
424
425 if (length == 0)
426 return 1;
427
7644a9ae 428 *data = OPENSSL_memdup(pkt->curr, length);
6d41fc80
EK
429 if (*data == NULL)
430 return 0;
431
432 *len = length;
433 return 1;
434}
435
436/*
437 * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
438 * buffer. Store a pointer to the result in |*data|.
439 * If |*data| is not NULL, the old data is OPENSSL_free'd.
440 * If the data in |pkt| does not contain a NUL-byte, the entire data is
441 * copied and NUL-terminated.
442 * Returns 1 if the malloc succeeds and 0 otherwise.
443 * Does not forward PACKET position (because it is typically the last thing done
444 * with a given PACKET).
445 */
80e0ecbf 446__owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
6d41fc80
EK
447{
448 OPENSSL_free(*data);
32942870
EK
449
450 /* This will succeed on an empty packet, unless pkt->curr == NULL. */
80e0ecbf 451 *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
6d41fc80
EK
452 return (*data != NULL);
453}
454
7e729bb5 455/* Move the current reading position forward |len| bytes */
80e0ecbf 456__owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
7e729bb5
MC
457{
458 if (PACKET_remaining(pkt) < len)
459 return 0;
460
6a12a574 461 packet_forward(pkt, len);
7e729bb5
MC
462
463 return 1;
464}
465
ec30e856
EK
466/*
467 * Reads a variable-length vector prefixed with a one-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.
472 */
80e0ecbf
DSH
473__owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
474 PACKET *subpkt)
ec30e856 475{
80e0ecbf 476 unsigned int length;
b6981744 477 const unsigned char *data;
80e0ecbf
DSH
478 PACKET tmp = *pkt;
479 if (!PACKET_get_1(&tmp, &length) ||
480 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
481 return 0;
482 }
483
484 *pkt = tmp;
485 subpkt->curr = data;
486 subpkt->remaining = length;
487
488 return 1;
ec30e856
EK
489}
490
491/*
492 * Reads a variable-length vector prefixed with a two-byte length, and stores
493 * the contents in |subpkt|. |pkt| can equal |subpkt|.
494 * Data is not copied: the |subpkt| packet will share its underlying buffer with
495 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
496 * Upon failure, the original |pkt| and |subpkt| are not modified.
497 */
80e0ecbf
DSH
498__owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
499 PACKET *subpkt)
ec30e856 500{
80e0ecbf 501 unsigned int length;
b6981744 502 const unsigned char *data;
80e0ecbf
DSH
503 PACKET tmp = *pkt;
504 if (!PACKET_get_net_2(&tmp, &length) ||
505 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
506 return 0;
507 }
508
509 *pkt = tmp;
510 subpkt->curr = data;
511 subpkt->remaining = length;
512
513 return 1;
ec30e856
EK
514}
515
516/*
517 * Reads a variable-length vector prefixed with a three-byte length, and stores
518 * the contents in |subpkt|. |pkt| can equal |subpkt|.
519 * Data is not copied: the |subpkt| packet will share its underlying buffer with
520 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
521 * Upon failure, the original |pkt| and |subpkt| are not modified.
522 */
80e0ecbf
DSH
523__owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
524 PACKET *subpkt)
ec30e856 525{
80e0ecbf 526 unsigned long length;
b6981744 527 const unsigned char *data;
80e0ecbf
DSH
528 PACKET tmp = *pkt;
529 if (!PACKET_get_net_3(&tmp, &length) ||
530 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
531 return 0;
532 }
533
534 *pkt = tmp;
535 subpkt->curr = data;
536 subpkt->remaining = length;
537
538 return 1;
ec30e856 539}
7e729bb5
MC
540# ifdef __cplusplus
541}
542# endif
543
80e0ecbf 544#endif /* HEADER_PACKET_LOCL_H */