~ubuntu-branches/ubuntu/intrepid/gnutls26/intrepid-proposed

« back to all changes in this revision

Viewing changes to lib/ext_max_record.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2008-05-01 13:09:49 UTC
  • Revision ID: james.westby@ubuntu.com-20080501130949-qsbsi06stso6a0ij
Tags: upstream-2.2.3~rc
ImportĀ upstreamĀ versionĀ 2.2.3~rc

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2001, 2004, 2005 Free Software Foundation
 
3
 *
 
4
 * Author: Nikos Mavrogiannopoulos
 
5
 *
 
6
 * This file is part of GNUTLS.
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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
 
22
 *
 
23
 */
 
24
 
 
25
/* This file contains the code for the Max Record Size TLS extension.
 
26
 */
 
27
 
 
28
#include "gnutls_int.h"
 
29
#include "gnutls_errors.h"
 
30
#include "gnutls_num.h"
 
31
#include <ext_max_record.h>
 
32
 
 
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.
 
37
 *
 
38
 * In case of a client: If a different max record size (than the default) has
 
39
 * been specified then it sends the extension.
 
40
 *
 
41
 */
 
42
 
 
43
int
 
44
_gnutls_max_record_recv_params (gnutls_session_t session,
 
45
                                const opaque * data, size_t _data_size)
 
46
{
 
47
  ssize_t new_size;
 
48
  ssize_t data_size = _data_size;
 
49
 
 
50
  if (session->security_parameters.entity == GNUTLS_SERVER)
 
51
    {
 
52
      if (data_size > 0)
 
53
        {
 
54
          DECR_LEN (data_size, 1);
 
55
 
 
56
          new_size = _gnutls_mre_num2record (data[0]);
 
57
 
 
58
          if (new_size < 0)
 
59
            {
 
60
              gnutls_assert ();
 
61
              return new_size;
 
62
            }
 
63
 
 
64
          session->security_parameters.max_record_send_size = new_size;
 
65
          session->security_parameters.max_record_recv_size = new_size;
 
66
        }
 
67
    }
 
68
  else
 
69
    {                           /* CLIENT SIDE - we must check if the sent record size is the right one 
 
70
                                 */
 
71
      if (data_size > 0)
 
72
        {
 
73
 
 
74
          if (data_size != 1)
 
75
            {
 
76
              gnutls_assert ();
 
77
              return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
 
78
            }
 
79
 
 
80
          new_size = _gnutls_mre_num2record (data[0]);
 
81
 
 
82
          if (new_size < 0
 
83
              || new_size != session->internals.proposed_record_size)
 
84
            {
 
85
              gnutls_assert ();
 
86
              return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
 
87
            }
 
88
          else
 
89
            {
 
90
              session->security_parameters.max_record_recv_size =
 
91
                session->internals.proposed_record_size;
 
92
            }
 
93
 
 
94
        }
 
95
 
 
96
 
 
97
    }
 
98
 
 
99
  return 0;
 
100
}
 
101
 
 
102
/* returns data_size or a negative number on failure
 
103
 */
 
104
int
 
105
_gnutls_max_record_send_params (gnutls_session_t session, opaque * data,
 
106
                                size_t data_size)
 
107
{
 
108
  uint16_t len;
 
109
  /* this function sends the client extension data (dnsname) */
 
110
  if (session->security_parameters.entity == GNUTLS_CLIENT)
 
111
    {
 
112
 
 
113
      if (session->internals.proposed_record_size != DEFAULT_MAX_RECORD_SIZE)
 
114
        {
 
115
          len = 1;
 
116
          if (data_size < len)
 
117
            {
 
118
              gnutls_assert ();
 
119
              return GNUTLS_E_SHORT_MEMORY_BUFFER;
 
120
            }
 
121
 
 
122
          data[0] =
 
123
            (uint8_t) _gnutls_mre_record2num (session->internals.
 
124
                                              proposed_record_size);
 
125
          return len;
 
126
        }
 
127
 
 
128
    }
 
129
  else
 
130
    {                           /* server side */
 
131
 
 
132
      if (session->security_parameters.max_record_recv_size !=
 
133
          DEFAULT_MAX_RECORD_SIZE)
 
134
        {
 
135
          len = 1;
 
136
          if (data_size < len)
 
137
            {
 
138
              gnutls_assert ();
 
139
              return GNUTLS_E_SHORT_MEMORY_BUFFER;
 
140
            }
 
141
 
 
142
          data[0] =
 
143
            (uint8_t) _gnutls_mre_record2num (session->
 
144
                                              security_parameters.
 
145
                                              max_record_recv_size);
 
146
          return len;
 
147
        }
 
148
 
 
149
 
 
150
    }
 
151
 
 
152
  return 0;
 
153
}
 
154
 
 
155
/* Maps numbers to record sizes according to the
 
156
 * extensions draft.
 
157
 */
 
158
int
 
159
_gnutls_mre_num2record (int num)
 
160
{
 
161
  switch (num)
 
162
    {
 
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;
 
173
    }
 
174
}
 
175
 
 
176
/* Maps record size to numbers according to the
 
177
 * extensions draft.
 
178
 */
 
179
int
 
180
_gnutls_mre_record2num (uint16_t record_size)
 
181
{
 
182
  switch (record_size)
 
183
    {
 
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;
 
194
    }
 
195
 
 
196
}