*** empty log message ***
[gnutls.git] / lib / ext_max_record.c
blob38e0dba6e320670825c91c7eedcc0565bb4b1c88
1 /*
2 * Copyright (C) 2001 Nikos Mavroyanopoulos
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "gnutls_int.h"
23 #include "gnutls_errors.h"
24 #include "gnutls_num.h"
25 #include "ext_max_record.h"
27 /*
28 * In case of a server: if a MAX_RECORD_SIZE extension type is received then it stores
29 * into the state the new value. The server may use gnutls_get_max_record_size(),
30 * in order to access it.
32 * In case of a client: If a different max record size (than the default) has
33 * been specified then it sends the extension.
37 int _gnutls_max_record_recv_params( GNUTLS_STATE state, const opaque* data, int data_size) {
38 size_t new_size;
40 if (state->security_parameters.entity == GNUTLS_SERVER) {
41 if (data_size > 0) {
42 if ( data_size != 1) {
43 gnutls_assert();
44 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
47 new_size = _gnutls_mre_num2record(data[0]);
49 if (new_size < 0) {
50 gnutls_assert();
51 return new_size;
54 state->security_parameters.max_record_size = new_size;
56 } else { /* CLIENT SIDE - we must check if the sent record size is the right one
58 if (data_size > 0) {
60 if ( data_size != 1) {
61 gnutls_assert();
62 return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
65 new_size = _gnutls_mre_num2record(data[0]);
67 if (new_size < 0 || new_size != state->gnutls_internals.proposed_record_size) {
68 gnutls_assert();
69 return GNUTLS_E_ILLEGAL_PARAMETER;
70 } else
71 state->security_parameters.max_record_size = state->gnutls_internals.proposed_record_size;
78 return 0;
81 /* returns data_size or a negative number on failure
82 * data is allocated localy
84 int _gnutls_max_record_send_params( GNUTLS_STATE state, opaque* data, int data_size) {
85 uint16 len;
86 /* this function sends the client extension data (dnsname) */
87 if (state->security_parameters.entity == GNUTLS_CLIENT) {
89 if (state->gnutls_internals.proposed_record_size != DEFAULT_MAX_RECORD_SIZE) {
90 gnutls_assert();
92 len = 1;
93 if (data_size < len) {
94 gnutls_assert();
95 return GNUTLS_E_INVALID_REQUEST;
98 data[0] = _gnutls_mre_record2num( state->gnutls_internals.proposed_record_size);
99 return len;
102 } else { /* server side */
104 if (state->security_parameters.max_record_size != DEFAULT_MAX_RECORD_SIZE) {
105 len = 1;
106 if (data_size < len) {
107 gnutls_assert();
108 return GNUTLS_E_INVALID_REQUEST;
111 data[0] = _gnutls_mre_record2num( state->security_parameters.max_record_size);
112 return len;
118 return 0;
121 /* Maps numbers to record sizes according to the
122 * extensions draft.
124 int _gnutls_mre_num2record( int num) {
125 switch( num) {
126 case 1:
127 return 512;
128 case 2:
129 return 1024;
130 case 3:
131 return 2048;
132 case 4:
133 return 4096;
134 default:
135 return GNUTLS_E_ILLEGAL_PARAMETER;
139 /* Maps record size to numbers according to the
140 * extensions draft.
142 int _gnutls_mre_record2num( int record_size) {
143 switch(record_size) {
144 case 512:
145 return 1;
146 case 1024:
147 return 2;
148 case 2048:
149 return 3;
150 case 4096:
151 return 4;
152 default:
153 return GNUTLS_E_ILLEGAL_PARAMETER;