1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/*
* Copyright (C) 2001-2012 Free Software Foundation, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* The GnuTLS is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*
*/
/* This file contains the code for the Max Record Size TLS extension.
*/
#include "gnutls_int.h"
#include "errors.h"
#include "num.h"
#include "hello_ext.h"
#include "ext/max_record.h"
static int _gnutls_max_record_recv_params(gnutls_session_t session,
const uint8_t *data,
size_t data_size);
static int _gnutls_max_record_send_params(gnutls_session_t session,
gnutls_buffer_st *extdata);
/* Maps record size to numbers according to the
* extensions draft.
*/
static int _gnutls_mre_num2record(int num);
static int _gnutls_mre_record2num(uint16_t record_size);
const hello_ext_entry_st ext_mod_max_record_size = {
.name = "Maximum Record Size",
.tls_id = 1,
.gid = GNUTLS_EXTENSION_MAX_RECORD_SIZE,
.client_parse_point = GNUTLS_EXT_TLS,
.server_parse_point = GNUTLS_EXT_TLS,
.validity = GNUTLS_EXT_FLAG_TLS | GNUTLS_EXT_FLAG_DTLS |
GNUTLS_EXT_FLAG_CLIENT_HELLO | GNUTLS_EXT_FLAG_EE |
GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO,
.recv_func = _gnutls_max_record_recv_params,
.send_func = _gnutls_max_record_send_params
};
/*
* In case of a server: if a MAX_RECORD_SIZE extension type is received then it stores
* into the session the new value. The server may use gnutls_get_max_record_size(),
* in order to access it.
*
* In case of a client: If a different max record size (than the default) has
* been specified then it sends the extension.
*
*/
static int _gnutls_max_record_recv_params(gnutls_session_t session,
const uint8_t *data, size_t data_size)
{
ssize_t new_size;
if (session->internals.hsk_flags & HSK_RECORD_SIZE_LIMIT_NEGOTIATED)
return 0;
if (session->security_parameters.entity == GNUTLS_SERVER) {
if (data_size > 0) {
DECR_LEN(data_size, 1);
new_size = _gnutls_mre_num2record(data[0]);
if (new_size < 0) {
gnutls_assert();
return new_size;
}
session->security_parameters.max_record_send_size =
new_size;
session->security_parameters.max_record_recv_size =
new_size;
}
} else { /* CLIENT SIDE - we must check if the sent record size is the right one
*/
if (data_size > 0) {
if (data_size != 1) {
gnutls_assert();
return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
}
new_size = _gnutls_mre_num2record(data[0]);
if (new_size < 0) {
gnutls_assert();
return new_size;
}
if (new_size != session->security_parameters
.max_user_record_send_size) {
gnutls_assert();
return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
} else {
session->security_parameters
.max_record_send_size = new_size;
session->security_parameters
.max_record_recv_size = new_size;
}
}
}
return 0;
}
/* returns data_size or a negative number on failure
*/
static int _gnutls_max_record_send_params(gnutls_session_t session,
gnutls_buffer_st *extdata)
{
uint8_t p;
int ret;
/* this function sends the client extension data (dnsname) */
if (session->security_parameters.entity == GNUTLS_CLIENT) {
/* if the user limits for sending and receiving are
* different, that means the programmer had chosen to
* use record_size_limit instead */
if (session->security_parameters.max_user_record_send_size !=
session->security_parameters.max_user_record_recv_size)
return 0;
if (session->security_parameters.max_user_record_send_size !=
DEFAULT_MAX_RECORD_SIZE) {
ret = _gnutls_mre_record2num(
session->security_parameters
.max_user_record_send_size);
/* it's not an error, as long as we send the
* record_size_limit extension with that value */
if (ret < 0)
return 0;
p = (uint8_t)ret;
ret = _gnutls_buffer_append_data(extdata, &p, 1);
if (ret < 0)
return gnutls_assert_val(ret);
return 1;
}
} else { /* server side */
if (session->internals.hsk_flags & HSK_RECORD_SIZE_LIMIT_SENT)
return 0;
if (session->security_parameters.max_record_recv_size !=
DEFAULT_MAX_RECORD_SIZE) {
ret = _gnutls_mre_record2num(
session->security_parameters
.max_record_recv_size);
if (ret < 0)
return gnutls_assert_val(ret);
p = (uint8_t)ret;
ret = _gnutls_buffer_append_data(extdata, &p, 1);
if (ret < 0)
return gnutls_assert_val(ret);
return 1;
}
}
return 0;
}
/* Maps numbers to record sizes according to the
* extensions draft.
*/
static int _gnutls_mre_num2record(int num)
{
switch (num) {
case 1:
return 512;
case 2:
return 1024;
case 3:
return 2048;
case 4:
return 4096;
default:
return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
}
}
/* Maps record size to numbers according to the
* extensions draft.
*/
static int _gnutls_mre_record2num(uint16_t record_size)
{
switch (record_size) {
case 512:
return 1;
case 1024:
return 2;
case 2048:
return 3;
case 4096:
return 4;
default:
return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
}
}
/**
* gnutls_record_get_max_size:
* @session: is a #gnutls_session_t type.
*
* Get the record size. The maximum record size is negotiated by the
* client after the first handshake message.
*
* Returns: The maximum record packet size in this connection.
**/
size_t gnutls_record_get_max_size(gnutls_session_t session)
{
/* Recv will hold the negotiated max record size
* always.
*/
return session->security_parameters.max_record_recv_size;
}
/**
* gnutls_record_set_max_size:
* @session: is a #gnutls_session_t type.
* @size: is the new size
*
* This function sets the maximum amount of plaintext sent and
* received in a record in this connection.
*
* Prior to 3.6.4, this function was implemented using a TLS extension
* called 'max fragment length', which limits the acceptable values to
* 512(=2^9), 1024(=2^10), 2048(=2^11) and 4096(=2^12).
*
* Since 3.6.4, the limit is also negotiated through a new TLS
* extension called 'record size limit', which doesn't have the
* limitation, as long as the value ranges between 512 and 16384.
* Note that while the 'record size limit' extension is preferred, not
* all TLS implementations use or even understand the extension.
*
* Deprecated: if the client can assume that the 'record size limit'
* extension is supported by the server, we recommend using
* gnutls_record_set_max_recv_size() instead.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
* otherwise a negative error code is returned.
**/
ssize_t gnutls_record_set_max_size(gnutls_session_t session, size_t size)
{
if (size < MIN_RECORD_SIZE || size > DEFAULT_MAX_RECORD_SIZE)
return GNUTLS_E_INVALID_REQUEST;
if (session->internals.handshake_in_progress)
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
session->security_parameters.max_user_record_send_size = size;
session->security_parameters.max_user_record_recv_size = size;
return 0;
}
/**
* gnutls_record_set_max_recv_size:
* @session: is a #gnutls_session_t type.
* @size: is the new size
*
* This function sets the maximum amount of plaintext received in a
* record in this connection.
*
* The limit is also negotiated through a TLS extension called 'record
* size limit'. Note that while the 'record size limit' extension is
* preferred, not all TLS implementations use or even understand the
* extension.
*
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
* otherwise a negative error code is returned.
*
* Since: 3.6.8
**/
ssize_t gnutls_record_set_max_recv_size(gnutls_session_t session, size_t size)
{
if (size < (session->internals.allow_small_records ?
MIN_RECORD_SIZE_SMALL :
MIN_RECORD_SIZE) ||
size > DEFAULT_MAX_RECORD_SIZE)
return GNUTLS_E_INVALID_REQUEST;
if (session->internals.handshake_in_progress)
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
session->security_parameters.max_user_record_recv_size = size;
return 0;
}
/**
* gnutls_record_get_max_send_size:
* @session: is a #gnutls_session_t type.
*
* Get the maximum plaintext send size. This maybe negotiated, or user specified.
*
* Returns: The maximum plaintext send size.
*
* Since: 3.8.11
**/
size_t gnutls_record_get_max_send_size(gnutls_session_t session)
{
return max_record_send_size(session);
}
|