PACKET: add methods for reading length-prefixed TLS vectors.
[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  * Returns a pointer to the PACKET's current position.
92  * For use in non-PACKETized APIs.
93  * TODO(openssl-team): this should return 'const unsigned char*' but can't
94  * currently because legacy code passes 'unsigned char*'s around.
95  */
96 static inline unsigned char *PACKET_data(const PACKET *pkt)
97 {
98     return pkt->curr;
99 }
100
101 /*
102  * Initialise a PACKET with |len| bytes held in |buf|. This does not make a
103  * copy of the data so |buf| must be present for the whole time that the PACKET
104  * is being used.
105  */
106 static inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf, size_t len)
107 {
108     pkt->start = pkt->curr = buf;
109     pkt->end = pkt->start + len;
110
111     /* Sanity checks */
112     if (pkt->start > pkt->end
113             || pkt->curr < pkt->start
114             || pkt->curr > pkt->end
115             || len != (size_t)(pkt->end - pkt->start)) {
116         return 0;
117     }
118
119     return 1;
120 }
121
122 /*
123  * Peek ahead and initialize |subpkt| with the next |len| bytes read from |pkt|.
124  * Data is not copied: the |subpkt| packet will share its underlying buffer with
125  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
126  */
127 __owur static inline int PACKET_peek_sub_packet(const PACKET *pkt,
128                                                 PACKET *subpkt, size_t len)
129 {
130     if (PACKET_remaining(pkt) < len)
131         return 0;
132
133     PACKET_buf_init(subpkt, pkt->curr, len);
134
135     return 1;
136 }
137
138 /*
139  * Initialize |subpkt| with the next |len| bytes read from |pkt|. Data is not
140  * copied: the |subpkt| packet will share its underlying buffer with the
141  * original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
142  */
143 __owur static inline int PACKET_get_sub_packet(PACKET *pkt, PACKET *subpkt,
144                                                size_t len)
145 {
146     if (!PACKET_peek_sub_packet(pkt, subpkt, len))
147         return 0;
148
149     pkt->curr += len;
150
151     return 1;
152 }
153
154 /* Peek ahead at 2 bytes in network order from |pkt| and store the value in
155  * |*data|
156  */
157 __owur static inline int PACKET_peek_net_2(const PACKET *pkt,
158                                            unsigned int *data)
159 {
160     if (PACKET_remaining(pkt) < 2)
161         return 0;
162
163     *data  = ((unsigned int)(*pkt->curr)) <<  8;
164     *data |= *(pkt->curr + 1);
165
166     return 1;
167 }
168
169 /* Equivalent of n2s */
170 /* Get 2 bytes in network order from |pkt| and store the value in |*data| */
171 __owur static inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)
172 {
173     if (!PACKET_peek_net_2(pkt, data))
174         return 0;
175
176     pkt->curr += 2;
177
178     return 1;
179 }
180
181 /* Peek ahead at 3 bytes in network order from |pkt| and store the value in
182  * |*data|
183  */
184 __owur static inline int PACKET_peek_net_3(const PACKET *pkt,
185                                            unsigned long *data)
186 {
187     if (PACKET_remaining(pkt) < 3)
188         return 0;
189
190     *data  = ((unsigned long)(*pkt->curr)) << 16;
191     *data |= ((unsigned long)(*(pkt->curr + 1))) <<  8;
192     *data |= *(pkt->curr + 2);
193
194     return 1;
195 }
196
197 /* Equivalent of n2l3 */
198 /* Get 3 bytes in network order from |pkt| and store the value in |*data| */
199 __owur static inline int PACKET_get_net_3(PACKET *pkt, unsigned long *data)
200 {
201     if (!PACKET_peek_net_3(pkt, data))
202         return 0;
203
204     pkt->curr += 3;
205
206     return 1;
207 }
208
209 /* Peek ahead at 4 bytes in network order from |pkt| and store the value in
210  * |*data|
211  */
212 __owur static inline int PACKET_peek_net_4(const PACKET *pkt,
213                                            unsigned long *data)
214 {
215     if (PACKET_remaining(pkt) < 4)
216         return 0;
217
218     *data  = ((unsigned long)(*pkt->curr)) << 24;
219     *data |= ((unsigned long)(*(pkt->curr + 1))) << 16;
220     *data |= ((unsigned long)(*(pkt->curr + 2))) <<  8;
221     *data |= *(pkt->curr+3);
222
223     return 1;
224 }
225
226 /* Equivalent of n2l */
227 /* Get 4 bytes in network order from |pkt| and store the value in |*data| */
228 __owur static inline int PACKET_get_net_4(PACKET *pkt, unsigned long *data)
229 {
230     if (!PACKET_peek_net_4(pkt, data))
231         return 0;
232
233     pkt->curr += 4;
234
235     return 1;
236 }
237
238 /* Peek ahead at 1 byte from |pkt| and store the value in |*data| */
239 __owur static inline int PACKET_peek_1(const PACKET *pkt, unsigned int *data)
240 {
241     if (!PACKET_remaining(pkt))
242         return 0;
243
244     *data = *pkt->curr;
245
246     return 1;
247 }
248
249 /* Get 1 byte from |pkt| and store the value in |*data| */
250 __owur static inline int PACKET_get_1(PACKET *pkt, unsigned int *data)
251 {
252     if (!PACKET_peek_1(pkt, data))
253         return 0;
254
255     pkt->curr++;
256
257     return 1;
258 }
259
260 /*
261  * Peek ahead at 4 bytes in reverse network order from |pkt| and store the value
262  * in |*data|
263  */
264 __owur static inline int PACKET_peek_4(const PACKET *pkt, unsigned long *data)
265 {
266     if (PACKET_remaining(pkt) < 4)
267         return 0;
268
269     *data  = *pkt->curr;
270     *data |= ((unsigned long)(*(pkt->curr + 1))) <<  8;
271     *data |= ((unsigned long)(*(pkt->curr + 2))) << 16;
272     *data |= ((unsigned long)(*(pkt->curr + 3))) << 24;
273
274     return 1;
275 }
276
277 /* Equivalent of c2l */
278 /*
279  * Get 4 bytes in reverse network order from |pkt| and store the value in
280  * |*data|
281  */
282 __owur static inline int PACKET_get_4(PACKET *pkt, unsigned long *data)
283 {
284     if (!PACKET_peek_4(pkt, data))
285         return 0;
286
287     pkt->curr += 4;
288
289     return 1;
290 }
291
292 /*
293  * Peek ahead at |len| bytes from the |pkt| and store a pointer to them in
294  * |*data|. This just points at the underlying buffer that |pkt| is using. The
295  * caller should not free this data directly (it will be freed when the
296  * underlying buffer gets freed
297  */
298 __owur static inline int PACKET_peek_bytes(const PACKET *pkt, unsigned char **data,
299                                           size_t len)
300 {
301     if (PACKET_remaining(pkt) < len)
302         return 0;
303
304     *data = pkt->curr;
305
306     return 1;
307 }
308
309 /*
310  * Read |len| bytes from the |pkt| and store a pointer to them in |*data|. This
311  * just points at the underlying buffer that |pkt| is using. The caller should
312  * not free this data directly (it will be freed when the underlying buffer gets
313  * freed
314  */
315 __owur static inline int PACKET_get_bytes(PACKET *pkt, unsigned char **data,
316                                           size_t len)
317 {
318     if (!PACKET_peek_bytes(pkt, data, len))
319         return 0;
320
321     pkt->curr += len;
322
323     return 1;
324 }
325
326 /* Peek ahead at |len| bytes from |pkt| and copy them to |data| */
327 __owur static inline int PACKET_peek_copy_bytes(const PACKET *pkt,
328                                                 unsigned char *data, size_t len)
329 {
330     if (PACKET_remaining(pkt) < len)
331         return 0;
332
333     memcpy(data, pkt->curr, len);
334
335     return 1;
336 }
337
338 /* Read |len| bytes from |pkt| and copy them to |data| */
339 __owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data,
340                                           size_t len)
341 {
342     if (!PACKET_peek_copy_bytes(pkt, data, len))
343         return 0;
344
345     pkt->curr += len;
346
347     return 1;
348 }
349
350 /* Move the current reading position back |len| bytes */
351 __owur static inline int PACKET_back(PACKET *pkt, size_t len)
352 {
353     if (len > (size_t)(pkt->curr - pkt->start))
354         return 0;
355
356     pkt->curr -= len;
357
358     return 1;
359 }
360
361 /* Move the current reading position forward |len| bytes */
362 __owur static inline int PACKET_forward(PACKET *pkt, size_t len)
363 {
364     if (PACKET_remaining(pkt) < len)
365         return 0;
366
367     pkt->curr += len;
368
369     return 1;
370 }
371
372 /* Store a bookmark for the current reading position in |*bm| */
373 __owur static inline int PACKET_get_bookmark(const PACKET *pkt, size_t *bm)
374 {
375     *bm = pkt->curr - pkt->start;
376
377     return 1;
378 }
379
380 /* Set the current reading position to the bookmark |bm| */
381 __owur static inline int PACKET_goto_bookmark(PACKET *pkt, size_t bm)
382 {
383     if (bm > (size_t)(pkt->end - pkt->start))
384         return 0;
385
386     pkt->curr = pkt->start + bm;
387
388     return 1;
389 }
390
391 /*
392  * Stores the total length of the packet we have in the underlying buffer in
393  * |*len|
394  */
395 __owur static inline int PACKET_length(const PACKET *pkt, size_t *len)
396 {
397     *len = pkt->end - pkt->start;
398
399     return 1;
400 }
401
402 /*
403  * Reads a variable-length vector prefixed with a one-byte length, and stores
404  * the contents in |subpkt|. |pkt| can equal |subpkt|.
405  * Data is not copied: the |subpkt| packet will share its underlying buffer with
406  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
407  * Upon failure, the original |pkt| and |subpkt| are not modified.
408  */
409 __owur static inline int PACKET_get_length_prefixed_1(PACKET *pkt, PACKET *subpkt)
410 {
411   unsigned int length;
412   unsigned char *data;
413   PACKET tmp = *pkt;
414   if (!PACKET_get_1(&tmp, &length) ||
415       !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
416       return 0;
417   }
418
419   *pkt = tmp;
420   subpkt->start = subpkt->curr = data;
421   subpkt->end = subpkt->start + length;
422
423   return 1;
424 }
425
426 /*
427  * Reads a variable-length vector prefixed with a two-byte length, and stores
428  * the contents in |subpkt|. |pkt| can equal |subpkt|.
429  * Data is not copied: the |subpkt| packet will share its underlying buffer with
430  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
431  * Upon failure, the original |pkt| and |subpkt| are not modified.
432  */
433 __owur static inline int PACKET_get_length_prefixed_2(PACKET *pkt, PACKET *subpkt)
434 {
435   unsigned int length;
436   unsigned char *data;
437   PACKET tmp = *pkt;
438   if (!PACKET_get_net_2(&tmp, &length) ||
439       !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
440       return 0;
441   }
442
443   *pkt = tmp;
444   subpkt->start = subpkt->curr = data;
445   subpkt->end = subpkt->start + length;
446
447   return 1;
448 }
449
450 /*
451  * Reads a variable-length vector prefixed with a three-byte length, and stores
452  * the contents in |subpkt|. |pkt| can equal |subpkt|.
453  * Data is not copied: the |subpkt| packet will share its underlying buffer with
454  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
455  * Upon failure, the original |pkt| and |subpkt| are not modified.
456  */
457 __owur static inline int PACKET_get_length_prefixed_3(PACKET *pkt, PACKET *subpkt)
458 {
459   unsigned long length;
460   unsigned char *data;
461   PACKET tmp = *pkt;
462   if (!PACKET_get_net_3(&tmp, &length) ||
463       !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
464       return 0;
465   }
466
467   *pkt = tmp;
468   subpkt->start = subpkt->curr = data;
469   subpkt->end = subpkt->start + length;
470
471   return 1;
472 }
473 # ifdef __cplusplus
474 }
475 # endif
476
477 #endif /* HEADER_PACKET_LOCL_H */