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
|
/*
* Copyright (C) 2007 Free Software Foundation
*
* Author: Simon Josefsson
*
* This file is part of GNUTLS.
*
* The GNUTLS library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA
*
*/
/* Implementation of Opaque PRF Input:
* http://tools.ietf.org/id/draft-rescorla-tls-opaque-prf-input-00.txt
*
*/
#include "MHD_config.h"
#include <ext_oprfi.h>
#include <gnutls_errors.h>
#include <gnutls_num.h>
static int
oprfi_recv_server (MHD_gtls_session_t session,
const opaque * data, size_t _data_size)
{
ssize_t data_size = _data_size;
uint16_t len;
if (!session->security_parameters.extensions.oprfi_cb)
{
MHD_gnutls_assert ();
return 0;
}
DECR_LEN (data_size, 2);
len = MHD_gtls_read_uint16 (data);
data += 2;
if (len != data_size)
{
MHD_gnutls_assert ();
return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
}
/* Store incoming data. */
session->security_parameters.extensions.oprfi_client_len = len;
session->security_parameters.extensions.oprfi_client =
MHD_gnutls_malloc (len);
if (!session->security_parameters.extensions.oprfi_client)
{
MHD_gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
memcpy (session->security_parameters.extensions.oprfi_client, data, len);
return 0;
}
#if MHD_DEBUG_TLS
static int
oprfi_recv_client (MHD_gtls_session_t session,
const opaque * data, size_t _data_size)
{
ssize_t data_size = _data_size;
uint16_t len;
if (session->security_parameters.extensions.oprfi_client == NULL)
{
MHD_gnutls_assert ();
return 0;
}
DECR_LEN (data_size, 2);
len = MHD_gtls_read_uint16 (data);
data += 2;
if (len != data_size)
{
MHD_gnutls_assert ();
return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
}
if (len != session->security_parameters.extensions.oprfi_client_len)
{
MHD_gnutls_assert ();
return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
}
/* Store incoming data. */
session->security_parameters.extensions.oprfi_server_len = len;
session->security_parameters.extensions.oprfi_server =
MHD_gnutls_malloc (len);
if (!session->security_parameters.extensions.oprfi_server)
{
MHD_gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
memcpy (session->security_parameters.extensions.oprfi_server, data, len);
return 0;
}
#endif
int
MHD_gtls_oprfi_recv_params (MHD_gtls_session_t session,
const opaque * data, size_t data_size)
{
#if MHD_DEBUG_TLS
if (session->security_parameters.entity == GNUTLS_CLIENT)
return oprfi_recv_client (session, data, data_size);
else
#endif
return oprfi_recv_server (session, data, data_size);
}
#if MHD_DEBUG_TLS
static int
oprfi_send_client (MHD_gtls_session_t session, opaque * data,
size_t _data_size)
{
opaque *p = data;
ssize_t data_size = _data_size;
int oprf_size = session->security_parameters.extensions.oprfi_client_len;
if (oprf_size == 0)
return 0;
DECR_LENGTH_RET (data_size, 2, GNUTLS_E_SHORT_MEMORY_BUFFER);
MHD_gtls_write_uint16 (oprf_size, p);
p += 2;
DECR_LENGTH_RET (data_size, oprf_size, GNUTLS_E_SHORT_MEMORY_BUFFER);
memcpy (p, session->security_parameters.extensions.oprfi_client, oprf_size);
return 2 + oprf_size;
}
#endif
static int
oprfi_send_server (MHD_gtls_session_t session, opaque * data,
size_t _data_size)
{
opaque *p = data;
int ret;
ssize_t data_size = _data_size;
if (!session->security_parameters.extensions.oprfi_client ||
!session->security_parameters.extensions.oprfi_cb)
return 0;
/* Allocate buffer for outgoing data. */
session->security_parameters.extensions.oprfi_server_len =
session->security_parameters.extensions.oprfi_client_len;
session->security_parameters.extensions.oprfi_server =
MHD_gnutls_malloc (session->security_parameters.extensions.
oprfi_server_len);
if (!session->security_parameters.extensions.oprfi_server)
{
MHD_gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
/* Get outgoing data. */
ret = session->security_parameters.extensions.oprfi_cb
(session, session->security_parameters.extensions.oprfi_userdata,
session->security_parameters.extensions.oprfi_client_len,
session->security_parameters.extensions.oprfi_client,
session->security_parameters.extensions.oprfi_server);
if (ret < 0)
{
MHD_gnutls_assert ();
MHD_gnutls_free (session->security_parameters.extensions.oprfi_server);
return ret;
}
DECR_LENGTH_RET (data_size, 2, GNUTLS_E_SHORT_MEMORY_BUFFER);
MHD_gtls_write_uint16 (session->security_parameters.extensions.
oprfi_server_len, p);
p += 2;
DECR_LENGTH_RET (data_size,
session->security_parameters.extensions.oprfi_server_len,
GNUTLS_E_SHORT_MEMORY_BUFFER);
memcpy (p, session->security_parameters.extensions.oprfi_server,
session->security_parameters.extensions.oprfi_server_len);
return 2 + session->security_parameters.extensions.oprfi_server_len;
}
int
MHD_gtls_oprfi_send_params (MHD_gtls_session_t session,
opaque * data, size_t data_size)
{
#if MHD_DEBUG_TLS
if (session->security_parameters.entity == GNUTLS_CLIENT)
return oprfi_send_client (session, data, data_size);
else
#endif
return oprfi_send_server (session, data, data_size);
}
|