Fix use of deprecated types, for now and the future.
[gnutls.git] / lib / ext_max_record.c
blob61f5b3264df0c019c7b24b2c1afde7ac75396e47
1 /*
2 * Copyright (C) 2001, 2004, 2005 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
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.
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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 /* This file contains the code for the Max Record Size TLS extension.
28 #include "gnutls_int.h"
29 #include "gnutls_errors.h"
30 #include "gnutls_num.h"
31 #include <ext_max_record.h>
33 /*
34 * In case of a server: if a MAX_RECORD_SIZE extension type is received then it stores
35 * into the session the new value. The server may use gnutls_get_max_record_size(),
36 * in order to access it.
38 * In case of a client: If a different max record size (than the default) has
39 * been specified then it sends the extension.
43 int
44 _gnutls_max_record_recv_params (gnutls_session_t session,
45 const opaque * data, size_t _data_size)
47 ssize_t new_size;
48 ssize_t data_size = _data_size;
50 if (session->security_parameters.entity == GNUTLS_SERVER)
52 if (data_size > 0)
54 DECR_LEN (data_size, 1);
56 new_size = _gnutls_mre_num2record (data[0]);
58 if (new_size < 0)
60 gnutls_assert ();
61 return new_size;
64 session->security_parameters.max_record_send_size = new_size;
65 session->security_parameters.max_record_recv_size = new_size;
68 else
69 { /* CLIENT SIDE - we must check if the sent record size is the right one
71 if (data_size > 0)
74 if (data_size != 1)
76 gnutls_assert ();
77 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
80 new_size = _gnutls_mre_num2record (data[0]);
82 if (new_size < 0
83 || new_size != session->internals.proposed_record_size)
85 gnutls_assert ();
86 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
88 else
90 session->security_parameters.max_record_recv_size =
91 session->internals.proposed_record_size;
99 return 0;
102 /* returns data_size or a negative number on failure
105 _gnutls_max_record_send_params (gnutls_session_t session, opaque * data,
106 size_t data_size)
108 uint16_t len;
109 /* this function sends the client extension data (dnsname) */
110 if (session->security_parameters.entity == GNUTLS_CLIENT)
113 if (session->internals.proposed_record_size != DEFAULT_MAX_RECORD_SIZE)
115 len = 1;
116 if (data_size < len)
118 gnutls_assert ();
119 return GNUTLS_E_SHORT_MEMORY_BUFFER;
122 data[0] =
123 (uint8_t) _gnutls_mre_record2num (session->internals.
124 proposed_record_size);
125 return len;
129 else
130 { /* server side */
132 if (session->security_parameters.max_record_recv_size !=
133 DEFAULT_MAX_RECORD_SIZE)
135 len = 1;
136 if (data_size < len)
138 gnutls_assert ();
139 return GNUTLS_E_SHORT_MEMORY_BUFFER;
142 data[0] =
143 (uint8_t)
144 _gnutls_mre_record2num
145 (session->security_parameters.max_record_recv_size);
146 return len;
152 return 0;
155 /* Maps numbers to record sizes according to the
156 * extensions draft.
159 _gnutls_mre_num2record (int num)
161 switch (num)
163 case 1:
164 return 512;
165 case 2:
166 return 1024;
167 case 3:
168 return 2048;
169 case 4:
170 return 4096;
171 default:
172 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
176 /* Maps record size to numbers according to the
177 * extensions draft.
180 _gnutls_mre_record2num (uint16_t record_size)
182 switch (record_size)
184 case 512:
185 return 1;
186 case 1024:
187 return 2;
188 case 2048:
189 return 3;
190 case 4096:
191 return 4;
192 default:
193 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;