]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/packet_locl.h
unified build scheme: add build.info files
[thirdparty/openssl.git] / ssl / packet_locl.h
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>
64 # include <openssl/crypto.h>
65 # include <openssl/e_os2.h>
66
67 # include "internal/numbers.h"
68
69 # ifdef __cplusplus
70 extern "C" {
71 # endif
72
73 typedef struct {
74 /* Pointer to where we are currently reading from */
75 unsigned char *curr;
76 /* Number of bytes remaining */
77 size_t remaining;
78 } PACKET;
79
80 /* Internal unchecked shorthand; don't use outside this file. */
81 static ossl_inline void packet_forward(PACKET *pkt, size_t len)
82 {
83 pkt->curr += len;
84 pkt->remaining -= len;
85 }
86
87 /*
88 * Returns the number of bytes remaining to be read in the PACKET
89 */
90 static ossl_inline size_t PACKET_remaining(const PACKET *pkt)
91 {
92 return pkt->remaining;
93 }
94
95 /*
96 * Returns a pointer to the PACKET's current position.
97 * For use in non-PACKETized APIs.
98 * TODO(openssl-team): this should return 'const unsigned char*' but can't
99 * currently because legacy code passes 'unsigned char*'s around.
100 */
101 static ossl_inline unsigned char *PACKET_data(const PACKET *pkt)
102 {
103 return pkt->curr;
104 }
105
106 /*
107 * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
108 * copy of the data so |buf| must be present for the whole time that the PACKET
109 * is being used.
110 */
111 __owur static ossl_inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf,
112 size_t len)
113 {
114 /* Sanity check for negative values. */
115 if (len > (size_t)(SIZE_MAX / 2))
116 return 0;
117
118 pkt->curr = buf;
119 pkt->remaining = len;
120 return 1;
121 }
122
123 /* Initialize a PACKET to hold zero bytes. */
124 static ossl_inline void PACKET_null_init(PACKET *pkt)
125 {
126 pkt->curr = NULL;
127 pkt->remaining = 0;
128 }
129
130 /*
131 * Returns 1 if the packet has length |num| and its contents equal the |num|
132 * bytes read from |ptr|. Returns 0 otherwise (lengths or contents not equal).
133 * If lengths are equal, performs the comparison in constant time.
134 */
135 __owur static ossl_inline int PACKET_equal(const PACKET *pkt, const void *ptr,
136 size_t num)
137 {
138 if (PACKET_remaining(pkt) != num)
139 return 0;
140 return CRYPTO_memcmp(pkt->curr, ptr, num) == 0;
141 }
142
143 /*
144 * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
145 * Data is not copied: the |subpkt| packet will share its underlying buffer with
146 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
147 */
148 __owur static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,
149 PACKET *subpkt,
150 size_t len)
151 {
152 if (PACKET_remaining(pkt) < len)
153 return 0;
154
155 return PACKET_buf_init(subpkt, pkt->curr, len);
156 }
157
158 /*
159 * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
160 * copied: the |subpkt| packet will share its underlying buffer with the
161 * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
162 */
163 __owur static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,
164 PACKET *subpkt,
165 size_t len)
166 {
167 if (!PACKET_peek_sub_packet(pkt, subpkt, len))
168 return 0;
169
170 packet_forward(pkt, len);
171
172 return 1;
173 }
174
175 /*
176 * Peek ahead at 2 bytes in network order from |pkt| and store the value in
177 * |*data|
178 */
179 __owur static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,
180 unsigned int *data)
181 {
182 if (PACKET_remaining(pkt) < 2)
183 return 0;
184
185 *data = ((unsigned int)(*pkt->curr)) << 8;
186 *data |= *(pkt->curr + 1);
187
188 return 1;
189 }
190
191 /* Equivalent of n2s */
192 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
193 __owur static ossl_inline int PACKET_get_net_2(PACKET *pkt,
194 unsigned int *data)
195 {
196 if (!PACKET_peek_net_2(pkt, data))
197 return 0;
198
199 packet_forward(pkt, 2);
200
201 return 1;
202 }
203
204 /*
205 * Peek ahead at 3 bytes in network order from |pkt| and store the value in
206 * |*data|
207 */
208 __owur static ossl_inline int PACKET_peek_net_3(const PACKET *pkt,
209 unsigned long *data)
210 {
211 if (PACKET_remaining(pkt) < 3)
212 return 0;
213
214 *data = ((unsigned long)(*pkt->curr)) << 16;
215 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
216 *data |= *(pkt->curr + 2);
217
218 return 1;
219 }
220
221 /* Equivalent of n2l3 */
222 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
223 __owur static ossl_inline int PACKET_get_net_3(PACKET *pkt,
224 unsigned long *data)
225 {
226 if (!PACKET_peek_net_3(pkt, data))
227 return 0;
228
229 packet_forward(pkt, 3);
230
231 return 1;
232 }
233
234 /*
235 * Peek ahead at 4 bytes in network order from |pkt| and store the value in
236 * |*data|
237 */
238 __owur static ossl_inline int PACKET_peek_net_4(const PACKET *pkt,
239 unsigned long *data)
240 {
241 if (PACKET_remaining(pkt) < 4)
242 return 0;
243
244 *data = ((unsigned long)(*pkt->curr)) << 24;
245 *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
246 *data |= ((unsigned long)(*(pkt->curr + 2))) << 8;
247 *data |= *(pkt->curr + 3);
248
249 return 1;
250 }
251
252 /* Equivalent of n2l */
253 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
254 __owur static ossl_inline int PACKET_get_net_4(PACKET *pkt,
255 unsigned long *data)
256 {
257 if (!PACKET_peek_net_4(pkt, data))
258 return 0;
259
260 packet_forward(pkt, 4);
261
262 return 1;
263 }
264
265 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
266 __owur static ossl_inline int PACKET_peek_1(const PACKET *pkt,
267 unsigned int *data)
268 {
269 if (!PACKET_remaining(pkt))
270 return 0;
271
272 *data = *pkt->curr;
273
274 return 1;
275 }
276
277 /* Get 1 byte from |pkt| and store the value in |*data| */
278 __owur static ossl_inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
279 {
280 if (!PACKET_peek_1(pkt, data))
281 return 0;
282
283 packet_forward(pkt, 1);
284
285 return 1;
286 }
287
288 /*
289 * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
290 * in |*data|
291 */
292 __owur static ossl_inline int PACKET_peek_4(const PACKET *pkt,
293 unsigned long *data)
294 {
295 if (PACKET_remaining(pkt) < 4)
296 return 0;
297
298 *data = *pkt->curr;
299 *data |= ((unsigned long)(*(pkt->curr + 1))) << 8;
300 *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
301 *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
302
303 return 1;
304 }
305
306 /* Equivalent of c2l */
307 /*
308 * Get 4 bytes in reverse network order from |pkt| and store the value in
309 * |*data|
310 */
311 __owur static ossl_inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
312 {
313 if (!PACKET_peek_4(pkt, data))
314 return 0;
315
316 packet_forward(pkt, 4);
317
318 return 1;
319 }
320
321 /*
322 * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
323 * |*data|. This just points at the underlying buffer that |pkt| is using. The
324 * caller should not free this data directly (it will be freed when the
325 * underlying buffer gets freed
326 */
327 __owur static ossl_inline int PACKET_peek_bytes(const PACKET *pkt,
328 unsigned char **data,
329 size_t len)
330 {
331 if (PACKET_remaining(pkt) < len)
332 return 0;
333
334 *data = pkt->curr;
335
336 return 1;
337 }
338
339 /*
340 * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
341 * just points at the underlying buffer that |pkt| is using. The caller should
342 * not free this data directly (it will be freed when the underlying buffer gets
343 * freed
344 */
345 __owur static ossl_inline int PACKET_get_bytes(PACKET *pkt,
346 unsigned char **data,
347 size_t len)
348 {
349 if (!PACKET_peek_bytes(pkt, data, len))
350 return 0;
351
352 packet_forward(pkt, len);
353
354 return 1;
355 }
356
357 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
358 __owur static ossl_inline int PACKET_peek_copy_bytes(const PACKET *pkt,
359 unsigned char *data,
360 size_t len)
361 {
362 if (PACKET_remaining(pkt) < len)
363 return 0;
364
365 memcpy(data, pkt->curr, len);
366
367 return 1;
368 }
369
370 /*
371 * Read |len| bytes from |pkt| and copy them to |data|.
372 * The caller is responsible for ensuring that |data| can hold |len| bytes.
373 */
374 __owur static ossl_inline int PACKET_copy_bytes(PACKET *pkt,
375 unsigned char *data,
376 size_t len)
377 {
378 if (!PACKET_peek_copy_bytes(pkt, data, len))
379 return 0;
380
381 packet_forward(pkt, len);
382
383 return 1;
384 }
385
386 /*
387 * Copy packet data to |dest|, and set |len| to the number of copied bytes.
388 * If the packet has more than |dest_len| bytes, nothing is copied.
389 * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
390 * Does not forward PACKET position (because it is typically the last thing
391 * done with a given PACKET).
392 */
393 __owur static ossl_inline int PACKET_copy_all(const PACKET *pkt,
394 unsigned char *dest,
395 size_t dest_len, size_t *len)
396 {
397 if (PACKET_remaining(pkt) > dest_len) {
398 *len = 0;
399 return 0;
400 }
401 *len = pkt->remaining;
402 memcpy(dest, pkt->curr, pkt->remaining);
403 return 1;
404 }
405
406 /*
407 * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
408 * result in |*data|, and the length in |len|.
409 * If |*data| is not NULL, the old data is OPENSSL_free'd.
410 * If the packet is empty, or malloc fails, |*data| will be set to NULL.
411 * Returns 1 if the malloc succeeds and 0 otherwise.
412 * Does not forward PACKET position (because it is typically the last thing
413 * done with a given PACKET).
414 */
415 __owur static ossl_inline int PACKET_memdup(const PACKET *pkt,
416 unsigned char **data, size_t *len)
417 {
418 size_t length;
419
420 OPENSSL_free(*data);
421 *data = NULL;
422 *len = 0;
423
424 length = PACKET_remaining(pkt);
425
426 if (length == 0)
427 return 1;
428
429 *data = OPENSSL_memdup(pkt->curr, length);
430 if (*data == NULL)
431 return 0;
432
433 *len = length;
434 return 1;
435 }
436
437 /*
438 * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
439 * buffer. Store a pointer to the result in |*data|.
440 * If |*data| is not NULL, the old data is OPENSSL_free'd.
441 * If the data in |pkt| does not contain a NUL-byte, the entire data is
442 * copied and NUL-terminated.
443 * Returns 1 if the malloc succeeds and 0 otherwise.
444 * Does not forward PACKET position (because it is typically the last thing done
445 * with a given PACKET).
446 */
447 __owur static ossl_inline int PACKET_strndup(const PACKET *pkt, char **data)
448 {
449 OPENSSL_free(*data);
450
451 /* This will succeed on an empty packet, unless pkt->curr == NULL. */
452 *data = OPENSSL_strndup((const char *)pkt->curr, PACKET_remaining(pkt));
453 return (*data != NULL);
454 }
455
456 /* Move the current reading position forward |len| bytes */
457 __owur static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)
458 {
459 if (PACKET_remaining(pkt) < len)
460 return 0;
461
462 packet_forward(pkt, len);
463
464 return 1;
465 }
466
467 /*
468 * Reads a variable-length vector prefixed with a one-byte length, and stores
469 * the contents in |subpkt|. |pkt| can equal |subpkt|.
470 * Data is not copied: the |subpkt| packet will share its underlying buffer with
471 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
472 * Upon failure, the original |pkt| and |subpkt| are not modified.
473 */
474 __owur static ossl_inline int PACKET_get_length_prefixed_1(PACKET *pkt,
475 PACKET *subpkt)
476 {
477 unsigned int length;
478 unsigned char *data;
479 PACKET tmp = *pkt;
480 if (!PACKET_get_1(&tmp, &length) ||
481 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
482 return 0;
483 }
484
485 *pkt = tmp;
486 subpkt->curr = data;
487 subpkt->remaining = length;
488
489 return 1;
490 }
491
492 /*
493 * Reads a variable-length vector prefixed with a two-byte length, and stores
494 * the contents in |subpkt|. |pkt| can equal |subpkt|.
495 * Data is not copied: the |subpkt| packet will share its underlying buffer with
496 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
497 * Upon failure, the original |pkt| and |subpkt| are not modified.
498 */
499 __owur static ossl_inline int PACKET_get_length_prefixed_2(PACKET *pkt,
500 PACKET *subpkt)
501 {
502 unsigned int length;
503 unsigned char *data;
504 PACKET tmp = *pkt;
505 if (!PACKET_get_net_2(&tmp, &length) ||
506 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
507 return 0;
508 }
509
510 *pkt = tmp;
511 subpkt->curr = data;
512 subpkt->remaining = length;
513
514 return 1;
515 }
516
517 /*
518 * Reads a variable-length vector prefixed with a three-byte length, and stores
519 * the contents in |subpkt|. |pkt| can equal |subpkt|.
520 * Data is not copied: the |subpkt| packet will share its underlying buffer with
521 * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
522 * Upon failure, the original |pkt| and |subpkt| are not modified.
523 */
524 __owur static ossl_inline int PACKET_get_length_prefixed_3(PACKET *pkt,
525 PACKET *subpkt)
526 {
527 unsigned long length;
528 unsigned char *data;
529 PACKET tmp = *pkt;
530 if (!PACKET_get_net_3(&tmp, &length) ||
531 !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
532 return 0;
533 }
534
535 *pkt = tmp;
536 subpkt->curr = data;
537 subpkt->remaining = length;
538
539 return 1;
540 }
541 # ifdef __cplusplus
542 }
543 # endif
544
545 #endif /* HEADER_PACKET_LOCL_H */