7a72f8e658b8b9c1208adf2b1afee370501b073d
[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 /*
339  * Read |len| bytes from |pkt| and copy them to |data|.
340  * The caller is responsible for ensuring that |data| can hold |len| bytes.
341  */
342 __owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data,
343                                            size_t len)
344 {
345     if (!PACKET_peek_copy_bytes(pkt, data, len))
346         return 0;
347
348     pkt->curr += len;
349
350     return 1;
351 }
352
353 /*
354  * Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
355  * result in |*data|, and the length in |len|.
356  * If |*data| is not NULL, the old data is OPENSSL_free'd.
357  * If the packet is empty, or malloc fails, |*data| will be set to NULL.
358  * Returns 1 if the malloc succeeds and 0 otherwise.
359  * Does not forward PACKET position (because it is typically the last thing
360  * done with a given PACKET).
361  */
362 __owur static inline int PACKET_memdup(const PACKET *pkt, unsigned char **data,
363                                        size_t *len)
364 {
365     size_t length;
366
367     OPENSSL_free(*data);
368     *data = NULL;
369     *len = 0;
370
371     length = PACKET_remaining(pkt);
372
373     if (length == 0)
374         return 1;
375
376     *data = BUF_memdup(pkt->curr, length);
377
378     if (*data == NULL)
379         return 0;
380
381     *len = length;
382     return 1;
383 }
384
385 /*
386  * Read a C string from |pkt| and copy to a newly allocated, NUL-terminated
387  * buffer. Store a pointer to the result in |*data|.
388  * If |*data| is not NULL, the old data is OPENSSL_free'd.
389  * If the data in |pkt| does not contain a NUL-byte, the entire data is
390  * copied and NUL-terminated.
391  * Returns 1 if the malloc succeeds and 0 otherwise.
392  * Does not forward PACKET position (because it is typically the last thing done
393  * with a given PACKET).
394  */
395 __owur static inline int PACKET_strndup(const PACKET *pkt, char **data)
396 {
397     OPENSSL_free(*data);
398     *data = BUF_strndup((const char*)pkt->curr, PACKET_remaining(pkt));
399     return (*data != NULL);
400 }
401
402 /* Move the current reading position back |len| bytes */
403 __owur static inline int PACKET_back(PACKET *pkt, size_t len)
404 {
405     if (len > (size_t)(pkt->curr - pkt->start))
406         return 0;
407
408     pkt->curr -= len;
409
410     return 1;
411 }
412
413 /* Move the current reading position forward |len| bytes */
414 __owur static inline int PACKET_forward(PACKET *pkt, size_t len)
415 {
416     if (PACKET_remaining(pkt) < len)
417         return 0;
418
419     pkt->curr += len;
420
421     return 1;
422 }
423
424 /*
425  * Stores the total length of the packet we have in the underlying buffer in
426  * |*len|
427  */
428 __owur static inline int PACKET_length(const PACKET *pkt, size_t *len)
429 {
430     *len = pkt->end - pkt->start;
431
432     return 1;
433 }
434
435 /*
436  * Reads a variable-length vector prefixed with a one-byte length, and stores
437  * the contents in |subpkt|. |pkt| can equal |subpkt|.
438  * Data is not copied: the |subpkt| packet will share its underlying buffer with
439  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
440  * Upon failure, the original |pkt| and |subpkt| are not modified.
441  */
442 __owur static inline int PACKET_get_length_prefixed_1(PACKET *pkt, PACKET *subpkt)
443 {
444   unsigned int length;
445   unsigned char *data;
446   PACKET tmp = *pkt;
447   if (!PACKET_get_1(&tmp, &length) ||
448       !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
449       return 0;
450   }
451
452   *pkt = tmp;
453   subpkt->start = subpkt->curr = data;
454   subpkt->end = subpkt->start + length;
455
456   return 1;
457 }
458
459 /*
460  * Reads a variable-length vector prefixed with a two-byte length, and stores
461  * the contents in |subpkt|. |pkt| can equal |subpkt|.
462  * Data is not copied: the |subpkt| packet will share its underlying buffer with
463  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
464  * Upon failure, the original |pkt| and |subpkt| are not modified.
465  */
466 __owur static inline int PACKET_get_length_prefixed_2(PACKET *pkt, PACKET *subpkt)
467 {
468   unsigned int length;
469   unsigned char *data;
470   PACKET tmp = *pkt;
471   if (!PACKET_get_net_2(&tmp, &length) ||
472       !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
473       return 0;
474   }
475
476   *pkt = tmp;
477   subpkt->start = subpkt->curr = data;
478   subpkt->end = subpkt->start + length;
479
480   return 1;
481 }
482
483 /*
484  * Reads a variable-length vector prefixed with a three-byte length, and stores
485  * the contents in |subpkt|. |pkt| can equal |subpkt|.
486  * Data is not copied: the |subpkt| packet will share its underlying buffer with
487  * the original |pkt|, so data wrapped by |pkt| must outlive the |subpkt|.
488  * Upon failure, the original |pkt| and |subpkt| are not modified.
489  */
490 __owur static inline int PACKET_get_length_prefixed_3(PACKET *pkt, PACKET *subpkt)
491 {
492   unsigned long length;
493   unsigned char *data;
494   PACKET tmp = *pkt;
495   if (!PACKET_get_net_3(&tmp, &length) ||
496       !PACKET_get_bytes(&tmp, &data, (size_t)length)) {
497       return 0;
498   }
499
500   *pkt = tmp;
501   subpkt->start = subpkt->curr = data;
502   subpkt->end = subpkt->start + length;
503
504   return 1;
505 }
506 # ifdef __cplusplus
507 }
508 # endif
509
510 #endif /* HEADER_PACKET_LOCL_H */