Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / lib / gnutls_range.c
1 /*
2  * Copyright (C) 2012 INRIA Paris-Rocquencourt
3  *
4  * Author: Alfredo Pironti
5  *
6  * This file is part of GnuTLS.
7  *
8  * The GnuTLS 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 #include "gnutls_int.h"
24 #include "gnutls_errors.h"
25 #include "algorithms.h"
26 #include "gnutls_constate.h"
27 #include "gnutls_record.h"
28
29 static void
30 _gnutls_set_range(gnutls_range_st * dst, const size_t low,
31                   const size_t high)
32 {
33         dst->low = low;
34         dst->high = high;
35         return;
36 }
37
38 /*
39  * Returns how much LH pad we can put in this fragment, given we'll
40  * put at least data_length bytes of user data.
41  */
42 static ssize_t
43 _gnutls_range_max_lh_pad(gnutls_session_t session, ssize_t data_length,
44                          ssize_t max_frag)
45 {
46         int ret;
47         ssize_t max_pad;
48         unsigned int fixed_pad;
49         record_parameters_st *record_params;
50         ssize_t this_pad;
51         ssize_t block_size;
52         ssize_t tag_size, overflow;
53
54         ret =
55             _gnutls_epoch_get(session, EPOCH_WRITE_CURRENT,
56                               &record_params);
57         if (ret < 0) {
58                 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
59         }
60
61         max_pad = MAX_PAD_SIZE;
62         fixed_pad = 1;
63
64         this_pad = MIN(max_pad, max_frag - data_length);
65
66         block_size = _gnutls_cipher_get_block_size(record_params->cipher);
67         tag_size =
68             _gnutls_auth_cipher_tag_len(&record_params->write.
69                                         cipher_state);
70         switch (_gnutls_cipher_is_block(record_params->cipher)) {
71         case CIPHER_STREAM:
72                 return this_pad;
73
74         case CIPHER_BLOCK:
75                 overflow =
76                     (data_length + this_pad + tag_size +
77                      fixed_pad) % block_size;
78                 if (overflow > this_pad) {
79                         return this_pad;
80                 } else {
81                         return this_pad - overflow;
82                 }
83         default:
84                 return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
85         }
86 }
87
88 /**
89  * gnutls_record_can_use_length_hiding:
90  * @session: is a #gnutls_session_t structure.
91  *
92  * If the session supports length-hiding padding, you can
93  * invoke gnutls_range_send_message() to send a message whose
94  * length is hidden in the given range. If the session does not
95  * support length hiding padding, you can use the standard
96  * gnutls_record_send() function, or gnutls_range_send_message()
97  * making sure that the range is the same as the length of the
98  * message you are trying to send.
99  *
100  * Returns: true (1) if the current session supports length-hiding
101  * padding, false (0) if the current session does not.
102  **/
103 int gnutls_record_can_use_length_hiding(gnutls_session_t session)
104 {
105         int ret;
106         record_parameters_st *record_params;
107
108         if (get_num_version(session) == GNUTLS_SSL3)
109                 return 0;
110
111         ret =
112             _gnutls_epoch_get(session, EPOCH_WRITE_CURRENT,
113                               &record_params);
114         if (ret < 0) {
115                 return 0;
116         }
117
118         switch (_gnutls_cipher_is_block(record_params->cipher)) {
119         case CIPHER_BLOCK:
120                 return 1;
121         case CIPHER_STREAM:
122         default:
123                 return 0;
124         }
125 }
126
127 /**
128  * gnutls_range_split:
129  * @session: is a #gnutls_session_t structure
130  * @orig: is the original range provided by the user
131  * @next: is the returned range that can be conveyed in a TLS record
132  * @remainder: is the returned remaining range
133  *
134  * This function should be used when it is required to hide the length
135  * of very long data that cannot be directly provided to gnutls_record_send_range().
136  * In that case this function should be called with the desired length
137  * hiding range in @orig. The returned @next value should then be used in
138  * the next call to gnutls_record_send_range() with the partial data.
139  * That process should be repeated until @remainder is (0,0).
140  *
141  * Returns: 0 in case splitting succeeds, non zero in case of error.
142  * Note that @orig is not changed, while the values of @next
143  * and @remainder are modified to store the resulting values.
144  */
145 int
146 gnutls_range_split(gnutls_session_t session,
147                    const gnutls_range_st * orig,
148                    gnutls_range_st * next, gnutls_range_st * remainder)
149 {
150         int ret;
151         ssize_t max_frag;
152         ssize_t orig_low = (ssize_t) orig->low;
153         ssize_t orig_high = (ssize_t) orig->high;
154         record_parameters_st *record_params;
155
156         ret =
157             _gnutls_epoch_get(session, EPOCH_WRITE_CURRENT,
158                               &record_params);
159         if (ret < 0)
160                 return gnutls_assert_val(ret);
161
162         max_frag = max_user_send_size(session, record_params);
163
164         if (orig_high == orig_low) {
165                 int length = MIN(orig_high, max_frag);
166                 int rem = orig_high - length;
167                 _gnutls_set_range(next, length, length);
168                 _gnutls_set_range(remainder, rem, rem);
169
170                 return 0;
171         } else {
172                 if (orig_low >= max_frag) {
173                         _gnutls_set_range(next, max_frag, max_frag);
174                         _gnutls_set_range(remainder, orig_low - max_frag,
175                                           orig_high - max_frag);
176                 } else {
177                         ret =
178                             _gnutls_range_max_lh_pad(session, orig_low,
179                                                      max_frag);
180                         if (ret < 0)
181                                 return gnutls_assert_val(ret);
182
183                         ssize_t this_pad = MIN(ret, orig_high - orig_low);
184
185                         _gnutls_set_range(next, orig_low,
186                                           orig_low + this_pad);
187                         _gnutls_set_range(remainder, 0,
188                                           orig_high - (orig_low +
189                                                        this_pad));
190                 }
191
192                 return 0;
193         }
194 }
195
196 static size_t
197 _gnutls_range_fragment(size_t data_size, gnutls_range_st cur,
198                        gnutls_range_st next)
199 {
200         return MIN(cur.high, data_size - next.low);
201 }
202
203 /**
204  * gnutls_record_send_range:
205  * @session: is a #gnutls_session_t structure.
206  * @data: contains the data to send.
207  * @data_size: is the length of the data.
208  * @range: is the range of lengths in which the real data length must be hidden.
209  *
210  * This function operates like gnutls_record_send() but, while
211  * gnutls_record_send() adds minimal padding to each TLS record,
212  * this function uses the TLS extra-padding feature to conceal the real
213  * data size within the range of lengths provided.
214  * Some TLS sessions do not support extra padding (e.g. stream ciphers in standard
215  * TLS or SSL3 sessions). To know whether the current session supports extra
216  * padding, and hence length hiding, use the gnutls_record_can_use_length_hiding()
217  * function.
218  *
219  * Note: This function currently is only limited to blocking sockets.
220  *
221  * Returns: The number of bytes sent (that is data_size in a successful invocation),
222  * or a negative error code.
223  **/
224 ssize_t
225 gnutls_record_send_range(gnutls_session_t session, const void *data,
226                          size_t data_size, const gnutls_range_st * range)
227 {
228         size_t sent = 0;
229         size_t next_fragment_length;
230         ssize_t ret;
231         gnutls_range_st cur_range, next_range;
232
233         /* sanity check on range and data size */
234         if (range->low > range->high ||
235             data_size < range->low || data_size > range->high) {
236                 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
237         }
238
239         ret = gnutls_record_can_use_length_hiding(session);
240         if (ret == 0)
241                 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
242
243         _gnutls_set_range(&cur_range, range->low, range->high);
244
245         _gnutls_record_log
246             ("RANGE: Preparing message with size %d, range (%d,%d)\n",
247              (int) data_size, (int) range->low, (int) range->high);
248
249         while (cur_range.high != 0) {
250                 ret =
251                     gnutls_range_split(session, &cur_range, &cur_range,
252                                        &next_range);
253                 if (ret < 0) {
254                         return ret;     /* already gnutls_assert_val'd */
255                 }
256
257                 next_fragment_length =
258                     _gnutls_range_fragment(data_size, cur_range,
259                                            next_range);
260
261                 _gnutls_record_log
262                     ("RANGE: Next fragment size: %d (%d,%d); remaining range: (%d,%d)\n",
263                      (int) next_fragment_length, (int) cur_range.low,
264                      (int) cur_range.high, (int) next_range.low,
265                      (int) next_range.high);
266
267                 ret =
268                     _gnutls_send_tlen_int(session, GNUTLS_APPLICATION_DATA,
269                                           -1, EPOCH_WRITE_CURRENT,
270                                           &(((char *) data)[sent]),
271                                           next_fragment_length,
272                                           cur_range.high -
273                                           next_fragment_length,
274                                           MBUFFER_FLUSH);
275
276                 while (ret == GNUTLS_E_AGAIN
277                        || ret == GNUTLS_E_INTERRUPTED) {
278                         ret =
279                             _gnutls_send_tlen_int(session,
280                                                   GNUTLS_APPLICATION_DATA,
281                                                   -1, EPOCH_WRITE_CURRENT,
282                                                   NULL, 0, 0,
283                                                   MBUFFER_FLUSH);
284                 }
285
286                 if (ret < 0) {
287                         return gnutls_assert_val(ret);
288                 }
289                 if (ret != (ssize_t) next_fragment_length) {
290                         _gnutls_record_log
291                             ("RANGE: ERROR: ret = %d; next_fragment_length = %d\n",
292                              (int) ret, (int) next_fragment_length);
293                         return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
294                 }
295                 sent += next_fragment_length;
296                 data_size -= next_fragment_length;
297                 _gnutls_set_range(&cur_range, next_range.low,
298                                   next_range.high);
299         }
300
301         return sent;
302 }