2 * Copyright (C) 2001-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
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.
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.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>
23 /* This file contains the code for the Max Record Size TLS extension.
26 #include "gnutls_int.h"
29 #include <hello_ext.h>
30 #include <ext/max_record.h>
32 static int _gnutls_max_record_recv_params(gnutls_session_t session,
35 static int _gnutls_max_record_send_params(gnutls_session_t session,
36 gnutls_buffer_st * extdata);
38 /* Maps record size to numbers according to the
41 static int _gnutls_mre_num2record(int num);
42 static int _gnutls_mre_record2num(uint16_t record_size);
45 const hello_ext_entry_st ext_mod_max_record_size = {
46 .name = "Maximum Record Size",
48 .gid = GNUTLS_EXTENSION_MAX_RECORD_SIZE,
49 .parse_type = GNUTLS_EXT_TLS,
50 .validity = GNUTLS_EXT_FLAG_TLS | GNUTLS_EXT_FLAG_DTLS | GNUTLS_EXT_FLAG_CLIENT_HELLO |
51 GNUTLS_EXT_FLAG_EE | GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO,
52 .recv_func = _gnutls_max_record_recv_params,
53 .send_func = _gnutls_max_record_send_params
57 * In case of a server: if a MAX_RECORD_SIZE extension type is received then it stores
58 * into the session the new value. The server may use gnutls_get_max_record_size(),
59 * in order to access it.
61 * In case of a client: If a different max record size (than the default) has
62 * been specified then it sends the extension.
67 _gnutls_max_record_recv_params(gnutls_session_t session,
68 const uint8_t * data, size_t _data_size)
71 ssize_t data_size = _data_size;
73 if (session->internals.hsk_flags & HSK_RECORD_SIZE_LIMIT_NEGOTIATED)
76 if (session->security_parameters.entity == GNUTLS_SERVER) {
78 DECR_LEN(data_size, 1);
80 new_size = _gnutls_mre_num2record(data[0]);
87 session->security_parameters.max_record_send_size =
89 session->security_parameters.max_record_recv_size =
92 } else { /* CLIENT SIDE - we must check if the sent record size is the right one
97 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
100 new_size = _gnutls_mre_num2record(data[0]);
107 if (new_size != session->security_parameters.
108 max_user_record_send_size) {
110 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
112 session->security_parameters.
113 max_record_send_size = new_size;
114 session->security_parameters.
115 max_record_recv_size = new_size;
126 /* returns data_size or a negative number on failure
129 _gnutls_max_record_send_params(gnutls_session_t session,
130 gnutls_buffer_st * extdata)
135 /* this function sends the client extension data (dnsname) */
136 if (session->security_parameters.entity == GNUTLS_CLIENT) {
137 /* if the user limits for sending and receiving are
138 * different, that means the programmer had chosen to
139 * use record_size_limit instead */
140 if (session->security_parameters.max_user_record_send_size !=
141 session->security_parameters.max_user_record_recv_size)
144 if (session->security_parameters.max_user_record_send_size !=
145 DEFAULT_MAX_RECORD_SIZE) {
146 ret = _gnutls_mre_record2num
147 (session->security_parameters.
148 max_user_record_send_size);
150 /* it's not an error, as long as we send the
151 * record_size_limit extension with that value */
156 ret = _gnutls_buffer_append_data(extdata, &p, 1);
158 return gnutls_assert_val(ret);
163 } else { /* server side */
165 if (session->internals.hsk_flags & HSK_RECORD_SIZE_LIMIT_SENT)
168 if (session->security_parameters.max_record_recv_size !=
169 DEFAULT_MAX_RECORD_SIZE) {
170 ret = _gnutls_mre_record2num
171 (session->security_parameters.
172 max_record_recv_size);
174 return gnutls_assert_val(ret);
177 ret = _gnutls_buffer_append_data(extdata, &p, 1);
179 return gnutls_assert_val(ret);
189 /* Maps numbers to record sizes according to the
192 static int _gnutls_mre_num2record(int num)
204 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
208 /* Maps record size to numbers according to the
211 static int _gnutls_mre_record2num(uint16_t record_size)
213 switch (record_size) {
223 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
229 * gnutls_record_get_max_size:
230 * @session: is a #gnutls_session_t type.
232 * Get the record size. The maximum record size is negotiated by the
233 * client after the first handshake message.
235 * Returns: The maximum record packet size in this connection.
237 size_t gnutls_record_get_max_size(gnutls_session_t session)
239 /* Recv will hold the negotiated max record size
242 return session->security_parameters.max_record_recv_size;
247 * gnutls_record_set_max_size:
248 * @session: is a #gnutls_session_t type.
249 * @size: is the new size
251 * This function sets the maximum amount of plaintext sent and
252 * received in a record in this connection.
254 * Prior to 3.6.4, this function was implemented using a TLS extension
255 * called 'max fragment length', which limits the acceptable values to
256 * 512(=2^9), 1024(=2^10), 2048(=2^11) and 4096(=2^12).
258 * Since 3.6.4, the limit is also negotiated through a new TLS
259 * extension called 'record size limit', which doesn't have the
260 * limitation, as long as the value ranges between 512 and 16384.
261 * Note that while the 'record size limit' extension is preferred, not
262 * all TLS implementations use or even understand the extension.
264 * Deprecated: if the client can assume that the 'record size limit'
265 * extension is supported by the server, we recommend using
266 * gnutls_record_set_max_recv_size() instead.
268 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
269 * otherwise a negative error code is returned.
271 ssize_t gnutls_record_set_max_size(gnutls_session_t session, size_t size)
273 if (size < MIN_RECORD_SIZE || size > DEFAULT_MAX_RECORD_SIZE)
274 return GNUTLS_E_INVALID_REQUEST;
276 if (session->internals.handshake_in_progress)
277 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
279 session->security_parameters.max_user_record_send_size = size;
280 session->security_parameters.max_user_record_recv_size = size;
286 * gnutls_record_set_max_recv_size:
287 * @session: is a #gnutls_session_t type.
288 * @size: is the new size
290 * This function sets the maximum amount of plaintext received in a
291 * record in this connection.
293 * The limit is also negotiated through a TLS extension called 'record
294 * size limit'. Note that while the 'record size limit' extension is
295 * preferred, not all TLS implementations use or even understand the
298 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
299 * otherwise a negative error code is returned.
303 ssize_t gnutls_record_set_max_recv_size(gnutls_session_t session, size_t size)
306 (session->internals.allow_small_records ?
307 MIN_RECORD_SIZE_SMALL : MIN_RECORD_SIZE) ||
308 size > DEFAULT_MAX_RECORD_SIZE)
309 return GNUTLS_E_INVALID_REQUEST;
311 if (session->internals.handshake_in_progress)
312 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
314 session->security_parameters.max_user_record_recv_size = size;