0d5d823083c10f48ccceabc3a3def2d6db7e17b7
[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 "e_os.h"
66
67 # ifdef __cplusplus
68 extern "C" {
69 # endif
70
71 typedef struct {
72     /* Pointer to the start of the buffer data */
73     unsigned char *start;
74
75     /* Pointer to the first byte after the end of the buffer data */
76     unsigned char *end;
77
78     /* Pointer to where we are currently reading from */
79     unsigned char *curr;
80 } PACKET;
81
82 /*
83  * Returns the number of bytes remaining to be read in the PACKET
84  */
85 __owur static inline size_t PACKET_remaining(const PACKET *pkt)
86 {
87     return (size_t)(pkt->end - pkt->curr);
88 }
89
90 /*
91  * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
92  * copy of the data so |buf| must be present for the whole time that the PACKET
93  * is being used.
94  */
95 static inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf, size_t len)
96 {
97     pkt->start = pkt->curr = buf;
98     pkt->end = pkt->start + len;
99
100     /* Sanity checks */
101     if (pkt->start > pkt->end
102             || pkt->curr < pkt->start
103             || pkt->curr > pkt->end
104             || len != (size_t)(pkt->end - pkt->start)) {
105         return 0;
106     }
107
108     return 1;
109 }
110
111 /*
112  * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
113  * Data is not copied: the |subpkt| packet will share its underlying buffer with
114  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
115  */
116 __owur static inline int PACKET_peek_sub_packet(const PACKET *pkt,
117                                                 PACKET *subpkt, size_t len)
118 {
119     if (PACKET_remaining(pkt) < len)
120         return 0;
121
122     PACKET_buf_init(subpkt, pkt->curr, len);
123
124     return 1;
125 }
126
127 /*
128  * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
129  * copied: the |subpkt| packet will share its underlying buffer with the
130  * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
131  */
132 __owur static inline int PACKET_get_sub_packet(PACKET *pkt, PACKET *subpkt,
133                                                size_t len)
134 {
135     if (!PACKET_peek_sub_packet(pkt, subpkt, len))
136         return 0;
137
138     pkt->curr += len;
139
140     return 1;
141 }
142
143 /* Peek ahead at 2 bytes in network order from |pkt| and store the value in
144  * |*data|
145  */
146 __owur static inline int PACKET_peek_net_2(const PACKET *pkt,
147                                            unsigned int *data)
148 {
149     if (PACKET_remaining(pkt) < 2)
150         return 0;
151
152     *data  = ((unsigned int)(*pkt->curr)) <<  8;
153     *data |= *(pkt->curr + 1);
154
155     return 1;
156 }
157
158 /* Equivalent of n2s */
159 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
160 __owur static inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
161 {
162     if (!PACKET_peek_net_2(pkt, data))
163         return 0;
164
165     pkt->curr += 2;
166
167     return 1;
168 }
169
170 /* Peek ahead at 3 bytes in network order from |pkt| and store the value in
171  * |*data|
172  */
173 __owur static inline int PACKET_peek_net_3(const PACKET *pkt,
174                                            unsigned long *data)
175 {
176     if (PACKET_remaining(pkt) < 3)
177         return 0;
178
179     *data  = ((unsigned long)(*pkt->curr)) << 16;
180     *data |= ((unsigned long)(*(pkt->curr + 1))) <<  8;
181     *data |= *(pkt->curr + 2);
182
183     return 1;
184 }
185
186 /* Equivalent of n2l3 */
187 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
188 __owur static inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
189 {
190     if (!PACKET_peek_net_3(pkt, data))
191         return 0;
192
193     pkt->curr += 3;
194
195     return 1;
196 }
197
198 /* Peek ahead at 4 bytes in network order from |pkt| and store the value in
199  * |*data|
200  */
201 __owur static inline int PACKET_peek_net_4(const PACKET *pkt,
202                                            unsigned long *data)
203 {
204     if (PACKET_remaining(pkt) < 4)
205         return 0;
206
207     *data  = ((unsigned long)(*pkt->curr)) << 24;
208     *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
209     *data |= ((unsigned long)(*(pkt->curr + 2))) <<  8;
210     *data |= *(pkt->curr+3);
211
212     return 1;
213 }
214
215 /* Equivalent of n2l */
216 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
217 __owur static inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
218 {
219     if (!PACKET_peek_net_4(pkt, data))
220         return 0;
221
222     pkt->curr += 4;
223
224     return 1;
225 }
226
227 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
228 __owur static inline int PACKET_peek_1(const PACKET *pkt, unsigned int *data)
229 {
230     if (!PACKET_remaining(pkt))
231         return 0;
232
233     *data = *pkt->curr;
234
235     return 1;
236 }
237
238 /* Get 1 byte from |pkt| and store the value in |*data| */
239 __owur static inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
240 {
241     if (!PACKET_peek_1(pkt, data))
242         return 0;
243
244     pkt->curr++;
245
246     return 1;
247 }
248
249 /*
250  * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
251  * in |*data|
252  */
253 __owur static inline int PACKET_peek_4(const PACKET *pkt, unsigned long *data)
254 {
255     if (PACKET_remaining(pkt) < 4)
256         return 0;
257
258     *data  = *pkt->curr;
259     *data |= ((unsigned long)(*(pkt->curr + 1))) <<  8;
260     *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
261     *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
262
263     return 1;
264 }
265
266 /* Equivalent of c2l */
267 /*
268  * Get 4 bytes in reverse network order from |pkt| and store the value in
269  * |*data|
270  */
271 __owur static inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
272 {
273     if (!PACKET_peek_4(pkt, data))
274         return 0;
275
276     pkt->curr += 4;
277
278     return 1;
279 }
280
281 /*
282  * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
283  * |*data|. This just points at the underlying buffer that |pkt| is using. The
284  * caller should not free this data directly (it will be freed when the
285  * underlying buffer gets freed
286  */
287 __owur static inline int PACKET_peek_bytes(const PACKET *pkt, unsigned char **data,
288                                           size_t len)
289 {
290     if (PACKET_remaining(pkt) < len)
291         return 0;
292
293     *data = pkt->curr;
294
295     return 1;
296 }
297
298 /*
299  * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
300  * just points at the underlying buffer that |pkt| is using. The caller should
301  * not free this data directly (it will be freed when the underlying buffer gets
302  * freed
303  */
304 __owur static inline int PACKET_get_bytes(PACKET *pkt, unsigned char **data,
305                                           size_t len)
306 {
307     if (!PACKET_peek_bytes(pkt, data, len))
308         return 0;
309
310     pkt->curr += len;
311
312     return 1;
313 }
314
315 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
316 __owur static inline int PACKET_peek_copy_bytes(const PACKET *pkt,
317                                                 unsigned char *data, size_t len)
318 {
319     if (PACKET_remaining(pkt) < len)
320         return 0;
321
322     memcpy(data, pkt->curr, len);
323
324     return 1;
325 }
326
327 /* Read |len| bytes from |pkt| and copy them to |data| */
328 __owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data,
329                                           size_t len)
330 {
331     if (!PACKET_peek_copy_bytes(pkt, data, len))
332         return 0;
333
334     pkt->curr += len;
335
336     return 1;
337 }
338
339 /* Move the current reading position back |len| bytes */
340 __owur static inline int PACKET_back(PACKET *pkt, size_t len)
341 {
342     if (len > (size_t)(pkt->curr - pkt->start))
343         return 0;
344
345     pkt->curr -= len;
346
347     return 1;
348 }
349
350 /* Move the current reading position forward |len| bytes */
351 __owur static inline int PACKET_forward(PACKET *pkt, size_t len)
352 {
353     if (PACKET_remaining(pkt) < len)
354         return 0;
355
356     pkt->curr += len;
357
358     return 1;
359 }
360
361 /* Store a bookmark for the current reading position in |*bm| */
362 __owur static inline int PACKET_get_bookmark(const PACKET *pkt, size_t *bm)
363 {
364     *bm = pkt->curr - pkt->start;
365
366     return 1;
367 }
368
369 /* Set the current reading position to the bookmark |bm| */
370 __owur static inline int PACKET_goto_bookmark(PACKET *pkt, size_t bm)
371 {
372     if (bm > (size_t)(pkt->end - pkt->start))
373         return 0;
374
375     pkt->curr = pkt->start + bm;
376
377     return 1;
378 }
379
380 /*
381  * Stores the total length of the packet we have in the underlying buffer in
382  * |*len|
383  */
384 __owur static inline int PACKET_length(const PACKET *pkt, size_t *len)
385 {
386     *len = pkt->end - pkt->start;
387
388     return 1;
389 }
390
391 # ifdef __cplusplus
392 }
393 # endif
394
395 #endif /* HEADER_PACKET_LOCL_H */