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 3 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 <http://www.gnu.org/licenses/>
23 /* This file contains the code for the Max Record Size TLS extension.
26 #include "gnutls_int.h"
27 #include "gnutls_errors.h"
28 #include "gnutls_num.h"
29 #include <gnutls_extensions.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 static int _gnutls_max_record_unpack (gnutls_buffer_st
* ps
,
39 extension_priv_data_t
* _priv
);
40 static int _gnutls_max_record_pack (extension_priv_data_t _priv
,
41 gnutls_buffer_st
* ps
);
43 /* Maps record size to numbers according to the
46 static int _gnutls_mre_num2record (int num
);
47 static int _gnutls_mre_record2num (uint16_t record_size
);
50 extension_entry_st ext_mod_max_record_size
= {
51 .name
= "MAX RECORD SIZE",
52 .type
= GNUTLS_EXTENSION_MAX_RECORD_SIZE
,
53 .parse_type
= GNUTLS_EXT_TLS
,
55 .recv_func
= _gnutls_max_record_recv_params
,
56 .send_func
= _gnutls_max_record_send_params
,
57 .pack_func
= _gnutls_max_record_pack
,
58 .unpack_func
= _gnutls_max_record_unpack
,
63 * In case of a server: if a MAX_RECORD_SIZE extension type is received then it stores
64 * into the session the new value. The server may use gnutls_get_max_record_size(),
65 * in order to access it.
67 * In case of a client: If a different max record size (than the default) has
68 * been specified then it sends the extension.
73 _gnutls_max_record_recv_params (gnutls_session_t session
,
74 const uint8_t * data
, size_t _data_size
)
77 ssize_t data_size
= _data_size
;
78 extension_priv_data_t epriv
;
81 if (session
->security_parameters
.entity
== GNUTLS_SERVER
)
85 DECR_LEN (data_size
, 1);
87 new_size
= _gnutls_mre_num2record (data
[0]);
95 session
->security_parameters
.max_record_send_size
= new_size
;
96 session
->security_parameters
.max_record_recv_size
= new_size
;
100 { /* CLIENT SIDE - we must check if the sent record size is the right one
104 ret
= _gnutls_ext_get_session_data (session
,
105 GNUTLS_EXTENSION_MAX_RECORD_SIZE
,
110 return GNUTLS_E_INTERNAL_ERROR
;
116 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH
;
119 new_size
= _gnutls_mre_num2record (data
[0]);
121 if (new_size
< 0 || new_size
!= (ssize_t
)epriv
.num
)
124 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER
;
128 session
->security_parameters
.max_record_recv_size
= epriv
.num
;
139 /* returns data_size or a negative number on failure
142 _gnutls_max_record_send_params (gnutls_session_t session
, gnutls_buffer_st
* extdata
)
147 /* this function sends the client extension data (dnsname) */
148 if (session
->security_parameters
.entity
== GNUTLS_CLIENT
)
150 extension_priv_data_t epriv
;
152 ret
= _gnutls_ext_get_session_data (session
,
153 GNUTLS_EXTENSION_MAX_RECORD_SIZE
,
155 if (ret
< 0) /* it is ok not to have it */
160 if (epriv
.num
!= DEFAULT_MAX_RECORD_SIZE
)
162 p
= (uint8_t) _gnutls_mre_record2num (epriv
.num
);
163 ret
= _gnutls_buffer_append_data( extdata
, &p
, 1);
165 return gnutls_assert_val(ret
);
174 if (session
->security_parameters
.max_record_recv_size
!=
175 DEFAULT_MAX_RECORD_SIZE
)
179 _gnutls_mre_record2num
180 (session
->security_parameters
.max_record_recv_size
);
182 ret
= _gnutls_buffer_append_data( extdata
, &p
, 1);
184 return gnutls_assert_val(ret
);
195 _gnutls_max_record_pack (extension_priv_data_t epriv
, gnutls_buffer_st
* ps
)
199 BUFFER_APPEND_NUM (ps
, epriv
.num
);
206 _gnutls_max_record_unpack (gnutls_buffer_st
* ps
,
207 extension_priv_data_t
* _priv
)
209 extension_priv_data_t epriv
;
212 BUFFER_POP_NUM (ps
, epriv
.num
);
222 /* Maps numbers to record sizes according to the
226 _gnutls_mre_num2record (int num
)
239 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER
;
243 /* Maps record size to numbers according to the
247 _gnutls_mre_record2num (uint16_t record_size
)
260 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER
;
266 * gnutls_record_get_max_size:
267 * @session: is a #gnutls_session_t structure.
269 * Get the record size. The maximum record size is negotiated by the
270 * client after the first handshake message.
272 * Returns: The maximum record packet size in this connection.
275 gnutls_record_get_max_size (gnutls_session_t session
)
277 /* Recv will hold the negotiated max record size
280 return session
->security_parameters
.max_record_recv_size
;
285 * gnutls_record_set_max_size:
286 * @session: is a #gnutls_session_t structure.
287 * @size: is the new size
289 * This function sets the maximum record packet size in this
290 * connection. This property can only be set to clients. The server
291 * may choose not to accept the requested size.
293 * Acceptable values are 512(=2^9), 1024(=2^10), 2048(=2^11) and
294 * 4096(=2^12). The requested record size does get in effect
295 * immediately only while sending data. The receive part will take
296 * effect after a successful handshake.
298 * This function uses a TLS extension called 'max record size'. Not
299 * all TLS implementations use or even understand this extension.
301 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
302 * otherwise a negative error code is returned.
305 gnutls_record_set_max_size (gnutls_session_t session
, size_t size
)
308 extension_priv_data_t epriv
;
310 if (session
->security_parameters
.entity
== GNUTLS_SERVER
)
311 return GNUTLS_E_INVALID_REQUEST
;
313 new_size
= _gnutls_mre_record2num (size
);
321 session
->security_parameters
.max_record_send_size
= size
;
324 _gnutls_ext_set_session_data (session
, GNUTLS_EXTENSION_MAX_RECORD_SIZE
,