]>
Commit | Line | Data |
---|---|---|
7e729bb5 MC |
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 "e_os.h" | |
66 | ||
67 | # ifdef __cplusplus | |
68 | extern "C" { | |
69 | # endif | |
70 | ||
71 | typedef struct { | |
7e729bb5 MC |
72 | /* Pointer to where we are currently reading from */ |
73 | unsigned char *curr; | |
6a12a574 EK |
74 | /* Number of bytes remaining */ |
75 | size_t remaining; | |
7e729bb5 MC |
76 | } PACKET; |
77 | ||
6a12a574 EK |
78 | /* Internal unchecked shorthand; don't use outside this file. */ |
79 | static inline void packet_forward(PACKET *pkt, size_t len) | |
80 | { | |
81 | pkt->curr += len; | |
82 | pkt->remaining -= len; | |
83 | } | |
84 | ||
7e729bb5 | 85 | /* |
bc6616a4 | 86 | * Returns the number of bytes remaining to be read in the PACKET |
7e729bb5 | 87 | */ |
2aa815c3 | 88 | __owur static inline size_t PACKET_remaining(const PACKET *pkt) |
7e729bb5 | 89 | { |
6a12a574 | 90 | return pkt->remaining; |
7e729bb5 MC |
91 | } |
92 | ||
ec30e856 EK |
93 | /* |
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. | |
98 | */ | |
99 | static inline unsigned char *PACKET_data(const PACKET *pkt) | |
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 | */ | |
109 | static inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf, size_t len) | |
110 | { | |
6a12a574 EK |
111 | /* Sanity check for negative values. */ |
112 | if (buf + len < buf) | |
7e729bb5 | 113 | return 0; |
7e729bb5 | 114 | |
6a12a574 EK |
115 | pkt->curr = buf; |
116 | pkt->remaining = len; | |
7e729bb5 MC |
117 | return 1; |
118 | } | |
119 | ||
b3e2272c EK |
120 | /* Initialize a PACKET to hold zero bytes. */ |
121 | static inline void PACKET_null_init(PACKET *pkt) | |
122 | { | |
123 | pkt->curr = NULL; | |
124 | pkt->remaining = 0; | |
125 | } | |
126 | ||
7e729bb5 MC |
127 | /* |
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|. | |
131 | */ | |
2aa815c3 EK |
132 | __owur static inline int PACKET_peek_sub_packet(const PACKET *pkt, |
133 | PACKET *subpkt, size_t len) | |
7e729bb5 MC |
134 | { |
135 | if (PACKET_remaining(pkt) < len) | |
136 | return 0; | |
137 | ||
138 | PACKET_buf_init(subpkt, pkt->curr, len); | |
139 | ||
140 | return 1; | |
141 | } | |
142 | ||
143 | /* | |
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|. | |
147 | */ | |
148 | __owur static inline int PACKET_get_sub_packet(PACKET *pkt, PACKET *subpkt, | |
149 | size_t len) | |
150 | { | |
151 | if (!PACKET_peek_sub_packet(pkt, subpkt, len)) | |
152 | return 0; | |
153 | ||
6a12a574 | 154 | packet_forward(pkt, len); |
7e729bb5 MC |
155 | |
156 | return 1; | |
157 | } | |
158 | ||
159 | /* Peek ahead at 2 bytes in network order from |pkt| and store the value in | |
160 | * |*data| | |
161 | */ | |
2aa815c3 EK |
162 | __owur static inline int PACKET_peek_net_2(const PACKET *pkt, |
163 | unsigned int *data) | |
7e729bb5 MC |
164 | { |
165 | if (PACKET_remaining(pkt) < 2) | |
166 | return 0; | |
167 | ||
168 | *data = ((unsigned int)(*pkt->curr)) << 8; | |
169 | *data |= *(pkt->curr + 1); | |
170 | ||
171 | return 1; | |
172 | } | |
173 | ||
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) | |
177 | { | |
178 | if (!PACKET_peek_net_2(pkt, data)) | |
179 | return 0; | |
180 | ||
6a12a574 | 181 | packet_forward(pkt, 2); |
7e729bb5 MC |
182 | |
183 | return 1; | |
184 | } | |
185 | ||
186 | /* Peek ahead at 3 bytes in network order from |pkt| and store the value in | |
187 | * |*data| | |
188 | */ | |
2aa815c3 EK |
189 | __owur static inline int PACKET_peek_net_3(const PACKET *pkt, |
190 | unsigned long *data) | |
7e729bb5 MC |
191 | { |
192 | if (PACKET_remaining(pkt) < 3) | |
193 | return 0; | |
194 | ||
195 | *data = ((unsigned long)(*pkt->curr)) << 16; | |
44128847 MC |
196 | *data |= ((unsigned long)(*(pkt->curr + 1))) << 8; |
197 | *data |= *(pkt->curr + 2); | |
7e729bb5 MC |
198 | |
199 | return 1; | |
200 | } | |
201 | ||
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) | |
205 | { | |
206 | if (!PACKET_peek_net_3(pkt, data)) | |
207 | return 0; | |
208 | ||
6a12a574 | 209 | packet_forward(pkt, 3); |
7e729bb5 MC |
210 | |
211 | return 1; | |
212 | } | |
213 | ||
214 | /* Peek ahead at 4 bytes in network order from |pkt| and store the value in | |
215 | * |*data| | |
216 | */ | |
2aa815c3 EK |
217 | __owur static inline int PACKET_peek_net_4(const PACKET *pkt, |
218 | unsigned long *data) | |
7e729bb5 MC |
219 | { |
220 | if (PACKET_remaining(pkt) < 4) | |
221 | return 0; | |
222 | ||
223 | *data = ((unsigned long)(*pkt->curr)) << 24; | |
44128847 MC |
224 | *data |= ((unsigned long)(*(pkt->curr + 1))) << 16; |
225 | *data |= ((unsigned long)(*(pkt->curr + 2))) << 8; | |
226 | *data |= *(pkt->curr+3); | |
7e729bb5 MC |
227 | |
228 | return 1; | |
229 | } | |
230 | ||
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) | |
234 | { | |
235 | if (!PACKET_peek_net_4(pkt, data)) | |
236 | return 0; | |
237 | ||
6a12a574 | 238 | packet_forward(pkt, 4); |
7e729bb5 MC |
239 | |
240 | return 1; | |
241 | } | |
242 | ||
243 | /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */ | |
2aa815c3 | 244 | __owur static inline int PACKET_peek_1(const PACKET *pkt, unsigned int *data) |
7e729bb5 MC |
245 | { |
246 | if (!PACKET_remaining(pkt)) | |
247 | return 0; | |
248 | ||
249 | *data = *pkt->curr; | |
250 | ||
251 | return 1; | |
252 | } | |
253 | ||
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) | |
256 | { | |
257 | if (!PACKET_peek_1(pkt, data)) | |
258 | return 0; | |
259 | ||
6a12a574 | 260 | packet_forward(pkt, 1); |
7e729bb5 MC |
261 | |
262 | return 1; | |
263 | } | |
264 | ||
265 | /* | |
266 | * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value | |
267 | * in |*data| | |
268 | */ | |
2aa815c3 | 269 | __owur static inline int PACKET_peek_4(const PACKET *pkt, unsigned long *data) |
7e729bb5 MC |
270 | { |
271 | if (PACKET_remaining(pkt) < 4) | |
272 | return 0; | |
273 | ||
274 | *data = *pkt->curr; | |
44128847 MC |
275 | *data |= ((unsigned long)(*(pkt->curr + 1))) << 8; |
276 | *data |= ((unsigned long)(*(pkt->curr + 2))) << 16; | |
277 | *data |= ((unsigned long)(*(pkt->curr + 3))) << 24; | |
7e729bb5 MC |
278 | |
279 | return 1; | |
280 | } | |
281 | ||
282 | /* Equivalent of c2l */ | |
283 | /* | |
284 | * Get 4 bytes in reverse network order from |pkt| and store the value in | |
285 | * |*data| | |
286 | */ | |
287 | __owur static inline int PACKET_get_4(PACKET *pkt, unsigned long *data) | |
288 | { | |
289 | if (!PACKET_peek_4(pkt, data)) | |
290 | return 0; | |
291 | ||
6a12a574 | 292 | packet_forward(pkt, 4); |
7e729bb5 MC |
293 | |
294 | return 1; | |
295 | } | |
296 | ||
297 | /* | |
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 | |
302 | */ | |
2aa815c3 | 303 | __owur static inline int PACKET_peek_bytes(const PACKET *pkt, unsigned char **data, |
67202973 | 304 | size_t len) |
7e729bb5 MC |
305 | { |
306 | if (PACKET_remaining(pkt) < len) | |
307 | return 0; | |
308 | ||
309 | *data = pkt->curr; | |
310 | ||
311 | return 1; | |
312 | } | |
313 | ||
314 | /* | |
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 | |
318 | * freed | |
319 | */ | |
320 | __owur static inline int PACKET_get_bytes(PACKET *pkt, unsigned char **data, | |
321 | size_t len) | |
322 | { | |
323 | if (!PACKET_peek_bytes(pkt, data, len)) | |
324 | return 0; | |
325 | ||
6a12a574 | 326 | packet_forward(pkt, len); |
7e729bb5 MC |
327 | |
328 | return 1; | |
329 | } | |
330 | ||
331 | /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */ | |
2aa815c3 | 332 | __owur static inline int PACKET_peek_copy_bytes(const PACKET *pkt, |
7e729bb5 MC |
333 | unsigned char *data, size_t len) |
334 | { | |
335 | if (PACKET_remaining(pkt) < len) | |
336 | return 0; | |
337 | ||
338 | memcpy(data, pkt->curr, len); | |
339 | ||
340 | return 1; | |
341 | } | |
342 | ||
6d41fc80 EK |
343 | /* |
344 | * Read |len| bytes from |pkt| and copy them to |data|. | |
345 | * The caller is responsible for ensuring that |data| can hold |len| bytes. | |
346 | */ | |
7e729bb5 | 347 | __owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data, |
6d41fc80 | 348 | size_t len) |
7e729bb5 MC |
349 | { |
350 | if (!PACKET_peek_copy_bytes(pkt, data, len)) | |
351 | return 0; | |
352 | ||
6a12a574 | 353 | packet_forward(pkt, len); |
7e729bb5 MC |
354 | |
355 | return 1; | |
356 | } | |
357 | ||
67202973 EK |
358 | /* |
359 | * Copy packet data to |dest|, and set |len| to the number of copied bytes. | |
360 | * If the packet has more than |dest_len| bytes, nothing is copied. | |
361 | * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise. | |
362 | * Does not forward PACKET position (because it is typically the last thing | |
363 | * done with a given PACKET). | |
364 | */ | |
365 | __owur static inline int PACKET_copy_all(const PACKET *pkt, unsigned char *dest, | |
366 | size_t dest_len, size_t *len) { | |
367 | if (PACKET_remaining(pkt) > dest_len) { | |
368 | *len = 0; | |
369 | return 0; | |
370 | } | |
371 | *len = pkt->remaining; | |
372 | memcpy(dest, pkt->curr, pkt->remaining); | |
373 | return 1; | |
374 | } | |
375 | ||
6d41fc80 EK |
376 | /* |
377 | * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the | |
378 | * result in |*data|, and the length in |len|. | |
379 | * If |*data| is not NULL, the old data is OPENSSL_free'd. | |
380 | * If the packet is empty, or malloc fails, |*data| will be set to NULL. | |
381 | * Returns 1 if the malloc succeeds and 0 otherwise. | |
382 | * Does not forward PACKET position (because it is typically the last thing | |
383 | * done with a given PACKET). | |
384 | */ | |
385 | __owur static inline int PACKET_memdup(const PACKET *pkt, unsigned char **data, | |
386 | size_t *len) | |
387 | { | |
388 | size_t length; | |
389 | ||
390 | OPENSSL_free(*data); | |
391 | *data = NULL; | |
392 | *len = 0; | |
393 | ||
394 | length = PACKET_remaining(pkt); | |
395 | ||
396 | if (length == 0) | |
397 | return 1; | |
398 | ||
399 | *data = BUF_memdup(pkt->curr, length); | |
400 | ||
401 | if (*data == NULL) | |
402 | return 0; | |
403 | ||
404 | *len = length; | |
405 | return 1; | |
406 | } | |
407 | ||
408 | /* | |
409 | * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated | |
410 | * buffer. Store a pointer to the result in |*data|. | |
411 | * If |*data| is not NULL, the old data is OPENSSL_free'd. | |
412 | * If the data in |pkt| does not contain a NUL-byte, the entire data is | |
413 | * copied and NUL-terminated. | |
414 | * Returns 1 if the malloc succeeds and 0 otherwise. | |
415 | * Does not forward PACKET position (because it is typically the last thing done | |
416 | * with a given PACKET). | |
417 | */ | |
418 | __owur static inline int PACKET_strndup(const PACKET *pkt, char **data) | |
419 | { | |
420 | OPENSSL_free(*data); | |
421 | *data = BUF_strndup((const char*)pkt->curr, PACKET_remaining(pkt)); | |
422 | return (*data != NULL); | |
423 | } | |
424 | ||
7e729bb5 MC |
425 | /* Move the current reading position forward |len| bytes */ |
426 | __owur static inline int PACKET_forward(PACKET *pkt, size_t len) | |
427 | { | |
428 | if (PACKET_remaining(pkt) < len) | |
429 | return 0; | |
430 | ||
6a12a574 | 431 | packet_forward(pkt, len); |
7e729bb5 MC |
432 | |
433 | return 1; | |
434 | } | |
435 | ||
ec30e856 EK |
436 | /* |
437 | * Reads a variable-length vector prefixed with a one-byte length, and stores | |
438 | * the contents in |subpkt|. |pkt| can equal |subpkt|. | |
439 | * Data is not copied: the |subpkt| packet will share its underlying buffer with | |
440 | * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|. | |
441 | * Upon failure, the original |pkt| and |subpkt| are not modified. | |
442 | */ | |
443 | __owur static inline int PACKET_get_length_prefixed_1(PACKET *pkt, PACKET *subpkt) | |
444 | { | |
445 | unsigned int length; | |
446 | unsigned char *data; | |
447 | PACKET tmp = *pkt; | |
448 | if (!PACKET_get_1(&tmp, &length) || | |
449 | !PACKET_get_bytes(&tmp, &data, (size_t)length)) { | |
450 | return 0; | |
451 | } | |
452 | ||
453 | *pkt = tmp; | |
6a12a574 EK |
454 | subpkt->curr = data; |
455 | subpkt->remaining = length; | |
ec30e856 EK |
456 | |
457 | return 1; | |
458 | } | |
459 | ||
460 | /* | |
461 | * Reads a variable-length vector prefixed with a two-byte length, and stores | |
462 | * the contents in |subpkt|. |pkt| can equal |subpkt|. | |
463 | * Data is not copied: the |subpkt| packet will share its underlying buffer with | |
464 | * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|. | |
465 | * Upon failure, the original |pkt| and |subpkt| are not modified. | |
466 | */ | |
467 | __owur static inline int PACKET_get_length_prefixed_2(PACKET *pkt, PACKET *subpkt) | |
468 | { | |
469 | unsigned int length; | |
470 | unsigned char *data; | |
471 | PACKET tmp = *pkt; | |
472 | if (!PACKET_get_net_2(&tmp, &length) || | |
473 | !PACKET_get_bytes(&tmp, &data, (size_t)length)) { | |
474 | return 0; | |
475 | } | |
476 | ||
477 | *pkt = tmp; | |
6a12a574 EK |
478 | subpkt->curr = data; |
479 | subpkt->remaining = length; | |
ec30e856 EK |
480 | |
481 | return 1; | |
482 | } | |
483 | ||
484 | /* | |
485 | * Reads a variable-length vector prefixed with a three-byte length, and stores | |
486 | * the contents in |subpkt|. |pkt| can equal |subpkt|. | |
487 | * Data is not copied: the |subpkt| packet will share its underlying buffer with | |
488 | * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|. | |
489 | * Upon failure, the original |pkt| and |subpkt| are not modified. | |
490 | */ | |
491 | __owur static inline int PACKET_get_length_prefixed_3(PACKET *pkt, PACKET *subpkt) | |
492 | { | |
493 | unsigned long length; | |
494 | unsigned char *data; | |
495 | PACKET tmp = *pkt; | |
496 | if (!PACKET_get_net_3(&tmp, &length) || | |
497 | !PACKET_get_bytes(&tmp, &data, (size_t)length)) { | |
498 | return 0; | |
499 | } | |
500 | ||
501 | *pkt = tmp; | |
6a12a574 EK |
502 | subpkt->curr = data; |
503 | subpkt->remaining = length; | |
ec30e856 EK |
504 | |
505 | return 1; | |
506 | } | |
7e729bb5 MC |
507 | # ifdef __cplusplus |
508 | } | |
509 | # endif | |
510 | ||
511 | #endif /* HEADER_PACKET_LOCL_H */ |