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