Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / lib / gnutls_mbuffers.h
1 /*
2  * Copyright (C) 2009-2012 Free Software Foundation, Inc.
3  *
4  * Author: Jonathan Bastien-Filiatrault
5  *
6  * This file is part of GNUTLS.
7  *
8  * The GNUTLS library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>
20  *
21  */
22
23 #ifndef GNUTLS_MBUFFERS_H
24 #define GNUTLS_MBUFFERS_H
25
26 #include <gnutls_int.h>
27 #include <gnutls_errors.h>
28
29 void _mbuffer_head_init(mbuffer_head_st * buf);
30 void _mbuffer_head_clear(mbuffer_head_st * buf);
31 void _mbuffer_enqueue(mbuffer_head_st * buf, mbuffer_st * bufel);
32 mbuffer_st *_mbuffer_dequeue(mbuffer_head_st * buf, mbuffer_st * bufel);
33 int _mbuffer_head_remove_bytes(mbuffer_head_st * buf, size_t bytes);
34 mbuffer_st *_mbuffer_alloc(size_t maximum_size);
35 int _mbuffer_linearize(mbuffer_head_st * buf);
36
37 mbuffer_st *_mbuffer_head_get_first(mbuffer_head_st * buf,
38                                     gnutls_datum_t * msg);
39 mbuffer_st *_mbuffer_head_get_next(mbuffer_st * cur, gnutls_datum_t * msg);
40
41 mbuffer_st *_mbuffer_head_pop_first(mbuffer_head_st * buf);
42
43 /* This is dangerous since it will replace bufel with a new
44  * one.
45  */
46 int _mbuffer_append_data(mbuffer_st * bufel, void *newdata,
47                          size_t newdata_size);
48
49
50 /* For "user" use. One can have buffer data and header.
51  */
52
53 inline static void *_mbuffer_get_uhead_ptr(mbuffer_st * bufel)
54 {
55         return bufel->msg.data + bufel->mark;
56 }
57
58 inline static void *_mbuffer_get_udata_ptr(mbuffer_st * bufel)
59 {
60         return bufel->msg.data + bufel->uhead_mark + bufel->mark;
61 }
62
63 inline static void _mbuffer_set_udata_size(mbuffer_st * bufel, size_t size)
64 {
65         bufel->msg.size = size + bufel->uhead_mark + bufel->mark;
66 }
67
68 inline static void
69 _mbuffer_set_udata(mbuffer_st * bufel, void *data, size_t data_size)
70 {
71         memcpy(_mbuffer_get_udata_ptr(bufel), data,
72                data_size);
73         _mbuffer_set_udata_size(bufel, data_size);
74 }
75
76 inline static size_t _mbuffer_get_udata_size(mbuffer_st * bufel)
77 {
78         return bufel->msg.size - bufel->uhead_mark - bufel->mark;
79 }
80
81 /* discards size bytes from the begging of the buffer */
82 inline static void
83 _mbuffer_consume(mbuffer_head_st * buf, mbuffer_st * bufel, size_t size)
84 {
85         bufel->uhead_mark = 0;
86         if (bufel->mark + size < bufel->msg.size)
87                 bufel->mark += size;
88         else
89                 bufel->mark = bufel->msg.size;
90
91         buf->byte_length -= size;
92 }
93
94 inline static size_t _mbuffer_get_uhead_size(mbuffer_st * bufel)
95 {
96         return bufel->uhead_mark;
97 }
98
99 inline static void _mbuffer_set_uhead_size(mbuffer_st * bufel, size_t size)
100 {
101         bufel->uhead_mark = size;
102 }
103
104
105
106 inline static mbuffer_st *_gnutls_handshake_alloc(gnutls_session_t session,
107                                                   size_t maximum)
108 {
109         mbuffer_st *bufel =
110             _mbuffer_alloc(HANDSHAKE_HEADER_SIZE(session) + maximum);
111
112         if (!bufel)
113                 return NULL;
114
115         _mbuffer_set_uhead_size(bufel, HANDSHAKE_HEADER_SIZE(session));
116         _mbuffer_set_udata_size(bufel, maximum);
117
118         return bufel;
119 }
120
121 /* Free a segment, if the pointer is not NULL
122  *
123  * We take a ** to detect and fix double free bugs (the dangling
124  * pointer case). It also makes sure the pointer has a known value
125  * after freeing.
126  */
127 inline static void _mbuffer_xfree(mbuffer_st ** bufel)
128 {
129         if (*bufel)
130                 gnutls_free(*bufel);
131
132         *bufel = NULL;
133 }
134
135 #ifdef ENABLE_ALIGN16
136 mbuffer_st *_mbuffer_alloc_align16(size_t maximum_size, unsigned align_pos);
137 int _mbuffer_linearize_align16(mbuffer_head_st * buf, unsigned align_pos);
138 #else
139 # define _mbuffer_alloc_align16(x,y) _mbuffer_alloc(x)
140 # define _mbuffer_linearize_align16(x,y) _mbuffer_linearize(x)
141 #endif
142
143 #endif