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